Skip to content

Commit

Permalink
Add test cases for health check interval value
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
grzpiotrowski committed Nov 26, 2024
1 parent 76e36c7 commit bbbe88d
Showing 1 changed file with 121 additions and 1 deletion.
122 changes: 121 additions & 1 deletion pkg/router/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
}

0 comments on commit bbbe88d

Please sign in to comment.