From 9dc8a8d5659e7f95bc3658c85d94247a2aef644b Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 1 Apr 2024 03:11:59 +0000 Subject: [PATCH 01/12] temp Signed-off-by: Xiaoxuan Wang --- cmd/oras/internal/display/metadata/text/push.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/cmd/oras/internal/display/metadata/text/push.go b/cmd/oras/internal/display/metadata/text/push.go index c6581d786..602656344 100644 --- a/cmd/oras/internal/display/metadata/text/push.go +++ b/cmd/oras/internal/display/metadata/text/push.go @@ -44,6 +44,10 @@ func (p *PushHandler) OnCopied(opts *option.Target) error { // OnCompleted is called after the push is completed. func (p *PushHandler) OnCompleted(root ocispec.Descriptor) error { - _, err := fmt.Fprintln(p.out, "Digest:", root.Digest) + _, err := fmt.Fprintln(p.out, "ArtifactType:", root.ArtifactType) + if err != nil { + return err + } + _, err = fmt.Fprintln(p.out, "Digest:", root.Digest) return err } From e39145d20e690e4b2a4757fb899e939afad636be Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Sun, 7 Apr 2024 06:18:37 +0000 Subject: [PATCH 02/12] added e2e tests Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/push.go | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index 741c70e20..b8f5fdbba 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -371,6 +371,42 @@ var _ = Describe("Remote registry users:", func() { Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) }) + It("should output artifact type when push is complete for image-spec v1.1", func() { + repo := pushTestRepo("print-artifact-type") + tempDir := PrepareTempFiles() + + ORAS("push", RegistryRef(ZOTHost, repo, tag), foobar.FileBarName, "-v", "--image-spec", "v1.1"). + MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). + MatchKeyWords("ArtifactType: ", "application/vnd.unknown.artifact.v1"). + WithWorkDir(tempDir).Exec() + + // validate + fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() + var manifest ocispec.Manifest + Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) + Expect(manifest.ArtifactType).Should(Equal("application/vnd.unknown.artifact.v1")) + Expect(manifest.Layers).Should(ContainElements(foobar.BlobBarDescriptor("application/vnd.oci.image.layer.v1.tar"))) + Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) + }) + + It("should output artifact type when push is complete for image-spec v1.0", func() { + repo := pushTestRepo("print-artifact-type") + tempDir := PrepareTempFiles() + + ORAS("push", RegistryRef(ZOTHost, repo, tag), foobar.FileBarName, "-v", "--image-spec", "v1.0"). + MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). + MatchKeyWords("ArtifactType: ", "application/vnd.unknown.config.v1+json"). + WithWorkDir(tempDir).Exec() + + // validate + fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() + var manifest ocispec.Manifest + Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) + Expect(manifest.ArtifactType).Should(Equal("application/vnd.unknown.artifact.v1")) + Expect(manifest.Layers).Should(ContainElements(foobar.BlobBarDescriptor("application/vnd.oci.image.layer.v1.tar"))) + Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) + }) + It("should push v1.1-rc.4 artifact", func() { repo := pushTestRepo("v1.1-artifact") tempDir := PrepareTempFiles() From 8514fa1a9e7c1f437847825140a4da667b30fc05 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Sun, 7 Apr 2024 06:25:31 +0000 Subject: [PATCH 03/12] e2e test Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/push.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index b8f5fdbba..47f31fef7 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -389,23 +389,23 @@ var _ = Describe("Remote registry users:", func() { Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) }) - It("should output artifact type when push is complete for image-spec v1.0", func() { - repo := pushTestRepo("print-artifact-type") - tempDir := PrepareTempFiles() - - ORAS("push", RegistryRef(ZOTHost, repo, tag), foobar.FileBarName, "-v", "--image-spec", "v1.0"). - MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). - MatchKeyWords("ArtifactType: ", "application/vnd.unknown.config.v1+json"). - WithWorkDir(tempDir).Exec() - - // validate - fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() - var manifest ocispec.Manifest - Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) - Expect(manifest.ArtifactType).Should(Equal("application/vnd.unknown.artifact.v1")) - Expect(manifest.Layers).Should(ContainElements(foobar.BlobBarDescriptor("application/vnd.oci.image.layer.v1.tar"))) - Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) - }) + // It("should output artifact type when push is complete for image-spec v1.0", func() { + // repo := pushTestRepo("print-artifact-type") + // tempDir := PrepareTempFiles() + + // ORAS("push", RegistryRef(ZOTHost, repo, tag), foobar.FileBarName, "-v", "--image-spec", "v1.0"). + // MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). + // MatchKeyWords("ArtifactType: ", "application/vnd.unknown.config.v1+json"). + // WithWorkDir(tempDir).Exec() + + // // validate + // fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() + // var manifest ocispec.Manifest + // Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) + // Expect(manifest.ArtifactType).Should(Equal("application/vnd.unknown.config.v1+json")) + // Expect(manifest.Layers).Should(ContainElements(foobar.BlobBarDescriptor("application/vnd.oci.image.layer.v1.tar"))) + // Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) + // }) It("should push v1.1-rc.4 artifact", func() { repo := pushTestRepo("v1.1-artifact") From 515fed3cc9ce21d19e6cfb7444a46c19de4ed360 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Sun, 7 Apr 2024 06:28:20 +0000 Subject: [PATCH 04/12] e2e for v1.0 Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/push.go | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index 47f31fef7..c4d2661c9 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -389,23 +389,23 @@ var _ = Describe("Remote registry users:", func() { Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) }) - // It("should output artifact type when push is complete for image-spec v1.0", func() { - // repo := pushTestRepo("print-artifact-type") - // tempDir := PrepareTempFiles() - - // ORAS("push", RegistryRef(ZOTHost, repo, tag), foobar.FileBarName, "-v", "--image-spec", "v1.0"). - // MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). - // MatchKeyWords("ArtifactType: ", "application/vnd.unknown.config.v1+json"). - // WithWorkDir(tempDir).Exec() - - // // validate - // fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() - // var manifest ocispec.Manifest - // Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) - // Expect(manifest.ArtifactType).Should(Equal("application/vnd.unknown.config.v1+json")) - // Expect(manifest.Layers).Should(ContainElements(foobar.BlobBarDescriptor("application/vnd.oci.image.layer.v1.tar"))) - // Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) - // }) + It("should output artifact type when push is complete for image-spec v1.0", func() { + repo := pushTestRepo("print-artifact-type") + tempDir := PrepareTempFiles() + + ORAS("push", RegistryRef(ZOTHost, repo, tag), foobar.FileBarName, "-v", "--image-spec", "v1.0"). + MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). + MatchKeyWords("ArtifactType: ", "application/vnd.unknown.config.v1+json"). + WithWorkDir(tempDir).Exec() + + // validate + fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() + var manifest ocispec.Manifest + Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) + Expect(manifest.ArtifactType).Should(Equal("application/vnd.unknown.config.v1+json")) + Expect(manifest.Layers).Should(ContainElements(foobar.BlobBarDescriptor("application/vnd.oci.image.layer.v1.tar"))) + Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) + }) It("should push v1.1-rc.4 artifact", func() { repo := pushTestRepo("v1.1-artifact") From 11dd554b703728977a9975843b23ff5868854936 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Sun, 7 Apr 2024 06:31:24 +0000 Subject: [PATCH 05/12] fix test Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/push.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index c4d2661c9..ee1591d0e 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -389,24 +389,6 @@ var _ = Describe("Remote registry users:", func() { Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) }) - It("should output artifact type when push is complete for image-spec v1.0", func() { - repo := pushTestRepo("print-artifact-type") - tempDir := PrepareTempFiles() - - ORAS("push", RegistryRef(ZOTHost, repo, tag), foobar.FileBarName, "-v", "--image-spec", "v1.0"). - MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). - MatchKeyWords("ArtifactType: ", "application/vnd.unknown.config.v1+json"). - WithWorkDir(tempDir).Exec() - - // validate - fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() - var manifest ocispec.Manifest - Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) - Expect(manifest.ArtifactType).Should(Equal("application/vnd.unknown.config.v1+json")) - Expect(manifest.Layers).Should(ContainElements(foobar.BlobBarDescriptor("application/vnd.oci.image.layer.v1.tar"))) - Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) - }) - It("should push v1.1-rc.4 artifact", func() { repo := pushTestRepo("v1.1-artifact") tempDir := PrepareTempFiles() From 463e1e5d86453facc12c3b6962ea551bd6aab8b7 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Sun, 7 Apr 2024 08:52:41 +0000 Subject: [PATCH 06/12] added e2e test case Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/push.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index ee1591d0e..3d7ead65a 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -372,7 +372,7 @@ var _ = Describe("Remote registry users:", func() { }) It("should output artifact type when push is complete for image-spec v1.1", func() { - repo := pushTestRepo("print-artifact-type") + repo := pushTestRepo("print-artifact-type-v1-1") tempDir := PrepareTempFiles() ORAS("push", RegistryRef(ZOTHost, repo, tag), foobar.FileBarName, "-v", "--image-spec", "v1.1"). @@ -389,6 +389,23 @@ var _ = Describe("Remote registry users:", func() { Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) }) + It("should output artifact type when push is complete for image-spec v1.0 when --config is used", func() { + repo := pushTestRepo("print-artifact-type-v1-0-config") + configType := "config/type" + tempDir := PrepareTempFiles() + + ORAS("push", RegistryRef(ZOTHost, repo, tag), "--config", fmt.Sprintf("%s:%s", foobar.FileConfigName, configType), foobar.FileBarName, "-v", "--image-spec", "v1.0"). + MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). + MatchKeyWords("ArtifactType: ", configType). + WithWorkDir(tempDir).Exec() + + // validate + fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() + var manifest ocispec.Manifest + Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) + Expect(manifest.ArtifactType).Should(Equal("configType")) + }) + It("should push v1.1-rc.4 artifact", func() { repo := pushTestRepo("v1.1-artifact") tempDir := PrepareTempFiles() From c4e054261a11da919614cd70110dc936f73b1649 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Sun, 7 Apr 2024 09:00:04 +0000 Subject: [PATCH 07/12] fix failed test Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/push.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index 3d7ead65a..ebd2529e1 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -395,7 +395,6 @@ var _ = Describe("Remote registry users:", func() { tempDir := PrepareTempFiles() ORAS("push", RegistryRef(ZOTHost, repo, tag), "--config", fmt.Sprintf("%s:%s", foobar.FileConfigName, configType), foobar.FileBarName, "-v", "--image-spec", "v1.0"). - MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). MatchKeyWords("ArtifactType: ", configType). WithWorkDir(tempDir).Exec() From d44e69a8b7ff35c3cb2ebb1321048b32470e09a8 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Sun, 7 Apr 2024 09:04:04 +0000 Subject: [PATCH 08/12] fix failed test Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/push.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index ebd2529e1..d8dc3e412 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -397,12 +397,6 @@ var _ = Describe("Remote registry users:", func() { ORAS("push", RegistryRef(ZOTHost, repo, tag), "--config", fmt.Sprintf("%s:%s", foobar.FileConfigName, configType), foobar.FileBarName, "-v", "--image-spec", "v1.0"). MatchKeyWords("ArtifactType: ", configType). WithWorkDir(tempDir).Exec() - - // validate - fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() - var manifest ocispec.Manifest - Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) - Expect(manifest.ArtifactType).Should(Equal("configType")) }) It("should push v1.1-rc.4 artifact", func() { From d16adda87ef331a6fc5453371e249c4091a83e2e Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Sun, 7 Apr 2024 09:07:32 +0000 Subject: [PATCH 09/12] refined Signed-off-by: Xiaoxuan Wang --- test/e2e/suite/command/push.go | 9 --------- 1 file changed, 9 deletions(-) diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index d8dc3e412..c74ae0e10 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -376,17 +376,8 @@ var _ = Describe("Remote registry users:", func() { tempDir := PrepareTempFiles() ORAS("push", RegistryRef(ZOTHost, repo, tag), foobar.FileBarName, "-v", "--image-spec", "v1.1"). - MatchStatus([]match.StateKey{foobar.FileBarStateKey, artifact.DefaultConfigStateKey}, true, 2). MatchKeyWords("ArtifactType: ", "application/vnd.unknown.artifact.v1"). WithWorkDir(tempDir).Exec() - - // validate - fetched := ORAS("manifest", "fetch", RegistryRef(ZOTHost, repo, tag)).Exec().Out.Contents() - var manifest ocispec.Manifest - Expect(json.Unmarshal(fetched, &manifest)).ShouldNot(HaveOccurred()) - Expect(manifest.ArtifactType).Should(Equal("application/vnd.unknown.artifact.v1")) - Expect(manifest.Layers).Should(ContainElements(foobar.BlobBarDescriptor("application/vnd.oci.image.layer.v1.tar"))) - Expect(manifest.Config).Should(Equal(artifact.EmptyLayerJSON)) }) It("should output artifact type when push is complete for image-spec v1.0 when --config is used", func() { From eb11c71e291139d1f607dfe66b97cdcfe0bff556 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 8 Apr 2024 06:23:55 +0000 Subject: [PATCH 10/12] increase coverage Signed-off-by: Xiaoxuan Wang --- .../display/metadata/text/push_test.go | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 cmd/oras/internal/display/metadata/text/push_test.go diff --git a/cmd/oras/internal/display/metadata/text/push_test.go b/cmd/oras/internal/display/metadata/text/push_test.go new file mode 100644 index 000000000..b715fcd0e --- /dev/null +++ b/cmd/oras/internal/display/metadata/text/push_test.go @@ -0,0 +1,74 @@ +/* +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 text + +import ( + "bytes" + "fmt" + "io" + "testing" + + "github.com/opencontainers/go-digest" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" +) + +// errorWriter implements the io.Writer interface returns an error in Write. +type errorWriter struct{} + +func (w *errorWriter) Write(p []byte) (n int, err error) { + return 0, fmt.Errorf("got an error") +} + +func TestPushHandler_OnCompleted(t *testing.T) { + content := []byte("content") + tests := []struct { + name string + out io.Writer + root ocispec.Descriptor + wantErr bool + }{ + { + "good path", + &bytes.Buffer{}, + ocispec.Descriptor{ + MediaType: "example", + Digest: digest.FromBytes(content), + Size: int64(len(content)), + }, + false, + }, + { + "error path", + &errorWriter{}, + ocispec.Descriptor{ + MediaType: "example", + Digest: digest.FromBytes(content), + Size: int64(len(content)), + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PushHandler{ + out: tt.out, + } + if err := p.OnCompleted(tt.root); (err != nil) != tt.wantErr { + t.Errorf("PushHandler.OnCompleted() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} From 71d56d425eafd4790b984fecaa5ad0f7c63df054 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 8 Apr 2024 06:46:10 +0000 Subject: [PATCH 11/12] resolve comments Signed-off-by: Xiaoxuan Wang --- cmd/oras/internal/display/metadata/text/push_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/oras/internal/display/metadata/text/push_test.go b/cmd/oras/internal/display/metadata/text/push_test.go index b715fcd0e..62d4e0b47 100644 --- a/cmd/oras/internal/display/metadata/text/push_test.go +++ b/cmd/oras/internal/display/metadata/text/push_test.go @@ -25,9 +25,9 @@ import ( ocispec "github.com/opencontainers/image-spec/specs-go/v1" ) -// errorWriter implements the io.Writer interface returns an error in Write. type errorWriter struct{} +// Write implements the io.Writer interface returns an error in Write. func (w *errorWriter) Write(p []byte) (n int, err error) { return 0, fmt.Errorf("got an error") } From 930b46d4627ca710404ffe4ff7c05dafb75029f2 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 8 Apr 2024 06:47:36 +0000 Subject: [PATCH 12/12] fix Signed-off-by: Xiaoxuan Wang --- cmd/oras/internal/display/metadata/text/push_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/oras/internal/display/metadata/text/push_test.go b/cmd/oras/internal/display/metadata/text/push_test.go index 62d4e0b47..706f4dc75 100644 --- a/cmd/oras/internal/display/metadata/text/push_test.go +++ b/cmd/oras/internal/display/metadata/text/push_test.go @@ -27,7 +27,7 @@ import ( type errorWriter struct{} -// Write implements the io.Writer interface returns an error in Write. +// Write implements the io.Writer interface and returns an error in Write. func (w *errorWriter) Write(p []byte) (n int, err error) { return 0, fmt.Errorf("got an error") }