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>
(cherry picked from commit 9ff169ccf4)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Cory Snider 2023-06-05 18:30:30 -04:00 committed by Sebastiaan van Stijn
parent f3743766e9
commit 0a6a5a9140
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -11,17 +11,18 @@ import (
"github.com/docker/docker/daemon/config"
"github.com/docker/docker/daemon/network"
"github.com/docker/docker/libnetwork"
"golang.org/x/sys/unix"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
"gotest.tools/v3/skip"
)
func setupFakeDaemon(t *testing.T, c *container.Container) *Daemon {
root, err := os.MkdirTemp("", "oci_linux_test-root")
assert.NilError(t, err)
t.Helper()
root := t.TempDir()
rootfs := filepath.Join(root, "rootfs")
err = os.MkdirAll(rootfs, 0755)
err := os.MkdirAll(rootfs, 0755)
assert.NilError(t, err)
netController, err := libnetwork.New()
@ -49,6 +50,18 @@ func setupFakeDaemon(t *testing.T, c *container.Container) *Daemon {
c.NetworkSettings = &network.Settings{Networks: make(map[string]*network.EndpointSettings)}
}
// HORRIBLE HACK: clean up shm mounts leaked by some tests. Otherwise the
// offending tests would fail due to the mounts blocking the temporary
// directory from being cleaned up.
t.Cleanup(func() {
if c.ShmPath != "" {
var err error
for err == nil { // Some tests over-mount over the same path multiple times.
err = unix.Unmount(c.ShmPath, unix.MNT_DETACH)
}
}
})
return d
}
@ -60,10 +73,6 @@ func (i *fakeImageService) StorageDriver() string {
return "overlay"
}
func cleanupFakeContainer(c *container.Container) {
_ = os.RemoveAll(c.Root)
}
// TestTmpfsDevShmNoDupMount checks that a user-specified /dev/shm tmpfs
// mount (as in "docker run --tmpfs /dev/shm:rw,size=NNN") does not result
// in "Duplicate mount point" error from the engine.
@ -81,7 +90,6 @@ func TestTmpfsDevShmNoDupMount(t *testing.T) {
},
}
d := setupFakeDaemon(t, c)
defer cleanupFakeContainer(c)
_, err := d.createSpec(context.TODO(), c)
assert.Check(t, err)
@ -100,7 +108,6 @@ func TestIpcPrivateVsReadonly(t *testing.T) {
},
}
d := setupFakeDaemon(t, c)
defer cleanupFakeContainer(c)
s, err := d.createSpec(context.TODO(), c)
assert.Check(t, err)
@ -129,7 +136,6 @@ func TestSysctlOverride(t *testing.T) {
},
}
d := setupFakeDaemon(t, c)
defer cleanupFakeContainer(c)
// Ensure that the implicit sysctl is set correctly.
s, err := d.createSpec(context.TODO(), c)
@ -181,7 +187,6 @@ func TestSysctlOverrideHost(t *testing.T) {
},
}
d := setupFakeDaemon(t, c)
defer cleanupFakeContainer(c)
// Ensure that the implicit sysctl is not set
s, err := d.createSpec(context.TODO(), c)