2017-06-19 20:39:00 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2017-12-05 23:32:52 +00:00
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2017-06-19 20:39:00 +00:00
|
|
|
"context"
|
2017-12-05 23:32:52 +00:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2017-12-02 02:17:40 +00:00
|
|
|
"io/ioutil"
|
2017-12-04 03:38:37 +00:00
|
|
|
"net/http"
|
2017-12-02 02:17:40 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-07-12 05:08:53 +00:00
|
|
|
"runtime"
|
2017-12-05 23:32:52 +00:00
|
|
|
"strings"
|
2017-06-19 20:39:00 +00:00
|
|
|
"testing"
|
2017-12-04 03:38:37 +00:00
|
|
|
"time"
|
2017-06-19 20:39:00 +00:00
|
|
|
|
2017-12-05 23:32:52 +00:00
|
|
|
"github.com/containerd/containerd"
|
|
|
|
"github.com/containerd/containerd/content"
|
2017-12-28 23:03:49 +00:00
|
|
|
"github.com/containerd/containerd/images"
|
2017-12-05 23:32:52 +00:00
|
|
|
"github.com/containerd/containerd/namespaces"
|
2017-12-28 23:03:49 +00:00
|
|
|
"github.com/containerd/containerd/snapshots"
|
2017-12-05 23:32:52 +00:00
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
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-05 23:32:52 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
"github.com/pkg/errors"
|
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-12-05 23:32:52 +00:00
|
|
|
testBuildPushAndValidate,
|
2017-12-11 02:18:18 +00:00
|
|
|
testResolveAndHosts,
|
2017-12-11 21:17:07 +00:00
|
|
|
testUser,
|
2017-12-13 05:55:16 +00:00
|
|
|
testOCIExporter,
|
2018-02-09 19:03:12 +00:00
|
|
|
testWhiteoutParentDir,
|
2018-02-13 19:18:18 +00:00
|
|
|
testDuplicateWhiteouts,
|
2018-03-02 16:33:08 +00:00
|
|
|
testSchema1Image,
|
2018-03-21 17:04:43 +00:00
|
|
|
testMountWithNoSource,
|
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) {
|
2017-12-09 02:19:08 +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
|
|
|
_, 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-06-19 20:39:00 +00:00
|
|
|
t.Parallel()
|
2017-12-09 02:19:08 +00:00
|
|
|
requiresLinux(t)
|
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)
|
2017-12-28 23:03:49 +00:00
|
|
|
|
|
|
|
checkAllReleasable(t, c, sb, true)
|
2017-12-02 02:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func testBuildHTTPSource(t *testing.T, sb integration.Sandbox) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
2017-12-04 03:38:37 +00:00
|
|
|
modTime := time.Now().Add(-24 * time.Hour) // avoid falso positive with current time
|
|
|
|
|
2017-12-02 02:17:40 +00:00
|
|
|
resp := httpserver.Response{
|
2017-12-04 03:38:37 +00:00
|
|
|
Etag: identity.NewID(),
|
|
|
|
Content: []byte("content1"),
|
|
|
|
LastModified: &modTime,
|
2017-12-02 02:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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{
|
2018-03-27 04:16:32 +00:00
|
|
|
Exporter: ExporterLocal,
|
|
|
|
ExporterOutputDir: tmpdir,
|
2017-12-02 02:17:40 +00:00
|
|
|
}, 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)
|
2017-12-04 03:38:37 +00:00
|
|
|
|
|
|
|
// test extra options
|
|
|
|
st = llb.HTTP(server.URL+"/foo", llb.Filename("bar"), llb.Chmod(0741), llb.Chown(1000, 1000))
|
|
|
|
|
|
|
|
def, err = st.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{
|
2018-03-27 04:16:32 +00:00
|
|
|
Exporter: ExporterLocal,
|
|
|
|
ExporterOutputDir: tmpdir,
|
2017-12-04 03:38:37 +00:00
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, server.Stats("/foo").AllRequests, 3)
|
|
|
|
require.Equal(t, server.Stats("/foo").CachedRequests, 1)
|
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(tmpdir, "bar"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, []byte("content1"), dt)
|
|
|
|
|
|
|
|
fi, err := os.Stat(filepath.Join(tmpdir, "bar"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, fi.ModTime().Format(http.TimeFormat), modTime.Format(http.TimeFormat))
|
|
|
|
require.Equal(t, int(fi.Mode()&0777), 0741)
|
|
|
|
|
2017-12-28 23:03:49 +00:00
|
|
|
checkAllReleasable(t, c, sb, true)
|
|
|
|
|
2017-12-02 02:17:40 +00:00
|
|
|
// 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
|
|
|
|
2017-12-11 02:18:18 +00:00
|
|
|
func testResolveAndHosts(t *testing.T, sb integration.Sandbox) {
|
|
|
|
requiresLinux(t)
|
|
|
|
t.Parallel()
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
busybox := llb.Image("busybox:latest")
|
|
|
|
st := llb.Scratch()
|
|
|
|
|
|
|
|
run := func(cmd string) {
|
|
|
|
st = busybox.Run(llb.Shlex(cmd), llb.Dir("/wd")).AddMount("/wd", st)
|
|
|
|
}
|
|
|
|
|
|
|
|
run(`sh -c "cp /etc/resolv.conf ."`)
|
|
|
|
run(`sh -c "cp /etc/hosts ."`)
|
|
|
|
|
|
|
|
def, err := st.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
destDir, err := ioutil.TempDir("", "buildkit")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(destDir)
|
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{
|
2018-03-27 04:16:32 +00:00
|
|
|
Exporter: ExporterLocal,
|
|
|
|
ExporterOutputDir: destDir,
|
2017-12-11 02:18:18 +00:00
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(destDir, "resolv.conf"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(dt), "nameserver")
|
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(destDir, "hosts"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(dt), "127.0.0.1 localhost")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-11 21:17:07 +00:00
|
|
|
func testUser(t *testing.T, sb integration.Sandbox) {
|
|
|
|
requiresLinux(t)
|
|
|
|
t.Parallel()
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
st := llb.Image("busybox:latest").Run(llb.Shlex(`sh -c "mkdir -m 0777 /wd"`))
|
|
|
|
|
|
|
|
run := func(user, cmd string) {
|
|
|
|
st = st.Run(llb.Shlex(cmd), llb.Dir("/wd"), llb.User(user))
|
|
|
|
}
|
|
|
|
|
|
|
|
run("daemon", `sh -c "id -nu > user"`)
|
|
|
|
run("daemon:daemon", `sh -c "id -ng > group"`)
|
|
|
|
run("daemon:nogroup", `sh -c "id -ng > nogroup"`)
|
|
|
|
run("1:1", `sh -c "id -g > userone"`)
|
|
|
|
|
|
|
|
st = st.Run(llb.Shlex("cp -a /wd/. /out/"))
|
|
|
|
out := st.AddMount("/out", llb.Scratch())
|
|
|
|
|
|
|
|
def, err := out.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
destDir, err := ioutil.TempDir("", "buildkit")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(destDir)
|
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{
|
2018-03-27 04:16:32 +00:00
|
|
|
Exporter: ExporterLocal,
|
|
|
|
ExporterOutputDir: destDir,
|
2017-12-11 21:17:07 +00:00
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(destDir, "user"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(dt), "daemon")
|
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(destDir, "group"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(dt), "daemon")
|
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(destDir, "nogroup"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(dt), "nogroup")
|
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(destDir, "userone"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, string(dt), "1")
|
2017-12-28 23:03:49 +00:00
|
|
|
|
|
|
|
checkAllReleasable(t, c, sb, true)
|
2017-12-11 21:17:07 +00:00
|
|
|
}
|
|
|
|
|
2017-12-13 05:55:16 +00:00
|
|
|
func testOCIExporter(t *testing.T, sb integration.Sandbox) {
|
|
|
|
requiresLinux(t)
|
|
|
|
t.Parallel()
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
busybox := llb.Image("busybox:latest")
|
|
|
|
st := llb.Scratch()
|
|
|
|
|
|
|
|
run := func(cmd string) {
|
|
|
|
st = busybox.Run(llb.Shlex(cmd), llb.Dir("/wd")).AddMount("/wd", st)
|
|
|
|
}
|
|
|
|
|
|
|
|
run(`sh -c "echo -n first > foo"`)
|
|
|
|
run(`sh -c "echo -n second > bar"`)
|
|
|
|
|
|
|
|
def, err := st.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-12-18 00:20:19 +00:00
|
|
|
for _, exp := range []string{ExporterOCI, ExporterDocker} {
|
|
|
|
|
|
|
|
destDir, err := ioutil.TempDir("", "buildkit")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(destDir)
|
|
|
|
|
|
|
|
out := filepath.Join(destDir, "out.tar")
|
2018-03-27 04:16:32 +00:00
|
|
|
outW, err := os.Create(out)
|
|
|
|
require.NoError(t, err)
|
2017-12-18 00:20:19 +00:00
|
|
|
target := "example.com/buildkit/testoci:latest"
|
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{
|
|
|
|
Exporter: exp,
|
|
|
|
ExporterAttrs: map[string]string{
|
2018-03-27 04:16:32 +00:00
|
|
|
"name": target,
|
2017-12-18 00:20:19 +00:00
|
|
|
},
|
2018-03-27 04:16:32 +00:00
|
|
|
ExporterOutput: outW,
|
2017-12-18 00:20:19 +00:00
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(out)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := readTarToMap(dt, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok := m["oci-layout"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
var index ocispec.Index
|
|
|
|
err = json.Unmarshal(m["index.json"].data, &index)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, index.SchemaVersion)
|
|
|
|
require.Equal(t, 1, len(index.Manifests))
|
|
|
|
|
|
|
|
var mfst ocispec.Manifest
|
|
|
|
err = json.Unmarshal(m["blobs/sha256/"+index.Manifests[0].Digest.Hex()].data, &mfst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, len(mfst.Layers))
|
|
|
|
|
|
|
|
var ociimg ocispec.Image
|
|
|
|
err = json.Unmarshal(m["blobs/sha256/"+mfst.Config.Digest.Hex()].data, &ociimg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "layers", ociimg.RootFS.Type)
|
|
|
|
require.Equal(t, 2, len(ociimg.RootFS.DiffIDs))
|
|
|
|
|
|
|
|
_, ok = m["blobs/sha256/"+mfst.Layers[0].Digest.Hex()]
|
|
|
|
require.True(t, ok)
|
|
|
|
_, ok = m["blobs/sha256/"+mfst.Layers[1].Digest.Hex()]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
if exp != ExporterDocker {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var dockerMfst []struct {
|
|
|
|
Config string
|
|
|
|
RepoTags []string
|
|
|
|
Layers []string
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(m["manifest.json"].data, &dockerMfst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 1, len(dockerMfst))
|
|
|
|
|
|
|
|
_, ok = m[dockerMfst[0].Config]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, 2, len(dockerMfst[0].Layers))
|
|
|
|
require.Equal(t, 1, len(dockerMfst[0].RepoTags))
|
|
|
|
require.Equal(t, target, dockerMfst[0].RepoTags[0])
|
|
|
|
|
|
|
|
for _, l := range dockerMfst[0].Layers {
|
|
|
|
_, ok := m[l]
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
}
|
2017-12-28 23:03:49 +00:00
|
|
|
|
|
|
|
checkAllReleasable(t, c, sb, true)
|
2017-12-13 05:55:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-05 23:32:52 +00:00
|
|
|
func testBuildPushAndValidate(t *testing.T, sb integration.Sandbox) {
|
|
|
|
requiresLinux(t)
|
|
|
|
t.Parallel()
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
busybox := llb.Image("busybox:latest")
|
|
|
|
st := llb.Scratch()
|
|
|
|
|
|
|
|
run := func(cmd string) {
|
|
|
|
st = busybox.Run(llb.Shlex(cmd), llb.Dir("/wd")).AddMount("/wd", st)
|
|
|
|
}
|
|
|
|
|
|
|
|
run(`sh -c "mkdir -p foo/sub; echo -n first > foo/sub/bar; chmod 0741 foo;"`)
|
2017-12-08 22:55:23 +00:00
|
|
|
run(`true`) // this doesn't create a layer
|
2017-12-05 23:32:52 +00:00
|
|
|
run(`sh -c "echo -n second > foo/sub/baz"`)
|
|
|
|
|
|
|
|
def, err := st.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
registry, err := sb.NewRegistry()
|
|
|
|
if errors.Cause(err) == integration.ErrorRequirements {
|
|
|
|
t.Skip(err.Error())
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
target := registry + "/buildkit/testpush:latest"
|
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{
|
|
|
|
Exporter: ExporterImage,
|
|
|
|
ExporterAttrs: map[string]string{
|
|
|
|
"name": target,
|
|
|
|
"push": "true",
|
|
|
|
},
|
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// test existence of the image with next build
|
|
|
|
firstBuild := llb.Image(target)
|
|
|
|
|
|
|
|
def, err = firstBuild.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
destDir, err := ioutil.TempDir("", "buildkit")
|
|
|
|
require.NoError(t, err)
|
2017-12-13 05:55:16 +00:00
|
|
|
defer os.RemoveAll(destDir)
|
2017-12-05 23:32:52 +00:00
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{
|
2018-03-27 04:16:32 +00:00
|
|
|
Exporter: ExporterLocal,
|
|
|
|
ExporterOutputDir: destDir,
|
2017-12-05 23:32:52 +00:00
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(destDir, "foo/sub/bar"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, dt, []byte("first"))
|
|
|
|
|
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(destDir, "foo/sub/baz"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, dt, []byte("second"))
|
|
|
|
|
|
|
|
fi, err := os.Stat(filepath.Join(destDir, "foo"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0741, int(fi.Mode()&0777))
|
|
|
|
|
2017-12-28 23:03:49 +00:00
|
|
|
checkAllReleasable(t, c, sb, false)
|
|
|
|
|
2017-12-05 23:32:52 +00:00
|
|
|
// examine contents of exported tars (requires containerd)
|
|
|
|
var cdAddress string
|
|
|
|
if cd, ok := sb.(interface {
|
|
|
|
ContainerdAddress() string
|
|
|
|
}); !ok {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
cdAddress = cd.ContainerdAddress()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make public pull helper function so this can be checked for standalone as well
|
|
|
|
|
|
|
|
client, err := containerd.New(cdAddress)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
ctx := namespaces.WithNamespace(context.Background(), "buildkit")
|
|
|
|
|
2017-12-28 23:03:49 +00:00
|
|
|
// check image in containerd
|
|
|
|
_, err = client.ImageService().Get(ctx, target)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// deleting image should release all content
|
|
|
|
err = client.ImageService().Delete(ctx, target, images.SynchronousDelete())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkAllReleasable(t, c, sb, true)
|
|
|
|
|
2017-12-05 23:32:52 +00:00
|
|
|
img, err := client.Pull(ctx, target)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
desc, err := img.Config(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dt, err = content.ReadBlob(ctx, img.ContentStore(), desc.Digest)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var ociimg ocispec.Image
|
|
|
|
err = json.Unmarshal(dt, &ociimg)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NotEqual(t, "", ociimg.OS)
|
|
|
|
require.NotEqual(t, "", ociimg.Architecture)
|
|
|
|
require.NotEqual(t, "", ociimg.Config.WorkingDir)
|
|
|
|
require.Equal(t, "layers", ociimg.RootFS.Type)
|
|
|
|
require.Equal(t, 2, len(ociimg.RootFS.DiffIDs))
|
2018-01-05 18:51:32 +00:00
|
|
|
require.NotNil(t, ociimg.Created)
|
|
|
|
require.True(t, time.Since(*ociimg.Created) < 2*time.Minute)
|
2017-12-05 23:32:52 +00:00
|
|
|
require.Condition(t, func() bool {
|
|
|
|
for _, env := range ociimg.Config.Env {
|
|
|
|
if strings.HasPrefix(env, "PATH=") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2017-12-08 22:55:23 +00:00
|
|
|
require.Equal(t, 3, len(ociimg.History))
|
|
|
|
require.Contains(t, ociimg.History[0].CreatedBy, "foo/sub/bar")
|
|
|
|
require.Contains(t, ociimg.History[1].CreatedBy, "true")
|
|
|
|
require.Contains(t, ociimg.History[2].CreatedBy, "foo/sub/baz")
|
|
|
|
require.False(t, ociimg.History[0].EmptyLayer)
|
|
|
|
require.True(t, ociimg.History[1].EmptyLayer)
|
|
|
|
require.False(t, ociimg.History[2].EmptyLayer)
|
|
|
|
|
2017-12-05 23:32:52 +00:00
|
|
|
dt, err = content.ReadBlob(ctx, img.ContentStore(), img.Target().Digest)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var mfst schema2.Manifest
|
|
|
|
err = json.Unmarshal(dt, &mfst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, schema2.MediaTypeManifest, mfst.MediaType)
|
|
|
|
require.Equal(t, 2, len(mfst.Layers))
|
|
|
|
|
|
|
|
dt, err = content.ReadBlob(ctx, img.ContentStore(), mfst.Layers[0].Digest)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-12-13 05:55:16 +00:00
|
|
|
m, err := readTarToMap(dt, true)
|
2017-12-05 23:32:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
item, ok := m["foo/"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, int32(item.header.Typeflag), tar.TypeDir)
|
|
|
|
require.Equal(t, 0741, int(item.header.Mode&0777))
|
|
|
|
|
|
|
|
item, ok = m["foo/sub/"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, int32(item.header.Typeflag), tar.TypeDir)
|
|
|
|
|
|
|
|
item, ok = m["foo/sub/bar"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, int32(item.header.Typeflag), tar.TypeReg)
|
|
|
|
require.Equal(t, []byte("first"), item.data)
|
|
|
|
|
|
|
|
_, ok = m["foo/sub/baz"]
|
|
|
|
require.False(t, ok)
|
|
|
|
|
|
|
|
dt, err = content.ReadBlob(ctx, img.ContentStore(), mfst.Layers[1].Digest)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-12-13 05:55:16 +00:00
|
|
|
m, err = readTarToMap(dt, true)
|
2017-12-05 23:32:52 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
item, ok = m["foo/sub/baz"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, int32(item.header.Typeflag), tar.TypeReg)
|
|
|
|
require.Equal(t, []byte("second"), item.data)
|
|
|
|
|
2017-12-20 21:54:48 +00:00
|
|
|
item, ok = m["foo/"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, int32(item.header.Typeflag), tar.TypeDir)
|
|
|
|
require.Equal(t, 0741, int(item.header.Mode&0777))
|
|
|
|
|
|
|
|
item, ok = m["foo/sub/"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.Equal(t, int32(item.header.Typeflag), tar.TypeDir)
|
|
|
|
|
|
|
|
_, ok = m["foo/sub/bar"]
|
|
|
|
require.False(t, ok)
|
2017-12-05 23:32:52 +00:00
|
|
|
}
|
|
|
|
|
2018-02-13 19:18:18 +00:00
|
|
|
// containerd/containerd#2119
|
|
|
|
func testDuplicateWhiteouts(t *testing.T, sb integration.Sandbox) {
|
|
|
|
requiresLinux(t)
|
|
|
|
t.Parallel()
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
busybox := llb.Image("busybox:latest")
|
|
|
|
st := llb.Scratch()
|
|
|
|
|
|
|
|
run := func(cmd string) {
|
|
|
|
st = busybox.Run(llb.Shlex(cmd), llb.Dir("/wd")).AddMount("/wd", st)
|
|
|
|
}
|
|
|
|
|
|
|
|
run(`sh -c "mkdir -p d0 d1; echo -n first > d1/bar;"`)
|
|
|
|
run(`sh -c "rm -rf d0 d1"`)
|
|
|
|
|
|
|
|
def, err := st.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
destDir, err := ioutil.TempDir("", "buildkit")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(destDir)
|
|
|
|
|
|
|
|
out := filepath.Join(destDir, "out.tar")
|
2018-03-27 04:16:32 +00:00
|
|
|
outW, err := os.Create(out)
|
|
|
|
require.NoError(t, err)
|
2018-02-13 19:18:18 +00:00
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{
|
2018-03-27 04:16:32 +00:00
|
|
|
Exporter: ExporterOCI,
|
|
|
|
ExporterOutput: outW,
|
2018-02-13 19:18:18 +00:00
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(out)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := readTarToMap(dt, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var index ocispec.Index
|
|
|
|
err = json.Unmarshal(m["index.json"].data, &index)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var mfst ocispec.Manifest
|
|
|
|
err = json.Unmarshal(m["blobs/sha256/"+index.Manifests[0].Digest.Hex()].data, &mfst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lastLayer := mfst.Layers[len(mfst.Layers)-1]
|
|
|
|
|
|
|
|
layer, ok := m["blobs/sha256/"+lastLayer.Digest.Hex()]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
m, err = readTarToMap(layer.data, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok = m[".wh.d0"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
_, ok = m[".wh.d1"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// check for a bug that added whiteout for subfile
|
|
|
|
_, ok = m["d1/.wh.bar"]
|
|
|
|
require.True(t, !ok)
|
|
|
|
}
|
|
|
|
|
2018-02-09 19:03:12 +00:00
|
|
|
// #276
|
|
|
|
func testWhiteoutParentDir(t *testing.T, sb integration.Sandbox) {
|
|
|
|
requiresLinux(t)
|
|
|
|
t.Parallel()
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
busybox := llb.Image("busybox:latest")
|
|
|
|
st := llb.Scratch()
|
|
|
|
|
|
|
|
run := func(cmd string) {
|
|
|
|
st = busybox.Run(llb.Shlex(cmd), llb.Dir("/wd")).AddMount("/wd", st)
|
|
|
|
}
|
|
|
|
|
|
|
|
run(`sh -c "mkdir -p foo; echo -n first > foo/bar;"`)
|
|
|
|
run(`rm foo/bar`)
|
|
|
|
|
|
|
|
def, err := st.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
destDir, err := ioutil.TempDir("", "buildkit")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(destDir)
|
|
|
|
|
|
|
|
out := filepath.Join(destDir, "out.tar")
|
2018-03-27 04:16:32 +00:00
|
|
|
outW, err := os.Create(out)
|
|
|
|
require.NoError(t, err)
|
2018-02-09 19:03:12 +00:00
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{
|
2018-03-27 04:16:32 +00:00
|
|
|
Exporter: ExporterOCI,
|
|
|
|
ExporterOutput: outW,
|
2018-02-09 19:03:12 +00:00
|
|
|
}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(out)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := readTarToMap(dt, false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var index ocispec.Index
|
|
|
|
err = json.Unmarshal(m["index.json"].data, &index)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var mfst ocispec.Manifest
|
|
|
|
err = json.Unmarshal(m["blobs/sha256/"+index.Manifests[0].Digest.Hex()].data, &mfst)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
lastLayer := mfst.Layers[len(mfst.Layers)-1]
|
|
|
|
|
|
|
|
layer, ok := m["blobs/sha256/"+lastLayer.Digest.Hex()]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
m, err = readTarToMap(layer.data, true)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok = m["foo/.wh.bar"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
_, ok = m["foo/"]
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
|
2018-03-02 16:33:08 +00:00
|
|
|
// #296
|
|
|
|
func testSchema1Image(t *testing.T, sb integration.Sandbox) {
|
|
|
|
t.Parallel()
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
st := llb.Image("gcr.io/google_containers/pause:3.0@sha256:0d093c962a6c2dd8bb8727b661e2b5f13e9df884af9945b4cc7088d9350cd3ee")
|
|
|
|
|
|
|
|
def, err := st.Marshal()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = c.Solve(context.TODO(), def, SolveOpt{}, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
checkAllReleasable(t, c, sb, true)
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:04:43 +00:00
|
|
|
// #319
|
|
|
|
func testMountWithNoSource(t *testing.T, sb integration.Sandbox) {
|
|
|
|
t.Parallel()
|
|
|
|
c, err := New(sb.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer c.Close()
|
|
|
|
|
|
|
|
busybox := llb.Image("docker.io/library/busybox:latest")
|
|
|
|
st := llb.Scratch()
|
|
|
|
|
|
|
|
var nilState llb.State
|
|
|
|
|
|
|
|
// This should never actually be run, but we want to succeed
|
|
|
|
// if it was, because we expect an error below, or a daemon
|
|
|
|
// panic if the issue has regressed.
|
|
|
|
run := busybox.Run(
|
|
|
|
llb.Args([]string{"/bin/true"}),
|
|
|
|
llb.AddMount("/nil", nilState, llb.SourcePath("/"), llb.Readonly))
|
|
|
|
|
|
|
|
st = run.AddMount("/mnt", st)
|
|
|
|
|
|
|
|
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(), "has no input")
|
|
|
|
|
|
|
|
checkAllReleasable(t, c, sb, true)
|
|
|
|
}
|
|
|
|
|
2017-11-27 22:24:29 +00:00
|
|
|
func requiresLinux(t *testing.T) {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
t.Skipf("unsupported GOOS: %s", runtime.GOOS)
|
|
|
|
}
|
|
|
|
}
|
2017-12-05 23:32:52 +00:00
|
|
|
|
|
|
|
type tarItem struct {
|
|
|
|
header *tar.Header
|
|
|
|
data []byte
|
|
|
|
}
|
|
|
|
|
2017-12-13 05:55:16 +00:00
|
|
|
func readTarToMap(dt []byte, compressed bool) (map[string]*tarItem, error) {
|
2017-12-05 23:32:52 +00:00
|
|
|
m := map[string]*tarItem{}
|
2017-12-13 05:55:16 +00:00
|
|
|
var r io.Reader = bytes.NewBuffer(dt)
|
|
|
|
if compressed {
|
|
|
|
gz, err := gzip.NewReader(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer gz.Close()
|
|
|
|
r = gz
|
2017-12-05 23:32:52 +00:00
|
|
|
}
|
2017-12-13 05:55:16 +00:00
|
|
|
tr := tar.NewReader(r)
|
2017-12-05 23:32:52 +00:00
|
|
|
for {
|
2017-12-13 05:55:16 +00:00
|
|
|
h, err := tr.Next()
|
2017-12-05 23:32:52 +00:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-09 19:47:07 +00:00
|
|
|
if _, ok := m[h.Name]; ok {
|
|
|
|
return nil, errors.Errorf("duplicate entries for %s", h.Name)
|
|
|
|
}
|
|
|
|
|
2017-12-05 23:32:52 +00:00
|
|
|
var dt []byte
|
|
|
|
if h.Typeflag == tar.TypeReg {
|
2017-12-13 05:55:16 +00:00
|
|
|
dt, err = ioutil.ReadAll(tr)
|
2017-12-05 23:32:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m[h.Name] = &tarItem{header: h, data: dt}
|
|
|
|
}
|
|
|
|
}
|
2017-12-28 23:03:49 +00:00
|
|
|
|
|
|
|
func checkAllReleasable(t *testing.T, c *Client, sb integration.Sandbox, checkContent bool) {
|
2018-01-07 21:34:10 +00:00
|
|
|
retries := 0
|
|
|
|
loop0:
|
|
|
|
for {
|
|
|
|
require.True(t, 20 > retries)
|
|
|
|
retries++
|
|
|
|
du, err := c.DiskUsage(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
for _, d := range du {
|
|
|
|
if d.InUse {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
continue loop0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2017-12-28 23:03:49 +00:00
|
|
|
err := c.Prune(context.TODO(), nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
du, err := c.DiskUsage(context.TODO())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 0, len(du))
|
|
|
|
|
|
|
|
// examine contents of exported tars (requires containerd)
|
|
|
|
var cdAddress string
|
|
|
|
if cd, ok := sb.(interface {
|
|
|
|
ContainerdAddress() string
|
|
|
|
}); !ok {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
cdAddress = cd.ContainerdAddress()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make public pull helper function so this can be checked for standalone as well
|
|
|
|
|
|
|
|
client, err := containerd.New(cdAddress)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer client.Close()
|
|
|
|
|
|
|
|
ctx := namespaces.WithNamespace(context.Background(), "buildkit")
|
|
|
|
snapshotService := client.SnapshotService("overlayfs")
|
|
|
|
|
2018-01-07 21:34:10 +00:00
|
|
|
retries = 0
|
2017-12-28 23:03:49 +00:00
|
|
|
for {
|
|
|
|
count := 0
|
|
|
|
err = snapshotService.Walk(ctx, func(context.Context, snapshots.Info) error {
|
|
|
|
count++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
require.True(t, 20 > retries)
|
|
|
|
retries++
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkContent {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
retries = 0
|
|
|
|
for {
|
|
|
|
count := 0
|
|
|
|
err = client.ContentStore().Walk(ctx, func(content.Info) error {
|
|
|
|
count++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
require.True(t, 20 > retries)
|
|
|
|
retries++
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|