Browse code

daemon: modernize oci_linux_test.go

Switch to using t.TempDir() instead of rolling our own.

Clean up mounts leaked by the tests as otherwise the tests fail due to
the leaked mounts because unlike the old cleanup code, t.TempDir()
cleanup does not ignore errors from os.RemoveAll.

Signed-off-by: Cory Snider <csnider@mirantis.com>

Cory Snider authored on 2023/06/06 07:30:30
Showing 1 changed files
... ...
@@ -11,17 +11,18 @@ import (
11 11
 	"github.com/docker/docker/daemon/config"
12 12
 	"github.com/docker/docker/daemon/network"
13 13
 	"github.com/docker/docker/libnetwork"
14
+	"golang.org/x/sys/unix"
14 15
 	"gotest.tools/v3/assert"
15 16
 	is "gotest.tools/v3/assert/cmp"
16 17
 	"gotest.tools/v3/skip"
17 18
 )
18 19
 
19 20
 func setupFakeDaemon(t *testing.T, c *container.Container) *Daemon {
20
-	root, err := os.MkdirTemp("", "oci_linux_test-root")
21
-	assert.NilError(t, err)
21
+	t.Helper()
22
+	root := t.TempDir()
22 23
 
23 24
 	rootfs := filepath.Join(root, "rootfs")
24
-	err = os.MkdirAll(rootfs, 0755)
25
+	err := os.MkdirAll(rootfs, 0755)
25 26
 	assert.NilError(t, err)
26 27
 
27 28
 	netController, err := libnetwork.New()
... ...
@@ -48,6 +49,18 @@ func setupFakeDaemon(t *testing.T, c *container.Container) *Daemon {
48 48
 		c.NetworkSettings = &network.Settings{Networks: make(map[string]*network.EndpointSettings)}
49 49
 	}
50 50
 
51
+	// HORRIBLE HACK: clean up shm mounts leaked by some tests. Otherwise the
52
+	// offending tests would fail due to the mounts blocking the temporary
53
+	// directory from being cleaned up.
54
+	t.Cleanup(func() {
55
+		if c.ShmPath != "" {
56
+			var err error
57
+			for err == nil { // Some tests over-mount over the same path multiple times.
58
+				err = unix.Unmount(c.ShmPath, unix.MNT_DETACH)
59
+			}
60
+		}
61
+	})
62
+
51 63
 	return d
52 64
 }
53 65
 
... ...
@@ -59,10 +72,6 @@ func (i *fakeImageService) StorageDriver() string {
59 59
 	return "overlay"
60 60
 }
61 61
 
62
-func cleanupFakeContainer(c *container.Container) {
63
-	_ = os.RemoveAll(c.Root)
64
-}
65
-
66 62
 // TestTmpfsDevShmNoDupMount checks that a user-specified /dev/shm tmpfs
67 63
 // mount (as in "docker run --tmpfs /dev/shm:rw,size=NNN") does not result
68 64
 // in "Duplicate mount point" error from the engine.
... ...
@@ -80,7 +89,6 @@ func TestTmpfsDevShmNoDupMount(t *testing.T) {
80 80
 		},
81 81
 	}
82 82
 	d := setupFakeDaemon(t, c)
83
-	defer cleanupFakeContainer(c)
84 83
 
85 84
 	_, err := d.createSpec(context.TODO(), &configStore{}, c)
86 85
 	assert.Check(t, err)
... ...
@@ -99,7 +107,6 @@ func TestIpcPrivateVsReadonly(t *testing.T) {
99 99
 		},
100 100
 	}
101 101
 	d := setupFakeDaemon(t, c)
102
-	defer cleanupFakeContainer(c)
103 102
 
104 103
 	s, err := d.createSpec(context.TODO(), &configStore{}, c)
105 104
 	assert.Check(t, err)
... ...
@@ -128,7 +135,6 @@ func TestSysctlOverride(t *testing.T) {
128 128
 		},
129 129
 	}
130 130
 	d := setupFakeDaemon(t, c)
131
-	defer cleanupFakeContainer(c)
132 131
 
133 132
 	// Ensure that the implicit sysctl is set correctly.
134 133
 	s, err := d.createSpec(context.TODO(), &configStore{}, c)
... ...
@@ -179,7 +185,6 @@ func TestSysctlOverrideHost(t *testing.T) {
179 179
 		},
180 180
 	}
181 181
 	d := setupFakeDaemon(t, c)
182
-	defer cleanupFakeContainer(c)
183 182
 
184 183
 	// Ensure that the implicit sysctl is not set
185 184
 	s, err := d.createSpec(context.TODO(), &configStore{}, c)