2015-08-23 18:52:25 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
2016-04-01 20:49:04 +03:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-08-23 18:52:25 +03:00
|
|
|
|
2015-10-17 15:24:56 +03:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-08-23 18:52:25 +03:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiDockerFileRemote(c *check.C) {
|
2015-09-18 20:41:12 +03:00
|
|
|
testRequires(c, NotUserNamespace)
|
2016-09-19 22:53:14 +03:00
|
|
|
var testD string
|
|
|
|
if daemonPlatform == "windows" {
|
|
|
|
testD = `FROM busybox
|
2015-08-23 18:52:25 +03:00
|
|
|
COPY * /tmp/
|
|
|
|
RUN find / -name ba*
|
2016-09-19 22:53:14 +03:00
|
|
|
RUN find /tmp/`
|
|
|
|
} else {
|
|
|
|
// -xdev is required because sysfs can cause EPERM
|
|
|
|
testD = `FROM busybox
|
|
|
|
COPY * /tmp/
|
|
|
|
RUN find / -xdev -name ba*
|
|
|
|
RUN find /tmp/`
|
|
|
|
}
|
|
|
|
server, err := fakeStorage(map[string]string{"testD": testD})
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
buf, err := readBody(body)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
// Make sure Dockerfile exists.
|
|
|
|
// Make sure 'baz' doesn't exist ANYWHERE despite being mentioned in the URL
|
|
|
|
out := string(buf)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(out, checker.Contains, "/tmp/Dockerfile")
|
|
|
|
c.Assert(out, checker.Not(checker.Contains), "baz")
|
2015-08-23 18:52:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiRemoteTarballContext(c *check.C) {
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
dockerfile := []byte("FROM busybox")
|
2015-10-17 15:24:56 +03:00
|
|
|
err := tw.WriteHeader(&tar.Header{
|
2015-08-23 18:52:25 +03:00
|
|
|
Name: "Dockerfile",
|
|
|
|
Size: int64(len(dockerfile)),
|
2015-10-17 15:24:56 +03:00
|
|
|
})
|
|
|
|
// failed to write tar file header
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
_, err = tw.Write(dockerfile)
|
|
|
|
// failed to write tar file content
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// failed to close tar archive
|
|
|
|
c.Assert(tw.Close(), checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
server, err := fakeBinaryStorage(map[string]*bytes.Buffer{
|
|
|
|
"testT.tar": buffer,
|
|
|
|
})
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
res, b, err := sockRequestRaw("POST", "/build?remote="+server.URL()+"/testT.tar", nil, "application/tar")
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 18:52:25 +03:00
|
|
|
b.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiRemoteTarballContextWithCustomDockerfile(c *check.C) {
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
dockerfile := []byte(`FROM busybox
|
|
|
|
RUN echo 'wrong'`)
|
2015-10-17 15:24:56 +03:00
|
|
|
err := tw.WriteHeader(&tar.Header{
|
2015-08-23 18:52:25 +03:00
|
|
|
Name: "Dockerfile",
|
|
|
|
Size: int64(len(dockerfile)),
|
2015-10-17 15:24:56 +03:00
|
|
|
})
|
|
|
|
// failed to write tar file header
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
_, err = tw.Write(dockerfile)
|
|
|
|
// failed to write tar file content
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
custom := []byte(`FROM busybox
|
|
|
|
RUN echo 'right'
|
|
|
|
`)
|
2015-10-17 15:24:56 +03:00
|
|
|
err = tw.WriteHeader(&tar.Header{
|
2015-08-23 18:52:25 +03:00
|
|
|
Name: "custom",
|
|
|
|
Size: int64(len(custom)),
|
2015-10-17 15:24:56 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
// failed to write tar file header
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
2015-10-17 15:24:56 +03:00
|
|
|
_, err = tw.Write(custom)
|
|
|
|
// failed to write tar file content
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// failed to close tar archive
|
|
|
|
c.Assert(tw.Close(), checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
server, err := fakeBinaryStorage(map[string]*bytes.Buffer{
|
|
|
|
"testT.tar": buffer,
|
|
|
|
})
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
defer server.Close()
|
|
|
|
url := "/build?dockerfile=custom&remote=" + server.URL() + "/testT.tar"
|
|
|
|
res, body, err := sockRequestRaw("POST", url, nil, "application/tar")
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
defer body.Close()
|
|
|
|
content, err := readBody(body)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
2015-10-17 15:24:56 +03:00
|
|
|
// Build used the wrong dockerfile.
|
|
|
|
c.Assert(string(content), checker.Not(checker.Contains), "wrong")
|
2015-08-23 18:52:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiLowerDockerfile(c *check.C) {
|
|
|
|
git, err := newFakeGit("repo", map[string]string{
|
|
|
|
"dockerfile": `FROM busybox
|
|
|
|
RUN echo from dockerfile`,
|
|
|
|
}, false)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
defer git.Close()
|
|
|
|
|
|
|
|
res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
buf, err := readBody(body)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
out := string(buf)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(out, checker.Contains, "from dockerfile")
|
2015-08-23 18:52:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiBuildGitWithF(c *check.C) {
|
|
|
|
git, err := newFakeGit("repo", map[string]string{
|
|
|
|
"baz": `FROM busybox
|
|
|
|
RUN echo from baz`,
|
|
|
|
"Dockerfile": `FROM busybox
|
|
|
|
RUN echo from Dockerfile`,
|
|
|
|
}, false)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
defer git.Close()
|
|
|
|
|
|
|
|
// Make sure it tries to 'dockerfile' query param value
|
|
|
|
res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+git.RepoURL, nil, "application/json")
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
buf, err := readBody(body)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
out := string(buf)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(out, checker.Contains, "from baz")
|
2015-08-23 18:52:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiDoubleDockerfile(c *check.C) {
|
|
|
|
testRequires(c, UnixCli) // dockerfile overwrites Dockerfile on Windows
|
|
|
|
git, err := newFakeGit("repo", map[string]string{
|
|
|
|
"Dockerfile": `FROM busybox
|
|
|
|
RUN echo from Dockerfile`,
|
|
|
|
"dockerfile": `FROM busybox
|
|
|
|
RUN echo from dockerfile`,
|
|
|
|
}, false)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
defer git.Close()
|
|
|
|
|
|
|
|
// Make sure it tries to 'dockerfile' query param value
|
|
|
|
res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
buf, err := readBody(body)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 18:52:25 +03:00
|
|
|
|
|
|
|
out := string(buf)
|
2015-10-17 15:24:56 +03:00
|
|
|
c.Assert(out, checker.Contains, "from Dockerfile")
|
2015-08-23 18:52:25 +03:00
|
|
|
}
|
|
|
|
|
2016-04-01 20:49:04 +03:00
|
|
|
func (s *DockerSuite) TestBuildApiUnnormalizedTarPaths(c *check.C) {
|
|
|
|
// Make sure that build context tars with entries of the form
|
|
|
|
// x/./y don't cause caching false positives.
|
|
|
|
|
|
|
|
buildFromTarContext := func(fileContents []byte) string {
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
dockerfile := []byte(`FROM busybox
|
|
|
|
COPY dir /dir/`)
|
|
|
|
err := tw.WriteHeader(&tar.Header{
|
|
|
|
Name: "Dockerfile",
|
|
|
|
Size: int64(len(dockerfile)),
|
|
|
|
})
|
|
|
|
//failed to write tar file header
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
_, err = tw.Write(dockerfile)
|
|
|
|
// failed to write Dockerfile in tar file content
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
err = tw.WriteHeader(&tar.Header{
|
|
|
|
Name: "dir/./file",
|
|
|
|
Size: int64(len(fileContents)),
|
|
|
|
})
|
|
|
|
//failed to write tar file header
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
_, err = tw.Write(fileContents)
|
|
|
|
// failed to write file contents in tar file content
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// failed to close tar archive
|
|
|
|
c.Assert(tw.Close(), checker.IsNil)
|
|
|
|
|
|
|
|
res, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
|
|
|
|
|
|
|
out, err := readBody(body)
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
lines := strings.Split(string(out), "\n")
|
|
|
|
c.Assert(len(lines), checker.GreaterThan, 1)
|
|
|
|
c.Assert(lines[len(lines)-2], checker.Matches, ".*Successfully built [0-9a-f]{12}.*")
|
|
|
|
|
|
|
|
re := regexp.MustCompile("Successfully built ([0-9a-f]{12})")
|
|
|
|
matches := re.FindStringSubmatch(lines[len(lines)-2])
|
|
|
|
return matches[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
imageA := buildFromTarContext([]byte("abc"))
|
|
|
|
imageB := buildFromTarContext([]byte("def"))
|
|
|
|
|
|
|
|
c.Assert(imageA, checker.Not(checker.Equals), imageB)
|
|
|
|
}
|