Skip to content

Commit

Permalink
fix cel validation tests
Browse files Browse the repository at this point in the history
Signed-off-by: lemonlinger <lemonlinger@gmail.com>
  • Loading branch information
lemonlinger committed Dec 25, 2023
1 parent 8d21fb8 commit 18511d2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 35 deletions.
5 changes: 2 additions & 3 deletions api/v1alpha1/healthcheck_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"`
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion site/content/en/latest/api/extension_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 45 additions & 29 deletions test/cel-validation/backendtrafficpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
},
},
{
Expand All @@ -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},
},
},
},
Expand All @@ -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{
Expand All @@ -473,21 +499,21 @@ 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'},
},
},
},
},
}
},
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{
Expand All @@ -502,23 +528,17 @@ 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"),
},
},
},
},
}
},
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.]`,
},
},
{
Expand All @@ -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"),
},
},
},
Expand Down Expand Up @@ -574,19 +592,17 @@ 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'},
},
},
},
},
}
},
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.]`,
},
},
}
Expand Down

0 comments on commit 18511d2

Please sign in to comment.