Browse code

Improve test accuracy for pkg/chrootarchive (part 1)

Check test correctness of untar by comparing destination with
source. For part one, it only compares the directories.

This is a supplement to the #11601 fix.

Signed-off-by: Yestin Sun <yestin.sun@polyera.com>

Yestin Sun authored on 2015/04/09 05:58:08
Showing 1 changed files
... ...
@@ -59,15 +59,15 @@ func TestChrootUntarEmptyArchive(t *testing.T) {
59 59
 	}
60 60
 }
61 61
 
62
-func prepareSourceDirectory(numberOfFiles int, targetPath string, makeLinks bool) (int, error) {
62
+func prepareSourceDirectory(numberOfFiles int, targetPath string, makeSymLinks bool) (int, error) {
63 63
 	fileData := []byte("fooo")
64 64
 	for n := 0; n < numberOfFiles; n++ {
65 65
 		fileName := fmt.Sprintf("file-%d", n)
66 66
 		if err := ioutil.WriteFile(path.Join(targetPath, fileName), fileData, 0700); err != nil {
67 67
 			return 0, err
68 68
 		}
69
-		if makeLinks {
70
-			if err := os.Link(path.Join(targetPath, fileName), path.Join(targetPath, fileName+"-link")); err != nil {
69
+		if makeSymLinks {
70
+			if err := os.Symlink(path.Join(targetPath, fileName), path.Join(targetPath, fileName+"-link")); err != nil {
71 71
 				return 0, err
72 72
 			}
73 73
 		}
... ...
@@ -76,8 +76,19 @@ func prepareSourceDirectory(numberOfFiles int, targetPath string, makeLinks bool
76 76
 	return totalSize, nil
77 77
 }
78 78
 
79
-func TestChrootTarUntarWithSoftLink(t *testing.T) {
80
-	tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntarWithSoftLink")
79
+func compareDirectories(src string, dest string) error {
80
+	changes, err := archive.ChangesDirs(dest, src)
81
+	if err != nil {
82
+		return err
83
+	}
84
+	if len(changes) > 0 {
85
+		return fmt.Errorf("Unexpected differences after untar: %v", changes)
86
+	}
87
+	return nil
88
+}
89
+
90
+func TestChrootTarUntarWithSymlink(t *testing.T) {
91
+	tmpdir, err := ioutil.TempDir("", "docker-TestChrootTarUntarWithSymlink")
81 92
 	if err != nil {
82 93
 		t.Fatal(err)
83 94
 	}
... ...
@@ -93,6 +104,9 @@ func TestChrootTarUntarWithSoftLink(t *testing.T) {
93 93
 	if err := TarUntar(src, dest); err != nil {
94 94
 		t.Fatal(err)
95 95
 	}
96
+	if err := compareDirectories(src, dest); err != nil {
97
+		t.Fatal(err)
98
+	}
96 99
 }
97 100
 
98 101
 func TestChrootCopyWithTar(t *testing.T) {
... ...
@@ -108,19 +122,29 @@ func TestChrootCopyWithTar(t *testing.T) {
108 108
 	if _, err := prepareSourceDirectory(10, src, true); err != nil {
109 109
 		t.Fatal(err)
110 110
 	}
111
-	dest := filepath.Join(tmpdir, "dest")
111
+
112 112
 	// Copy directory
113
+	dest := filepath.Join(tmpdir, "dest")
113 114
 	if err := CopyWithTar(src, dest); err != nil {
114 115
 		t.Fatal(err)
115 116
 	}
117
+	if err := compareDirectories(src, dest); err != nil {
118
+		t.Fatal(err)
119
+	}
120
+
116 121
 	// Copy file
117 122
 	srcfile := filepath.Join(src, "file-1")
118
-	if err := CopyWithTar(srcfile, dest); err != nil {
123
+	dest = filepath.Join(tmpdir, "destFile")
124
+	destfile := filepath.Join(dest, "file-1")
125
+	if err := CopyWithTar(srcfile, destfile); err != nil {
119 126
 		t.Fatal(err)
120 127
 	}
128
+
121 129
 	// Copy symbolic link
122
-	linkfile := filepath.Join(src, "file-1-link")
123
-	if err := CopyWithTar(linkfile, dest); err != nil {
130
+	srcLinkfile := filepath.Join(src, "file-1-link")
131
+	dest = filepath.Join(tmpdir, "destSymlink")
132
+	destLinkfile := filepath.Join(dest, "file-1-link")
133
+	if err := CopyWithTar(srcLinkfile, destLinkfile); err != nil {
124 134
 		t.Fatal(err)
125 135
 	}
126 136
 }
... ...
@@ -138,19 +162,26 @@ func TestChrootCopyFileWithTar(t *testing.T) {
138 138
 	if _, err := prepareSourceDirectory(10, src, true); err != nil {
139 139
 		t.Fatal(err)
140 140
 	}
141
-	dest := filepath.Join(tmpdir, "dest")
141
+
142 142
 	// Copy directory
143
+	dest := filepath.Join(tmpdir, "dest")
143 144
 	if err := CopyFileWithTar(src, dest); err == nil {
144 145
 		t.Fatal("Expected error on copying directory")
145 146
 	}
147
+
146 148
 	// Copy file
147 149
 	srcfile := filepath.Join(src, "file-1")
148
-	if err := CopyFileWithTar(srcfile, dest); err != nil {
150
+	dest = filepath.Join(tmpdir, "destFile")
151
+	destfile := filepath.Join(dest, "file-1")
152
+	if err := CopyFileWithTar(srcfile, destfile); err != nil {
149 153
 		t.Fatal(err)
150 154
 	}
155
+
151 156
 	// Copy symbolic link
152
-	linkfile := filepath.Join(src, "file-1-link")
153
-	if err := CopyFileWithTar(linkfile, dest); err != nil {
157
+	srcLinkfile := filepath.Join(src, "file-1-link")
158
+	dest = filepath.Join(tmpdir, "destSymlink")
159
+	destLinkfile := filepath.Join(dest, "file-1-link")
160
+	if err := CopyFileWithTar(srcLinkfile, destLinkfile); err != nil {
154 161
 		t.Fatal(err)
155 162
 	}
156 163
 }
... ...
@@ -188,6 +219,9 @@ func TestChrootUntarPath(t *testing.T) {
188 188
 	if err := UntarPath(tarfile, dest); err != nil {
189 189
 		t.Fatal(err)
190 190
 	}
191
+	if err := compareDirectories(src, dest); err != nil {
192
+		t.Fatal(err)
193
+	}
191 194
 }
192 195
 
193 196
 type slowEmptyTarReader struct {