Signed-off-by: Tomasz Kopczynski <tomek@kopczynski.net.pl>
Tomasz Kopczynski authored on 2016/05/03 18:10:51... | ... |
@@ -5,7 +5,6 @@ import ( |
5 | 5 |
"bytes" |
6 | 6 |
"io" |
7 | 7 |
"io/ioutil" |
8 |
- "os" |
|
9 | 8 |
"path/filepath" |
10 | 9 |
"runtime" |
11 | 10 |
"strings" |
... | ... |
@@ -14,46 +13,24 @@ import ( |
14 | 14 |
"github.com/docker/docker/pkg/archive" |
15 | 15 |
) |
16 | 16 |
|
17 |
-const ( |
|
18 |
- dockerfileTestName = "Dockerfile-test" |
|
19 |
- dockerfileContent = "FROM busybox" |
|
20 |
-) |
|
21 |
- |
|
22 |
-var prepareEmpty = func(t *testing.T) string { |
|
23 |
- return "" |
|
17 |
+var prepareEmpty = func(t *testing.T) (string, func()) { |
|
18 |
+ return "", func() {} |
|
24 | 19 |
} |
25 | 20 |
|
26 |
-var prepareNoFiles = func(t *testing.T) string { |
|
27 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
28 |
- |
|
29 |
- if err != nil { |
|
30 |
- t.Fatalf("Error when creating temporary directory: %s", err) |
|
31 |
- } |
|
32 |
- |
|
33 |
- return contextDir |
|
21 |
+var prepareNoFiles = func(t *testing.T) (string, func()) { |
|
22 |
+ return createTestTempDir(t, "", "builder-context-test") |
|
34 | 23 |
} |
35 | 24 |
|
36 |
-var prepareOneFile = func(t *testing.T) string { |
|
37 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
38 |
- |
|
39 |
- if err != nil { |
|
40 |
- t.Fatalf("Error when creating temporary directory: %s", err) |
|
41 |
- } |
|
42 |
- |
|
43 |
- dockerfileFilename := filepath.Join(contextDir, dockerfileTestName) |
|
44 |
- err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777) |
|
45 |
- |
|
46 |
- if err != nil { |
|
47 |
- t.Fatalf("Error with writing to file: %s", err) |
|
48 |
- } |
|
49 |
- |
|
50 |
- return contextDir |
|
25 |
+var prepareOneFile = func(t *testing.T) (string, func()) { |
|
26 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") |
|
27 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
28 |
+ return contextDir, cleanup |
|
51 | 29 |
} |
52 | 30 |
|
53 |
-func testValidateContextDirectory(t *testing.T, prepare func(t *testing.T) string, excludes []string) { |
|
54 |
- contextDir := prepare(t) |
|
31 |
+func testValidateContextDirectory(t *testing.T, prepare func(t *testing.T) (string, func()), excludes []string) { |
|
32 |
+ contextDir, cleanup := prepare(t) |
|
33 |
+ defer cleanup() |
|
55 | 34 |
|
56 |
- defer os.RemoveAll(contextDir) |
|
57 | 35 |
err := ValidateContextDirectory(contextDir, excludes) |
58 | 36 |
|
59 | 37 |
if err != nil { |
... | ... |
@@ -62,13 +39,8 @@ func testValidateContextDirectory(t *testing.T, prepare func(t *testing.T) strin |
62 | 62 |
} |
63 | 63 |
|
64 | 64 |
func TestGetContextFromLocalDirNoDockerfile(t *testing.T) { |
65 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
66 |
- |
|
67 |
- defer os.RemoveAll(contextDir) |
|
68 |
- |
|
69 |
- if err != nil { |
|
70 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
71 |
- } |
|
65 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") |
|
66 |
+ defer cleanup() |
|
72 | 67 |
|
73 | 68 |
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "") |
74 | 69 |
|
... | ... |
@@ -86,13 +58,8 @@ func TestGetContextFromLocalDirNoDockerfile(t *testing.T) { |
86 | 86 |
} |
87 | 87 |
|
88 | 88 |
func TestGetContextFromLocalDirNotExistingDir(t *testing.T) { |
89 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
90 |
- |
|
91 |
- if err != nil { |
|
92 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
93 |
- } |
|
94 |
- |
|
95 |
- defer os.RemoveAll(contextDir) |
|
89 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") |
|
90 |
+ defer cleanup() |
|
96 | 91 |
|
97 | 92 |
fakePath := filepath.Join(contextDir, "fake") |
98 | 93 |
|
... | ... |
@@ -112,13 +79,8 @@ func TestGetContextFromLocalDirNotExistingDir(t *testing.T) { |
112 | 112 |
} |
113 | 113 |
|
114 | 114 |
func TestGetContextFromLocalDirNotExistingDockerfile(t *testing.T) { |
115 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
116 |
- |
|
117 |
- if err != nil { |
|
118 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
119 |
- } |
|
120 |
- |
|
121 |
- defer os.RemoveAll(contextDir) |
|
115 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") |
|
116 |
+ defer cleanup() |
|
122 | 117 |
|
123 | 118 |
fakePath := filepath.Join(contextDir, "fake") |
124 | 119 |
|
... | ... |
@@ -138,34 +100,14 @@ func TestGetContextFromLocalDirNotExistingDockerfile(t *testing.T) { |
138 | 138 |
} |
139 | 139 |
|
140 | 140 |
func TestGetContextFromLocalDirWithNoDirectory(t *testing.T) { |
141 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
141 |
+ contextDir, dirCleanup := createTestTempDir(t, "", "builder-context-test") |
|
142 |
+ defer dirCleanup() |
|
142 | 143 |
|
143 |
- if err != nil { |
|
144 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
145 |
- } |
|
146 |
- |
|
147 |
- defer os.RemoveAll(contextDir) |
|
148 |
- |
|
149 |
- dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName) |
|
150 |
- err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777) |
|
144 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
151 | 145 |
|
152 |
- if err != nil { |
|
153 |
- t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err) |
|
154 |
- } |
|
146 |
+ chdirCleanup := chdir(t, contextDir) |
|
147 |
+ defer chdirCleanup() |
|
155 | 148 |
|
156 |
- workingDirectory, err := os.Getwd() |
|
157 |
- |
|
158 |
- if err != nil { |
|
159 |
- t.Fatalf("Error when retrieving working directory: %s", err) |
|
160 |
- } |
|
161 |
- |
|
162 |
- defer os.Chdir(workingDirectory) |
|
163 |
- |
|
164 |
- err = os.Chdir(contextDir) |
|
165 |
- |
|
166 |
- if err != nil { |
|
167 |
- t.Fatalf("Error when changing directory to %s: %s", contextDir, err) |
|
168 |
- } |
|
169 | 149 |
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "") |
170 | 150 |
|
171 | 151 |
if err != nil { |
... | ... |
@@ -182,20 +124,10 @@ func TestGetContextFromLocalDirWithNoDirectory(t *testing.T) { |
182 | 182 |
} |
183 | 183 |
|
184 | 184 |
func TestGetContextFromLocalDirWithDockerfile(t *testing.T) { |
185 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
186 |
- |
|
187 |
- if err != nil { |
|
188 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
189 |
- } |
|
190 |
- |
|
191 |
- defer os.RemoveAll(contextDir) |
|
192 |
- |
|
193 |
- dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName) |
|
194 |
- err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777) |
|
185 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") |
|
186 |
+ defer cleanup() |
|
195 | 187 |
|
196 |
- if err != nil { |
|
197 |
- t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err) |
|
198 |
- } |
|
188 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
199 | 189 |
|
200 | 190 |
absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, "") |
201 | 191 |
|
... | ... |
@@ -213,28 +145,11 @@ func TestGetContextFromLocalDirWithDockerfile(t *testing.T) { |
213 | 213 |
} |
214 | 214 |
|
215 | 215 |
func TestGetContextFromLocalDirLocalFile(t *testing.T) { |
216 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
216 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") |
|
217 |
+ defer cleanup() |
|
217 | 218 |
|
218 |
- if err != nil { |
|
219 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
220 |
- } |
|
221 |
- |
|
222 |
- defer os.RemoveAll(contextDir) |
|
223 |
- |
|
224 |
- dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName) |
|
225 |
- err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777) |
|
226 |
- |
|
227 |
- if err != nil { |
|
228 |
- t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err) |
|
229 |
- } |
|
230 |
- |
|
231 |
- testFilename := filepath.Join(contextDir, "tmpTest") |
|
232 |
- testContent := "test" |
|
233 |
- err = ioutil.WriteFile(testFilename, []byte(testContent), 0777) |
|
234 |
- |
|
235 |
- if err != nil { |
|
236 |
- t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err) |
|
237 |
- } |
|
219 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
220 |
+ testFilename := createTestTempFile(t, contextDir, "tmpTest", "test", 0777) |
|
238 | 221 |
|
239 | 222 |
absContextDir, relDockerfile, err := GetContextFromLocalDir(testFilename, "") |
240 | 223 |
|
... | ... |
@@ -252,36 +167,15 @@ func TestGetContextFromLocalDirLocalFile(t *testing.T) { |
252 | 252 |
} |
253 | 253 |
|
254 | 254 |
func TestGetContextFromLocalDirWithCustomDockerfile(t *testing.T) { |
255 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
256 |
- |
|
257 |
- if err != nil { |
|
258 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
259 |
- } |
|
260 |
- |
|
261 |
- defer os.RemoveAll(contextDir) |
|
262 |
- |
|
263 |
- workingDirectory, err := os.Getwd() |
|
255 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") |
|
256 |
+ defer cleanup() |
|
264 | 257 |
|
265 |
- if err != nil { |
|
266 |
- t.Fatalf("Error when retrieving working directory: %s", err) |
|
267 |
- } |
|
268 |
- |
|
269 |
- defer os.Chdir(workingDirectory) |
|
258 |
+ chdirCleanup := chdir(t, contextDir) |
|
259 |
+ defer chdirCleanup() |
|
270 | 260 |
|
271 |
- err = os.Chdir(contextDir) |
|
272 |
- |
|
273 |
- if err != nil { |
|
274 |
- t.Fatalf("Error when changing directory to %s: %s", contextDir, err) |
|
275 |
- } |
|
261 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
276 | 262 |
|
277 |
- dockerfileFilename := filepath.Join(contextDir, dockerfileTestName) |
|
278 |
- err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777) |
|
279 |
- |
|
280 |
- if err != nil { |
|
281 |
- t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err) |
|
282 |
- } |
|
283 |
- |
|
284 |
- absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, dockerfileTestName) |
|
263 |
+ absContextDir, relDockerfile, err := GetContextFromLocalDir(contextDir, DefaultDockerfileName) |
|
285 | 264 |
|
286 | 265 |
if err != nil { |
287 | 266 |
t.Fatalf("Error when getting context from local dir: %s", err) |
... | ... |
@@ -291,14 +185,14 @@ func TestGetContextFromLocalDirWithCustomDockerfile(t *testing.T) { |
291 | 291 |
t.Fatalf("Absolute directory path should be equal to %s, got: %s", contextDir, absContextDir) |
292 | 292 |
} |
293 | 293 |
|
294 |
- if relDockerfile != dockerfileTestName { |
|
295 |
- t.Fatalf("Relative path to dockerfile should be equal to %s, got: %s", dockerfileTestName, relDockerfile) |
|
294 |
+ if relDockerfile != DefaultDockerfileName { |
|
295 |
+ t.Fatalf("Relative path to dockerfile should be equal to %s, got: %s", DefaultDockerfileName, relDockerfile) |
|
296 | 296 |
} |
297 | 297 |
|
298 | 298 |
} |
299 | 299 |
|
300 | 300 |
func TestGetContextFromReaderString(t *testing.T) { |
301 |
- tarArchive, relDockerfile, err := GetContextFromReader(ioutil.NopCloser(strings.NewReader(dockerfileContent)), "") |
|
301 |
+ tarArchive, relDockerfile, err := GetContextFromReader(ioutil.NopCloser(strings.NewReader(dockerfileContents)), "") |
|
302 | 302 |
|
303 | 303 |
if err != nil { |
304 | 304 |
t.Fatalf("Error when executing GetContextFromReader: %s", err) |
... | ... |
@@ -326,8 +220,8 @@ func TestGetContextFromReaderString(t *testing.T) { |
326 | 326 |
t.Fatalf("Error when closing tar stream: %s", err) |
327 | 327 |
} |
328 | 328 |
|
329 |
- if dockerfileContent != contents { |
|
330 |
- t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContent, contents) |
|
329 |
+ if dockerfileContents != contents { |
|
330 |
+ t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContents, contents) |
|
331 | 331 |
} |
332 | 332 |
|
333 | 333 |
if relDockerfile != DefaultDockerfileName { |
... | ... |
@@ -336,20 +230,10 @@ func TestGetContextFromReaderString(t *testing.T) { |
336 | 336 |
} |
337 | 337 |
|
338 | 338 |
func TestGetContextFromReaderTar(t *testing.T) { |
339 |
- contextDir, err := ioutil.TempDir("", "builder-context-test") |
|
340 |
- |
|
341 |
- if err != nil { |
|
342 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
343 |
- } |
|
344 |
- |
|
345 |
- defer os.RemoveAll(contextDir) |
|
339 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-context-test") |
|
340 |
+ defer cleanup() |
|
346 | 341 |
|
347 |
- dockerfileFilename := filepath.Join(contextDir, dockerfileTestName) |
|
348 |
- err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777) |
|
349 |
- |
|
350 |
- if err != nil { |
|
351 |
- t.Fatalf("Error when writing file (%s) contents: %s", dockerfileFilename, err) |
|
352 |
- } |
|
342 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
353 | 343 |
|
354 | 344 |
tarStream, err := archive.Tar(contextDir, archive.Uncompressed) |
355 | 345 |
|
... | ... |
@@ -357,7 +241,7 @@ func TestGetContextFromReaderTar(t *testing.T) { |
357 | 357 |
t.Fatalf("Error when creating tar: %s", err) |
358 | 358 |
} |
359 | 359 |
|
360 |
- tarArchive, relDockerfile, err := GetContextFromReader(tarStream, dockerfileTestName) |
|
360 |
+ tarArchive, relDockerfile, err := GetContextFromReader(tarStream, DefaultDockerfileName) |
|
361 | 361 |
|
362 | 362 |
if err != nil { |
363 | 363 |
t.Fatalf("Error when executing GetContextFromReader: %s", err) |
... | ... |
@@ -371,8 +255,8 @@ func TestGetContextFromReaderTar(t *testing.T) { |
371 | 371 |
t.Fatalf("Error when reading tar archive: %s", err) |
372 | 372 |
} |
373 | 373 |
|
374 |
- if header.Name != dockerfileTestName { |
|
375 |
- t.Fatalf("Dockerfile name should be: %s, got: %s", dockerfileTestName, header.Name) |
|
374 |
+ if header.Name != DefaultDockerfileName { |
|
375 |
+ t.Fatalf("Dockerfile name should be: %s, got: %s", DefaultDockerfileName, header.Name) |
|
376 | 376 |
} |
377 | 377 |
|
378 | 378 |
buff := new(bytes.Buffer) |
... | ... |
@@ -389,12 +273,12 @@ func TestGetContextFromReaderTar(t *testing.T) { |
389 | 389 |
t.Fatalf("Error when closing tar stream: %s", err) |
390 | 390 |
} |
391 | 391 |
|
392 |
- if dockerfileContent != contents { |
|
393 |
- t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContent, contents) |
|
392 |
+ if dockerfileContents != contents { |
|
393 |
+ t.Fatalf("Uncompressed tar archive does not equal: %s, got: %s", dockerfileContents, contents) |
|
394 | 394 |
} |
395 | 395 |
|
396 |
- if relDockerfile != dockerfileTestName { |
|
397 |
- t.Fatalf("Relative path not equals %s, got: %s", dockerfileTestName, relDockerfile) |
|
396 |
+ if relDockerfile != DefaultDockerfileName { |
|
397 |
+ t.Fatalf("Relative path not equals %s, got: %s", DefaultDockerfileName, relDockerfile) |
|
398 | 398 |
} |
399 | 399 |
} |
400 | 400 |
|
... | ... |
@@ -419,5 +303,5 @@ func TestValidateContextDirectoryWithOneFile(t *testing.T) { |
419 | 419 |
} |
420 | 420 |
|
421 | 421 |
func TestValidateContextDirectoryWithOneFileExcludes(t *testing.T) { |
422 |
- testValidateContextDirectory(t, prepareOneFile, []string{dockerfileTestName}) |
|
422 |
+ testValidateContextDirectory(t, prepareOneFile, []string{DefaultDockerfileName}) |
|
423 | 423 |
} |
... | ... |
@@ -4,174 +4,92 @@ import ( |
4 | 4 |
"io/ioutil" |
5 | 5 |
"log" |
6 | 6 |
"os" |
7 |
- "path/filepath" |
|
7 |
+ "sort" |
|
8 | 8 |
"testing" |
9 | 9 |
) |
10 | 10 |
|
11 |
-func TestProcessShouldRemoveDockerfileDockerignore(t *testing.T) { |
|
12 |
- contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test") |
|
11 |
+const shouldStayFilename = "should_stay" |
|
13 | 12 |
|
14 |
- if err != nil { |
|
15 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
13 |
+func extractFilenames(files []os.FileInfo) []string { |
|
14 |
+ filenames := make([]string, len(files), len(files)) |
|
15 |
+ |
|
16 |
+ for i, file := range files { |
|
17 |
+ filenames[i] = file.Name() |
|
16 | 18 |
} |
17 | 19 |
|
18 |
- defer os.RemoveAll(contextDir) |
|
20 |
+ return filenames |
|
21 |
+} |
|
19 | 22 |
|
20 |
- testFilename := filepath.Join(contextDir, "should_stay") |
|
21 |
- testContent := "test" |
|
22 |
- err = ioutil.WriteFile(testFilename, []byte(testContent), 0777) |
|
23 |
+func checkDirectory(t *testing.T, dir string, expectedFiles []string) { |
|
24 |
+ files, err := ioutil.ReadDir(dir) |
|
23 | 25 |
|
24 | 26 |
if err != nil { |
25 |
- t.Fatalf("Error when creating should_stay file: %s", err) |
|
27 |
+ t.Fatalf("Could not read directory: %s", err) |
|
26 | 28 |
} |
27 | 29 |
|
28 |
- dockerignoreFilename := filepath.Join(contextDir, ".dockerignore") |
|
29 |
- dockerignoreContent := "Dockerfile\n.dockerignore" |
|
30 |
- err = ioutil.WriteFile(dockerignoreFilename, []byte(dockerignoreContent), 0777) |
|
31 |
- |
|
32 |
- if err != nil { |
|
33 |
- t.Fatalf("Error when creating .dockerignore file: %s", err) |
|
30 |
+ if len(files) != len(expectedFiles) { |
|
31 |
+ log.Fatalf("Directory should contain exactly %d file(s), got %d", len(expectedFiles), len(files)) |
|
34 | 32 |
} |
35 | 33 |
|
36 |
- dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName) |
|
37 |
- dockerfileContent := "FROM busybox" |
|
38 |
- err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777) |
|
34 |
+ filenames := extractFilenames(files) |
|
35 |
+ sort.Strings(filenames) |
|
36 |
+ sort.Strings(expectedFiles) |
|
39 | 37 |
|
40 |
- if err != nil { |
|
41 |
- t.Fatalf("Error when creating Dockerfile file: %s", err) |
|
38 |
+ for i, filename := range filenames { |
|
39 |
+ if filename != expectedFiles[i] { |
|
40 |
+ t.Fatalf("File %s should be in the directory, got: %s", expectedFiles[i], filename) |
|
41 |
+ } |
|
42 | 42 |
} |
43 |
+} |
|
43 | 44 |
|
45 |
+func executeProcess(t *testing.T, contextDir string) { |
|
44 | 46 |
modifiableCtx := &tarSumContext{root: contextDir} |
45 | 47 |
ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx} |
46 | 48 |
|
47 |
- err = ctx.Process([]string{DefaultDockerfileName}) |
|
49 |
+ err := ctx.Process([]string{DefaultDockerfileName}) |
|
48 | 50 |
|
49 | 51 |
if err != nil { |
50 | 52 |
t.Fatalf("Error when executing Process: %s", err) |
51 | 53 |
} |
54 |
+} |
|
52 | 55 |
|
53 |
- files, err := ioutil.ReadDir(contextDir) |
|
56 |
+func TestProcessShouldRemoveDockerfileDockerignore(t *testing.T) { |
|
57 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test") |
|
58 |
+ defer cleanup() |
|
54 | 59 |
|
55 |
- if err != nil { |
|
56 |
- t.Fatalf("Could not read directory: %s", err) |
|
57 |
- } |
|
60 |
+ createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777) |
|
61 |
+ createTestTempFile(t, contextDir, dockerignoreFilename, "Dockerfile\n.dockerignore", 0777) |
|
62 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
58 | 63 |
|
59 |
- if len(files) != 1 { |
|
60 |
- log.Fatal("Directory should contain exactly one file") |
|
61 |
- } |
|
64 |
+ executeProcess(t, contextDir) |
|
62 | 65 |
|
63 |
- for _, file := range files { |
|
64 |
- if "should_stay" != file.Name() { |
|
65 |
- log.Fatalf("File %s should not be in the directory", file.Name()) |
|
66 |
- } |
|
67 |
- } |
|
66 |
+ checkDirectory(t, contextDir, []string{shouldStayFilename}) |
|
68 | 67 |
|
69 | 68 |
} |
70 | 69 |
|
71 | 70 |
func TestProcessNoDockerignore(t *testing.T) { |
72 |
- contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test") |
|
71 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test") |
|
72 |
+ defer cleanup() |
|
73 | 73 |
|
74 |
- if err != nil { |
|
75 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
76 |
- } |
|
77 |
- |
|
78 |
- defer os.RemoveAll(contextDir) |
|
79 |
- |
|
80 |
- testFilename := filepath.Join(contextDir, "should_stay") |
|
81 |
- testContent := "test" |
|
82 |
- err = ioutil.WriteFile(testFilename, []byte(testContent), 0777) |
|
83 |
- |
|
84 |
- if err != nil { |
|
85 |
- t.Fatalf("Error when creating should_stay file: %s", err) |
|
86 |
- } |
|
87 |
- |
|
88 |
- dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName) |
|
89 |
- dockerfileContent := "FROM busybox" |
|
90 |
- err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777) |
|
91 |
- |
|
92 |
- if err != nil { |
|
93 |
- t.Fatalf("Error when creating Dockerfile file: %s", err) |
|
94 |
- } |
|
95 |
- |
|
96 |
- modifiableCtx := &tarSumContext{root: contextDir} |
|
97 |
- ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx} |
|
98 |
- |
|
99 |
- ctx.Process([]string{DefaultDockerfileName}) |
|
100 |
- |
|
101 |
- files, err := ioutil.ReadDir(contextDir) |
|
102 |
- |
|
103 |
- if err != nil { |
|
104 |
- t.Fatalf("Could not read directory: %s", err) |
|
105 |
- } |
|
74 |
+ createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777) |
|
75 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
106 | 76 |
|
107 |
- if len(files) != 2 { |
|
108 |
- log.Fatal("Directory should contain exactly two files") |
|
109 |
- } |
|
77 |
+ executeProcess(t, contextDir) |
|
110 | 78 |
|
111 |
- for _, file := range files { |
|
112 |
- if "should_stay" != file.Name() && DefaultDockerfileName != file.Name() { |
|
113 |
- log.Fatalf("File %s should not be in the directory", file.Name()) |
|
114 |
- } |
|
115 |
- } |
|
79 |
+ checkDirectory(t, contextDir, []string{shouldStayFilename, DefaultDockerfileName}) |
|
116 | 80 |
|
117 | 81 |
} |
118 | 82 |
|
119 | 83 |
func TestProcessShouldLeaveAllFiles(t *testing.T) { |
120 |
- contextDir, err := ioutil.TempDir("", "builder-dockerignore-process-test") |
|
121 |
- |
|
122 |
- if err != nil { |
|
123 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
124 |
- } |
|
125 |
- |
|
126 |
- defer os.RemoveAll(contextDir) |
|
127 |
- |
|
128 |
- testFilename := filepath.Join(contextDir, "should_stay") |
|
129 |
- testContent := "test" |
|
130 |
- err = ioutil.WriteFile(testFilename, []byte(testContent), 0777) |
|
131 |
- |
|
132 |
- if err != nil { |
|
133 |
- t.Fatalf("Error when creating should_stay file: %s", err) |
|
134 |
- } |
|
135 |
- |
|
136 |
- dockerignoreFilename := filepath.Join(contextDir, ".dockerignore") |
|
137 |
- dockerignoreContent := "input1\ninput2" |
|
138 |
- err = ioutil.WriteFile(dockerignoreFilename, []byte(dockerignoreContent), 0777) |
|
139 |
- |
|
140 |
- if err != nil { |
|
141 |
- t.Fatalf("Error when creating .dockerignore file: %s", err) |
|
142 |
- } |
|
143 |
- |
|
144 |
- dockerfileFilename := filepath.Join(contextDir, DefaultDockerfileName) |
|
145 |
- dockerfileContent := "FROM busybox" |
|
146 |
- err = ioutil.WriteFile(dockerfileFilename, []byte(dockerfileContent), 0777) |
|
84 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test") |
|
85 |
+ defer cleanup() |
|
147 | 86 |
|
148 |
- if err != nil { |
|
149 |
- t.Fatalf("Error when creating Dockerfile file: %s", err) |
|
150 |
- } |
|
151 |
- |
|
152 |
- modifiableCtx := &tarSumContext{root: contextDir} |
|
153 |
- ctx := DockerIgnoreContext{ModifiableContext: modifiableCtx} |
|
154 |
- |
|
155 |
- err = ctx.Process([]string{DefaultDockerfileName}) |
|
156 |
- |
|
157 |
- if err != nil { |
|
158 |
- t.Fatalf("Error when executing Process: %s", err) |
|
159 |
- } |
|
160 |
- |
|
161 |
- files, err := ioutil.ReadDir(contextDir) |
|
87 |
+ createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777) |
|
88 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
89 |
+ createTestTempFile(t, contextDir, dockerignoreFilename, "input1\ninput2", 0777) |
|
162 | 90 |
|
163 |
- if err != nil { |
|
164 |
- t.Fatalf("Could not read directory: %s", err) |
|
165 |
- } |
|
91 |
+ executeProcess(t, contextDir) |
|
166 | 92 |
|
167 |
- if len(files) != 3 { |
|
168 |
- log.Fatal("Directory should contain exactly three files") |
|
169 |
- } |
|
170 |
- |
|
171 |
- for _, file := range files { |
|
172 |
- if "should_stay" != file.Name() && DefaultDockerfileName != file.Name() && ".dockerignore" != file.Name() { |
|
173 |
- log.Fatalf("File %s should not be in the directory", file.Name()) |
|
174 |
- } |
|
175 |
- } |
|
93 |
+ checkDirectory(t, contextDir, []string{shouldStayFilename, DefaultDockerfileName, dockerignoreFilename}) |
|
176 | 94 |
|
177 | 95 |
} |
... | ... |
@@ -7,15 +7,12 @@ import ( |
7 | 7 |
"net/http" |
8 | 8 |
"net/http/httptest" |
9 | 9 |
"net/url" |
10 |
- "os" |
|
11 |
- "path/filepath" |
|
12 | 10 |
"testing" |
13 | 11 |
|
14 | 12 |
"github.com/docker/docker/pkg/archive" |
15 | 13 |
"github.com/docker/docker/pkg/httputils" |
16 | 14 |
) |
17 | 15 |
|
18 |
-var textPlainDockerfile = "FROM busybox" |
|
19 | 16 |
var binaryContext = []byte{0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00} //xz magic |
20 | 17 |
|
21 | 18 |
func TestSelectAcceptableMIME(t *testing.T) { |
... | ... |
@@ -95,10 +92,10 @@ func TestInspectResponseBinary(t *testing.T) { |
95 | 95 |
} |
96 | 96 |
|
97 | 97 |
func TestResponseUnsupportedContentType(t *testing.T) { |
98 |
- content := []byte(textPlainDockerfile) |
|
98 |
+ content := []byte(dockerfileContents) |
|
99 | 99 |
ct := "application/json" |
100 | 100 |
br := ioutil.NopCloser(bytes.NewReader(content)) |
101 |
- contentType, bReader, err := inspectResponse(ct, br, int64(len(textPlainDockerfile))) |
|
101 |
+ contentType, bReader, err := inspectResponse(ct, br, int64(len(dockerfileContents))) |
|
102 | 102 |
|
103 | 103 |
if err == nil { |
104 | 104 |
t.Fatal("Should have returned an error on content-type 'application/json'") |
... | ... |
@@ -110,13 +107,13 @@ func TestResponseUnsupportedContentType(t *testing.T) { |
110 | 110 |
if err != nil { |
111 | 111 |
t.Fatal(err) |
112 | 112 |
} |
113 |
- if string(body) != textPlainDockerfile { |
|
113 |
+ if string(body) != dockerfileContents { |
|
114 | 114 |
t.Fatalf("Corrupted response body %s", body) |
115 | 115 |
} |
116 | 116 |
} |
117 | 117 |
|
118 | 118 |
func TestInspectResponseTextSimple(t *testing.T) { |
119 |
- content := []byte(textPlainDockerfile) |
|
119 |
+ content := []byte(dockerfileContents) |
|
120 | 120 |
ct := "text/plain" |
121 | 121 |
br := ioutil.NopCloser(bytes.NewReader(content)) |
122 | 122 |
contentType, bReader, err := inspectResponse(ct, br, int64(len(content))) |
... | ... |
@@ -130,13 +127,13 @@ func TestInspectResponseTextSimple(t *testing.T) { |
130 | 130 |
if err != nil { |
131 | 131 |
t.Fatal(err) |
132 | 132 |
} |
133 |
- if string(body) != textPlainDockerfile { |
|
133 |
+ if string(body) != dockerfileContents { |
|
134 | 134 |
t.Fatalf("Corrupted response body %s", body) |
135 | 135 |
} |
136 | 136 |
} |
137 | 137 |
|
138 | 138 |
func TestInspectResponseEmptyContentType(t *testing.T) { |
139 |
- content := []byte(textPlainDockerfile) |
|
139 |
+ content := []byte(dockerfileContents) |
|
140 | 140 |
br := ioutil.NopCloser(bytes.NewReader(content)) |
141 | 141 |
contentType, bodyReader, err := inspectResponse("", br, int64(len(content))) |
142 | 142 |
if err != nil { |
... | ... |
@@ -149,26 +146,16 @@ func TestInspectResponseEmptyContentType(t *testing.T) { |
149 | 149 |
if err != nil { |
150 | 150 |
t.Fatal(err) |
151 | 151 |
} |
152 |
- if string(body) != textPlainDockerfile { |
|
152 |
+ if string(body) != dockerfileContents { |
|
153 | 153 |
t.Fatalf("Corrupted response body %s", body) |
154 | 154 |
} |
155 | 155 |
} |
156 | 156 |
|
157 | 157 |
func TestMakeRemoteContext(t *testing.T) { |
158 |
- contextDir, err := ioutil.TempDir("", "builder-remote-test") |
|
158 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
159 |
+ defer cleanup() |
|
159 | 160 |
|
160 |
- if err != nil { |
|
161 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
162 |
- } |
|
163 |
- |
|
164 |
- defer os.RemoveAll(contextDir) |
|
165 |
- |
|
166 |
- testFilename := filepath.Join(contextDir, DefaultDockerfileName) |
|
167 |
- err = ioutil.WriteFile(testFilename, []byte(textPlainDockerfile), 0777) |
|
168 |
- |
|
169 |
- if err != nil { |
|
170 |
- t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err) |
|
171 |
- } |
|
161 |
+ createTestTempFile(t, contextDir, DefaultDockerfileName, dockerfileContents, 0777) |
|
172 | 162 |
|
173 | 163 |
mux := http.NewServeMux() |
174 | 164 |
server := httptest.NewServer(mux) |
... | ... |
@@ -45,20 +45,10 @@ func TestCloseRootDirectory(t *testing.T) { |
45 | 45 |
} |
46 | 46 |
|
47 | 47 |
func TestOpenFile(t *testing.T) { |
48 |
- contextDir, err := ioutil.TempDir("", "builder-tarsum-test") |
|
49 |
- |
|
50 |
- if err != nil { |
|
51 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
52 |
- } |
|
53 |
- |
|
54 |
- defer os.RemoveAll(contextDir) |
|
48 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
49 |
+ defer cleanup() |
|
55 | 50 |
|
56 |
- testFilename := filepath.Join(contextDir, filename) |
|
57 |
- err = ioutil.WriteFile(testFilename, []byte(contents), 0777) |
|
58 |
- |
|
59 |
- if err != nil { |
|
60 |
- t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err) |
|
61 |
- } |
|
51 |
+ createTestTempFile(t, contextDir, filename, contents, 0777) |
|
62 | 52 |
|
63 | 53 |
tarSum := &tarSumContext{root: contextDir} |
64 | 54 |
|
... | ... |
@@ -84,13 +74,8 @@ func TestOpenFile(t *testing.T) { |
84 | 84 |
} |
85 | 85 |
|
86 | 86 |
func TestOpenNotExisting(t *testing.T) { |
87 |
- contextDir, err := ioutil.TempDir("", "builder-tarsum-test") |
|
88 |
- |
|
89 |
- if err != nil { |
|
90 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
91 |
- } |
|
92 |
- |
|
93 |
- defer os.RemoveAll(contextDir) |
|
87 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
88 |
+ defer cleanup() |
|
94 | 89 |
|
95 | 90 |
tarSum := &tarSumContext{root: contextDir} |
96 | 91 |
|
... | ... |
@@ -106,20 +91,10 @@ func TestOpenNotExisting(t *testing.T) { |
106 | 106 |
} |
107 | 107 |
|
108 | 108 |
func TestStatFile(t *testing.T) { |
109 |
- contextDir, err := ioutil.TempDir("", "builder-tarsum-test") |
|
110 |
- |
|
111 |
- if err != nil { |
|
112 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
113 |
- } |
|
114 |
- |
|
115 |
- defer os.RemoveAll(contextDir) |
|
109 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
110 |
+ defer cleanup() |
|
116 | 111 |
|
117 |
- testFilename := filepath.Join(contextDir, filename) |
|
118 |
- err = ioutil.WriteFile(testFilename, []byte(contents), 0777) |
|
119 |
- |
|
120 |
- if err != nil { |
|
121 |
- t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err) |
|
122 |
- } |
|
112 |
+ testFilename := createTestTempFile(t, contextDir, filename, contents, 0777) |
|
123 | 113 |
|
124 | 114 |
tarSum := &tarSumContext{root: contextDir} |
125 | 115 |
|
... | ... |
@@ -139,26 +114,12 @@ func TestStatFile(t *testing.T) { |
139 | 139 |
} |
140 | 140 |
|
141 | 141 |
func TestStatSubdir(t *testing.T) { |
142 |
- contextDir, err := ioutil.TempDir("", "builder-tarsum-test") |
|
143 |
- |
|
144 |
- if err != nil { |
|
145 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
146 |
- } |
|
147 |
- |
|
148 |
- defer os.RemoveAll(contextDir) |
|
149 |
- |
|
150 |
- contextSubdir, err := ioutil.TempDir(contextDir, "builder-tarsum-test-subdir") |
|
142 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
143 |
+ defer cleanup() |
|
151 | 144 |
|
152 |
- if err != nil { |
|
153 |
- t.Fatalf("Error with creating temporary subdirectory: %s", err) |
|
154 |
- } |
|
155 |
- |
|
156 |
- testFilename := filepath.Join(contextSubdir, filename) |
|
157 |
- err = ioutil.WriteFile(testFilename, []byte(contents), 0777) |
|
145 |
+ contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir") |
|
158 | 146 |
|
159 |
- if err != nil { |
|
160 |
- t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err) |
|
161 |
- } |
|
147 |
+ testFilename := createTestTempFile(t, contextSubdir, filename, contents, 0777) |
|
162 | 148 |
|
163 | 149 |
tarSum := &tarSumContext{root: contextDir} |
164 | 150 |
|
... | ... |
@@ -184,13 +145,8 @@ func TestStatSubdir(t *testing.T) { |
184 | 184 |
} |
185 | 185 |
|
186 | 186 |
func TestStatNotExisting(t *testing.T) { |
187 |
- contextDir, err := ioutil.TempDir("", "builder-tarsum-test") |
|
188 |
- |
|
189 |
- if err != nil { |
|
190 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
191 |
- } |
|
192 |
- |
|
193 |
- defer os.RemoveAll(contextDir) |
|
187 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
188 |
+ defer cleanup() |
|
194 | 189 |
|
195 | 190 |
tarSum := &tarSumContext{root: contextDir} |
196 | 191 |
|
... | ... |
@@ -210,19 +166,10 @@ func TestStatNotExisting(t *testing.T) { |
210 | 210 |
} |
211 | 211 |
|
212 | 212 |
func TestRemoveDirectory(t *testing.T) { |
213 |
- contextDir, err := ioutil.TempDir("", "builder-tarsum-test") |
|
214 |
- |
|
215 |
- if err != nil { |
|
216 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
217 |
- } |
|
218 |
- |
|
219 |
- defer os.RemoveAll(contextDir) |
|
220 |
- |
|
221 |
- contextSubdir, err := ioutil.TempDir(contextDir, "builder-tarsum-test-subdir") |
|
213 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
214 |
+ defer cleanup() |
|
222 | 215 |
|
223 |
- if err != nil { |
|
224 |
- t.Fatalf("Error with creating temporary subdirectory: %s", err) |
|
225 |
- } |
|
216 |
+ contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir") |
|
226 | 217 |
|
227 | 218 |
relativePath, err := filepath.Rel(contextDir, contextSubdir) |
228 | 219 |
|
... | ... |
@@ -246,20 +193,10 @@ func TestRemoveDirectory(t *testing.T) { |
246 | 246 |
} |
247 | 247 |
|
248 | 248 |
func TestMakeSumTarContext(t *testing.T) { |
249 |
- contextDir, err := ioutil.TempDir("", "builder-tarsum-test") |
|
250 |
- |
|
251 |
- if err != nil { |
|
252 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
253 |
- } |
|
249 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
250 |
+ defer cleanup() |
|
254 | 251 |
|
255 |
- defer os.RemoveAll(contextDir) |
|
256 |
- |
|
257 |
- testFilename := filepath.Join(contextDir, filename) |
|
258 |
- err = ioutil.WriteFile(testFilename, []byte(contents), 0644) |
|
259 |
- |
|
260 |
- if err != nil { |
|
261 |
- t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err) |
|
262 |
- } |
|
252 |
+ createTestTempFile(t, contextDir, filename, contents, 0777) |
|
263 | 253 |
|
264 | 254 |
tarStream, err := archive.Tar(contextDir, archive.Uncompressed) |
265 | 255 |
|
... | ... |
@@ -281,26 +218,12 @@ func TestMakeSumTarContext(t *testing.T) { |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
func TestWalkWithoutError(t *testing.T) { |
284 |
- contextDir, err := ioutil.TempDir("", "builder-tarsum-test") |
|
284 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
285 |
+ defer cleanup() |
|
285 | 286 |
|
286 |
- if err != nil { |
|
287 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
288 |
- } |
|
289 |
- |
|
290 |
- defer os.RemoveAll(contextDir) |
|
291 |
- |
|
292 |
- contextSubdir, err := ioutil.TempDir(contextDir, "builder-tarsum-test-subdir") |
|
287 |
+ contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir") |
|
293 | 288 |
|
294 |
- if err != nil { |
|
295 |
- t.Fatalf("Error with creating temporary subdirectory: %s", err) |
|
296 |
- } |
|
297 |
- |
|
298 |
- testFilename := filepath.Join(contextSubdir, filename) |
|
299 |
- err = ioutil.WriteFile(testFilename, []byte(contents), 0777) |
|
300 |
- |
|
301 |
- if err != nil { |
|
302 |
- t.Fatalf("Error when writing file (%s) contents: %s", testFilename, err) |
|
303 |
- } |
|
289 |
+ createTestTempFile(t, contextSubdir, filename, contents, 0777) |
|
304 | 290 |
|
305 | 291 |
tarSum := &tarSumContext{root: contextDir} |
306 | 292 |
|
... | ... |
@@ -308,7 +231,7 @@ func TestWalkWithoutError(t *testing.T) { |
308 | 308 |
return nil |
309 | 309 |
} |
310 | 310 |
|
311 |
- err = tarSum.Walk(contextSubdir, walkFun) |
|
311 |
+ err := tarSum.Walk(contextSubdir, walkFun) |
|
312 | 312 |
|
313 | 313 |
if err != nil { |
314 | 314 |
t.Fatalf("Error when executing Walk: %s", err) |
... | ... |
@@ -323,19 +246,10 @@ func (we WalkError) Error() string { |
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
func TestWalkWithError(t *testing.T) { |
326 |
- contextDir, err := ioutil.TempDir("", "builder-tarsum-test") |
|
327 |
- |
|
328 |
- if err != nil { |
|
329 |
- t.Fatalf("Error with creating temporary directory: %s", err) |
|
330 |
- } |
|
331 |
- |
|
332 |
- defer os.RemoveAll(contextDir) |
|
333 |
- |
|
334 |
- contextSubdir, err := ioutil.TempDir(contextDir, "builder-tarsum-test-subdir") |
|
326 |
+ contextDir, cleanup := createTestTempDir(t, "", "builder-tarsum-test") |
|
327 |
+ defer cleanup() |
|
335 | 328 |
|
336 |
- if err != nil { |
|
337 |
- t.Fatalf("Error with creating temporary subdirectory: %s", err) |
|
338 |
- } |
|
329 |
+ contextSubdir := createTestTempSubdir(t, contextDir, "builder-tarsum-test-subdir") |
|
339 | 330 |
|
340 | 331 |
tarSum := &tarSumContext{root: contextDir} |
341 | 332 |
|
... | ... |
@@ -343,7 +257,7 @@ func TestWalkWithError(t *testing.T) { |
343 | 343 |
return WalkError{} |
344 | 344 |
} |
345 | 345 |
|
346 |
- err = tarSum.Walk(contextSubdir, walkFun) |
|
346 |
+ err := tarSum.Walk(contextSubdir, walkFun) |
|
347 | 347 |
|
348 | 348 |
if err == nil { |
349 | 349 |
t.Fatalf("Error should not be nil") |
350 | 350 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,87 @@ |
0 |
+package builder |
|
1 |
+ |
|
2 |
+import ( |
|
3 |
+ "io/ioutil" |
|
4 |
+ "os" |
|
5 |
+ "path/filepath" |
|
6 |
+ "testing" |
|
7 |
+) |
|
8 |
+ |
|
9 |
+const ( |
|
10 |
+ dockerfileContents = "FROM busybox" |
|
11 |
+ dockerignoreFilename = ".dockerignore" |
|
12 |
+ testfileContents = "test" |
|
13 |
+) |
|
14 |
+ |
|
15 |
+// createTestTempDir creates a temporary directory for testing. |
|
16 |
+// It returns the created path and a cleanup function which is meant to be used as deferred call. |
|
17 |
+// When an error occurs, it terminates the test. |
|
18 |
+func createTestTempDir(t *testing.T, dir, prefix string) (string, func()) { |
|
19 |
+ path, err := ioutil.TempDir(dir, prefix) |
|
20 |
+ |
|
21 |
+ if err != nil { |
|
22 |
+ t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err) |
|
23 |
+ } |
|
24 |
+ |
|
25 |
+ return path, func() { |
|
26 |
+ err = os.RemoveAll(path) |
|
27 |
+ |
|
28 |
+ if err != nil { |
|
29 |
+ t.Fatalf("Error when removing directory %s: %s", path, err) |
|
30 |
+ } |
|
31 |
+ } |
|
32 |
+} |
|
33 |
+ |
|
34 |
+// createTestTempSubdir creates a temporary directory for testing. |
|
35 |
+// It returns the created path but doesn't provide a cleanup function, |
|
36 |
+// so createTestTempSubdir should be used only for creating temporary subdirectories |
|
37 |
+// whose parent directories are properly cleaned up. |
|
38 |
+// When an error occurs, it terminates the test. |
|
39 |
+func createTestTempSubdir(t *testing.T, dir, prefix string) string { |
|
40 |
+ path, err := ioutil.TempDir(dir, prefix) |
|
41 |
+ |
|
42 |
+ if err != nil { |
|
43 |
+ t.Fatalf("Error when creating directory %s with prefix %s: %s", dir, prefix, err) |
|
44 |
+ } |
|
45 |
+ |
|
46 |
+ return path |
|
47 |
+} |
|
48 |
+ |
|
49 |
+// createTestTempFile creates a temporary file within dir with specific contents and permissions. |
|
50 |
+// When an error occurs, it terminates the test |
|
51 |
+func createTestTempFile(t *testing.T, dir, filename, contents string, perm os.FileMode) string { |
|
52 |
+ filePath := filepath.Join(dir, filename) |
|
53 |
+ err := ioutil.WriteFile(filePath, []byte(contents), perm) |
|
54 |
+ |
|
55 |
+ if err != nil { |
|
56 |
+ t.Fatalf("Error when creating %s file: %s", filename, err) |
|
57 |
+ } |
|
58 |
+ |
|
59 |
+ return filePath |
|
60 |
+} |
|
61 |
+ |
|
62 |
+// chdir changes current working directory to dir. |
|
63 |
+// It returns a function which changes working directory back to the previous one. |
|
64 |
+// This function is meant to be executed as a deferred call. |
|
65 |
+// When an error occurs, it terminates the test. |
|
66 |
+func chdir(t *testing.T, dir string) func() { |
|
67 |
+ workingDirectory, err := os.Getwd() |
|
68 |
+ |
|
69 |
+ if err != nil { |
|
70 |
+ t.Fatalf("Error when retrieving working directory: %s", err) |
|
71 |
+ } |
|
72 |
+ |
|
73 |
+ err = os.Chdir(dir) |
|
74 |
+ |
|
75 |
+ if err != nil { |
|
76 |
+ t.Fatalf("Error when changing directory to %s: %s", dir, err) |
|
77 |
+ } |
|
78 |
+ |
|
79 |
+ return func() { |
|
80 |
+ err = os.Chdir(workingDirectory) |
|
81 |
+ |
|
82 |
+ if err != nil { |
|
83 |
+ t.Fatalf("Error when changing back to working directory (%s): %s", workingDirectory, err) |
|
84 |
+ } |
|
85 |
+ } |
|
86 |
+} |