2015-11-18 17:18:07 -05:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/digest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFSGetSet(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "images-fs-store")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
fs, err := NewFSStoreBackend(tmpdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testGetSet(t, fs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSGetInvalidData(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "images-fs-store")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
fs, err := NewFSStoreBackend(tmpdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := fs.Set([]byte("foobar"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dgst := digest.Digest(id)
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(tmpdir, contentDirName, string(dgst.Algorithm()), dgst.Hex()), []byte("foobar2"), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fs.Get(id)
|
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatal("expected get to fail after data modification.")
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSInvalidSet(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "images-fs-store")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
fs, err := NewFSStoreBackend(tmpdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-12-08 14:14:02 -05:00
|
|
|
id := digest.FromBytes([]byte("foobar"))
|
2015-11-18 17:18:07 -05:00
|
|
|
err = os.Mkdir(filepath.Join(tmpdir, contentDirName, string(id.Algorithm()), id.Hex()), 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fs.Set([]byte("foobar"))
|
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatal("expected error from invalid filesystem data.")
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSInvalidRoot(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "images-fs-store")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
tcases := []struct {
|
|
|
|
root, invalidFile string
|
|
|
|
}{
|
|
|
|
{"root", "root"},
|
|
|
|
{"root", "root/content"},
|
|
|
|
{"root", "root/metadata"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcases {
|
|
|
|
root := filepath.Join(tmpdir, tc.root)
|
|
|
|
filePath := filepath.Join(tmpdir, tc.invalidFile)
|
|
|
|
err := os.MkdirAll(filepath.Dir(filePath), 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f, err := os.Create(filePath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
_, err = NewFSStoreBackend(root)
|
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected error from root %q and invalid file %q", tc.root, tc.invalidFile)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
os.RemoveAll(root)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMetadataGetSet(t *testing.T, store StoreBackend) {
|
|
|
|
id, err := store.Set([]byte("foo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
id2, err := store.Set([]byte("bar"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tcases := []struct {
|
2016-09-15 19:37:32 -04:00
|
|
|
id digest.Digest
|
2015-11-18 17:18:07 -05:00
|
|
|
key string
|
|
|
|
value []byte
|
|
|
|
}{
|
|
|
|
{id, "tkey", []byte("tval1")},
|
|
|
|
{id, "tkey2", []byte("tval2")},
|
|
|
|
{id2, "tkey", []byte("tval3")},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcases {
|
|
|
|
err = store.SetMetadata(tc.id, tc.key, tc.value)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, err := store.GetMetadata(tc.id, tc.key)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if bytes.Compare(actual, tc.value) != 0 {
|
|
|
|
t.Fatalf("Metadata expected %q, got %q", tc.value, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = store.GetMetadata(id2, "tkey2")
|
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatal("expected error for getting metadata for unknown key")
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2015-12-08 14:14:02 -05:00
|
|
|
id3 := digest.FromBytes([]byte("baz"))
|
2016-09-15 19:37:32 -04:00
|
|
|
err = store.SetMetadata(id3, "tkey", []byte("tval"))
|
2015-11-18 17:18:07 -05:00
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatal("expected error for setting metadata for unknown ID.")
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
_, err = store.GetMetadata(id3, "tkey")
|
2015-11-18 17:18:07 -05:00
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatal("expected error for getting metadata for unknown ID.")
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSMetadataGetSet(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "images-fs-store")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
fs, err := NewFSStoreBackend(tmpdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testMetadataGetSet(t, fs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSDelete(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "images-fs-store")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
fs, err := NewFSStoreBackend(tmpdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testDelete(t, fs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSWalker(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "images-fs-store")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
fs, err := NewFSStoreBackend(tmpdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testWalker(t, fs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSInvalidWalker(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "images-fs-store")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
fs, err := NewFSStoreBackend(tmpdir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fooID, err := fs.Set([]byte("foo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(filepath.Join(tmpdir, contentDirName, "sha256/foobar"), []byte("foobar"), 0600); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
n := 0
|
2016-09-15 19:37:32 -04:00
|
|
|
err = fs.Walk(func(id digest.Digest) error {
|
2015-11-18 17:18:07 -05:00
|
|
|
if id != fooID {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("invalid walker ID %q, expected %q", id, fooID)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
n++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("invalid data should not have caused walker error, got %v", err)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
if n != 1 {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected 1 walk initialization, got %d", n)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetSet(t *testing.T, store StoreBackend) {
|
|
|
|
type tcase struct {
|
|
|
|
input []byte
|
2016-09-15 19:37:32 -04:00
|
|
|
expected digest.Digest
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
tcases := []tcase{
|
2016-09-15 19:37:32 -04:00
|
|
|
{[]byte("foobar"), digest.Digest("sha256:c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")},
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
randomInput := make([]byte, 8*1024)
|
|
|
|
_, err := rand.Read(randomInput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-12-13 09:15:08 -05:00
|
|
|
// skipping use of digest pkg because it is used by the implementation
|
2015-11-18 17:18:07 -05:00
|
|
|
h := sha256.New()
|
|
|
|
_, err = h.Write(randomInput)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tcases = append(tcases, tcase{
|
|
|
|
input: randomInput,
|
2016-09-15 19:37:32 -04:00
|
|
|
expected: digest.Digest("sha256:" + hex.EncodeToString(h.Sum(nil))),
|
2015-11-18 17:18:07 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
for _, tc := range tcases {
|
|
|
|
id, err := store.Set([]byte(tc.input))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if id != tc.expected {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected ID %q, got %q", tc.expected, id)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, emptyData := range [][]byte{nil, {}} {
|
|
|
|
_, err := store.Set(emptyData)
|
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatal("expected error for nil input.")
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcases {
|
|
|
|
data, err := store.Get(tc.expected)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if bytes.Compare(data, tc.input) != 0 {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected data %q, got %q", tc.input, data)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
for _, key := range []digest.Digest{"foobar:abc", "sha256:abc", "sha256:c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2a"} {
|
2015-11-18 17:18:07 -05:00
|
|
|
_, err := store.Get(key)
|
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected error for ID %q.", key)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDelete(t *testing.T, store StoreBackend) {
|
|
|
|
id, err := store.Set([]byte("foo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
id2, err := store.Set([]byte("bar"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.Delete(id)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = store.Get(id)
|
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected getting deleted item %q to fail", id)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
_, err = store.Get(id2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = store.Delete(id2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = store.Get(id2)
|
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected getting deleted item %q to fail", id2)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testWalker(t *testing.T, store StoreBackend) {
|
|
|
|
id, err := store.Set([]byte("foo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
id2, err := store.Set([]byte("bar"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
tcases := make(map[digest.Digest]struct{})
|
2015-11-18 17:18:07 -05:00
|
|
|
tcases[id] = struct{}{}
|
|
|
|
tcases[id2] = struct{}{}
|
|
|
|
n := 0
|
2016-09-15 19:37:32 -04:00
|
|
|
err = store.Walk(func(id digest.Digest) error {
|
2015-11-18 17:18:07 -05:00
|
|
|
delete(tcases, id)
|
|
|
|
n++
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if n != 2 {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected 2 walk initializations, got %d", n)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
if len(tcases) != 0 {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected empty unwalked set, got %+v", tcases)
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// stop on error
|
2016-09-15 19:37:32 -04:00
|
|
|
tcases = make(map[digest.Digest]struct{})
|
2015-11-18 17:18:07 -05:00
|
|
|
tcases[id] = struct{}{}
|
2016-09-15 19:37:32 -04:00
|
|
|
err = store.Walk(func(id digest.Digest) error {
|
2015-11-18 17:18:07 -05:00
|
|
|
return errors.New("")
|
|
|
|
})
|
|
|
|
if err == nil {
|
2016-12-13 09:15:08 -05:00
|
|
|
t.Fatalf("expected error from walker.")
|
2015-11-18 17:18:07 -05:00
|
|
|
}
|
|
|
|
}
|