2015-08-23 15:52:25 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
2017-06-05 15:12:26 +00:00
|
|
|
"encoding/json"
|
2017-06-20 00:15:23 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-02-28 16:12:30 +00:00
|
|
|
"io/ioutil"
|
2015-08-23 15:52:25 +00:00
|
|
|
"net/http"
|
2016-04-01 17:49:04 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-08-23 15:52:25 +00:00
|
|
|
|
2017-06-20 00:15:23 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-12-30 17:23:00 +00:00
|
|
|
"github.com/docker/docker/integration-cli/checker"
|
2017-04-10 12:42:21 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build/fakecontext"
|
2017-04-19 13:04:24 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build/fakegit"
|
2017-04-10 12:42:21 +00:00
|
|
|
"github.com/docker/docker/integration-cli/cli/build/fakestorage"
|
2016-12-30 09:49:36 +00:00
|
|
|
"github.com/docker/docker/integration-cli/request"
|
2015-08-23 15:52:25 +00:00
|
|
|
"github.com/go-check/check"
|
2017-07-28 23:04:34 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/filesync"
|
2017-06-05 15:12:26 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/net/context"
|
2017-05-15 21:54:27 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2015-08-23 15:52:25 +00:00
|
|
|
)
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestBuildAPIDockerFileRemote(c *check.C) {
|
2017-09-12 12:53:20 +00:00
|
|
|
testRequires(c, NotUserNamespace)
|
2017-09-11 14:34:08 +00:00
|
|
|
|
2016-09-19 19:53:14 +00:00
|
|
|
var testD string
|
2018-01-15 14:32:06 +00:00
|
|
|
if testEnv.OSType == "windows" {
|
2016-09-19 19:53:14 +00:00
|
|
|
testD = `FROM busybox
|
2015-08-23 15:52:25 +00:00
|
|
|
RUN find / -name ba*
|
2016-09-19 19:53:14 +00: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 12:42:21 +00:00
|
|
|
server := fakestorage.New(c, "", fakecontext.WithFiles(map[string]string{"testD": testD}))
|
2015-08-23 15:52:25 +00:00
|
|
|
defer server.Close()
|
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
res, body, err := request.Post("/build?dockerfile=baz&remote="+server.URL()+"/testD", request.JSON)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
buf, err := request.ReadBody(body)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
|
|
|
// Make sure Dockerfile exists.
|
|
|
|
// Make sure 'baz' doesn't exist ANYWHERE despite being mentioned in the URL
|
|
|
|
out := string(buf)
|
2017-03-20 22:22:29 +00:00
|
|
|
c.Assert(out, checker.Contains, "RUN find /tmp")
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(out, checker.Not(checker.Contains), "baz")
|
2015-08-23 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestBuildAPIRemoteTarballContext(c *check.C) {
|
2015-08-23 15:52:25 +00:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
dockerfile := []byte("FROM busybox")
|
2015-10-17 12:24:56 +00:00
|
|
|
err := tw.WriteHeader(&tar.Header{
|
2015-08-23 15:52:25 +00:00
|
|
|
Name: "Dockerfile",
|
|
|
|
Size: int64(len(dockerfile)),
|
2015-10-17 12:24:56 +00: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 15:52:25 +00:00
|
|
|
|
2017-04-10 12:42:21 +00:00
|
|
|
server := fakestorage.New(c, "", fakecontext.WithBinaryFiles(map[string]*bytes.Buffer{
|
2015-08-23 15:52:25 +00:00
|
|
|
"testT.tar": buffer,
|
2017-04-10 12:42:21 +00:00
|
|
|
}))
|
2015-08-23 15:52:25 +00:00
|
|
|
defer server.Close()
|
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
res, b, err := request.Post("/build?remote="+server.URL()+"/testT.tar", request.ContentType("application/tar"))
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 15:52:25 +00:00
|
|
|
b.Close()
|
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestBuildAPIRemoteTarballContextWithCustomDockerfile(c *check.C) {
|
2015-08-23 15:52:25 +00:00
|
|
|
buffer := new(bytes.Buffer)
|
|
|
|
tw := tar.NewWriter(buffer)
|
|
|
|
defer tw.Close()
|
|
|
|
|
|
|
|
dockerfile := []byte(`FROM busybox
|
|
|
|
RUN echo 'wrong'`)
|
2015-10-17 12:24:56 +00:00
|
|
|
err := tw.WriteHeader(&tar.Header{
|
2015-08-23 15:52:25 +00:00
|
|
|
Name: "Dockerfile",
|
|
|
|
Size: int64(len(dockerfile)),
|
2015-10-17 12:24:56 +00: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 15:52:25 +00:00
|
|
|
|
|
|
|
custom := []byte(`FROM busybox
|
|
|
|
RUN echo 'right'
|
|
|
|
`)
|
2015-10-17 12:24:56 +00:00
|
|
|
err = tw.WriteHeader(&tar.Header{
|
2015-08-23 15:52:25 +00:00
|
|
|
Name: "custom",
|
|
|
|
Size: int64(len(custom)),
|
2015-10-17 12:24:56 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// failed to write tar file header
|
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
2015-10-17 12:24:56 +00: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 15:52:25 +00:00
|
|
|
|
2017-04-10 12:42:21 +00:00
|
|
|
server := fakestorage.New(c, "", fakecontext.WithBinaryFiles(map[string]*bytes.Buffer{
|
2015-08-23 15:52:25 +00:00
|
|
|
"testT.tar": buffer,
|
2017-04-10 12:42:21 +00:00
|
|
|
}))
|
2015-08-23 15:52:25 +00:00
|
|
|
defer server.Close()
|
2017-01-16 10:30:14 +00:00
|
|
|
|
2015-08-23 15:52:25 +00:00
|
|
|
url := "/build?dockerfile=custom&remote=" + server.URL() + "/testT.tar"
|
2017-03-06 15:35:27 +00:00
|
|
|
res, body, err := request.Post(url, request.ContentType("application/tar"))
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
|
|
|
defer body.Close()
|
2017-08-21 22:50:40 +00:00
|
|
|
content, err := request.ReadBody(body)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
2015-10-17 12:24:56 +00:00
|
|
|
// Build used the wrong dockerfile.
|
|
|
|
c.Assert(string(content), checker.Not(checker.Contains), "wrong")
|
2015-08-23 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestBuildAPILowerDockerfile(c *check.C) {
|
2017-04-19 13:04:24 +00:00
|
|
|
git := fakegit.New(c, "repo", map[string]string{
|
2015-08-23 15:52:25 +00:00
|
|
|
"dockerfile": `FROM busybox
|
|
|
|
RUN echo from dockerfile`,
|
|
|
|
}, false)
|
|
|
|
defer git.Close()
|
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
res, body, err := request.Post("/build?remote="+git.RepoURL, request.JSON)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
buf, err := request.ReadBody(body)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
|
|
|
out := string(buf)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(out, checker.Contains, "from dockerfile")
|
2015-08-23 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestBuildAPIBuildGitWithF(c *check.C) {
|
2017-04-19 13:04:24 +00:00
|
|
|
git := fakegit.New(c, "repo", map[string]string{
|
2015-08-23 15:52:25 +00: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 15:35:27 +00:00
|
|
|
res, body, err := request.Post("/build?dockerfile=baz&remote="+git.RepoURL, request.JSON)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
buf, err := request.ReadBody(body)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
|
|
|
out := string(buf)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(out, checker.Contains, "from baz")
|
2015-08-23 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestBuildAPIDoubleDockerfile(c *check.C) {
|
2017-09-12 12:53:20 +00:00
|
|
|
testRequires(c, UnixCli) // dockerfile overwrites Dockerfile on Windows
|
2017-04-19 13:04:24 +00:00
|
|
|
git := fakegit.New(c, "repo", map[string]string{
|
2015-08-23 15:52:25 +00: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 15:35:27 +00:00
|
|
|
res, body, err := request.Post("/build?remote="+git.RepoURL, request.JSON)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
buf, err := request.ReadBody(body)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
2015-08-23 15:52:25 +00:00
|
|
|
|
|
|
|
out := string(buf)
|
2015-10-17 12:24:56 +00:00
|
|
|
c.Assert(out, checker.Contains, "from Dockerfile")
|
2015-08-23 15:52:25 +00:00
|
|
|
}
|
|
|
|
|
[nit] integration-cli: obey Go's naming convention
No substantial code change.
- Api --> API
- Cli --> CLI
- Http, Https --> HTTP, HTTPS
- Id --> ID
- Uid,Gid,Pid --> UID,PID,PID
- Ipam --> IPAM
- Tls --> TLS (TestDaemonNoTlsCliTlsVerifyWithEnv --> TestDaemonTLSVerifyIssue13964)
Didn't touch in this commit:
- Git: because it is officially "Git": https://git-scm.com/
- Tar: because it is officially "Tar": https://www.gnu.org/software/tar/
- Cpu, Nat, Mac, Ipc, Shm: for keeping a consistency with existing production code (not changable, for compatibility)
Signed-off-by: Akihiro Suda <suda.akihiro@lab.ntt.co.jp>
2016-09-28 01:50:12 +00:00
|
|
|
func (s *DockerSuite) TestBuildAPIUnnormalizedTarPaths(c *check.C) {
|
2016-04-01 17:49:04 +00: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)),
|
|
|
|
})
|
|
|
|
//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)
|
|
|
|
|
2017-03-06 15:35:27 +00:00
|
|
|
res, body, err := request.Post("/build", request.RawContent(ioutil.NopCloser(buffer)), request.ContentType("application/x-tar"))
|
2016-04-01 17:49:04 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
out, err := request.ReadBody(body)
|
2016-04-01 17:49:04 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-06-02 20:06:14 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildOnBuildWithCopy(c *check.C) {
|
|
|
|
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"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
out, err := request.ReadBody(body)
|
2017-06-02 20:06:14 +00:00
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(string(out), checker.Contains, "Successfully built")
|
|
|
|
}
|
2017-06-05 15:12:26 +00:00
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildOnBuildCache(c *check.C) {
|
|
|
|
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"))
|
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Equal(c, http.StatusOK, res.StatusCode)
|
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
out, err := request.ReadBody(body)
|
2017-06-05 15:12:26 +00:00
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Contains(c, string(out), "Successfully built")
|
|
|
|
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)
|
|
|
|
assert.Len(c, imageIDs, 2)
|
|
|
|
parentID, childID := imageIDs[0], imageIDs[1]
|
|
|
|
|
|
|
|
client, err := request.NewClient()
|
|
|
|
require.NoError(c, err)
|
|
|
|
|
|
|
|
// check parentID is correct
|
|
|
|
image, _, err := client.ImageInspectWithRaw(context.Background(), childID)
|
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Equal(c, parentID, image.Parent)
|
|
|
|
}
|
|
|
|
|
2017-06-20 00:15:23 +00:00
|
|
|
func (s *DockerRegistrySuite) TestBuildCopyFromForcePull(c *check.C) {
|
|
|
|
client, err := request.NewClient()
|
|
|
|
require.NoError(c, err)
|
|
|
|
|
|
|
|
repoName := fmt.Sprintf("%v/dockercli/busybox", privateRegistryURL)
|
|
|
|
// tag the image to upload it to the private registry
|
|
|
|
err = client.ImageTag(context.TODO(), "busybox", repoName)
|
|
|
|
assert.Nil(c, err)
|
|
|
|
// push the image to the registry
|
|
|
|
rc, err := client.ImagePush(context.TODO(), repoName, types.ImagePushOptions{RegistryAuth: "{}"})
|
|
|
|
assert.Nil(c, err)
|
|
|
|
_, err = io.Copy(ioutil.Discard, rc)
|
|
|
|
assert.Nil(c, err)
|
|
|
|
|
|
|
|
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"))
|
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Equal(c, http.StatusOK, res.StatusCode)
|
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
out, err := request.ReadBody(body)
|
2017-06-27 17:38:01 +00:00
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Contains(c, string(out), "Successfully built")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildAddRemoteNoDecompress(c *check.C) {
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
require.NoError(c, err)
|
|
|
|
_, err = tw.Write(dt)
|
|
|
|
require.NoError(c, err)
|
|
|
|
err = tw.Close()
|
|
|
|
require.NoError(c, err)
|
|
|
|
|
|
|
|
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"))
|
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Equal(c, http.StatusOK, res.StatusCode)
|
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
out, err := request.ReadBody(body)
|
2017-06-20 00:15:23 +00:00
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Contains(c, string(out), "Successfully built")
|
2017-07-26 16:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildChownOnCopy(c *check.C) {
|
|
|
|
testRequires(c, DaemonIsLinux)
|
|
|
|
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"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
|
|
|
|
2017-08-28 17:19:54 +00:00
|
|
|
out, err := request.ReadBody(body)
|
2017-06-20 00:15:23 +00:00
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Contains(c, string(out), "Successfully built")
|
|
|
|
}
|
|
|
|
|
2017-05-22 15:21:17 +00:00
|
|
|
func (s *DockerSuite) TestBuildCopyCacheOnFileChange(c *check.C) {
|
|
|
|
|
|
|
|
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"))
|
|
|
|
|
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Equal(c, http.StatusOK, res.StatusCode)
|
|
|
|
|
|
|
|
out, err := request.ReadBody(body)
|
2018-01-13 12:35:10 +00:00
|
|
|
require.NoError(c, err)
|
2017-05-22 15:21:17 +00:00
|
|
|
|
|
|
|
ids := getImageIDsFromBuild(c, out)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *DockerSuite) TestBuildAddCacheOnFileChange(c *check.C) {
|
|
|
|
|
|
|
|
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"))
|
|
|
|
|
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Equal(c, http.StatusOK, res.StatusCode)
|
|
|
|
|
|
|
|
out, err := request.ReadBody(body)
|
2018-01-13 12:35:10 +00:00
|
|
|
require.NoError(c, err)
|
2017-05-22 15:21:17 +00:00
|
|
|
|
|
|
|
ids := getImageIDsFromBuild(c, out)
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 21:54:27 +00:00
|
|
|
func (s *DockerSuite) TestBuildWithSession(c *check.C) {
|
|
|
|
testRequires(c, ExperimentalDaemon)
|
|
|
|
|
|
|
|
dockerfile := `
|
|
|
|
FROM busybox
|
|
|
|
COPY file /
|
|
|
|
RUN cat /file
|
|
|
|
`
|
|
|
|
|
|
|
|
fctx := fakecontext.New(c, "",
|
|
|
|
fakecontext.WithFile("file", "some content"),
|
|
|
|
)
|
|
|
|
defer fctx.Close()
|
|
|
|
|
|
|
|
out := testBuildWithSession(c, fctx.Dir, dockerfile)
|
|
|
|
assert.Contains(c, out, "some content")
|
|
|
|
|
|
|
|
fctx.Add("second", "contentcontent")
|
|
|
|
|
|
|
|
dockerfile += `
|
|
|
|
COPY second /
|
|
|
|
RUN cat /second
|
|
|
|
`
|
|
|
|
|
|
|
|
out = testBuildWithSession(c, fctx.Dir, dockerfile)
|
|
|
|
assert.Equal(c, strings.Count(out, "Using cache"), 2)
|
|
|
|
assert.Contains(c, out, "contentcontent")
|
|
|
|
|
|
|
|
client, err := request.NewClient()
|
|
|
|
require.NoError(c, err)
|
|
|
|
|
|
|
|
du, err := client.DiskUsage(context.TODO())
|
|
|
|
assert.Nil(c, err)
|
|
|
|
assert.True(c, du.BuilderSize > 10)
|
|
|
|
|
|
|
|
out = testBuildWithSession(c, fctx.Dir, dockerfile)
|
|
|
|
assert.Equal(c, strings.Count(out, "Using cache"), 4)
|
|
|
|
|
|
|
|
du2, err := client.DiskUsage(context.TODO())
|
|
|
|
assert.Nil(c, err)
|
|
|
|
assert.Equal(c, du.BuilderSize, du2.BuilderSize)
|
|
|
|
|
|
|
|
// rebuild with regular tar, confirm cache still applies
|
|
|
|
fctx.Add("Dockerfile", dockerfile)
|
|
|
|
res, body, err := request.Post(
|
|
|
|
"/build",
|
|
|
|
request.RawContent(fctx.AsTarReader(c)),
|
|
|
|
request.ContentType("application/x-tar"))
|
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Equal(c, http.StatusOK, res.StatusCode)
|
|
|
|
|
2017-08-21 22:50:40 +00:00
|
|
|
outBytes, err := request.ReadBody(body)
|
2017-05-15 21:54:27 +00:00
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Contains(c, string(outBytes), "Successfully built")
|
|
|
|
assert.Equal(c, strings.Count(string(outBytes), "Using cache"), 4)
|
|
|
|
|
|
|
|
_, err = client.BuildCachePrune(context.TODO())
|
|
|
|
assert.Nil(c, err)
|
|
|
|
|
|
|
|
du, err = client.DiskUsage(context.TODO())
|
|
|
|
assert.Nil(c, err)
|
|
|
|
assert.Equal(c, du.BuilderSize, int64(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBuildWithSession(c *check.C, dir, dockerfile string) (outStr string) {
|
|
|
|
client, err := request.NewClient()
|
|
|
|
require.NoError(c, err)
|
|
|
|
|
|
|
|
sess, err := session.NewSession("foo1", "foo")
|
|
|
|
assert.Nil(c, err)
|
|
|
|
|
2017-09-22 05:07:45 +00:00
|
|
|
fsProvider := filesync.NewFSSyncProvider([]filesync.SyncedDir{
|
|
|
|
{Dir: dir},
|
|
|
|
})
|
2017-05-15 21:54:27 +00:00
|
|
|
sess.Allow(fsProvider)
|
|
|
|
|
|
|
|
g, ctx := errgroup.WithContext(context.Background())
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
return sess.Run(ctx, client.DialSession)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Go(func() error {
|
2017-09-22 05:07:45 +00:00
|
|
|
res, body, err := request.Post("/build?remote=client-session&session="+sess.ID(), func(req *http.Request) error {
|
2017-05-15 21:54:27 +00:00
|
|
|
req.Body = ioutil.NopCloser(strings.NewReader(dockerfile))
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
assert.Equal(c, res.StatusCode, http.StatusOK)
|
2017-08-21 22:50:40 +00:00
|
|
|
out, err := request.ReadBody(body)
|
2017-05-15 21:54:27 +00:00
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Contains(c, string(out), "Successfully built")
|
|
|
|
sess.Close()
|
|
|
|
outStr = string(out)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
err = g.Wait()
|
|
|
|
assert.Nil(c, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-07 02:21:10 +00:00
|
|
|
func (s *DockerSuite) TestBuildScratchCopy(c *check.C) {
|
|
|
|
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"))
|
|
|
|
c.Assert(err, checker.IsNil)
|
|
|
|
c.Assert(res.StatusCode, checker.Equals, http.StatusOK)
|
|
|
|
|
|
|
|
out, err := request.ReadBody(body)
|
|
|
|
require.NoError(c, err)
|
|
|
|
assert.Contains(c, string(out), "Successfully built")
|
|
|
|
}
|
|
|
|
|
2017-06-05 15:12:26 +00:00
|
|
|
type buildLine struct {
|
|
|
|
Stream string
|
|
|
|
Aux struct {
|
|
|
|
ID string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getImageIDsFromBuild(c *check.C, output []byte) []string {
|
|
|
|
ids := []string{}
|
|
|
|
for _, line := range bytes.Split(output, []byte("\n")) {
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
entry := buildLine{}
|
|
|
|
require.NoError(c, json.Unmarshal(line, &entry))
|
|
|
|
if entry.Aux.ID != "" {
|
|
|
|
ids = append(ids, entry.Aux.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ids
|
|
|
|
}
|