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 (
|
2018-03-13 16:21:56 +00:00
|
|
|
"fmt"
|
2015-11-18 22:18:07 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-12-06 11:27:39 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2015-11-18 22:18:07 +00:00
|
|
|
"github.com/docker/docker/layer"
|
2020-02-07 13:39:24 +00:00
|
|
|
"gotest.tools/v3/assert"
|
2022-07-26 12:12:44 +00:00
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-11-18 22:18:07 +00:00
|
|
|
)
|
|
|
|
|
2021-01-11 14:59:35 +00:00
|
|
|
func TestCreate(t *testing.T) {
|
2022-07-26 12:12:44 +00:00
|
|
|
imgStore, cleanup := defaultImageStore(t)
|
2021-01-11 14:59:35 +00:00
|
|
|
defer cleanup()
|
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
_, err := imgStore.Create([]byte(`{}`))
|
|
|
|
assert.Check(t, is.Error(err, "invalid image JSON, no RootFS key"))
|
2021-01-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
func TestRestore(t *testing.T) {
|
2017-03-03 17:38:06 +00:00
|
|
|
fs, cleanup := defaultFSStoreBackend(t)
|
|
|
|
defer cleanup()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
|
|
|
id1, err := fs.Set([]byte(`{"comment": "abc", "rootfs": {"type": "layers"}}`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
_, err = fs.Set([]byte(`invalid`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
id2, err := fs.Set([]byte(`{"comment": "def", "rootfs": {"type": "layers", "diff_ids": ["2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"]}}`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
err = fs.SetMetadata(id2, "parent", []byte(id1))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
imgStore, err := NewImageStore(fs, &mockLayerGetReleaser{})
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Len(imgStore.Map(), 2))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
img1, err := imgStore.Get(ID(id1))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal(ID(id1), img1.computedID))
|
|
|
|
assert.Check(t, is.Equal(string(id1), img1.computedID.String()))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
img2, err := imgStore.Get(ID(id2))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal("abc", img1.Comment))
|
|
|
|
assert.Check(t, is.Equal("def", img2.Comment))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
_, err = imgStore.GetParent(ID(id1))
|
2022-12-06 11:27:39 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.ErrorContains(t, err, "failed to read metadata")
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
p, err := imgStore.GetParent(ID(id2))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal(ID(id1), p))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
children := imgStore.Children(ID(id1))
|
|
|
|
assert.Check(t, is.Len(children, 1))
|
|
|
|
assert.Check(t, is.Equal(ID(id2), children[0]))
|
|
|
|
assert.Check(t, is.Len(imgStore.Heads(), 1))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
sid1, err := imgStore.Search(string(id1)[:10])
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal(ID(id1), sid1))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-11-08 15:42:13 +00:00
|
|
|
sid1, err = imgStore.Search(id1.Encoded()[:6])
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal(ID(id1), sid1))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-11-08 15:42:13 +00:00
|
|
|
invalidPattern := id1.Encoded()[1:6]
|
2022-07-26 12:12:44 +00:00
|
|
|
_, err = imgStore.Search(invalidPattern)
|
2022-12-06 11:27:39 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
|
|
|
assert.Check(t, is.ErrorContains(err, invalidPattern))
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddDelete(t *testing.T) {
|
2022-07-26 12:12:44 +00:00
|
|
|
imgStore, cleanup := defaultImageStore(t)
|
2017-03-03 17:38:06 +00:00
|
|
|
defer cleanup()
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
id1, err := imgStore.Create([]byte(`{"comment": "abc", "rootfs": {"type": "layers", "diff_ids": ["2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"]}}`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal(ID("sha256:8d25a9c45df515f9d0fe8e4a6b1c64dd3b965a84790ddbcc7954bb9bc89eb993"), id1))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
img, err := imgStore.Get(id1)
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal("abc", img.Comment))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
id2, err := imgStore.Create([]byte(`{"comment": "def", "rootfs": {"type": "layers", "diff_ids": ["2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"]}}`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
err = imgStore.SetParent(id2, id1)
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
pid1, err := imgStore.GetParent(id2)
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal(pid1, id1))
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
_, err = imgStore.Delete(id1)
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
_, err = imgStore.Get(id1)
|
2022-12-06 11:27:39 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.ErrorContains(t, err, "failed to get digest")
|
2017-03-03 17:38:06 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
_, err = imgStore.Get(id2)
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2015-11-18 22:18:07 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
_, err = imgStore.GetParent(id2)
|
2022-12-06 11:27:39 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.ErrorContains(t, err, "failed to read metadata")
|
2015-11-18 22:18:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 21:15:54 +00:00
|
|
|
func TestSearchAfterDelete(t *testing.T) {
|
2022-07-26 12:12:44 +00:00
|
|
|
imgStore, cleanup := defaultImageStore(t)
|
2017-03-03 17:38:06 +00:00
|
|
|
defer cleanup()
|
2015-12-04 21:15:54 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
id, err := imgStore.Create([]byte(`{"comment": "abc", "rootfs": {"type": "layers"}}`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2015-12-04 21:15:54 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
id1, err := imgStore.Search(string(id)[:15])
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal(id1, id))
|
2015-12-04 21:15:54 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
_, err = imgStore.Delete(id)
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2015-12-04 21:15:54 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
_, err = imgStore.Search(string(id)[:15])
|
2022-12-06 11:27:39 +00:00
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.ErrorContains(t, err, "No such image")
|
2015-12-04 21:15:54 +00:00
|
|
|
}
|
|
|
|
|
2022-12-06 11:27:39 +00:00
|
|
|
func TestDeleteNotExisting(t *testing.T) {
|
|
|
|
imgStore, cleanup := defaultImageStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
_, err := imgStore.Delete(ID("i_dont_exists"))
|
|
|
|
assert.Check(t, is.ErrorType(err, errdefs.IsNotFound))
|
|
|
|
}
|
|
|
|
|
2016-02-06 01:04:44 +00:00
|
|
|
func TestParentReset(t *testing.T) {
|
2022-07-26 12:12:44 +00:00
|
|
|
imgStore, cleanup := defaultImageStore(t)
|
2017-03-03 17:38:06 +00:00
|
|
|
defer cleanup()
|
2016-02-06 01:04:44 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
id, err := imgStore.Create([]byte(`{"comment": "abc1", "rootfs": {"type": "layers"}}`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-02-06 01:04:44 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
id2, err := imgStore.Create([]byte(`{"comment": "abc2", "rootfs": {"type": "layers"}}`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-02-06 01:04:44 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
id3, err := imgStore.Create([]byte(`{"comment": "abc3", "rootfs": {"type": "layers"}}`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-02-06 01:04:44 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, imgStore.SetParent(id, id2))
|
|
|
|
assert.Check(t, is.Len(imgStore.Children(id2), 1))
|
2016-02-06 01:04:44 +00:00
|
|
|
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, imgStore.SetParent(id, id3))
|
|
|
|
assert.Check(t, is.Len(imgStore.Children(id2), 0))
|
|
|
|
assert.Check(t, is.Len(imgStore.Children(id3), 1))
|
2017-03-03 17:38:06 +00:00
|
|
|
}
|
2016-02-06 01:04:44 +00:00
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
func defaultImageStore(t *testing.T) (Store, func()) {
|
|
|
|
fsBackend, cleanup := defaultFSStoreBackend(t)
|
2016-02-06 01:04:44 +00:00
|
|
|
|
2021-03-19 14:34:08 +00:00
|
|
|
store, err := NewImageStore(fsBackend, &mockLayerGetReleaser{})
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2016-02-06 01:04:44 +00:00
|
|
|
|
2017-03-03 17:38:06 +00:00
|
|
|
return store, cleanup
|
2016-02-06 01:04:44 +00:00
|
|
|
}
|
|
|
|
|
2017-03-02 20:47:02 +00:00
|
|
|
func TestGetAndSetLastUpdated(t *testing.T) {
|
|
|
|
store, cleanup := defaultImageStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
id, err := store.Create([]byte(`{"comment": "abc1", "rootfs": {"type": "layers"}}`))
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2017-03-02 20:47:02 +00:00
|
|
|
|
|
|
|
updated, err := store.GetLastUpdated(id)
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal(updated.IsZero(), true))
|
2017-03-02 20:47:02 +00:00
|
|
|
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.Check(t, store.SetLastUpdated(id))
|
2017-03-02 20:47:02 +00:00
|
|
|
|
|
|
|
updated, err = store.GetLastUpdated(id)
|
2017-12-22 21:30:49 +00:00
|
|
|
assert.NilError(t, err)
|
2022-07-26 12:12:44 +00:00
|
|
|
assert.Check(t, is.Equal(updated.IsZero(), false))
|
2017-03-02 20:47:02 +00:00
|
|
|
}
|
|
|
|
|
2018-03-13 16:21:56 +00:00
|
|
|
func TestStoreLen(t *testing.T) {
|
|
|
|
store, cleanup := defaultImageStore(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
expected := 10
|
|
|
|
for i := 0; i < expected; i++ {
|
|
|
|
_, err := store.Create([]byte(fmt.Sprintf(`{"comment": "abc%d", "rootfs": {"type": "layers"}}`, i)))
|
2018-03-13 19:28:34 +00:00
|
|
|
assert.NilError(t, err)
|
2018-03-13 16:21:56 +00:00
|
|
|
}
|
|
|
|
numImages := store.Len()
|
|
|
|
assert.Equal(t, expected, numImages)
|
|
|
|
assert.Equal(t, len(store.Map()), numImages)
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:18:07 +00:00
|
|
|
type mockLayerGetReleaser struct{}
|
|
|
|
|
|
|
|
func (ls *mockLayerGetReleaser) Get(layer.ChainID) (layer.Layer, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *mockLayerGetReleaser) Release(layer.Layer) ([]layer.Metadata, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|