diff --git a/cgroup2/cpuv2_test.go b/cgroup2/cpuv2_test.go index 3942c7b0..30450f32 100644 --- a/cgroup2/cpuv2_test.go +++ b/cgroup2/cpuv2_test.go @@ -24,6 +24,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCgroupv2CpuStats(t *testing.T) { @@ -45,10 +46,10 @@ func TestCgroupv2CpuStats(t *testing.T) { }, } c, err := NewManager(defaultCgroup2Path, groupPath, &res) - if err != nil { - t.Fatal("failed to init new cgroup manager: ", err) - } - defer os.Remove(c.path) + require.NoError(t, err, "failed to init new cgroup manager") + t.Cleanup(func() { + os.Remove(c.path) + }) checkFileContent(t, c.path, "cpu.weight", strconv.FormatUint(weight, 10)) checkFileContent(t, c.path, "cpu.max", max) @@ -62,9 +63,8 @@ func TestSystemdCgroupCpuController(t *testing.T) { var weight uint64 = 100 res := Resources{CPU: &CPU{Weight: &weight}} c, err := NewSystemd("", group, os.Getpid(), &res) - if err != nil { - t.Fatal("failed to init new cgroup systemd manager: ", err) - } + require.NoError(t, err, "failed to init new cgroup systemd manager") + checkFileContent(t, c.path, "cpu.weight", strconv.FormatUint(weight, 10)) } @@ -82,9 +82,7 @@ func TestSystemdCgroupCpuController_NilWeight(t *testing.T) { }, } _, err := NewSystemd("/", group, -1, &res) - if err != nil { - t.Fatal("failed to init new cgroup systemd manager: ", err) - } + require.NoError(t, err, "failed to init new cgroup systemd manager") } func TestExtractQuotaAndPeriod(t *testing.T) { diff --git a/cgroup2/devicefilter_test.go b/cgroup2/devicefilter_test.go index 5c8b31fe..9be17143 100644 --- a/cgroup2/devicefilter_test.go +++ b/cgroup2/devicefilter_test.go @@ -21,6 +21,7 @@ import ( "testing" "github.com/opencontainers/runtime-spec/specs-go" + "github.com/stretchr/testify/require" ) func hash(s, comm string) string { @@ -37,17 +38,14 @@ func hash(s, comm string) string { func testDeviceFilter(t testing.TB, devices []specs.LinuxDeviceCgroup, expectedStr string) { insts, _, err := DeviceFilter(devices) - if err != nil { - t.Fatalf("%s: %v (devices: %+v)", t.Name(), err, devices) - } + require.NoErrorf(t, err, "%s: (devices: %+v)", t.Name(), devices) + s := insts.String() t.Logf("%s: devices: %+v\n%s", t.Name(), devices, s) if expectedStr != "" { hashed := hash(s, "//") expectedHashed := hash(expectedStr, "//") - if expectedHashed != hashed { - t.Fatalf("expected:\n%q\ngot\n%q", expectedHashed, hashed) - } + require.Equal(t, expectedHashed, hashed) } } diff --git a/cgroup2/hugetlbv2_test.go b/cgroup2/hugetlbv2_test.go index 7011cc7e..ccb8c6b3 100644 --- a/cgroup2/hugetlbv2_test.go +++ b/cgroup2/hugetlbv2_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCgroupv2HugetlbStats(t *testing.T) { @@ -34,14 +35,14 @@ func TestCgroupv2HugetlbStats(t *testing.T) { HugeTlb: &hugeTlb, } c, err := NewManager(defaultCgroup2Path, groupPath, &res) - if err != nil { - t.Fatal("failed to init new cgroup manager: ", err) - } - defer os.Remove(c.path) + require.NoError(t, err, "failed to init new cgroup manager") + t.Cleanup(func() { + os.Remove(c.path) + }) + stats, err := c.Stat() - if err != nil { - t.Fatal("failed to get cgroups stats: ", err) - } + require.NoError(t, err, "failed to get cgroup stats") + for _, entry := range stats.Hugetlb { if entry.Pagesize == "2MB" { assert.Equal(t, uint64(1073741824), entry.Max) diff --git a/cgroup2/iov2_test.go b/cgroup2/iov2_test.go index 2d0a50d1..41ffa5b6 100644 --- a/cgroup2/iov2_test.go +++ b/cgroup2/iov2_test.go @@ -20,6 +20,8 @@ import ( "fmt" "os" "testing" + + "github.com/stretchr/testify/require" ) func TestCgroupv2IOController(t *testing.T) { @@ -39,10 +41,10 @@ func TestCgroupv2IOController(t *testing.T) { }, } c, err := NewManager(defaultCgroup2Path, groupPath, &res) - if err != nil { - t.Fatal("failed to init new cgroup manager: ", err) - } - defer os.Remove(c.path) + require.NoError(t, err, "failed to init new cgroup manager") + t.Cleanup(func() { + os.Remove(c.path) + }) checkFileContent(t, c.path, "io.max", "8:0 rbps=max wbps=max riops=120 wiops=max") } diff --git a/cgroup2/manager_test.go b/cgroup2/manager_test.go index b4515296..c2bc25db 100644 --- a/cgroup2/manager_test.go +++ b/cgroup2/manager_test.go @@ -35,43 +35,35 @@ func TestEventChanCleanupOnCgroupRemoval(t *testing.T) { cmd := exec.Command("cat") stdin, err := cmd.StdinPipe() - if err != nil { - t.Fatalf("Failed to create cat process: %v", err) - } - if err := cmd.Start(); err != nil { - t.Fatalf("Failed to start cat process: %v", err) - } + require.NoError(t, err, "failed to create cat process") + + err = cmd.Start() + require.NoError(t, err, "failed to start cat process") + proc := cmd.Process - if proc == nil { - t.Fatal("Process is nil") - } + require.NotNil(t, proc, "process was nil") group := fmt.Sprintf("testing-watcher-%d.scope", proc.Pid) c, err := NewSystemd("", group, proc.Pid, &Resources{}) - if err != nil { - t.Fatalf("Failed to init new cgroup manager: %v", err) - } + require.NoError(t, err, "failed to init new cgroup manager") evCh, errCh := c.EventChan() // give event goroutine a chance to start time.Sleep(500 * time.Millisecond) - if err := stdin.Close(); err != nil { - t.Fatalf("Failed closing stdin: %v", err) - } - if err := cmd.Wait(); err != nil { - t.Fatalf("Failed waiting for cmd: %v", err) - } + err = stdin.Close() + require.NoError(t, err, "failed closing stdin") + + err = cmd.Wait() + require.NoError(t, err, "failed waiting for cmd") done := false for !done { select { case <-evCh: case err := <-errCh: - if err != nil { - t.Fatalf("Unexpected error on error channel: %v", err) - } + require.NoError(t, err, "unexpected error on error channel") done = true case <-time.After(5 * time.Second): t.Fatal("Timed out") @@ -147,35 +139,31 @@ func TestSystemdFullPath(t *testing.T) { func TestKill(t *testing.T) { checkCgroupMode(t) manager, err := NewManager(defaultCgroup2Path, "/test1", ToResources(&specs.LinuxResources{})) - if err != nil { - t.Fatal(err) - } - var procs []*exec.Cmd - for i := 0; i < 5; i++ { + require.NoError(t, err) + + var ( + procs []*exec.Cmd + numProcs = 5 + ) + for i := 0; i < numProcs; i++ { cmd := exec.Command("sleep", "infinity") - if err := cmd.Start(); err != nil { - t.Fatal(err) - } - if cmd.Process == nil { - t.Fatal("Process is nil") - } - if err := manager.AddProc(uint64(cmd.Process.Pid)); err != nil { - t.Fatal(err) - } + err = cmd.Start() + require.NoError(t, err) + require.NotNil(t, cmd.Process, "process is nil") + + err = manager.AddProc(uint64(cmd.Process.Pid)) + require.NoError(t, err) + procs = append(procs, cmd) } // Verify we have 5 pids before beginning Kill below. pids, err := manager.Procs(true) - if err != nil { - t.Fatal(err) - } - if len(pids) != 5 { - t.Fatalf("expected 5 pids, got %d", len(pids)) - } + require.NoError(t, err) + require.Len(t, pids, numProcs, "pid count unexpected") + // Now run kill, and check that nothing is running after. - if err := manager.Kill(); err != nil { - t.Fatal(err) - } + err = manager.Kill() + require.NoError(t, err) done := make(chan struct{}) go func() { @@ -195,36 +183,27 @@ func TestKill(t *testing.T) { func TestMoveTo(t *testing.T) { checkCgroupMode(t) manager, err := NewManager(defaultCgroup2Path, "/test1", ToResources(&specs.LinuxResources{})) - if err != nil { - t.Error(err) - return - } + require.NoError(t, err) + proc := os.Getpid() - if err := manager.AddProc(uint64(proc)); err != nil { - t.Error(err) - return - } + err = manager.AddProc(uint64(proc)) + require.NoError(t, err) + destination, err := NewManager(defaultCgroup2Path, "/test2", ToResources(&specs.LinuxResources{})) - if err != nil { - t.Error(err) - return - } - if err := manager.MoveTo(destination); err != nil { - t.Error(err) - return - } + require.NoError(t, err) + + err = manager.MoveTo(destination) + require.NoError(t, err) + desProcs, err := destination.Procs(true) - if err != nil { - t.Error(err) - return - } + require.NoError(t, err) + desMap := make(map[int]bool) for _, p := range desProcs { desMap[int(p)] = true } if !desMap[proc] { - t.Errorf("process %v not in destination cgroup", proc) - return + t.Fatalf("process %v not in destination cgroup", proc) } } diff --git a/cgroup2/memoryv2_test.go b/cgroup2/memoryv2_test.go index dec46cab..d1d534b6 100644 --- a/cgroup2/memoryv2_test.go +++ b/cgroup2/memoryv2_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestCgroupv2MemoryStats(t *testing.T) { @@ -36,14 +37,13 @@ func TestCgroupv2MemoryStats(t *testing.T) { }, } c, err := NewManager(defaultCgroup2Path, groupPath, &res) - if err != nil { - t.Fatal("failed to init new cgroup manager: ", err) - } - defer os.Remove(c.path) + require.NoError(t, err, "failed to init new cgroup manager") + t.Cleanup(func() { + os.Remove(c.path) + }) + stats, err := c.Stat() - if err != nil { - t.Fatal("failed to get cgroups stats: ", err) - } + require.NoError(t, err, "failed to get cgroup stats") assert.Equal(t, uint64(314572800), stats.Memory.SwapLimit) assert.Equal(t, uint64(629145600), stats.Memory.UsageLimit) @@ -61,9 +61,8 @@ func TestSystemdCgroupMemoryController(t *testing.T) { }, } c, err := NewSystemd("", group, os.Getpid(), &res) - if err != nil { - t.Fatal("failed to init new cgroup systemd manager: ", err) - } + require.NoError(t, err, "failed to init new cgroup systemd manager") + checkFileContent(t, c.path, "memory.min", "16384") checkFileContent(t, c.path, "memory.max", "629145600") } diff --git a/cgroup2/paths_test.go b/cgroup2/paths_test.go index 143c0d87..5ce9d16c 100644 --- a/cgroup2/paths_test.go +++ b/cgroup2/paths_test.go @@ -18,6 +18,8 @@ package cgroup2 import ( "testing" + + "github.com/stretchr/testify/assert" ) func TestVerifyGroupPath(t *testing.T) { @@ -34,13 +36,9 @@ func TestVerifyGroupPath(t *testing.T) { for s, valid := range valids { err := VerifyGroupPath(s) if valid { - if err != nil { - t.Error(err) - } + assert.NoError(t, err) } else { - if err == nil { - t.Error("error is expected") - } + assert.Error(t, err) } } } diff --git a/cgroup2/pidsv2_test.go b/cgroup2/pidsv2_test.go index 7f2e7462..358ac6c4 100644 --- a/cgroup2/pidsv2_test.go +++ b/cgroup2/pidsv2_test.go @@ -21,6 +21,8 @@ import ( "os" "strconv" "testing" + + "github.com/stretchr/testify/require" ) func TestCgroupv2PidsStats(t *testing.T) { @@ -34,10 +36,10 @@ func TestCgroupv2PidsStats(t *testing.T) { }, } c, err := NewManager(defaultCgroup2Path, groupPath, &res) - if err != nil { - t.Fatal("failed to init new cgroup manager: ", err) - } - defer os.Remove(c.path) + require.NoError(t, err, "failed to init new cgroup manager") + t.Cleanup(func() { + os.Remove(c.path) + }) checkFileContent(t, c.path, "pids.max", strconv.Itoa(int(max))) } @@ -48,8 +50,7 @@ func TestSystemdCgroupPidsController(t *testing.T) { pid := os.Getpid() res := Resources{} c, err := NewSystemd("", group, pid, &res) - if err != nil { - t.Fatal("failed to init new cgroup systemd manager: ", err) - } + require.NoError(t, err, "failed to init new cgroup systemd manager") + checkFileContent(t, c.path, "cgroup.procs", strconv.Itoa(pid)) } diff --git a/cgroup2/testutils_test.go b/cgroup2/testutils_test.go index 1f1cc647..894874d7 100644 --- a/cgroup2/testutils_test.go +++ b/cgroup2/testutils_test.go @@ -23,14 +23,15 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "golang.org/x/sys/unix" ) func checkCgroupMode(t *testing.T) { var st unix.Statfs_t - if err := unix.Statfs(defaultCgroup2Path, &st); err != nil { - t.Fatal("cannot statfs cgroup root") - } + err := unix.Statfs(defaultCgroup2Path, &st) + require.NoError(t, err, "cannot statfs cgroup root") + isUnified := st.Type == unix.CGROUP2_SUPER_MAGIC if !isUnified { t.Skip("System running in hybrid or cgroupv1 mode") @@ -46,8 +47,6 @@ func checkCgroupControllerSupported(t *testing.T, controller string) { func checkFileContent(t *testing.T, path, filename, value string) { out, err := os.ReadFile(filepath.Join(path, filename)) - if err != nil { - t.Fatalf("failed to read %s file", filename) - } + require.NoErrorf(t, err, "failed to read %s file", filename) assert.Equal(t, value, strings.TrimSpace(string(out))) } diff --git a/cgroup2/utils_test.go b/cgroup2/utils_test.go index 3c47af87..1fca5f15 100644 --- a/cgroup2/utils_test.go +++ b/cgroup2/utils_test.go @@ -33,16 +33,10 @@ func TestParseCgroupFromReader(t *testing.T) { for s, expected := range cases { g, err := parseCgroupFromReader(strings.NewReader(s)) if expected != "" { - if g != expected { - t.Errorf("expected %q, got %q", expected, g) - } - if err != nil { - t.Error(err) - } + assert.Equal(t, g, expected) + assert.NoError(t, err) } else { - if err == nil { - t.Error("error is expected") - } + assert.Error(t, err) } } }