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