Skip to content

Commit

Permalink
implement delete manifest tests (#915)
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Panato <ctadeu@gmail.com>
  • Loading branch information
cpanato committed Jan 19, 2021
1 parent 38d292e commit 213dd48
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/crane/crane_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/google/go-containerregistry/pkg/v1/remote"
)

// TODO(jonjohnsonjr): Test crane.Delete behavior.
// TODO(jonjohnsonjr): Test crane.ListTags behavior.
// TODO(jonjohnsonjr): Test crane.Catalog behavior.
// TODO(jonjohnsonjr): Test crane.Copy failures.
Expand Down Expand Up @@ -148,6 +147,30 @@ func TestCraneRegistry(t *testing.T) {
if err := compare.Layers(pulledLayer, layer); err != nil {
t.Fatal(err)
}

// Delete the non existing image
if err := crane.Delete("honk-image"); err == nil {
t.Fatal("wanted err, got nil")
}

// Delete the image
if err := crane.Delete(src); err != nil {
t.Fatal(err)
}

// check if the image was really deleted
if _, err := crane.Pull(src); err == nil {
t.Fatal("wanted err, got nil")
}

// check if the copied image still exist
dstPulled, err := crane.Pull(dst)
if err != nil {
t.Fatal(err)
}
if err := compare.Images(dstPulled, copied); err != nil {
t.Fatal(err)
}
}

func TestCraneCopyIndex(t *testing.T) {
Expand Down
26 changes: 26 additions & 0 deletions pkg/registry/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,32 @@ func (m *manifests) handle(resp http.ResponseWriter, req *http.Request) *regErro
resp.WriteHeader(http.StatusCreated)
return nil
}

if req.Method == "DELETE" {
m.lock.Lock()
defer m.lock.Unlock()
if _, ok := m.manifests[repo]; !ok {
return &regError{
Status: http.StatusNotFound,
Code: "NAME_UNKNOWN",
Message: "Unknown name",
}
}

_, ok := m.manifests[repo][target]
if !ok {
return &regError{
Status: http.StatusNotFound,
Code: "MANIFEST_UNKNOWN",
Message: "Unknown manifest",
}
}

delete(m.manifests[repo], target)
resp.WriteHeader(http.StatusAccepted)
return nil
}

return &regError{
Status: http.StatusBadRequest,
Code: "METHOD_UNKNOWN",
Expand Down
27 changes: 27 additions & 0 deletions pkg/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,33 @@ func TestCalls(t *testing.T) {
"Location": "/v2/foo/blobs/uploads/1",
},
},
{
Description: "DELETE Unknown name",
Method: "DELETE",
URL: "/v2/test/honk/manifests/latest",
Code: http.StatusNotFound,
},
{
Description: "DELETE Unknown manifest",
Manifests: map[string]string{"honk/manifests/latest": "honk"},
Method: "DELETE",
URL: "/v2/honk/manifests/tag-honk",
Code: http.StatusNotFound,
},
{
Description: "DELETE existing manifest",
Manifests: map[string]string{"foo/manifests/latest": "foo"},
Method: "DELETE",
URL: "/v2/foo/manifests/latest",
Code: http.StatusAccepted,
},
{
Description: "DELETE existing manifest by digest",
Manifests: map[string]string{"foo/manifests/latest": "foo"},
Method: "DELETE",
URL: "/v2/foo/manifests/sha256:" + sha256String("foo"),
Code: http.StatusAccepted,
},
}

for _, tc := range tcs {
Expand Down

0 comments on commit 213dd48

Please sign in to comment.