2018-05-12 01:32:56 +00:00
|
|
|
package testutil
|
2018-02-26 18:51:38 +00:00
|
|
|
|
|
|
|
import (
|
2018-05-12 01:32:56 +00:00
|
|
|
"fmt"
|
2018-02-26 18:51:38 +00:00
|
|
|
"reflect"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
2018-02-26 18:51:38 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
func RunCacheStorageTests(t *testing.T, st func() (solver.CacheKeyStorage, func())) {
|
|
|
|
for _, tc := range []func(*testing.T, solver.CacheKeyStorage){
|
2018-02-26 18:51:38 +00:00
|
|
|
testResults,
|
|
|
|
testLinks,
|
2018-02-27 01:39:18 +00:00
|
|
|
testResultReleaseSingleLevel,
|
|
|
|
testResultReleaseMultiLevel,
|
2018-04-13 20:52:27 +00:00
|
|
|
testBacklinks,
|
2018-04-24 19:01:50 +00:00
|
|
|
testWalkIDsByResult,
|
2018-02-26 18:51:38 +00:00
|
|
|
} {
|
|
|
|
runStorageTest(t, tc, st)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
func runStorageTest(t *testing.T, fn func(t *testing.T, st solver.CacheKeyStorage), st func() (solver.CacheKeyStorage, func())) {
|
2018-02-26 18:51:38 +00:00
|
|
|
require.True(t, t.Run(getFunctionName(fn), func(t *testing.T) {
|
|
|
|
s, cleanup := st()
|
|
|
|
defer cleanup()
|
|
|
|
fn(t, s)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
func testResults(t *testing.T, st solver.CacheKeyStorage) {
|
2018-02-26 18:51:38 +00:00
|
|
|
t.Parallel()
|
2018-05-12 01:32:56 +00:00
|
|
|
err := st.AddResult("foo", solver.CacheResult{
|
2018-02-26 18:51:38 +00:00
|
|
|
ID: "foo0",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.AddResult("foo", solver.CacheResult{
|
2018-02-26 18:51:38 +00:00
|
|
|
ID: "foo1",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.AddResult("bar", solver.CacheResult{
|
2018-02-26 18:51:38 +00:00
|
|
|
ID: "bar0",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
m := map[string]solver.CacheResult{}
|
|
|
|
err = st.WalkResults("foo", func(r solver.CacheResult) error {
|
2018-02-26 18:51:38 +00:00
|
|
|
m[r.ID] = r
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, len(m), 2)
|
|
|
|
f0, ok := m["foo0"]
|
|
|
|
require.True(t, ok)
|
|
|
|
f1, ok := m["foo1"]
|
|
|
|
require.True(t, ok)
|
|
|
|
require.True(t, f0.CreatedAt.Before(f1.CreatedAt))
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
m = map[string]solver.CacheResult{}
|
|
|
|
err = st.WalkResults("bar", func(r solver.CacheResult) error {
|
2018-02-26 18:51:38 +00:00
|
|
|
m[r.ID] = r
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, len(m), 1)
|
|
|
|
_, ok = m["bar0"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// empty result
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.WalkResults("baz", func(r solver.CacheResult) error {
|
2018-02-26 18:51:38 +00:00
|
|
|
require.Fail(t, "unreachable")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res, err := st.Load("foo", "foo1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, res.ID, "foo1")
|
|
|
|
|
|
|
|
_, err = st.Load("foo1", "foo1")
|
|
|
|
require.Error(t, err)
|
2018-05-12 01:32:56 +00:00
|
|
|
require.Equal(t, errors.Cause(err), solver.ErrNotFound)
|
2018-02-26 18:51:38 +00:00
|
|
|
|
|
|
|
_, err = st.Load("foo", "foo2")
|
|
|
|
require.Error(t, err)
|
2018-05-12 01:32:56 +00:00
|
|
|
require.Equal(t, errors.Cause(err), solver.ErrNotFound)
|
2018-02-26 18:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
func testLinks(t *testing.T, st solver.CacheKeyStorage) {
|
2018-02-26 18:51:38 +00:00
|
|
|
t.Parallel()
|
2018-02-27 01:39:18 +00:00
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
l0 := solver.CacheInfoLink{
|
2018-02-26 18:51:38 +00:00
|
|
|
Input: 0, Output: 1, Digest: digest.FromBytes([]byte(">target0")),
|
|
|
|
}
|
2018-03-17 04:07:18 +00:00
|
|
|
err := st.AddLink("foo", l0, "target0")
|
2018-02-26 18:51:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
err = st.AddLink("bar", l0, "target0-second")
|
2018-02-26 18:51:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m := map[string]struct{}{}
|
2018-03-17 04:07:18 +00:00
|
|
|
err = st.WalkLinks("foo", l0, func(id string) error {
|
2018-02-26 18:51:38 +00:00
|
|
|
m[id] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, len(m), 1)
|
|
|
|
_, ok := m["target0"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
l1 := solver.CacheInfoLink{
|
2018-02-26 18:51:38 +00:00
|
|
|
Input: 0, Output: 1, Digest: digest.FromBytes([]byte(">target1")),
|
|
|
|
}
|
|
|
|
m = map[string]struct{}{}
|
2018-03-17 04:07:18 +00:00
|
|
|
err = st.WalkLinks("foo", l1, func(id string) error {
|
2018-02-26 18:51:38 +00:00
|
|
|
m[id] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(m), 0)
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
err = st.AddLink("foo", l1, "target1")
|
2018-02-26 18:51:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m = map[string]struct{}{}
|
2018-03-17 04:07:18 +00:00
|
|
|
err = st.WalkLinks("foo", l1, func(id string) error {
|
2018-02-26 18:51:38 +00:00
|
|
|
m[id] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(m), 1)
|
|
|
|
|
|
|
|
_, ok = m["target1"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
err = st.AddLink("foo", l1, "target1-second")
|
2018-02-26 18:51:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m = map[string]struct{}{}
|
2018-03-17 04:07:18 +00:00
|
|
|
err = st.WalkLinks("foo", l1, func(id string) error {
|
2018-02-26 18:51:38 +00:00
|
|
|
m[id] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(m), 2)
|
|
|
|
_, ok = m["target1"]
|
|
|
|
require.True(t, ok)
|
|
|
|
_, ok = m["target1-second"]
|
|
|
|
require.True(t, ok)
|
|
|
|
}
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
func testResultReleaseSingleLevel(t *testing.T, st solver.CacheKeyStorage) {
|
2018-02-27 01:39:18 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err := st.AddResult("foo", solver.CacheResult{
|
2018-02-27 01:39:18 +00:00
|
|
|
ID: "foo0",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.AddResult("foo", solver.CacheResult{
|
2018-02-27 01:39:18 +00:00
|
|
|
ID: "foo1",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = st.Release("foo0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m := map[string]struct{}{}
|
2018-05-12 01:32:56 +00:00
|
|
|
st.WalkResults("foo", func(res solver.CacheResult) error {
|
2018-02-27 01:39:18 +00:00
|
|
|
m[res.ID] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Equal(t, len(m), 1)
|
|
|
|
_, ok := m["foo1"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
err = st.Release("foo1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m = map[string]struct{}{}
|
2018-05-12 01:32:56 +00:00
|
|
|
st.WalkResults("foo", func(res solver.CacheResult) error {
|
2018-02-27 01:39:18 +00:00
|
|
|
m[res.ID] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
require.Equal(t, len(m), 0)
|
2018-07-24 01:24:13 +00:00
|
|
|
|
|
|
|
st.Walk(func(id string) error {
|
|
|
|
require.False(t, true, fmt.Sprintf("id %s should have been released", id))
|
|
|
|
return nil
|
|
|
|
})
|
2018-02-27 01:39:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
func testBacklinks(t *testing.T, st solver.CacheKeyStorage) {
|
2018-04-13 20:52:27 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err := st.AddResult("foo", solver.CacheResult{
|
2018-04-13 20:52:27 +00:00
|
|
|
ID: "foo-result",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.AddResult("sub0", solver.CacheResult{
|
2018-04-13 20:52:27 +00:00
|
|
|
ID: "sub0-result",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
l0 := solver.CacheInfoLink{
|
2018-04-13 20:52:27 +00:00
|
|
|
Input: 0, Output: 1, Digest: digest.FromBytes([]byte("to-sub0")),
|
|
|
|
}
|
|
|
|
err = st.AddLink("foo", l0, "sub0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
backlinks := 0
|
2018-05-12 01:32:56 +00:00
|
|
|
st.WalkBacklinks("sub0", func(id string, link solver.CacheInfoLink) error {
|
2018-04-13 20:52:27 +00:00
|
|
|
require.Equal(t, id, "foo")
|
2018-05-12 01:32:56 +00:00
|
|
|
require.Equal(t, link.Input, solver.Index(0))
|
2018-04-13 20:52:27 +00:00
|
|
|
require.Equal(t, link.Digest, rootKey(digest.FromBytes([]byte("to-sub0")), 1))
|
|
|
|
backlinks++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.Equal(t, backlinks, 1)
|
|
|
|
}
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
func testResultReleaseMultiLevel(t *testing.T, st solver.CacheKeyStorage) {
|
2018-02-27 01:39:18 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err := st.AddResult("foo", solver.CacheResult{
|
2018-02-27 01:39:18 +00:00
|
|
|
ID: "foo-result",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.AddResult("sub0", solver.CacheResult{
|
2018-02-27 01:39:18 +00:00
|
|
|
ID: "sub0-result",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
l0 := solver.CacheInfoLink{
|
2018-02-27 01:39:18 +00:00
|
|
|
Input: 0, Output: 1, Digest: digest.FromBytes([]byte("to-sub0")),
|
|
|
|
}
|
2018-03-17 04:07:18 +00:00
|
|
|
err = st.AddLink("foo", l0, "sub0")
|
2018-02-27 01:39:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.AddResult("sub1", solver.CacheResult{
|
2018-02-27 01:39:18 +00:00
|
|
|
ID: "sub1-result",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
err = st.AddLink("foo", l0, "sub1")
|
2018-02-27 01:39:18 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// delete one sub doesn't delete parent
|
|
|
|
|
|
|
|
err = st.Release("sub0-result")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m := map[string]struct{}{}
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.WalkResults("foo", func(res solver.CacheResult) error {
|
2018-02-27 01:39:18 +00:00
|
|
|
m[res.ID] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, len(m), 1)
|
|
|
|
_, ok := m["foo-result"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
require.False(t, st.Exists("sub0"))
|
2018-02-27 01:39:18 +00:00
|
|
|
|
|
|
|
m = map[string]struct{}{}
|
|
|
|
err = st.WalkLinks("foo", l0, func(id string) error {
|
|
|
|
m[id] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(m), 1)
|
|
|
|
|
|
|
|
_, ok = m["sub1"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
// release foo removes the result but doesn't break the chain
|
|
|
|
|
|
|
|
err = st.Release("foo-result")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
require.True(t, st.Exists("foo"))
|
2018-02-27 01:39:18 +00:00
|
|
|
|
|
|
|
m = map[string]struct{}{}
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.WalkResults("foo", func(res solver.CacheResult) error {
|
2018-02-27 01:39:18 +00:00
|
|
|
m[res.ID] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, len(m), 0)
|
|
|
|
|
|
|
|
m = map[string]struct{}{}
|
|
|
|
err = st.WalkLinks("foo", l0, func(id string) error {
|
|
|
|
m[id] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, len(m), 1)
|
|
|
|
|
|
|
|
// release sub1 now releases foo as well
|
|
|
|
err = st.Release("sub1-result")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-03-17 04:07:18 +00:00
|
|
|
require.False(t, st.Exists("sub1"))
|
|
|
|
require.False(t, st.Exists("foo"))
|
2018-07-24 01:24:13 +00:00
|
|
|
|
|
|
|
st.Walk(func(id string) error {
|
|
|
|
require.False(t, true, fmt.Sprintf("id %s should have been released", id))
|
|
|
|
return nil
|
|
|
|
})
|
2018-02-27 01:39:18 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
func testWalkIDsByResult(t *testing.T, st solver.CacheKeyStorage) {
|
2018-04-24 19:01:50 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err := st.AddResult("foo", solver.CacheResult{
|
2018-04-24 19:01:50 +00:00
|
|
|
ID: "foo-result",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.AddResult("foo2", solver.CacheResult{
|
2018-04-24 19:01:50 +00:00
|
|
|
ID: "foo-result",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-12 01:32:56 +00:00
|
|
|
err = st.AddResult("bar", solver.CacheResult{
|
2018-04-24 19:01:50 +00:00
|
|
|
ID: "bar-result",
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m := map[string]struct{}{}
|
|
|
|
err = st.WalkIDsByResult("foo-result", func(id string) error {
|
|
|
|
m[id] = struct{}{}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, ok := m["foo"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
_, ok = m["foo2"]
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
_, ok = m["bar"]
|
|
|
|
require.False(t, ok)
|
|
|
|
}
|
|
|
|
|
2018-02-26 18:51:38 +00:00
|
|
|
func getFunctionName(i interface{}) string {
|
|
|
|
fullname := runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
|
|
|
|
dot := strings.LastIndex(fullname, ".") + 1
|
|
|
|
return strings.Title(fullname[dot:])
|
|
|
|
}
|
2018-05-12 01:32:56 +00:00
|
|
|
|
|
|
|
func rootKey(dgst digest.Digest, output solver.Index) digest.Digest {
|
|
|
|
return digest.FromBytes([]byte(fmt.Sprintf("%s@%d", dgst, output)))
|
|
|
|
}
|