From bc0228a0fb1344999256804033fd7670ce7e4c0a Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Tue, 14 Mar 2023 09:32:08 +0000 Subject: [PATCH 1/2] test(e2e): OCI layout specs for `oras attach` Signed-off-by: Billy Zha --- test/e2e/suite/command/attach.go | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/e2e/suite/command/attach.go b/test/e2e/suite/command/attach.go index c9faa4180..9bd6bbae3 100644 --- a/test/e2e/suite/command/attach.go +++ b/test/e2e/suite/command/attach.go @@ -182,3 +182,71 @@ var _ = Describe("Fallback registry users:", func() { }) }) }) + +var _ = Describe("OCI image layout users:", func() { + When("running attach command", func() { + prepare := func(root string) { + ORAS("cp", RegistryRef(Host, ImageRepo, foobar.Tag), Flags.ToLayout, LayoutRef(root, foobar.Tag)).Exec() + } + It("should attach a file to a subject", func() { + root := PrepareTempFiles() + subjectRef := LayoutRef(root, foobar.Tag) + prepare(root) + ORAS("attach", "--artifact-type", "test.attach", Flags.Layout, subjectRef, fmt.Sprintf("%s:%s", foobar.AttachFileName, foobar.AttachFileMedia)). + WithWorkDir(root). + MatchStatus([]match.StateKey{foobar.AttachFileStateKey}, false, 1).Exec() + }) + + It("should attach a file to a subject and export the built manifest", func() { + // prepare + root := PrepareTempFiles() + exportName := "manifest.json" + subjectRef := LayoutRef(root, foobar.Tag) + prepare(root) + // test + ORAS("attach", "--artifact-type", "test.attach", Flags.Layout, subjectRef, fmt.Sprintf("%s:%s", foobar.AttachFileName, foobar.AttachFileMedia), "--export-manifest", exportName). + WithWorkDir(root). + MatchStatus([]match.StateKey{foobar.AttachFileStateKey}, false, 1).Exec() + // validate + var index ocispec.Index + bytes := ORAS("discover", Flags.Layout, subjectRef, "-o", "json").Exec().Out.Contents() + Expect(json.Unmarshal(bytes, &index)).ShouldNot(HaveOccurred()) + Expect(len(index.Manifests)).To(Equal(1)) + Expect(index.Manifests[0].MediaType).To(Equal(ocispec.MediaTypeImageManifest)) + fetched := ORAS("manifest", "fetch", Flags.Layout, LayoutRef(root, index.Manifests[0].Digest.String())).Exec().Out.Contents() + MatchFile(filepath.Join(root, exportName), string(fetched), DefaultTimeout) + }) + It("should attach a file via a OCI Image", func() { + root := PrepareTempFiles() + subjectRef := LayoutRef(root, foobar.Tag) + prepare(root) + // test + ORAS("attach", "--artifact-type", "test.attach", Flags.Layout, subjectRef, fmt.Sprintf("%s:%s", foobar.AttachFileName, foobar.AttachFileMedia), "--image-spec", "v1.1-image"). + WithWorkDir(root). + MatchStatus([]match.StateKey{foobar.AttachFileStateKey}, false, 1).Exec() + + // validate + var index ocispec.Index + bytes := ORAS("discover", subjectRef, Flags.Layout, "-o", "json").Exec().Out.Contents() + Expect(json.Unmarshal(bytes, &index)).ShouldNot(HaveOccurred()) + Expect(len(index.Manifests)).To(Equal(1)) + Expect(index.Manifests[0].MediaType).To(Equal(ocispec.MediaTypeImageManifest)) + }) + It("should attach a file via a OCI Artifact", func() { + root := PrepareTempFiles() + subjectRef := LayoutRef(root, foobar.Tag) + prepare(root) + // test + ORAS("attach", "--artifact-type", "test.attach", subjectRef, Flags.Layout, fmt.Sprintf("%s:%s", foobar.AttachFileName, foobar.AttachFileMedia), "--image-spec", "v1.1-artifact"). + WithWorkDir(root). + MatchStatus([]match.StateKey{foobar.AttachFileStateKey}, false, 1).Exec() + + // validate + var index ocispec.Index + bytes := ORAS("discover", subjectRef, Flags.Layout, "-o", "json").Exec().Out.Contents() + Expect(json.Unmarshal(bytes, &index)).ShouldNot(HaveOccurred()) + Expect(len(index.Manifests)).To(Equal(1)) + Expect(index.Manifests[0].MediaType).To(Equal(ocispec.MediaTypeArtifactManifest)) + }) + }) +}) From 13c4432a7c50dfd3fdd7be993886319fcc1f57b1 Mon Sep 17 00:00:00 2001 From: Billy Zha Date: Tue, 14 Mar 2023 09:36:36 +0000 Subject: [PATCH 2/2] add failure tests Signed-off-by: Billy Zha --- test/e2e/suite/command/attach.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/e2e/suite/command/attach.go b/test/e2e/suite/command/attach.go index 9bd6bbae3..e761a1a0e 100644 --- a/test/e2e/suite/command/attach.go +++ b/test/e2e/suite/command/attach.go @@ -50,10 +50,16 @@ var _ = Describe("ORAS beginners:", func() { ORAS("attach", "--artifact-type", "oras.test").ExpectFailure().MatchErrKeyWords("Error:").Exec() }) - It("should fail if no file reference or manifest annotation provided", func() { + It("should fail if no file reference or manifest annotation provided for registry", func() { ORAS("attach", "--artifact-type", "oras.test", RegistryRef(Host, ImageRepo, foobar.Tag)). ExpectFailure().MatchErrKeyWords("Error: no blob or manifest annotation are provided").Exec() }) + + It("should fail if no file reference or manifest annotation provided for OCI layout", func() { + root := GinkgoT().TempDir() + ORAS("attach", "--artifact-type", "oras.test", LayoutRef(root, foobar.Tag)). + ExpectFailure().MatchErrKeyWords("Error: no blob or manifest annotation are provided").Exec() + }) }) })