Browse code

fix some leaking mounts in tests

This should help with errors such as:

=== RUN TestSysctlOverride
testing.go:1090: TempDir RemoveAll cleanup: unlinkat /tmp/TestSysctlOverride3702360633/001/mounts/shm: device or resource busy
--- FAIL: TestSysctlOverride (0.00s)

=== RUN TestSysctlOverrideHost
testing.go:1090: TempDir RemoveAll cleanup: unlinkat /tmp/TestSysctlOverrideHost226485533/001/mounts/shm: device or resource busy
--- FAIL: TestSysctlOverrideHost (0.00s)

=== RUN TestDockerSuite/TestRunWithVolumesIsRecursive
testing.go:1090: TempDir RemoveAll cleanup: unlinkat /tmp/TestDockerSuiteTestRunWithVolumesIsRecursive1156692230/001/tmpfs: device or resource busy
--- FAIL: TestDockerSuite/TestRunWithVolumesIsRecursive (0.49s)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2022/06/01 06:49:50
Showing 4 changed files
... ...
@@ -1971,22 +1971,22 @@ func (s *DockerAPISuite) TestContainersAPICreateMountsCreate(c *testing.T) {
1971 1971
 			assert.NilError(c, err)
1972 1972
 			defer os.RemoveAll(tmpDir3)
1973 1973
 
1974
-			assert.Assert(c, mountWrapper(tmpDir3, tmpDir3, "none", "bind,shared") == nil)
1975
-
1976
-			cases = append(cases, []testCase{
1977
-				{
1978
-					spec:     mount.Mount{Type: "bind", Source: tmpDir3, Target: destPath},
1979
-					expected: types.MountPoint{Type: "bind", RW: true, Destination: destPath, Source: tmpDir3},
1980
-				},
1981
-				{
1982
-					spec:     mount.Mount{Type: "bind", Source: tmpDir3, Target: destPath, ReadOnly: true},
1983
-					expected: types.MountPoint{Type: "bind", RW: false, Destination: destPath, Source: tmpDir3},
1984
-				},
1985
-				{
1986
-					spec:     mount.Mount{Type: "bind", Source: tmpDir3, Target: destPath, ReadOnly: true, BindOptions: &mount.BindOptions{Propagation: "shared"}},
1987
-					expected: types.MountPoint{Type: "bind", RW: false, Destination: destPath, Source: tmpDir3, Propagation: "shared"},
1988
-				},
1989
-			}...)
1974
+			if assert.Check(c, mountWrapper(c, tmpDir3, tmpDir3, "none", "bind,shared")) {
1975
+				cases = append(cases, []testCase{
1976
+					{
1977
+						spec:     mount.Mount{Type: "bind", Source: tmpDir3, Target: destPath},
1978
+						expected: types.MountPoint{Type: "bind", RW: true, Destination: destPath, Source: tmpDir3},
1979
+					},
1980
+					{
1981
+						spec:     mount.Mount{Type: "bind", Source: tmpDir3, Target: destPath, ReadOnly: true},
1982
+						expected: types.MountPoint{Type: "bind", RW: false, Destination: destPath, Source: tmpDir3},
1983
+					},
1984
+					{
1985
+						spec:     mount.Mount{Type: "bind", Source: tmpDir3, Target: destPath, ReadOnly: true, BindOptions: &mount.BindOptions{Propagation: "shared"}},
1986
+						expected: types.MountPoint{Type: "bind", RW: false, Destination: destPath, Source: tmpDir3, Propagation: "shared"},
1987
+					},
1988
+				}...)
1989
+			}
1990 1990
 		}
1991 1991
 	}
1992 1992
 
... ...
@@ -2,8 +2,18 @@
2 2
 
3 3
 package main
4 4
 
5
-import "github.com/moby/sys/mount"
5
+import (
6
+	"testing"
6 7
 
7
-func mountWrapper(device, target, mType, options string) error {
8
-	return mount.Mount(device, target, mType, options)
8
+	"github.com/moby/sys/mount"
9
+)
10
+
11
+func mountWrapper(t *testing.T, device, target, mType, options string) error {
12
+	t.Helper()
13
+	err := mount.Mount(device, target, mType, options)
14
+	if err != nil {
15
+		return err
16
+	}
17
+	t.Cleanup(func() { _ = mount.Unmount(target) })
18
+	return nil
9 19
 }
... ...
@@ -73,7 +73,7 @@ func (s *DockerAPISuite) TestContainersAPICreateMountsBindNamedPipe(c *testing.T
73 73
 	assert.Check(c, is.Equal(text, strings.TrimSpace(string(b))))
74 74
 }
75 75
 
76
-func mountWrapper(device, target, mType, options string) error {
76
+func mountWrapper(t *testing.T, device, target, mType, options string) error {
77 77
 	// This should never be called.
78 78
 	return errors.Errorf("there is no implementation of Mount on this platform")
79 79
 }
... ...
@@ -70,6 +70,7 @@ func (s *DockerCLIRunSuite) TestRunWithVolumesIsRecursive(c *testing.T) {
70 70
 	tmpfsDir := filepath.Join(tmpDir, "tmpfs")
71 71
 	assert.Assert(c, os.MkdirAll(tmpfsDir, 0o777) == nil, "failed to mkdir at %s", tmpfsDir)
72 72
 	assert.Assert(c, mount.Mount("tmpfs", tmpfsDir, "tmpfs", "") == nil, "failed to create a tmpfs mount at %s", tmpfsDir)
73
+	defer mount.Unmount(tmpfsDir)
73 74
 
74 75
 	f, err := os.CreateTemp(tmpfsDir, "touch-me")
75 76
 	assert.NilError(c, err)