From 86b3b94edfdfc0dced61408e2f1b429d830bc1f1 Mon Sep 17 00:00:00 2001 From: "Lixia (Sylvia) Lei" Date: Thu, 13 Jul 2023 16:50:51 +0800 Subject: [PATCH] return error Signed-off-by: Lixia (Sylvia) Lei --- pack.go | 33 ++++++++++++++++++++++----------- pack_test.go | 30 +++++------------------------- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/pack.go b/pack.go index 65c41efc..6cedd1b6 100644 --- a/pack.go +++ b/pack.go @@ -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 { @@ -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) { @@ -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) diff --git a/pack_test.go b/pack_test.go index ddc07c16..a6a2f4a3 100644 --- a/pack_test.go +++ b/pack_test.go @@ -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) } } @@ -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) } @@ -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) }