Skip to content

Commit

Permalink
refactor: update implementation of Referrers API (#340)
Browse files Browse the repository at this point in the history
Signed-off-by: Lixia (Sylvia) Lei <lixlei@microsoft.com>
  • Loading branch information
Wwwsylvia authored Oct 17, 2022
1 parent 9945bfb commit 5416ed6
Show file tree
Hide file tree
Showing 11 changed files with 899 additions and 504 deletions.
18 changes: 8 additions & 10 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,17 @@ func TestMain(m *testing.M) {
if m == "GET" {
w.Write(exampleManifest)
}
case strings.Contains(p, "/artifacts/referrers"):
w.Header().Set("ORAS-Api-Version", "oras/1.0")
q := r.URL.Query()
case strings.Contains(p, "/v2/source/referrers/"):
var referrers []ocispec.Descriptor
if q.Get("digest") == exampleManifestDescriptor.Digest.String() {
if p == "/v2/source/referrers/"+exampleManifestDescriptor.Digest.String() {
referrers = []ocispec.Descriptor{exampleSignatureManifestDescriptor}
} else if q.Get("digest") == exampleSignatureManifestDescriptor.Digest.String() {
referrers = []ocispec.Descriptor{}
}
result := struct {
Referrers []ocispec.Descriptor `json:"referrers"`
}{
Referrers: referrers,
result := ocispec.Index{
Versioned: specs.Versioned{
SchemaVersion: 2, // historical value. does not pertain to OCI or docker version
},
MediaType: ocispec.MediaTypeImageIndex,
Manifests: referrers,
}
if err := json.NewEncoder(w).Encode(result); err != nil {
panic(err)
Expand Down
78 changes: 33 additions & 45 deletions extendedcopy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,18 @@ func TestExtendedCopyGraph_FilterAnnotationWithMultipleRegex_Referrers(t *testin
// set up test server
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
w.Header().Set("ORAS-Api-Version", "oras/1.0")
switch {
case p == "/v2/test/referrers/"+descs[0].Digest.String():
result := ocispec.Index{
Versioned: specs.Versioned{
SchemaVersion: 2, // historical value. does not pertain to OCI or docker version
},
MediaType: ocispec.MediaTypeImageIndex,
Manifests: descs[1:],
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("failed to write response: %v", err)
}
case strings.Contains(p, descs[0].Digest.String()):
w.Header().Set("Content-Type", ocispec.MediaTypeImageLayer)
w.Header().Set("Content-Digest", descs[0].Digest.String())
Expand Down Expand Up @@ -941,20 +951,6 @@ func TestExtendedCopyGraph_FilterAnnotationWithMultipleRegex_Referrers(t *testin
w.Header().Set("Content-Digest", descs[5].Digest.String())
w.Header().Set("Content-Length", strconv.Itoa(len(blobs[5])))
w.Write(blobs[5])
case strings.Contains(p, "referrers"):
q := r.URL.Query()
var referrers []ocispec.Descriptor
if q.Get("digest") == descs[0].Digest.String() {
referrers = descs[1:]
}
result := struct {
Referrers []ocispec.Descriptor `json:"referrers"`
}{
Referrers: referrers,
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("failed to write response: %v", err)
}
default:
t.Errorf("unexpected access: %s %s", r.Method, r.URL)
w.WriteHeader(http.StatusNotFound)
Expand Down Expand Up @@ -1336,8 +1332,18 @@ func TestExtendedCopyGraph_FilterArtifactTypeWithMultipleRegex_Referrers(t *test
// set up test server
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
w.Header().Set("ORAS-Api-Version", "oras/1.0")
switch {
case p == "/v2/test/referrers/"+descs[0].Digest.String():
result := ocispec.Index{
Versioned: specs.Versioned{
SchemaVersion: 2, // historical value. does not pertain to OCI or docker version
},
MediaType: ocispec.MediaTypeImageIndex,
Manifests: descs[1:],
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("failed to write response: %v", err)
}
case strings.Contains(p, descs[0].Digest.String()):
w.Header().Set("Content-Type", ocispec.MediaTypeImageLayer)
w.Header().Set("Content-Digest", descs[0].Digest.String())
Expand Down Expand Up @@ -1368,20 +1374,6 @@ func TestExtendedCopyGraph_FilterArtifactTypeWithMultipleRegex_Referrers(t *test
w.Header().Set("Content-Digest", descs[5].Digest.String())
w.Header().Set("Content-Length", strconv.Itoa(len(blobs[5])))
w.Write(blobs[5])
case strings.Contains(p, "referrers"):
q := r.URL.Query()
var referrers []ocispec.Descriptor
if q.Get("digest") == descs[0].Digest.String() {
referrers = descs[1:]
}
result := struct {
Referrers []ocispec.Descriptor `json:"referrers"`
}{
Referrers: referrers,
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("failed to write response: %v", err)
}
default:
t.Errorf("unexpected access: %s %s", r.Method, r.URL)
w.WriteHeader(http.StatusNotFound)
Expand Down Expand Up @@ -1581,8 +1573,18 @@ func TestExtendedCopyGraph_FilterArtifactTypeAndAnnotationWithMultipleRegex_Refe
// set up test server
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
p := r.URL.Path
w.Header().Set("ORAS-Api-Version", "oras/1.0")
switch {
case p == "/v2/test/referrers/"+descs[0].Digest.String():
result := ocispec.Index{
Versioned: specs.Versioned{
SchemaVersion: 2, // historical value. does not pertain to OCI or docker version
},
MediaType: ocispec.MediaTypeImageIndex,
Manifests: descs[1:],
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("failed to write response: %v", err)
}
case strings.Contains(p, descs[0].Digest.String()):
w.Header().Set("Content-Type", ocispec.MediaTypeImageLayer)
w.Header().Set("Content-Digest", descs[0].Digest.String())
Expand Down Expand Up @@ -1633,20 +1635,6 @@ func TestExtendedCopyGraph_FilterArtifactTypeAndAnnotationWithMultipleRegex_Refe
w.Header().Set("Content-Digest", descs[9].Digest.String())
w.Header().Set("Content-Length", strconv.Itoa(len(blobs[9])))
w.Write(blobs[9])
case strings.Contains(p, "referrers"):
q := r.URL.Query()
var referrers []ocispec.Descriptor
if q.Get("digest") == descs[0].Digest.String() {
referrers = descs[1:]
}
result := struct {
Referrers []ocispec.Descriptor `json:"referrers"`
}{
Referrers: referrers,
}
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("failed to write response: %v", err)
}
default:
t.Errorf("unexpected access: %s %s", r.Method, r.URL)
w.WriteHeader(http.StatusNotFound)
Expand Down
20 changes: 7 additions & 13 deletions registry/remote/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,51 +125,45 @@ func TestMain(m *testing.M) {
case p == fmt.Sprintf("/v2/%s/manifests/%s", exampleRepositoryName, exampleTag) && m == "PUT":
w.WriteHeader(http.StatusCreated)
case p == fmt.Sprintf("/v2/%s/manifests/%s", exampleRepositoryName, ManifestDigest) && m == "PUT":
w.Header().Set("ORAS-Api-Version", "oras/1.0")
w.WriteHeader(http.StatusCreated)
case p == fmt.Sprintf("/v2/%s/manifests/%s", exampleRepositoryName, ReferenceManifestDigest) && m == "PUT":
w.Header().Set("ORAS-Api-Version", "oras/1.0")
w.WriteHeader(http.StatusCreated)
case p == fmt.Sprintf("/v2/%s/manifests/%s", exampleRepositoryName, exampleSignatureManifestDescriptor.Digest) && m == "GET":
w.Header().Set("ORAS-Api-Version", "oras/1.0")
w.Header().Set("Content-Type", ocispec.MediaTypeArtifactManifest)
w.Header().Set("Content-Digest", string(exampleSignatureManifestDescriptor.Digest))
w.Header().Set("Content-Length", strconv.Itoa(len(exampleSignatureManifest)))
w.Write(exampleSignatureManifest)
case p == fmt.Sprintf("/v2/%s/manifests/%s", exampleRepositoryName, exampleSBoMManifestDescriptor.Digest) && m == "GET":
w.Header().Set("ORAS-Api-Version", "oras/1.0")
w.Header().Set("Content-Type", ocispec.MediaTypeArtifactManifest)
w.Header().Set("Content-Digest", string(exampleSBoMManifestDescriptor.Digest))
w.Header().Set("Content-Length", strconv.Itoa(len(exampleSBoMManifest)))
w.Write(exampleSBoMManifest)
case p == fmt.Sprintf("/v2/%s/manifests/%s", exampleRepositoryName, exampleManifestWithBlobsDescriptor.Digest) && m == "GET":
w.Header().Set("ORAS-Api-Version", "oras/1.0")
w.Header().Set("Content-Type", ocispec.MediaTypeArtifactManifest)
w.Header().Set("Content-Digest", string(exampleManifestWithBlobsDescriptor.Digest))
w.Header().Set("Content-Length", strconv.Itoa(len(exampleManifestWithBlobs)))
w.Write(exampleManifestWithBlobs)
case p == fmt.Sprintf("/v2/%s/blobs/%s", exampleRepositoryName, blobDescriptor.Digest) && m == "GET":
w.Header().Set("ORAS-Api-Version", "oras/1.0")
w.Header().Set("Content-Type", ocispec.MediaTypeArtifactManifest)
w.Header().Set("Content-Digest", string(blobDescriptor.Digest))
w.Header().Set("Content-Length", strconv.Itoa(len(blobContent)))
w.Write([]byte(blobContent))
case p == fmt.Sprintf("/v2/%s/_oras/artifacts/referrers", exampleRepositoryName):
case p == fmt.Sprintf("/v2/%s/referrers/%s", exampleRepositoryName, exampleManifestDescriptor.Digest.String()):
q := r.URL.Query()
var referrers []ocispec.Descriptor
switch q.Get("test") {
case "page1":
referrers = exampleReferrerDescriptors[1]
w.Header().Set("ORAS-Api-Version", "oras/1.0")
default:
referrers = exampleReferrerDescriptors[0]
w.Header().Set("ORAS-Api-Version", "oras/1.0")
w.Header().Set("Link", fmt.Sprintf(`<%s?n=1&test=page1>; rel="next"`, p))
}
result := struct {
Referrers []ocispec.Descriptor `json:"referrers"`
}{
Referrers: referrers,
result := ocispec.Index{
Versioned: specs.Versioned{
SchemaVersion: 2, // historical value. does not pertain to OCI or docker version
},
MediaType: ocispec.MediaTypeImageIndex,
Manifests: referrers,
}
if err := json.NewEncoder(w).Encode(result); err != nil {
panic(err)
Expand Down
2 changes: 0 additions & 2 deletions registry/remote/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"strings"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
artifactspec "github.com/oras-project/artifacts-spec/specs-go/v1"
"oras.land/oras-go/v2/internal/docker"
)

Expand All @@ -29,7 +28,6 @@ var defaultManifestMediaTypes = []string{
docker.MediaTypeManifestList,
ocispec.MediaTypeImageManifest,
ocispec.MediaTypeImageIndex,
artifactspec.MediaTypeArtifactManifest, // TODO: deprecate
ocispec.MediaTypeArtifactManifest,
}

Expand Down
Loading

0 comments on commit 5416ed6

Please sign in to comment.