Browse code

fix unclosed file-handles in tests

These seemed to prevent cleaning up directories;

On arm64:

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

On Windows:

=== Failed
=== FAIL: github.com/docker/docker/daemon TestLoadOrCreateTrustKeyInvalidKeyFile (0.00s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestLoadOrCreateTrustKeyInvalidKeyFile2014634395\001\keyfile4156691647: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/daemon/graphdriver TestIsEmptyDir (0.01s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestIsEmptyDir1962964337\001\dir-with-empty-file\file2523853824: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/pkg/directory TestSizeEmptyFile (0.00s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestSizeEmptyFile1562416712\001\file16507846: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/pkg/directory TestSizeNonemptyFile (0.00s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestSizeNonemptyFile1240832785\001\file3265662846: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/pkg/directory TestSizeFileAndNestedDirectoryEmpty (0.00s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestSizeFileAndNestedDirectoryEmpty2163416550\001\file3715413181: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/pkg/directory TestSizeFileAndNestedDirectoryNonempty (0.00s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestSizeFileAndNestedDirectoryNonempty878205470\001\file3280422273: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/volume/service TestSetGetMeta (0.01s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestSetGetMeta3332268057\001\db: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/volume/service TestList (0.03s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestList2846947953\001\volumes\metadata.db: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/volume/service TestRestore (0.02s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestRestore3368254142\001\volumes\metadata.db: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/daemon/graphdriver TestIsEmptyDir (0.00s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestIsEmptyDir2823795693\001\dir-with-empty-file\file2625561089: The process cannot access the file because it is being used by another process.

=== FAIL: github.com/docker/docker/pkg/directory TestSizeFileAndNestedDirectoryNonempty (0.00s)
testing.go:1090: TempDir RemoveAll cleanup: remove C:\Users\CONTAI~1\AppData\Local\Temp\TestSizeFileAndNestedDirectoryNonempty4246252950\001\nested3442260313\file21164327: The process cannot access the file because it is being used by another process.

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

Sebastiaan van Stijn authored on 2022/06/01 00:01:43
Showing 6 changed files
... ...
@@ -28,8 +28,9 @@ func TestIsEmptyDir(t *testing.T) {
28 28
 	d = filepath.Join(tmp, "dir-with-empty-file")
29 29
 	err = os.Mkdir(d, 0755)
30 30
 	assert.NilError(t, err)
31
-	_, err = os.CreateTemp(d, "file")
31
+	f, err := os.CreateTemp(d, "file")
32 32
 	assert.NilError(t, err)
33
+	defer f.Close()
33 34
 	empty = isEmptyDir(d)
34 35
 	assert.Check(t, !empty)
35 36
 }
... ...
@@ -18,6 +18,7 @@ func TestLoadOrCreateTrustKeyInvalidKeyFile(t *testing.T) {
18 18
 
19 19
 	tmpKeyFile, err := os.CreateTemp(tmpKeyFolderPath, "keyfile")
20 20
 	assert.NilError(t, err)
21
+	defer tmpKeyFile.Close()
21 22
 
22 23
 	_, err = loadOrCreateTrustKey(tmpKeyFile.Name())
23 24
 	assert.Check(t, is.ErrorContains(err, "Error loading key file"))
... ...
@@ -35,6 +35,7 @@ func TestSizeEmptyFile(t *testing.T) {
35 35
 	if file, err = os.CreateTemp(dir, "file"); err != nil {
36 36
 		t.Fatalf("failed to create file: %s", err)
37 37
 	}
38
+	defer file.Close()
38 39
 
39 40
 	var size int64
40 41
 	if size, _ = Size(context.Background(), file.Name()); size != 0 {
... ...
@@ -54,6 +55,7 @@ func TestSizeNonemptyFile(t *testing.T) {
54 54
 	if file, err = os.CreateTemp(dir, "file"); err != nil {
55 55
 		t.Fatalf("failed to create file: %s", err)
56 56
 	}
57
+	defer file.Close()
57 58
 
58 59
 	d := []byte{97, 98, 99, 100, 101}
59 60
 	file.Write(d)
... ...
@@ -96,6 +98,7 @@ func TestSizeFileAndNestedDirectoryEmpty(t *testing.T) {
96 96
 	if file, err = os.CreateTemp(dir, "file"); err != nil {
97 97
 		t.Fatalf("failed to create file: %s", err)
98 98
 	}
99
+	defer file.Close()
99 100
 
100 101
 	d := []byte{100, 111, 99, 107, 101, 114}
101 102
 	file.Write(d)
... ...
@@ -121,6 +124,7 @@ func TestSizeFileAndNestedDirectoryNonempty(t *testing.T) {
121 121
 	if file, err = os.CreateTemp(dir, "file"); err != nil {
122 122
 		t.Fatalf("failed to create file: %s", err)
123 123
 	}
124
+	defer file.Close()
124 125
 
125 126
 	data := []byte{100, 111, 99, 107, 101, 114}
126 127
 	file.Write(data)
... ...
@@ -129,6 +133,7 @@ func TestSizeFileAndNestedDirectoryNonempty(t *testing.T) {
129 129
 	if nestedFile, err = os.CreateTemp(dirNested, "file"); err != nil {
130 130
 		t.Fatalf("failed to create file in nested directory: %s", err)
131 131
 	}
132
+	defer nestedFile.Close()
132 133
 
133 134
 	nestedData := []byte{100, 111, 99, 107, 101, 114}
134 135
 	nestedFile.Write(nestedData)
... ...
@@ -22,6 +22,7 @@ func TestSetGetMeta(t *testing.T) {
22 22
 	assert.NilError(t, err)
23 23
 
24 24
 	store := &VolumeStore{db: db}
25
+	defer store.Shutdown()
25 26
 
26 27
 	_, err = store.getMeta("test")
27 28
 	assert.Assert(t, is.ErrorContains(err, ""))
... ...
@@ -40,6 +40,7 @@ func TestRestore(t *testing.T) {
40 40
 
41 41
 	s, err = NewStore(dir, drivers)
42 42
 	assert.NilError(t, err)
43
+	defer s.Shutdown()
43 44
 
44 45
 	v, err := s.Get(ctx, "test1")
45 46
 	assert.NilError(t, err)
... ...
@@ -121,6 +121,7 @@ func TestList(t *testing.T) {
121 121
 	if err != nil {
122 122
 		t.Fatal(err)
123 123
 	}
124
+	defer s.Shutdown()
124 125
 	ls, _, err = s.Find(ctx, nil)
125 126
 	if err != nil {
126 127
 		t.Fatal(err)