From a98b69b2b0f4fc41dd6d1187452e2d46f259afdb Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Wed, 4 Sep 2024 16:40:46 +0800 Subject: [PATCH 1/6] working code waiting for testing Signed-off-by: Xiaoxuan Wang --- cmd/oras/internal/option/target.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cmd/oras/internal/option/target.go b/cmd/oras/internal/option/target.go index b81c52b26..bdadbb044 100644 --- a/cmd/oras/internal/option/target.go +++ b/cmd/oras/internal/option/target.go @@ -113,6 +113,21 @@ func (opts *Target) Parse(cmd *cobra.Command) error { } } else { opts.Reference = ref.Reference + + raw := opts.RawReference + var path string + if idx := strings.LastIndex(raw, "@"); idx != -1 { + // `digest` found + path = raw[:idx] + } else { + // find `tag` + var err error + path, _, err = fileref.Parse(raw, "") + if err != nil { + return errors.Join(err, errdef.ErrInvalidReference) + } + } + opts.Path = path } return opts.Remote.Parse(cmd) } From 6a5ac23f80f47080fb5096632f41858e6e7c7da6 Mon Sep 17 00:00:00 2001 From: wangxiaoxuan273 Date: Fri, 6 Sep 2024 10:00:50 +0800 Subject: [PATCH 2/6] working code Signed-off-by: wangxiaoxuan273 --- cmd/oras/internal/option/target.go | 38 ++++-------------------------- 1 file changed, 5 insertions(+), 33 deletions(-) diff --git a/cmd/oras/internal/option/target.go b/cmd/oras/internal/option/target.go index bdadbb044..c0255fc91 100644 --- a/cmd/oras/internal/option/target.go +++ b/cmd/oras/internal/option/target.go @@ -113,21 +113,8 @@ func (opts *Target) Parse(cmd *cobra.Command) error { } } else { opts.Reference = ref.Reference - - raw := opts.RawReference - var path string - if idx := strings.LastIndex(raw, "@"); idx != -1 { - // `digest` found - path = raw[:idx] - } else { - // find `tag` - var err error - path, _, err = fileref.Parse(raw, "") - if err != nil { - return errors.Join(err, errdef.ErrInvalidReference) - } - } - opts.Path = path + ref.Reference = "" + opts.Path = ref.String() } return opts.Remote.Parse(cmd) } @@ -160,15 +147,7 @@ func (opts *Target) newOCIStore() (*oci.Store, error) { } func (opts *Target) newRepository(common Common, logger logrus.FieldLogger) (*remote.Repository, error) { - repo, err := opts.NewRepository(opts.RawReference, common, logger) - if err != nil { - return nil, err - } - tmp := repo.Reference - tmp.Reference = "" - opts.Path = tmp.String() - opts.Reference = repo.Reference.Reference - return repo, nil + return opts.NewRepository(opts.RawReference, common, logger) } // NewTarget generates a new target based on opts. @@ -247,15 +226,8 @@ func (opts *Target) NewReadonlyTarget(ctx context.Context, common Common, logger } return store, nil case TargetTypeRemote: - repo, err := opts.NewRepository(opts.RawReference, common, logger) - if err != nil { - return nil, err - } - tmp := repo.Reference - tmp.Reference = "" - opts.Path = tmp.String() - opts.Reference = repo.Reference.Reference - return repo, nil + return opts.NewRepository(opts.RawReference, common, logger) + } return nil, fmt.Errorf("unknown target type: %q", opts.Type) } From e995f184976dc7232c93e0c87db102506328b241 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 9 Sep 2024 10:50:06 +0800 Subject: [PATCH 3/6] add test Signed-off-by: Xiaoxuan Wang --- cmd/oras/internal/option/target.go | 1 - test/e2e/suite/command/pull.go | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/oras/internal/option/target.go b/cmd/oras/internal/option/target.go index c0255fc91..489eeb820 100644 --- a/cmd/oras/internal/option/target.go +++ b/cmd/oras/internal/option/target.go @@ -227,7 +227,6 @@ func (opts *Target) NewReadonlyTarget(ctx context.Context, common Common, logger return store, nil case TargetTypeRemote: return opts.NewRepository(opts.RawReference, common, logger) - } return nil, fmt.Errorf("unknown target type: %q", opts.Type) } diff --git a/test/e2e/suite/command/pull.go b/test/e2e/suite/command/pull.go index cb9e7a666..946704f76 100644 --- a/test/e2e/suite/command/pull.go +++ b/test/e2e/suite/command/pull.go @@ -267,6 +267,12 @@ var _ = Describe("OCI spec 1.1 registry users:", func() { Expect(err).ShouldNot(HaveOccurred()) }) + It("should show correct reference", func() { + tempDir := PrepareTempFiles() + ref := RegistryRef(ZOTHost, ArtifactRepo, unnamed.Tag) + ORAS("pull", ref, "--format", "{{.reference}}").WithWorkDir(tempDir).Exec() + }) + It("should pull specific platform", func() { ORAS("pull", RegistryRef(ZOTHost, ImageRepo, "multi"), "--platform", "linux/amd64", "-v", "-o", GinkgoT().TempDir()). MatchStatus(multi_arch.LinuxAMD64StateKeys, true, len(multi_arch.LinuxAMD64StateKeys)).Exec() From 4c256e80ee9d1dc5dc8670a083292b7b9ff55404 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 9 Sep 2024 10:55:02 +0800 Subject: [PATCH 4/6] fix test Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/pull.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/suite/command/pull.go b/test/e2e/suite/command/pull.go index 946704f76..bc9981616 100644 --- a/test/e2e/suite/command/pull.go +++ b/test/e2e/suite/command/pull.go @@ -269,8 +269,8 @@ var _ = Describe("OCI spec 1.1 registry users:", func() { It("should show correct reference", func() { tempDir := PrepareTempFiles() - ref := RegistryRef(ZOTHost, ArtifactRepo, unnamed.Tag) - ORAS("pull", ref, "--format", "{{.reference}}").WithWorkDir(tempDir).Exec() + ref := RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag) + ORAS("pull", ref, "--format", "go-template={{.reference}}").WithWorkDir(tempDir).Exec() }) It("should pull specific platform", func() { From daa4bd88d951017b5c496063aa175bceb90aa359 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 9 Sep 2024 11:08:12 +0800 Subject: [PATCH 5/6] refine test Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/pull.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/e2e/suite/command/pull.go b/test/e2e/suite/command/pull.go index bc9981616..20583930d 100644 --- a/test/e2e/suite/command/pull.go +++ b/test/e2e/suite/command/pull.go @@ -270,7 +270,8 @@ var _ = Describe("OCI spec 1.1 registry users:", func() { It("should show correct reference", func() { tempDir := PrepareTempFiles() ref := RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag) - ORAS("pull", ref, "--format", "go-template={{.reference}}").WithWorkDir(tempDir).Exec() + out := ORAS("pull", ref, "--format", "go-template={{.reference}}").WithWorkDir(tempDir).Exec().Out.Contents() + Expect(out).To(Equal("localhost:7000/command/artifacts@sha256:fd6ed2f36b5465244d5dc86cb4e7df0ab8a9d24adc57825099f522fe009a22bb")) }) It("should pull specific platform", func() { From 17b3f10b01e952bf65d79fcc2032c536aa81c5c0 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 9 Sep 2024 11:13:21 +0800 Subject: [PATCH 6/6] fix test Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/pull.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/suite/command/pull.go b/test/e2e/suite/command/pull.go index 20583930d..d463cf582 100644 --- a/test/e2e/suite/command/pull.go +++ b/test/e2e/suite/command/pull.go @@ -271,7 +271,7 @@ var _ = Describe("OCI spec 1.1 registry users:", func() { tempDir := PrepareTempFiles() ref := RegistryRef(ZOTHost, ArtifactRepo, foobar.Tag) out := ORAS("pull", ref, "--format", "go-template={{.reference}}").WithWorkDir(tempDir).Exec().Out.Contents() - Expect(out).To(Equal("localhost:7000/command/artifacts@sha256:fd6ed2f36b5465244d5dc86cb4e7df0ab8a9d24adc57825099f522fe009a22bb")) + Expect(out).To(Equal([]byte("localhost:7000/command/artifacts@sha256:fd6ed2f36b5465244d5dc86cb4e7df0ab8a9d24adc57825099f522fe009a22bb"))) }) It("should pull specific platform", func() {