Skip to content

Commit

Permalink
Increase coverage of the runtime assets package (#628)
Browse files Browse the repository at this point in the history
* Increases coverage to 88.2%.
* Introduce a fake registry to test crane against.

Fixes #619

Signed-off-by: Brad P. Crochet <[email protected]>
  • Loading branch information
bcrochet authored May 12, 2022
1 parent dce7f6d commit e96c75f
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
75 changes: 75 additions & 0 deletions certification/runtime/assets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package runtime

import (
"context"
"fmt"
"net/http/httptest"
"net/url"

"github.com/google/go-containerregistry/pkg/crane"
"github.com/google/go-containerregistry/pkg/registry"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/random"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/spf13/viper"
)

var _ = Describe("Runtime assets tests", func() {
var src, srcHost string
var digest v1.Hash
BeforeEach(func() {
// Set up a fake registry.
s := httptest.NewServer(registry.New())
DeferCleanup(func() {
s.Close()
})
u, err := url.Parse(s.URL)
Expect(err).ToNot(HaveOccurred())
src = fmt.Sprintf("%s/test/preflight", u.Host)
srcHost = u.Hostname()

img, err := random.Image(1024, 5)
Expect(err).ToNot(HaveOccurred())

digest, err = img.Digest()
Expect(err).ToNot(HaveOccurred())

err = crane.Push(img, src)
Expect(err).ToNot(HaveOccurred())

images["assettest"] = fmt.Sprintf("%s:latest", src)
})
Context("when asking for assets", func() {
Context("the registry works", func() {
It("should return a digest list", func() {
data := Assets(context.TODO())
Expect(data.Images).To(ContainElement(fmt.Sprintf("%s@%s", srcHost, digest.String())))
})
})
Context("the registry throws an error", func() {
It("should not return the image", func() {
data := Assets(context.TODO())
Expect(data.Images).ToNot(ContainElement("quay.io/operator-framework/scorecard-test@sha256:deadb33f"))
})
})
})
})

var _ = Describe("Scorecard Image tests", func() {
Context("when getting the Scorecard image", func() {
Context("the default is used", func() {
It("should return the default", func() {
image := ScorecardImage()
Expect(image).To(Equal(images["scorecard"]))
})
})
Context("the image is overriden", func() {
It("should return the passed param", func() {
viper.Set("scorecard_image", "quay.io/some/container:v1.0.0")
image := ScorecardImage()
Expect(image).To(Equal("quay.io/some/container:v1.0.0"))
})
})
})
})
13 changes: 13 additions & 0 deletions certification/runtime/runtime_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package runtime

import (
"testing"

. "github.com/onsi/ginkgo/v2/dsl/core"
. "github.com/onsi/gomega"
)

func TestBundle(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Runtime Suite")
}
16 changes: 16 additions & 0 deletions certification/runtime/runtime_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package runtime

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

var _ = Describe("Runtime tests", func() {
Context("when the cluster version is unknown", func() {
It("will get unknown/unknown in the version struct", func() {
version := UnknownOpenshiftClusterVersion()
Expect(version.Name).To(Equal("unknown"))
Expect(version.Version).To(Equal("unknown"))
})
})
})

0 comments on commit e96c75f

Please sign in to comment.