Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Fix remove image by Id #740

Merged
merged 2 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/store/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ func (b *bundleStore) List() ([]reference.Reference, error) {

// Remove removes a bundle from the bundle store.
func (b *bundleStore) Remove(ref reference.Reference) error {
if id, ok := ref.(ID); ok {
if len(b.refsMap[id]) == 0 {
return fmt.Errorf("no such image %q", reference.FamiliarString(ref))
} else if len(b.refsMap[id]) > 1 {
return fmt.Errorf("unable to delete %q - App is referenced in multiple repositories", reference.FamiliarString(ref))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we tell the user which repositories?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the docker image rm command does not do it

}
ref = b.refsMap[id][0]
}
path, err := b.storePath(ref)
if err != nil {
return err
Expand Down
46 changes: 46 additions & 0 deletions internal/store/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,52 @@ func TestRemove(t *testing.T) {
})
}

func TestRemoveById(t *testing.T) {
dockerConfigDir := fs.NewDir(t, t.Name(), fs.WithMode(0755))
defer dockerConfigDir.Remove()
appstore, err := NewApplicationStore(dockerConfigDir.Path())
assert.NilError(t, err)
bundleStore, err := appstore.BundleStore()
assert.NilError(t, err)

t.Run("error when id does not exist", func(t *testing.T) {
idRef, err := FromBundle(relocated.FromBundle(&bundle.Bundle{Name: "not-stored-bundle-name"}))
assert.NilError(t, err)

err = bundleStore.Remove(idRef)
assert.Equal(t, err.Error(), fmt.Sprintf("no such image %q", reference.FamiliarString(idRef)))
})

t.Run("error on multiple repositories", func(t *testing.T) {
bndl := relocated.FromBundle(&bundle.Bundle{Name: "bundle-name"})
idRef, err := FromBundle(bndl)
assert.NilError(t, err)
_, err = bundleStore.Store(idRef, bndl)
assert.NilError(t, err)
_, err = bundleStore.Store(parseRefOrDie(t, "my-repo/a-bundle:my-tag"), bndl)
assert.NilError(t, err)

err = bundleStore.Remove(idRef)
assert.Equal(t, err.Error(), fmt.Sprintf("unable to delete %q - App is referenced in multiple repositories", reference.FamiliarString(idRef)))
})

t.Run("success when only one reference exists", func(t *testing.T) {
bndl := relocated.FromBundle(&bundle.Bundle{Name: "other-bundle-name"})
ref := parseRefOrDie(t, "my-repo/other-bundle:my-tag")
_, err = bundleStore.Store(ref, bndl)

idRef, err := FromBundle(bndl)
assert.NilError(t, err)

err = bundleStore.Remove(idRef)
assert.NilError(t, err)
bundles, err := bundleStore.List()
assert.NilError(t, err)
for _, bref := range bundles {
assert.Equal(t, bref == ref, false)
}
})
}
func TestLookUp(t *testing.T) {
dockerConfigDir := fs.NewDir(t, t.Name(), fs.WithMode(0755))
defer dockerConfigDir.Remove()
Expand Down