Skip to content

Commit

Permalink
Cgroup2: Remove lingering cgroups after tests
Browse files Browse the repository at this point in the history
There was a couple cgroup tests that wouldn't cleanup after themselves.
This alters the names of these so they're more obvious and calls kill+delete
if necessary.

Signed-off-by: Danny Canter <danny@dcantah.dev>
  • Loading branch information
dcantah committed Apr 7, 2023
1 parent 49edf54 commit 85933b4
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions cgroup2/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,15 @@ func TestSystemdFullPath(t *testing.T) {

func TestKill(t *testing.T) {
checkCgroupMode(t)
manager, err := NewManager(defaultCgroup2Path, "/test1", ToResources(&specs.LinuxResources{}))

manager, err := NewManager(defaultCgroup2Path, "/test-kill", ToResources(&specs.LinuxResources{}))
if err != nil {
t.Fatal(err)
}
defer func() {
_ = manager.Delete()
}()

var procs []*exec.Cmd
for i := 0; i < 5; i++ {
cmd := exec.Command("sleep", "infinity")
Expand Down Expand Up @@ -194,21 +199,35 @@ func TestKill(t *testing.T) {

func TestMoveTo(t *testing.T) {
checkCgroupMode(t)
manager, err := NewManager(defaultCgroup2Path, "/test1", ToResources(&specs.LinuxResources{}))

manager, err := NewManager(defaultCgroup2Path, "/test-moveto-1", ToResources(&specs.LinuxResources{}))
if err != nil {
t.Error(err)
return
}
proc := os.Getpid()
defer func() {
_ = manager.Kill()
_ = manager.Delete()
}()

cmd := exec.Command("sleep", "infinity")
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
proc := cmd.Process.Pid
if err := manager.AddProc(uint64(proc)); err != nil {
t.Error(err)
return
}
destination, err := NewManager(defaultCgroup2Path, "/test2", ToResources(&specs.LinuxResources{}))
destination, err := NewManager(defaultCgroup2Path, "/test-moveto-2", ToResources(&specs.LinuxResources{}))
if err != nil {
t.Error(err)
return
}
defer func() {
_ = destination.Kill()
_ = destination.Delete()
}()
if err := manager.MoveTo(destination); err != nil {
t.Error(err)
return
Expand All @@ -230,8 +249,9 @@ func TestMoveTo(t *testing.T) {

func TestCgroupType(t *testing.T) {
checkCgroupMode(t)
manager, err := NewManager(defaultCgroup2Path, "/test1", ToResources(&specs.LinuxResources{}))
manager, err := NewManager(defaultCgroup2Path, "/test-type", ToResources(&specs.LinuxResources{}))
require.NoError(t, err)
defer os.RemoveAll(manager.path)

cgType, err := manager.GetType()
require.NoError(t, err)
Expand Down

0 comments on commit 85933b4

Please sign in to comment.