2017-06-15 14:41:54 -04:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/cli/cli/manifest/types"
|
|
|
|
"github.com/docker/distribution/reference"
|
2017-12-21 16:27:57 -05:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
|
|
|
is "github.com/gotestyourself/gotestyourself/assert/cmp"
|
2017-06-15 14:41:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type fakeRef struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fakeRef) String() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f fakeRef) Name() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func ref(name string) fakeRef {
|
|
|
|
return fakeRef{name: name}
|
|
|
|
}
|
|
|
|
|
|
|
|
func sref(t *testing.T, name string) *types.SerializableNamed {
|
|
|
|
named, err := reference.ParseNamed("example.com/" + name)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 14:41:54 -04:00
|
|
|
return &types.SerializableNamed{Named: named}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestStore(t *testing.T) (Store, func()) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "manifest-store-test")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 14:41:54 -04:00
|
|
|
|
|
|
|
return NewStore(tmpdir), func() { os.RemoveAll(tmpdir) }
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFiles(t *testing.T, store Store) []os.FileInfo {
|
|
|
|
infos, err := ioutil.ReadDir(store.(*fsStore).root)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 14:41:54 -04:00
|
|
|
return infos
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreRemove(t *testing.T) {
|
|
|
|
store, cleanup := newTestStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
listRef := ref("list")
|
|
|
|
data := types.ImageManifest{Ref: sref(t, "abcdef")}
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, store.Save(listRef, ref("manifest"), data))
|
|
|
|
assert.Assert(t, is.Len(getFiles(t, store), 1))
|
2017-06-15 14:41:54 -04:00
|
|
|
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, store.Remove(listRef))
|
|
|
|
assert.Check(t, is.Len(getFiles(t, store), 0))
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreSaveAndGet(t *testing.T) {
|
|
|
|
store, cleanup := newTestStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
listRef := ref("list")
|
|
|
|
data := types.ImageManifest{Ref: sref(t, "abcdef")}
|
|
|
|
err := store.Save(listRef, ref("exists"), data)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-06-15 14:41:54 -04:00
|
|
|
|
|
|
|
var testcases = []struct {
|
|
|
|
listRef reference.Reference
|
|
|
|
manifestRef reference.Reference
|
|
|
|
expected types.ImageManifest
|
|
|
|
expectedErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
listRef: listRef,
|
|
|
|
manifestRef: ref("exists"),
|
|
|
|
expected: data,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
listRef: listRef,
|
|
|
|
manifestRef: ref("exist:does-not"),
|
|
|
|
expectedErr: "No such manifest: exist:does-not",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
listRef: ref("list:does-not-exist"),
|
|
|
|
manifestRef: ref("manifest:does-not-exist"),
|
|
|
|
expectedErr: "No such manifest: manifest:does-not-exist",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
2017-12-21 16:27:57 -05:00
|
|
|
t.Run(testcase.manifestRef.String(), func(t *testing.T) {
|
|
|
|
actual, err := store.Get(testcase.listRef, testcase.manifestRef)
|
|
|
|
if testcase.expectedErr != "" {
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, testcase.expectedErr)
|
2017-12-21 16:27:57 -05:00
|
|
|
assert.Check(t, IsNotFound(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.DeepEqual(t, testcase.expected, actual, cmpReferenceNamed)
|
|
|
|
})
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 16:27:57 -05:00
|
|
|
var cmpReferenceNamed = cmp.Transformer("namedref", func(r reference.Named) string {
|
|
|
|
return r.String()
|
|
|
|
})
|
|
|
|
|
2017-06-15 14:41:54 -04:00
|
|
|
func TestStoreGetList(t *testing.T) {
|
|
|
|
store, cleanup := newTestStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
listRef := ref("list")
|
|
|
|
first := types.ImageManifest{Ref: sref(t, "first")}
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, store.Save(listRef, ref("first"), first))
|
2017-06-15 14:41:54 -04:00
|
|
|
second := types.ImageManifest{Ref: sref(t, "second")}
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, store.Save(listRef, ref("exists"), second))
|
2017-06-15 14:41:54 -04:00
|
|
|
|
|
|
|
list, err := store.GetList(listRef)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, is.Len(list, 2))
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStoreGetListDoesNotExist(t *testing.T) {
|
|
|
|
store, cleanup := newTestStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
listRef := ref("list")
|
|
|
|
_, err := store.GetList(listRef)
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "No such manifest: list")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, IsNotFound(err))
|
2017-06-15 14:41:54 -04:00
|
|
|
}
|