From ebe66b1d0f52dc58a98a428d4efa4d2f2743b96e Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 12 Jun 2017 14:47:27 +0200 Subject: [PATCH] Use tempdir instead of working directory as build-context The `makefile()` utility was used to create a temporary Dockerfile, and after tests completed, this file was deleted. However, the _build_ used the current path (`/usr/local/bin/docker`) as build-context. As a result, roughtly 20 MB was sent as build-context for each build, but none of the builds actually required a build-context. This patch; - creates a temp-dir for the test, which can be used as build-context - changes the `makefile()` utility and removes the `cleanup` functionality - instead, the `temp-dir` is removed after the test finishes (which also removes the temporary `Dockerfile`) Signed-off-by: Sebastiaan van Stijn --- .../docker_cli_registry_user_agent_test.go | 15 ++++--- integration-cli/docker_cli_v2_only_test.go | 44 ++++++++----------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/integration-cli/docker_cli_registry_user_agent_test.go b/integration-cli/docker_cli_registry_user_agent_test.go index 9f50aa676a..62ec130e51 100644 --- a/integration-cli/docker_cli_registry_user_agent_test.go +++ b/integration-cli/docker_cli_registry_user_agent_test.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "io/ioutil" "net/http" + "os" "regexp" "github.com/docker/docker/integration-cli/registry" @@ -100,10 +102,14 @@ func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) { "--insecure-registry", pushReg.URL(), "--insecure-registry", loginReg.URL()) - dockerfileName, cleanup1, err := makefile(fmt.Sprintf("FROM %s", buildRepoName)) + tmp, err := ioutil.TempDir("", "integration-cli-") + c.Assert(err, check.IsNil) + defer os.RemoveAll(tmp) + + dockerfileName, err := makefile(tmp, fmt.Sprintf("FROM %s", buildRepoName)) c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile")) - defer cleanup1() - s.d.Cmd("build", "--file", dockerfileName, ".") + + s.d.Cmd("build", "--file", dockerfileName, tmp) regexpCheckUA(c, buildUA) s.d.Cmd("login", "-u", "richard", "-p", "testtest", loginReg.URL()) @@ -112,10 +118,9 @@ func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) { s.d.Cmd("pull", pullRepoName) regexpCheckUA(c, pullUA) - dockerfileName, cleanup2, err := makefile(`FROM scratch + dockerfileName, err = makefile(tmp, `FROM scratch ENV foo bar`) c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile")) - defer cleanup2() s.d.Cmd("build", "-t", pushRepoName, "--file", dockerfileName, ".") s.d.Cmd("push", pushRepoName) diff --git a/integration-cli/docker_cli_v2_only_test.go b/integration-cli/docker_cli_v2_only_test.go index 3500e787ca..b82cdbde1f 100644 --- a/integration-cli/docker_cli_v2_only_test.go +++ b/integration-cli/docker_cli_v2_only_test.go @@ -10,28 +10,16 @@ import ( "github.com/go-check/check" ) -func makefile(contents string) (string, func(), error) { - cleanup := func() { - - } - - f, err := ioutil.TempFile(".", "tmp") +func makefile(path string, contents string) (string, error) { + f, err := ioutil.TempFile(path, "tmp") if err != nil { - return "", cleanup, err + return "", err } err = ioutil.WriteFile(f.Name(), []byte(contents), os.ModePerm) if err != nil { - return "", cleanup, err + return "", err } - - cleanup = func() { - err := os.Remove(f.Name()) - if err != nil { - fmt.Println("Error removing tmpfile") - } - } - return f.Name(), cleanup, nil - + return f.Name(), nil } // TestV2Only ensures that a daemon by default does not @@ -53,11 +41,14 @@ func (s *DockerRegistrySuite) TestV2Only(c *check.C) { s.d.Start(c, "--insecure-registry", reg.URL()) - dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.URL())) - c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile")) - defer cleanup() + tmp, err := ioutil.TempDir("", "integration-cli-") + c.Assert(err, check.IsNil) + defer os.RemoveAll(tmp) - s.d.Cmd("build", "--file", dockerfileName, ".") + dockerfileName, err := makefile(tmp, fmt.Sprintf("FROM %s/busybox", reg.URL())) + c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile")) + + s.d.Cmd("build", "--file", dockerfileName, tmp) s.d.Cmd("run", repoName) s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.URL()) @@ -102,11 +93,14 @@ func (s *DockerRegistrySuite) TestV1(c *check.C) { s.d.Start(c, "--insecure-registry", reg.URL(), "--disable-legacy-registry=false") - dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.URL())) - c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile")) - defer cleanup() + tmp, err := ioutil.TempDir("", "integration-cli-") + c.Assert(err, check.IsNil) + defer os.RemoveAll(tmp) - s.d.Cmd("build", "--file", dockerfileName, ".") + dockerfileName, err := makefile(tmp, fmt.Sprintf("FROM %s/busybox", reg.URL())) + c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile")) + + s.d.Cmd("build", "--file", dockerfileName, tmp) c.Assert(v1Repo, check.Equals, 1, check.Commentf("Expected v1 repository access after build")) repoName := fmt.Sprintf("%s/busybox", reg.URL())