Skip to content

Commit

Permalink
Test get subnet (#2411)
Browse files Browse the repository at this point in the history
* test: LbaasV2 method buildPoolCreateOpt

* test: LbaasV2 method getSubnetID
  • Loading branch information
majorchork committed Oct 19, 2023
1 parent 1972e4c commit 92bde6c
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions pkg/openstack/loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openstack

import (
"context"
"fmt"
"reflect"
"sort"
"testing"
Expand Down Expand Up @@ -1754,3 +1755,129 @@ func TestBuildBatchUpdateMemberOpts(t *testing.T) {
})
}
}

func Test_getSubnetID(t *testing.T) {
type args struct {
svcConf *serviceConfig
service *corev1.Service
lbaasV2 *LbaasV2
}
tests := []struct {
name string
args args
want string
expectedErr string
}{
{
name: "test get subnet from service annotation",
args: args{
svcConf: &serviceConfig{},
lbaasV2: &LbaasV2{
LoadBalancer{
opts: LoadBalancerOpts{
LBClasses: map[string]*LBClass{
"test-class": {
SubnetID: "test-class-subnet-id",
},
},
},
},
},
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{
"loadbalancer.openstack.org/subnet-id": "annotation-test-id",
"loadbalancer.openstack.org/class": "test-class",
},
},
},
},
want: "annotation-test-id",
},
{
name: "test get subnet from config class",
args: args{
svcConf: &serviceConfig{},
lbaasV2: &LbaasV2{
LoadBalancer{
opts: LoadBalancerOpts{
LBClasses: map[string]*LBClass{
"test-class": {
SubnetID: "test-class-subnet-id",
},
},
},
},
},
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{
"loadbalancer.openstack.org/class": "test-class",
},
},
},
},
want: "test-class-subnet-id",
},
{
name: "test get subnet from config class with invalid loadbalancer class",
args: args{
svcConf: &serviceConfig{},
lbaasV2: &LbaasV2{
LoadBalancer{
opts: LoadBalancerOpts{
LBClasses: map[string]*LBClass{
"decoy-class": {
SubnetID: "test-id",
},
},
SubnetID: "test-subnet-id",
},
},
},
service: &corev1.Service{
ObjectMeta: v1.ObjectMeta{
Annotations: map[string]string{
"loadbalancer.openstack.org/class": "test-class",
},
},
},
},
want: "",
expectedErr: fmt.Sprintf("invalid loadbalancer class %q", "test-class"),
},
{
name: "test get subnet from default config",
args: args{
svcConf: &serviceConfig{},
lbaasV2: &LbaasV2{
LoadBalancer{
opts: LoadBalancerOpts{
LBClasses: map[string]*LBClass{
"test-config-class-subnet-id": {
SubnetID: "test-id",
},
},
SubnetID: "test-default-subnet-id",
},
},
},
service: &corev1.Service{},
},
want: "test-default-subnet-id",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.args.lbaasV2.getSubnetID(tt.args.service, tt.args.svcConf)
if tt.expectedErr != "" {
assert.EqualError(t, err, tt.expectedErr)
}
if tt.expectedErr == "" {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 92bde6c

Please sign in to comment.