Skip to content

Commit

Permalink
Cgroup2: Testify all tests
Browse files Browse the repository at this point in the history
Use require/assert for all the cgroup2 tests. These tests were in a
weird spot where some of them used a mix of testify.assert, and some
used `testify.require` in place of `if err != nil { t.Fatal() }`. This
just moves everything over to `require.NoError` and friends.

Signed-off-by: Danny Canter <danny@dcantah.dev>
  • Loading branch information
dcantah committed Apr 13, 2023
1 parent db40fde commit 55c197e
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 131 deletions.
18 changes: 8 additions & 10 deletions cgroup2/cpuv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCgroupv2CpuStats(t *testing.T) {
Expand All @@ -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)
Expand All @@ -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))
}

Expand All @@ -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) {
Expand Down
10 changes: 4 additions & 6 deletions cgroup2/devicefilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/require"
)

func hash(s, comm string) string {
Expand All @@ -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)
}
}

Expand Down
15 changes: 8 additions & 7 deletions cgroup2/hugetlbv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCgroupv2HugetlbStats(t *testing.T) {
Expand All @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions cgroup2/iov2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestCgroupv2IOController(t *testing.T) {
Expand All @@ -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")
}
111 changes: 45 additions & 66 deletions cgroup2/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}
}

Expand Down
19 changes: 9 additions & 10 deletions cgroup2/memoryv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCgroupv2MemoryStats(t *testing.T) {
Expand All @@ -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)
Expand All @@ -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")
}
10 changes: 4 additions & 6 deletions cgroup2/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package cgroup2

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestVerifyGroupPath(t *testing.T) {
Expand All @@ -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)
}
}
}
15 changes: 8 additions & 7 deletions cgroup2/pidsv2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"os"
"strconv"
"testing"

"github.com/stretchr/testify/require"
)

func TestCgroupv2PidsStats(t *testing.T) {
Expand All @@ -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)))
}
Expand All @@ -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))
}
Loading

0 comments on commit 55c197e

Please sign in to comment.