| 1 |
/* |
| 2 |
Copyright The ORAS Authors. |
| 3 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
you may not use this file except in compliance with the License. |
| 5 |
You may obtain a copy of the License at |
| 6 |
|
| 7 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
|
| 9 |
Unless required by applicable law or agreed to in writing, software |
| 10 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
See the License for the specific language governing permissions and |
| 13 |
limitations under the License. |
| 14 |
*/ |
| 15 |
|
| 16 |
// Package file_test includes all the testable examples for the file package. |
| 17 |
package file_test |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"os" |
| 23 |
"path/filepath" |
| 24 |
"testing" |
| 25 |
|
| 26 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 27 |
"oras.land/oras-go/v2" |
| 28 |
"oras.land/oras-go/v2/content/file" |
| 29 |
) |
| 30 |
|
| 31 |
var workingDir string // the working directory for the examples |
| 32 |
|
| 33 |
func TestMain(m *testing.M) { |
| 34 |
// prepare test directory |
| 35 |
var err error |
| 36 |
workingDir, err = os.MkdirTemp("", "oras_file_example_*") |
| 37 |
if err != nil { |
| 38 |
panic(err) |
| 39 |
} |
| 40 |
tearDown := func() { |
| 41 |
if err := os.RemoveAll(workingDir); err != nil { |
| 42 |
panic(err) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
// prepare test file 1 |
| 47 |
content := []byte("foo") |
| 48 |
filename := "foo.txt" |
| 49 |
path := filepath.Join(workingDir, filename) |
| 50 |
if err := os.WriteFile(path, content, 0444); err != nil { |
| 51 |
panic(err) |
| 52 |
} |
| 53 |
// prepare test file 2 |
| 54 |
content = []byte("bar") |
| 55 |
filename = "bar.txt" |
| 56 |
path = filepath.Join(workingDir, filename) |
| 57 |
if err := os.WriteFile(path, content, 0444); err != nil { |
| 58 |
panic(err) |
| 59 |
} |
| 60 |
|
| 61 |
// run tests |
| 62 |
exitCode := m.Run() |
| 63 |
|
| 64 |
// tear down and exit |
| 65 |
tearDown() |
| 66 |
os.Exit(exitCode) |
| 67 |
} |
| 68 |
|
| 69 |
// Example_packFiles gives an example of adding files and generating a manifest |
| 70 |
// referencing the files as defined in image-spec v1.1.1. |
| 71 |
func Example_packFiles() { |
| 72 |
store, err := file.New(workingDir) |
| 73 |
if err != nil { |
| 74 |
panic(err) |
| 75 |
} |
| 76 |
defer store.Close() |
| 77 |
ctx := context.Background() |
| 78 |
|
| 79 |
// 1. Add files into the file store |
| 80 |
mediaType := "example/file" |
| 81 |
fileNames := []string{"foo.txt", "bar.txt"} |
| 82 |
fileDescriptors := make([]ocispec.Descriptor, 0, len(fileNames)) |
| 83 |
for _, name := range fileNames { |
| 84 |
fileDescriptor, err := store.Add(ctx, name, mediaType, "") |
| 85 |
if err != nil { |
| 86 |
panic(err) |
| 87 |
} |
| 88 |
fileDescriptors = append(fileDescriptors, fileDescriptor) |
| 89 |
|
| 90 |
fmt.Printf("file descriptor for %s: %v\n", name, fileDescriptor) |
| 91 |
} |
| 92 |
|
| 93 |
// 2. Generate a manifest referencing the files |
| 94 |
artifactType := "example/test" |
| 95 |
opts := oras.PackManifestOptions{ |
| 96 |
Layers: fileDescriptors, |
| 97 |
} |
| 98 |
manifestDescriptor, err := oras.PackManifest(ctx, store, oras.PackManifestVersion1_1, artifactType, opts) |
| 99 |
if err != nil { |
| 100 |
panic(err) |
| 101 |
} |
| 102 |
fmt.Println("manifest media type:", manifestDescriptor.MediaType) |
| 103 |
|
| 104 |
// Output: |
| 105 |
// file descriptor for foo.txt: {example/file sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae 3 [] map[org.opencontainers.image.title:foo.txt] [] <nil> } |
| 106 |
// file descriptor for bar.txt: {example/file sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9 3 [] map[org.opencontainers.image.title:bar.txt] [] <nil> } |
| 107 |
// manifest media type: application/vnd.oci.image.manifest.v1+json |
| 108 |
} |