Skip to content

Commit

Permalink
enh: add tests for checkListenerPorts (#2405)
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshi-1505 authored Oct 24, 2023
1 parent 3c97ceb commit 8da2313
Showing 1 changed file with 166 additions and 0 deletions.
166 changes: 166 additions & 0 deletions pkg/openstack/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,172 @@ func Test_getListenerProtocol(t *testing.T) {
}
}

func TestLbaasV2_checkListenerPorts(t *testing.T) {
type args struct {
service *corev1.Service
curListenerMapping map[listenerKey]*listeners.Listener
isLBOwner bool
lbName string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "error is not thrown if loadblanacer matches & if port is already in use by a lb",
args: args{
service: &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "service",
Protocol: "https",
Port: 9090,
},
},
},
},
curListenerMapping: map[listenerKey]*listeners.Listener{
{
Protocol: "https",
Port: 9090,
}: {
ID: "listenerid",
Tags: []string{"test-lb"},
},
},
isLBOwner: false,
lbName: "test-lb",
},
wantErr: false,
},
{
name: "error is thrown if loadbalancer doesn't matches & if port is already in use by a service",
args: args{
service: &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "service",
Protocol: "https",
Port: 9090,
},
},
},
},
curListenerMapping: map[listenerKey]*listeners.Listener{
{
Protocol: "https",
Port: 9090,
}: {
ID: "listenerid",
Tags: []string{"test-lb", "test-lb1"},
},
},
isLBOwner: false,
lbName: "test-lb2",
},
wantErr: true,
},
{
name: "error is not thrown if lbOwner is present & no tags on service",
args: args{
service: &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "service",
Protocol: "https",
Port: 9090,
},
},
},
},
curListenerMapping: map[listenerKey]*listeners.Listener{
{
Protocol: "https",
Port: 9090,
}: {
ID: "listenerid",
},
},
isLBOwner: true,
lbName: "test-lb",
},
wantErr: false,
},
{
name: "error is not thrown if lbOwner is true & there are tags on service",
args: args{
service: &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "service",
Protocol: "http",
Port: 9091,
},
},
},
},
curListenerMapping: map[listenerKey]*listeners.Listener{
{
Protocol: "https",
Port: 9090,
}: {
ID: "listenerid",
Tags: []string{"test-lb"},
},
},
isLBOwner: true,
lbName: "test-lb",
},
wantErr: false,
},
{
name: "error is not thrown if listener key doesn't match port & protocol",
args: args{
service: &corev1.Service{
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "service",
Protocol: "http",
Port: 9091,
},
},
},
},
curListenerMapping: map[listenerKey]*listeners.Listener{
{
Protocol: "https",
Port: 9090,
}: {
ID: "listenerid",
Tags: []string{"test-lb"},
},
},
isLBOwner: false,
lbName: "test-lb",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
lbaas := &LbaasV2{
LoadBalancer: LoadBalancer{},
}
err := lbaas.checkListenerPorts(tt.args.service, tt.args.curListenerMapping, tt.args.isLBOwner, tt.args.lbName)
if tt.wantErr == true {
assert.ErrorContains(t, err, "already exists")
} else {
assert.NoError(t, err)
}
})
}
}
func TestLbaasV2_createLoadBalancerStatus(t *testing.T) {
type fields struct {
LoadBalancer LoadBalancer
Expand Down

0 comments on commit 8da2313

Please sign in to comment.