From f03e1b1db26750102151f381bdef17b60bf008c5 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Tue, 22 Nov 2022 15:33:15 +0800 Subject: [PATCH 01/19] test: add utility for file matching Signed-off-by: Billy Zha --- test/e2e/internal/utils/file.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/e2e/internal/utils/file.go b/test/e2e/internal/utils/file.go index 9d117cb82..de072b192 100644 --- a/test/e2e/internal/utils/file.go +++ b/test/e2e/internal/utils/file.go @@ -18,6 +18,10 @@ import ( "io/fs" "os" "path/filepath" + "time" + + . "github.com/onsi/gomega" + "github.com/onsi/gomega/gbytes" ) var testFileRoot string @@ -48,6 +52,15 @@ func CopyTestData(dstRoot string) error { }) } +// MatchFile reads content of filepath, matches it with want with timeout. +func MatchFile(filepath string, want string, timeout time.Duration) { + Expect(filepath).Should(BeAnExistingFile()) + f, err := os.Open(filepath) + Expect(err).ShouldNot(HaveOccurred()) + defer f.Close() + Eventually(gbytes.BufferReader(f)).Should(gbytes.Say(want)) +} + func copyFile(srcFile, dstFile string) error { to, err := os.Create(dstFile) if err != nil { From b78daa59dcc6f872f425233a228aef679790f45e Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Tue, 22 Nov 2022 15:36:53 +0800 Subject: [PATCH 02/19] code clean & add timeout Signed-off-by: Billy Zha --- test/e2e/internal/utils/file.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/internal/utils/file.go b/test/e2e/internal/utils/file.go index de072b192..ad3433458 100644 --- a/test/e2e/internal/utils/file.go +++ b/test/e2e/internal/utils/file.go @@ -52,13 +52,13 @@ func CopyTestData(dstRoot string) error { }) } -// MatchFile reads content of filepath, matches it with want with timeout. +// MatchFile reads content from filepath, matches it with want with timeout. func MatchFile(filepath string, want string, timeout time.Duration) { Expect(filepath).Should(BeAnExistingFile()) f, err := os.Open(filepath) Expect(err).ShouldNot(HaveOccurred()) defer f.Close() - Eventually(gbytes.BufferReader(f)).Should(gbytes.Say(want)) + Eventually(gbytes.BufferReader(f)).WithTimeout(timeout).Should(gbytes.Say(want)) } func copyFile(srcFile, dstFile string) error { From d6c36f4ecd3707e0c69419103c21e53e77be4b6b Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Tue, 22 Nov 2022 15:58:30 +0800 Subject: [PATCH 03/19] use file utility Signed-off-by: Billy Zha --- test/e2e/internal/utils/exec.go | 3 ++- test/e2e/suite/command/manifest.go | 3 +-- test/e2e/suite/command/push.go | 6 ++---- test/e2e/suite/scenario/oci_image.go | 7 +++---- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/test/e2e/internal/utils/exec.go b/test/e2e/internal/utils/exec.go index 1ed1058ab..d849973c4 100644 --- a/test/e2e/internal/utils/exec.go +++ b/test/e2e/internal/utils/exec.go @@ -34,6 +34,7 @@ const ( Username = "hello" Password = "oras-test" AuthConfigPath = "test.config" + DefaultTimeout = 10 * time.Second ) // ExecOption provides option used to execute a command. @@ -61,7 +62,7 @@ func Binary(path string, args ...string) *ExecOption { return &ExecOption{ binary: path, args: args, - timeout: 10 * time.Second, + timeout: DefaultTimeout, shouldFail: false, } } diff --git a/test/e2e/suite/command/manifest.go b/test/e2e/suite/command/manifest.go index a4aadae9f..ca6d9d563 100644 --- a/test/e2e/suite/command/manifest.go +++ b/test/e2e/suite/command/manifest.go @@ -85,8 +85,7 @@ var _ = Describe("Common registry users:", func() { fetchPath := filepath.Join(GinkgoT().TempDir(), "fetchedImage") ORAS("manifest", "fetch", Reference(Host, repo, multiImage), "--output", fetchPath, "--descriptor"). MatchContent(descriptor_multi).Exec() - Binary("cat", fetchPath). - MatchContent(manifest_multi).Exec() + MatchFile(fetchPath, manifest_multi, DefaultTimeout) }) It("should fetch manifest via tag with platform selection", func() { diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index 5aeedce3d..facbc3329 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -84,10 +84,8 @@ var _ = Describe("Remote registry users:", func() { ORAS("push", Reference(Host, repo, tag), files[1]+":"+layerType, "-v", "--export-manifest", exportPath). MatchStatus(statusKeys, true, 2). WithWorkDir(tempDir).Exec() - fetched := ORAS("manifest", "fetch", Reference(Host, repo, tag)).Exec().Out - Binary("cat", exportPath). - WithWorkDir(tempDir). - MatchTrimmedContent(string(fetched.Contents())).Exec() + fetched := ORAS("manifest", "fetch", Reference(Host, repo, tag)).Exec().Out.Contents() + MatchFile(exportPath, string(fetched), DefaultTimeout) }) It("should push files with customized config file", func() { diff --git a/test/e2e/suite/scenario/oci_image.go b/test/e2e/suite/scenario/oci_image.go index 011df9851..d07b37ea2 100644 --- a/test/e2e/suite/scenario/oci_image.go +++ b/test/e2e/suite/scenario/oci_image.go @@ -55,10 +55,9 @@ var _ = Describe("OCI image user:", Ordered, func() { WithWorkDir(tempDir). WithDescription("push files with manifest exported").Exec() - session := Binary("cat", manifestName).WithWorkDir(tempDir).Exec() - ORAS("manifest", "fetch", Reference(Host, repo, tag)). - MatchContent(string(session.Out.Contents())). - WithDescription("fetch pushed manifest content").Exec() + fetched := ORAS("manifest", "fetch", Reference(Host, repo, tag)). + WithDescription("fetch pushed manifest content").Exec().Out.Contents() + MatchFile(filepath.Join(tempDir, manifestName), string(fetched), DefaultTimeout) pullRoot := "pulled" ORAS("pull", Reference(Host, repo, tag), "-v", "--config", files[0], "-o", pullRoot). From c73b59d9aba20f088bb8fecb76c61bd671527f41 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Wed, 23 Nov 2022 11:03:26 +0800 Subject: [PATCH 04/19] bug fix Signed-off-by: Billy Zha --- test/e2e/internal/utils/file.go | 5 +++-- test/e2e/suite/command/push.go | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/test/e2e/internal/utils/file.go b/test/e2e/internal/utils/file.go index ad3433458..2d95f883f 100644 --- a/test/e2e/internal/utils/file.go +++ b/test/e2e/internal/utils/file.go @@ -21,7 +21,6 @@ import ( "time" . "github.com/onsi/gomega" - "github.com/onsi/gomega/gbytes" ) var testFileRoot string @@ -58,7 +57,9 @@ func MatchFile(filepath string, want string, timeout time.Duration) { f, err := os.Open(filepath) Expect(err).ShouldNot(HaveOccurred()) defer f.Close() - Eventually(gbytes.BufferReader(f)).WithTimeout(timeout).Should(gbytes.Say(want)) + content, err := os.ReadFile(filepath) + Expect(err).ShouldNot(HaveOccurred()) + Eventually(string(content)).WithTimeout(timeout).Should(Equal(want)) } func copyFile(srcFile, dstFile string) error { diff --git a/test/e2e/suite/command/push.go b/test/e2e/suite/command/push.go index facbc3329..ff4861942 100644 --- a/test/e2e/suite/command/push.go +++ b/test/e2e/suite/command/push.go @@ -16,6 +16,7 @@ package command import ( "bytes" "fmt" + "path/filepath" . "github.com/onsi/ginkgo/v2" ocispec "github.com/opencontainers/image-spec/specs-go/v1" @@ -85,7 +86,7 @@ var _ = Describe("Remote registry users:", func() { MatchStatus(statusKeys, true, 2). WithWorkDir(tempDir).Exec() fetched := ORAS("manifest", "fetch", Reference(Host, repo, tag)).Exec().Out.Contents() - MatchFile(exportPath, string(fetched), DefaultTimeout) + MatchFile(filepath.Join(tempDir, exportPath), string(fetched), DefaultTimeout) }) It("should push files with customized config file", func() { From 64a7be55c0967613978875317b1ab576828f2f3a Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Wed, 23 Nov 2022 11:07:46 +0800 Subject: [PATCH 05/19] fix bug Signed-off-by: Billy Zha --- test/e2e/internal/utils/file.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/e2e/internal/utils/file.go b/test/e2e/internal/utils/file.go index 2d95f883f..2e8ce9858 100644 --- a/test/e2e/internal/utils/file.go +++ b/test/e2e/internal/utils/file.go @@ -18,9 +18,11 @@ import ( "io/fs" "os" "path/filepath" + "regexp" "time" . "github.com/onsi/gomega" + "github.com/onsi/gomega/gbytes" ) var testFileRoot string @@ -57,9 +59,8 @@ func MatchFile(filepath string, want string, timeout time.Duration) { f, err := os.Open(filepath) Expect(err).ShouldNot(HaveOccurred()) defer f.Close() - content, err := os.ReadFile(filepath) - Expect(err).ShouldNot(HaveOccurred()) - Eventually(string(content)).WithTimeout(timeout).Should(Equal(want)) + want = regexp.QuoteMeta(want) + Eventually(gbytes.BufferReader(f)).WithTimeout(timeout).Should(gbytes.Say(want)) } func copyFile(srcFile, dstFile string) error { From b59435cfe13791b2c298f8598643298688ee7105 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Wed, 23 Nov 2022 11:08:23 +0800 Subject: [PATCH 06/19] code clean Signed-off-by: Billy Zha --- test/e2e/internal/utils/file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/internal/utils/file.go b/test/e2e/internal/utils/file.go index 2e8ce9858..db931f8e9 100644 --- a/test/e2e/internal/utils/file.go +++ b/test/e2e/internal/utils/file.go @@ -57,7 +57,7 @@ func CopyTestData(dstRoot string) error { func MatchFile(filepath string, want string, timeout time.Duration) { Expect(filepath).Should(BeAnExistingFile()) f, err := os.Open(filepath) - Expect(err).ShouldNot(HaveOccurred()) + Expect(err).ToNot(HaveOccurred()) defer f.Close() want = regexp.QuoteMeta(want) Eventually(gbytes.BufferReader(f)).WithTimeout(timeout).Should(gbytes.Say(want)) From d9b88c30a6f57e2b07be8a674f6613e0e1237ed9 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Wed, 23 Nov 2022 11:15:52 +0800 Subject: [PATCH 07/19] code clean Signed-off-by: Billy Zha --- test/e2e/internal/utils/file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/internal/utils/file.go b/test/e2e/internal/utils/file.go index db931f8e9..22084c9f0 100644 --- a/test/e2e/internal/utils/file.go +++ b/test/e2e/internal/utils/file.go @@ -55,7 +55,7 @@ func CopyTestData(dstRoot string) error { // MatchFile reads content from filepath, matches it with want with timeout. func MatchFile(filepath string, want string, timeout time.Duration) { - Expect(filepath).Should(BeAnExistingFile()) + Expect(filepath).To(BeAnExistingFile()) f, err := os.Open(filepath) Expect(err).ToNot(HaveOccurred()) defer f.Close() From b256c8703b878644425c25443213d751fb47391a Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Mon, 28 Nov 2022 19:20:32 +0800 Subject: [PATCH 08/19] test: add spec to cover `blob push` Signed-off-by: Billy Zha --- test/e2e/internal/utils/file.go | 11 +++ test/e2e/suite/auth/auth.go | 1 + test/e2e/suite/command/blob.go | 116 ++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 test/e2e/suite/command/blob.go diff --git a/test/e2e/internal/utils/file.go b/test/e2e/internal/utils/file.go index 22084c9f0..f4c0711ba 100644 --- a/test/e2e/internal/utils/file.go +++ b/test/e2e/internal/utils/file.go @@ -21,6 +21,8 @@ import ( "regexp" "time" + . "github.com/onsi/ginkgo/v2" + "github.com/onsi/gomega" . "github.com/onsi/gomega" "github.com/onsi/gomega/gbytes" ) @@ -63,6 +65,15 @@ func MatchFile(filepath string, want string, timeout time.Duration) { Eventually(gbytes.BufferReader(f)).WithTimeout(timeout).Should(gbytes.Say(want)) } +// WriteTempFile writes content into name under a temp folder. +func WriteTempFile(name string, content string) (path string) { + tempDir := GinkgoT().TempDir() + path = filepath.Join(tempDir, name) + err := os.WriteFile(path, []byte(content), 0777) + Expect(err).ToNot(gomega.HaveOccurred()) + return path +} + func copyFile(srcFile, dstFile string) error { to, err := os.Create(dstFile) if err != nil { diff --git a/test/e2e/suite/auth/auth.go b/test/e2e/suite/auth/auth.go index c62bc2f4e..9c37ac407 100644 --- a/test/e2e/suite/auth/auth.go +++ b/test/e2e/suite/auth/auth.go @@ -33,6 +33,7 @@ var _ = Describe("Common registry user", Ordered, func() { RunWithoutLogin("push", "-a", "key=value", Host+"/repo:tag") RunWithoutLogin("pull", Host+"/repo:tag") RunWithoutLogin("manifest", "fetch", Host+"/repo:tag") + RunWithoutLogin("blob", "push", Host+"/repo:tag") }) }) diff --git a/test/e2e/suite/command/blob.go b/test/e2e/suite/command/blob.go new file mode 100644 index 000000000..612fc06bd --- /dev/null +++ b/test/e2e/suite/command/blob.go @@ -0,0 +1,116 @@ +/* +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 command + +import ( + "fmt" + "strconv" + "strings" + + . "github.com/onsi/ginkgo/v2" + "oras.land/oras/cmd/oras/blob" + . "oras.land/oras/test/e2e/internal/utils" +) + +var pushContent = "test-blob" +var pushLength = strconv.Itoa(len(pushContent)) +var pushDigest = "sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d8" +var wrongDigest = "sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d9" +var pushDescFmt = `{"mediaType":"%s","digest":"sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d8","size":9}` +var repoFmt = fmt.Sprintf("command/blob/push/%d/%%s", GinkgoRandomSeed()) + +var _ = Describe("ORAS beginners:", func() { + When("running blob command", func() { + runAndShowPreviewInHelp([]string{"blob"}) + + When("running `blob push`", func() { + runAndShowPreviewInHelp([]string{"blob", "push"}, preview_desc, example_desc) + It("should fail to read blob content and password from stdin at the same time", func() { + repo := fmt.Sprintf(repoFmt, "password-stdin") + ORAS("blob", "push", Reference(Host, repo, ""), "--password-stdin", "-"). + WithFailureCheck(). + MatchTrimmedContent("Error: `-` read file from input and `--password-stdin` read password from input cannot be both used").Exec() + }) + It("should fail to push a blob from stdin but no blob size provided", func() { + repo := fmt.Sprintf(repoFmt, "no-size") + ORAS("blob", "push", Reference(Host, repo, pushDigest), "-"). + WithInput(strings.NewReader(pushContent)). + WithFailureCheck(). + MatchTrimmedContent("Error: `--size` must be provided if the blob is read from stdin").Exec() + }) + + It("should fail to push a blob from stdin if invalid blob size provided", func() { + repo := fmt.Sprintf(repoFmt, "invalid-stdin-size") + blob.Cmd().ExecuteC() + ORAS("blob", "push", Reference(Host, repo, pushDigest), "-", "--size", "3"). + WithInput(strings.NewReader(pushContent)).WithFailureCheck(). + Exec() + }) + + It("should fail to push a blob from stdin if invalid digest provided", func() { + repo := fmt.Sprintf(repoFmt, "invalid-stdin-digest") + ORAS("blob", "push", Reference(Host, repo, wrongDigest), "-", "--size", pushLength). + WithInput(strings.NewReader(pushContent)).WithFailureCheck(). + Exec() + }) + + It("should fail to push a blob from file if invalid blob size provided", func() { + repo := fmt.Sprintf(repoFmt, "invalid-file-digest") + blobPath := WriteTempFile("blob", pushContent) + ORAS("blob", "push", Reference(Host, repo, pushDigest), blobPath, "--size", "3"). + WithFailureCheck(). + Exec() + }) + + It("should fail to push a blob from file if invalid digest provided", func() { + repo := fmt.Sprintf(repoFmt, "invalid-stdin-size") + blobPath := WriteTempFile("blob", pushContent) + ORAS("blob", "push", Reference(Host, repo, wrongDigest), blobPath, "--size", string(rune(len(pushContent)))). + WithInput(strings.NewReader(pushContent)).WithFailureCheck(). + Exec() + }) + + It("should fail if no reference is provided", func() { + ORAS("blob", "push").WithFailureCheck().Exec() + }) + }) + }) +}) + +var _ = Describe("Common registry users:", func() { + When("running `blob push`", func() { + It("should push a blob from a file and output the descriptor with specific media-type", func() { + mediaType := "test.media" + repo := fmt.Sprintf(repoFmt, "blob-file-media-type") + blobPath := WriteTempFile("blob", pushContent) + ORAS("blob", "push", Reference(Host, repo, ""), blobPath, "--media-type", mediaType, "--descriptor"). + MatchContent(fmt.Sprintf(pushDescFmt, mediaType)).Exec() + ORAS("blob", "fetch", Reference(Host, repo, pushDigest), "--output", "-").MatchContent(pushContent).Exec() + + ORAS("blob", "push", Reference(Host, repo, ""), blobPath, "-v"). + WithDescription("skip the pushing if the blob already exists in the target repo"). + MatchKeyWords("Exists").Exec() + + }) + + It("should push a blob from a stdin and output the descriptor with specific media-type", func() { + mediaType := "test.media" + repo := fmt.Sprintf(repoFmt, "blob-file-media-type") + ORAS("blob", "push", Reference(Host, repo, pushDigest), "-", "--media-type", mediaType, "--descriptor", "--size", pushLength). + WithInput(strings.NewReader(pushContent)). + MatchContent(fmt.Sprintf(pushDescFmt, mediaType)).Exec() + ORAS("blob", "fetch", Reference(Host, repo, pushDigest), "--output", "-").MatchContent(pushContent).Exec() + }) + }) +}) From de131595ce0d746f3b39545c91b1f3edb4d619a6 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Mon, 28 Nov 2022 19:25:48 +0800 Subject: [PATCH 09/19] fixing unauthed Signed-off-by: Billy Zha --- test/e2e/suite/auth/auth.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/suite/auth/auth.go b/test/e2e/suite/auth/auth.go index 9c37ac407..b76dd0fc3 100644 --- a/test/e2e/suite/auth/auth.go +++ b/test/e2e/suite/auth/auth.go @@ -33,7 +33,7 @@ var _ = Describe("Common registry user", Ordered, func() { RunWithoutLogin("push", "-a", "key=value", Host+"/repo:tag") RunWithoutLogin("pull", Host+"/repo:tag") RunWithoutLogin("manifest", "fetch", Host+"/repo:tag") - RunWithoutLogin("blob", "push", Host+"/repo:tag") + RunWithoutLogin("blob", "push", Host+"/repo", "some.blob") }) }) From e286146c479369ed2bb248bf8efd11df41db285f Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Mon, 28 Nov 2022 19:27:43 +0800 Subject: [PATCH 10/19] add temp file Signed-off-by: Billy Zha --- test/e2e/suite/auth/auth.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/suite/auth/auth.go b/test/e2e/suite/auth/auth.go index b76dd0fc3..62c373262 100644 --- a/test/e2e/suite/auth/auth.go +++ b/test/e2e/suite/auth/auth.go @@ -33,7 +33,7 @@ var _ = Describe("Common registry user", Ordered, func() { RunWithoutLogin("push", "-a", "key=value", Host+"/repo:tag") RunWithoutLogin("pull", Host+"/repo:tag") RunWithoutLogin("manifest", "fetch", Host+"/repo:tag") - RunWithoutLogin("blob", "push", Host+"/repo", "some.blob") + RunWithoutLogin("blob", "push", Host+"/repo", WriteTempFile("blob", "test")) }) }) From 02aed529142c178309e0ed61c818a35cb0b2d015 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 15 Dec 2022 09:57:02 +0800 Subject: [PATCH 11/19] fix merge error Signed-off-by: Billy Zha --- test/e2e/suite/command/blob.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/e2e/suite/command/blob.go b/test/e2e/suite/command/blob.go index 84f35fb6f..84319662c 100644 --- a/test/e2e/suite/command/blob.go +++ b/test/e2e/suite/command/blob.go @@ -14,15 +14,16 @@ limitations under the License. package command import ( + "fmt" "os" "path/filepath" - "fmt" "strconv" "strings" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "oras.land/oras/cmd/oras/blob" "github.com/onsi/gomega/gbytes" + "oras.land/oras/cmd/oras/blob" . "oras.land/oras/test/e2e/internal/utils" ) @@ -87,6 +88,9 @@ var _ = Describe("ORAS beginners:", func() { It("should fail if no reference is provided", func() { ORAS("blob", "push").WithFailureCheck().Exec() + }) + }) + When("running `blob fetch`", func() { runAndShowPreviewInHelp([]string{"blob", "fetch"}, preview_desc, example_desc) @@ -152,10 +156,11 @@ var _ = Describe("Common registry users:", func() { MatchContent(fmt.Sprintf(pushDescFmt, mediaType)).Exec() ORAS("blob", "fetch", Reference(Host, repo, pushDigest), "--output", "-").MatchContent(pushContent).Exec() }) + }) + var blobDigest = "sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae" var blobContent = "foo" var blobDescriptor = `{"mediaType":"application/octet-stream","digest":"sha256:2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae","size":3}` - When("running `blob fetch`", func() { It("should fetch blob descriptor ", func() { ORAS("blob", "fetch", Reference(Host, repo, blobDigest), "--descriptor"). From 545244f9c977933c917698cd72106a123aa07eb9 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 15 Dec 2022 10:44:17 +0800 Subject: [PATCH 12/19] update blob fetch with file utils Signed-off-by: Billy Zha --- test/e2e/suite/command/blob.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/test/e2e/suite/command/blob.go b/test/e2e/suite/command/blob.go index 84319662c..c8467574b 100644 --- a/test/e2e/suite/command/blob.go +++ b/test/e2e/suite/command/blob.go @@ -15,14 +15,12 @@ package command import ( "fmt" - "os" "path/filepath" "strconv" "strings" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "github.com/onsi/gomega/gbytes" "oras.land/oras/cmd/oras/blob" . "oras.land/oras/test/e2e/internal/utils" @@ -175,11 +173,7 @@ var _ = Describe("Common registry users:", func() { contentPath := filepath.Join(tempDir, "fetched") ORAS("blob", "fetch", Reference(Host, repo, blobDigest), "--output", contentPath). WithWorkDir(tempDir).Exec() - Expect(contentPath).Should(BeAnExistingFile()) - f, err := os.Open(contentPath) - Expect(err).ShouldNot(HaveOccurred()) - defer f.Close() - Eventually(gbytes.BufferReader(f)).Should(gbytes.Say(blobContent)) + MatchFile(contentPath, blobContent, DefaultTimeout) }) It("should fetch blob descriptor and output content to a file", func() { tempDir := GinkgoT().TempDir() @@ -187,11 +181,7 @@ var _ = Describe("Common registry users:", func() { ORAS("blob", "fetch", Reference(Host, repo, blobDigest), "--output", contentPath, "--descriptor"). MatchContent(blobDescriptor). WithWorkDir(tempDir).Exec() - Expect(contentPath).Should(BeAnExistingFile()) - f, err := os.Open(contentPath) - Expect(err).ShouldNot(HaveOccurred()) - defer f.Close() - Eventually(gbytes.BufferReader(f)).Should(gbytes.Say(blobContent)) + MatchFile(contentPath, blobContent, DefaultTimeout) }) }) }) From 153f87321268e7d2dfb391b138b4d35c6e4d77f6 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 15 Dec 2022 10:53:25 +0800 Subject: [PATCH 13/19] code clean Signed-off-by: Billy Zha --- test/e2e/suite/command/blob.go | 1 - 1 file changed, 1 deletion(-) diff --git a/test/e2e/suite/command/blob.go b/test/e2e/suite/command/blob.go index c8467574b..e8f4cc23b 100644 --- a/test/e2e/suite/command/blob.go +++ b/test/e2e/suite/command/blob.go @@ -20,7 +20,6 @@ import ( "strings" . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" "oras.land/oras/cmd/oras/blob" . "oras.land/oras/test/e2e/internal/utils" From 0744ec555b4513ed23740afa31a5743723839317 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 15 Dec 2022 10:54:09 +0800 Subject: [PATCH 14/19] code clean Signed-off-by: Billy Zha --- test/e2e/suite/command/blob.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/e2e/suite/command/blob.go b/test/e2e/suite/command/blob.go index e8f4cc23b..f056efb6d 100644 --- a/test/e2e/suite/command/blob.go +++ b/test/e2e/suite/command/blob.go @@ -20,7 +20,6 @@ import ( "strings" . "github.com/onsi/ginkgo/v2" - "oras.land/oras/cmd/oras/blob" . "oras.land/oras/test/e2e/internal/utils" ) @@ -54,7 +53,6 @@ var _ = Describe("ORAS beginners:", func() { It("should fail to push a blob from stdin if invalid blob size provided", func() { repo := fmt.Sprintf(repoFmt, "invalid-stdin-size") - blob.Cmd().ExecuteC() ORAS("blob", "push", Reference(Host, repo, pushDigest), "-", "--size", "3"). WithInput(strings.NewReader(pushContent)).WithFailureCheck(). Exec() From 26836f8e00a9d5b8b1d5432aac215a24c7ca10bf Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 15 Dec 2022 15:23:10 +0800 Subject: [PATCH 15/19] update func name Signed-off-by: Billy Zha --- test/e2e/suite/command/blob.go | 6 +++--- test/e2e/suite/command/manifest.go | 6 +++--- test/e2e/suite/command/repo.go | 2 +- test/e2e/suite/command/tag.go | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/e2e/suite/command/blob.go b/test/e2e/suite/command/blob.go index f056efb6d..ef3983679 100644 --- a/test/e2e/suite/command/blob.go +++ b/test/e2e/suite/command/blob.go @@ -33,10 +33,10 @@ var repoFmt = fmt.Sprintf("command/blob/push/%d/%%s", GinkgoRandomSeed()) var _ = Describe("ORAS beginners:", func() { When("running blob command", func() { - runAndShowPreviewInHelp([]string{"blob"}) + RunAndShowPreviewInHelp([]string{"blob"}) When("running `blob push`", func() { - runAndShowPreviewInHelp([]string{"blob", "push"}, preview_desc, example_desc) + RunAndShowPreviewInHelp([]string{"blob", "push"}, preview_desc, example_desc) It("should fail to read blob content and password from stdin at the same time", func() { repo := fmt.Sprintf(repoFmt, "password-stdin") ORAS("blob", "push", Reference(Host, repo, ""), "--password-stdin", "-"). @@ -87,7 +87,7 @@ var _ = Describe("ORAS beginners:", func() { }) When("running `blob fetch`", func() { - runAndShowPreviewInHelp([]string{"blob", "fetch"}, preview_desc, example_desc) + RunAndShowPreviewInHelp([]string{"blob", "fetch"}, preview_desc, example_desc) It("should call sub-commands with aliases", func() { ORAS("blob", "get", "--help"). diff --git a/test/e2e/suite/command/manifest.go b/test/e2e/suite/command/manifest.go index 81e1afbcb..7a9c8aaa1 100644 --- a/test/e2e/suite/command/manifest.go +++ b/test/e2e/suite/command/manifest.go @@ -45,8 +45,8 @@ const ( var _ = Describe("ORAS beginners:", func() { When("running manifest command", func() { - runAndShowPreviewInHelp([]string{"manifest"}) - runAndShowPreviewInHelp([]string{"manifest", "fetch"}, preview_desc, example_desc) + RunAndShowPreviewInHelp([]string{"manifest"}) + RunAndShowPreviewInHelp([]string{"manifest", "fetch"}, preview_desc, example_desc) It("should call sub-commands with aliases", func() { ORAS("manifest", "get", "--help"). @@ -80,7 +80,7 @@ var _ = Describe("ORAS beginners:", func() { }) }) -func runAndShowPreviewInHelp(args []string, keywords ...string) { +func RunAndShowPreviewInHelp(args []string, keywords ...string) { It(fmt.Sprintf("should run %q command", strings.Join(args, " ")), func() { ORAS(append(args, "--help")...). MatchKeyWords(append(keywords, "[Preview] "+args[len(args)-1], "\nUsage:")...). diff --git a/test/e2e/suite/command/repo.go b/test/e2e/suite/command/repo.go index 129496240..7a1903957 100644 --- a/test/e2e/suite/command/repo.go +++ b/test/e2e/suite/command/repo.go @@ -22,7 +22,7 @@ import ( var _ = Describe("ORAS beginners:", func() { When("running repo command", func() { - runAndShowPreviewInHelp([]string{"repo"}) + RunAndShowPreviewInHelp([]string{"repo"}) When("running `repo ls`", func() { It("should show preview in help", func() { ORAS("repo", "ls", "--help").MatchKeyWords("[Preview] List", preview_desc, example_desc).Exec() diff --git a/test/e2e/suite/command/tag.go b/test/e2e/suite/command/tag.go index 545c16ee9..b1463c563 100644 --- a/test/e2e/suite/command/tag.go +++ b/test/e2e/suite/command/tag.go @@ -24,7 +24,7 @@ var repoFmt = fmt.Sprintf("command/tag/%%s/%d/%%s", GinkgoRandomSeed()) var _ = Describe("ORAS beginners:", func() { When("running repo command", func() { - runAndShowPreviewInHelp([]string{"tag"}) + RunAndShowPreviewInHelp([]string{"tag"}) It("should fail when no manifest reference provided", func() { ORAS("tag").WithFailureCheck().MatchErrKeyWords("Error:").Exec() }) From fd33cb0951439d563ebfaf480e44f6531082aad1 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 15 Dec 2022 15:30:16 +0800 Subject: [PATCH 16/19] code clean Signed-off-by: Billy Zha --- test/e2e/suite/command/blob.go | 17 +++++++++-------- test/e2e/suite/command/tag.go | 4 ---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/test/e2e/suite/command/blob.go b/test/e2e/suite/command/blob.go index ef3983679..2d66025ad 100644 --- a/test/e2e/suite/command/blob.go +++ b/test/e2e/suite/command/blob.go @@ -20,15 +20,16 @@ import ( "strings" . "github.com/onsi/ginkgo/v2" - . "oras.land/oras/test/e2e/internal/utils" ) -var pushContent = "test-blob" -var pushLength = strconv.Itoa(len(pushContent)) -var pushDigest = "sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d8" -var wrongDigest = "sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d9" -var pushDescFmt = `{"mediaType":"%s","digest":"sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d8","size":9}` +const ( + pushContent = "test-blob" + pushDigest = "sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d8" + wrongDigest = "sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d9" + pushDescFmt = `{"mediaType":"%s","digest":"sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d8","size":9}` +) + var repoFmt = fmt.Sprintf("command/blob/push/%d/%%s", GinkgoRandomSeed()) var _ = Describe("ORAS beginners:", func() { @@ -60,7 +61,7 @@ var _ = Describe("ORAS beginners:", func() { It("should fail to push a blob from stdin if invalid digest provided", func() { repo := fmt.Sprintf(repoFmt, "invalid-stdin-digest") - ORAS("blob", "push", Reference(Host, repo, wrongDigest), "-", "--size", pushLength). + ORAS("blob", "push", Reference(Host, repo, wrongDigest), "-", "--size", strconv.Itoa(len(pushContent))). WithInput(strings.NewReader(pushContent)).WithFailureCheck(). Exec() }) @@ -146,7 +147,7 @@ var _ = Describe("Common registry users:", func() { It("should push a blob from a stdin and output the descriptor with specific media-type", func() { mediaType := "test.media" repo := fmt.Sprintf(repoFmt, "blob-file-media-type") - ORAS("blob", "push", Reference(Host, repo, pushDigest), "-", "--media-type", mediaType, "--descriptor", "--size", pushLength). + ORAS("blob", "push", Reference(Host, repo, pushDigest), "-", "--media-type", mediaType, "--descriptor", "--size", string(rune(len(pushContent)))). WithInput(strings.NewReader(pushContent)). MatchContent(fmt.Sprintf(pushDescFmt, mediaType)).Exec() ORAS("blob", "fetch", Reference(Host, repo, pushDigest), "--output", "-").MatchContent(pushContent).Exec() diff --git a/test/e2e/suite/command/tag.go b/test/e2e/suite/command/tag.go index b1463c563..47d2fa430 100644 --- a/test/e2e/suite/command/tag.go +++ b/test/e2e/suite/command/tag.go @@ -14,14 +14,10 @@ limitations under the License. package command import ( - "fmt" - . "github.com/onsi/ginkgo/v2" . "oras.land/oras/test/e2e/internal/utils" ) -var repoFmt = fmt.Sprintf("command/tag/%%s/%d/%%s", GinkgoRandomSeed()) - var _ = Describe("ORAS beginners:", func() { When("running repo command", func() { RunAndShowPreviewInHelp([]string{"tag"}) From 326ecea9f78c529bad78058b9ab4decfe3b4b164 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 15 Dec 2022 15:41:51 +0800 Subject: [PATCH 17/19] corrected size Signed-off-by: Billy Zha --- test/e2e/suite/command/blob.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/suite/command/blob.go b/test/e2e/suite/command/blob.go index 2d66025ad..8e56ec7ae 100644 --- a/test/e2e/suite/command/blob.go +++ b/test/e2e/suite/command/blob.go @@ -77,7 +77,7 @@ var _ = Describe("ORAS beginners:", func() { It("should fail to push a blob from file if invalid digest provided", func() { repo := fmt.Sprintf(repoFmt, "invalid-stdin-size") blobPath := WriteTempFile("blob", pushContent) - ORAS("blob", "push", Reference(Host, repo, wrongDigest), blobPath, "--size", string(rune(len(pushContent)))). + ORAS("blob", "push", Reference(Host, repo, wrongDigest), blobPath, "--size", strconv.Itoa(len(pushContent))). WithInput(strings.NewReader(pushContent)).WithFailureCheck(). Exec() }) @@ -147,7 +147,7 @@ var _ = Describe("Common registry users:", func() { It("should push a blob from a stdin and output the descriptor with specific media-type", func() { mediaType := "test.media" repo := fmt.Sprintf(repoFmt, "blob-file-media-type") - ORAS("blob", "push", Reference(Host, repo, pushDigest), "-", "--media-type", mediaType, "--descriptor", "--size", string(rune(len(pushContent)))). + ORAS("blob", "push", Reference(Host, repo, pushDigest), "-", "--media-type", mediaType, "--descriptor", "--size", strconv.Itoa(len(pushContent))). WithInput(strings.NewReader(pushContent)). MatchContent(fmt.Sprintf(pushDescFmt, mediaType)).Exec() ORAS("blob", "fetch", Reference(Host, repo, pushDigest), "--output", "-").MatchContent(pushContent).Exec() From f4b5e36ebd1e9489221a9a41ce283437a22554d5 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 15 Dec 2022 15:45:19 +0800 Subject: [PATCH 18/19] resolve comments Signed-off-by: Billy Zha --- test/e2e/internal/utils/file.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/test/e2e/internal/utils/file.go b/test/e2e/internal/utils/file.go index f4c0711ba..e18473bfe 100644 --- a/test/e2e/internal/utils/file.go +++ b/test/e2e/internal/utils/file.go @@ -22,7 +22,6 @@ import ( "time" . "github.com/onsi/ginkgo/v2" - "github.com/onsi/gomega" . "github.com/onsi/gomega" "github.com/onsi/gomega/gbytes" ) @@ -69,8 +68,8 @@ func MatchFile(filepath string, want string, timeout time.Duration) { func WriteTempFile(name string, content string) (path string) { tempDir := GinkgoT().TempDir() path = filepath.Join(tempDir, name) - err := os.WriteFile(path, []byte(content), 0777) - Expect(err).ToNot(gomega.HaveOccurred()) + err := os.WriteFile(path, []byte(content), 0666) + Expect(err).ToNot(HaveOccurred()) return path } From 262fbab917ec821c721e488ad96a22a552de83b8 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Thu, 15 Dec 2022 15:46:40 +0800 Subject: [PATCH 19/19] code clean Signed-off-by: Billy Zha --- test/e2e/suite/command/blob.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/suite/command/blob.go b/test/e2e/suite/command/blob.go index 8e56ec7ae..e5a1c5686 100644 --- a/test/e2e/suite/command/blob.go +++ b/test/e2e/suite/command/blob.go @@ -30,9 +30,8 @@ const ( pushDescFmt = `{"mediaType":"%s","digest":"sha256:e1ca41574914ba00e8ed5c8fc78ec8efdfd48941c7e48ad74dad8ada7f2066d8","size":9}` ) -var repoFmt = fmt.Sprintf("command/blob/push/%d/%%s", GinkgoRandomSeed()) - var _ = Describe("ORAS beginners:", func() { + repoFmt := fmt.Sprintf("command/blob/push/%d/%%s", GinkgoRandomSeed()) When("running blob command", func() { RunAndShowPreviewInHelp([]string{"blob"}) @@ -129,6 +128,7 @@ var _ = Describe("ORAS beginners:", func() { }) var _ = Describe("Common registry users:", func() { + repoFmt := fmt.Sprintf("command/blob/push/%d/%%s", GinkgoRandomSeed()) When("running `blob push`", func() { It("should push a blob from a file and output the descriptor with specific media-type", func() { mediaType := "test.media"