2017-06-19 23:10:09 +00:00
|
|
|
// +build linux,standalone
|
2017-05-27 05:10:26 +00:00
|
|
|
|
2017-05-26 22:15:20 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
2017-06-02 00:30:02 +00:00
|
|
|
"bytes"
|
2017-07-11 06:55:04 +00:00
|
|
|
"io"
|
2017-05-26 22:15:20 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
"github.com/containerd/containerd/namespaces"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
2017-07-05 02:00:27 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
2017-07-25 22:14:46 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
"github.com/moby/buildkit/snapshot/blobmapping"
|
|
|
|
"github.com/moby/buildkit/source"
|
|
|
|
"github.com/moby/buildkit/source/containerimage"
|
|
|
|
"github.com/moby/buildkit/worker"
|
|
|
|
"github.com/moby/buildkit/worker/runcworker"
|
2017-05-26 22:15:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2017-06-13 23:01:47 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-05-26 22:15:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestControl(t *testing.T) {
|
2017-06-13 23:01:47 +00:00
|
|
|
ctx := namespaces.WithNamespace(context.Background(), "buildkit-test")
|
|
|
|
|
2017-05-30 21:17:04 +00:00
|
|
|
// this should be an example or e2e test
|
2017-05-27 05:10:26 +00:00
|
|
|
tmpdir, err := ioutil.TempDir("", "controltest")
|
|
|
|
assert.NoError(t, err)
|
2017-05-30 21:17:04 +00:00
|
|
|
defer os.RemoveAll(tmpdir)
|
2017-05-27 05:10:26 +00:00
|
|
|
|
2017-06-19 20:39:00 +00:00
|
|
|
cd, err := newStandalonePullDeps(tmpdir)
|
2017-05-27 05:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-07-05 02:00:27 +00:00
|
|
|
md, err := metadata.NewStore(filepath.Join(tmpdir, "metadata.db"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-05-31 22:05:22 +00:00
|
|
|
snapshotter, err := blobmapping.NewSnapshotter(blobmapping.Opt{
|
2017-07-05 02:00:27 +00:00
|
|
|
Content: cd.ContentStore,
|
|
|
|
Snapshotter: cd.Snapshotter,
|
|
|
|
MetadataStore: md,
|
2017-05-31 22:05:22 +00:00
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
cm, err := cache.NewManager(cache.ManagerOpt{
|
2017-07-05 02:00:27 +00:00
|
|
|
Snapshotter: snapshotter,
|
|
|
|
MetadataStore: md,
|
2017-05-27 05:10:26 +00:00
|
|
|
})
|
2017-06-08 00:53:36 +00:00
|
|
|
assert.NoError(t, err)
|
2017-05-27 05:10:26 +00:00
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
sm, err := source.NewManager()
|
2017-05-26 22:15:20 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
is, err := containerimage.NewSource(containerimage.SourceOpt{
|
2017-05-31 22:05:22 +00:00
|
|
|
Snapshotter: snapshotter,
|
2017-05-30 21:17:04 +00:00
|
|
|
ContentStore: cd.ContentStore,
|
|
|
|
Applier: cd.Applier,
|
|
|
|
CacheAccessor: cm,
|
2017-05-27 05:10:26 +00:00
|
|
|
})
|
2017-05-26 22:15:20 +00:00
|
|
|
assert.NoError(t, err)
|
2017-05-27 05:10:26 +00:00
|
|
|
|
|
|
|
sm.Register(is)
|
|
|
|
|
2017-06-01 22:24:23 +00:00
|
|
|
img, err := source.NewImageIdentifier("docker.io/library/busybox:latest")
|
2017-05-27 05:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-07-06 20:15:54 +00:00
|
|
|
src, err := sm.Resolve(ctx, img)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
snap, err := src.Snapshot(ctx)
|
2017-05-27 05:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-07-18 06:08:22 +00:00
|
|
|
mounts, err := snap.Mount(ctx, false)
|
2017-05-30 21:17:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(mounts)
|
|
|
|
|
|
|
|
target, err := lm.Mount()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
f, err := os.Open(target)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
names, err := f.Readdirnames(-1)
|
|
|
|
assert.NoError(t, err)
|
2017-06-01 22:24:23 +00:00
|
|
|
assert.True(t, len(names) > 5)
|
2017-05-30 21:17:04 +00:00
|
|
|
|
|
|
|
err = f.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
lm.Unmount()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-07-25 22:14:46 +00:00
|
|
|
du, err := cm.DiskUsage(ctx, client.DiskUsageInfo{})
|
2017-05-31 23:45:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// for _, d := range du {
|
2017-05-31 23:52:53 +00:00
|
|
|
// fmt.Printf("du: %+v\n", d)
|
2017-05-31 23:45:04 +00:00
|
|
|
// }
|
|
|
|
|
2017-06-01 22:24:23 +00:00
|
|
|
for _, d := range du {
|
|
|
|
assert.True(t, d.Size >= 8192)
|
|
|
|
}
|
|
|
|
|
|
|
|
w, err := runcworker.New(tmpdir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
meta := worker.Meta{
|
|
|
|
Args: []string{"/bin/sh", "-c", "echo \"foo\" > /bar"},
|
2017-06-02 00:30:02 +00:00
|
|
|
Cwd: "/",
|
2017-06-01 22:24:23 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 00:30:02 +00:00
|
|
|
stderr := bytes.NewBuffer(nil)
|
|
|
|
|
2017-10-01 00:58:07 +00:00
|
|
|
err = w.Exec(ctx, meta, snap, nil, nil, nil, &nopCloser{stderr})
|
2017-06-01 22:24:23 +00:00
|
|
|
assert.Error(t, err) // Read-only root
|
2017-09-09 09:49:48 +00:00
|
|
|
// typical error is like `mkdir /.../rootfs/proc: read-only file system`.
|
|
|
|
// make sure the error is caused before running `echo foo > /bar`.
|
|
|
|
assert.Contains(t, stderr.String(), "read-only file system")
|
2017-06-01 22:24:23 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
root, err := cm.New(ctx, snap)
|
2017-06-01 22:24:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-10-01 00:58:07 +00:00
|
|
|
err = w.Exec(ctx, meta, root, nil, nil, nil, nil)
|
2017-06-01 22:24:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
rf, err := root.Commit(ctx)
|
2017-06-01 22:24:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-07-18 06:08:22 +00:00
|
|
|
mounts, err = rf.Mount(ctx, false)
|
2017-06-01 22:24:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
lm = snapshot.LocalMounter(mounts)
|
|
|
|
|
|
|
|
target, err = lm.Mount()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(target, "bar"))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, string(dt), "foo\n")
|
|
|
|
|
|
|
|
lm.Unmount()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
err = rf.Release(ctx)
|
2017-06-01 22:24:23 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
err = snap.Release(ctx)
|
2017-05-30 21:17:04 +00:00
|
|
|
assert.NoError(t, err)
|
2017-05-31 23:45:04 +00:00
|
|
|
|
2017-07-25 22:14:46 +00:00
|
|
|
du2, err := cm.DiskUsage(ctx, client.DiskUsageInfo{})
|
2017-05-31 23:45:04 +00:00
|
|
|
assert.NoError(t, err)
|
2017-06-01 22:24:23 +00:00
|
|
|
assert.Equal(t, 1, len(du2)-len(du))
|
2017-05-31 23:45:04 +00:00
|
|
|
|
2017-05-26 22:15:20 +00:00
|
|
|
}
|
2017-07-11 06:55:04 +00:00
|
|
|
|
|
|
|
type nopCloser struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nopCloser) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|