Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test get subnet #2411

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
majorchork marked this conversation as resolved.
Show resolved Hide resolved
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)
})
}
}