From 18511d2b669914cb0af95accc62693086f530454 Mon Sep 17 00:00:00 2001 From: lemonlinger Date: Mon, 25 Dec 2023 15:35:36 +0800 Subject: [PATCH] fix cel validation tests Signed-off-by: lemonlinger --- api/v1alpha1/healthcheck_types.go | 5 +- ....envoyproxy.io_backendtrafficpolicies.yaml | 3 +- site/content/en/latest/api/extension_types.md | 2 +- .../backendtrafficpolicy_test.go | 74 +++++++++++-------- 4 files changed, 49 insertions(+), 35 deletions(-) diff --git a/api/v1alpha1/healthcheck_types.go b/api/v1alpha1/healthcheck_types.go index 7b4c08cf227..2116fd1ff14 100644 --- a/api/v1alpha1/healthcheck_types.go +++ b/api/v1alpha1/healthcheck_types.go @@ -93,11 +93,10 @@ type HTTPHealthChecker struct { // ExpectedStatuses defines a list of HTTP response statuses considered healthy. // Defaults to 200 only // +optional - // +kubebuilder:validation:UniqueItems=true ExpectedStatuses []HTTPStatus `json:"expectedStatuses,omitempty" yaml:"expectedStatuses,omitempty"` // ExpectedResponse defines a list of HTTP expected responses to match. // +optional - ExpectedResponse *HealthCheckPayload `json:"expectedResponse,omitempty" yaml:"expectedResponses,omitempty"` + ExpectedResponse *HealthCheckPayload `json:"expectedResponse,omitempty" yaml:"expectedResponse,omitempty"` } // GRPCHealthChecker defines the settings of grpc health check. @@ -117,7 +116,7 @@ type TCPHealthChecker struct { // Send defines the request payload. // +optional Send *HealthCheckPayload `json:"send,omitempty" yaml:"send,omitempty"` - // Receive defines the expected response payloads. + // Receive defines the expected response payload. // +optional Receive *HealthCheckPayload `json:"receive,omitempty" yaml:"receive,omitempty"` } diff --git a/charts/gateway-helm/crds/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml b/charts/gateway-helm/crds/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml index f0bf41e26dc..c4d8b53cf87 100644 --- a/charts/gateway-helm/crds/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml +++ b/charts/gateway-helm/crds/generated/gateway.envoyproxy.io_backendtrafficpolicies.yaml @@ -117,7 +117,6 @@ spec: minimum: 100 type: integer type: array - uniqueItems: true method: description: Method defines the HTTP method used for health checking. Defaults to GET @@ -136,7 +135,7 @@ spec: It's required while the health checker type is TCP. properties: receive: - description: Receive defines the expected response payloads. + description: Receive defines the expected response payload. properties: binary: description: Binary payload base64 encoded. diff --git a/site/content/en/latest/api/extension_types.md b/site/content/en/latest/api/extension_types.md index 03797ef700b..6b1f2b892c5 100644 --- a/site/content/en/latest/api/extension_types.md +++ b/site/content/en/latest/api/extension_types.md @@ -1872,7 +1872,7 @@ _Appears in:_ | Field | Description | | --- | --- | | `send` _[HealthCheckPayload](#healthcheckpayload)_ | Send defines the request payload. | -| `receive` _[HealthCheckPayload](#healthcheckpayload)_ | Receive defines the expected response payloads. | +| `receive` _[HealthCheckPayload](#healthcheckpayload)_ | Receive defines the expected response payload. | #### TCPKeepalive diff --git a/test/cel-validation/backendtrafficpolicy_test.go b/test/cel-validation/backendtrafficpolicy_test.go index f54da612f4c..8af9633f78b 100644 --- a/test/cel-validation/backendtrafficpolicy_test.go +++ b/test/cel-validation/backendtrafficpolicy_test.go @@ -423,14 +423,14 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { Type: egv1a1.HealthCheckerTypeHTTP, HTTP: &egv1a1.HTTPHealthChecker{ Path: "/healthz", - ExpectedStatuses: []egv1a1.HTTPStatusRange{{Start: 400, End: 200}}, + ExpectedStatuses: []egv1a1.HTTPStatus{99, 200}, }, }, }, } }, wantErrors: []string{ - `spec.healthCheck.healthChecker.http.expectedStatuses[0]: Invalid value: "object": start should be not greater than end`, + `spec.healthCheck.healthChecker.http.expectedStatuses[0]: Invalid value: 99: spec.healthCheck.healthChecker.http.expectedStatuses[0] in body should be greater than or equal to 100`, }, }, { @@ -449,7 +449,7 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { Type: egv1a1.HealthCheckerTypeHTTP, HTTP: &egv1a1.HTTPHealthChecker{ Path: "/healthz", - ExpectedStatuses: []egv1a1.HTTPStatusRange{{Start: 100, End: 600}}, + ExpectedStatuses: []egv1a1.HTTPStatus{100, 200, 201}, }, }, }, @@ -459,6 +459,32 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { }, { desc: "invalid http expected statuses - out of range", + mutate: func(btp *egv1a1.BackendTrafficPolicy) { + btp.Spec = egv1a1.BackendTrafficPolicySpec{ + TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ + PolicyTargetReference: gwapiv1a2.PolicyTargetReference{ + Group: gwapiv1a2.Group("gateway.networking.k8s.io"), + Kind: gwapiv1a2.Kind("Gateway"), + Name: gwapiv1a2.ObjectName("eg"), + }, + }, + HealthCheck: &egv1a1.HealthCheck{ + HealthChecker: egv1a1.HealthChecker{ + Type: egv1a1.HealthCheckerTypeHTTP, + HTTP: &egv1a1.HTTPHealthChecker{ + Path: "/healthz", + ExpectedStatuses: []egv1a1.HTTPStatus{200, 300, 601}, + }, + }, + }, + } + }, + wantErrors: []string{ + `spec.healthCheck.healthChecker.http.expectedStatuses[2]: Invalid value: 601: spec.healthCheck.healthChecker.http.expectedStatuses[2] in body should be less than 600`, + }, + }, + { + desc: "http expected responses - invalid text payload", mutate: func(btp *egv1a1.BackendTrafficPolicy) { btp.Spec = egv1a1.BackendTrafficPolicySpec{ TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ @@ -473,9 +499,9 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { Type: egv1a1.HealthCheckerTypeHTTP, HTTP: &egv1a1.HTTPHealthChecker{ Path: "/healthz", - ExpectedStatuses: []egv1a1.HTTPStatusRange{ - {Start: 600, End: 600}, - {Start: 200, End: 700}, + ExpectedResponse: &egv1a1.HealthCheckPayload{ + Type: egv1a1.HealthCheckPayloadTypeText, + Binary: []byte{'f', 'o', 'o'}, }, }, }, @@ -483,11 +509,11 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { } }, wantErrors: []string{ - `spec.healthCheck.healthChecker.http.expectedStatuses[0].start: Invalid value: 600: spec.healthCheck.healthChecker.http.expectedStatuses[0].start in body should be less than 600, spec.healthCheck.healthChecker.http.expectedStatuses[1].end: Invalid value: 700: spec.healthCheck.healthChecker.http.expectedStatuses[1].end in body should be less than or equal to 600`, + `[spec.healthCheck.healthChecker.http.expectedResponse: Invalid value: "object": If payload type is Text, text field needs to be set., spec.healthCheck.healthChecker.http.expectedResponse: Invalid value: "object": If payload type is Binary, binary field needs to be set.]`, }, }, { - desc: "invalid http expected responses", + desc: "http expected responses - invalid binary payload", mutate: func(btp *egv1a1.BackendTrafficPolicy) { btp.Spec = egv1a1.BackendTrafficPolicySpec{ TargetRef: gwapiv1a2.PolicyTargetReferenceWithSectionName{ @@ -502,15 +528,9 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { Type: egv1a1.HealthCheckerTypeHTTP, HTTP: &egv1a1.HTTPHealthChecker{ Path: "/healthz", - ExpectedResponses: []egv1a1.HealthCheckPayload{ - { - Type: egv1a1.HealthCheckPayloadTypeText, - Binary: []byte{'f', 'o', 'o'}, - }, - { - Type: egv1a1.HealthCheckPayloadTypeBinary, - Text: ptr.To("foo"), - }, + ExpectedResponse: &egv1a1.HealthCheckPayload{ + Type: egv1a1.HealthCheckPayloadTypeBinary, + Text: ptr.To("foo"), }, }, }, @@ -518,7 +538,7 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { } }, wantErrors: []string{ - `spec.healthCheck.healthChecker.http.expectedResponses[0]: Invalid value: "object": If payload type is Text, text field needs to be set., spec.healthCheck.healthChecker.http.expectedResponses[0]: Invalid value: "object": If payload type is Binary, binary field needs to be set., spec.healthCheck.healthChecker.http.expectedResponses[1]: Invalid value: "object": If payload type is Text, text field needs to be set., spec.healthCheck.healthChecker.http.expectedResponses[1]: Invalid value: "object": If payload type is Binary, binary field needs to be set.`, + `[spec.healthCheck.healthChecker.http.expectedResponse: Invalid value: "object": If payload type is Text, text field needs to be set., spec.healthCheck.healthChecker.http.expectedResponse: Invalid value: "object": If payload type is Binary, binary field needs to be set.]`, }, }, { @@ -540,11 +560,9 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { Type: egv1a1.HealthCheckPayloadTypeText, Binary: []byte{'f', 'o', 'o'}, }, - Receive: []egv1a1.HealthCheckPayload{ - { - Type: egv1a1.HealthCheckPayloadTypeText, - Text: ptr.To("foo"), - }, + Receive: &egv1a1.HealthCheckPayload{ + Type: egv1a1.HealthCheckPayloadTypeText, + Text: ptr.To("foo"), }, }, }, @@ -574,11 +592,9 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { Type: egv1a1.HealthCheckPayloadTypeText, Text: ptr.To("foo"), }, - Receive: []egv1a1.HealthCheckPayload{ - { - Type: egv1a1.HealthCheckPayloadTypeText, - Binary: []byte{'f', 'o', 'o'}, - }, + Receive: &egv1a1.HealthCheckPayload{ + Type: egv1a1.HealthCheckPayloadTypeText, + Binary: []byte{'f', 'o', 'o'}, }, }, }, @@ -586,7 +602,7 @@ func TestBackendTrafficPolicyTarget(t *testing.T) { } }, wantErrors: []string{ - `spec.healthCheck.healthChecker.tcp.receive[0]: Invalid value: "object": If payload type is Text, text field needs to be set., spec.healthCheck.healthChecker.tcp.receive[0]: Invalid value: "object": If payload type is Binary, binary field needs to be set.`, + `[spec.healthCheck.healthChecker.tcp.receive: Invalid value: "object": If payload type is Text, text field needs to be set., spec.healthCheck.healthChecker.tcp.receive: Invalid value: "object": If payload type is Binary, binary field needs to be set.]`, }, }, }