1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #20058 from tonistiigi/fix-parent-reset

Clear old parent reference on resetting image parent
This commit is contained in:
Antonio Murdaca 2016-02-06 10:38:35 +01:00
commit 78f2b8d87d
2 changed files with 59 additions and 0 deletions

View file

@ -231,6 +231,9 @@ func (is *store) SetParent(id, parent ID) error {
if parentMeta == nil {
return fmt.Errorf("unknown parent image ID %s", parent.String())
}
if parent, err := is.GetParent(id); err == nil && is.images[parent] != nil {
delete(is.images[parent].children, id)
}
parentMeta.children[id] = struct{}{}
return is.fs.SetMetadata(id, "parent", []byte(parent))
}

View file

@ -233,6 +233,62 @@ func TestSearchAfterDelete(t *testing.T) {
}
}
func TestParentReset(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)
}
is, err := NewImageStore(fs, &mockLayerGetReleaser{})
if err != nil {
t.Fatal(err)
}
id, err := is.Create([]byte(`{"comment": "abc1", "rootfs": {"type": "layers"}}`))
if err != nil {
t.Fatal(err)
}
id2, err := is.Create([]byte(`{"comment": "abc2", "rootfs": {"type": "layers"}}`))
if err != nil {
t.Fatal(err)
}
id3, err := is.Create([]byte(`{"comment": "abc3", "rootfs": {"type": "layers"}}`))
if err != nil {
t.Fatal(err)
}
if err := is.SetParent(id, id2); err != nil {
t.Fatal(err)
}
ids := is.Children(id2)
if actual, expected := len(ids), 1; expected != actual {
t.Fatalf("wrong number of children: %d, got %d", expected, actual)
}
if err := is.SetParent(id, id3); err != nil {
t.Fatal(err)
}
ids = is.Children(id2)
if actual, expected := len(ids), 0; expected != actual {
t.Fatalf("wrong number of children after parent reset: %d, got %d", expected, actual)
}
ids = is.Children(id3)
if actual, expected := len(ids), 1; expected != actual {
t.Fatalf("wrong number of children after parent reset: %d, got %d", expected, actual)
}
}
type mockLayerGetReleaser struct{}
func (ls *mockLayerGetReleaser) Get(layer.ChainID) (layer.Layer, error) {