浏览代码

Add more tests around using -f Dockerfile via the CLI
Mainly to make sure we don't treat the -f value as relative to the
root of the build context, but instead it must be relative to the current
dir.

Signed-off-by: Doug Davis <dug@us.ibm.com>

Doug Davis 10 年之前
父节点
当前提交
c42d2625d1
共有 1 个文件被更改,包括 32 次插入39 次删除
  1. 32 39
      integration-cli/docker_cli_build_test.go

+ 32 - 39
integration-cli/docker_cli_build_test.go

@@ -4519,6 +4519,7 @@ func TestBuildRenamedDockerfile(t *testing.T) {
 			"files/Dockerfile": "FROM busybox\nRUN echo from files/Dockerfile",
 			"files/dFile":      "FROM busybox\nRUN echo from files/dFile",
 			"dFile":            "FROM busybox\nRUN echo from dFile",
+			"files/dFile2":     "FROM busybox\nRUN echo from files/dFile2",
 		})
 	defer ctx.Close()
 	if err != nil {
@@ -4526,85 +4527,82 @@ func TestBuildRenamedDockerfile(t *testing.T) {
 	}
 
 	out, _, err := dockerCmdInDir(t, ctx.Dir, "build", "-t", "test1", ".")
-
 	if err != nil {
 		t.Fatalf("Failed to build: %s\n%s", out, err)
 	}
 	if !strings.Contains(out, "from Dockerfile") {
-		t.Fatalf("Should have used Dockerfile, output:%s", out)
+		t.Fatalf("test1 should have used Dockerfile, output:%s", out)
 	}
 
 	out, _, err = dockerCmdInDir(t, ctx.Dir, "build", "-f", "files/Dockerfile", "-t", "test2", ".")
-
 	if err != nil {
 		t.Fatal(err)
 	}
 	if !strings.Contains(out, "from files/Dockerfile") {
-		t.Fatalf("Should have used files/Dockerfile, output:%s", out)
+		t.Fatalf("test2 should have used files/Dockerfile, output:%s", out)
 	}
 
 	out, _, err = dockerCmdInDir(t, ctx.Dir, "build", "--file=files/dFile", "-t", "test3", ".")
-
 	if err != nil {
 		t.Fatal(err)
 	}
 	if !strings.Contains(out, "from files/dFile") {
-		t.Fatalf("Should have used files/dFile, output:%s", out)
+		t.Fatalf("test3 should have used files/dFile, output:%s", out)
 	}
 
 	out, _, err = dockerCmdInDir(t, ctx.Dir, "build", "--file=dFile", "-t", "test4", ".")
-
 	if err != nil {
 		t.Fatal(err)
 	}
 	if !strings.Contains(out, "from dFile") {
-		t.Fatalf("Should have used dFile, output:%s", out)
+		t.Fatalf("test4 should have used dFile, output:%s", out)
 	}
 
 	out, _, err = dockerCmdInDir(t, ctx.Dir, "build", "--file=/etc/passwd", "-t", "test5", ".")
-
 	if err == nil {
-		t.Fatalf("Was supposed to fail to find passwd")
+		t.Fatalf("test5 was supposed to fail to find passwd")
 	}
-
 	if !strings.Contains(out, "The Dockerfile (/etc/passwd) must be within the build context (.)") {
-		t.Fatalf("Wrong error message for passwd:%v", out)
+		t.Fatalf("test5 - wrong error message for passwd:%v", out)
 	}
 
-	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", "../Dockerfile", "-t", "test5", "..")
-
+	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", "../Dockerfile", "-t", "test6", "..")
 	if err != nil {
-		t.Fatal(err)
+		t.Fatalf("test6 failed: %s", err)
 	}
-
 	if !strings.Contains(out, "from Dockerfile") {
-		t.Fatalf("Should have used root Dockerfile, output:%s", out)
+		t.Fatalf("test6 should have used root Dockerfile, output:%s", out)
 	}
 
-	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", ctx.Dir+"/files/Dockerfile", "-t", "test6", "..")
-
+	out, _, err = dockerCmdInDir(t, filepath.Join(ctx.Dir, "files"), "build", "-f", ctx.Dir+"/files/Dockerfile", "-t", "test7", "..")
 	if err != nil {
-		t.Fatal(err)
+		t.Fatalf("test7 failed: %s", err)
 	}
-
 	if !strings.Contains(out, "from files/Dockerfile") {
-		t.Fatalf("Should have used files Dockerfile - 2, output:%s", out)
+		t.Fatalf("test7 should have used files Dockerfile, output:%s", out)
 	}
 
-	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", "../Dockerfile", "-t", "test7", ".")
-
+	out, _, err = dockerCmdInDir(t, ctx.Dir+"/files", "build", "-f", "../Dockerfile", "-t", "test8", ".")
 	if err == nil || !strings.Contains(out, "must be within the build context") {
-		t.Fatalf("Should have failed with Dockerfile out of context")
+		t.Fatalf("test8 should have failed with Dockerfile out of context: %s", err)
 	}
 
-	out, _, err = dockerCmdInDir(t, "/tmp", "build", "-t", "test6", ctx.Dir)
+	tmpDir := os.TempDir()
 
+	out, _, err = dockerCmdInDir(t, tmpDir, "build", "-t", "test9", ctx.Dir)
 	if err != nil {
-		t.Fatal(err)
+		t.Fatalf("test9 - failed: %s", err)
 	}
-
 	if !strings.Contains(out, "from Dockerfile") {
-		t.Fatalf("Should have used root Dockerfile, output:%s", out)
+		t.Fatalf("test9 should have used root Dockerfile, output:%s", out)
+	}
+
+	out, _, err = dockerCmdInDir(t, filepath.Join(ctx.Dir, "files"), "build", "-f", "dFile2", "-t", "test10", ".")
+	if err != nil {
+		t.Fatalf("test10 should have worked: %s", err)
+	}
+	if !strings.Contains(out, "from files/dFile2") {
+		t.Fatalf("test10 should have used files/dFile2, output:%s", out)
 	}
 
 	logDone("build - rename dockerfile")
@@ -4642,7 +4640,7 @@ func TestBuildDockerfileOutsideContext(t *testing.T) {
 	if err := os.MkdirAll(ctx, 0755); err != nil {
 		t.Fatal(err)
 	}
-	if err := ioutil.WriteFile(filepath.Join(ctx, "Dockerfile"), []byte("FROM busybox"), 0644); err != nil {
+	if err := ioutil.WriteFile(filepath.Join(ctx, "Dockerfile"), []byte("FROM scratch\nENV X Y"), 0644); err != nil {
 		t.Fatal(err)
 	}
 	wd, err := os.Getwd()
@@ -4653,7 +4651,7 @@ func TestBuildDockerfileOutsideContext(t *testing.T) {
 	if err := os.Chdir(ctx); err != nil {
 		t.Fatal(err)
 	}
-	if err := ioutil.WriteFile(filepath.Join(tmpdir, "outsideDockerfile"), []byte("FROM busbox"), 0644); err != nil {
+	if err := ioutil.WriteFile(filepath.Join(tmpdir, "outsideDockerfile"), []byte("FROM scratch\nENV x y"), 0644); err != nil {
 		t.Fatal(err)
 	}
 	if err := os.Symlink("../outsideDockerfile", filepath.Join(ctx, "dockerfile1")); err != nil {
@@ -4662,23 +4660,18 @@ func TestBuildDockerfileOutsideContext(t *testing.T) {
 	if err := os.Symlink(filepath.Join(tmpdir, "outsideDockerfile"), filepath.Join(ctx, "dockerfile2")); err != nil {
 		t.Fatal(err)
 	}
-	if err := os.Link("../outsideDockerfile", filepath.Join(ctx, "dockerfile3")); err != nil {
-		t.Fatal(err)
-	}
-	if err := os.Link(filepath.Join(tmpdir, "outsideDockerfile"), filepath.Join(ctx, "dockerfile4")); err != nil {
-		t.Fatal(err)
-	}
 	for _, dockerfilePath := range []string{
 		"../outsideDockerfile",
 		filepath.Join(ctx, "dockerfile1"),
 		filepath.Join(ctx, "dockerfile2"),
-		filepath.Join(ctx, "dockerfile3"),
-		filepath.Join(ctx, "dockerfile4"),
 	} {
 		out, _, err := runCommandWithOutput(exec.Command(dockerBinary, "build", "-t", name, "--no-cache", "-f", dockerfilePath, "."))
 		if err == nil {
 			t.Fatalf("Expected error with %s. Out: %s", dockerfilePath, out)
 		}
+		if !strings.Contains(out, "must be within the build context") && !strings.Contains(out, "Cannot locate Dockerfile") {
+			t.Fatalf("Unexpected error with %s. Out: %s", dockerfilePath, out)
+		}
 		deleteImages(name)
 	}