Skip to content

Commit

Permalink
feat: Add Untag() to oci.Store.
Browse files Browse the repository at this point in the history
Signed-off-by: Francis Laniel <[email protected]>
  • Loading branch information
eiffel-fl committed Dec 15, 2023
1 parent 15ee0be commit f3249fa
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
23 changes: 23 additions & 0 deletions content/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,29 @@ func (s *Store) Resolve(ctx context.Context, reference string) (ocispec.Descript
return desc, nil
}

func (s *Store) Untag(ctx context.Context, reference string) error {
if reference == "" {
return errdef.ErrMissingReference
}

s.sync.RLock()
defer s.sync.RUnlock()

desc, err := s.tagResolver.Resolve(ctx, reference)
if err != nil {
return fmt.Errorf("resolving reference %q: %w", reference, err)
}
if reference == desc.Digest.String() {
return fmt.Errorf("reference %q is a digest and not a tag: %w", reference, errdef.ErrInvalidReference)
}

s.tagResolver.Untag(reference)
if s.AutoSaveIndex {
return s.saveIndex()
}
return nil
}

// Predecessors returns the nodes directly pointing to the current node.
// Predecessors returns nil without error if the node does not exists in the
// store.
Expand Down
45 changes: 45 additions & 0 deletions content/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,19 @@ func TestStore_Success(t *testing.T) {
if !bytes.Equal(got, manifest) {
t.Errorf("Store.Fetch() = %v, want %v", got, manifest)
}

// test untag
err = s.Untag(ctx, ref)
if err != nil {
t.Fatal("Store.Untag() error =", err)
}
gotDesc, err = s.Resolve(ctx, ref)
if err == nil {
t.Fatalf("error expected as reference was untagged, got %v", gotDesc)
}
if got, want := len(internalResolver.Map()), 1; got != want {
t.Errorf("resolver.Map() = %v, want %v", got, want)
}
}

func TestStore_RelativeRoot_Success(t *testing.T) {
Expand Down Expand Up @@ -333,6 +346,19 @@ func TestStore_RelativeRoot_Success(t *testing.T) {
if !bytes.Equal(got, manifest) {
t.Errorf("Store.Fetch() = %v, want %v", got, manifest)
}

// test untag
err = s.Untag(ctx, ref)
if err != nil {
t.Fatal("Store.Untag() error =", err)
}
gotDesc, err = s.Resolve(ctx, ref)
if err == nil {
t.Fatalf("error expected as reference was untagged, got %v", gotDesc)
}
if got, want := len(internalResolver.Map()), 1; got != want {
t.Errorf("resolver.Map() = %v, want %v", got, want)
}
}

func TestStore_NotExistingRoot(t *testing.T) {
Expand Down Expand Up @@ -654,6 +680,25 @@ func TestStore_DisableAutoSaveIndex(t *testing.T) {
if _, err := os.Stat(s.indexPath); err != nil {
t.Errorf("error: %s does not exist", s.indexPath)
}

// test untag
err = s.Untag(ctx, ref)
if err != nil {
t.Fatal("Store.Untag() error =", err)
}
if got, want := len(internalResolver.Map()), 1; got != want {
t.Errorf("resolver.Map() = %v, want %v", got, want)
}
if got, want := len(s.index.Manifests), 1; got != want {
t.Errorf("len(index.Manifests) = %v, want %v", got, want)
}
if err := s.saveIndex(); err != nil {
t.Fatal("Store.SaveIndex() error =", err)
}
// test index file again
if got, want := len(s.index.Manifests), 1; got != want {
t.Errorf("len(index.Manifests) = %v, want %v", got, want)
}
}

func TestStore_RepeatTag(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions content/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ type TagResolver interface {
Tagger
Resolver
}

// Untagger untags reference tags.
type Untagger interface {
// Untag untags the given reference string.
Untag(ctx context.Context, reference string) error
}

0 comments on commit f3249fa

Please sign in to comment.