From bd27f9ced0486c7b370ace235b60d59f588bf956 Mon Sep 17 00:00:00 2001 From: Alex Suraci Date: Tue, 7 Aug 2018 15:37:54 -0400 Subject: [PATCH] add tests for check --- check_test.go | 117 +++++++++++++++++++++++++++++++++++++ testdata/setup-images | 7 ++- testdata/static/Dockerfile | 9 +++ 3 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 check_test.go create mode 100644 testdata/static/Dockerfile diff --git a/check_test.go b/check_test.go new file mode 100644 index 0000000..e8cf187 --- /dev/null +++ b/check_test.go @@ -0,0 +1,117 @@ +package resource_test + +import ( + "bytes" + "encoding/json" + "os/exec" + + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" + + resource "github.com/concourse/registry-image-resource" +) + +var _ = Describe("Check", func() { + var req struct { + Source resource.Source + Version *resource.Version + } + + var res []resource.Version + + JustBeforeEach(func() { + cmd := exec.Command(bins.Check) + + payload, err := json.Marshal(req) + Expect(err).ToNot(HaveOccurred()) + + outBuf := new(bytes.Buffer) + + cmd.Stdin = bytes.NewBuffer(payload) + cmd.Stdout = outBuf + cmd.Stderr = GinkgoWriter + + err = cmd.Run() + Expect(err).ToNot(HaveOccurred()) + + err = json.Unmarshal(outBuf.Bytes(), &res) + Expect(err).ToNot(HaveOccurred()) + }) + + Context("when invoked with no cursor version", func() { + BeforeEach(func() { + req.Source = resource.Source{ + Repository: "concourse/test-image-static", + RawTag: "latest", + } + + req.Version = nil + }) + + It("returns the current digest", func() { + Expect(res).To(Equal([]resource.Version{ + {Digest: "sha256:64a6988c58cbdd634198f56452e8f8945e5b54a4bbca4bff7e960e1c830671ff"}, + })) + }) + }) + + Context("when invoked with an up-to-date cursor version", func() { + BeforeEach(func() { + req.Source = resource.Source{ + Repository: "concourse/test-image-static", + RawTag: "latest", + } + + req.Version = &resource.Version{ + Digest: "sha256:64a6988c58cbdd634198f56452e8f8945e5b54a4bbca4bff7e960e1c830671ff", + } + }) + + It("returns the given digest", func() { + Expect(res).To(Equal([]resource.Version{ + {Digest: "sha256:64a6988c58cbdd634198f56452e8f8945e5b54a4bbca4bff7e960e1c830671ff"}, + })) + }) + }) + + Context("when invoked with a valid but out-of-date cursor version", func() { + BeforeEach(func() { + req.Source = resource.Source{ + Repository: "concourse/test-image-static", + RawTag: "latest", + } + + req.Version = &resource.Version{ + // this was previously pushed to the 'latest' tag + Digest: "sha256:031567a617423a84ad68b62267c30693185bd2b92c2668732efc8c70b036bd3a", + } + }) + + It("returns the previous digest and the current digest", func() { + Expect(res).To(Equal([]resource.Version{ + {Digest: "sha256:031567a617423a84ad68b62267c30693185bd2b92c2668732efc8c70b036bd3a"}, + {Digest: "sha256:64a6988c58cbdd634198f56452e8f8945e5b54a4bbca4bff7e960e1c830671ff"}, + })) + }) + }) + + Context("when invoked with an invalid cursor version", func() { + BeforeEach(func() { + req.Source = resource.Source{ + Repository: "concourse/test-image-static", + RawTag: "latest", + } + + req.Version = &resource.Version{ + // (note the end) + Digest: "sha256:031567a617423a84ad68b62267c30693185bd2b92c2668732efc8c70deadbeef", + } + }) + + It("returns only the current digest", func() { + Expect(res).To(Equal([]resource.Version{ + {Digest: "sha256:64a6988c58cbdd634198f56452e8f8945e5b54a4bbca4bff7e960e1c830671ff"}, + })) + }) + }) +}) diff --git a/testdata/setup-images b/testdata/setup-images index 54eea60..f906061 100755 --- a/testdata/setup-images +++ b/testdata/setup-images @@ -4,7 +4,12 @@ set -e -u cd $(dirname $0) -for df in $(find . -name Dockerfile); do +filter="$(echo "$@" | tr ' ' '|')" +if [ -z "$filter" ]; then + filter="." +fi + +for df in $(find . -name Dockerfile | ag "$filter"); do dir=$(dirname $df) name=concourse/test-image-$(basename $dir) echo "building $dir as $name" diff --git a/testdata/static/Dockerfile b/testdata/static/Dockerfile new file mode 100644 index 0000000..61a2e42 --- /dev/null +++ b/testdata/static/Dockerfile @@ -0,0 +1,9 @@ +FROM scratch + +# nothing; just need an image with a fixed digest +COPY Dockerfile / + +# hello! +# +# this comment was added after pushing with the above content so that there's a +# new digest for the image, so that we can test for old versions