Przeglądaj źródła

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 <github@gone.nl>
Sebastiaan van Stijn 8 lat temu
rodzic
commit
ebe66b1d0f

+ 10 - 5
integration-cli/docker_cli_registry_user_agent_test.go

@@ -2,7 +2,9 @@ package main
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"io/ioutil"
 	"net/http"
 	"net/http"
+	"os"
 	"regexp"
 	"regexp"
 
 
 	"github.com/docker/docker/integration-cli/registry"
 	"github.com/docker/docker/integration-cli/registry"
@@ -100,10 +102,14 @@ func (s *DockerRegistrySuite) TestUserAgentPassThrough(c *check.C) {
 		"--insecure-registry", pushReg.URL(),
 		"--insecure-registry", pushReg.URL(),
 		"--insecure-registry", loginReg.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"))
 	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)
 	regexpCheckUA(c, buildUA)
 
 
 	s.d.Cmd("login", "-u", "richard", "-p", "testtest", loginReg.URL())
 	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)
 	s.d.Cmd("pull", pullRepoName)
 	regexpCheckUA(c, pullUA)
 	regexpCheckUA(c, pullUA)
 
 
-	dockerfileName, cleanup2, err := makefile(`FROM scratch
+	dockerfileName, err = makefile(tmp, `FROM scratch
 	ENV foo bar`)
 	ENV foo bar`)
 	c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
 	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("build", "-t", pushRepoName, "--file", dockerfileName, ".")
 
 
 	s.d.Cmd("push", pushRepoName)
 	s.d.Cmd("push", pushRepoName)

+ 17 - 23
integration-cli/docker_cli_v2_only_test.go

@@ -10,28 +10,16 @@ import (
 	"github.com/go-check/check"
 	"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 {
 	if err != nil {
-		return "", cleanup, err
+		return "", err
 	}
 	}
 	err = ioutil.WriteFile(f.Name(), []byte(contents), os.ModePerm)
 	err = ioutil.WriteFile(f.Name(), []byte(contents), os.ModePerm)
 	if err != nil {
 	if err != nil {
-		return "", cleanup, err
-	}
-
-	cleanup = func() {
-		err := os.Remove(f.Name())
-		if err != nil {
-			fmt.Println("Error removing tmpfile")
-		}
+		return "", err
 	}
 	}
-	return f.Name(), cleanup, nil
-
+	return f.Name(), nil
 }
 }
 
 
 // TestV2Only ensures that a daemon by default does not
 // 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())
 	s.d.Start(c, "--insecure-registry", reg.URL())
 
 
-	dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.URL()))
+	tmp, err := ioutil.TempDir("", "integration-cli-")
+	c.Assert(err, check.IsNil)
+	defer os.RemoveAll(tmp)
+
+	dockerfileName, err := makefile(tmp, fmt.Sprintf("FROM %s/busybox", reg.URL()))
 	c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
 	c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
-	defer cleanup()
 
 
-	s.d.Cmd("build", "--file", dockerfileName, ".")
+	s.d.Cmd("build", "--file", dockerfileName, tmp)
 
 
 	s.d.Cmd("run", repoName)
 	s.d.Cmd("run", repoName)
 	s.d.Cmd("login", "-u", "richard", "-p", "testtest", reg.URL())
 	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")
 	s.d.Start(c, "--insecure-registry", reg.URL(), "--disable-legacy-registry=false")
 
 
-	dockerfileName, cleanup, err := makefile(fmt.Sprintf("FROM %s/busybox", reg.URL()))
+	tmp, err := ioutil.TempDir("", "integration-cli-")
+	c.Assert(err, check.IsNil)
+	defer os.RemoveAll(tmp)
+
+	dockerfileName, err := makefile(tmp, fmt.Sprintf("FROM %s/busybox", reg.URL()))
 	c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
 	c.Assert(err, check.IsNil, check.Commentf("Unable to create test dockerfile"))
-	defer cleanup()
 
 
-	s.d.Cmd("build", "--file", dockerfileName, ".")
+	s.d.Cmd("build", "--file", dockerfileName, tmp)
 	c.Assert(v1Repo, check.Equals, 1, check.Commentf("Expected v1 repository access after build"))
 	c.Assert(v1Repo, check.Equals, 1, check.Commentf("Expected v1 repository access after build"))
 
 
 	repoName := fmt.Sprintf("%s/busybox", reg.URL())
 	repoName := fmt.Sprintf("%s/busybox", reg.URL())