Skip to content

Commit

Permalink
filestore option to ignore noname error (#239)
Browse files Browse the repository at this point in the history
Signed-off-by: Avi Deitcher <[email protected]>
  • Loading branch information
deitch authored Mar 11, 2021
1 parent a2848fe commit 0380ecb
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 13 deletions.
36 changes: 26 additions & 10 deletions pkg/content/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,28 @@ type FileStore struct {
// Reproducible enables stripping times from added files
Reproducible bool

root string
descriptor *sync.Map // map[digest.Digest]ocispec.Descriptor
pathMap *sync.Map
tmpFiles *sync.Map
root string
descriptor *sync.Map // map[digest.Digest]ocispec.Descriptor
pathMap *sync.Map
tmpFiles *sync.Map
ignoreNoName bool
}

// NewFileStore creats a new file store
func NewFileStore(rootPath string) *FileStore {
func NewFileStore(rootPath string, opts ...WriterOpt) *FileStore {
// we have to process the opts to find if they told us to change defaults
var wOpts WriterOpts
for _, opt := range opts {
if err := opt(&wOpts); err != nil {
continue
}
}
return &FileStore{
root: rootPath,
descriptor: &sync.Map{},
pathMap: &sync.Map{},
tmpFiles: &sync.Map{},
root: rootPath,
descriptor: &sync.Map{},
pathMap: &sync.Map{},
tmpFiles: &sync.Map{},
ignoreNoName: wOpts.IgnoreNoName,
}
}

Expand Down Expand Up @@ -198,7 +207,14 @@ func (s *FileStore) Writer(ctx context.Context, opts ...content.WriterOpt) (cont

name, ok := ResolveName(desc)
if !ok {
return nil, ErrNoName
// if we were not told to ignore NoName, then return an error
if !s.ignoreNoName {
return nil, ErrNoName
}

// just return a nil writer - we do not want to calculate the hash, so just use
// whatever was passed in the descriptor
return NewIoContentWriter(ioutil.Discard, WithOutputHash(desc.Digest)), nil
}
path, err := s.resolveWritePath(name)
if err != nil {
Expand Down
45 changes: 45 additions & 0 deletions pkg/content/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package content_test

import (
"context"
"io/ioutil"
"os"
"testing"

ctrcontent "github.com/containerd/containerd/content"
"github.com/deislabs/oras/pkg/content"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

func TestFileStoreNoName(t *testing.T) {
testContent := []byte("Hello World!")
descriptor := ocispec.Descriptor{
MediaType: ocispec.MediaTypeImageConfig,
Digest: digest.FromBytes(testContent),
Size: int64(len(testContent)),
// do NOT add the AnnotationTitle here; it is the essence of the test
}

tests := []struct {
opts []content.WriterOpt
err error
}{
{nil, content.ErrNoName},
{[]content.WriterOpt{content.WithIgnoreNoName()}, nil},
}
for _, tt := range tests {
rootPath, err := ioutil.TempDir("", "oras_filestore_test")
if err != nil {
t.Fatalf("error creating tempdir: %v", err)
}
defer os.RemoveAll(rootPath)
fileStore := content.NewFileStore(rootPath, tt.opts...)
ctx := context.Background()
refOpt := ctrcontent.WithDescriptor(descriptor)
if _, err := fileStore.Writer(ctx, refOpt); err != tt.err {
t.Errorf("mismatched error, actual '%v', expected '%v'", err, tt.err)
}

}
}
18 changes: 15 additions & 3 deletions pkg/content/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ type WriterOpts struct {
OutputHash *digest.Digest
Blocksize int
MultiWriterIngester bool
IgnoreNoName bool
}

type WriterOpt func(*WriterOpts) error

func DefaultWriterOpts() WriterOpts {
return WriterOpts{
InputHash: nil,
OutputHash: nil,
Blocksize: DefaultBlocksize,
InputHash: nil,
OutputHash: nil,
Blocksize: DefaultBlocksize,
IgnoreNoName: false,
}
}

Expand Down Expand Up @@ -71,3 +73,13 @@ func WithMultiWriterIngester() WriterOpt {
return nil
}
}

// WithIgnoreNoName some ingesters, when creating a Writer, return an error if
// the descriptor does not have a valid name on the descriptor. Passing WithIgnoreNoName
// tells the writer not to return an error, but rather to pass the data to a nil writer.
func WithIgnoreNoName() WriterOpt {
return func(w *WriterOpts) error {
w.IgnoreNoName = true
return nil
}
}

0 comments on commit 0380ecb

Please sign in to comment.