Skip to content

Commit d0ac8e4

Browse files
docs: update example tests (#932)
This PR: 1. Update the outdated examples involving `Pack` and `ArtifactManifest`. 2. Add numbered step documentation to the two package level examples of copy, so that there is a consistency with the other four package level examples. 3. Minor fixes such as renaming examples. Fixes #818 Signed-off-by: Xiaoxuan Wang <[email protected]>
1 parent 283d392 commit d0ac8e4

File tree

4 files changed

+89
-55
lines changed

4 files changed

+89
-55
lines changed

content/example_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"fmt"
2121
"io"
2222

23+
_ "crypto/sha256"
24+
2325
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2426
"oras.land/oras-go/v2/content"
2527
)

content/file/example_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func TestMain(m *testing.M) {
6767
}
6868

6969
// Example_packFiles gives an example of adding files and generating a manifest
70-
// referencing the files.
70+
// referencing the files as defined in image-spec v1.1.1.
7171
func Example_packFiles() {
7272
store, err := file.New(workingDir)
7373
if err != nil {
@@ -92,7 +92,10 @@ func Example_packFiles() {
9292

9393
// 2. Generate a manifest referencing the files
9494
artifactType := "example/test"
95-
manifestDescriptor, err := oras.Pack(ctx, store, artifactType, fileDescriptors, oras.PackOptions{})
95+
opts := oras.PackManifestOptions{
96+
Layers: fileDescriptors,
97+
}
98+
manifestDescriptor, err := oras.PackManifest(ctx, store, oras.PackManifestVersion1_1, artifactType, opts)
9699
if err != nil {
97100
panic(err)
98101
}
@@ -101,5 +104,5 @@ func Example_packFiles() {
101104
// Output:
102105
// file descriptor for foo.txt: {example/file sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae 3 [] map[org.opencontainers.image.title:foo.txt] [] <nil> }
103106
// file descriptor for bar.txt: {example/file sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9 3 [] map[org.opencontainers.image.title:bar.txt] [] <nil> }
104-
// manifest media type: application/vnd.oci.artifact.manifest.v1+json
107+
// manifest media type: application/vnd.oci.image.manifest.v1+json
105108
}

example_copy_test.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ func TestMain(m *testing.M) {
189189
os.Exit(m.Run())
190190
}
191191

192-
func ExampleCopy_remoteToRemote() {
192+
func ExampleCopy_repositoryToRepository() {
193193
reg, err := remote.NewRegistry(remoteHost)
194194
if err != nil {
195195
panic(err) // Handle error
@@ -199,7 +199,7 @@ func ExampleCopy_remoteToRemote() {
199199
if err != nil {
200200
panic(err) // Handle error
201201
}
202-
dst, err := reg.Repository(ctx, "target")
202+
dst, err := reg.Repository(ctx, "destination")
203203
if err != nil {
204204
panic(err) // Handle error
205205
}
@@ -215,7 +215,7 @@ func ExampleCopy_remoteToRemote() {
215215
// sha256:7cbb44b44e8ede5a89cf193db3f5f2fd019d89697e6b87e8ed2589e60649b0d1
216216
}
217217

218-
func ExampleCopy_remoteToRemoteWithMount() {
218+
func ExampleCopy_repositoryToRepositoryWithMount() {
219219
reg, err := remote.NewRegistry(remoteHost)
220220
if err != nil {
221221
panic(err) // Handle error
@@ -225,7 +225,7 @@ func ExampleCopy_remoteToRemoteWithMount() {
225225
if err != nil {
226226
panic(err) // Handle error
227227
}
228-
dst, err := reg.Repository(ctx, "target")
228+
dst, err := reg.Repository(ctx, "destination")
229229
if err != nil {
230230
panic(err) // Handle error
231231
}
@@ -241,7 +241,7 @@ func ExampleCopy_remoteToRemoteWithMount() {
241241

242242
// Enable cross-repository blob mounting
243243
opts.MountFrom = func(ctx context.Context, desc ocispec.Descriptor) ([]string, error) {
244-
// the slice of source repositores may also come from a database of known locations of blobs
244+
// the slice of source repositories may also come from a database of known locations of blobs
245245
return []string{"source/repository/name"}, nil
246246
}
247247

@@ -255,7 +255,7 @@ func ExampleCopy_remoteToRemoteWithMount() {
255255
// Final sha256:7cbb44b44e8ede5a89cf193db3f5f2fd019d89697e6b87e8ed2589e60649b0d1
256256
}
257257

258-
func ExampleCopy_remoteToLocal() {
258+
func ExampleCopy_repositoryToMemory() {
259259
reg, err := remote.NewRegistry(remoteHost)
260260
if err != nil {
261261
panic(err) // Handle error
@@ -279,7 +279,7 @@ func ExampleCopy_remoteToLocal() {
279279
// sha256:7cbb44b44e8ede5a89cf193db3f5f2fd019d89697e6b87e8ed2589e60649b0d1
280280
}
281281

282-
func ExampleCopy_localToLocal() {
282+
func ExampleCopy_memoryToMemory() {
283283
src := exampleMemoryStore
284284
dst := memory.New()
285285

@@ -295,7 +295,7 @@ func ExampleCopy_localToLocal() {
295295
// sha256:7cbb44b44e8ede5a89cf193db3f5f2fd019d89697e6b87e8ed2589e60649b0d1
296296
}
297297

298-
func ExampleCopy_localToOciFile() {
298+
func ExampleCopy_memoryToOCIStore() {
299299
src := exampleMemoryStore
300300
tempDir, err := os.MkdirTemp("", "oras_oci_example_*")
301301
if err != nil {
@@ -319,14 +319,14 @@ func ExampleCopy_localToOciFile() {
319319
// sha256:7cbb44b44e8ede5a89cf193db3f5f2fd019d89697e6b87e8ed2589e60649b0d1
320320
}
321321

322-
func ExampleCopy_localToRemote() {
322+
func ExampleCopy_memoryToRepository() {
323323
src := exampleMemoryStore
324324
reg, err := remote.NewRegistry(remoteHost)
325325
if err != nil {
326326
panic(err) // Handle error
327327
}
328328
ctx := context.Background()
329-
dst, err := reg.Repository(ctx, "target")
329+
dst, err := reg.Repository(ctx, "destination")
330330
if err != nil {
331331
panic(err) // Handle error
332332
}
@@ -342,27 +342,34 @@ func ExampleCopy_localToRemote() {
342342
// sha256:7cbb44b44e8ede5a89cf193db3f5f2fd019d89697e6b87e8ed2589e60649b0d1
343343
}
344344

345-
// ExampleCopyArtifactManifestRemoteToLocal gives an example of copying
346-
// an artifact manifest from a remote repository into memory.
347-
func Example_copyArtifactManifestRemoteToLocal() {
348-
src, err := remote.NewRepository(fmt.Sprintf("%s/source", remoteHost))
345+
// ExampleCopyArtifactFromRepository gives an example of copying
346+
// an artifact from a remote repository into memory.
347+
func Example_copyArtifactFromRepository() {
348+
// 0. Connect to a remote repository
349+
repositoryName := "source"
350+
src, err := remote.NewRepository(fmt.Sprintf("%s/%s", remoteHost, repositoryName))
349351
if err != nil {
350352
panic(err)
351353
}
354+
355+
// 1. Create a memory store
352356
dst := memory.New()
353357
ctx := context.Background()
354358

359+
// 2. Resolve the descriptor of the artifact from the digest
355360
exampleDigest := "sha256:70c29a81e235dda5c2cebb8ec06eafd3cca346cbd91f15ac74cefd98681c5b3d"
356361
descriptor, err := src.Resolve(ctx, exampleDigest)
357362
if err != nil {
358363
panic(err)
359364
}
365+
366+
// 3. Copy the artifact from the remote repository
360367
err = oras.CopyGraph(ctx, src, dst, descriptor, oras.DefaultCopyGraphOptions)
361368
if err != nil {
362369
panic(err)
363370
}
364371

365-
// verify that the artifact manifest described by the descriptor exists in dst
372+
// 4. Verify that the artifact manifest described by the descriptor exists in dst
366373
contentExists, err := dst.Exists(ctx, descriptor)
367374
if err != nil {
368375
panic(err)
@@ -373,17 +380,22 @@ func Example_copyArtifactManifestRemoteToLocal() {
373380
// true
374381
}
375382

376-
// ExampleExtendedCopyArtifactAndReferrersRemoteToLocal gives an example of
383+
// ExampleExtendedCopyArtifactAndReferrersFromRepository gives an example of
377384
// copying an artifact along with its referrers from a remote repository into
378385
// memory.
379-
func Example_extendedCopyArtifactAndReferrersRemoteToLocal() {
380-
src, err := remote.NewRepository(fmt.Sprintf("%s/source", remoteHost))
386+
func Example_extendedCopyArtifactAndReferrersFromRepository() {
387+
// 0. Connect to a remote repository
388+
repositoryName := "source"
389+
src, err := remote.NewRepository(fmt.Sprintf("%s/%s", remoteHost, repositoryName))
381390
if err != nil {
382391
panic(err)
383392
}
393+
394+
// 1. Create a memory store
384395
dst := memory.New()
385396
ctx := context.Background()
386397

398+
// 2. Copy the artifact and its referrers from the remote repository
387399
tagName := "latest"
388400
// ExtendedCopy will copy the artifact tagged by "latest" along with all of its
389401
// referrers from src to dst.

0 commit comments

Comments
 (0)