2017-05-27 06:12:13 +00:00
|
|
|
package cache
|
2017-05-26 00:16:32 +00:00
|
|
|
|
|
|
|
import (
|
2017-05-26 18:56:12 +00:00
|
|
|
"context"
|
2017-05-26 00:16:32 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-05-26 17:20:41 +00:00
|
|
|
"path/filepath"
|
2017-05-26 00:16:32 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
"github.com/containerd/containerd/namespaces"
|
2017-05-26 17:20:41 +00:00
|
|
|
"github.com/containerd/containerd/snapshot/naive"
|
2017-07-05 02:00:27 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2017-05-26 17:59:33 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-05-26 00:16:32 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
func TestManager(t *testing.T) {
|
2017-06-13 23:01:47 +00:00
|
|
|
ctx := namespaces.WithNamespace(context.Background(), "buildkit-test")
|
|
|
|
|
2017-05-26 00:16:32 +00:00
|
|
|
tmpdir, err := ioutil.TempDir("", "cachemanager")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2017-05-26 17:20:41 +00:00
|
|
|
snapshotter, err := naive.NewSnapshotter(filepath.Join(tmpdir, "snapshots"))
|
|
|
|
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-27 06:12:13 +00:00
|
|
|
cm, err := NewManager(ManagerOpt{
|
2017-07-05 02:00:27 +00:00
|
|
|
Snapshotter: snapshotter,
|
|
|
|
MetadataStore: md,
|
2017-05-26 17:20:41 +00:00
|
|
|
})
|
2017-05-26 00:16:32 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
_, err = cm.Get(ctx, "foobar")
|
2017-05-26 00:34:59 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 0, 0)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
active, err := cm.New(ctx, nil)
|
2017-05-26 17:20:41 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
m, err := active.Mount(ctx)
|
2017-05-26 17:20:41 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
lm := snapshot.LocalMounter(m)
|
|
|
|
target, err := lm.Mount()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
fi, err := os.Stat(target)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, fi.IsDir(), true)
|
|
|
|
|
|
|
|
err = lm.Unmount()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
_, err = cm.GetMutable(ctx, active.ID())
|
2017-05-26 17:59:33 +00:00
|
|
|
assert.Error(t, err)
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.Equal(t, errLocked, errors.Cause(err))
|
2017-05-26 17:59:33 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 1, 0)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
snap, err := active.Freeze()
|
2017-05-26 17:59:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 1, 0)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
_, err = cm.GetMutable(ctx, active.ID())
|
2017-05-26 17:59:33 +00:00
|
|
|
assert.Error(t, err)
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.Equal(t, errLocked, errors.Cause(err))
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
err = snap.Release(ctx)
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 0, 1)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
active, err = cm.GetMutable(ctx, active.ID())
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 1, 0)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
snap, err = active.ReleaseAndCommit(ctx)
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.NoError(t, err)
|
2017-05-26 17:59:33 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 1, 0)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
err = snap.Release(ctx)
|
2017-05-26 17:59:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
_, err = cm.GetMutable(ctx, active.ID())
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, errNotFound, errors.Cause(err))
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
_, err = cm.GetMutable(ctx, snap.ID())
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, errInvalid, errors.Cause(err))
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
snap, err = cm.Get(ctx, snap.ID())
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
snap2, err := cm.Get(ctx, snap.ID())
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 1, 0)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
err = snap.Release(ctx)
|
2017-05-26 18:56:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
active2, err := cm.New(ctx, snap2)
|
2017-05-26 20:45:01 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 2, 0)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
snap3, err := active2.Freeze()
|
2017-05-26 20:45:01 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
err = snap2.Release(ctx)
|
2017-05-26 20:45:01 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 2, 0)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
err = snap3.Release(ctx)
|
2017-05-26 17:59:33 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
checkDiskUsage(t, ctx, cm, 0, 2)
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-05-26 00:16:32 +00:00
|
|
|
err = cm.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2017-05-26 21:17:38 +00:00
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
func checkDiskUsage(t *testing.T, ctx context.Context, cm Manager, inuse, unused int) {
|
|
|
|
du, err := cm.DiskUsage(ctx)
|
2017-05-26 21:17:38 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
var inuseActual, unusedActual int
|
|
|
|
for _, r := range du {
|
|
|
|
if r.InUse {
|
|
|
|
inuseActual++
|
|
|
|
} else {
|
|
|
|
unusedActual++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, inuse, inuseActual)
|
|
|
|
assert.Equal(t, unused, unusedActual)
|
|
|
|
}
|