Skip to content

Commit

Permalink
remove!: Remove file.PackFiles() and add an example instead (#400)
Browse files Browse the repository at this point in the history
Resolves: #345
BREAKING CHANGE: Remove `file.PackFiles()`
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
  • Loading branch information
Wwwsylvia authored Jan 12, 2023
1 parent aa4bd09 commit 2950902
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 97 deletions.
103 changes: 103 additions & 0 deletions content/file/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
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 file_test includes all the testable examples for the file package.
package file_test

import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

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

var workingDir string // the working directory for the examples

func TestMain(m *testing.M) {
// prepare test directory
var err error
workingDir, err = os.MkdirTemp("", "oras_file_example_*")
if err != nil {
panic(err)
}
tearDown := func() {
if err := os.RemoveAll(workingDir); err != nil {
panic(err)
}
}

// prepare test file 1
content := []byte("foo")
filename := "foo.txt"
path := filepath.Join(workingDir, filename)
if err := ioutil.WriteFile(path, content, 0444); err != nil {
panic(err)
}
// prepare test file 2
content = []byte("bar")
filename = "bar.txt"
path = filepath.Join(workingDir, filename)
if err := ioutil.WriteFile(path, content, 0444); err != nil {
panic(err)
}

// run tests
exitCode := m.Run()

// tear down and exit
tearDown()
os.Exit(exitCode)
}

// Example_packFiles gives an example of adding files and generating a manifest
// referencing the files.
func Example_packFiles() {
store := file.New(workingDir)
defer store.Close()
ctx := context.Background()

// 1. Add files into the file store
mediaType := "example/file"
fileNames := []string{"foo.txt", "bar.txt"}
fileDescriptors := make([]ocispec.Descriptor, 0, len(fileNames))
for _, name := range fileNames {
fileDescriptor, err := store.Add(ctx, name, mediaType, "")
if err != nil {
panic(err)
}
fileDescriptors = append(fileDescriptors, fileDescriptor)

fmt.Printf("file descriptor for %s: %v\n", name, fileDescriptor)
}

// 2. Generate a manifest referencing the files
artifactType := "example/test"
manifestDescriptor, err := oras.Pack(ctx, store, artifactType, fileDescriptors, oras.PackOptions{})
if err != nil {
panic(err)
}
fmt.Println("manifest media type:", manifestDescriptor.MediaType)

// Output:
// file descriptor for foo.txt: {example/file sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae 3 [] map[org.opencontainers.image.title:foo.txt] [] <nil> }
// file descriptor for bar.txt: {example/file sha256:fcde2b2edba56bf408601fb721fe9b5c338d10ee429ea04fae5511b68fbf8fb9 3 [] map[org.opencontainers.image.title:bar.txt] [] <nil> }
// manifest media type: application/vnd.oci.artifact.manifest.v1+json
}
20 changes: 0 additions & 20 deletions content/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content"
"oras.land/oras-go/v2/errdef"
"oras.land/oras-go/v2/internal/cas"
Expand Down Expand Up @@ -424,25 +423,6 @@ func (s *Store) Add(_ context.Context, name, mediaType, path string) (ocispec.De
return desc, nil
}

// generates a manifest for the pack, and store the manifest in the file store.
// If succeeded, returns a descriptor of the manifest.
func (s *Store) PackFiles(ctx context.Context, names []string) (ocispec.Descriptor, error) {
if s.isClosedSet() {
return ocispec.Descriptor{}, ErrStoreClosed
}

var layers []ocispec.Descriptor
for _, name := range names {
desc, err := s.Add(ctx, name, "", "")
if err != nil {
return ocispec.Descriptor{}, fmt.Errorf("failed to add %s: %w", name, err)
}
layers = append(layers, desc)
}

return oras.Pack(ctx, s, "", layers, oras.PackOptions{})
}

// saveFile saves content matching the descriptor to the given file.
func (s *Store) saveFile(fp *os.File, expected ocispec.Descriptor, content io.Reader) (err error) {
defer func() {
Expand Down
77 changes: 0 additions & 77 deletions content/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,6 @@ func TestStore_Close(t *testing.T) {
if _, err := s.Predecessors(ctx, desc); !errors.Is(err, ErrStoreClosed) {
t.Errorf("Store.Predecessors() = %v, want %v", err, ErrStoreClosed)
}

// test PackFiles after close
if _, err := s.PackFiles(ctx, []string{}); !errors.Is(err, ErrStoreClosed) {
t.Errorf("Store.PackFiles() = %v, want %v", err, ErrStoreClosed)
}
}

func TestStore_File_Push(t *testing.T) {
Expand Down Expand Up @@ -993,78 +988,6 @@ func TestStore_File_Add_SameContent(t *testing.T) {
}
}

func TestStore_PackFiles(t *testing.T) {
var names []string

content_1 := []byte("hello world")
name_1 := "test_1.txt"
names = append(names, name_1)
desc_1 := ocispec.Descriptor{
MediaType: defaultBlobMediaType,
Digest: digest.FromBytes(content_1),
Size: int64(len(content_1)),
Annotations: map[string]string{
ocispec.AnnotationTitle: name_1,
},
}

content_2 := []byte("goodbye world")
name_2 := "test_2.txt"
names = append(names, name_2)
desc_2 := ocispec.Descriptor{
MediaType: defaultBlobMediaType,
Digest: digest.FromBytes(content_2),
Size: int64(len(content_2)),
Annotations: map[string]string{
ocispec.AnnotationTitle: name_2,
},
}

tempDir := t.TempDir()
if err := ioutil.WriteFile(filepath.Join(tempDir, name_1), content_1, 0444); err != nil {
t.Fatal("error calling WriteFile(), error =", err)
}

if err := ioutil.WriteFile(filepath.Join(tempDir, name_2), content_2, 0444); err != nil {
t.Fatal("error calling WriteFile(), error =", err)
}

s := New(tempDir)
defer s.Close()
ctx := context.Background()

// test pack
manifestDesc, err := s.PackFiles(ctx, names)
if err != nil {
t.Fatal("Store.Pack() error =", err)
}

// test exists
exists, err := s.Exists(ctx, desc_1)
if err != nil {
t.Fatal("Store.Exists() error =", err)
}
if !exists {
t.Errorf("Store.Exists() = %v, want %v", exists, true)
}

exists, err = s.Exists(ctx, desc_2)
if err != nil {
t.Fatal("Store.Exists() error =", err)
}
if !exists {
t.Errorf("Store.Exists() = %v, want %v", exists, true)
}

exists, err = s.Exists(ctx, manifestDesc)
if err != nil {
t.Fatal("Store.Exists() error =", err)
}
if !exists {
t.Errorf("Store.Exists() = %v, want %v", exists, true)
}
}

func TestStore_File_Push_SameContent(t *testing.T) {
mediaType := "test"
content := []byte("hello world")
Expand Down

0 comments on commit 2950902

Please sign in to comment.