Skip to content

Commit

Permalink
return error
Browse files Browse the repository at this point in the history
Signed-off-by: Lixia (Sylvia) Lei <[email protected]>
  • Loading branch information
Wwwsylvia committed Jul 13, 2023
1 parent 5c8d440 commit 86b3b94
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
33 changes: 22 additions & 11 deletions pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ const (
PackManifestTypeImageManifest
)

// ErrInvalidDateTimeFormat is returned by [Pack] when
// AnnotationArtifactCreated or AnnotationCreated is provided, but its value
// is not in RFC 3339 format.
// Reference: https://www.rfc-editor.org/rfc/rfc3339#section-5.6
var ErrInvalidDateTimeFormat = errors.New("invalid date and time format")
var (
// ErrInvalidDateTimeFormat is returned by [Pack] when
// AnnotationArtifactCreated or AnnotationCreated is provided, but its value
// is not in RFC 3339 format.
// Reference: https://www.rfc-editor.org/rfc/rfc3339#section-5.6
ErrInvalidDateTimeFormat = errors.New("invalid date and time format")

// ErrMissingArtifactType is returned by [Pack] when artifactType is not
// specified and the config media type is set to
// "application/vnd.oci.empty.v1+json".
ErrMissingArtifactType = errors.New("missing artifact type")
)

// PackOptions contains parameters for [Pack].
type PackOptions struct {
Expand Down Expand Up @@ -101,9 +108,13 @@ var DefaultPackOptions PackOptions = PackOptions{
// Pack packs the given blobs, generates a manifest for the pack,
// and pushes it to a content storage.
//
// When opts.PackImageManifest is true and opts.PackManifestType is
// PackManifestTypeImageManifestLegacy, artifactType will be used as the
// the config descriptor mediaType of the image manifest.
// - If opts.PackImageManifest is true and opts.PackManifestType is
// PackManifestTypeImageManifestLegacy (default value),
// artifactType will be used as the the config media type of the image
// manifest when opts.ConfigDescriptor is not specified.
// - If opts.PackImageManifest is true and opts.PackManifestType is
// PackManifestTypeImageManifest, [ErrMissingArtifactType] will be returned
// when none of artifactType and opts.ConfigDescriptor is specified.
//
// If succeeded, returns a descriptor of the manifest.
func Pack(ctx context.Context, pusher content.Pusher, artifactType string, blobs []ocispec.Descriptor, opts PackOptions) (ocispec.Descriptor, error) {
Expand Down Expand Up @@ -236,11 +247,11 @@ func packImage(ctx context.Context, pusher content.Pusher, artifactType string,
}
}
if artifactType == "" {
// artifactType MUST be set when config.mediaType is set to the empty value
artifactType = configDesc.MediaType
if configDesc.MediaType == ocispec.MediaTypeEmptyJSON {
artifactType = MediaTypeUnknownArtifact
// artifactType MUST be set when config.mediaType is set to the empty value
return ocispec.Descriptor{}, fmt.Errorf("artifactType must be set when the config media type is the empty value: %w", ErrMissingArtifactType)
}
artifactType = configDesc.MediaType
}

annotations, err := ensureAnnotationCreated(opts.ManifestAnnotations, ocispec.AnnotationCreated)
Expand Down
30 changes: 5 additions & 25 deletions pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -780,29 +780,9 @@ func Test_Pack_Image_NoArtifactType(t *testing.T) {
PackImageManifest: true,
PackManifestType: PackManifestTypeImageManifest,
}
manifestDesc, err := Pack(ctx, s, "", nil, opts)
if err != nil {
t.Fatal("Oras.Pack() error =", err)
}

var manifest ocispec.Manifest
rc, err := s.Fetch(ctx, manifestDesc)
if err != nil {
t.Fatal("Store.Fetch() error =", err)
}
if err := json.NewDecoder(rc).Decode(&manifest); err != nil {
t.Fatal("error decoding manifest, error =", err)
}
if err := rc.Close(); err != nil {
t.Fatal("Store.Fetch().Close() error =", err)
}

// verify artifact type
if want := MediaTypeUnknownArtifact; manifest.ArtifactType != want {
t.Fatalf("got artifact type = %s, want %s", manifest.ArtifactType, want)
}
if want := MediaTypeUnknownArtifact; manifestDesc.ArtifactType != want {
t.Fatalf("got artifact type = %s, want %s", manifestDesc.ArtifactType, want)
_, err := Pack(ctx, s, "", nil, opts)
if wantErr := ErrMissingArtifactType; !errors.Is(err, wantErr) {
t.Errorf("Oras.Pack() error = %v, wantErr = %v", err, wantErr)
}
}

Expand All @@ -815,7 +795,7 @@ func Test_Pack_Image_NoLayer(t *testing.T) {
PackImageManifest: true,
PackManifestType: PackManifestTypeImageManifest,
}
manifestDesc, err := Pack(ctx, s, "", nil, opts)
manifestDesc, err := Pack(ctx, s, "test", nil, opts)
if err != nil {
t.Fatal("Oras.Pack() error =", err)
}
Expand Down Expand Up @@ -850,7 +830,7 @@ func Test_Pack_Image_InvalidDateTimeFormat(t *testing.T) {
ocispec.AnnotationCreated: "2000/01/01 00:00:00",
},
}
_, err := Pack(ctx, s, "", nil, opts)
_, err := Pack(ctx, s, "test", nil, opts)
if wantErr := ErrInvalidDateTimeFormat; !errors.Is(err, wantErr) {
t.Errorf("Oras.Pack() error = %v, wantErr = %v", err, wantErr)
}
Expand Down

0 comments on commit 86b3b94

Please sign in to comment.