Skip to content

Commit

Permalink
Add storage.Transport.NewStoreReference
Browse files Browse the repository at this point in the history
To allow callers to create storage reference without having to
parse strings.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Feb 10, 2020
1 parent 0c46986 commit b8cb494
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
8 changes: 8 additions & 0 deletions storage/storage_reference.go
Expand Up @@ -30,6 +30,14 @@ func newReference(transport storageTransport, named reference.Named, id string)
if named == nil && id == "" {
return nil, ErrInvalidReference
}
if named != nil && reference.IsNameOnly(named) {
return nil, errors.Wrapf(ErrInvalidReference, "reference %s has neither a tag nor a digest", named.String())
}
if id != "" {
if err := validateImageID(id); err != nil {
return nil, errors.Wrapf(ErrInvalidReference, "invalid ID value %q: %v", id, err)
}
}
// We take a copy of the transport, which contains a pointer to the
// store that it used for resolving this reference, so that the
// transport that we'll return from Transport() won't be affected by
Expand Down
7 changes: 7 additions & 0 deletions storage/storage_reference_test.go
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/containers/image/v5/docker/reference"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -18,6 +19,12 @@ func TestNewReference(t *testing.T) {
// Success is tested throughout; test only the failure
_, err := newReference(*st, nil, "")
assert.Error(t, err)
_, err = newReference(*st, nil, "ab")
assert.Error(t, err)
ref, err := reference.ParseNormalizedNamed("busybox")
require.NoError(t, err)
_, err = newReference(*st, ref, "")
assert.Error(t, err)
}

func TestStorageReferenceTransport(t *testing.T) {
Expand Down
11 changes: 10 additions & 1 deletion storage/storage_transport.go
Expand Up @@ -54,6 +54,9 @@ type StoreTransport interface {
// ParseStoreReference parses a reference, overriding any store
// specification that it may contain.
ParseStoreReference(store storage.Store, reference string) (*storageReference, error)
// NewStoreReference creates a reference for (named@ID) in store.
// either of name or ID can be unset; named must not be a reference.IsNameOnly.
NewStoreReference(store storage.Store, named reference.Named, id string) (*storageReference, error)
// SetDefaultUIDMap sets the default UID map to use when opening stores.
SetDefaultUIDMap(idmap []idtools.IDMap)
// SetDefaultGIDMap sets the default GID map to use when opening stores.
Expand Down Expand Up @@ -174,14 +177,20 @@ func (s storageTransport) ParseStoreReference(store storage.Store, ref string) (
named = reference.TagNameOnly(named)
}

result, err := newReference(storageTransport{store: store, defaultUIDMap: s.defaultUIDMap, defaultGIDMap: s.defaultGIDMap}, named, id)
result, err := s.NewStoreReference(store, named, id)
if err != nil {
return nil, err
}
logrus.Debugf("parsed reference into %q", result.StringWithinTransport())
return result, nil
}

// NewStoreReference creates a reference for (named@ID) in store.
// either of name or ID can be unset; named must not be a reference.IsNameOnly.
func (s *storageTransport) NewStoreReference(store storage.Store, named reference.Named, id string) (*storageReference, error) {
return newReference(storageTransport{store: store, defaultUIDMap: s.defaultUIDMap, defaultGIDMap: s.defaultGIDMap}, named, id)
}

func (s *storageTransport) GetStore() (storage.Store, error) {
// Return the transport's previously-set store. If we don't have one
// of those, initialize one now.
Expand Down

0 comments on commit b8cb494

Please sign in to comment.