Explorar o código

add tests for check

Alex Suraci %!s(int64=7) %!d(string=hai) anos
pai
achega
bd27f9ced0
Modificáronse 3 ficheiros con 132 adicións e 1 borrados
  1. 117 0
      check_test.go
  2. 6 1
      testdata/setup-images
  3. 9 0
      testdata/static/Dockerfile

+ 117 - 0
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"},
+			}))
+		})
+	})
+})

+ 6 - 1
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"

+ 9 - 0
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