Skip to content

Commit

Permalink
feat: support deletion for OCI storage
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 committed Sep 25, 2023
1 parent cb8c8bc commit 8adc949
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions content/oci/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ func (s *Storage) Push(_ context.Context, expected ocispec.Descriptor, content i
return nil
}

// Delete removes the target from the system. With Delete, Storage implements the
// DeletableStorage interface.
func (s *Storage) Delete(ctx context.Context, target ocispec.Descriptor) error {
path, err := blobPath(target.Digest)
if err != nil {
return fmt.Errorf("%s: %s: %w", target.Digest, target.MediaType, errdef.ErrInvalidDigest)
}
targetPath := filepath.Join(s.root, path)
return os.Remove(targetPath)
}

// ingest write the content into a temporary ingest file.
func (s *Storage) ingest(expected ocispec.Descriptor, content io.Reader) (path string, ingestErr error) {
if err := ensureDir(s.ingestRoot); err != nil {
Expand Down
36 changes: 36 additions & 0 deletions content/oci/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,39 @@ func TestStorage_Fetch_Concurrent(t *testing.T) {
t.Fatal(err)
}
}

func TestStorage_Delete(t *testing.T) {
content := []byte("test delete")
desc := ocispec.Descriptor{
MediaType: "test",
Digest: digest.FromBytes(content),
Size: int64(len(content)),
}
tempDir := t.TempDir()
s, err := NewStorage(tempDir)
if err != nil {
t.Fatal("New() error =", err)
}
ctx := context.Background()
if err := s.Push(ctx, desc, bytes.NewReader(content)); err != nil {
t.Fatal("Storage.Push() error =", err)
}
exists, err := s.Exists(ctx, desc)
if err != nil {
t.Fatal("Storage.Exists() error =", err)
}
if !exists {
t.Errorf("Storage.Exists() = %v, want %v", exists, true)
}
err = s.Delete(ctx, desc)
if err != nil {
t.Fatal("Storage.Delete() error =", err)
}
exists, err = s.Exists(ctx, desc)
if err != nil {
t.Fatal("Storage.Exists() error =", err)
}
if exists {
t.Errorf("Storage.Exists() = %v, want %v", exists, false)
}
}

0 comments on commit 8adc949

Please sign in to comment.