diff --git a/pkg/crane/crane_test.go b/pkg/crane/crane_test.go index 6bf14d5ea..b325ff1d6 100644 --- a/pkg/crane/crane_test.go +++ b/pkg/crane/crane_test.go @@ -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. @@ -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) { diff --git a/pkg/registry/manifest.go b/pkg/registry/manifest.go index a22af346a..6a08f5b43 100644 --- a/pkg/registry/manifest.go +++ b/pkg/registry/manifest.go @@ -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 ®Error{ + Status: http.StatusNotFound, + Code: "NAME_UNKNOWN", + Message: "Unknown name", + } + } + + _, ok := m.manifests[repo][target] + if !ok { + return ®Error{ + Status: http.StatusNotFound, + Code: "MANIFEST_UNKNOWN", + Message: "Unknown manifest", + } + } + + delete(m.manifests[repo], target) + resp.WriteHeader(http.StatusAccepted) + return nil + } + return ®Error{ Status: http.StatusBadRequest, Code: "METHOD_UNKNOWN", diff --git a/pkg/registry/registry_test.go b/pkg/registry/registry_test.go index dfe51a186..e72eb98bc 100644 --- a/pkg/registry/registry_test.go +++ b/pkg/registry/registry_test.go @@ -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 {