diff --git a/pkg/openstack/loadbalancer_test.go b/pkg/openstack/loadbalancer_test.go index 93dc1aabb1..dc2445527a 100644 --- a/pkg/openstack/loadbalancer_test.go +++ b/pkg/openstack/loadbalancer_test.go @@ -7,7 +7,9 @@ import ( "sort" "testing" + "github.com/gophercloud/gophercloud" "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners" + v2monitors "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors" "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools" "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules" "github.com/stretchr/testify/assert" @@ -2139,3 +2141,150 @@ func TestLbaasV2_getNetworkID(t *testing.T) { }) } } + +func Test_buildMonitorCreateOpts(t *testing.T) { + type testArg struct { + lbaas *LbaasV2 + svcConf *serviceConfig + port corev1.ServicePort + } + tests := []struct { + name string + testArg testArg + want v2monitors.CreateOpts + }{ + { + name: "test for port protocol udp with ovn provider", + testArg: testArg{ + lbaas: &LbaasV2{ + LoadBalancer{ + opts: LoadBalancerOpts{ + LBProvider: "ovn", + }, + lb: &gophercloud.ServiceClient{}, + }, + }, + svcConf: &serviceConfig{ + healthMonitorDelay: 6, + healthMonitorTimeout: 5, + healthMonitorMaxRetries: 4, + healthMonitorMaxRetriesDown: 3, + healthCheckNodePort: 32100, + }, + port: corev1.ServicePort{ + Protocol: corev1.ProtocolUDP, + }, + }, + want: v2monitors.CreateOpts{ + Name: "test for port protocol udp with ovn provider", + Type: "UDP-CONNECT", + Delay: 6, + Timeout: 5, + MaxRetries: 4, + MaxRetriesDown: 3, + }, + }, + { + name: "using tcp with ovn provider", + testArg: testArg{ + lbaas: &LbaasV2{ + LoadBalancer{ + opts: LoadBalancerOpts{ + LBProvider: "ovn", + }, + }, + }, + svcConf: &serviceConfig{ + healthMonitorDelay: 3, + healthMonitorTimeout: 8, + healthMonitorMaxRetries: 6, + healthMonitorMaxRetriesDown: 2, + healthCheckNodePort: 31200, + }, + port: corev1.ServicePort{ + Protocol: corev1.ProtocolTCP, + }, + }, + want: v2monitors.CreateOpts{ + Name: "using tcp with ovn provider", + Type: "TCP", + Delay: 3, + Timeout: 8, + MaxRetries: 6, + MaxRetriesDown: 2, + }, + }, + { + name: "using node port zero", + testArg: testArg{ + lbaas: &LbaasV2{ + LoadBalancer{ + opts: LoadBalancerOpts{ + LBProvider: "ovn", + }, + }, + }, + svcConf: &serviceConfig{ + healthMonitorDelay: 3, + healthMonitorTimeout: 5, + healthMonitorMaxRetries: 1, + healthMonitorMaxRetriesDown: 2, + healthCheckNodePort: 0, + }, + port: corev1.ServicePort{ + Protocol: corev1.ProtocolTCP, + }, + }, + want: v2monitors.CreateOpts{ + Name: "using node port zero", + Type: "TCP", + Delay: 3, + Timeout: 5, + MaxRetries: 1, + MaxRetriesDown: 2, + }, + }, + { + name: "using tcp protocol with not-ovn provider", + testArg: testArg{ + lbaas: &LbaasV2{ + LoadBalancer{ + opts: LoadBalancerOpts{ + LBProvider: "amphora", + }, + lb: &gophercloud.ServiceClient{}, + }, + }, + svcConf: &serviceConfig{ + healthMonitorDelay: 3, + healthMonitorTimeout: 4, + healthMonitorMaxRetries: 1, + healthMonitorMaxRetriesDown: 5, + healthCheckNodePort: 310000, + }, + port: corev1.ServicePort{ + Protocol: corev1.ProtocolTCP, + }, + }, + want: v2monitors.CreateOpts{ + Name: "using tcp protocol with not-ovn provider", + Type: "HTTP", + Delay: 3, + Timeout: 4, + MaxRetries: 1, + MaxRetriesDown: 5, + + URLPath: "/healthz", + HTTPMethod: "GET", + ExpectedCodes: "200", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := tt.testArg.lbaas.buildMonitorCreateOpts(tt.testArg.svcConf, tt.testArg.port, tt.name) + assert.Equal(t, tt.want, result) + }) + } +}