2017-05-15 17:54:27 -04:00
|
|
|
package fscache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-07-28 19:04:34 -04:00
|
|
|
"github.com/moby/buildkit/session/filesync"
|
2017-05-15 17:54:27 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFSCache(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", "fscache")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
backend := NewNaiveCacheBackend(filepath.Join(tmpDir, "backend"))
|
|
|
|
|
|
|
|
opt := Opt{
|
|
|
|
Root: tmpDir,
|
|
|
|
Backend: backend,
|
|
|
|
GCPolicy: GCPolicy{MaxSize: 15, MaxKeepDuration: time.Hour},
|
|
|
|
}
|
|
|
|
|
|
|
|
fscache, err := NewFSCache(opt)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
defer fscache.Close()
|
|
|
|
|
|
|
|
err = fscache.RegisterTransport("test", &testTransport{})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
src1, err := fscache.SyncFrom(context.TODO(), &testIdentifier{"foo", "data", "bar"})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(src1.Root().Path(), "foo"))
|
2017-05-15 17:54:27 -04:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, string(dt), "data")
|
|
|
|
|
|
|
|
// same id doesn't recalculate anything
|
|
|
|
src2, err := fscache.SyncFrom(context.TODO(), &testIdentifier{"foo", "data2", "bar"})
|
|
|
|
assert.Nil(t, err)
|
2017-08-03 20:22:00 -04:00
|
|
|
assert.Equal(t, src1.Root().Path(), src2.Root().Path())
|
2017-05-15 17:54:27 -04:00
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(src1.Root().Path(), "foo"))
|
2017-05-15 17:54:27 -04:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, string(dt), "data")
|
|
|
|
assert.Nil(t, src2.Close())
|
|
|
|
|
|
|
|
src3, err := fscache.SyncFrom(context.TODO(), &testIdentifier{"foo2", "data2", "bar"})
|
|
|
|
assert.Nil(t, err)
|
2017-08-03 20:22:00 -04:00
|
|
|
assert.NotEqual(t, src1.Root().Path(), src3.Root().Path())
|
2017-05-15 17:54:27 -04:00
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(src3.Root().Path(), "foo2"))
|
2017-05-15 17:54:27 -04:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, string(dt), "data2")
|
|
|
|
|
|
|
|
s, err := fscache.DiskUsage()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, s, int64(0))
|
|
|
|
|
|
|
|
assert.Nil(t, src3.Close())
|
|
|
|
|
|
|
|
s, err = fscache.DiskUsage()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, s, int64(5))
|
|
|
|
|
|
|
|
// new upload with the same shared key shoutl overwrite
|
|
|
|
src4, err := fscache.SyncFrom(context.TODO(), &testIdentifier{"foo3", "data3", "bar"})
|
|
|
|
assert.Nil(t, err)
|
2017-08-03 20:22:00 -04:00
|
|
|
assert.NotEqual(t, src1.Root().Path(), src3.Root().Path())
|
2017-05-15 17:54:27 -04:00
|
|
|
|
2017-08-03 20:22:00 -04:00
|
|
|
dt, err = ioutil.ReadFile(filepath.Join(src3.Root().Path(), "foo3"))
|
2017-05-15 17:54:27 -04:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, string(dt), "data3")
|
2017-08-03 20:22:00 -04:00
|
|
|
assert.Equal(t, src4.Root().Path(), src3.Root().Path())
|
2017-05-15 17:54:27 -04:00
|
|
|
assert.Nil(t, src4.Close())
|
|
|
|
|
|
|
|
s, err = fscache.DiskUsage()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, s, int64(10))
|
|
|
|
|
|
|
|
// this one goes over the GC limit
|
|
|
|
src5, err := fscache.SyncFrom(context.TODO(), &testIdentifier{"foo4", "datadata", "baz"})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Nil(t, src5.Close())
|
|
|
|
|
|
|
|
// GC happens async
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
|
|
|
|
// only last insertion after GC
|
|
|
|
s, err = fscache.DiskUsage()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, s, int64(8))
|
|
|
|
|
|
|
|
// prune deletes everything
|
2017-07-06 04:47:21 -04:00
|
|
|
released, err := fscache.Prune(context.TODO())
|
2017-05-15 17:54:27 -04:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, released, uint64(8))
|
|
|
|
|
|
|
|
s, err = fscache.DiskUsage()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, s, int64(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
type testTransport struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testTransport) Copy(ctx context.Context, id RemoteIdentifier, dest string, cs filesync.CacheUpdater) error {
|
|
|
|
testid := id.(*testIdentifier)
|
|
|
|
return ioutil.WriteFile(filepath.Join(dest, testid.filename), []byte(testid.data), 0600)
|
|
|
|
}
|
|
|
|
|
|
|
|
type testIdentifier struct {
|
|
|
|
filename string
|
|
|
|
data string
|
|
|
|
sharedKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testIdentifier) Key() string {
|
|
|
|
return t.filename
|
|
|
|
}
|
|
|
|
func (t *testIdentifier) SharedKey() string {
|
|
|
|
return t.sharedKey
|
|
|
|
}
|
|
|
|
func (t *testIdentifier) Transport() string {
|
|
|
|
return "test"
|
|
|
|
}
|