Skip to content

Commit

Permalink
refactor internal func
Browse files Browse the repository at this point in the history
Signed-off-by: Lixia (Sylvia) Lei <lixlei@microsoft.com>
  • Loading branch information
Wwwsylvia committed Jul 24, 2023
1 parent 4388126 commit 103dd22
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 62 deletions.
18 changes: 0 additions & 18 deletions registry/remote/referrers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ package remote

import (
"errors"
"net/http"
"strings"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/internal/descriptor"
"oras.land/oras-go/v2/internal/spec"
)

// zeroDigest represents a digest that consists of zeros. zeroDigest is used
Expand Down Expand Up @@ -111,22 +109,6 @@ func buildReferrersTag(desc ocispec.Descriptor) string {
return alg + "-" + encoded
}

// isAnnotationsContainReferrersFilter checks annotations to see if requested
// is in the applied filter list.
// Reference: https://github.com/opencontainers/distribution-spec/blob/v1.1.0-rc1/spec.md#listing-referrers
func isAnnotationsContainReferrersFilter(annotations map[string]string, requested string) bool {
applied := annotations[spec.AnnotationReferrersFiltersApplied]
return isReferrersFilterApplied(applied, requested)
}

// isHeaderContainReferrersFilter checks headers of resp to see if requested
// is in the applied filter list.
// Reference: https://github.com/opencontainers/distribution-spec/blob/v1.1.0-rc3/spec.md#listing-referrers
func isHeaderContainReferrersFilter(resp *http.Response, requested string) bool {
applied := resp.Header.Get(headerOCIFiltersApplied)
return isReferrersFilterApplied(applied, requested)
}

// isReferrersFilterApplied checks if requsted is in the applied filter list.
func isReferrersFilterApplied(applied, requested string) bool {
if applied == "" || requested == "" {
Expand Down
74 changes: 34 additions & 40 deletions registry/remote/referrers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,65 +61,59 @@ func Test_buildReferrersTag(t *testing.T) {
}
}

func Test_isAnnotationsContainReferrersFilter(t *testing.T) {
func Test_isReferrersFilterApplied(t *testing.T) {
tests := []struct {
name string
annotations map[string]string
requested string
want bool
name string
applied string
requested string
want bool
}{
{
name: "single filter applied, specified filter matches",
annotations: map[string]string{spec.AnnotationReferrersFiltersApplied: "artifactType"},
requested: "artifactType",
want: true,
name: "single filter applied, specified filter matches",
applied: "artifactType",
requested: "artifactType",
want: true,
},
{
name: "single filter applied, specified filter does not match",
annotations: map[string]string{spec.AnnotationReferrersFiltersApplied: "foo"},
requested: "artifactType",
want: false,
name: "single filter applied, specified filter does not match",
applied: "foo",
requested: "artifactType",
want: false,
},
{
name: "multiple filters applied, specified filter matches",
annotations: map[string]string{spec.AnnotationReferrersFiltersApplied: "foo,artifactType"},
requested: "artifactType",
want: true,
name: "multiple filters applied, specified filter matches",
applied: "foo,artifactType",
requested: "artifactType",
want: true,
},
{
name: "multiple filters applied, specified filter does not match",
annotations: map[string]string{spec.AnnotationReferrersFiltersApplied: "foo,bar"},
requested: "artifactType",
want: false,
name: "multiple filters applied, specified filter does not match",
applied: "foo,bar",
requested: "artifactType",
want: false,
},
{
name: "single filter applied, specified filter empty",
annotations: map[string]string{spec.AnnotationReferrersFiltersApplied: "foo"},
requested: "",
want: false,
name: "single filter applied, no specified filter",
applied: "foo",
requested: "",
want: false,
},
{
name: "no filter applied",
annotations: map[string]string{},
requested: "artifactType",
want: false,
name: "no filter applied, specified filter does not match",
applied: "",
requested: "artifactType",
want: false,
},
{
name: "empty filter applied",
annotations: map[string]string{spec.AnnotationReferrersFiltersApplied: ""},
requested: "artifactType",
want: false,
},
{
name: "no filter applied, specified filter empty",
annotations: map[string]string{},
requested: "",
want: false,
name: "no filter applied, no specified filter",
applied: "",
requested: "",
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isAnnotationsContainReferrersFilter(tt.annotations, tt.requested); got != tt.want {
if got := isReferrersFilterApplied(tt.applied, tt.requested); got != tt.want {
t.Errorf("isReferrersFilterApplied() = %v, want %v", got, tt.want)
}
})
Expand Down
10 changes: 6 additions & 4 deletions registry/remote/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,12 @@ func (r *Repository) referrersPageByAPI(ctx context.Context, artifactType string
referrers := index.Manifests
if artifactType != "" {
requested := "artifactType"
// check both header and annotations for compatibility
// reference for header: https://github.com/opencontainers/distribution-spec/blob/v1.1.0-rc3/spec.md#listing-referrers
// reference for annotations: https://github.com/opencontainers/distribution-spec/blob/v1.1.0-rc1/spec.md#listing-referrers
if !isHeaderContainReferrersFilter(resp, requested) && !isAnnotationsContainReferrersFilter(index.Annotations, requested) {
// check both filters header and filters annotations for compatibility
// reference for filters header: https://github.com/opencontainers/distribution-spec/blob/v1.1.0-rc3/spec.md#listing-referrers
// reference for filters annotations: https://github.com/opencontainers/distribution-spec/blob/v1.1.0-rc1/spec.md#listing-referrers
filtersHeader := resp.Header.Get(headerOCIFiltersApplied)
filtersAnnotation := index.Annotations[spec.AnnotationReferrersFiltersApplied]
if !isReferrersFilterApplied(filtersHeader, requested) && !isReferrersFilterApplied(filtersAnnotation, requested) {
// perform client side filtering if the filter is not applied on the server side
referrers = filterReferrers(referrers, artifactType)
}
Expand Down

0 comments on commit 103dd22

Please sign in to comment.