Browse code

Merge pull request #9261 from unclejack/fix_test_tmp_cleanup

fix cleanup of /tmp in tests

Alexander Morozov authored on 2014/11/22 01:48:04
Showing 8 changed files
... ...
@@ -4,16 +4,18 @@ import (
4 4
 	"crypto/sha256"
5 5
 	"encoding/hex"
6 6
 	"fmt"
7
-	"github.com/docker/docker/daemon/graphdriver"
8
-	"github.com/docker/docker/pkg/archive"
9 7
 	"io/ioutil"
10 8
 	"os"
11 9
 	"path"
12 10
 	"testing"
11
+
12
+	"github.com/docker/docker/daemon/graphdriver"
13
+	"github.com/docker/docker/pkg/archive"
13 14
 )
14 15
 
15 16
 var (
16
-	tmp = path.Join(os.TempDir(), "aufs-tests", "aufs")
17
+	tmpOuter = path.Join(os.TempDir(), "aufs-tests")
18
+	tmp      = path.Join(tmpOuter, "aufs")
17 19
 )
18 20
 
19 21
 func testInit(dir string, t *testing.T) graphdriver.Driver {
... ...
@@ -640,8 +642,8 @@ func testMountMoreThan42Layers(t *testing.T, mountPath string) {
640 640
 		t.Fatal(err)
641 641
 	}
642 642
 
643
-	d := testInit(mountPath, t).(*Driver)
644 643
 	defer os.RemoveAll(mountPath)
644
+	d := testInit(mountPath, t).(*Driver)
645 645
 	defer d.Cleanup()
646 646
 	var last string
647 647
 	var expected int
... ...
@@ -662,24 +664,24 @@ func testMountMoreThan42Layers(t *testing.T, mountPath string) {
662 662
 
663 663
 		if err := d.Create(current, parent); err != nil {
664 664
 			t.Logf("Current layer %d", i)
665
-			t.Fatal(err)
665
+			t.Error(err)
666 666
 		}
667 667
 		point, err := d.Get(current, "")
668 668
 		if err != nil {
669 669
 			t.Logf("Current layer %d", i)
670
-			t.Fatal(err)
670
+			t.Error(err)
671 671
 		}
672 672
 		f, err := os.Create(path.Join(point, current))
673 673
 		if err != nil {
674 674
 			t.Logf("Current layer %d", i)
675
-			t.Fatal(err)
675
+			t.Error(err)
676 676
 		}
677 677
 		f.Close()
678 678
 
679 679
 		if i%10 == 0 {
680 680
 			if err := os.Remove(path.Join(point, parent)); err != nil {
681 681
 				t.Logf("Current layer %d", i)
682
-				t.Fatal(err)
682
+				t.Error(err)
683 683
 			}
684 684
 			expected--
685 685
 		}
... ...
@@ -689,28 +691,30 @@ func testMountMoreThan42Layers(t *testing.T, mountPath string) {
689 689
 	// Perform the actual mount for the top most image
690 690
 	point, err := d.Get(last, "")
691 691
 	if err != nil {
692
-		t.Fatal(err)
692
+		t.Error(err)
693 693
 	}
694 694
 	files, err := ioutil.ReadDir(point)
695 695
 	if err != nil {
696
-		t.Fatal(err)
696
+		t.Error(err)
697 697
 	}
698 698
 	if len(files) != expected {
699
-		t.Fatalf("Expected %d got %d", expected, len(files))
699
+		t.Errorf("Expected %d got %d", expected, len(files))
700 700
 	}
701 701
 }
702 702
 
703 703
 func TestMountMoreThan42Layers(t *testing.T) {
704
+	os.RemoveAll(tmpOuter)
704 705
 	testMountMoreThan42Layers(t, tmp)
705 706
 }
706 707
 
707 708
 func TestMountMoreThan42LayersMatchingPathLength(t *testing.T) {
708
-	tmp := "aufs-tests"
709
+	defer os.RemoveAll(tmpOuter)
710
+	zeroes := "0"
709 711
 	for {
710 712
 		// This finds a mount path so that when combined into aufs mount options
711 713
 		// 4096 byte boundary would be in between the paths or in permission
712
-		// section. For '/tmp' it will use '/tmp/aufs-tests00000000/aufs'
713
-		mountPath := path.Join(os.TempDir(), tmp, "aufs")
714
+		// section. For '/tmp' it will use '/tmp/aufs-tests/00000000/aufs'
715
+		mountPath := path.Join(tmpOuter, zeroes, "aufs")
714 716
 		pathLength := 77 + len(mountPath)
715 717
 
716 718
 		if mod := 4095 % pathLength; mod == 0 || mod > pathLength-2 {
... ...
@@ -718,6 +722,6 @@ func TestMountMoreThan42LayersMatchingPathLength(t *testing.T) {
718 718
 			testMountMoreThan42Layers(t, mountPath)
719 719
 			return
720 720
 		}
721
-		tmp += "0"
721
+		zeroes += "0"
722 722
 	}
723 723
 }
... ...
@@ -179,6 +179,7 @@ func TestBuildEnvironmentReplacementAddCopy(t *testing.T) {
179 179
 	if err != nil {
180 180
 		t.Fatal(err)
181 181
 	}
182
+	defer ctx.Close()
182 183
 
183 184
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
184 185
 		t.Fatal(err)
... ...
@@ -632,6 +633,8 @@ func TestBuildSixtySteps(t *testing.T) {
632 632
 	if err != nil {
633 633
 		t.Fatal(err)
634 634
 	}
635
+	defer ctx.Close()
636
+
635 637
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
636 638
 		t.Fatal(err)
637 639
 	}
... ...
@@ -656,6 +659,8 @@ RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
656 656
 	if err != nil {
657 657
 		t.Fatal(err)
658 658
 	}
659
+	defer ctx.Close()
660
+
659 661
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
660 662
 		t.Fatal(err)
661 663
 	}
... ...
@@ -674,6 +679,8 @@ ADD test_file .`,
674 674
 	if err != nil {
675 675
 		t.Fatal(err)
676 676
 	}
677
+	defer ctx.Close()
678
+
677 679
 	done := make(chan struct{})
678 680
 	go func() {
679 681
 		if _, err := buildImageFromContext(name, ctx, true); err != nil {
... ...
@@ -708,6 +715,8 @@ RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio'
708 708
 	if err != nil {
709 709
 		t.Fatal(err)
710 710
 	}
711
+	defer ctx.Close()
712
+
711 713
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
712 714
 		t.Fatal(err)
713 715
 	}
... ...
@@ -947,6 +956,8 @@ RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
947 947
 	if err != nil {
948 948
 		t.Fatal(err)
949 949
 	}
950
+	defer ctx.Close()
951
+
950 952
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
951 953
 		t.Fatal(err)
952 954
 	}
... ...
@@ -971,6 +982,8 @@ RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
971 971
 	if err != nil {
972 972
 		t.Fatal(err)
973 973
 	}
974
+	defer ctx.Close()
975
+
974 976
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
975 977
 		t.Fatal(err)
976 978
 	}
... ...
@@ -996,6 +1009,8 @@ RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]`,
996 996
 	if err != nil {
997 997
 		t.Fatal(err)
998 998
 	}
999
+	defer ctx.Close()
1000
+
999 1001
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1000 1002
 		t.Fatal(err)
1001 1003
 	}
... ...
@@ -1022,6 +1037,8 @@ RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1022 1022
 	if err != nil {
1023 1023
 		t.Fatal(err)
1024 1024
 	}
1025
+	defer ctx.Close()
1026
+
1025 1027
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1026 1028
 		t.Fatal(err)
1027 1029
 	}
... ...
@@ -1040,6 +1057,8 @@ ADD . /`,
1040 1040
 	if err != nil {
1041 1041
 		t.Fatal(err)
1042 1042
 	}
1043
+	defer ctx.Close()
1044
+
1043 1045
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1044 1046
 		t.Fatal(err)
1045 1047
 	}
... ...
@@ -1064,6 +1083,8 @@ RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1064 1064
 	if err != nil {
1065 1065
 		t.Fatal(err)
1066 1066
 	}
1067
+	defer ctx.Close()
1068
+
1067 1069
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1068 1070
 		t.Fatal(err)
1069 1071
 	}
... ...
@@ -1082,6 +1103,8 @@ COPY test_file .`,
1082 1082
 	if err != nil {
1083 1083
 		t.Fatal(err)
1084 1084
 	}
1085
+	defer ctx.Close()
1086
+
1085 1087
 	done := make(chan struct{})
1086 1088
 	go func() {
1087 1089
 		if _, err := buildImageFromContext(name, ctx, true); err != nil {
... ...
@@ -1116,6 +1139,8 @@ RUN [ $(ls -l /exists/exists_file | awk '{print $3":"$4}') = 'dockerio:dockerio'
1116 1116
 	if err != nil {
1117 1117
 		t.Fatal(err)
1118 1118
 	}
1119
+	defer ctx.Close()
1120
+
1119 1121
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1120 1122
 		t.Fatal(err)
1121 1123
 	}
... ...
@@ -1140,6 +1165,8 @@ RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1140 1140
 	if err != nil {
1141 1141
 		t.Fatal(err)
1142 1142
 	}
1143
+	defer ctx.Close()
1144
+
1143 1145
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1144 1146
 		t.Fatal(err)
1145 1147
 	}
... ...
@@ -1163,6 +1190,8 @@ RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1163 1163
 	if err != nil {
1164 1164
 		t.Fatal(err)
1165 1165
 	}
1166
+	defer ctx.Close()
1167
+
1166 1168
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1167 1169
 		t.Fatal(err)
1168 1170
 	}
... ...
@@ -1188,6 +1217,8 @@ RUN [ $(ls -l /exists/test_file | awk '{print $3":"$4}') = 'root:root' ]`,
1188 1188
 	if err != nil {
1189 1189
 		t.Fatal(err)
1190 1190
 	}
1191
+	defer ctx.Close()
1192
+
1191 1193
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1192 1194
 		t.Fatal(err)
1193 1195
 	}
... ...
@@ -1214,6 +1245,8 @@ RUN [ $(ls -l /exists | awk '{print $3":"$4}') = 'dockerio:dockerio' ]`,
1214 1214
 	if err != nil {
1215 1215
 		t.Fatal(err)
1216 1216
 	}
1217
+	defer ctx.Close()
1218
+
1217 1219
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1218 1220
 		t.Fatal(err)
1219 1221
 	}
... ...
@@ -1231,6 +1264,8 @@ COPY . /`,
1231 1231
 	if err != nil {
1232 1232
 		t.Fatal(err)
1233 1233
 	}
1234
+	defer ctx.Close()
1235
+
1234 1236
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1235 1237
 		t.Fatal(err)
1236 1238
 	}
... ...
@@ -1858,6 +1893,7 @@ func TestBuildOnBuildLimitedInheritence(t *testing.T) {
1858 1858
 		if err != nil {
1859 1859
 			t.Fatal(err)
1860 1860
 		}
1861
+		defer ctx.Close()
1861 1862
 
1862 1863
 		out1, _, err := dockerCmdInDir(t, ctx.Dir, "build", "-t", name1, ".")
1863 1864
 		if err != nil {
... ...
@@ -1874,6 +1910,7 @@ func TestBuildOnBuildLimitedInheritence(t *testing.T) {
1874 1874
 		if err != nil {
1875 1875
 			t.Fatal(err)
1876 1876
 		}
1877
+		defer ctx.Close()
1877 1878
 
1878 1879
 		out2, _, err = dockerCmdInDir(t, ctx.Dir, "build", "-t", name2, ".")
1879 1880
 		if err != nil {
... ...
@@ -1890,6 +1927,7 @@ func TestBuildOnBuildLimitedInheritence(t *testing.T) {
1890 1890
 		if err != nil {
1891 1891
 			t.Fatal(err)
1892 1892
 		}
1893
+		defer ctx.Close()
1893 1894
 
1894 1895
 		out3, _, err = dockerCmdInDir(t, ctx.Dir, "build", "-t", name3, ".")
1895 1896
 		if err != nil {
... ...
@@ -2984,6 +3022,8 @@ RUN    [ "$(cat $TO)" = "hello" ]
2984 2984
 	if err != nil {
2985 2985
 		t.Fatal(err)
2986 2986
 	}
2987
+	defer ctx.Close()
2988
+
2987 2989
 	_, err = buildImageFromContext(name, ctx, true)
2988 2990
 	if err != nil {
2989 2991
 		t.Fatal(err)
... ...
@@ -3046,6 +3086,8 @@ RUN [ "$(cat /testfile)" = 'test!' ]`
3046 3046
 	if err != nil {
3047 3047
 		t.Fatal(err)
3048 3048
 	}
3049
+	defer ctx.Close()
3050
+
3049 3051
 	_, err = buildImageFromContext(name, ctx, true)
3050 3052
 	if err != nil {
3051 3053
 		t.Fatal(err)
... ...
@@ -3100,6 +3142,7 @@ RUN cat /existing-directory-trailing-slash/test/foo | grep Hi`
3100 3100
 		}
3101 3101
 		return &FakeContext{Dir: tmpDir}
3102 3102
 	}()
3103
+	defer ctx.Close()
3103 3104
 
3104 3105
 	if _, err := buildImageFromContext(name, ctx, true); err != nil {
3105 3106
 		t.Fatalf("build failed to complete for TestBuildAddTar: %v", err)
... ...
@@ -1257,6 +1257,7 @@ func TestRunWithVolumesIsRecursive(t *testing.T) {
1257 1257
 	if err := mount.Mount("tmpfs", tmpfsDir, "tmpfs", ""); err != nil {
1258 1258
 		t.Fatalf("failed to create a tmpfs mount at %s - %s", tmpfsDir, err)
1259 1259
 	}
1260
+	defer mount.Unmount(tmpfsDir)
1260 1261
 
1261 1262
 	f, err := ioutil.TempFile(tmpfsDir, "touch-me")
1262 1263
 	if err != nil {
... ...
@@ -742,17 +742,20 @@ func NewTempArchive(src Archive, dir string) (*TempArchive, error) {
742 742
 		return nil, err
743 743
 	}
744 744
 	size := st.Size()
745
-	return &TempArchive{f, size}, nil
745
+	return &TempArchive{f, size, 0}, nil
746 746
 }
747 747
 
748 748
 type TempArchive struct {
749 749
 	*os.File
750 750
 	Size int64 // Pre-computed from Stat().Size() as a convenience
751
+	read int64
751 752
 }
752 753
 
753 754
 func (archive *TempArchive) Read(data []byte) (int, error) {
754 755
 	n, err := archive.File.Read(data)
755
-	if err != nil {
756
+	archive.read += int64(n)
757
+	if err != nil || archive.read == archive.Size {
758
+		archive.File.Close()
756 759
 		os.Remove(archive.File.Name())
757 760
 	}
758 761
 	return n, err
... ...
@@ -46,6 +46,7 @@ func TestFollowSymLinkUnderLinkedDir(t *testing.T) {
46 46
 	if err != nil {
47 47
 		t.Fatal(err)
48 48
 	}
49
+	defer os.RemoveAll(dir)
49 50
 
50 51
 	os.Mkdir(filepath.Join(dir, "realdir"), 0700)
51 52
 	os.Symlink("realdir", filepath.Join(dir, "linkdir"))
... ...
@@ -1,11 +1,13 @@
1 1
 package system
2 2
 
3 3
 import (
4
+	"os"
4 5
 	"testing"
5 6
 )
6 7
 
7 8
 func TestLstat(t *testing.T) {
8
-	file, invalid, _ := prepareFiles(t)
9
+	file, invalid, _, dir := prepareFiles(t)
10
+	defer os.RemoveAll(dir)
9 11
 
10 12
 	statFile, err := Lstat(file)
11 13
 	if err != nil {
... ...
@@ -1,12 +1,14 @@
1 1
 package system
2 2
 
3 3
 import (
4
+	"os"
4 5
 	"syscall"
5 6
 	"testing"
6 7
 )
7 8
 
8 9
 func TestFromStatT(t *testing.T) {
9
-	file, _, _ := prepareFiles(t)
10
+	file, _, _, dir := prepareFiles(t)
11
+	defer os.RemoveAll(dir)
10 12
 
11 13
 	stat := &syscall.Stat_t{}
12 14
 	err := syscall.Lstat(file, stat)
... ...
@@ -8,7 +8,7 @@ import (
8 8
 	"testing"
9 9
 )
10 10
 
11
-func prepareFiles(t *testing.T) (string, string, string) {
11
+func prepareFiles(t *testing.T) (string, string, string, string) {
12 12
 	dir, err := ioutil.TempDir("", "docker-system-test")
13 13
 	if err != nil {
14 14
 		t.Fatal(err)
... ...
@@ -26,11 +26,12 @@ func prepareFiles(t *testing.T) (string, string, string) {
26 26
 		t.Fatal(err)
27 27
 	}
28 28
 
29
-	return file, invalid, symlink
29
+	return file, invalid, symlink, dir
30 30
 }
31 31
 
32 32
 func TestLUtimesNano(t *testing.T) {
33
-	file, invalid, symlink := prepareFiles(t)
33
+	file, invalid, symlink, dir := prepareFiles(t)
34
+	defer os.RemoveAll(dir)
34 35
 
35 36
 	before, err := os.Stat(file)
36 37
 	if err != nil {