diff --git a/pkg/core/resources/apis/mesh/gateway_route_validator_test.go b/pkg/core/resources/apis/mesh/gateway_route_validator_test.go index 7249f0747267..733011d3c604 100644 --- a/pkg/core/resources/apis/mesh/gateway_route_validator_test.go +++ b/pkg/core/resources/apis/mesh/gateway_route_validator_test.go @@ -4,25 +4,13 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/kumahq/kuma/pkg/core/resources/apis/mesh" - "github.com/kumahq/kuma/pkg/core/resources/model" "github.com/kumahq/kuma/pkg/core/validators" _ "github.com/kumahq/kuma/pkg/plugins/runtime/gateway/register" + . "github.com/kumahq/kuma/pkg/test/resources" ) -// MeshGatewayRouteGenerator is a ResourceGenerator that creates MeshGatewayResource objects. -type MeshGatewayRouteGenerator func() *MeshGatewayRouteResource - -func (g MeshGatewayRouteGenerator) New() model.Resource { - if g != nil { - return g() - } - - return nil -} - var _ = Describe("MeshGatewayRoute", func() { - DescribeValidCases(MeshGatewayRouteGenerator(NewMeshGatewayRouteResource), - + DescribeValidCases(NewMeshGatewayRouteResource, Entry("HTTP route", ` type: MeshGatewayRoute name: route @@ -115,7 +103,7 @@ conf: `), ) - DescribeErrorCases(MeshGatewayRouteGenerator(NewMeshGatewayRouteResource), + DescribeErrorCases(NewMeshGatewayRouteResource, ErrorCase("missing conf", validators.Violation{ Field: "conf", Message: "cannot be empty", diff --git a/pkg/core/resources/apis/mesh/gateway_validator_test.go b/pkg/core/resources/apis/mesh/gateway_validator_test.go index 6e5c7358a839..6c7e54255d2c 100644 --- a/pkg/core/resources/apis/mesh/gateway_validator_test.go +++ b/pkg/core/resources/apis/mesh/gateway_validator_test.go @@ -7,22 +7,12 @@ import ( "github.com/kumahq/kuma/pkg/core/resources/model" "github.com/kumahq/kuma/pkg/core/validators" _ "github.com/kumahq/kuma/pkg/plugins/runtime/gateway/register" + . "github.com/kumahq/kuma/pkg/test/resources" ) -// GatewayGenerateor is a ResourceGenerator that creates GatewayResource objects. -type GatewayGenerator func() *MeshGatewayResource - -func (g GatewayGenerator) New() model.Resource { - if g != nil { - return g() - } - - return nil -} - var _ = Describe("Gateway", func() { DescribeValidCases( - GatewayGenerator(NewMeshGatewayResource), + NewMeshGatewayResource, Entry("HTTPS listener", ` type: MeshGateway name: gateway @@ -112,7 +102,7 @@ conf: ) DescribeErrorCases( - GatewayGenerator(NewMeshGatewayResource), + NewMeshGatewayResource, ErrorCase("doesn't have any selectors", validators.Violation{ Field: `selectors`, diff --git a/pkg/core/resources/apis/mesh/mesh_suite_test.go b/pkg/core/resources/apis/mesh/mesh_suite_test.go index f9b59de4e47f..92002ce38a91 100644 --- a/pkg/core/resources/apis/mesh/mesh_suite_test.go +++ b/pkg/core/resources/apis/mesh/mesh_suite_test.go @@ -6,95 +6,9 @@ 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" "github.com/kumahq/kuma/pkg/test" ) func TestMesh(t *testing.T) { test.RunSpecs(t, "Mesh Suite") } - -// 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(generator ResourceGenerator, cases ...TableEntry) { - DescribeTable( - "should pass validation", - func(given string) { - // setup - resource := generator.New() - - // 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(generator ResourceGenerator, cases ...TableEntry) { - DescribeTable( - "should validate all fields and return as many individual errors as possible", - func(given ResourceValidationCase) { - // setup - resource := generator.New() - - // 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, - }, - ) -} diff --git a/pkg/test/resources/validation.go b/pkg/test/resources/validation.go new file mode 100644 index 000000000000..76a07126e298 --- /dev/null +++ b/pkg/test/resources/validation.go @@ -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, + }, + ) +}