@@ -24,20 +24,30 @@ import (
2424 "github.com/google/uuid"
2525 "github.com/sirupsen/logrus"
2626 "github.com/stretchr/testify/require"
27+ "go.uber.org/multierr"
2728
2829 "go.linka.cloud/d2vm/pkg/docker"
2930 "go.linka.cloud/d2vm/pkg/exec"
3031)
3132
32- func testConfig (t * testing.T , ctx context.Context , name , img string , config Config , luks , grubBIOS , grubEFI bool ) {
33+ func testConfig (t * testing.T , ctx context.Context , name , img string , config Config , luks , grubBIOS , grubEFI bool ) func () error {
3334 require .NoError (t , docker .Pull (ctx , Arch , img ))
3435 tmpPath := filepath .Join (os .TempDir (), "d2vm-tests" , strings .NewReplacer (":" , "-" , "." , "-" ).Replace (name ))
3536 require .NoError (t , os .MkdirAll (tmpPath , 0755 ))
3637 defer os .RemoveAll (tmpPath )
3738 logrus .Infof ("inspecting image %s" , img )
3839 r , err := FetchDockerImageOSRelease (ctx , img )
3940 require .NoError (t , err )
40- defer docker .Remove (ctx , img )
41+ var fns []func () error
42+ clean := func () (err error ) {
43+ for _ , v := range fns {
44+ err = multierr .Append (err , v ())
45+ }
46+ return err
47+ }
48+ fns = append (fns , func () error {
49+ return docker .Remove (ctx , img )
50+ })
4151 if ! r .SupportsLUKS () && luks {
4252 t .Skipf ("LUKS not supported for %s" , r .Version )
4353 }
@@ -53,13 +63,16 @@ func testConfig(t *testing.T, ctx context.Context, name, img string, config Conf
5363 imgUUID := uuid .New ().String ()
5464 logrus .Infof ("building kernel enabled image" )
5565 require .NoError (t , docker .Build (ctx , false , imgUUID , p , dir , Arch ))
56- defer docker .Remove (ctx , imgUUID )
66+ fns = append (fns , func () error {
67+ return docker .Remove (ctx , imgUUID )
68+ })
5769 // we don't need to test the kernel location if grub is enabled
5870 if grubBIOS || grubEFI {
59- return
71+ return clean
6072 }
6173 require .NoError (t , docker .RunAndRemove (ctx , imgUUID , "test" , "-f" , config .Kernel ))
6274 require .NoError (t , docker .RunAndRemove (ctx , imgUUID , "test" , "-f" , config .Initrd ))
75+ return clean
6376}
6477
6578func TestConfig (t * testing.T ) {
@@ -147,12 +160,17 @@ func TestConfig(t *testing.T) {
147160 }
148161 }
149162 name := strings .Join (n , "-" )
163+ var clean func () error
150164 t .Run (name , func (t * testing.T ) {
151- t .Parallel ()
152165 ctx , cancel := context .WithCancel (context .Background ())
153166 defer cancel ()
154- testConfig (t , ctx , name , test .image , test .config , luks , grubBIOS , grubEFI )
167+ clean = testConfig (t , ctx , name , test .image , test .config , luks , grubBIOS , grubEFI )
155168 })
169+ defer func () {
170+ if clean != nil {
171+ _ = clean ()
172+ }
173+ }()
156174 }
157175 }
158176 }
0 commit comments