Skip to content

Commit

Permalink
test: LbaasV2 method getSubnetID
Browse files Browse the repository at this point in the history
  • Loading branch information
majorchork committed Oct 9, 2023
1 parent 135260b commit 110691b
Showing 1 changed file with 148 additions and 0 deletions.
148 changes: 148 additions & 0 deletions pkg/openstack/loadbalancer_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package openstack

import (
"fmt"
"reflect"
"sort"
"testing"
Expand All @@ -10,6 +11,7 @@ import (
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules"
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type testPopListener struct {
Expand Down Expand Up @@ -728,3 +730,149 @@ func Test_buildPoolCreateOpt(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 error
}{
{
name: "test get subnet from service annotation",
args: args{
svcConf: &serviceConfig{
keepClientIP: true,
tlsContainerRef: "tls-container-ref",
enableProxyProtocol: true,
},
lbaasV2: &LbaasV2{
LoadBalancer{
opts: LoadBalancerOpts{
LBClasses: map[string]*LBClass{
"test-class": {
SubnetID: "test-id",
FloatingNetworkID: "test-floating-network-id",
FloatingSubnetID: "test-floating-subnet-id",
FloatingSubnet: "test-floating-subnet",
NetworkID: "test-network-id",
FloatingSubnetTags: "test-floating-subnet-tags",
MemberSubnetID: "test-member-subnet-id",
},
},
},
},
},
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"loadbalancer.openstack.org/subnet-id": "test-id",
"loadbalancer.openstack.org/class": "test-class",
},
},
},
},
want: "test-id",
},
{
name: "test get subnet from config class",
args: args{
svcConf: &serviceConfig{
keepClientIP: true,
tlsContainerRef: "tls-container-ref",
enableProxyProtocol: true,
},
lbaasV2: &LbaasV2{
LoadBalancer{
opts: LoadBalancerOpts{
LBClasses: map[string]*LBClass{
"test-class": {
SubnetID: "test-class-subnet-id",
FloatingNetworkID: "test-floating-network-id",
FloatingSubnetID: "test-floating-subnet-id",
FloatingSubnet: "test-floating-subnet",
NetworkID: "test-network-id",
FloatingSubnetTags: "test-floating-subnet-tags",
MemberSubnetID: "test-member-subnet-id",
},
},
},
},
},
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"loadbalancer.openstack.org/class": "test-class",
},
},
},
},
want: "test-class-subnet-id",
expectedErr: nil,
},
{
name: "test get subnet from config class",
args: args{
svcConf: &serviceConfig{
keepClientIP: true,
tlsContainerRef: "tls-container-ref",
enableProxyProtocol: true,
},
lbaasV2: &LbaasV2{
LoadBalancer{
opts: LoadBalancerOpts{
SubnetID: "test-subnet-id",
},
},
},
service: &corev1.Service{},
},
want: "test-subnet-id",
expectedErr: nil,
},
{
name: "test get subnet from config class with invalid loadbalancer class",
args: args{
svcConf: &serviceConfig{
keepClientIP: true,
tlsContainerRef: "tls-container-ref",
enableProxyProtocol: true,
},
lbaasV2: &LbaasV2{
LoadBalancer{
opts: LoadBalancerOpts{
SubnetID: "test-subnet-id",
},
},
},
service: &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"loadbalancer.openstack.org/class": "test-class",
},
},
},
},
want: "",
expectedErr: fmt.Errorf("invalid loadbalancer class %q", "test-class"),
},
}

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 != nil {
assert.EqualError(t, err, tt.expectedErr.Error())
}

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getSubnetID() = %v, expected %v", got, tt.want)
}
})
}
}

0 comments on commit 110691b

Please sign in to comment.