-
Notifications
You must be signed in to change notification settings - Fork 112
feat: Referrers support for content.ReadOnlyGraphStorage #659
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7d537e9
feat: Referrers support for oci.Store
wangxiaoxuan273 7444a42
generic implementation
wangxiaoxuan273 b1205f4
resolved comments
wangxiaoxuan273 0b22ea7
added unit test
wangxiaoxuan273 a0dccad
changed the function signature
wangxiaoxuan273 7ec19a1
added test cases
wangxiaoxuan273 70afd23
removed empty line
wangxiaoxuan273 dea20f8
increase coverage
wangxiaoxuan273 4e6c66f
increase converage
wangxiaoxuan273 ba808c7
increase coverage
wangxiaoxuan273 d29c51a
increase coverage
wangxiaoxuan273 8b7299c
increase coverage
wangxiaoxuan273 404b39d
increase coverage
wangxiaoxuan273 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,291 @@ | ||
| /* | ||
| Copyright The ORAS Authors. | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package registry | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/opencontainers/go-digest" | ||
| ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
| "golang.org/x/sync/errgroup" | ||
| "oras.land/oras-go/v2/content/memory" | ||
| "oras.land/oras-go/v2/errdef" | ||
| "oras.land/oras-go/v2/internal/docker" | ||
| "oras.land/oras-go/v2/internal/spec" | ||
| ) | ||
|
|
||
| // testStorage implements content.ReadOnlyGraphStorage | ||
| type testStorage struct { | ||
| store *memory.Store | ||
| } | ||
|
|
||
| func (s *testStorage) Push(ctx context.Context, expected ocispec.Descriptor, reader io.Reader) error { | ||
| return s.store.Push(ctx, expected, reader) | ||
| } | ||
|
|
||
| func (s *testStorage) Fetch(ctx context.Context, target ocispec.Descriptor) (io.ReadCloser, error) { | ||
| return s.store.Fetch(ctx, target) | ||
| } | ||
|
|
||
| func (s *testStorage) Exists(ctx context.Context, target ocispec.Descriptor) (bool, error) { | ||
| return s.store.Exists(ctx, target) | ||
| } | ||
|
|
||
| func (s *testStorage) Predecessors(ctx context.Context, node ocispec.Descriptor) ([]ocispec.Descriptor, error) { | ||
| return s.store.Predecessors(ctx, node) | ||
| } | ||
|
wangxiaoxuan273 marked this conversation as resolved.
|
||
|
|
||
| // TestReferrerLister implements content.ReadOnlyGraphStorage and registry.ReferrerLister | ||
| type TestReferrerLister struct { | ||
| *testStorage | ||
| } | ||
|
|
||
| func (rl *TestReferrerLister) Referrers(ctx context.Context, desc ocispec.Descriptor, artifactType string, fn func(referrers []ocispec.Descriptor) error) error { | ||
| results := []ocispec.Descriptor{desc} | ||
| return fn(results) | ||
| } | ||
|
|
||
| func TestReferrers(t *testing.T) { | ||
|
wangxiaoxuan273 marked this conversation as resolved.
|
||
| s := testStorage{ | ||
| store: memory.New(), | ||
| } | ||
| ctx := context.Background() | ||
|
|
||
| // generate test content | ||
| var blobs [][]byte | ||
| var descs []ocispec.Descriptor | ||
| appendBlob := func(mediaType string, artifactType string, blob []byte) { | ||
| blobs = append(blobs, blob) | ||
| descs = append(descs, ocispec.Descriptor{ | ||
| MediaType: mediaType, | ||
| ArtifactType: artifactType, | ||
| Annotations: map[string]string{"test": "content"}, | ||
| Digest: digest.FromBytes(blob), | ||
| Size: int64(len(blob)), | ||
| }) | ||
| } | ||
| generateImageManifest := func(config ocispec.Descriptor, subject *ocispec.Descriptor, layers ...ocispec.Descriptor) { | ||
| manifest := ocispec.Manifest{ | ||
| MediaType: ocispec.MediaTypeImageManifest, | ||
| Config: config, | ||
| Subject: subject, | ||
| Layers: layers, | ||
| Annotations: map[string]string{"test": "content"}, | ||
| } | ||
| manifestJSON, err := json.Marshal(manifest) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| appendBlob(ocispec.MediaTypeImageManifest, manifest.Config.MediaType, manifestJSON) | ||
| } | ||
| generateArtifactManifest := func(subject *ocispec.Descriptor, blobs ...ocispec.Descriptor) { | ||
| artifact := spec.Artifact{ | ||
| MediaType: spec.MediaTypeArtifactManifest, | ||
| ArtifactType: "artifact", | ||
| Subject: subject, | ||
| Blobs: blobs, | ||
| Annotations: map[string]string{"test": "content"}, | ||
| } | ||
| manifestJSON, err := json.Marshal(artifact) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| appendBlob(spec.MediaTypeArtifactManifest, artifact.ArtifactType, manifestJSON) | ||
| } | ||
| generateIndex := func(subject *ocispec.Descriptor, manifests ...ocispec.Descriptor) { | ||
| index := ocispec.Index{ | ||
| MediaType: ocispec.MediaTypeImageIndex, | ||
| ArtifactType: "index", | ||
| Subject: subject, | ||
| Manifests: manifests, | ||
| Annotations: map[string]string{"test": "content"}, | ||
| } | ||
| indexJSON, err := json.Marshal(index) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| appendBlob(ocispec.MediaTypeImageIndex, index.ArtifactType, indexJSON) | ||
| } | ||
| generateManifestList := func(manifests ...ocispec.Descriptor) { | ||
| index := ocispec.Index{ | ||
| ArtifactType: "manifest list", | ||
| Manifests: manifests, | ||
| } | ||
| indexJSON, err := json.Marshal(index) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| appendBlob(docker.MediaTypeManifestList, index.ArtifactType, indexJSON) | ||
| } | ||
|
|
||
| appendBlob("image manifest", "image config", []byte("config")) // Blob 0 | ||
| appendBlob(ocispec.MediaTypeImageLayer, "layer", []byte("foo")) // Blob 1 | ||
| appendBlob(ocispec.MediaTypeImageLayer, "layer", []byte("bar")) // Blob 2 | ||
| appendBlob(ocispec.MediaTypeImageLayer, "layer", []byte("hello")) // Blob 3 | ||
| generateImageManifest(descs[0], nil, descs[1]) // Blob 4 | ||
| generateArtifactManifest(&descs[4], descs[2]) // Blob 5 | ||
| generateImageManifest(descs[0], &descs[5], descs[3]) // Blob 6 | ||
| generateIndex(&descs[6], descs[4:6]...) // Blob 7 | ||
| generateIndex(&descs[4], descs[5:8]...) // Blob 8 | ||
| generateManifestList(descs[4:7]...) // blob 9 | ||
| generateImageManifest(descs[0], nil, descs[4]) // Blob 10 | ||
| generateArtifactManifest(nil, descs[5]) // Blob 11 | ||
|
|
||
| eg, egCtx := errgroup.WithContext(ctx) | ||
| for i := range blobs { | ||
| eg.Go(func(i int) func() error { | ||
| return func() error { | ||
| err := s.Push(egCtx, descs[i], bytes.NewReader(blobs[i])) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to push test content to src: %d: %v", i, err) | ||
| } | ||
| return nil | ||
| } | ||
| }(i)) | ||
| } | ||
| if err := eg.Wait(); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // verify predecessors | ||
| wantedPredecessors := [][]ocispec.Descriptor{ | ||
| {descs[4], descs[6], descs[10]}, // Blob 0 | ||
| {descs[4]}, // Blob 1 | ||
| {descs[5]}, // Blob 2 | ||
| {descs[6]}, // Blob 3 | ||
| {descs[5], descs[7], descs[8], descs[9], descs[10]}, // Blob 4 | ||
| {descs[6], descs[7], descs[8], descs[9], descs[11]}, // Blob 5 | ||
| {descs[7], descs[8], descs[9]}, // Blob 6 | ||
| {descs[8]}, // Blob 7 | ||
| nil, // Blob 8 | ||
| nil, // Blob 9 | ||
| } | ||
| for i, want := range wantedPredecessors { | ||
| predecessors, err := s.Predecessors(ctx, descs[i]) | ||
| if err != nil { | ||
| t.Errorf("Store.Predecessors(%d) error = %v", i, err) | ||
| } | ||
| if !equalDescriptorSet(predecessors, want) { | ||
| t.Errorf("Store.Predecessors(%d) = %v, want %v", i, predecessors, want) | ||
| } | ||
| } | ||
|
|
||
| // verify referrers | ||
| wantedReferrers := [][]ocispec.Descriptor{ | ||
| nil, // Blob 0 | ||
| nil, // Blob 1 | ||
| nil, // Blob 2 | ||
| nil, // Blob 3 | ||
| {descs[5], descs[8]}, // Blob 4 | ||
| {descs[6]}, // Blob 5 | ||
| {descs[7]}, // Blob 6 | ||
| nil, // Blob 7 | ||
| nil, // Blob 8 | ||
| nil, // Blob 9 | ||
| } | ||
| for i := 0; i <= 3; i++ { | ||
| _, err := Referrers(ctx, &s, descs[i], "") | ||
| if !errors.Is(err, errdef.ErrUnsupported) { | ||
| t.Errorf("Store.Referrers(%d) error = %v, want %v", i, err, errdef.ErrUnsupported) | ||
| } | ||
| } | ||
| for i := 4; i < len(wantedReferrers); i++ { | ||
| want := wantedReferrers[i] | ||
| results, err := Referrers(ctx, &s, descs[i], "") | ||
| if err != nil { | ||
| t.Errorf("Store.Referrers(%d) error = %v", i, err) | ||
| } | ||
| if !equalDescriptorSet(results, want) { | ||
| t.Errorf("Store.Predecessors(%d) = %v, want %v", i, results, want) | ||
| } | ||
| } | ||
|
|
||
| // test filtering on ArtifactType | ||
| wantedReferrers = [][]ocispec.Descriptor{ | ||
| nil, // Blob 0 | ||
| nil, // Blob 1 | ||
| nil, // Blob 2 | ||
| nil, // Blob 3 | ||
| nil, // Blob 4 | ||
| {descs[6]}, // Blob 5 | ||
| nil, // Blob 6 | ||
| nil, // Blob 7 | ||
| nil, // Blob 8 | ||
| nil, // Blob 9 | ||
| } | ||
| for i := 4; i < len(wantedReferrers); i++ { | ||
| want := wantedReferrers[i] | ||
| results, err := Referrers(ctx, &s, descs[i], "image manifest") | ||
| if err != nil { | ||
| t.Errorf("Store.Referrers(%d) error = %v", i, err) | ||
| } | ||
| if !equalDescriptorSet(results, want) { | ||
| t.Errorf("Store.Predecessors(%d) = %v, want %v", i, results, want) | ||
| } | ||
| } | ||
|
|
||
| // test ReferrerLister | ||
| rl := TestReferrerLister{testStorage: &s} | ||
| wantedReferrers = [][]ocispec.Descriptor{ | ||
| nil, // Blob 0 | ||
| nil, // Blob 1 | ||
| nil, // Blob 2 | ||
| nil, // Blob 3 | ||
| {descs[4]}, // Blob 4 | ||
| {descs[5]}, // Blob 5 | ||
| {descs[6]}, // Blob 6 | ||
| {descs[7]}, // Blob 7 | ||
| {descs[8]}, // Blob 8 | ||
| {descs[9]}, // Blob 9 | ||
| } | ||
| for i := 4; i < len(wantedReferrers); i++ { | ||
| want := wantedReferrers[i] | ||
| results, err := Referrers(ctx, &rl, descs[i], "") | ||
| if err != nil { | ||
| t.Errorf("Store.Referrers(%d) error = %v", i, err) | ||
| } | ||
| if !equalDescriptorSet(results, want) { | ||
| t.Errorf("Store.Predecessors(%d) = %v, want %v", i, results, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func equalDescriptorSet(actual []ocispec.Descriptor, expected []ocispec.Descriptor) bool { | ||
| if len(actual) != len(expected) { | ||
| return false | ||
| } | ||
| contains := func(node ocispec.Descriptor) bool { | ||
| for _, candidate := range actual { | ||
| if reflect.DeepEqual(candidate, node) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
| for _, node := range expected { | ||
| if !contains(node) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.