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"
|
2017-09-19 15:14:46 -04:00
|
|
|
"runtime"
|
2015-11-18 17:18:07 -05:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/layer"
|
2017-01-06 20:23:18 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2018-06-11 09:32:11 -04:00
|
|
|
"gotest.tools/assert"
|
|
|
|
"gotest.tools/assert/cmp"
|
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
|
|
|
|
2017-09-19 15:14:46 -04:00
|
|
|
mlgrMap := make(map[string]LayerGetReleaser)
|
|
|
|
mlgrMap[runtime.GOOS] = &mockLayerGetReleaser{}
|
|
|
|
is, err := NewImageStore(fs, mlgrMap)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
2015-11-18 17:18:07 -05:00
|
|
|
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Check(t, cmp.Len(is.Map(), 2))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
img1, err := is.Get(ID(id1))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, cmp.Equal(ID(id1), img1.computedID))
|
|
|
|
assert.Check(t, cmp.Equal(string(id1), img1.computedID.String()))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
img2, err := is.Get(ID(id2))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, cmp.Equal("abc", img1.Comment))
|
|
|
|
assert.Check(t, cmp.Equal("def", img2.Comment))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
2017-09-08 18:00:14 -04:00
|
|
|
_, err = is.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
|
|
|
|
2017-09-08 18:00:14 -04:00
|
|
|
p, err := is.GetParent(ID(id2))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, cmp.Equal(ID(id1), p))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
children := is.Children(ID(id1))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Check(t, cmp.Len(children, 1))
|
|
|
|
assert.Check(t, cmp.Equal(ID(id2), children[0]))
|
|
|
|
assert.Check(t, cmp.Len(is.Heads(), 1))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
sid1, err := is.Search(string(id1)[:10])
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, cmp.Equal(ID(id1), sid1))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
sid1, err = is.Search(digest.Digest(id1).Hex()[:6])
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, cmp.Equal(ID(id1), sid1))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
invalidPattern := digest.Digest(id1).Hex()[1:6]
|
|
|
|
_, err = is.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) {
|
2017-03-03 12:38:06 -05:00
|
|
|
is, cleanup := defaultImageStore(t)
|
|
|
|
defer cleanup()
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
id1, err := is.Create([]byte(`{"comment": "abc", "rootfs": {"type": "layers", "diff_ids": ["2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"]}}`))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, cmp.Equal(ID("sha256:8d25a9c45df515f9d0fe8e4a6b1c64dd3b965a84790ddbcc7954bb9bc89eb993"), id1))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
img, err := is.Get(id1)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, cmp.Equal("abc", img.Comment))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
id2, err := is.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
|
|
|
|
|
|
|
err = is.SetParent(id2, id1)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
pid1, err := is.GetParent(id2)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, cmp.Equal(pid1, id1))
|
2015-11-18 17:18:07 -05:00
|
|
|
|
|
|
|
_, err = is.Delete(id1)
|
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 = is.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
|
|
|
|
2015-11-18 17:18:07 -05:00
|
|
|
_, err = is.Get(id2)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
2015-11-18 17:18:07 -05:00
|
|
|
|
2017-03-03 12:38:06 -05:00
|
|
|
_, err = is.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) {
|
2017-03-03 12:38:06 -05:00
|
|
|
is, cleanup := defaultImageStore(t)
|
|
|
|
defer cleanup()
|
2015-12-04 16:15:54 -05:00
|
|
|
|
|
|
|
id, err := is.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
|
|
|
|
|
|
|
id1, err := is.Search(string(id)[:15])
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Check(t, cmp.Equal(id1, id))
|
2015-12-04 16:15:54 -05:00
|
|
|
|
2017-03-03 12:38:06 -05:00
|
|
|
_, err = is.Delete(id)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.NilError(t, err)
|
2015-12-04 16:15:54 -05:00
|
|
|
|
2017-03-03 12:38:06 -05:00
|
|
|
_, err = is.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) {
|
2017-03-03 12:38:06 -05:00
|
|
|
is, cleanup := defaultImageStore(t)
|
|
|
|
defer cleanup()
|
2016-02-05 20:04:44 -05:00
|
|
|
|
|
|
|
id, err := is.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
|
|
|
|
|
|
|
id2, err := is.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
|
|
|
|
|
|
|
id3, err := is.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
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.SetParent(id, id2))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Check(t, cmp.Len(is.Children(id2), 1))
|
2016-02-05 20:04:44 -05:00
|
|
|
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.SetParent(id, id3))
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Check(t, cmp.Len(is.Children(id2), 0))
|
|
|
|
assert.Check(t, cmp.Len(is.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
|
|
|
|
2017-09-19 15:14:46 -04:00
|
|
|
mlgrMap := make(map[string]LayerGetReleaser)
|
|
|
|
mlgrMap[runtime.GOOS] = &mockLayerGetReleaser{}
|
|
|
|
store, err := NewImageStore(fsBackend, mlgrMap)
|
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)
|
|
|
|
assert.Check(t, cmp.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)
|
|
|
|
assert.Check(t, cmp.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
|
|
|
|
}
|