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

Commit cbf819c

Browse files
authored
Merge pull request #717 from jcsirot/fix-handling-short-id
Fix handling short id in docker app commands
2 parents 6267dc4 + 280f562 commit cbf819c

File tree

11 files changed

+497
-76
lines changed

11 files changed

+497
-76
lines changed

e2e/images_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Deleted: b-simple-app:latest`,
9797
cmd.Command = dockerCli.Command("app", "image", "rm", "b-simple-app")
9898
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
9999
ExitCode: 1,
100-
Err: `Error: no such image b-simple-app:latest`,
100+
Err: `b-simple-app:latest: reference not found`,
101101
})
102102

103103
expectedOutput := "APP IMAGE APP NAME\n"
@@ -141,28 +141,28 @@ a-simple-app:latest simple
141141
dockerAppImageTag("a-simple-app$2", "b-simple-app")
142142
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
143143
ExitCode: 1,
144-
Err: `could not parse 'a-simple-app$2' as a valid reference`,
144+
Err: `could not parse "a-simple-app$2" as a valid reference`,
145145
})
146146

147147
// with invalid target reference
148148
dockerAppImageTag("a-simple-app", "b@simple-app")
149149
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
150150
ExitCode: 1,
151-
Err: `could not parse 'b@simple-app' as a valid reference`,
151+
Err: `could not parse "b@simple-app" as a valid reference`,
152152
})
153153

154154
// with unexisting source image
155155
dockerAppImageTag("b-simple-app", "target")
156156
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
157157
ExitCode: 1,
158-
Err: `could not tag 'b-simple-app': no such App image`,
158+
Err: `could not tag "b-simple-app": no such App image`,
159159
})
160160

161161
// with unexisting source tag
162162
dockerAppImageTag("a-simple-app:not-a-tag", "target")
163163
icmd.RunCmd(cmd).Assert(t, icmd.Expected{
164164
ExitCode: 1,
165-
Err: `could not tag 'a-simple-app:not-a-tag': no such App image`,
165+
Err: `could not tag "a-simple-app:not-a-tag": no such App image`,
166166
})
167167

168168
// tag image with only names

internal/cnab/cnab.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/docker/cli/cli/command"
1616
"github.com/docker/cnab-to-oci/remotes"
1717
"github.com/docker/distribution/reference"
18+
"github.com/sirupsen/logrus"
1819
)
1920

2021
type nameKind uint
@@ -90,23 +91,34 @@ func ResolveBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name
9091

9192
// GetBundle searches for the bundle locally and tries to pull it if not found
9293
func GetBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name string) (*bundle.Bundle, reference.Reference, error) {
93-
ref, err := store.StringToRef(name)
94+
bndl, ref, err := getBundleFromStore(bundleStore, name)
9495
if err != nil {
96+
named, err := store.StringToNamedRef(name)
97+
if err != nil {
98+
return nil, nil, err
99+
}
100+
fmt.Fprintf(dockerCli.Err(), "Unable to find App image %q locally\n", reference.FamiliarString(named))
101+
fmt.Fprintf(dockerCli.Out(), "Pulling from registry...\n")
102+
bndl, err = PullBundle(dockerCli, bundleStore, named)
103+
if err != nil {
104+
return nil, nil, err
105+
}
106+
ref = named
107+
}
108+
return bndl, ref, nil
109+
}
110+
111+
func getBundleFromStore(bundleStore appstore.BundleStore, name string) (*bundle.Bundle, reference.Reference, error) {
112+
ref, err := bundleStore.LookUp(name)
113+
if err != nil {
114+
logrus.Debugf("Unable to find reference %q in the bundle store", name)
95115
return nil, nil, err
96116
}
97117
bndl, err := bundleStore.Read(ref)
98118
if err != nil {
99-
fmt.Fprintf(dockerCli.Err(), "Unable to find App image %q locally\n", reference.FamiliarString(ref))
100-
101-
fmt.Fprintf(dockerCli.Out(), "Pulling from registry...\n")
102-
if named, ok := ref.(reference.Named); ok {
103-
bndl, err = PullBundle(dockerCli, bundleStore, named)
104-
if err != nil {
105-
return nil, nil, err
106-
}
107-
}
119+
logrus.Debugf("Unable to read bundle %q from store", reference.FamiliarString(ref))
120+
return nil, nil, err
108121
}
109-
110122
return bndl, ref, nil
111123
}
112124

internal/commands/image/list_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func (b *bundleStoreStubForListCmd) Remove(ref reference.Reference) error {
4848
return nil
4949
}
5050

51+
func (b *bundleStoreStubForListCmd) LookUp(refOrID string) (reference.Reference, error) {
52+
return nil, nil
53+
}
54+
5155
func TestListWithQuietFlag(t *testing.T) {
5256
var buf bytes.Buffer
5357
w := bufio.NewWriter(&buf)

internal/commands/image/rm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ $ docker app image rm 34be4a0c5f50`,
4848
}
4949

5050
func runRm(bundleStore store.BundleStore, app string) error {
51-
ref, err := store.StringToRef(app)
51+
ref, err := bundleStore.LookUp(app)
5252
if err != nil {
5353
return err
5454
}

internal/commands/image/tag.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/docker/app/internal/store"
88
"github.com/docker/cli/cli"
99
"github.com/docker/cli/cli/config"
10+
"github.com/pkg/errors"
1011
"github.com/spf13/cobra"
1112
)
1213

@@ -46,20 +47,26 @@ func runTag(bundleStore store.BundleStore, srcAppImage, destAppImage string) err
4647
}
4748

4849
func readBundle(name string, bundleStore store.BundleStore) (*bundle.Bundle, error) {
49-
cnabRef, err := store.StringToRef(name)
50+
cnabRef, err := bundleStore.LookUp(name)
5051
if err != nil {
51-
return nil, err
52+
switch err.(type) {
53+
case *store.UnknownReferenceError:
54+
return nil, fmt.Errorf("could not tag %q: no such App image", name)
55+
default:
56+
return nil, errors.Wrapf(err, "could not tag %q", name)
57+
}
58+
5259
}
5360

5461
bundle, err := bundleStore.Read(cnabRef)
5562
if err != nil {
56-
return nil, fmt.Errorf("could not tag '%s': no such App image", name)
63+
return nil, errors.Wrapf(err, "could not tag %q: no such App image", name)
5764
}
5865
return bundle, nil
5966
}
6067

6168
func storeBundle(bundle *bundle.Bundle, name string, bundleStore store.BundleStore) error {
62-
cnabRef, err := store.StringToRef(name)
69+
cnabRef, err := store.StringToNamedRef(name)
6370
if err != nil {
6471
return err
6572
}

internal/commands/image/tag_test.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ import (
1010
"github.com/docker/distribution/reference"
1111
)
1212

13+
func parseRefOrDie(t *testing.T, ref string) reference.Named {
14+
t.Helper()
15+
named, err := reference.ParseNormalizedNamed(ref)
16+
assert.NilError(t, err)
17+
return named
18+
}
19+
1320
type bundleStoreStub struct {
1421
ReadBundle *bundle.Bundle
1522
ReadError error
1623
StoredBundle string
1724
StoredError error
1825
StoredID reference.Digested
26+
LookUpRef reference.Reference
27+
LookUpError error
1928
}
2029

2130
func (b *bundleStoreStub) Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Digested, error) {
@@ -44,15 +53,25 @@ func (b *bundleStoreStub) Remove(ref reference.Reference) error {
4453
return nil
4554
}
4655

56+
func (b *bundleStoreStub) LookUp(refOrID string) (reference.Reference, error) {
57+
defer func() {
58+
b.LookUpRef = nil
59+
b.LookUpError = nil
60+
}()
61+
return b.LookUpRef, b.LookUpError
62+
}
63+
4764
var mockedBundleStore = &bundleStoreStub{}
4865

4966
func TestInvalidSourceReference(t *testing.T) {
5067
// given a bad source image reference
5168
const badRef = "b@d reference"
69+
// and given bundle store will return an error on LookUp
70+
mockedBundleStore.LookUpError = fmt.Errorf("error from bundleStore.LookUp")
5271

5372
err := runTag(mockedBundleStore, badRef, "")
5473

55-
assert.ErrorContains(t, err, fmt.Sprintf("could not parse '%s' as a valid reference", badRef))
74+
assert.Assert(t, err != nil)
5675
}
5776

5877
func TestUnexistingSource(t *testing.T) {
@@ -67,18 +86,20 @@ func TestUnexistingSource(t *testing.T) {
6786
}
6887

6988
func TestInvalidDestinationReference(t *testing.T) {
70-
// given a bundle is returned by bundleStore.Read
89+
// given a reference and a bundle is returned by bundleStore.LookUp and bundleStore.Read
90+
mockedBundleStore.LookUpRef = parseRefOrDie(t, "ref")
7191
mockedBundleStore.ReadBundle = &bundle.Bundle{}
7292
// and given a bad destination reference
7393
const badRef = "b@d reference"
7494

7595
err := runTag(mockedBundleStore, "ref", badRef)
7696

77-
assert.ErrorContains(t, err, fmt.Sprintf("could not parse '%s' as a valid reference", badRef))
97+
assert.ErrorContains(t, err, fmt.Sprintf("invalid reference format"))
7898
}
7999

80100
func TestBundleNotStored(t *testing.T) {
81-
// given a bundle is returned by bundleStore.Read
101+
// given a reference and a bundle is returned by bundleStore.LookUp and bundleStore.Read
102+
mockedBundleStore.LookUpRef = parseRefOrDie(t, "src-app")
82103
mockedBundleStore.ReadBundle = &bundle.Bundle{}
83104
// and given bundleStore.Store will return an error
84105
mockedBundleStore.StoredError = fmt.Errorf("error from bundleStore.Store")
@@ -89,7 +110,8 @@ func TestBundleNotStored(t *testing.T) {
89110
}
90111

91112
func TestSuccessfulyTag(t *testing.T) {
92-
// given a bundle is returned by bundleStore.Read
113+
// given a reference and a bundle is returned by bundleStore.LookUp and bundleStore.Read
114+
mockedBundleStore.LookUpRef = parseRefOrDie(t, "src-app")
93115
mockedBundleStore.ReadBundle = &bundle.Bundle{}
94116
// and given valid source and output references
95117
const (

internal/store/app.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (a ApplicationStore) BundleStore() (BundleStore, error) {
7474
if err := os.MkdirAll(path, 0755); err != nil {
7575
return nil, errors.Wrapf(err, "failed to create bundle store directory %q", path)
7676
}
77-
return &bundleStore{path: path}, nil
77+
return NewBundleStore(path)
7878
}
7979

8080
func makeDigestedDirectory(context string) string {

0 commit comments

Comments
 (0)