github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/integration/build/build_session_test.go (about) 1 package build 2 3 import ( 4 "context" 5 "io/ioutil" 6 "net/http" 7 "strings" 8 "testing" 9 10 "github.com/docker/docker/api/types" 11 dclient "github.com/docker/docker/client" 12 "github.com/docker/docker/internal/test/daemon" 13 "github.com/docker/docker/internal/test/fakecontext" 14 "github.com/docker/docker/internal/test/request" 15 "github.com/moby/buildkit/session" 16 "github.com/moby/buildkit/session/filesync" 17 "golang.org/x/sync/errgroup" 18 "gotest.tools/assert" 19 is "gotest.tools/assert/cmp" 20 "gotest.tools/skip" 21 ) 22 23 func TestBuildWithSession(t *testing.T) { 24 skip.If(t, testEnv.DaemonInfo.OSType == "windows") 25 26 var client dclient.APIClient 27 if !testEnv.DaemonInfo.ExperimentalBuild { 28 skip.If(t, testEnv.IsRemoteDaemon, "cannot run daemon when remote daemon") 29 30 d := daemon.New(t, daemon.WithExperimental) 31 d.StartWithBusybox(t) 32 defer d.Stop(t) 33 client = d.NewClientT(t) 34 } else { 35 client = testEnv.APIClient() 36 } 37 38 dockerfile := ` 39 FROM busybox 40 COPY file / 41 RUN cat /file 42 ` 43 44 fctx := fakecontext.New(t, "", 45 fakecontext.WithFile("file", "some content"), 46 ) 47 defer fctx.Close() 48 49 out := testBuildWithSession(t, client, client.DaemonHost(), fctx.Dir, dockerfile) 50 assert.Check(t, is.Contains(out, "some content")) 51 52 fctx.Add("second", "contentcontent") 53 54 dockerfile += ` 55 COPY second / 56 RUN cat /second 57 ` 58 59 out = testBuildWithSession(t, client, client.DaemonHost(), fctx.Dir, dockerfile) 60 assert.Check(t, is.Equal(strings.Count(out, "Using cache"), 2)) 61 assert.Check(t, is.Contains(out, "contentcontent")) 62 63 du, err := client.DiskUsage(context.TODO()) 64 assert.Check(t, err) 65 assert.Check(t, du.BuilderSize > 10) 66 67 out = testBuildWithSession(t, client, client.DaemonHost(), fctx.Dir, dockerfile) 68 assert.Check(t, is.Equal(strings.Count(out, "Using cache"), 4)) 69 70 du2, err := client.DiskUsage(context.TODO()) 71 assert.Check(t, err) 72 assert.Check(t, is.Equal(du.BuilderSize, du2.BuilderSize)) 73 74 // rebuild with regular tar, confirm cache still applies 75 fctx.Add("Dockerfile", dockerfile) 76 // FIXME(vdemeester) use sock here 77 res, body, err := request.Do( 78 "/build", 79 request.Host(client.DaemonHost()), 80 request.Method(http.MethodPost), 81 request.RawContent(fctx.AsTarReader(t)), 82 request.ContentType("application/x-tar")) 83 assert.NilError(t, err) 84 assert.Check(t, is.DeepEqual(http.StatusOK, res.StatusCode)) 85 86 outBytes, err := request.ReadBody(body) 87 assert.NilError(t, err) 88 assert.Check(t, is.Contains(string(outBytes), "Successfully built")) 89 assert.Check(t, is.Equal(strings.Count(string(outBytes), "Using cache"), 4)) 90 91 _, err = client.BuildCachePrune(context.TODO(), types.BuildCachePruneOptions{All: true}) 92 assert.Check(t, err) 93 94 du, err = client.DiskUsage(context.TODO()) 95 assert.Check(t, err) 96 assert.Check(t, is.Equal(du.BuilderSize, int64(0))) 97 } 98 99 func testBuildWithSession(t *testing.T, client dclient.APIClient, daemonHost string, dir, dockerfile string) (outStr string) { 100 ctx := context.Background() 101 sess, err := session.NewSession(ctx, "foo1", "foo") 102 assert.Check(t, err) 103 104 fsProvider := filesync.NewFSSyncProvider([]filesync.SyncedDir{ 105 {Dir: dir}, 106 }) 107 sess.Allow(fsProvider) 108 109 g, ctx := errgroup.WithContext(ctx) 110 111 g.Go(func() error { 112 return sess.Run(ctx, client.DialSession) 113 }) 114 115 g.Go(func() error { 116 // FIXME use sock here 117 res, body, err := request.Do( 118 "/build?remote=client-session&session="+sess.ID(), 119 request.Host(daemonHost), 120 request.Method(http.MethodPost), 121 request.With(func(req *http.Request) error { 122 req.Body = ioutil.NopCloser(strings.NewReader(dockerfile)) 123 return nil 124 }), 125 ) 126 if err != nil { 127 return err 128 } 129 assert.Check(t, is.DeepEqual(res.StatusCode, http.StatusOK)) 130 out, err := request.ReadBody(body) 131 assert.NilError(t, err) 132 assert.Check(t, is.Contains(string(out), "Successfully built")) 133 sess.Close() 134 outStr = string(out) 135 return nil 136 }) 137 138 err = g.Wait() 139 assert.Check(t, err) 140 return 141 }