github.com/rumpl/bof@v23.0.0-rc.2+incompatible/integration/build/build_session_test.go (about)

     1  package build
     2  
     3  import (
     4  	"context"
     5  	"io"
     6  	"net"
     7  	"net/http"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/docker/docker/api/types"
    12  	"github.com/docker/docker/api/types/versions"
    13  	dclient "github.com/docker/docker/client"
    14  	"github.com/docker/docker/testutil/fakecontext"
    15  	"github.com/docker/docker/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(), types.DiskUsageOptions{})
    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(), types.DiskUsageOptions{})
    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(), types.DiskUsageOptions{})
    88  	assert.Check(t, err)
    89  	assert.Check(t, is.Equal(du.BuilderSize, int64(0)))
    90  }
    91  
    92  //nolint:unused // false positive: linter detects this as "unused"
    93  func testBuildWithSession(t *testing.T, client dclient.APIClient, daemonHost string, dir, dockerfile string) (outStr string) {
    94  	ctx := context.Background()
    95  	sess, err := session.NewSession(ctx, "foo1", "foo")
    96  	assert.Check(t, err)
    97  
    98  	fsProvider := filesync.NewFSSyncProvider([]filesync.SyncedDir{
    99  		{Dir: dir},
   100  	})
   101  	sess.Allow(fsProvider)
   102  
   103  	g, ctx := errgroup.WithContext(ctx)
   104  
   105  	g.Go(func() error {
   106  		return sess.Run(ctx, func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) {
   107  			return client.DialHijack(ctx, "/session", "h2c", meta)
   108  		})
   109  	})
   110  
   111  	g.Go(func() error {
   112  		// FIXME use sock here
   113  		res, body, err := request.Do(
   114  			"/build?remote=client-session&session="+sess.ID(),
   115  			request.Host(daemonHost),
   116  			request.Method(http.MethodPost),
   117  			request.With(func(req *http.Request) error {
   118  				req.Body = io.NopCloser(strings.NewReader(dockerfile))
   119  				return nil
   120  			}),
   121  		)
   122  		if err != nil {
   123  			return err
   124  		}
   125  		assert.Check(t, is.DeepEqual(res.StatusCode, http.StatusOK))
   126  		out, err := request.ReadBody(body)
   127  		assert.NilError(t, err)
   128  		assert.Check(t, is.Contains(string(out), "Successfully built"))
   129  		sess.Close()
   130  		outStr = string(out)
   131  		return nil
   132  	})
   133  
   134  	err = g.Wait()
   135  	assert.Check(t, err)
   136  	return
   137  }