2015-08-23 11:52:25 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2017-06-05 11:12:26 -04:00
|
|
|
"encoding/json"
|
2017-06-19 20:15:23 -04:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2015-08-23 11:52:25 -04:00
|
|
|
"net/http"
|
2016-04-01 13:49:04 -04:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-09-09 17:06:12 -04:00
|
|
|
"testing"
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2017-06-19 20:15:23 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/fakecontext"
|
|
|
|
"github.com/docker/docker/testutil/fakegit"
|
|
|
|
"github.com/docker/docker/testutil/fakestorage"
|
|
|
|
"github.com/docker/docker/testutil/request"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-08-23 11:52:25 -04:00
|
|
|
)
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildAPIDockerFileRemote(c *testing.T) {
|
2017-09-12 08:53:20 -04:00
|
|
|
testRequires(c, NotUserNamespace)
|
2017-09-11 10:34:08 -04:00
|
|
|
|
2016-09-19 15:53:14 -04:00
|
|
|
var testD string
|
2018-01-15 09:32:06 -05:00
|
|
|
if testEnv.OSType == "windows" {
|
2016-09-19 15:53:14 -04:00
|
|
|
testD = `FROM busybox
|
2015-08-23 11:52:25 -04:00
|
|
|
RUN find / -name ba*
|
2016-09-19 15:53:14 -04:00
|
|
|
RUN find /tmp/`
|
|
|
|
} else {
|
|
|
|
// -xdev is required because sysfs can cause EPERM
|
|
|
|
testD = `FROM busybox
|
|
|
|
RUN find / -xdev -name ba*
|
|
|
|
RUN find /tmp/`
|
|
|
|
}
|
2017-04-10 08:42:21 -04:00
|
|
|
server := fakestorage.New(c, "", fakecontext.WithFiles(map[string]string{"testD": testD}))
|
2015-08-23 11:52:25 -04:00
|
|
|
defer server.Close()
|
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
res, body, err := request.Post("/build?dockerfile=baz&remote="+server.URL()+"/testD", request.JSON)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2017-08-21 18:50:40 -04:00
|
|
|
buf, err := request.ReadBody(body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
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)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.Contains(out, "RUN find /tmp"))
|
|
|
|
assert.Assert(c, !strings.Contains(out, "baz"))
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildAPIRemoteTarballContext(c *testing.T) {
|
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
|
|
|
})
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to write tar file header")
|
2015-10-17 08:24:56 -04:00
|
|
|
|
|
|
|
_, err = tw.Write(dockerfile)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err, "failed to write tar file content")
|
|
|
|
assert.NilError(c, tw.Close(), "failed to close tar archive")
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2017-04-10 08:42:21 -04:00
|
|
|
server := fakestorage.New(c, "", fakecontext.WithBinaryFiles(map[string]*bytes.Buffer{
|
2015-08-23 11:52:25 -04:00
|
|
|
"testT.tar": buffer,
|
2017-04-10 08:42:21 -04:00
|
|
|
}))
|
2015-08-23 11:52:25 -04:00
|
|
|
defer server.Close()
|
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
res, b, err := request.Post("/build?remote="+server.URL()+"/testT.tar", request.ContentType("application/tar"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
b.Close()
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildAPIRemoteTarballContextWithCustomDockerfile(c *testing.T) {
|
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
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-17 08:24:56 -04:00
|
|
|
|
|
|
|
_, err = tw.Write(dockerfile)
|
|
|
|
// failed to write tar file content
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
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
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
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
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-10-17 08:24:56 -04:00
|
|
|
|
|
|
|
// failed to close tar archive
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, tw.Close())
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2017-04-10 08:42:21 -04:00
|
|
|
server := fakestorage.New(c, "", fakecontext.WithBinaryFiles(map[string]*bytes.Buffer{
|
2015-08-23 11:52:25 -04:00
|
|
|
"testT.tar": buffer,
|
2017-04-10 08:42:21 -04:00
|
|
|
}))
|
2015-08-23 11:52:25 -04:00
|
|
|
defer server.Close()
|
2017-01-16 05:30:14 -05:00
|
|
|
|
2015-08-23 11:52:25 -04:00
|
|
|
url := "/build?dockerfile=custom&remote=" + server.URL() + "/testT.tar"
|
2017-03-06 10:35:27 -05:00
|
|
|
res, body, err := request.Post(url, request.ContentType("application/tar"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
defer body.Close()
|
2017-08-21 18:50:40 -04:00
|
|
|
content, err := request.ReadBody(body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2015-10-17 08:24:56 -04:00
|
|
|
// Build used the wrong dockerfile.
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, !strings.Contains(string(content), "wrong"))
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildAPILowerDockerfile(c *testing.T) {
|
2017-04-19 09:04:24 -04:00
|
|
|
git := fakegit.New(c, "repo", map[string]string{
|
2015-08-23 11:52:25 -04:00
|
|
|
"dockerfile": `FROM busybox
|
|
|
|
RUN echo from dockerfile`,
|
|
|
|
}, false)
|
|
|
|
defer git.Close()
|
|
|
|
|
2017-03-06 10:35:27 -05:00
|
|
|
res, body, err := request.Post("/build?remote="+git.RepoURL, request.JSON)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2017-08-21 18:50:40 -04:00
|
|
|
buf, err := request.ReadBody(body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
out := string(buf)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.Contains(out, "from dockerfile"))
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildAPIBuildGitWithF(c *testing.T) {
|
2017-04-19 09:04:24 -04:00
|
|
|
git := fakegit.New(c, "repo", map[string]string{
|
2015-08-23 11:52:25 -04:00
|
|
|
"baz": `FROM busybox
|
|
|
|
RUN echo from baz`,
|
|
|
|
"Dockerfile": `FROM busybox
|
|
|
|
RUN echo from Dockerfile`,
|
|
|
|
}, false)
|
|
|
|
defer git.Close()
|
|
|
|
|
|
|
|
// Make sure it tries to 'dockerfile' query param value
|
2017-03-06 10:35:27 -05:00
|
|
|
res, body, err := request.Post("/build?dockerfile=baz&remote="+git.RepoURL, request.JSON)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2017-08-21 18:50:40 -04:00
|
|
|
buf, err := request.ReadBody(body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
out := string(buf)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.Contains(out, "from baz"))
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildAPIDoubleDockerfile(c *testing.T) {
|
2017-09-12 08:53:20 -04:00
|
|
|
testRequires(c, UnixCli) // dockerfile overwrites Dockerfile on Windows
|
2017-04-19 09:04:24 -04:00
|
|
|
git := fakegit.New(c, "repo", map[string]string{
|
2015-08-23 11:52:25 -04:00
|
|
|
"Dockerfile": `FROM busybox
|
|
|
|
RUN echo from Dockerfile`,
|
|
|
|
"dockerfile": `FROM busybox
|
|
|
|
RUN echo from dockerfile`,
|
|
|
|
}, false)
|
|
|
|
defer git.Close()
|
|
|
|
|
|
|
|
// Make sure it tries to 'dockerfile' query param value
|
2017-03-06 10:35:27 -05:00
|
|
|
res, body, err := request.Post("/build?remote="+git.RepoURL, request.JSON)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
2017-08-21 18:50:40 -04:00
|
|
|
buf, err := request.ReadBody(body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2015-08-23 11:52:25 -04:00
|
|
|
|
|
|
|
out := string(buf)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, is.Contains(out, "from Dockerfile"))
|
2015-08-23 11:52:25 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildAPIUnnormalizedTarPaths(c *testing.T) {
|
2016-04-01 13:49:04 -04:00
|
|
|
// 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)),
|
|
|
|
})
|
2019-11-27 09:36:45 -05:00
|
|
|
assert.NilError(c, err, "failed to write tar file header")
|
2016-04-01 13:49:04 -04:00
|
|
|
|
|
|
|
_, err = tw.Write(dockerfile)
|
2019-11-27 09:36:45 -05:00
|
|
|
assert.NilError(c, err, "failed to write Dockerfile in tar file content")
|
2016-04-01 13:49:04 -04:00
|
|
|
|
|
|
|
err = tw.WriteHeader(&tar.Header{
|
|
|
|
Name: "dir/./file",
|
|
|
|
Size: int64(len(fileContents)),
|
|
|
|
})
|
2019-11-27 09:36:45 -05:00
|
|
|
assert.NilError(c, err, "failed to write tar file header")
|
2016-04-01 13:49:04 -04:00
|
|
|
|
|
|
|
_, err = tw.Write(fileContents)
|
2019-11-27 09:36:45 -05:00
|
|
|
assert.NilError(c, err, "failed to write file contents in tar file content")
|
2016-04-01 13:49:04 -04:00
|
|
|
|
2019-11-27 09:36:45 -05:00
|
|
|
assert.NilError(c, tw.Close(), "failed to close tar archive")
|
2016-04-01 13:49:04 -04:00
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
res, body, err := request.Post("/build", request.RawContent(io.NopCloser(buffer)), request.ContentType("application/x-tar"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2016-04-01 13:49:04 -04:00
|
|
|
|
2017-08-21 18:50:40 -04:00
|
|
|
out, err := request.ReadBody(body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
2016-04-01 13:49:04 -04:00
|
|
|
lines := strings.Split(string(out), "\n")
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, len(lines) > 1)
|
|
|
|
matched, err := regexp.MatchString(".*Successfully built [0-9a-f]{12}.*", lines[len(lines)-2])
|
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, matched)
|
2016-04-01 13:49:04 -04:00
|
|
|
|
|
|
|
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"))
|
|
|
|
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.Assert(c, imageA != imageB)
|
2016-04-01 13:49:04 -04:00
|
|
|
}
|
2017-06-02 16:06:14 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildOnBuildWithCopy(c *testing.T) {
|
2017-06-02 16:06:14 -04:00
|
|
|
dockerfile := `
|
|
|
|
FROM ` + minimalBaseImage() + ` as onbuildbase
|
|
|
|
ONBUILD COPY file /file
|
|
|
|
|
|
|
|
FROM onbuildbase
|
|
|
|
`
|
|
|
|
ctx := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
fakecontext.WithFile("file", "some content"),
|
|
|
|
)
|
|
|
|
defer ctx.Close()
|
|
|
|
|
|
|
|
res, body, err := request.Post(
|
|
|
|
"/build",
|
|
|
|
request.RawContent(ctx.AsTarReader(c)),
|
|
|
|
request.ContentType("application/x-tar"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2017-06-02 16:06:14 -04:00
|
|
|
|
2017-08-21 18:50:40 -04:00
|
|
|
out, err := request.ReadBody(body)
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Assert(c, is.Contains(string(out), "Successfully built"))
|
2017-06-02 16:06:14 -04:00
|
|
|
}
|
2017-06-05 11:12:26 -04:00
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildOnBuildCache(c *testing.T) {
|
2017-06-05 11:12:26 -04:00
|
|
|
build := func(dockerfile string) []byte {
|
|
|
|
ctx := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
)
|
|
|
|
defer ctx.Close()
|
|
|
|
|
|
|
|
res, body, err := request.Post(
|
|
|
|
"/build",
|
|
|
|
request.RawContent(ctx.AsTarReader(c)),
|
|
|
|
request.ContentType("application/x-tar"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
|
2017-06-05 11:12:26 -04:00
|
|
|
|
2017-08-21 18:50:40 -04:00
|
|
|
out, err := request.ReadBody(body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
2018-12-20 20:39:29 -05:00
|
|
|
assert.Assert(c, is.Contains(string(out), "Successfully built"))
|
2017-06-05 11:12:26 -04:00
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
dockerfile := `
|
|
|
|
FROM ` + minimalBaseImage() + ` as onbuildbase
|
|
|
|
ENV something=bar
|
|
|
|
ONBUILD ENV foo=bar
|
|
|
|
`
|
|
|
|
build(dockerfile)
|
|
|
|
|
|
|
|
dockerfile += "FROM onbuildbase"
|
|
|
|
out := build(dockerfile)
|
|
|
|
|
|
|
|
imageIDs := getImageIDsFromBuild(c, out)
|
2018-12-20 20:39:29 -05:00
|
|
|
assert.Assert(c, is.Len(imageIDs, 2))
|
2017-06-05 11:12:26 -04:00
|
|
|
parentID, childID := imageIDs[0], imageIDs[1]
|
|
|
|
|
2017-09-19 16:12:29 -04:00
|
|
|
client := testEnv.APIClient()
|
2017-06-05 11:12:26 -04:00
|
|
|
|
|
|
|
// check parentID is correct
|
|
|
|
image, _, err := client.ImageInspectWithRaw(context.Background(), childID)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.Equal(parentID, image.Parent))
|
2017-06-05 11:12:26 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerRegistrySuite) TestBuildCopyFromForcePull(c *testing.T) {
|
2017-09-19 16:12:29 -04:00
|
|
|
client := testEnv.APIClient()
|
2017-06-19 20:15:23 -04:00
|
|
|
|
|
|
|
repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
|
|
|
|
// tag the image to upload it to the private registry
|
2017-09-19 16:12:29 -04:00
|
|
|
err := client.ImageTag(context.TODO(), "busybox", repoName)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(c, err)
|
2017-06-19 20:15:23 -04:00
|
|
|
// push the image to the registry
|
|
|
|
rc, err := client.ImagePush(context.TODO(), repoName, types.ImagePushOptions{RegistryAuth: "{}"})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(c, err)
|
2021-08-24 06:10:50 -04:00
|
|
|
_, err = io.Copy(io.Discard, rc)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(c, err)
|
2017-06-19 20:15:23 -04:00
|
|
|
|
|
|
|
dockerfile := fmt.Sprintf(`
|
|
|
|
FROM %s AS foo
|
|
|
|
RUN touch abc
|
|
|
|
FROM %s
|
|
|
|
COPY --from=foo /abc /
|
|
|
|
`, repoName, repoName)
|
|
|
|
|
|
|
|
ctx := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
)
|
|
|
|
defer ctx.Close()
|
|
|
|
|
|
|
|
res, body, err := request.Post(
|
|
|
|
"/build?pull=1",
|
|
|
|
request.RawContent(ctx.AsTarReader(c)),
|
|
|
|
request.ContentType("application/x-tar"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
|
2017-06-19 20:15:23 -04:00
|
|
|
|
2017-08-21 18:50:40 -04:00
|
|
|
out, err := request.ReadBody(body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.Contains(string(out), "Successfully built"))
|
2017-06-27 13:38:01 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildAddRemoteNoDecompress(c *testing.T) {
|
2017-06-27 13:38:01 -04:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
dt := []byte("contents")
|
|
|
|
err := tw.WriteHeader(&tar.Header{
|
|
|
|
Name: "foo",
|
|
|
|
Size: int64(len(dt)),
|
|
|
|
Mode: 0600,
|
|
|
|
Typeflag: tar.TypeReg,
|
|
|
|
})
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
2017-06-27 13:38:01 -04:00
|
|
|
_, err = tw.Write(dt)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
2017-06-27 13:38:01 -04:00
|
|
|
err = tw.Close()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
2017-06-27 13:38:01 -04:00
|
|
|
|
|
|
|
server := fakestorage.New(c, "", fakecontext.WithBinaryFiles(map[string]*bytes.Buffer{
|
|
|
|
"test.tar": buffer,
|
|
|
|
}))
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
dockerfile := fmt.Sprintf(`
|
|
|
|
FROM busybox
|
|
|
|
ADD %s/test.tar /
|
|
|
|
RUN [ -f test.tar ]
|
|
|
|
`, server.URL())
|
|
|
|
|
|
|
|
ctx := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
)
|
|
|
|
defer ctx.Close()
|
|
|
|
|
|
|
|
res, body, err := request.Post(
|
|
|
|
"/build",
|
|
|
|
request.RawContent(ctx.AsTarReader(c)),
|
|
|
|
request.ContentType("application/x-tar"))
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
|
2017-06-27 13:38:01 -04:00
|
|
|
|
2017-08-21 18:50:40 -04:00
|
|
|
out, err := request.ReadBody(body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.Contains(string(out), "Successfully built"))
|
2017-07-26 12:05:55 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildChownOnCopy(c *testing.T) {
|
2018-05-04 17:15:00 -04:00
|
|
|
// new feature added in 1.31 - https://github.com/moby/moby/pull/34263
|
|
|
|
testRequires(c, DaemonIsLinux, MinimumAPIVersion("1.31"))
|
2017-07-26 12:05:55 -04:00
|
|
|
dockerfile := `FROM busybox
|
|
|
|
RUN echo 'test1:x:1001:1001::/bin:/bin/false' >> /etc/passwd
|
|
|
|
RUN echo 'test1:x:1001:' >> /etc/group
|
|
|
|
RUN echo 'test2:x:1002:' >> /etc/group
|
|
|
|
COPY --chown=test1:1002 . /new_dir
|
|
|
|
RUN ls -l /
|
|
|
|
RUN [ $(ls -l / | grep new_dir | awk '{print $3":"$4}') = 'test1:test2' ]
|
|
|
|
RUN [ $(ls -nl / | grep new_dir | awk '{print $3":"$4}') = '1001:1002' ]
|
|
|
|
`
|
|
|
|
ctx := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
fakecontext.WithFile("test_file1", "some test content"),
|
|
|
|
)
|
|
|
|
defer ctx.Close()
|
|
|
|
|
|
|
|
res, body, err := request.Post(
|
|
|
|
"/build",
|
|
|
|
request.RawContent(ctx.AsTarReader(c)),
|
|
|
|
request.ContentType("application/x-tar"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2017-07-26 12:05:55 -04:00
|
|
|
|
2017-08-28 13:19:54 -04:00
|
|
|
out, err := request.ReadBody(body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.Contains(string(out), "Successfully built"))
|
2017-06-19 20:15:23 -04:00
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildCopyCacheOnFileChange(c *testing.T) {
|
2017-05-22 11:21:17 -04:00
|
|
|
|
|
|
|
dockerfile := `FROM busybox
|
|
|
|
COPY file /file`
|
|
|
|
|
|
|
|
ctx1 := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
fakecontext.WithFile("file", "foo"))
|
|
|
|
ctx2 := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
fakecontext.WithFile("file", "bar"))
|
|
|
|
|
|
|
|
var build = func(ctx *fakecontext.Fake) string {
|
|
|
|
res, body, err := request.Post("/build",
|
|
|
|
request.RawContent(ctx.AsTarReader(c)),
|
|
|
|
request.ContentType("application/x-tar"))
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
|
2017-05-22 11:21:17 -04:00
|
|
|
|
|
|
|
out, err := request.ReadBody(body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
2018-12-20 20:39:29 -05:00
|
|
|
assert.Assert(c, is.Contains(string(out), "Successfully built"))
|
2017-05-22 11:21:17 -04:00
|
|
|
|
|
|
|
ids := getImageIDsFromBuild(c, out)
|
2018-12-20 20:39:29 -05:00
|
|
|
assert.Assert(c, is.Len(ids, 1))
|
2017-05-22 11:21:17 -04:00
|
|
|
return ids[len(ids)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
id1 := build(ctx1)
|
|
|
|
id2 := build(ctx1)
|
|
|
|
id3 := build(ctx2)
|
|
|
|
|
|
|
|
if id1 != id2 {
|
|
|
|
c.Fatal("didn't use the cache")
|
|
|
|
}
|
|
|
|
if id1 == id3 {
|
|
|
|
c.Fatal("COPY With different source file should not share same cache")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildAddCacheOnFileChange(c *testing.T) {
|
2017-05-22 11:21:17 -04:00
|
|
|
|
|
|
|
dockerfile := `FROM busybox
|
|
|
|
ADD file /file`
|
|
|
|
|
|
|
|
ctx1 := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
fakecontext.WithFile("file", "foo"))
|
|
|
|
ctx2 := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
fakecontext.WithFile("file", "bar"))
|
|
|
|
|
|
|
|
var build = func(ctx *fakecontext.Fake) string {
|
|
|
|
res, body, err := request.Post("/build",
|
|
|
|
request.RawContent(ctx.AsTarReader(c)),
|
|
|
|
request.ContentType("application/x-tar"))
|
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.DeepEqual(http.StatusOK, res.StatusCode))
|
2017-05-22 11:21:17 -04:00
|
|
|
|
|
|
|
out, err := request.ReadBody(body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
2018-12-20 20:39:29 -05:00
|
|
|
assert.Assert(c, is.Contains(string(out), "Successfully built"))
|
2017-05-22 11:21:17 -04:00
|
|
|
|
|
|
|
ids := getImageIDsFromBuild(c, out)
|
2018-12-20 20:39:29 -05:00
|
|
|
assert.Assert(c, is.Len(ids, 1))
|
2017-05-22 11:21:17 -04:00
|
|
|
return ids[len(ids)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
id1 := build(ctx1)
|
|
|
|
id2 := build(ctx1)
|
|
|
|
id3 := build(ctx2)
|
|
|
|
|
|
|
|
if id1 != id2 {
|
|
|
|
c.Fatal("didn't use the cache")
|
|
|
|
}
|
|
|
|
if id1 == id3 {
|
|
|
|
c.Fatal("COPY With different source file should not share same cache")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *DockerSuite) TestBuildScratchCopy(c *testing.T) {
|
2017-11-06 21:21:10 -05:00
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
dockerfile := `FROM scratch
|
|
|
|
ADD Dockerfile /
|
|
|
|
ENV foo bar`
|
|
|
|
ctx := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithDockerfile(dockerfile),
|
|
|
|
)
|
|
|
|
defer ctx.Close()
|
|
|
|
|
|
|
|
res, body, err := request.Post(
|
|
|
|
"/build",
|
|
|
|
request.RawContent(ctx.AsTarReader(c)),
|
|
|
|
request.ContentType("application/x-tar"))
|
2019-04-04 09:23:19 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2017-11-06 21:21:10 -05:00
|
|
|
|
|
|
|
out, err := request.ReadBody(body)
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, err)
|
|
|
|
assert.Check(c, is.Contains(string(out), "Successfully built"))
|
2017-11-06 21:21:10 -05:00
|
|
|
}
|
|
|
|
|
2017-06-05 11:12:26 -04:00
|
|
|
type buildLine struct {
|
|
|
|
Stream string
|
|
|
|
Aux struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func getImageIDsFromBuild(c *testing.T, output []byte) []string {
|
2018-05-19 07:38:54 -04:00
|
|
|
var ids []string
|
2017-06-05 11:12:26 -04:00
|
|
|
for _, line := range bytes.Split(output, []byte("\n")) {
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
entry := buildLine{}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.NilError(c, json.Unmarshal(line, &entry))
|
2017-06-05 11:12:26 -04:00
|
|
|
if entry.Aux.ID != "" {
|
|
|
|
ids = append(ids, entry.Aux.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|