From a899505377ae649152f1781fd0addb586d838c66 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 1 Feb 2021 19:31:36 +0100 Subject: [PATCH 1/7] errcheck: libcontainer/integration Signed-off-by: Sebastiaan van Stijn --- libcontainer/integration/checkpoint_test.go | 31 +++-- libcontainer/integration/exec_test.go | 122 ++++++++++---------- libcontainer/integration/execin_test.go | 72 ++++++------ libcontainer/integration/utils_test.go | 8 +- 4 files changed, 118 insertions(+), 115 deletions(-) diff --git a/libcontainer/integration/checkpoint_test.go b/libcontainer/integration/checkpoint_test.go index 9ec03b92d..2b93e42af 100644 --- a/libcontainer/integration/checkpoint_test.go +++ b/libcontainer/integration/checkpoint_test.go @@ -14,16 +14,16 @@ import ( "golang.org/x/sys/unix" ) -func showFile(t *testing.T, fname string) error { +func showFile(t *testing.T, fname string) { t.Helper() t.Logf("=== %s ===\n", fname) f, err := os.Open(fname) if err != nil { t.Log(err) - return err + return } - defer f.Close() + defer f.Close() //nolint: errcheck scanner := bufio.NewScanner(f) for scanner.Scan() { @@ -31,12 +31,11 @@ func showFile(t *testing.T, fname string) error { } if err := scanner.Err(); err != nil { - return err + t.Log(err) + return } t.Logf("=== END ===\n") - - return nil } func TestUsernsCheckpoint(t *testing.T) { @@ -65,7 +64,7 @@ func testCheckpoint(t *testing.T, userns bool) { root, err := newTestRoot() ok(t, err) - defer os.RemoveAll(root) + defer remove(root) rootfs, err := newRootfs() ok(t, err) @@ -80,7 +79,7 @@ func testCheckpoint(t *testing.T, userns bool) { container, err := factory.Create("test", config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) stdinR, stdinW, err := os.Pipe() ok(t, err) @@ -97,8 +96,8 @@ func testCheckpoint(t *testing.T, userns bool) { } err = container.Run(&pconfig) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) pid, err := pconfig.Pid() @@ -109,7 +108,7 @@ func testCheckpoint(t *testing.T, userns bool) { parentDir, err := ioutil.TempDir("", "criu-parent") ok(t, err) - defer os.RemoveAll(parentDir) + defer remove(parentDir) preDumpOpts := &libcontainer.CriuOpts{ ImagesDirectory: parentDir, @@ -132,7 +131,7 @@ func testCheckpoint(t *testing.T, userns bool) { imagesDir, err := ioutil.TempDir("", "criu") ok(t, err) - defer os.RemoveAll(imagesDir) + defer remove(imagesDir) checkpointOpts := &libcontainer.CriuOpts{ ImagesDirectory: imagesDir, @@ -154,7 +153,7 @@ func testCheckpoint(t *testing.T, userns bool) { t.Fatal("Unexpected state checkpoint: ", state) } - stdinW.Close() + _ = stdinW.Close() _, err = process.Wait() ok(t, err) @@ -174,8 +173,8 @@ func testCheckpoint(t *testing.T, userns bool) { } err = container.Restore(restoreProcessConfig, checkpointOpts) - restoreStdinR.Close() - defer restoreStdinW.Close() + _ = restoreStdinR.Close() + defer restoreStdinW.Close() //nolint: errcheck if err != nil { showFile(t, restoreLog) t.Fatal(err) @@ -196,7 +195,7 @@ func testCheckpoint(t *testing.T, userns bool) { _, err = restoreStdinW.WriteString("Hello!") ok(t, err) - restoreStdinW.Close() + _ = restoreStdinW.Close() waitProcess(restoreProcessConfig, t) output := restoreStdout.String() diff --git a/libcontainer/integration/exec_test.go b/libcontainer/integration/exec_test.go index 505006a89..9763a6c13 100644 --- a/libcontainer/integration/exec_test.go +++ b/libcontainer/integration/exec_test.go @@ -213,7 +213,7 @@ func TestEnter(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) // Execute a first process in the container stdinR, stdinW, err := os.Pipe() @@ -230,8 +230,8 @@ func TestEnter(t *testing.T) { Init: true, } err = container.Run(&pconfig) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) pid, err := pconfig.Pid() ok(t, err) @@ -248,8 +248,8 @@ func TestEnter(t *testing.T) { pconfig2.Stdout = &stdout2 err = container.Run(&pconfig2) - stdinR2.Close() - defer stdinW2.Close() + _ = stdinR2.Close() + defer stdinW2.Close() //nolint: errcheck ok(t, err) pid2, err := pconfig2.Pid() @@ -269,10 +269,10 @@ func TestEnter(t *testing.T) { } // Wait processes - stdinW2.Close() + _ = stdinW2.Close() waitProcess(&pconfig2, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(&pconfig, t) // Check that both processes live in the same pidns @@ -300,7 +300,7 @@ func TestProcessEnv(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) var stdout bytes.Buffer pconfig := libcontainer.Process{ @@ -349,7 +349,7 @@ func TestProcessEmptyCaps(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) var stdout bytes.Buffer pconfig := libcontainer.Process{ @@ -397,7 +397,7 @@ func TestProcessCaps(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) var stdout bytes.Buffer pconfig := libcontainer.Process{ @@ -462,7 +462,7 @@ func TestAdditionalGroups(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) var stdout bytes.Buffer pconfig := libcontainer.Process{ @@ -518,7 +518,7 @@ func testFreeze(t *testing.T, systemd bool) { }) container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) stdinR, stdinW, err := os.Pipe() ok(t, err) @@ -531,8 +531,8 @@ func testFreeze(t *testing.T, systemd bool) { Init: true, } err = container.Run(pconfig) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) err = container.Pause() @@ -545,7 +545,7 @@ func testFreeze(t *testing.T, systemd bool) { t.Fatal("Unexpected state: ", state) } - stdinW.Close() + _ = stdinW.Close() waitProcess(pconfig, t) } @@ -826,7 +826,7 @@ func TestContainerState(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) stdinR, stdinW, err := os.Pipe() ok(t, err) @@ -840,8 +840,8 @@ func TestContainerState(t *testing.T) { } err = container.Run(p) ok(t, err) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck st, err := container.State() ok(t, err) @@ -851,7 +851,7 @@ func TestContainerState(t *testing.T) { if l1 != l { t.Fatal("Container using non-host ipc namespace") } - stdinW.Close() + _ = stdinW.Close() waitProcess(p, t) } @@ -868,7 +868,7 @@ func TestPassExtraFiles(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) var stdout bytes.Buffer pipeout1, pipein1, err := os.Pipe() @@ -921,7 +921,7 @@ func TestMountCmds(t *testing.T) { tmpDir, err := ioutil.TempDir("", "tmpdir") ok(t, err) - defer os.RemoveAll(tmpDir) + defer remove(tmpDir) config := newTemplateConfig(t, &tParam{rootfs: rootfs}) config.Mounts = append(config.Mounts, &configs.Mount{ @@ -941,7 +941,7 @@ func TestMountCmds(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) pconfig := libcontainer.Process{ Cwd: "/", @@ -981,7 +981,7 @@ func TestSysctl(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) var stdout bytes.Buffer pconfig := libcontainer.Process{ @@ -1110,7 +1110,7 @@ func TestOomScoreAdj(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) var stdout bytes.Buffer pconfig := libcontainer.Process{ @@ -1305,8 +1305,8 @@ func TestSTDIOPermissions(t *testing.T) { } } -func unmountOp(path string) error { - return unix.Unmount(path, unix.MNT_DETACH) +func unmountOp(path string) { + _ = unix.Unmount(path, unix.MNT_DETACH) } // Launch container with rootfsPropagation in rslave mode. Also @@ -1333,7 +1333,7 @@ func TestRootfsPropagationSlaveMount(t *testing.T) { // Bind mount a volume dir1host, err := ioutil.TempDir("", "mnt1host") ok(t, err) - defer os.RemoveAll(dir1host) + defer remove(dir1host) // Make this dir a "shared" mount point. This will make sure a // slave relationship can be established in container. @@ -1351,7 +1351,7 @@ func TestRootfsPropagationSlaveMount(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) stdinR, stdinW, err := os.Pipe() ok(t, err) @@ -1365,15 +1365,15 @@ func TestRootfsPropagationSlaveMount(t *testing.T) { } err = container.Run(pconfig) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) // Create mnt1host/mnt2host and bind mount itself on top of it. This // should be visible in container. dir2host, err := ioutil.TempDir(dir1host, "mnt2host") ok(t, err) - defer os.RemoveAll(dir2host) + defer remove(dir2host) err = unix.Mount(dir2host, dir2host, "bind", unix.MS_BIND, "") defer unmountOp(dir2host) @@ -1394,13 +1394,13 @@ func TestRootfsPropagationSlaveMount(t *testing.T) { } err = container.Run(pconfig2) - stdinR2.Close() - defer stdinW2.Close() + _ = stdinR2.Close() + defer stdinW2.Close() //nolint: errcheck ok(t, err) - stdinW2.Close() + _ = stdinW2.Close() waitProcess(pconfig2, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(pconfig, t) mountPropagated = false @@ -1448,7 +1448,7 @@ func TestRootfsPropagationSharedMount(t *testing.T) { // Bind mount a volume dir1host, err := ioutil.TempDir("", "mnt1host") ok(t, err) - defer os.RemoveAll(dir1host) + defer remove(dir1host) // Make this dir a "shared" mount point. This will make sure a // shared relationship can be established in container. @@ -1466,7 +1466,7 @@ func TestRootfsPropagationSharedMount(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) stdinR, stdinW, err := os.Pipe() ok(t, err) @@ -1480,8 +1480,8 @@ func TestRootfsPropagationSharedMount(t *testing.T) { } err = container.Run(pconfig) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) // Create mnt1host/mnt2cont. This will become visible inside container @@ -1489,7 +1489,7 @@ func TestRootfsPropagationSharedMount(t *testing.T) { // should be visible on host now. dir2host, err := ioutil.TempDir(dir1host, "mnt2cont") ok(t, err) - defer os.RemoveAll(dir2host) + defer remove(dir2host) dir2cont = filepath.Join(dir1cont, filepath.Base(dir2host)) @@ -1515,14 +1515,14 @@ func TestRootfsPropagationSharedMount(t *testing.T) { pconfig2.Capabilities.Inheritable = append(config.Capabilities.Inheritable, "CAP_SYS_ADMIN") err = container.Run(pconfig2) - stdinR2.Close() - defer stdinW2.Close() + _ = stdinR2.Close() + defer stdinW2.Close() //nolint: errcheck ok(t, err) // Wait for process - stdinW2.Close() + _ = stdinW2.Close() waitProcess(pconfig2, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(pconfig, t) defer unmountOp(dir2host) @@ -1629,7 +1629,7 @@ func TestInitJoinPID(t *testing.T) { // Execute a long-running container container1, err := newContainer(t, newTemplateConfig(t, &tParam{rootfs: rootfs})) ok(t, err) - defer container1.Destroy() + defer destroyContainer(container1) stdinR1, stdinW1, err := os.Pipe() ok(t, err) @@ -1641,8 +1641,8 @@ func TestInitJoinPID(t *testing.T) { Init: true, } err = container1.Run(init1) - stdinR1.Close() - defer stdinW1.Close() + _ = stdinR1.Close() + defer stdinW1.Close() //nolint: errcheck ok(t, err) // get the state of the first container @@ -1656,7 +1656,7 @@ func TestInitJoinPID(t *testing.T) { config2.Cgroups.Path = "integration/test2" container2, err := newContainer(t, config2) ok(t, err) - defer container2.Destroy() + defer destroyContainer(container2) stdinR2, stdinW2, err := os.Pipe() ok(t, err) @@ -1668,8 +1668,8 @@ func TestInitJoinPID(t *testing.T) { Init: true, } err = container2.Run(init2) - stdinR2.Close() - defer stdinW2.Close() + _ = stdinR2.Close() + defer stdinW2.Close() //nolint: errcheck ok(t, err) // get the state of the second container state2, err := container2.State() @@ -1703,9 +1703,9 @@ func TestInitJoinPID(t *testing.T) { // Stop init processes one by one. Stop the second container should // not stop the first. - stdinW2.Close() + _ = stdinW2.Close() waitProcess(init2, t) - stdinW1.Close() + _ = stdinW1.Close() waitProcess(init1, t) out := strings.TrimSpace(buffers.Stdout.String()) @@ -1736,7 +1736,7 @@ func TestInitJoinNetworkAndUser(t *testing.T) { }) container1, err := newContainer(t, config1) ok(t, err) - defer container1.Destroy() + defer destroyContainer(container1) stdinR1, stdinW1, err := os.Pipe() ok(t, err) @@ -1748,8 +1748,8 @@ func TestInitJoinNetworkAndUser(t *testing.T) { Init: true, } err = container1.Run(init1) - stdinR1.Close() - defer stdinW1.Close() + _ = stdinR1.Close() + defer stdinW1.Close() //nolint: errcheck ok(t, err) // get the state of the first container @@ -1772,7 +1772,7 @@ func TestInitJoinNetworkAndUser(t *testing.T) { config2.Cgroups.Path = "integration/test2" container2, err := newContainer(t, config2) ok(t, err) - defer container2.Destroy() + defer destroyContainer(container2) stdinR2, stdinW2, err := os.Pipe() ok(t, err) @@ -1784,8 +1784,8 @@ func TestInitJoinNetworkAndUser(t *testing.T) { Init: true, } err = container2.Run(init2) - stdinR2.Close() - defer stdinW2.Close() + _ = stdinR2.Close() + defer stdinW2.Close() //nolint: errcheck ok(t, err) // get the state of the second container @@ -1809,9 +1809,9 @@ func TestInitJoinNetworkAndUser(t *testing.T) { } // Stop init processes one by one. Stop the second container should // not stop the first. - stdinW2.Close() + _ = stdinW2.Close() waitProcess(init2, t) - stdinW1.Close() + _ = stdinW1.Close() waitProcess(init1, t) } @@ -1835,7 +1835,7 @@ func TestTmpfsCopyUp(t *testing.T) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) var stdout bytes.Buffer pconfig := libcontainer.Process{ diff --git a/libcontainer/integration/execin_test.go b/libcontainer/integration/execin_test.go index 894e54f36..a889cc027 100644 --- a/libcontainer/integration/execin_test.go +++ b/libcontainer/integration/execin_test.go @@ -28,7 +28,7 @@ func TestExecIn(t *testing.T) { config := newTemplateConfig(t, &tParam{rootfs: rootfs}) container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) // Execute a first process in the container stdinR, stdinW, err := os.Pipe() @@ -41,8 +41,8 @@ func TestExecIn(t *testing.T) { Init: true, } err = container.Run(process) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) buffers := newStdBuffers() @@ -58,7 +58,7 @@ func TestExecIn(t *testing.T) { err = container.Run(ps) ok(t, err) waitProcess(ps, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(process, t) out := buffers.Stdout.String() @@ -98,7 +98,7 @@ func testExecInRlimit(t *testing.T, userns bool) { container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) stdinR, stdinW, err := os.Pipe() ok(t, err) @@ -110,8 +110,8 @@ func testExecInRlimit(t *testing.T, userns bool) { Init: true, } err = container.Run(process) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) buffers := newStdBuffers() @@ -132,7 +132,7 @@ func testExecInRlimit(t *testing.T, userns bool) { ok(t, err) waitProcess(ps, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(process, t) out := buffers.Stdout.String() @@ -153,7 +153,7 @@ func TestExecInAdditionalGroups(t *testing.T) { config := newTemplateConfig(t, &tParam{rootfs: rootfs}) container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) // Execute a first process in the container stdinR, stdinW, err := os.Pipe() @@ -166,8 +166,8 @@ func TestExecInAdditionalGroups(t *testing.T) { Init: true, } err = container.Run(process) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) var stdout bytes.Buffer @@ -185,7 +185,7 @@ func TestExecInAdditionalGroups(t *testing.T) { // Wait for process waitProcess(&pconfig, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(process, t) outputGroups := stdout.String() @@ -210,7 +210,7 @@ func TestExecInError(t *testing.T) { config := newTemplateConfig(t, &tParam{rootfs: rootfs}) container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) // Execute a first process in the container stdinR, stdinW, err := os.Pipe() @@ -223,9 +223,9 @@ func TestExecInError(t *testing.T) { Init: true, } err = container.Run(process) - stdinR.Close() + _ = stdinR.Close() defer func() { - stdinW.Close() + _ = stdinW.Close() if _, err := process.Wait(); err != nil { t.Log(err) } @@ -264,7 +264,7 @@ func TestExecInTTY(t *testing.T) { config := newTemplateConfig(t, &tParam{rootfs: rootfs}) container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) // Execute a first process in the container stdinR, stdinW, err := os.Pipe() @@ -277,9 +277,9 @@ func TestExecInTTY(t *testing.T) { Init: true, } err = container.Run(process) - stdinR.Close() + _ = stdinR.Close() defer func() { - stdinW.Close() + _ = stdinW.Close() if _, err := process.Wait(); err != nil { t.Log(err) } @@ -335,8 +335,8 @@ func TestExecInTTY(t *testing.T) { } waitProcess(ps, t) - parent.Close() - child.Close() + _ = parent.Close() + _ = child.Close() out := stdout.String() if !strings.Contains(out, "cat") || !strings.Contains(out, "ps") { @@ -358,7 +358,7 @@ func TestExecInEnvironment(t *testing.T) { config := newTemplateConfig(t, &tParam{rootfs: rootfs}) container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) // Execute a first process in the container stdinR, stdinW, err := os.Pipe() @@ -371,8 +371,8 @@ func TestExecInEnvironment(t *testing.T) { Init: true, } err = container.Run(process) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) buffers := newStdBuffers() @@ -394,7 +394,7 @@ func TestExecInEnvironment(t *testing.T) { ok(t, err) waitProcess(process2, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(process, t) out := buffers.Stdout.String() @@ -419,7 +419,7 @@ func TestExecinPassExtraFiles(t *testing.T) { config := newTemplateConfig(t, &tParam{rootfs: rootfs}) container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) // Execute a first process in the container stdinR, stdinW, err := os.Pipe() @@ -432,8 +432,8 @@ func TestExecinPassExtraFiles(t *testing.T) { Init: true, } err = container.Run(process) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) var stdout bytes.Buffer @@ -453,7 +453,7 @@ func TestExecinPassExtraFiles(t *testing.T) { ok(t, err) waitProcess(inprocess, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(process, t) out := stdout.String() @@ -488,7 +488,7 @@ func TestExecInOomScoreAdj(t *testing.T) { config.OomScoreAdj = ptrInt(200) container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) stdinR, stdinW, err := os.Pipe() ok(t, err) @@ -500,8 +500,8 @@ func TestExecInOomScoreAdj(t *testing.T) { Init: true, } err = container.Run(process) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) buffers := newStdBuffers() @@ -517,7 +517,7 @@ func TestExecInOomScoreAdj(t *testing.T) { ok(t, err) waitProcess(ps, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(process, t) out := buffers.Stdout.String() @@ -542,7 +542,7 @@ func TestExecInUserns(t *testing.T) { }) container, err := newContainer(t, config) ok(t, err) - defer container.Destroy() + defer destroyContainer(container) // Execute a first process in the container stdinR, stdinW, err := os.Pipe() @@ -556,8 +556,8 @@ func TestExecInUserns(t *testing.T) { Init: true, } err = container.Run(process) - stdinR.Close() - defer stdinW.Close() + _ = stdinR.Close() + defer stdinW.Close() //nolint: errcheck ok(t, err) initPID, err := process.Pid() @@ -578,7 +578,7 @@ func TestExecInUserns(t *testing.T) { err = container.Run(process2) ok(t, err) waitProcess(process2, t) - stdinW.Close() + _ = stdinW.Close() waitProcess(process, t) if out := strings.TrimSpace(buffers.Stdout.String()); out != initUserns { diff --git a/libcontainer/integration/utils_test.go b/libcontainer/integration/utils_test.go index 75203d6f5..f3d176bf4 100644 --- a/libcontainer/integration/utils_test.go +++ b/libcontainer/integration/utils_test.go @@ -137,7 +137,7 @@ func newRootfs() (string, error) { } func remove(dir string) { - os.RemoveAll(dir) + _ = os.RemoveAll(dir) } // copyBusybox copies the rootfs for a busybox container created for the test image @@ -179,7 +179,7 @@ func runContainer(t *testing.T, config *configs.Config, console string, args ... if err != nil { return nil, -1, err } - defer container.Destroy() + defer destroyContainer(container) buffers = newStdBuffers() process := &libcontainer.Process{ Cwd: "/", @@ -209,3 +209,7 @@ func runContainer(t *testing.T, config *configs.Config, console string, args ... } return } + +func destroyContainer(container libcontainer.Container) { + _ = container.Destroy() +} From 7e7ff8722add9399dab2b3694ca14ff33fb475c2 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 1 Feb 2021 19:32:45 +0100 Subject: [PATCH 2/7] errcheck: libcontainer/configs Signed-off-by: Sebastiaan van Stijn --- libcontainer/configs/config.go | 2 +- libcontainer/configs/config_test.go | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/libcontainer/configs/config.go b/libcontainer/configs/config.go index 14a096038..c6b259e9f 100644 --- a/libcontainer/configs/config.go +++ b/libcontainer/configs/config.go @@ -387,7 +387,7 @@ func (c Command) Run(s *specs.State) error { case err := <-errC: return err case <-timerCh: - cmd.Process.Kill() + _ = cmd.Process.Kill() <-errC return fmt.Errorf("hook ran past specified timeout of %.1fs", c.Timeout.Seconds()) } diff --git a/libcontainer/configs/config_test.go b/libcontainer/configs/config_test.go index 97286740f..a270f3780 100644 --- a/libcontainer/configs/config_test.go +++ b/libcontainer/configs/config_test.go @@ -145,12 +145,15 @@ func TestFuncHookRun(t *testing.T) { fHook := configs.NewFunctionHook(func(s *specs.State) error { if !reflect.DeepEqual(state, s) { - t.Errorf("Expected state %+v to equal %+v", state, s) + return fmt.Errorf("expected state %+v to equal %+v", state, s) } return nil }) - fHook.Run(state) + err := fHook.Run(state) + if err != nil { + t.Fatal(err) + } } func TestCommandHookRun(t *testing.T) { From 463ee5e19aea7e937aa02c735620207bf11ea1f5 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 1 Feb 2021 19:33:24 +0100 Subject: [PATCH 3/7] errcheck: libcontainer/nsenter Signed-off-by: Sebastiaan van Stijn --- libcontainer/nsenter/nsenter_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libcontainer/nsenter/nsenter_test.go b/libcontainer/nsenter/nsenter_test.go index 83340913b..9627a61fe 100644 --- a/libcontainer/nsenter/nsenter_test.go +++ b/libcontainer/nsenter/nsenter_test.go @@ -84,7 +84,7 @@ func TestNsenterValidPaths(t *testing.T) { if err != nil { t.Fatalf("%v", err) } - p.Wait() + _, _ = p.Wait() } func TestNsenterInvalidPaths(t *testing.T) { @@ -179,8 +179,10 @@ func TestNsenterChildLogging(t *testing.T) { if err != nil { t.Fatalf("failed to create log pipe %v", err) } - defer logread.Close() - defer logwrite.Close() + defer func() { + _ = logwrite.Close() + _ = logread.Close() + }() namespaces := []string{ // join pid ns of the current process From b45fbd43b8f120389ae978e9f856ac07f60d0663 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 1 Feb 2021 19:33:47 +0100 Subject: [PATCH 4/7] errcheck: libcontainer Signed-off-by: Sebastiaan van Stijn --- libcontainer/container_linux.go | 4 ++-- libcontainer/factory_linux.go | 6 +++++- libcontainer/factory_linux_test.go | 11 +++++++---- libcontainer/process_linux.go | 30 ++++++++++++++--------------- libcontainer/rootfs_linux.go | 16 +++++++-------- libcontainer/setns_init_linux.go | 4 ++-- libcontainer/standard_init_linux.go | 8 ++++---- libcontainer/state_linux.go | 2 +- 8 files changed, 44 insertions(+), 37 deletions(-) diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index 849bf4a61..5b65f8896 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -1352,7 +1352,7 @@ func (c *linuxContainer) Restore(process *Process, criuOpts *CriuOpts) error { if err != nil { return err } - defer unix.Unmount(root, unix.MNT_DETACH) + defer unix.Unmount(root, unix.MNT_DETACH) //nolint: errcheck t := criurpc.CriuReqType_RESTORE req := &criurpc.CriuReq{ Type: &t, @@ -1665,7 +1665,7 @@ func (c *linuxContainer) criuSwrk(process *Process, req *criurpc.CriuReq, opts * break } - criuClientCon.CloseWrite() + _ = criuClientCon.CloseWrite() // cmd.Wait() waits cmd.goroutines which are used for proxying file descriptors. // Here we want to wait only the CRIU process. criuProcessState, err = criuProcess.Wait() diff --git a/libcontainer/factory_linux.go b/libcontainer/factory_linux.go index 972b0f2b5..b3beba81e 100644 --- a/libcontainer/factory_linux.go +++ b/libcontainer/factory_linux.go @@ -196,7 +196,11 @@ func New(root string, options ...func(*LinuxFactory) error) (Factory, error) { Validator: validate.New(), CriuPath: "criu", } - Cgroupfs(l) + + if err := Cgroupfs(l); err != nil { + return nil, err + } + for _, opt := range options { if opt == nil { continue diff --git a/libcontainer/factory_linux_test.go b/libcontainer/factory_linux_test.go index 665a8a7c4..0b88c3d32 100644 --- a/libcontainer/factory_linux_test.go +++ b/libcontainer/factory_linux_test.go @@ -122,7 +122,10 @@ func TestFactoryNewTmpfs(t *testing.T) { if m.Source != "tmpfs" { t.Fatalf("Source of root: %s, expected %s", m.Source, "tmpfs") } - unix.Unmount(root, unix.MNT_DETACH) + err = unix.Unmount(root, unix.MNT_DETACH) + if err != nil { + t.Error("failed to unmount root:", err) + } } func TestFactoryLoadNotExists(t *testing.T) { @@ -130,7 +133,7 @@ func TestFactoryLoadNotExists(t *testing.T) { if rerr != nil { t.Fatal(rerr) } - defer os.RemoveAll(root) + defer os.RemoveAll(root) //nolint: errcheck factory, err := New(root, Cgroupfs) if err != nil { t.Fatal(err) @@ -153,7 +156,7 @@ func TestFactoryLoadContainer(t *testing.T) { if err != nil { t.Fatal(err) } - defer os.RemoveAll(root) + defer os.RemoveAll(root) //nolint: errcheck // setup default container config and state for mocking var ( id = "1" @@ -219,7 +222,7 @@ func marshal(path string, v interface{}) error { if err != nil { return err } - defer f.Close() + defer f.Close() //nolint: errcheck return utils.WriteJSON(f, v) } diff --git a/libcontainer/process_linux.go b/libcontainer/process_linux.go index 053971b91..80f25e334 100644 --- a/libcontainer/process_linux.go +++ b/libcontainer/process_linux.go @@ -188,7 +188,7 @@ func (p *setnsProcess) start() (retErr error) { } // Must be done after Shutdown so the child will exit and we can wait for it. if ierr != nil { - p.wait() + _, _ = p.wait() return ierr } return nil @@ -201,16 +201,16 @@ func (p *setnsProcess) start() (retErr error) { func (p *setnsProcess) execSetns() error { status, err := p.cmd.Process.Wait() if err != nil { - p.cmd.Wait() + _ = p.cmd.Wait() return newSystemErrorWithCause(err, "waiting on setns process to finish") } if !status.Success() { - p.cmd.Wait() + _ = p.cmd.Wait() return newSystemError(&exec.ExitError{ProcessState: status}) } var pid *pid if err := json.NewDecoder(p.messageSockPair.parent).Decode(&pid); err != nil { - p.cmd.Wait() + _ = p.cmd.Wait() return newSystemErrorWithCause(err, "reading pid from init pipe") } @@ -292,7 +292,7 @@ func (p *initProcess) externalDescriptors() []string { func (p *initProcess) getChildPid() (int, error) { var pid pid if err := json.NewDecoder(p.messageSockPair.parent).Decode(&pid); err != nil { - p.cmd.Wait() + _ = p.cmd.Wait() return -1, err } @@ -309,11 +309,11 @@ func (p *initProcess) getChildPid() (int, error) { func (p *initProcess) waitForChildExit(childPid int) error { status, err := p.cmd.Process.Wait() if err != nil { - p.cmd.Wait() + _ = p.cmd.Wait() return err } if !status.Success() { - p.cmd.Wait() + _ = p.cmd.Wait() return &exec.ExitError{ProcessState: status} } @@ -327,12 +327,12 @@ func (p *initProcess) waitForChildExit(childPid int) error { } func (p *initProcess) start() (retErr error) { - defer p.messageSockPair.parent.Close() + defer p.messageSockPair.parent.Close() //nolint: errcheck err := p.cmd.Start() p.process.ops = p // close the write-side of the pipes (controlled by child) - p.messageSockPair.child.Close() - p.logFilePair.child.Close() + _ = p.messageSockPair.child.Close() + _ = p.logFilePair.child.Close() if err != nil { p.process.ops = nil return newSystemErrorWithCause(err, "starting init process command") @@ -371,9 +371,9 @@ func (p *initProcess) start() (retErr error) { logrus.WithError(err).Warn("unable to terminate initProcess") } - p.manager.Destroy() + _ = p.manager.Destroy() if p.intelRdtManager != nil { - p.intelRdtManager.Destroy() + _ = p.intelRdtManager.Destroy() } } }() @@ -553,7 +553,7 @@ func (p *initProcess) start() (retErr error) { // Must be done after Shutdown so the child will exit and we can wait for it. if ierr != nil { - p.wait() + _, _ = p.wait() return ierr } return nil @@ -563,7 +563,7 @@ func (p *initProcess) wait() (*os.ProcessState, error) { err := p.cmd.Wait() // we should kill all processes in cgroup when init is died if we use host PID namespace if p.sharePidns { - signalAllProcesses(p.manager, unix.SIGKILL) + _ = signalAllProcesses(p.manager, unix.SIGKILL) } return p.cmd.ProcessState, err } @@ -668,7 +668,7 @@ func (p *Process) InitializeIO(rootuid, rootgid int) (i *IO, err error) { defer func() { if err != nil { for _, fd := range fds { - unix.Close(int(fd)) + _ = unix.Close(int(fd)) } } }() diff --git a/libcontainer/rootfs_linux.go b/libcontainer/rootfs_linux.go index d9c5146dd..ff91b3dbc 100644 --- a/libcontainer/rootfs_linux.go +++ b/libcontainer/rootfs_linux.go @@ -196,9 +196,9 @@ func prepareTmp(topTmpDir string) (string, error) { return tmpdir, nil } -func cleanupTmp(tmpdir string) error { - unix.Unmount(tmpdir, 0) - return os.RemoveAll(tmpdir) +func cleanupTmp(tmpdir string) { + _ = unix.Unmount(tmpdir, 0) + _ = os.RemoveAll(tmpdir) } func mountCmd(cmd configs.Command) error { @@ -615,7 +615,7 @@ func reOpenDevNull() error { if err != nil { return fmt.Errorf("Failed to open /dev/null - %s", err) } - defer file.Close() + defer file.Close() //nolint: errcheck if err := unix.Fstat(int(file.Fd()), &devNullStat); err != nil { return err } @@ -661,7 +661,7 @@ func bindMountDeviceNode(rootfs, dest string, node *devices.Device) error { return err } if f != nil { - f.Close() + _ = f.Close() } return utils.WithProcfd(rootfs, dest, func(procfd string) error { return unix.Mount(node.Path, procfd, "bind", unix.MS_BIND, "") @@ -826,13 +826,13 @@ func pivotRoot(rootfs string) error { if err != nil { return err } - defer unix.Close(oldroot) + defer unix.Close(oldroot) //nolint: errcheck newroot, err := unix.Open(rootfs, unix.O_DIRECTORY|unix.O_RDONLY, 0) if err != nil { return err } - defer unix.Close(newroot) + defer unix.Close(newroot) //nolint: errcheck // Change to the new root so that the pivot_root actually acts on it. if err := unix.Fchdir(newroot); err != nil { @@ -956,7 +956,7 @@ func createIfNotExists(path string, isDir bool) error { if err != nil { return err } - f.Close() + _ = f.Close() } } return nil diff --git a/libcontainer/setns_init_linux.go b/libcontainer/setns_init_linux.go index 97987f1d0..2d7e5814e 100644 --- a/libcontainer/setns_init_linux.go +++ b/libcontainer/setns_init_linux.go @@ -37,7 +37,7 @@ func (l *linuxSetnsInit) Init() error { if err := selinux.SetKeyLabel(l.config.ProcessLabel); err != nil { return err } - defer selinux.SetKeyLabel("") + defer selinux.SetKeyLabel("") //nolint: errcheck // Do not inherit the parent's session keyring. if _, err := keys.JoinSessionKeyring(l.getSessionRingName()); err != nil { // Same justification as in standart_init_linux.go as to why we @@ -65,7 +65,7 @@ func (l *linuxSetnsInit) Init() error { if err := selinux.SetExecLabel(l.config.ProcessLabel); err != nil { return err } - defer selinux.SetExecLabel("") + defer selinux.SetExecLabel("") //nolint: errcheck // Without NoNewPrivileges seccomp is a privileged operation, so we need to // do this before dropping capabilities; otherwise do it as late as possible // just before execve so as few syscalls take place after it as possible. diff --git a/libcontainer/standard_init_linux.go b/libcontainer/standard_init_linux.go index d77022ad4..45c6d66cb 100644 --- a/libcontainer/standard_init_linux.go +++ b/libcontainer/standard_init_linux.go @@ -52,7 +52,7 @@ func (l *linuxStandardInit) Init() error { if err := selinux.SetKeyLabel(l.config.ProcessLabel); err != nil { return err } - defer selinux.SetKeyLabel("") + defer selinux.SetKeyLabel("") //nolint: errcheck ringname, keepperms, newperms := l.getSessionRingParams() // Do not inherit the parent's session keyring. @@ -151,7 +151,7 @@ func (l *linuxStandardInit) Init() error { if err := selinux.SetExecLabel(l.config.ProcessLabel); err != nil { return errors.Wrap(err, "set process label") } - defer selinux.SetExecLabel("") + defer selinux.SetExecLabel("") //nolint: errcheck // Without NoNewPrivileges seccomp is a privileged operation, so we need to // do this before dropping capabilities; otherwise do it as late as possible // just before execve so as few syscalls take place after it as possible. @@ -183,7 +183,7 @@ func (l *linuxStandardInit) Init() error { } // Close the pipe to signal that we have completed our init. logrus.Debugf("init: closing the pipe to signal completion") - l.pipe.Close() + _ = l.pipe.Close() // Close the log pipe fd so the parent's ForwardLogs can exit. if err := unix.Close(l.logFd); err != nil { @@ -207,7 +207,7 @@ func (l *linuxStandardInit) Init() error { // N.B. the core issue itself (passing dirfds to the host filesystem) has // since been resolved. // https://github.com/torvalds/linux/blob/v4.9/fs/exec.c#L1290-L1318 - unix.Close(l.fifoFd) + _ = unix.Close(l.fifoFd) // Set seccomp as close to execve as possible, so as few syscalls take // place afterward (reducing the amount of syscalls that users need to // enable in their seccomp profiles). diff --git a/libcontainer/state_linux.go b/libcontainer/state_linux.go index 02ff06ea9..43c040c85 100644 --- a/libcontainer/state_linux.go +++ b/libcontainer/state_linux.go @@ -157,7 +157,7 @@ func (i *createdState) transition(s containerState) error { } func (i *createdState) destroy() error { - i.c.initProcess.signal(unix.SIGKILL) + _ = i.c.initProcess.signal(unix.SIGKILL) return destroy(i.c) } From 3b31e3eaacbfddfac40db68282fa436a537d15e7 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 1 Feb 2021 19:34:04 +0100 Subject: [PATCH 5/7] errcheck: tty Signed-off-by: Sebastiaan van Stijn --- tty.go | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tty.go b/tty.go index 844d7f842..8ca193aa8 100644 --- a/tty.go +++ b/tty.go @@ -27,8 +27,8 @@ type tty struct { func (t *tty) copyIO(w io.Writer, r io.ReadCloser) { defer t.wg.Done() - io.Copy(w, r) - r.Close() + _, _ = io.Copy(w, r) + _ = r.Close() } // setup pipes for the process so that advanced features like c/r are able to easily checkpoint @@ -56,8 +56,8 @@ func setupProcessPipes(p *libcontainer.Process, rootuid, rootgid int) (*tty, err } } go func() { - io.Copy(i.Stdin, os.Stdin) - i.Stdin.Close() + _, _ = io.Copy(i.Stdin, os.Stdin) + _ = i.Stdin.Close() }() t.wg.Add(2) go t.copyIO(os.Stdout, i.Stdout) @@ -126,11 +126,11 @@ func (t *tty) recvtty(process *libcontainer.Process, socket *os.File) (Err error } defer func() { if Err != nil { - epollConsole.Close() + _ = epollConsole.Close() } }() - go epoller.Wait() - go io.Copy(epollConsole, os.Stdin) + go func() { _ = epoller.Wait() }() + go func() { _, _ = io.Copy(epollConsole, os.Stdin) }() t.wg.Add(1) go t.copyIO(os.Stdout, epollConsole) @@ -150,7 +150,7 @@ func handleInterrupt(c console.Console) { sigchan := make(chan os.Signal, 1) signal.Notify(sigchan, os.Interrupt) <-sigchan - c.Reset() + _ = c.Reset() os.Exit(0) } @@ -165,7 +165,7 @@ func (t *tty) waitConsole() error { // so that we no longer have copy in our process. func (t *tty) ClosePostStart() error { for _, c := range t.postStart { - c.Close() + _ = c.Close() } return nil } @@ -175,19 +175,19 @@ func (t *tty) ClosePostStart() error { func (t *tty) Close() error { // ensure that our side of the fds are always closed for _, c := range t.postStart { - c.Close() + _ = c.Close() } // the process is gone at this point, shutting down the console if we have // one and wait for all IO to be finished if t.console != nil && t.epoller != nil { - t.console.Shutdown(t.epoller.CloseConsole) + _ = t.console.Shutdown(t.epoller.CloseConsole) } t.wg.Wait() for _, c := range t.closers { - c.Close() + _ = c.Close() } if t.hostConsole != nil { - t.hostConsole.Reset() + _ = t.hostConsole.Reset() } return nil } From 0c65f833ef5efd4145d6af8ec6355772bf1f64e9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 1 Feb 2021 19:34:22 +0100 Subject: [PATCH 6/7] errcheck: signals Signed-off-by: Sebastiaan van Stijn --- signals.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/signals.go b/signals.go index 5682989c6..73d8de0fa 100644 --- a/signals.go +++ b/signals.go @@ -66,11 +66,11 @@ func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach if h.notifySocket != nil { if detach { - h.notifySocket.run(pid1) + _ = h.notifySocket.run(pid1) return 0, nil } - h.notifySocket.run(os.Getpid()) - go h.notifySocket.run(0) + _ = h.notifySocket.run(os.Getpid()) + go func() { _ = h.notifySocket.run(0) }() } // Perform the initial tty resize. Always ignore errors resizing because @@ -96,7 +96,7 @@ func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach // call Wait() on the process even though we already have the exit // status because we must ensure that any of the go specific process // fun such as flushing pipes are complete before we return. - process.Wait() + _, _ = process.Wait() return e.status, nil } } From df0206a6b33bcbacc9e773d0eda887314d9d6cc9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 1 Feb 2021 19:34:33 +0100 Subject: [PATCH 7/7] errcheck: utils Signed-off-by: Sebastiaan van Stijn --- utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils.go b/utils.go index 85413c291..f3244a497 100644 --- a/utils.go +++ b/utils.go @@ -39,7 +39,7 @@ func checkArgs(context *cli.Context, expected, checkType int) error { if err != nil { fmt.Printf("Incorrect Usage.\n\n") - cli.ShowCommandHelp(context, cmdName) + _ = cli.ShowCommandHelp(context, cmdName) return err } return nil