diff --git a/pkg/shortnames/shortnames.go b/pkg/shortnames/shortnames.go index e02703d77..198ac1cc6 100644 --- a/pkg/shortnames/shortnames.go +++ b/pkg/shortnames/shortnames.go @@ -225,9 +225,8 @@ func (c *PullCandidate) Record() error { // Note that tags and digests are stripped from the specified name before // looking up an alias. Stripped off tags and digests are later on appended to // all candidates. If neither tag nor digest is specified, candidates are -// normalized with the "latest" tag. PullCandidates in the returned value may -// be empty if there is no matching alias and no unqualified-search registries -// are configured. +// normalized with the "latest" tag. An error is returned if there is no +// matching alias and no unqualified-search registries are configured. // // Note that callers *must* call `(PullCandidate).Record` after a returned // item has been pulled successfully; this callback will record a new @@ -312,6 +311,10 @@ func Resolve(ctx *types.SystemContext, name string) (*Resolved, error) { if err != nil { return nil, err } + // Error out if there's no matching alias and no search registries. + if len(unqualifiedSearchRegistries) == 0 { + return nil, errors.Errorf("short-name %q did not resolve to an alias and no unqualified-search registries are defined in %q", name, usrConfig) + } resolved.originDescription = usrConfig for _, reg := range unqualifiedSearchRegistries { @@ -331,10 +334,8 @@ func Resolve(ctx *types.SystemContext, name string) (*Resolved, error) { return resolved, nil } - // If we have only one candidate, there's no ambiguity. In case of an - // empty candidate slices, callers can implement custom logic or raise - // an error. - if len(resolved.PullCandidates) <= 1 { + // If we have only one candidate, there's no ambiguity. + if len(resolved.PullCandidates) == 1 { return resolved, nil } diff --git a/pkg/shortnames/shortnames_test.go b/pkg/shortnames/shortnames_test.go index 7e0f30e5f..423f63764 100644 --- a/pkg/shortnames/shortnames_test.go +++ b/pkg/shortnames/shortnames_test.go @@ -141,12 +141,11 @@ func TestResolve(t *testing.T) { assert.False(t, resolved.PullCandidates[0].record) } - // Non-existent should return an empty slice as no search registries - // are configured in the config. + // Non-existent should return an error as no search registries are + // configured in the config. resolved, err := Resolve(sys, "dontexist") - require.NoError(t, err) - require.NotNil(t, resolved) - require.Len(t, resolved.PullCandidates, 0) + require.Error(t, err) + require.Nil(t, resolved) // An empty name is not valid. resolved, err = Resolve(sys, "") @@ -354,7 +353,7 @@ func TestResolveWithVaryingShortNameModes(t *testing.T) { {"testdata/one-reg.conf", types.ShortNameModePermissive, "repo/image", false, 1}, {"testdata/two-reg.conf", types.ShortNameModePermissive, "repo/image", false, 1}, // Permisive + no match -> search (no tty) - {"testdata/no-reg.conf", types.ShortNameModePermissive, "donotexist", false, 0}, + {"testdata/no-reg.conf", types.ShortNameModePermissive, "donotexist", true, 0}, {"testdata/one-reg.conf", types.ShortNameModePermissive, "donotexist", false, 1}, {"testdata/two-reg.conf", types.ShortNameModePermissive, "donotexist", false, 2}, // Disabled + match -> return alias @@ -362,7 +361,7 @@ func TestResolveWithVaryingShortNameModes(t *testing.T) { {"testdata/one-reg.conf", types.ShortNameModeDisabled, "repo/image", false, 1}, {"testdata/two-reg.conf", types.ShortNameModeDisabled, "repo/image", false, 1}, // Disabled + no match -> search - {"testdata/no-reg.conf", types.ShortNameModeDisabled, "donotexist", false, 0}, + {"testdata/no-reg.conf", types.ShortNameModeDisabled, "donotexist", true, 0}, {"testdata/one-reg.conf", types.ShortNameModeDisabled, "donotexist", false, 1}, {"testdata/two-reg.conf", types.ShortNameModeDisabled, "donotexist", false, 2}, // Enforcing + match -> return alias @@ -370,7 +369,7 @@ func TestResolveWithVaryingShortNameModes(t *testing.T) { {"testdata/one-reg.conf", types.ShortNameModeEnforcing, "repo/image", false, 1}, {"testdata/two-reg.conf", types.ShortNameModeEnforcing, "repo/image", false, 1}, // Enforcing + no match -> error if search regs > 1 and no tty - {"testdata/no-reg.conf", types.ShortNameModeEnforcing, "donotexist", false, 0}, + {"testdata/no-reg.conf", types.ShortNameModeEnforcing, "donotexist", true, 0}, {"testdata/one-reg.conf", types.ShortNameModeEnforcing, "donotexist", false, 1}, {"testdata/two-reg.conf", types.ShortNameModeEnforcing, "donotexist", true, 0}, }