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