diff --git a/solver/boltdbcachestorage/storage_test.go b/solver/boltdbcachestorage/storage_test.go index a76c7726..69a34999 100644 --- a/solver/boltdbcachestorage/storage_test.go +++ b/solver/boltdbcachestorage/storage_test.go @@ -7,11 +7,12 @@ import ( "testing" "github.com/moby/buildkit/solver" + "github.com/moby/buildkit/solver/testutil" "github.com/stretchr/testify/require" ) func TestBoltCacheStorage(t *testing.T) { - solver.RunCacheStorageTests(t, func() (solver.CacheKeyStorage, func()) { + testutil.RunCacheStorageTests(t, func() (solver.CacheKeyStorage, func()) { tmpDir, err := ioutil.TempDir("", "storage") require.NoError(t, err) diff --git a/solver/memorycachestorage_test.go b/solver/memorycachestorage_test.go deleted file mode 100644 index 69a112ad..00000000 --- a/solver/memorycachestorage_test.go +++ /dev/null @@ -1,9 +0,0 @@ -package solver - -import "testing" - -func TestMemoryCacheStorage(t *testing.T) { - RunCacheStorageTests(t, func() (CacheKeyStorage, func()) { - return NewInMemoryCacheStorage(), func() {} - }) -} diff --git a/solver/cachestorage_testsuite.go b/solver/testutil/cachestorage_testsuite.go similarity index 71% rename from solver/cachestorage_testsuite.go rename to solver/testutil/cachestorage_testsuite.go index 1292c553..7bc16aae 100644 --- a/solver/cachestorage_testsuite.go +++ b/solver/testutil/cachestorage_testsuite.go @@ -1,19 +1,21 @@ -package solver +package testutil import ( + "fmt" "reflect" "runtime" "strings" "testing" "time" + "github.com/moby/buildkit/solver" digest "github.com/opencontainers/go-digest" "github.com/pkg/errors" "github.com/stretchr/testify/require" ) -func RunCacheStorageTests(t *testing.T, st func() (CacheKeyStorage, func())) { - for _, tc := range []func(*testing.T, CacheKeyStorage){ +func RunCacheStorageTests(t *testing.T, st func() (solver.CacheKeyStorage, func())) { + for _, tc := range []func(*testing.T, solver.CacheKeyStorage){ testResults, testLinks, testResultReleaseSingleLevel, @@ -25,7 +27,7 @@ func RunCacheStorageTests(t *testing.T, st func() (CacheKeyStorage, func())) { } } -func runStorageTest(t *testing.T, fn func(t *testing.T, st CacheKeyStorage), st func() (CacheKeyStorage, func())) { +func runStorageTest(t *testing.T, fn func(t *testing.T, st solver.CacheKeyStorage), st func() (solver.CacheKeyStorage, func())) { require.True(t, t.Run(getFunctionName(fn), func(t *testing.T) { s, cleanup := st() defer cleanup() @@ -33,28 +35,28 @@ func runStorageTest(t *testing.T, fn func(t *testing.T, st CacheKeyStorage), st })) } -func testResults(t *testing.T, st CacheKeyStorage) { +func testResults(t *testing.T, st solver.CacheKeyStorage) { t.Parallel() - err := st.AddResult("foo", CacheResult{ + err := st.AddResult("foo", solver.CacheResult{ ID: "foo0", CreatedAt: time.Now(), }) require.NoError(t, err) - err = st.AddResult("foo", CacheResult{ + err = st.AddResult("foo", solver.CacheResult{ ID: "foo1", CreatedAt: time.Now(), }) require.NoError(t, err) - err = st.AddResult("bar", CacheResult{ + err = st.AddResult("bar", solver.CacheResult{ ID: "bar0", CreatedAt: time.Now(), }) require.NoError(t, err) - m := map[string]CacheResult{} - err = st.WalkResults("foo", func(r CacheResult) error { + m := map[string]solver.CacheResult{} + err = st.WalkResults("foo", func(r solver.CacheResult) error { m[r.ID] = r return nil }) @@ -67,8 +69,8 @@ func testResults(t *testing.T, st CacheKeyStorage) { require.True(t, ok) require.True(t, f0.CreatedAt.Before(f1.CreatedAt)) - m = map[string]CacheResult{} - err = st.WalkResults("bar", func(r CacheResult) error { + m = map[string]solver.CacheResult{} + err = st.WalkResults("bar", func(r solver.CacheResult) error { m[r.ID] = r return nil }) @@ -79,7 +81,7 @@ func testResults(t *testing.T, st CacheKeyStorage) { require.True(t, ok) // empty result - err = st.WalkResults("baz", func(r CacheResult) error { + err = st.WalkResults("baz", func(r solver.CacheResult) error { require.Fail(t, "unreachable") return nil }) @@ -92,17 +94,17 @@ func testResults(t *testing.T, st CacheKeyStorage) { _, err = st.Load("foo1", "foo1") require.Error(t, err) - require.Equal(t, errors.Cause(err), ErrNotFound) + require.Equal(t, errors.Cause(err), solver.ErrNotFound) _, err = st.Load("foo", "foo2") require.Error(t, err) - require.Equal(t, errors.Cause(err), ErrNotFound) + require.Equal(t, errors.Cause(err), solver.ErrNotFound) } -func testLinks(t *testing.T, st CacheKeyStorage) { +func testLinks(t *testing.T, st solver.CacheKeyStorage) { t.Parallel() - l0 := CacheInfoLink{ + l0 := solver.CacheInfoLink{ Input: 0, Output: 1, Digest: digest.FromBytes([]byte(">target0")), } err := st.AddLink("foo", l0, "target0") @@ -122,7 +124,7 @@ func testLinks(t *testing.T, st CacheKeyStorage) { _, ok := m["target0"] require.True(t, ok) - l1 := CacheInfoLink{ + l1 := solver.CacheInfoLink{ Input: 0, Output: 1, Digest: digest.FromBytes([]byte(">target1")), } m = map[string]struct{}{} @@ -163,16 +165,16 @@ func testLinks(t *testing.T, st CacheKeyStorage) { require.True(t, ok) } -func testResultReleaseSingleLevel(t *testing.T, st CacheKeyStorage) { +func testResultReleaseSingleLevel(t *testing.T, st solver.CacheKeyStorage) { t.Parallel() - err := st.AddResult("foo", CacheResult{ + err := st.AddResult("foo", solver.CacheResult{ ID: "foo0", CreatedAt: time.Now(), }) require.NoError(t, err) - err = st.AddResult("foo", CacheResult{ + err = st.AddResult("foo", solver.CacheResult{ ID: "foo1", CreatedAt: time.Now(), }) @@ -182,7 +184,7 @@ func testResultReleaseSingleLevel(t *testing.T, st CacheKeyStorage) { require.NoError(t, err) m := map[string]struct{}{} - st.WalkResults("foo", func(res CacheResult) error { + st.WalkResults("foo", func(res solver.CacheResult) error { m[res.ID] = struct{}{} return nil }) @@ -195,7 +197,7 @@ func testResultReleaseSingleLevel(t *testing.T, st CacheKeyStorage) { require.NoError(t, err) m = map[string]struct{}{} - st.WalkResults("foo", func(res CacheResult) error { + st.WalkResults("foo", func(res solver.CacheResult) error { m[res.ID] = struct{}{} return nil }) @@ -203,31 +205,31 @@ func testResultReleaseSingleLevel(t *testing.T, st CacheKeyStorage) { require.Equal(t, len(m), 0) } -func testBacklinks(t *testing.T, st CacheKeyStorage) { +func testBacklinks(t *testing.T, st solver.CacheKeyStorage) { t.Parallel() - err := st.AddResult("foo", CacheResult{ + err := st.AddResult("foo", solver.CacheResult{ ID: "foo-result", CreatedAt: time.Now(), }) require.NoError(t, err) - err = st.AddResult("sub0", CacheResult{ + err = st.AddResult("sub0", solver.CacheResult{ ID: "sub0-result", CreatedAt: time.Now(), }) require.NoError(t, err) - l0 := CacheInfoLink{ + l0 := solver.CacheInfoLink{ Input: 0, Output: 1, Digest: digest.FromBytes([]byte("to-sub0")), } err = st.AddLink("foo", l0, "sub0") require.NoError(t, err) backlinks := 0 - st.WalkBacklinks("sub0", func(id string, link CacheInfoLink) error { + st.WalkBacklinks("sub0", func(id string, link solver.CacheInfoLink) error { require.Equal(t, id, "foo") - require.Equal(t, link.Input, Index(0)) + require.Equal(t, link.Input, solver.Index(0)) require.Equal(t, link.Digest, rootKey(digest.FromBytes([]byte("to-sub0")), 1)) backlinks++ return nil @@ -235,28 +237,28 @@ func testBacklinks(t *testing.T, st CacheKeyStorage) { require.Equal(t, backlinks, 1) } -func testResultReleaseMultiLevel(t *testing.T, st CacheKeyStorage) { +func testResultReleaseMultiLevel(t *testing.T, st solver.CacheKeyStorage) { t.Parallel() - err := st.AddResult("foo", CacheResult{ + err := st.AddResult("foo", solver.CacheResult{ ID: "foo-result", CreatedAt: time.Now(), }) require.NoError(t, err) - err = st.AddResult("sub0", CacheResult{ + err = st.AddResult("sub0", solver.CacheResult{ ID: "sub0-result", CreatedAt: time.Now(), }) require.NoError(t, err) - l0 := CacheInfoLink{ + l0 := solver.CacheInfoLink{ Input: 0, Output: 1, Digest: digest.FromBytes([]byte("to-sub0")), } err = st.AddLink("foo", l0, "sub0") require.NoError(t, err) - err = st.AddResult("sub1", CacheResult{ + err = st.AddResult("sub1", solver.CacheResult{ ID: "sub1-result", CreatedAt: time.Now(), }) @@ -271,7 +273,7 @@ func testResultReleaseMultiLevel(t *testing.T, st CacheKeyStorage) { require.NoError(t, err) m := map[string]struct{}{} - err = st.WalkResults("foo", func(res CacheResult) error { + err = st.WalkResults("foo", func(res solver.CacheResult) error { m[res.ID] = struct{}{} return nil }) @@ -302,7 +304,7 @@ func testResultReleaseMultiLevel(t *testing.T, st CacheKeyStorage) { require.True(t, st.Exists("foo")) m = map[string]struct{}{} - err = st.WalkResults("foo", func(res CacheResult) error { + err = st.WalkResults("foo", func(res solver.CacheResult) error { m[res.ID] = struct{}{} return nil }) @@ -327,22 +329,22 @@ func testResultReleaseMultiLevel(t *testing.T, st CacheKeyStorage) { require.False(t, st.Exists("foo")) } -func testWalkIDsByResult(t *testing.T, st CacheKeyStorage) { +func testWalkIDsByResult(t *testing.T, st solver.CacheKeyStorage) { t.Parallel() - err := st.AddResult("foo", CacheResult{ + err := st.AddResult("foo", solver.CacheResult{ ID: "foo-result", CreatedAt: time.Now(), }) require.NoError(t, err) - err = st.AddResult("foo2", CacheResult{ + err = st.AddResult("foo2", solver.CacheResult{ ID: "foo-result", CreatedAt: time.Now(), }) require.NoError(t, err) - err = st.AddResult("bar", CacheResult{ + err = st.AddResult("bar", solver.CacheResult{ ID: "bar-result", CreatedAt: time.Now(), }) @@ -370,3 +372,7 @@ func getFunctionName(i interface{}) string { dot := strings.LastIndex(fullname, ".") + 1 return strings.Title(fullname[dot:]) } + +func rootKey(dgst digest.Digest, output solver.Index) digest.Digest { + return digest.FromBytes([]byte(fmt.Sprintf("%s@%d", dgst, output))) +} diff --git a/solver/testutil/memorycachestorage_test.go b/solver/testutil/memorycachestorage_test.go new file mode 100644 index 00000000..ea1f6be4 --- /dev/null +++ b/solver/testutil/memorycachestorage_test.go @@ -0,0 +1,13 @@ +package testutil + +import ( + "testing" + + "github.com/moby/buildkit/solver" +) + +func TestMemoryCacheStorage(t *testing.T) { + RunCacheStorageTests(t, func() (solver.CacheKeyStorage, func()) { + return solver.NewInMemoryCacheStorage(), func() {} + }) +}