2018-03-30 07:10:02 -04:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-08-15 17:24:37 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2018-03-30 07:10:02 -04:00
|
|
|
dclient "github.com/docker/docker/client"
|
2018-04-19 05:14:15 -04:00
|
|
|
"github.com/docker/docker/internal/test/daemon"
|
2018-04-16 08:39:13 -04:00
|
|
|
"github.com/docker/docker/internal/test/fakecontext"
|
2018-04-17 04:22:04 -04:00
|
|
|
"github.com/docker/docker/internal/test/request"
|
2018-03-30 07:10:02 -04:00
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/filesync"
|
|
|
|
"golang.org/x/sync/errgroup"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
is "gotest.tools/assert/cmp"
|
|
|
|
"gotest.tools/skip"
|
2018-03-30 07:10:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildWithSession(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon")
|
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
|
|
|
d := daemon.New(t, daemon.WithExperimental)
|
|
|
|
d.StartWithBusybox(t)
|
|
|
|
defer d.Stop(t)
|
2018-03-30 07:10:02 -04:00
|
|
|
|
2018-12-22 08:37:38 -05:00
|
|
|
client := d.NewClientT(t)
|
2018-03-30 07:10:02 -04:00
|
|
|
|
|
|
|
dockerfile := `
|
|
|
|
FROM busybox
|
|
|
|
COPY file /
|
|
|
|
RUN cat /file
|
|
|
|
`
|
|
|
|
|
|
|
|
fctx := fakecontext.New(t, "",
|
|
|
|
fakecontext.WithFile("file", "some content"),
|
|
|
|
)
|
|
|
|
defer fctx.Close()
|
|
|
|
|
2018-04-25 05:03:43 -04:00
|
|
|
out := testBuildWithSession(t, client, client.DaemonHost(), fctx.Dir, dockerfile)
|
2018-03-30 07:10:02 -04:00
|
|
|
assert.Check(t, is.Contains(out, "some content"))
|
|
|
|
|
|
|
|
fctx.Add("second", "contentcontent")
|
|
|
|
|
|
|
|
dockerfile += `
|
|
|
|
COPY second /
|
|
|
|
RUN cat /second
|
|
|
|
`
|
|
|
|
|
2018-04-25 05:03:43 -04:00
|
|
|
out = testBuildWithSession(t, client, client.DaemonHost(), fctx.Dir, dockerfile)
|
2018-03-30 07:10:02 -04:00
|
|
|
assert.Check(t, is.Equal(strings.Count(out, "Using cache"), 2))
|
|
|
|
assert.Check(t, is.Contains(out, "contentcontent"))
|
|
|
|
|
|
|
|
du, err := client.DiskUsage(context.TODO())
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, du.BuilderSize > 10)
|
|
|
|
|
2018-04-25 05:03:43 -04:00
|
|
|
out = testBuildWithSession(t, client, client.DaemonHost(), fctx.Dir, dockerfile)
|
2018-03-30 07:10:02 -04:00
|
|
|
assert.Check(t, is.Equal(strings.Count(out, "Using cache"), 4))
|
|
|
|
|
|
|
|
du2, err := client.DiskUsage(context.TODO())
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.Equal(du.BuilderSize, du2.BuilderSize))
|
|
|
|
|
|
|
|
// rebuild with regular tar, confirm cache still applies
|
|
|
|
fctx.Add("Dockerfile", dockerfile)
|
|
|
|
// FIXME(vdemeester) use sock here
|
2018-04-17 04:22:04 -04:00
|
|
|
res, body, err := request.Do(
|
2018-03-30 07:10:02 -04:00
|
|
|
"/build",
|
2018-04-25 05:03:43 -04:00
|
|
|
request.Host(client.DaemonHost()),
|
2018-03-30 07:10:02 -04:00
|
|
|
request.Method(http.MethodPost),
|
|
|
|
request.RawContent(fctx.AsTarReader(t)),
|
|
|
|
request.ContentType("application/x-tar"))
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.DeepEqual(http.StatusOK, res.StatusCode))
|
|
|
|
|
|
|
|
outBytes, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(outBytes), "Successfully built"))
|
|
|
|
assert.Check(t, is.Equal(strings.Count(string(outBytes), "Using cache"), 4))
|
|
|
|
|
2018-08-15 17:24:37 -04:00
|
|
|
_, err = client.BuildCachePrune(context.TODO(), types.BuildCachePruneOptions{All: true})
|
2018-03-30 07:10:02 -04:00
|
|
|
assert.Check(t, err)
|
|
|
|
|
|
|
|
du, err = client.DiskUsage(context.TODO())
|
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.Equal(du.BuilderSize, int64(0)))
|
|
|
|
}
|
|
|
|
|
2018-04-25 05:03:43 -04:00
|
|
|
func testBuildWithSession(t *testing.T, client dclient.APIClient, daemonHost string, dir, dockerfile string) (outStr string) {
|
2018-06-02 12:46:53 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
sess, err := session.NewSession(ctx, "foo1", "foo")
|
2018-03-30 07:10:02 -04:00
|
|
|
assert.Check(t, err)
|
|
|
|
|
|
|
|
fsProvider := filesync.NewFSSyncProvider([]filesync.SyncedDir{
|
|
|
|
{Dir: dir},
|
|
|
|
})
|
|
|
|
sess.Allow(fsProvider)
|
|
|
|
|
2018-04-18 17:55:50 -04:00
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
2018-03-30 07:10:02 -04:00
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
return sess.Run(ctx, client.DialSession)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
// FIXME use sock here
|
2018-04-17 04:22:04 -04:00
|
|
|
res, body, err := request.Do(
|
2018-03-30 07:10:02 -04:00
|
|
|
"/build?remote=client-session&session="+sess.ID(),
|
2018-04-25 05:03:43 -04:00
|
|
|
request.Host(daemonHost),
|
2018-03-30 07:10:02 -04:00
|
|
|
request.Method(http.MethodPost),
|
2018-04-17 04:22:04 -04:00
|
|
|
request.With(func(req *http.Request) error {
|
2018-03-30 07:10:02 -04:00
|
|
|
req.Body = ioutil.NopCloser(strings.NewReader(dockerfile))
|
|
|
|
return nil
|
2018-04-17 04:22:04 -04:00
|
|
|
}),
|
2018-03-30 07:10:02 -04:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
assert.Check(t, is.DeepEqual(res.StatusCode, http.StatusOK))
|
|
|
|
out, err := request.ReadBody(body)
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Contains(string(out), "Successfully built"))
|
|
|
|
sess.Close()
|
|
|
|
outStr = string(out)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
err = g.Wait()
|
|
|
|
assert.Check(t, err)
|
|
|
|
return
|
|
|
|
}
|