Browse code

integcli: run build tests in tmpdir

Docker-DCO-1.1-Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com> (github: unclejack)

unclejack authored on 2014/09/18 01:34:45
Showing 2 changed files
... ...
@@ -60,7 +60,17 @@ func TestBuildSixtySteps(t *testing.T) {
60 60
 }
61 61
 
62 62
 func TestAddSingleFileToRoot(t *testing.T) {
63
-	buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", "SingleFileToRoot")
63
+	testDirName := "SingleFileToRoot"
64
+	sourceDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", testDirName)
65
+	buildDirectory, err := ioutil.TempDir("", "test-build-add")
66
+	defer os.RemoveAll(buildDirectory)
67
+
68
+	err = copyWithCP(sourceDirectory, buildDirectory)
69
+	if err != nil {
70
+		t.Fatalf("failed to copy files to temporary directory: %s", err)
71
+	}
72
+
73
+	buildDirectory = filepath.Join(buildDirectory, testDirName)
64 74
 	f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
65 75
 	if err != nil {
66 76
 		t.Fatal(err)
... ...
@@ -80,7 +90,17 @@ func TestAddSingleFileToRoot(t *testing.T) {
80 80
 
81 81
 // Issue #3960: "ADD src ." hangs
82 82
 func TestAddSingleFileToWorkdir(t *testing.T) {
83
-	buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", "SingleFileToWorkdir")
83
+	testDirName := "SingleFileToWorkdir"
84
+	sourceDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", testDirName)
85
+	buildDirectory, err := ioutil.TempDir("", "test-build-add")
86
+	defer os.RemoveAll(buildDirectory)
87
+
88
+	err = copyWithCP(sourceDirectory, buildDirectory)
89
+	if err != nil {
90
+		t.Fatalf("failed to copy files to temporary directory: %s", err)
91
+	}
92
+
93
+	buildDirectory = filepath.Join(buildDirectory, testDirName)
84 94
 	f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
85 95
 	if err != nil {
86 96
 		t.Fatal(err)
... ...
@@ -171,7 +191,17 @@ func TestAddDirContentToExistDir(t *testing.T) {
171 171
 }
172 172
 
173 173
 func TestAddWholeDirToRoot(t *testing.T) {
174
-	buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", "WholeDirToRoot")
174
+	testDirName := "WholeDirToRoot"
175
+	sourceDirectory := filepath.Join(workingDirectory, "build_tests", "TestAdd", testDirName)
176
+	buildDirectory, err := ioutil.TempDir("", "test-build-add")
177
+	defer os.RemoveAll(buildDirectory)
178
+
179
+	err = copyWithCP(sourceDirectory, buildDirectory)
180
+	if err != nil {
181
+		t.Fatalf("failed to copy files to temporary directory: %s", err)
182
+	}
183
+
184
+	buildDirectory = filepath.Join(buildDirectory, testDirName)
175 185
 	test_dir := filepath.Join(buildDirectory, "test_dir")
176 186
 	if err := os.MkdirAll(test_dir, 0755); err != nil {
177 187
 		t.Fatal(err)
... ...
@@ -207,7 +237,17 @@ func TestAddEtcToRoot(t *testing.T) {
207 207
 }
208 208
 
209 209
 func TestCopySingleFileToRoot(t *testing.T) {
210
-	buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", "SingleFileToRoot")
210
+	testDirName := "SingleFileToRoot"
211
+	sourceDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", testDirName)
212
+	buildDirectory, err := ioutil.TempDir("", "test-build-add")
213
+	defer os.RemoveAll(buildDirectory)
214
+
215
+	err = copyWithCP(sourceDirectory, buildDirectory)
216
+	if err != nil {
217
+		t.Fatalf("failed to copy files to temporary directory: %s", err)
218
+	}
219
+
220
+	buildDirectory = filepath.Join(buildDirectory, testDirName)
211 221
 	f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
212 222
 	if err != nil {
213 223
 		t.Fatal(err)
... ...
@@ -227,7 +267,17 @@ func TestCopySingleFileToRoot(t *testing.T) {
227 227
 
228 228
 // Issue #3960: "ADD src ." hangs - adapted for COPY
229 229
 func TestCopySingleFileToWorkdir(t *testing.T) {
230
-	buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", "SingleFileToWorkdir")
230
+	testDirName := "SingleFileToWorkdir"
231
+	sourceDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", testDirName)
232
+	buildDirectory, err := ioutil.TempDir("", "test-build-add")
233
+	defer os.RemoveAll(buildDirectory)
234
+
235
+	err = copyWithCP(sourceDirectory, buildDirectory)
236
+	if err != nil {
237
+		t.Fatalf("failed to copy files to temporary directory: %s", err)
238
+	}
239
+
240
+	buildDirectory = filepath.Join(buildDirectory, testDirName)
231 241
 	f, err := os.OpenFile(filepath.Join(buildDirectory, "test_file"), os.O_CREATE, 0644)
232 242
 	if err != nil {
233 243
 		t.Fatal(err)
... ...
@@ -318,7 +368,17 @@ func TestCopyDirContentToExistDir(t *testing.T) {
318 318
 }
319 319
 
320 320
 func TestCopyWholeDirToRoot(t *testing.T) {
321
-	buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", "WholeDirToRoot")
321
+	testDirName := "WholeDirToRoot"
322
+	sourceDirectory := filepath.Join(workingDirectory, "build_tests", "TestCopy", testDirName)
323
+	buildDirectory, err := ioutil.TempDir("", "test-build-add")
324
+	defer os.RemoveAll(buildDirectory)
325
+
326
+	err = copyWithCP(sourceDirectory, buildDirectory)
327
+	if err != nil {
328
+		t.Fatalf("failed to copy files to temporary directory: %s", err)
329
+	}
330
+
331
+	buildDirectory = filepath.Join(buildDirectory, testDirName)
322 332
 	test_dir := filepath.Join(buildDirectory, "test_dir")
323 333
 	if err := os.MkdirAll(test_dir, 0755); err != nil {
324 334
 		t.Fatal(err)
... ...
@@ -370,8 +430,18 @@ func TestCopyDisallowRemote(t *testing.T) {
370 370
 // Issue #5270 - ensure we throw a better error than "unexpected EOF"
371 371
 // when we can't access files in the context.
372 372
 func TestBuildWithInaccessibleFilesInContext(t *testing.T) {
373
-	buildDirectory := filepath.Join(workingDirectory, "build_tests", "TestBuildWithInaccessibleFilesInContext")
373
+	testDirName := "TestBuildWithInaccessibleFilesInContext"
374
+
375
+	sourceDirectory := filepath.Join(workingDirectory, "build_tests", testDirName)
376
+	buildDirectory, err := ioutil.TempDir("", "test-build-inaccessible-directory")
377
+	defer os.RemoveAll(buildDirectory)
378
+
379
+	err = copyWithCP(sourceDirectory, buildDirectory)
380
+	if err != nil {
381
+		t.Fatalf("failed to copy files to temporary directory: %s", err)
382
+	}
374 383
 
384
+	buildDirectory = filepath.Join(buildDirectory, testDirName)
375 385
 	{
376 386
 		// This is used to ensure we detect inaccessible files early during build in the cli client
377 387
 		pathToInaccessibleFileBuildDirectory := filepath.Join(buildDirectory, "inaccessiblefile")
... ...
@@ -238,3 +238,12 @@ func fileServer(files map[string]string) (*FileServer, error) {
238 238
 		Server: server,
239 239
 	}, nil
240 240
 }
241
+
242
+func copyWithCP(source, target string) error {
243
+	copyCmd := exec.Command("cp", "-rp", source, target)
244
+	out, exitCode, err := runCommandWithOutput(copyCmd)
245
+	if err != nil || exitCode != 0 {
246
+		return fmt.Errorf("failed to copy: error: %q ,output: %q", err, out)
247
+	}
248
+	return nil
249
+}