Browse code

Merge pull request #10555 from duglin/MoreRenamedDockerfileTests

Add more tests around using -f Dockerfile via the CLI

Jessie Frazelle authored on 2015/02/20 06:59:35
Showing 1 changed files
... ...
@@ -4541,6 +4541,7 @@ func TestBuildRenamedDockerfile(t *testing.T) {
4541 4541
 			"files/Dockerfile": "FROM busybox\nRUN echo from files/Dockerfile",
4542 4542
 			"files/dFile":      "FROM busybox\nRUN echo from files/dFile",
4543 4543
 			"dFile":            "FROM busybox\nRUN echo from dFile",
4544
+			"files/dFile2":     "FROM busybox\nRUN echo from files/dFile2",
4544 4545
 		})
4545 4546
 	defer ctx.Close()
4546 4547
 	if err != nil {
... ...
@@ -4548,85 +4549,82 @@ func TestBuildRenamedDockerfile(t *testing.T) {
4548 4548
 	}
4549 4549
 
4550 4550
 	out, _, err := dockerCmdInDir(t, ctx.Dir, "build", "-t", "test1", ".")
4551
-
4552 4551
 	if err != nil {
4553 4552
 		t.Fatalf("Failed to build: %s\n%s", out, err)
4554 4553
 	}
4555 4554
 	if !strings.Contains(out, "from Dockerfile") {
4556
-		t.Fatalf("Should have used Dockerfile, output:%s", out)
4555
+		t.Fatalf("test1 should have used Dockerfile, output:%s", out)
4557 4556
 	}
4558 4557
 
4559 4558
 	out, _, err = dockerCmdInDir(t, ctx.Dir, "build", "-f", "files/Dockerfile", "-t", "test2", ".")
4560
-
4561 4559
 	if err != nil {
4562 4560
 		t.Fatal(err)
4563 4561
 	}
4564 4562
 	if !strings.Contains(out, "from files/Dockerfile") {
4565
-		t.Fatalf("Should have used files/Dockerfile, output:%s", out)
4563
+		t.Fatalf("test2 should have used files/Dockerfile, output:%s", out)
4566 4564
 	}
4567 4565
 
4568 4566
 	out, _, err = dockerCmdInDir(t, ctx.Dir, "build", "--file=files/dFile", "-t", "test3", ".")
4569
-
4570 4567
 	if err != nil {
4571 4568
 		t.Fatal(err)
4572 4569
 	}
4573 4570
 	if !strings.Contains(out, "from files/dFile") {
4574
-		t.Fatalf("Should have used files/dFile, output:%s", out)
4571
+		t.Fatalf("test3 should have used files/dFile, output:%s", out)
4575 4572
 	}
4576 4573
 
4577 4574
 	out, _, err = dockerCmdInDir(t, ctx.Dir, "build", "--file=dFile", "-t", "test4", ".")
4578
-
4579 4575
 	if err != nil {
4580 4576
 		t.Fatal(err)
4581 4577
 	}
4582 4578
 	if !strings.Contains(out, "from dFile") {
4583
-		t.Fatalf("Should have used dFile, output:%s", out)
4579
+		t.Fatalf("test4 should have used dFile, output:%s", out)
4584 4580
 	}
4585 4581
 
4586 4582
 	out, _, err = dockerCmdInDir(t, ctx.Dir, "build", "--file=/etc/passwd", "-t", "test5", ".")
4587
-
4588 4583
 	if err == nil {
4589
-		t.Fatalf("Was supposed to fail to find passwd")
4584
+		t.Fatalf("test5 was supposed to fail to find passwd")
4590 4585
 	}
4591
-
4592 4586
 	if !strings.Contains(out, "The Dockerfile (/etc/passwd) must be within the build context (.)") {
4593
-		t.Fatalf("Wrong error message for passwd:%v", out)
4587
+		t.Fatalf("test5 - wrong error message for passwd:%v", out)
4594 4588
 	}
4595 4589
 
4596
-	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", "../Dockerfile", "-t", "test5", "..")
4597
-
4590
+	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", "../Dockerfile", "-t", "test6", "..")
4598 4591
 	if err != nil {
4599
-		t.Fatal(err)
4592
+		t.Fatalf("test6 failed: %s", err)
4600 4593
 	}
4601
-
4602 4594
 	if !strings.Contains(out, "from Dockerfile") {
4603
-		t.Fatalf("Should have used root Dockerfile, output:%s", out)
4595
+		t.Fatalf("test6 should have used root Dockerfile, output:%s", out)
4604 4596
 	}
4605 4597
 
4606
-	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", ctx.Dir+"/files/Dockerfile", "-t", "test6", "..")
4607
-
4598
+	out, _, err = dockerCmdInDir(t, filepath.Join(ctx.Dir, "files"), "build", "-f", ctx.Dir+"/files/Dockerfile", "-t", "test7", "..")
4608 4599
 	if err != nil {
4609
-		t.Fatal(err)
4600
+		t.Fatalf("test7 failed: %s", err)
4610 4601
 	}
4611
-
4612 4602
 	if !strings.Contains(out, "from files/Dockerfile") {
4613
-		t.Fatalf("Should have used files Dockerfile - 2, output:%s", out)
4603
+		t.Fatalf("test7 should have used files Dockerfile, output:%s", out)
4614 4604
 	}
4615 4605
 
4616
-	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", "../Dockerfile", "-t", "test7", ".")
4617
-
4606
+	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", "../Dockerfile", "-t", "test8", ".")
4618 4607
 	if err == nil || !strings.Contains(out, "must be within the build context") {
4619
-		t.Fatalf("Should have failed with Dockerfile out of context")
4608
+		t.Fatalf("test8 should have failed with Dockerfile out of context: %s", err)
4620 4609
 	}
4621 4610
 
4622
-	out, _, err = dockerCmdInDir(t, "/tmp", "build", "-t", "test6", ctx.Dir)
4611
+	tmpDir := os.TempDir()
4623 4612
 
4613
+	out, _, err = dockerCmdInDir(t, tmpDir, "build", "-t", "test9", ctx.Dir)
4624 4614
 	if err != nil {
4625
-		t.Fatal(err)
4615
+		t.Fatalf("test9 - failed: %s", err)
4626 4616
 	}
4627
-
4628 4617
 	if !strings.Contains(out, "from Dockerfile") {
4629
-		t.Fatalf("Should have used root Dockerfile, output:%s", out)
4618
+		t.Fatalf("test9 should have used root Dockerfile, output:%s", out)
4619
+	}
4620
+
4621
+	out, _, err = dockerCmdInDir(t, filepath.Join(ctx.Dir, "files"), "build", "-f", "dFile2", "-t", "test10", ".")
4622
+	if err != nil {
4623
+		t.Fatalf("test10 should have worked: %s", err)
4624
+	}
4625
+	if !strings.Contains(out, "from files/dFile2") {
4626
+		t.Fatalf("test10 should have used files/dFile2, output:%s", out)
4630 4627
 	}
4631 4628
 
4632 4629
 	logDone("build - rename dockerfile")
... ...
@@ -4664,7 +4662,7 @@ func TestBuildDockerfileOutsideContext(t *testing.T) {
4664 4664
 	if err := os.MkdirAll(ctx, 0755); err != nil {
4665 4665
 		t.Fatal(err)
4666 4666
 	}
4667
-	if err := ioutil.WriteFile(filepath.Join(ctx, "Dockerfile"), []byte("FROM busybox"), 0644); err != nil {
4667
+	if err := ioutil.WriteFile(filepath.Join(ctx, "Dockerfile"), []byte("FROM scratch\nENV X Y"), 0644); err != nil {
4668 4668
 		t.Fatal(err)
4669 4669
 	}
4670 4670
 	wd, err := os.Getwd()
... ...
@@ -4675,7 +4673,7 @@ func TestBuildDockerfileOutsideContext(t *testing.T) {
4675 4675
 	if err := os.Chdir(ctx); err != nil {
4676 4676
 		t.Fatal(err)
4677 4677
 	}
4678
-	if err := ioutil.WriteFile(filepath.Join(tmpdir, "outsideDockerfile"), []byte("FROM busbox"), 0644); err != nil {
4678
+	if err := ioutil.WriteFile(filepath.Join(tmpdir, "outsideDockerfile"), []byte("FROM scratch\nENV x y"), 0644); err != nil {
4679 4679
 		t.Fatal(err)
4680 4680
 	}
4681 4681
 	if err := os.Symlink("../outsideDockerfile", filepath.Join(ctx, "dockerfile1")); err != nil {
... ...
@@ -4684,23 +4682,18 @@ func TestBuildDockerfileOutsideContext(t *testing.T) {
4684 4684
 	if err := os.Symlink(filepath.Join(tmpdir, "outsideDockerfile"), filepath.Join(ctx, "dockerfile2")); err != nil {
4685 4685
 		t.Fatal(err)
4686 4686
 	}
4687
-	if err := os.Link("../outsideDockerfile", filepath.Join(ctx, "dockerfile3")); err != nil {
4688
-		t.Fatal(err)
4689
-	}
4690
-	if err := os.Link(filepath.Join(tmpdir, "outsideDockerfile"), filepath.Join(ctx, "dockerfile4")); err != nil {
4691
-		t.Fatal(err)
4692
-	}
4693 4687
 	for _, dockerfilePath := range []string{
4694 4688
 		"../outsideDockerfile",
4695 4689
 		filepath.Join(ctx, "dockerfile1"),
4696 4690
 		filepath.Join(ctx, "dockerfile2"),
4697
-		filepath.Join(ctx, "dockerfile3"),
4698
-		filepath.Join(ctx, "dockerfile4"),
4699 4691
 	} {
4700 4692
 		out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "build", "-t", name, "--no-cache", "-f", dockerfilePath, "."))
4701 4693
 		if err == nil {
4702 4694
 			t.Fatalf("Expected error with %s. Out: %s", dockerfilePath, out)
4703 4695
 		}
4696
+		if !strings.Contains(out, "must be within the build context") && !strings.Contains(out, "Cannot locate Dockerfile") {
4697
+			t.Fatalf("Unexpected error with %s. Out: %s", dockerfilePath, out)
4698
+		}
4704 4699
 		deleteImages(name)
4705 4700
 	}
4706 4701