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

Commit

Permalink
Merge pull request #740 from jcsirot/fix-remove-by-id
Browse files Browse the repository at this point in the history
Fix remove image by Id
  • Loading branch information
silvin-lubecki authored Nov 14, 2019
2 parents b0bb564 + 26cc7d3 commit 27a4e6e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
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))
}
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

0 comments on commit 27a4e6e

Please sign in to comment.