Skip to content

Commit

Permalink
Fix the coverage of the certification package (#629)
Browse files Browse the repository at this point in the history
* Add a test suite to make adding tests in the future easier, and
  to have it reported in the test coverage reports.
* Add tests for the GenericCheck
* Coverage: 100%

Fixes #613

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

import (
"testing"

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

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

import (
"context"
"errors"

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

var _ = Describe("Generic check tests", func() {
var validatorFn ValidatorFunc = func(ctx context.Context, imageRef ImageReference) (bool, error) {
switch imageRef.ImageFSPath {
case "error":
return false, errors.New("invalid validator")
case "failed":
return false, nil
default:
return true, nil
}
}
var metadata Metadata = Metadata{
Description: "test metadata",
}
var helpText HelpText = HelpText{
Message: "test message",
}
When("A generic check is created", func() {
var testCheck Check
var imgRef ImageReference
BeforeEach(func() {
testCheck = NewGenericCheck(
"testname",
validatorFn,
metadata,
helpText,
)
imgRef = ImageReference{}
})
It("should return the correct name", func() {
Expect(testCheck.Name()).To(Equal("testname"))
})
It("should return the correct metadata", func() {
Expect(testCheck.Metadata().Description).To(Equal("test metadata"))
})
It("should return the correct helpText", func() {
Expect(testCheck.Help().Message).To(Equal("test message"))
})
It("should execute the validator successfully", func() {
result, err := testCheck.Validate(context.TODO(), imgRef)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(BeTrue())
})
Context("but an error occurs", func() {
It("should return an error and false for result", func() {
imgRef.ImageFSPath = "error"
result, err := testCheck.Validate(context.TODO(), imgRef)
Expect(err).To(HaveOccurred())
Expect(result).To(BeFalse())
})
})
Context("but an failure occurs", func() {
It("should not return an error and false for result", func() {
imgRef.ImageFSPath = "failed"
result, err := testCheck.Validate(context.TODO(), imgRef)
Expect(err).ToNot(HaveOccurred())
Expect(result).To(BeFalse())
})
})
})
})

0 comments on commit dce7f6d

Please sign in to comment.