From c0f0f5c9887032c606750b645001829d9f14f47c Mon Sep 17 00:00:00 2001 From: Doug Davis Date: Thu, 23 Oct 2014 14:54:35 -0700 Subject: [PATCH] Do some cleanup on .dockerignore paths While working on the fix for #8330 I noticed a few things: 1 - the split() call for the .dockerignore process will generate a blank "exclude". While this isn't causing an issue right now, I got worried that in the future some code later on might interpret "" as something bad, like "everything" or ".". So I added a check for an empty "exclude" and skipped it 2 - if someone puts "foo" in their .dockerignore then we'll skip "foo". However, if they put "./foo" then we won't due to the painfully simplistic logic of go's filepath.Match algorithm. To help things a little (and to treat ./Dockerfile just like Dockerfile) I added code to filepath.Clean() each entry in .dockerignore. It should result in the same semantic path but ensure that no matter how the user expresses the path, we'll match it. Signed-off-by: Doug Davis --- api/client/commands.go | 5 ++++ integration-cli/docker_cli_build_test.go | 32 +++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/api/client/commands.go b/api/client/commands.go index f4ced5ecff..de0076c4ac 100644 --- a/api/client/commands.go +++ b/api/client/commands.go @@ -143,6 +143,11 @@ func (cli *DockerCli) CmdBuild(args ...string) error { return fmt.Errorf("Error reading .dockerignore: '%s'", err) } for _, pattern := range strings.Split(string(ignore), "\n") { + pattern = strings.TrimSpace(pattern) + if pattern == "" { + continue + } + pattern = filepath.Clean(pattern) ok, err := filepath.Match(pattern, "Dockerfile") if err != nil { return fmt.Errorf("Bad .dockerignore pattern: '%s', error: %s", pattern, err) diff --git a/integration-cli/docker_cli_build_test.go b/integration-cli/docker_cli_build_test.go index c909b14f0d..8812cab1be 100644 --- a/integration-cli/docker_cli_build_test.go +++ b/integration-cli/docker_cli_build_test.go @@ -2325,6 +2325,29 @@ func TestBuildDockerignore(t *testing.T) { logDone("build - test .dockerignore") } +func TestBuildDockerignoreCleanPaths(t *testing.T) { + name := "testbuilddockerignorecleanpaths" + defer deleteImages(name) + dockerfile := ` + FROM busybox + ADD . /tmp/ + RUN (! ls /tmp/foo) && (! ls /tmp/foo2) && (! ls /tmp/dir1/foo)` + ctx, err := fakeContext(dockerfile, map[string]string{ + "foo": "foo", + "foo2": "foo2", + "dir1/foo": "foo in dir1", + ".dockerignore": "./foo\ndir1//foo\n./dir1/../foo2", + }) + if err != nil { + t.Fatal(err) + } + defer ctx.Close() + if _, err := buildImageFromContext(name, ctx, true); err != nil { + t.Fatal(err) + } + logDone("build - test .dockerignore with clean paths") +} + func TestBuildDockerignoringDockerfile(t *testing.T) { name := "testbuilddockerignoredockerfile" defer deleteImages(name) @@ -2334,13 +2357,20 @@ func TestBuildDockerignoringDockerfile(t *testing.T) { "Dockerfile": "FROM scratch", ".dockerignore": "Dockerfile\n", }) - defer ctx.Close() if err != nil { t.Fatal(err) } + defer ctx.Close() if _, err = buildImageFromContext(name, ctx, true); err == nil { t.Fatalf("Didn't get expected error from ignoring Dockerfile") } + + // now try it with ./Dockerfile + ctx.Add(".dockerignore", "./Dockerfile\n") + if _, err = buildImageFromContext(name, ctx, true); err == nil { + t.Fatalf("Didn't get expected error from ignoring ./Dockerfile") + } + logDone("build - test .dockerignore of Dockerfile") }