Skip to content

Commit 6a09a65

Browse files
authoredAug 2, 2022
Added an example of using ExtendedCopy (#248)
Signed-off-by: wangxiaoxuan273 <wangxiaoxuan119@gmail.com>
1 parent da314f8 commit 6a09a65

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed
 

‎example_test.go

+57-4
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,26 @@ var (
4444
exampleManifest, _ = json.Marshal(artifactspec.Manifest{
4545
MediaType: artifactspec.MediaTypeArtifactManifest,
4646
ArtifactType: "example/content"})
47-
exampleManifestDescriptor = artifactspec.Descriptor{
47+
exampleManifestDescriptor = ocispec.Descriptor{
4848
MediaType: artifactspec.MediaTypeArtifactManifest,
4949
Digest: digest.Digest(digest.FromBytes(exampleManifest)),
5050
Size: int64(len(exampleManifest))}
51+
exampleManifestDescriptorArtifactspec = artifactspec.Descriptor{
52+
MediaType: exampleManifestDescriptor.MediaType,
53+
Digest: exampleManifestDescriptor.Digest,
54+
Size: exampleManifestDescriptor.Size}
5155
exampleSignatureManifest, _ = json.Marshal(artifactspec.Manifest{
5256
MediaType: artifactspec.MediaTypeArtifactManifest,
5357
ArtifactType: "example/signature",
54-
Subject: &exampleManifestDescriptor})
58+
Subject: &exampleManifestDescriptorArtifactspec})
5559
exampleSignatureManifestDescriptor = ocispec.Descriptor{
5660
MediaType: artifactspec.MediaTypeArtifactManifest,
5761
Digest: digest.FromBytes(exampleSignatureManifest),
5862
Size: int64(len(exampleSignatureManifest))}
63+
exampleSignatureManifestDescriptorArtifactspec = artifactspec.Descriptor{
64+
MediaType: exampleSignatureManifestDescriptor.MediaType,
65+
Digest: exampleSignatureManifestDescriptor.Digest,
66+
Size: exampleSignatureManifestDescriptor.Size}
5967
)
6068

6169
func pushBlob(ctx context.Context, mediaType string, blob []byte, target oras.Target) (desc ocispec.Descriptor, err error) {
@@ -122,11 +130,33 @@ func TestMain(m *testing.M) {
122130
w.Header().Set("Docker-Content-Digest", string(exampleSignatureManifestDescriptor.Digest))
123131
w.Header().Set("Content-Length", strconv.Itoa(len(exampleSignatureManifest)))
124132
w.Write(exampleSignatureManifest)
125-
case strings.Contains(p, "/manifests/"+string(exampleManifestDescriptor.Digest)):
133+
case strings.Contains(p, "/manifests/latest") && m == "PUT":
134+
w.WriteHeader(http.StatusCreated)
135+
case strings.Contains(p, "/manifests/"+string(exampleManifestDescriptor.Digest)),
136+
strings.Contains(p, "/manifests/latest") && m == "HEAD":
126137
w.Header().Set("Content-Type", artifactspec.MediaTypeArtifactManifest)
127138
w.Header().Set("Docker-Content-Digest", string(exampleManifestDescriptor.Digest))
128139
w.Header().Set("Content-Length", strconv.Itoa(len(exampleManifest)))
129-
w.Write(exampleManifest)
140+
if m == "GET" {
141+
w.Write(exampleManifest)
142+
}
143+
case strings.Contains(p, "/artifacts/referrers"):
144+
w.Header().Set("ORAS-Api-Version", "oras/1.0")
145+
q := r.URL.Query()
146+
var referrers []artifactspec.Descriptor
147+
if q.Get("digest") == exampleManifestDescriptor.Digest.String() {
148+
referrers = []artifactspec.Descriptor{exampleSignatureManifestDescriptorArtifactspec}
149+
} else if q.Get("digest") == exampleSignatureManifestDescriptor.Digest.String() {
150+
referrers = []artifactspec.Descriptor{}
151+
}
152+
result := struct {
153+
Referrers []artifactspec.Descriptor `json:"referrers"`
154+
}{
155+
Referrers: referrers,
156+
}
157+
if err := json.NewEncoder(w).Encode(result); err != nil {
158+
panic(err)
159+
}
130160
case strings.Contains(p, "/manifests/") && (m == "HEAD" || m == "GET"):
131161
w.Header().Set("Content-Type", ocispec.MediaTypeImageManifest)
132162
w.Header().Set("Docker-Content-Digest", string(manifestDesc.Digest))
@@ -311,3 +341,26 @@ func Example_copyArtifactManifestRemoteToLocal() {
311341
// Output:
312342
// true
313343
}
344+
345+
// Example_extendedCopyArtifactAndReferrersRemoteToLocal gives an example of
346+
// copying an artifact along with its referrers from a remote repository to local.
347+
func Example_extendedCopyArtifactAndReferrersRemoteToLocal() {
348+
src, err := remote.NewRepository(fmt.Sprintf("%s/source", remoteHost))
349+
if err != nil {
350+
panic(err)
351+
}
352+
dst := memory.New()
353+
ctx := context.Background()
354+
355+
tagName := "latest"
356+
// ExtendedCopy will copy the artifact tagged by "latest" along with all of its
357+
// referrers from src to dst.
358+
desc, err := oras.ExtendedCopy(ctx, src, tagName, dst, tagName, oras.DefaultExtendedCopyOptions)
359+
if err != nil {
360+
panic(err)
361+
}
362+
363+
fmt.Println(desc.Digest)
364+
// Output:
365+
// sha256:1f3e679d4fc05dca20a699ae5af5fb2b7d481d5694aff929165d1c8b0f4c8598
366+
}

0 commit comments

Comments
 (0)
Please sign in to comment.