From 009413affd1a54f29f24ec87be9b80d1b3f402f9 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Mon, 11 Jul 2022 17:15:22 +0100 Subject: [PATCH] Add test for annotations Signed-off-by: Somtochi Onyekwere --- internal/oci/list_test.go | 15 +++++++++++++++ internal/oci/push_pull_test.go | 10 +++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/internal/oci/list_test.go b/internal/oci/list_test.go index 0a21237d..c67a3a6c 100644 --- a/internal/oci/list_test.go +++ b/internal/oci/list_test.go @@ -23,6 +23,8 @@ import ( "github.com/google/go-containerregistry/pkg/crane" "github.com/google/go-containerregistry/pkg/name" + gcrv1 "github.com/google/go-containerregistry/pkg/v1" + "github.com/google/go-containerregistry/pkg/v1/mutate" "github.com/google/go-containerregistry/pkg/v1/random" . "github.com/onsi/gomega" ) @@ -32,11 +34,18 @@ func Test_List(t *testing.T) { ctx := context.Background() repo := "test-list" + randStringRunes(5) tags := []string{"v0.0.1", "v0.0.2", "v0.0.3"} + source := "github.com/fluxcd/fluxv2" + rev := "rev" + m := Metadata{ + Source: source, + Revision: rev, + } for _, tag := range tags { dst := fmt.Sprintf("%s/%s:%s", dockerReg, repo, tag) img, err := random.Image(1024, 1) g.Expect(err).ToNot(HaveOccurred()) + img = mutate.Annotations(img, m.ToAnnotations()).(gcrv1.Image) err = crane.Push(img, dst, craneOptions(ctx)...) g.Expect(err).ToNot(HaveOccurred()) } @@ -49,5 +58,11 @@ func Test_List(t *testing.T) { tag, err := name.NewTag(meta.URL) g.Expect(err).ToNot(HaveOccurred()) g.Expect(tag.TagStr()).Should(BeElementOf(tags)) + + g.Expect(meta.ToAnnotations()).To(Equal(m.ToAnnotations())) + + digest, err := crane.Digest(meta.URL, craneOptions(ctx)...) + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(meta.Digest).To(Equal(digest)) } } diff --git a/internal/oci/push_pull_test.go b/internal/oci/push_pull_test.go index c37c92d2..1083dcdd 100644 --- a/internal/oci/push_pull_test.go +++ b/internal/oci/push_pull_test.go @@ -36,9 +36,10 @@ func Test_Push_Pull(t *testing.T) { url := fmt.Sprintf("%s/%s:%s", dockerReg, repo, tag) metadata := Metadata{ - Source: "github.com/fluxcd/fluxv2", + Source: "github.com/fluxcd/flux2", Revision: "rev", } + annotations := metadata.ToAnnotations() testDir := "testdata/build" _, err := Push(ctx, url, testDir, metadata) @@ -48,6 +49,13 @@ func Test_Push_Pull(t *testing.T) { g.Expect(err).ToNot(HaveOccurred()) g.Expect(len(tags)).To(BeEquivalentTo(1)) + image, err := crane.Pull(fmt.Sprintf("%s/%s:%s", dockerReg, repo, tag)) + g.Expect(err).ToNot(HaveOccurred()) + + manifest, err := image.Manifest() + g.Expect(err).ToNot(HaveOccurred()) + g.Expect(manifest.Annotations).To(BeEquivalentTo(annotations)) + tmpDir := t.TempDir() _, err = Pull(ctx, url, tmpDir) g.Expect(err).ToNot(HaveOccurred())