-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the coverage of the certification package (#629)
* 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
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}) | ||
}) | ||
}) | ||
}) |