-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mike Beaumont <mjboamail@gmail.com>
- Loading branch information
1 parent
570d9f7
commit f92acc8
Showing
4 changed files
with
99 additions
and
114 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
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
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
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,93 @@ | ||
package resources | ||
|
||
import ( | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
|
||
core_model "github.com/kumahq/kuma/pkg/core/resources/model" | ||
"github.com/kumahq/kuma/pkg/core/validators" | ||
) | ||
|
||
// ResourceGenerator creates a resource of a pre-defined type. | ||
type ResourceGenerator interface { | ||
New() core_model.Resource | ||
} | ||
|
||
// ResourceValidationCase captures a resource YAML and any corresponding validation error. | ||
type ResourceValidationCase struct { | ||
Resource string | ||
Violations []validators.Violation | ||
} | ||
|
||
// DescribeValidCases creates a Ginkgo table test for the given entries, | ||
// where each entry is a valid YAML resource. It ensures that each entry | ||
// can be successfully validated. | ||
func DescribeValidCases[T core_model.Resource](generator func() T, cases ...TableEntry) { | ||
DescribeTable( | ||
"should pass validation", | ||
func(given string) { | ||
// setup | ||
resource := generator() | ||
|
||
// when | ||
err := core_model.FromYAML([]byte(given), resource.GetSpec()) | ||
|
||
// then | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// when | ||
verr := core_model.Validate(resource) | ||
|
||
// then | ||
Expect(verr).ToNot(HaveOccurred()) | ||
}, | ||
cases) | ||
} | ||
|
||
// DescribeErrorCases creates a Ginkgo table test for the given entries, where each entry | ||
// is a ResourceValidationCase that contains an invalid resource YAML and the corresponding | ||
// validation error. | ||
func DescribeErrorCases[T core_model.Resource](generator func() T, cases ...TableEntry) { | ||
DescribeTable( | ||
"should validate all fields and return as many individual errors as possible", | ||
func(given ResourceValidationCase) { | ||
// setup | ||
resource := generator() | ||
|
||
// when | ||
Expect( | ||
core_model.FromYAML([]byte(given.Resource), resource.GetSpec()), | ||
).ToNot(HaveOccurred()) | ||
|
||
expected := validators.ValidationError{ | ||
Violations: given.Violations, | ||
} | ||
|
||
// then | ||
err := core_model.Validate(resource).(*validators.ValidationError) | ||
Expect(err.Violations).To(ConsistOf(expected.Violations)) | ||
}, | ||
cases, | ||
) | ||
} | ||
|
||
// ErrorCase is a helper that generates a table entry for DescribeErrorCases. | ||
func ErrorCase(description string, err validators.Violation, yaml string) TableEntry { | ||
return Entry( | ||
description, | ||
ResourceValidationCase{ | ||
Violations: []validators.Violation{err}, | ||
Resource: yaml, | ||
}, | ||
) | ||
} | ||
|
||
func ErrorCases(description string, errs []validators.Violation, yaml string) TableEntry { | ||
return Entry( | ||
description, | ||
ResourceValidationCase{ | ||
Violations: errs, | ||
Resource: yaml, | ||
}, | ||
) | ||
} |