mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #10555 from duglin/MoreRenamedDockerfileTests
Add more tests around using -f Dockerfile via the CLI
This commit is contained in:
commit
7829e7b40f
1 changed files with 32 additions and 39 deletions
|
@ -4541,6 +4541,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 {
|
||||
|
@ -4548,85 +4549,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("test9 should have used root Dockerfile, output:%s", out)
|
||||
}
|
||||
|
||||
if !strings.Contains(out, "from Dockerfile") {
|
||||
t.Fatalf("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")
|
||||
|
@ -4664,7 +4662,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()
|
||||
|
@ -4675,7 +4673,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 {
|
||||
|
@ -4684,23 +4682,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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue