@@ -20,6 +20,7 @@ import (
2020 "fmt"
2121 "os"
2222 "os/exec"
23+ "syscall"
2324 "testing"
2425 "time"
2526
@@ -140,6 +141,9 @@ func TestKill(t *testing.T) {
140141 checkCgroupMode (t )
141142 manager , err := NewManager (defaultCgroup2Path , "/test1" , ToResources (& specs.LinuxResources {}))
142143 require .NoError (t , err )
144+ t .Cleanup (func () {
145+ _ = manager .Delete ()
146+ })
143147
144148 var (
145149 procs []* exec.Cmd
@@ -182,17 +186,35 @@ func TestKill(t *testing.T) {
182186
183187func TestMoveTo (t * testing.T ) {
184188 checkCgroupMode (t )
185- manager , err := NewManager (defaultCgroup2Path , "/test1" , ToResources (& specs.LinuxResources {}))
189+
190+ src , err := NewManager (defaultCgroup2Path , "/test-moveto-src" , ToResources (& specs.LinuxResources {}))
191+ require .NoError (t , err )
192+ t .Cleanup (func () {
193+ _ = src .Kill ()
194+ _ = src .Delete ()
195+ })
196+
197+ cmd := exec .Command ("sleep" , "infinity" )
198+ // Don't leak the process if we fail to join the cg,
199+ // send sigkill after tests over.
200+ cmd .SysProcAttr = & syscall.SysProcAttr {
201+ Pdeathsig : syscall .SIGKILL ,
202+ }
203+ err = cmd .Start ()
186204 require .NoError (t , err )
187205
188- proc := os . Getpid ()
189- err = manager .AddProc (uint64 (proc ))
206+ proc := cmd . Process . Pid
207+ err = src .AddProc (uint64 (proc ))
190208 require .NoError (t , err )
191209
192- destination , err := NewManager (defaultCgroup2Path , "/test2 " , ToResources (& specs.LinuxResources {}))
210+ destination , err := NewManager (defaultCgroup2Path , "/test-moveto-dest " , ToResources (& specs.LinuxResources {}))
193211 require .NoError (t , err )
212+ t .Cleanup (func () {
213+ _ = destination .Kill ()
214+ _ = destination .Delete ()
215+ })
194216
195- err = manager .MoveTo (destination )
217+ err = src .MoveTo (destination )
196218 require .NoError (t , err )
197219
198220 desProcs , err := destination .Procs (true )
@@ -209,8 +231,11 @@ func TestMoveTo(t *testing.T) {
209231
210232func TestCgroupType (t * testing.T ) {
211233 checkCgroupMode (t )
212- manager , err := NewManager (defaultCgroup2Path , "/test1 " , ToResources (& specs.LinuxResources {}))
234+ manager , err := NewManager (defaultCgroup2Path , "/test-type " , ToResources (& specs.LinuxResources {}))
213235 require .NoError (t , err )
236+ t .Cleanup (func () {
237+ os .RemoveAll (manager .path )
238+ })
214239
215240 cgType , err := manager .GetType ()
216241 require .NoError (t , err )
0 commit comments