Browse code

integ-cli: add build test for absolute symlink

Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com>

unclejack authored on 2014/11/28 05:14:15
Showing 1 changed files
... ...
@@ -1310,6 +1310,81 @@ COPY https://index.docker.io/robots.txt /`,
1310 1310
 	logDone("build - copy - disallow copy from remote")
1311 1311
 }
1312 1312
 
1313
+func TestBuildAddBadLinks(t *testing.T) {
1314
+	const (
1315
+		dockerfile = `
1316
+			FROM scratch
1317
+			ADD links.tar /
1318
+			ADD foo.txt /symlink/
1319
+			`
1320
+		targetFile = "foo.txt"
1321
+	)
1322
+	var (
1323
+		name = "test-link-absolute"
1324
+	)
1325
+	defer deleteImages(name)
1326
+	ctx, err := fakeContext(dockerfile, nil)
1327
+	if err != nil {
1328
+		t.Fatal(err)
1329
+	}
1330
+	defer ctx.Close()
1331
+
1332
+	tempDir, err := ioutil.TempDir("", "test-link-absolute-temp-")
1333
+	if err != nil {
1334
+		t.Fatalf("failed to create temporary directory: %s", tempDir)
1335
+	}
1336
+	defer os.RemoveAll(tempDir)
1337
+
1338
+	symlinkTarget := fmt.Sprintf("/../../../../../../../../../../../..%s", tempDir)
1339
+	tarPath := filepath.Join(ctx.Dir, "links.tar")
1340
+	nonExistingFile := filepath.Join(tempDir, targetFile)
1341
+	fooPath := filepath.Join(ctx.Dir, targetFile)
1342
+
1343
+	tarOut, err := os.Create(tarPath)
1344
+	if err != nil {
1345
+		t.Fatal(err)
1346
+	}
1347
+
1348
+	tarWriter := tar.NewWriter(tarOut)
1349
+
1350
+	header := &tar.Header{
1351
+		Name:     "symlink",
1352
+		Typeflag: tar.TypeSymlink,
1353
+		Linkname: symlinkTarget,
1354
+		Mode:     0755,
1355
+		Uid:      0,
1356
+		Gid:      0,
1357
+	}
1358
+
1359
+	err = tarWriter.WriteHeader(header)
1360
+	if err != nil {
1361
+		t.Fatal(err)
1362
+	}
1363
+
1364
+	tarWriter.Close()
1365
+	tarOut.Close()
1366
+
1367
+	foo, err := os.Create(fooPath)
1368
+	if err != nil {
1369
+		t.Fatal(err)
1370
+	}
1371
+	defer foo.Close()
1372
+
1373
+	if _, err := foo.WriteString("test"); err != nil {
1374
+		t.Fatal(err)
1375
+	}
1376
+
1377
+	if _, err := buildImageFromContext(name, ctx, true); err != nil {
1378
+		t.Fatal(err)
1379
+	}
1380
+
1381
+	if _, err := os.Stat(nonExistingFile); err == nil || err != nil && !os.IsNotExist(err) {
1382
+		t.Fatalf("%s shouldn't have been written and it shouldn't exist", nonExistingFile)
1383
+	}
1384
+
1385
+	logDone("build - ADD must add files in container")
1386
+}
1387
+
1313 1388
 // Issue #5270 - ensure we throw a better error than "unexpected EOF"
1314 1389
 // when we can't access files in the context.
1315 1390
 func TestBuildWithInaccessibleFilesInContext(t *testing.T) {