2018-02-05 21:05:59 +00:00
|
|
|
package image // import "github.com/docker/docker/image"
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2022-03-04 13:49:42 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-11-18 22:18:07 +00:00
|
|
|
)
|
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func defaultFSStoreBackend(t *testing.T) (StoreBackend, func()) {
|
2021-08-24 10:10:50 +00:00
|
|
|
tmpdir, err := os.MkdirTemp("", "images-fs-store")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
fsBackend, err := NewFSStoreBackend(tmpdir)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
|
|
|
return fsBackend, func() { os.RemoveAll(tmpdir) }
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSGetInvalidData(t *testing.T) {
|
2017-03-03 17:38:06 +00:00
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2019-08-07 00:38:51 +00:00
|
|
|
dgst, err := store.Set([]byte("foobar"))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-11-08 15:42:13 +00:00
|
|
|
err = os.WriteFile(filepath.Join(store.(*fs).root, contentDirName, string(dgst.Algorithm()), dgst.Encoded()), []byte("foobar2"), 0o600)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2019-08-07 00:38:51 +00:00
|
|
|
_, err = store.Get(dgst)
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "failed to verify"))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSInvalidSet(t *testing.T) {
|
2017-03-03 17:38:06 +00:00
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2015-12-08 19:14:02 +00:00
|
|
|
id := digest.FromBytes([]byte("foobar"))
|
2022-11-08 15:42:13 +00:00
|
|
|
err := os.Mkdir(filepath.Join(store.(*fs).root, contentDirName, string(id.Algorithm()), id.Encoded()), 0o700)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
_, err = store.Set([]byte("foobar"))
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "failed to write digest data"))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSInvalidRoot(t *testing.T) {
|
2021-08-24 10:10:50 +00:00
|
|
|
tmpdir, err := os.MkdirTemp("", "images-fs-store")
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
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)
|
2023-08-24 15:57:22 +00:00
|
|
|
err := os.MkdirAll(filepath.Dir(filePath), 0o700)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
f, err := os.Create(filePath)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-06 15:36:52 +00:00
|
|
|
f.Close()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
_, err = NewFSStoreBackend(root)
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "failed to create storage backend"))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
os.RemoveAll(root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func TestFSMetadataGetSet(t *testing.T) {
|
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
id, err := store.Set([]byte("foo"))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
id2, err := store.Set([]byte("bar"))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
tcases := []struct {
|
2016-09-15 23:37:32 +00:00
|
|
|
id digest.Digest
|
2015-11-18 22:18:07 +00: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)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
actual, err := store.GetMetadata(tc.id, tc.key)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.DeepEqual(tc.value, actual))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = store.GetMetadata(id2, "tkey2")
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "failed to read metadata"))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2015-12-08 19:14:02 +00:00
|
|
|
id3 := digest.FromBytes([]byte("baz"))
|
2016-09-15 23:37:32 +00:00
|
|
|
err = store.SetMetadata(id3, "tkey", []byte("tval"))
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "failed to get digest"))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2016-09-15 23:37:32 +00:00
|
|
|
_, err = store.GetMetadata(id3, "tkey")
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "failed to get digest"))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSInvalidWalker(t *testing.T) {
|
2017-03-03 17:38:06 +00:00
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
fooID, err := store.Set([]byte("foo"))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2023-08-24 15:57:22 +00:00
|
|
|
err = os.WriteFile(filepath.Join(store.(*fs).root, contentDirName, "sha256/foobar"), []byte("foobar"), 0o600)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
n := 0
|
2017-03-03 17:38:06 +00:00
|
|
|
err = store.Walk(func(id digest.Digest) error {
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, is.Equal(fooID, id))
|
2015-11-18 22:18:07 +00:00
|
|
|
n++
|
|
|
|
return nil
|
|
|
|
})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.Equal(1, n))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func TestFSGetSet(t *testing.T) {
|
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
type tcase struct {
|
|
|
|
input []byte
|
2016-09-15 23:37:32 +00:00
|
|
|
expected digest.Digest
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
tcases := []tcase{
|
2016-09-15 23:37:32 +00:00
|
|
|
{[]byte("foobar"), digest.Digest("sha256:c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2")},
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
randomInput := make([]byte, 8*1024)
|
|
|
|
_, err := rand.Read(randomInput)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2016-12-13 14:15:08 +00:00
|
|
|
// skipping use of digest pkg because it is used by the implementation
|
2015-11-18 22:18:07 +00:00
|
|
|
h := sha256.New()
|
|
|
|
_, err = h.Write(randomInput)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
tcases = append(tcases, tcase{
|
|
|
|
input: randomInput,
|
2016-09-15 23:37:32 +00:00
|
|
|
expected: digest.Digest("sha256:" + hex.EncodeToString(h.Sum(nil))),
|
2015-11-18 22:18:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
for _, tc := range tcases {
|
2019-08-07 00:38:51 +00:00
|
|
|
id, err := store.Set(tc.input)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.Equal(tc.expected, id))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcases {
|
|
|
|
data, err := store.Get(tc.expected)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.DeepEqual(tc.input, data))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
2017-03-03 17:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFSGetUnsetKey(t *testing.T) {
|
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2016-09-15 23:37:32 +00:00
|
|
|
for _, key := range []digest.Digest{"foobar:abc", "sha256:abc", "sha256:c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2a"} {
|
2015-11-18 22:18:07 +00:00
|
|
|
_, err := store.Get(key)
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "failed to get digest"))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
2017-03-03 17:38:06 +00:00
|
|
|
}
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func TestFSGetEmptyData(t *testing.T) {
|
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
for _, emptyData := range [][]byte{nil, {}} {
|
|
|
|
_, err := store.Set(emptyData)
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "invalid empty data"))
|
2017-03-03 17:38:06 +00:00
|
|
|
}
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func TestFSDelete(t *testing.T) {
|
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
id, err := store.Set([]byte("foo"))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
id2, err := store.Set([]byte("bar"))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
err = store.Delete(id)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
_, err = store.Get(id)
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "failed to get digest"))
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
_, err = store.Get(id2)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
err = store.Delete(id2)
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
_, err = store.Get(id2)
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "failed to get digest"))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func TestFSWalker(t *testing.T) {
|
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
id, err := store.Set([]byte("foo"))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
id2, err := store.Set([]byte("bar"))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2016-09-15 23:37:32 +00:00
|
|
|
tcases := make(map[digest.Digest]struct{})
|
2015-11-18 22:18:07 +00:00
|
|
|
tcases[id] = struct{}{}
|
|
|
|
tcases[id2] = struct{}{}
|
|
|
|
n := 0
|
2016-09-15 23:37:32 +00:00
|
|
|
err = store.Walk(func(id digest.Digest) error {
|
2015-11-18 22:18:07 +00:00
|
|
|
delete(tcases, id)
|
|
|
|
n++
|
|
|
|
return nil
|
|
|
|
})
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
|
|
|
assert.Check(t, is.Equal(2, n))
|
|
|
|
assert.Check(t, is.Len(tcases, 0))
|
2017-03-03 17:38:06 +00:00
|
|
|
}
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func TestFSWalkerStopOnError(t *testing.T) {
|
|
|
|
store, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
id, err := store.Set([]byte("foo"))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
|
|
|
tcases := make(map[digest.Digest]struct{})
|
2015-11-18 22:18:07 +00:00
|
|
|
tcases[id] = struct{}{}
|
2016-09-15 23:37:32 +00:00
|
|
|
err = store.Walk(func(id digest.Digest) error {
|
2017-03-03 17:38:06 +00:00
|
|
|
return errors.New("what")
|
2015-11-18 22:18:07 +00:00
|
|
|
})
|
2018-05-20 22:06:50 +00:00
|
|
|
assert.Check(t, is.ErrorContains(err, "what"))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|