2018-03-30 07:10:02 -04:00
|
|
|
package build
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
2019-04-02 00:08:16 -04:00
|
|
|
"net"
|
2018-03-30 07:10:02 -04:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2018-08-15 17:24:37 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2019-07-17 17:40:37 -04:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2018-03-30 07:10:02 -04:00
|
|
|
dclient "github.com/docker/docker/client"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/fakecontext"
|
|
|
|
"github.com/docker/docker/testutil/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"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-03-30 07:10:02 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildWithSession(t *testing.T) {
|
2019-09-24 17:37:38 -04:00
|
|
|
t.Skip("TODO: BuildKit")
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2019-07-17 17:40:37 -04:00
|
|
|
skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.39"), "experimental in older versions")
|
2018-03-30 07:10:02 -04:00
|
|
|
|
2019-07-17 17:40:37 -04:00
|
|
|
client := testEnv.APIClient()
|
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 {
|
2019-04-02 00:08:16 -04:00
|
|
|
return sess.Run(ctx, func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) {
|
|
|
|
return client.DialHijack(ctx, "/session", "h2c", meta)
|
|
|
|
})
|
2018-03-30 07:10:02 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|