From bbbe88d53ff1948b931df5c9a0ba24c26b6a9652 Mon Sep 17 00:00:00 2001 From: Grzegorz Piotrowski Date: Wed, 16 Oct 2024 14:57:30 +0100 Subject: [PATCH] Add test cases for health check interval value * pkg/router/router_test.go: (TestConfigTemplate): Add test cases for route health check interval annotation. Verify that the correct value is added to the backend server line and that the values exceeding the maximum haproxy time value get clipped to the max limit. Verify that invalid annotation values result in the default health check interval value applied. Test clipping of the health check interval for a passthrough route. (MustCreateEndpointSlice): Add addresses field ((MustCreateEndpointSlice).Apply): Initialize the endpoint's Addresses field to the addresses array from the mustCreateEndpointSlice parameters if specified. This enables having at least two endpoints for the route in the test case and satisfy the conditions needed to configure the check inter in the backend server line. (passthroughBackendName): New helper function to construct a config's backend name for a passthrough route. --- pkg/router/router_test.go | 122 +++++++++++++++++++++++++++++++++++++- 1 file changed, 121 insertions(+), 1 deletion(-) diff --git a/pkg/router/router_test.go b/pkg/router/router_test.go index 889a1289e..5821d75eb 100644 --- a/pkg/router/router_test.go +++ b/pkg/router/router_test.go @@ -802,6 +802,115 @@ func TestConfigTemplate(t *testing.T) { }, }, }, + "valid route health check interval annotation": { + mustCreateWithConfig{ + mustCreateEndpointSlices: []mustCreateEndpointSlice{ + { + name: "servicer1", + serviceName: "servicer1", + addresses: []string{"1.1.1.1", "1.1.1.2"}, + }, + }, + mustCreateRoute: mustCreateRoute{ + name: "r1", + host: "r1example.com", + targetServiceName: "servicer1", + weight: 1, + time: start, + annotations: map[string]string{ + "router.openshift.io/haproxy.health.check.interval": "10s", + }, + }, + mustMatchConfig: mustMatchConfig{ + section: "backend", + sectionName: insecureBackendName(h.namespace, "r1"), + attribute: "server", + value: "inter 10s", + }, + }, + }, + "route health check interval annotation exceeds the haproxy maximum": { + mustCreateWithConfig{ + mustCreateEndpointSlices: []mustCreateEndpointSlice{ + { + name: "servicer2", + serviceName: "servicer2", + addresses: []string{"1.1.1.1", "1.1.1.2"}, + }, + }, + mustCreateRoute: mustCreateRoute{ + name: "r2", + host: "r2example.com", + targetServiceName: "servicer2", + weight: 1, + time: start, + annotations: map[string]string{ + "router.openshift.io/haproxy.health.check.interval": "5000d", + }, + }, + mustMatchConfig: mustMatchConfig{ + section: "backend", + sectionName: insecureBackendName(h.namespace, "r2"), + attribute: "server", + value: "inter 2147483647ms", + }, + }, + }, + "invalid route health check interval annotation": { + mustCreateWithConfig{ + mustCreateEndpointSlices: []mustCreateEndpointSlice{ + { + name: "servicer3", + serviceName: "servicer3", + addresses: []string{"1.1.1.1", "1.1.1.2"}, + }, + }, + mustCreateRoute: mustCreateRoute{ + name: "r3", + host: "r3example.com", + targetServiceName: "servicer3", + weight: 1, + time: start, + annotations: map[string]string{ + "router.openshift.io/haproxy.health.check.interval": "abc", + }, + }, + mustMatchConfig: mustMatchConfig{ + section: "backend", + sectionName: insecureBackendName(h.namespace, "r3"), + attribute: "server", + value: "inter 5000ms", + }, + }, + }, + "passthrough route health check interval annotation exceeds the haproxy maximum": { + mustCreateWithConfig{ + mustCreateEndpointSlices: []mustCreateEndpointSlice{ + { + name: "servicer4", + serviceName: "servicer4", + addresses: []string{"1.1.1.1", "1.1.1.2"}, + }, + }, + mustCreateRoute: mustCreateRoute{ + name: "r4", + host: "r4example.com", + targetServiceName: "servicer4", + weight: 1, + time: start, + annotations: map[string]string{ + "router.openshift.io/haproxy.health.check.interval": "9000d", + }, + tlsTermination: routev1.TLSTerminationPassthrough, + }, + mustMatchConfig: mustMatchConfig{ + section: "backend", + sectionName: passthroughBackendName(h.namespace, "r4"), + attribute: "server", + value: "inter 2147483647ms", + }, + }, + }, } defer cleanUpRoutes(t) @@ -963,6 +1072,8 @@ type mustCreateEndpointSlice struct { serviceName string // appProtocol is the appProtocol of the endpointslice. appProtocol string + // addresses is the addresses field of the endpoint + addresses []string } func (e mustCreateEndpointSlice) Apply(h *harness) error { @@ -973,6 +1084,10 @@ func (e mustCreateEndpointSlice) Apply(h *harness) error { if e.appProtocol != "" { appProtocol = &e.appProtocol } + addresses := []string{"1.1.1.1"} + if len(e.addresses) > 0 { + addresses = e.addresses + } ep := &discoveryv1.EndpointSlice{ ObjectMeta: metav1.ObjectMeta{ Namespace: h.namespace, @@ -983,7 +1098,7 @@ func (e mustCreateEndpointSlice) Apply(h *harness) error { UID: h.nextUID(), }, Endpoints: []discoveryv1.Endpoint{{ - Addresses: []string{"1.1.1.1"}, + Addresses: addresses, }}, Ports: []discoveryv1.EndpointPort{{ AppProtocol: appProtocol, @@ -1246,3 +1361,8 @@ func insecureBackendName(ns, route string) string { func reencryptBackendName(ns, route string) string { return "be_secure:" + ns + ":" + route } + +// passthroughBackendName contructs the HAProxy config's backend name for a passthrough route. +func passthroughBackendName(ns, route string) string { + return "be_tcp:" + ns + ":" + route +}