Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update Referrers API for distribution-spec v1.1.0-rc3 #553

Merged
merged 10 commits into from
Jul 25, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
filter test
Signed-off-by: Lixia (Sylvia) Lei <lixlei@microsoft.com>
  • Loading branch information
Wwwsylvia committed Jul 24, 2023
commit 4388126d1a49ffdfef9bdd8d5f0b9fd7e06301eb
91 changes: 85 additions & 6 deletions registry/remote/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1847,7 +1847,6 @@ func TestRepository_Referrers_RepositoryNotFound(t *testing.T) {
}
}

// TODO: more tests
func TestRepository_Referrers_ServerFiltering(t *testing.T) {
manifest := []byte(`{"layers":[]}`)
manifestDesc := ocispec.Descriptor{
Expand Down Expand Up @@ -1894,7 +1893,7 @@ func TestRepository_Referrers_ServerFiltering(t *testing.T) {
},
}

// Test with annotations only
// Test with filter annotations only
var ts *httptest.Server
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := "/v2/test/referrers/" + manifestDesc.Digest.String()
Expand Down Expand Up @@ -1933,7 +1932,7 @@ func TestRepository_Referrers_ServerFiltering(t *testing.T) {
},
MediaType: ocispec.MediaTypeImageIndex,
Manifests: referrers,
// set annotations
// set filter annotations
Annotations: map[string]string{
spec.AnnotationReferrersFiltersApplied: "artifactType",
},
Expand Down Expand Up @@ -1974,7 +1973,7 @@ func TestRepository_Referrers_ServerFiltering(t *testing.T) {
t.Errorf("fn invoked %d time(s), want %d", index, len(referrerSet))
}

// Test with header only
// Test with filter header only
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := "/v2/test/referrers/" + manifestDesc.Digest.String()
queryParams, err := url.ParseQuery(r.URL.RawQuery)
Expand Down Expand Up @@ -2013,7 +2012,88 @@ func TestRepository_Referrers_ServerFiltering(t *testing.T) {
MediaType: ocispec.MediaTypeImageIndex,
Manifests: referrers,
}
// set header
// set filter header
w.Header().Set("OCI-Filters-Applied", "artifactType")
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("failed to write response: %v", err)
}
}))
defer ts.Close()
uri, err = url.Parse(ts.URL)
if err != nil {
t.Fatalf("invalid test http server: %v", err)
}

repo, err = NewRepository(uri.Host + "/test")
if err != nil {
t.Fatalf("NewRepository() error = %v", err)
}
repo.PlainHTTP = true
repo.ReferrerListPageSize = 2

ctx = context.Background()
index = 0
if err := repo.Referrers(ctx, manifestDesc, "application/vnd.test", func(got []ocispec.Descriptor) error {
if index >= len(referrerSet) {
t.Fatalf("out of index bound: %d", index)
}
referrers := referrerSet[index]
index++
if !reflect.DeepEqual(got, referrers) {
t.Errorf("Repository.Referrers() = %v, want %v", got, referrers)
}
return nil
}); err != nil {
t.Errorf("Repository.Referrers() error = %v", err)
}
if index != len(referrerSet) {
t.Errorf("fn invoked %d time(s), want %d", index, len(referrerSet))
}

// Test with both filter annotation and filter header
ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
path := "/v2/test/referrers/" + manifestDesc.Digest.String()
queryParams, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
t.Fatal("failed to parse url query")
}
if r.Method != http.MethodGet ||
r.URL.Path != path ||
reflect.DeepEqual(queryParams["artifactType"], []string{"application%2Fvnd.test"}) {
t.Errorf("unexpected access: %s %q", r.Method, r.URL)
w.WriteHeader(http.StatusNotFound)
return
}
q := r.URL.Query()
n, err := strconv.Atoi(q.Get("n"))
if err != nil || n != 2 {
t.Errorf("bad page size: %s", q.Get("n"))
w.WriteHeader(http.StatusBadRequest)
return
}
var referrers []ocispec.Descriptor
switch q.Get("test") {
case "foo":
referrers = referrerSet[1]
w.Header().Set("Link", fmt.Sprintf(`<%s%s?n=2&test=bar>; rel="next"`, ts.URL, path))
case "bar":
referrers = referrerSet[2]
default:
referrers = referrerSet[0]
w.Header().Set("Link", fmt.Sprintf(`<%s?n=2&test=foo>; rel="next"`, path))
}
result := ocispec.Index{
Versioned: specs.Versioned{
SchemaVersion: 2, // historical value. does not pertain to OCI or docker version
},
MediaType: ocispec.MediaTypeImageIndex,
Manifests: referrers,
// set filter annotation
Annotations: map[string]string{
spec.AnnotationReferrersFiltersApplied: "artifactType",
},
}
// set filter header
w.Header().Set("OCI-Filters-Applied", "artifactType")
if err := json.NewEncoder(w).Encode(result); err != nil {
t.Errorf("failed to write response: %v", err)
Expand Down Expand Up @@ -2052,7 +2132,6 @@ func TestRepository_Referrers_ServerFiltering(t *testing.T) {
}
}

// TODO: more tests
func TestRepository_Referrers_ClientFiltering(t *testing.T) {
manifest := []byte(`{"layers":[]}`)
manifestDesc := ocispec.Descriptor{
Expand Down