2017-06-19 20:39:00 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-12-02 02:17:40 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-12 05:08:53 +00:00
|
|
|
"runtime"
|
2017-06-19 20:39:00 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/client/llb"
|
2017-12-02 02:17:40 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
|
|
|
"github.com/moby/buildkit/util/testutil/httpserver"
|
2017-11-27 22:24:29 +00:00
|
|
|
"github.com/moby/buildkit/util/testutil/integration"
|
2017-12-02 02:17:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-06-19 20:39:00 +00:00
|
|
|
)
|
|
|
|
|
2017-11-27 22:24:29 +00:00
|
|
|
func TestClientIntegration(t *testing.T) {
|
2017-11-30 02:49:04 +00:00
|
|
|
integration.Run(t, []integration.Test{
|
|
|
|
testCallDiskUsage,
|
|
|
|
testBuildMultiMount,
|
2017-12-02 02:17:40 +00:00
|
|
|
testBuildHTTPSource,
|
2017-11-30 02:49:04 +00:00
|
|
|
})
|
2017-07-12 05:08:53 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 22:24:29 +00:00
|
|
|
func testCallDiskUsage(t *testing.T, sb integration.Sandbox) {
|
|
|
|
c, err := New(sb.Address())
|
2017-12-02 02:17:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
2017-06-19 20:39:00 +00:00
|
|
|
_, err = c.DiskUsage(context.TODO())
|
2017-12-02 02:17:40 +00:00
|
|
|
require.NoError(t, err)
|
2017-06-19 20:39:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 22:24:29 +00:00
|
|
|
func testBuildMultiMount(t *testing.T, sb integration.Sandbox) {
|
2017-07-12 05:08:53 +00:00
|
|
|
requiresLinux(t)
|
2017-06-19 20:39:00 +00:00
|
|
|
t.Parallel()
|
2017-11-27 22:24:29 +00:00
|
|
|
c, err := New(sb.Address())
|
2017-12-02 02:17:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
2017-06-19 20:39:00 +00:00
|
|
|
|
|
|
|
alpine := llb.Image("docker.io/library/alpine:latest")
|
2017-06-21 21:48:21 +00:00
|
|
|
ls := alpine.Run(llb.Shlex("/bin/ls -l"))
|
2017-06-19 20:39:00 +00:00
|
|
|
busybox := llb.Image("docker.io/library/busybox:latest")
|
2017-06-21 21:48:21 +00:00
|
|
|
cp := ls.Run(llb.Shlex("/bin/cp -a /busybox/etc/passwd baz"))
|
2017-06-19 20:39:00 +00:00
|
|
|
cp.AddMount("/busybox", busybox)
|
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
def, err := cp.Marshal()
|
2017-12-02 02:17:40 +00:00
|
|
|
require.NoError(t, err)
|
2017-06-19 20:39:00 +00:00
|
|
|
|
2017-10-02 04:59:34 +00:00
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{}, nil)
|
2017-12-02 02:17:40 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBuildHTTPSource(t *testing.T, sb integration.Sandbox) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
resp := httpserver.Response{
|
|
|
|
Etag: identity.NewID(),
|
|
|
|
Content: []byte("content1"),
|
|
|
|
}
|
|
|
|
|
|
|
|
server := httpserver.NewTestServer(map[string]httpserver.Response{
|
|
|
|
"/foo": resp,
|
|
|
|
})
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
// invalid URL first
|
|
|
|
st := llb.HTTP(server.URL + "/bar")
|
|
|
|
|
|
|
|
def, err := st.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{}, nil)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "invalid response status 404")
|
|
|
|
|
|
|
|
// first correct request
|
|
|
|
st = llb.HTTP(server.URL + "/foo")
|
|
|
|
|
|
|
|
def, err = st.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, server.Stats("/foo").AllRequests, 1)
|
|
|
|
require.Equal(t, server.Stats("/foo").CachedRequests, 0)
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "buildkit")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{
|
|
|
|
Exporter: ExporterLocal,
|
|
|
|
ExporterAttrs: map[string]string{
|
|
|
|
exporterLocalOutputDir: tmpdir,
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, server.Stats("/foo").AllRequests, 2)
|
|
|
|
require.Equal(t, server.Stats("/foo").CachedRequests, 1)
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(tmpdir, "foo"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("content1"), dt)
|
|
|
|
// TODO: check that second request was marked as cached
|
2017-06-19 20:39:00 +00:00
|
|
|
}
|
2017-11-27 22:24:29 +00:00
|
|
|
|
|
|
|
func requiresLinux(t *testing.T) {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
t.Skipf("unsupported GOOS: %s", runtime.GOOS)
|
|
|
|
}
|
|
|
|
}
|