2015-08-23 11:52:25 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"net/http"
|
2016-04-01 13:49:04 -04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2015-10-17 08:24:56 -04:00
|
|
|
"github.com/docker/docker/pkg/integration/checker"
|
2015-08-23 11:52:25 -04:00
|
|
|
"github.com/go-check/check"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiDockerFileRemote(c *check.C) {
|
2015-09-18 13:41:12 -04:00
|
|
|
testRequires(c, NotUserNamespace)
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-23 11:52:25 -04:00
|
|
|
server, err := fakeStorage(map[string]string{
|
|
|
|
"testD": `FROM busybox
|
|
|
|
COPY * /tmp/
|
|
|
|
RUN find / -name ba*
|
|
|
|
RUN find /tmp/`,
|
|
|
|
})
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
res, body, err := sockRequestRaw("POST", "/build?dockerfile=baz&remote="+server.URL()+"/testD", nil, "application/json")
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
buf, err := readBody(body)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
// Make sure Dockerfile exists.
|
|
|
|
// Make sure 'baz' doesn't exist ANYWHERE despite being mentioned in the URL
|
|
|
|
out := string(buf)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(out, checker.Contains, "/tmp/Dockerfile")
|
|
|
|
c.Assert(out, checker.Not(checker.Contains), "baz")
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiRemoteTarballContext(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-23 11:52:25 -04:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
dockerfile := []byte("FROM busybox")
|
2015-10-17 08:24:56 -04:00
|
|
|
err := tw.WriteHeader(&tar.Header{
|
2015-08-23 11:52:25 -04:00
|
|
|
Name: "Dockerfile",
|
|
|
|
Size: int64(len(dockerfile)),
|
2015-10-17 08:24:56 -04: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 11:52:25 -04:00
|
|
|
|
|
|
|
server, err := fakeBinaryStorage(map[string]*bytes.Buffer{
|
|
|
|
"testT.tar": buffer,
|
|
|
|
})
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
res, b, err := sockRequestRaw("POST", "/build?remote="+server.URL()+"/testT.tar", nil, "application/tar")
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
b.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiRemoteTarballContextWithCustomDockerfile(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-23 11:52:25 -04:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
dockerfile := []byte(`FROM busybox
|
|
|
|
RUN echo 'wrong'`)
|
2015-10-17 08:24:56 -04:00
|
|
|
err := tw.WriteHeader(&tar.Header{
|
2015-08-23 11:52:25 -04:00
|
|
|
Name: "Dockerfile",
|
|
|
|
Size: int64(len(dockerfile)),
|
2015-10-17 08:24:56 -04: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 11:52:25 -04:00
|
|
|
|
|
|
|
custom := []byte(`FROM busybox
|
|
|
|
RUN echo 'right'
|
|
|
|
`)
|
2015-10-17 08:24:56 -04:00
|
|
|
err = tw.WriteHeader(&tar.Header{
|
2015-08-23 11:52:25 -04:00
|
|
|
Name: "custom",
|
|
|
|
Size: int64(len(custom)),
|
2015-10-17 08:24:56 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
// failed to write tar file header
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2015-10-17 08:24:56 -04: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 11:52:25 -04:00
|
|
|
|
|
|
|
server, err := fakeBinaryStorage(map[string]*bytes.Buffer{
|
|
|
|
"testT.tar": buffer,
|
|
|
|
})
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04: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 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
defer body.Close()
|
|
|
|
content, err := readBody(body)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2015-10-17 08:24:56 -04:00
|
|
|
// Build used the wrong dockerfile.
|
|
|
|
c.Assert(string(content), checker.Not(checker.Contains), "wrong")
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiLowerDockerfile(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-23 11:52:25 -04:00
|
|
|
git, err := newFakeGit("repo", map[string]string{
|
|
|
|
"dockerfile": `FROM busybox
|
|
|
|
RUN echo from dockerfile`,
|
|
|
|
}, false)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
defer git.Close()
|
|
|
|
|
|
|
|
res, body, err := sockRequestRaw("POST", "/build?remote="+git.RepoURL, nil, "application/json")
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
buf, err := readBody(body)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
out := string(buf)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(out, checker.Contains, "from dockerfile")
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiBuildGitWithF(c *check.C) {
|
2015-08-28 13:36:42 -04:00
|
|
|
testRequires(c, DaemonIsLinux)
|
2015-08-23 11:52:25 -04:00
|
|
|
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 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04: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 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
buf, err := readBody(body)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
out := string(buf)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(out, checker.Contains, "from baz")
|
2015-08-23 11:52:25 -04: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 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04: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 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
buf, err := readBody(body)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
out := string(buf)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(out, checker.Contains, "from Dockerfile")
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildApiDockerfileSymlink(c *check.C) {
|
|
|
|
// Test to make sure we stop people from trying to leave the
|
|
|
|
// build context when specifying a symlink as the path to the dockerfile
|
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
defer tw.Close()
|
|
|
|
|
2015-10-17 08:24:56 -04:00
|
|
|
err := tw.WriteHeader(&tar.Header{
|
2015-08-23 11:52:25 -04:00
|
|
|
Name: "Dockerfile",
|
|
|
|
Typeflag: tar.TypeSymlink,
|
|
|
|
Linkname: "/etc/passwd",
|
2015-10-17 08:24:56 -04:00
|
|
|
})
|
|
|
|
// failed to write tar file header
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
|
|
|
|
// failed to close tar archive
|
|
|
|
c.Assert(tw.Close(), checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
res, body, err := sockRequestRaw("POST", "/build", buffer, "application/x-tar")
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusInternalServerError)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
out, err := readBody(body)
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
// The reason the error is "Cannot locate specified Dockerfile" is because
|
|
|
|
// in the builder, the symlink is resolved within the context, therefore
|
|
|
|
// Dockerfile -> /etc/passwd becomes etc/passwd from the context which is
|
|
|
|
// a nonexistent file.
|
2015-10-17 08:24:56 -04:00
|
|
|
c.Assert(string(out), checker.Contains, "Cannot locate specified Dockerfile: Dockerfile", check.Commentf("Didn't complain about leaving build context"))
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
2016-04-01 13:49:04 -04: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)
|
|
|
|
}
|