Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 27a4e6e

Browse files
Merge pull request #740 from jcsirot/fix-remove-by-id
Fix remove image by Id
2 parents b0bb564 + 26cc7d3 commit 27a4e6e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

internal/store/bundle.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ func (b *bundleStore) List() ([]reference.Reference, error) {
112112

113113
// Remove removes a bundle from the bundle store.
114114
func (b *bundleStore) Remove(ref reference.Reference) error {
115+
if id, ok := ref.(ID); ok {
116+
if len(b.refsMap[id]) == 0 {
117+
return fmt.Errorf("no such image %q", reference.FamiliarString(ref))
118+
} else if len(b.refsMap[id]) > 1 {
119+
return fmt.Errorf("unable to delete %q - App is referenced in multiple repositories", reference.FamiliarString(ref))
120+
}
121+
ref = b.refsMap[id][0]
122+
}
115123
path, err := b.storePath(ref)
116124
if err != nil {
117125
return err

internal/store/bundle_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,52 @@ func TestRemove(t *testing.T) {
295295
})
296296
}
297297

298+
func TestRemoveById(t *testing.T) {
299+
dockerConfigDir := fs.NewDir(t, t.Name(), fs.WithMode(0755))
300+
defer dockerConfigDir.Remove()
301+
appstore, err := NewApplicationStore(dockerConfigDir.Path())
302+
assert.NilError(t, err)
303+
bundleStore, err := appstore.BundleStore()
304+
assert.NilError(t, err)
305+
306+
t.Run("error when id does not exist", func(t *testing.T) {
307+
idRef, err := FromBundle(relocated.FromBundle(&bundle.Bundle{Name: "not-stored-bundle-name"}))
308+
assert.NilError(t, err)
309+
310+
err = bundleStore.Remove(idRef)
311+
assert.Equal(t, err.Error(), fmt.Sprintf("no such image %q", reference.FamiliarString(idRef)))
312+
})
313+
314+
t.Run("error on multiple repositories", func(t *testing.T) {
315+
bndl := relocated.FromBundle(&bundle.Bundle{Name: "bundle-name"})
316+
idRef, err := FromBundle(bndl)
317+
assert.NilError(t, err)
318+
_, err = bundleStore.Store(idRef, bndl)
319+
assert.NilError(t, err)
320+
_, err = bundleStore.Store(parseRefOrDie(t, "my-repo/a-bundle:my-tag"), bndl)
321+
assert.NilError(t, err)
322+
323+
err = bundleStore.Remove(idRef)
324+
assert.Equal(t, err.Error(), fmt.Sprintf("unable to delete %q - App is referenced in multiple repositories", reference.FamiliarString(idRef)))
325+
})
326+
327+
t.Run("success when only one reference exists", func(t *testing.T) {
328+
bndl := relocated.FromBundle(&bundle.Bundle{Name: "other-bundle-name"})
329+
ref := parseRefOrDie(t, "my-repo/other-bundle:my-tag")
330+
_, err = bundleStore.Store(ref, bndl)
331+
332+
idRef, err := FromBundle(bndl)
333+
assert.NilError(t, err)
334+
335+
err = bundleStore.Remove(idRef)
336+
assert.NilError(t, err)
337+
bundles, err := bundleStore.List()
338+
assert.NilError(t, err)
339+
for _, bref := range bundles {
340+
assert.Equal(t, bref == ref, false)
341+
}
342+
})
343+
}
298344
func TestLookUp(t *testing.T) {
299345
dockerConfigDir := fs.NewDir(t, t.Name(), fs.WithMode(0755))
300346
defer dockerConfigDir.Remove()

0 commit comments

Comments
 (0)