Skip to content

Commit

Permalink
Add unit tests for VPC LB integration
Browse files Browse the repository at this point in the history
Signed-off-by: Prajyot-Parab <[email protected]>
  • Loading branch information
Prajyot-Parab committed Sep 9, 2022
1 parent ee9cd5d commit fe289d3
Show file tree
Hide file tree
Showing 7 changed files with 1,056 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ func (s *ClusterScope) CreateLoadBalancer() (*vpcv1.LoadBalancer, error) {
}
options.Subnets = append(options.Subnets, subnet)
} else {
return nil, errors.Wrap(err, "Error subnet required for load balancer creation")
return nil, fmt.Errorf("error subnet required for load balancer creation")
}

options.SetPools([]vpcv1.LoadBalancerPoolPrototype{
Expand Down
205 changes: 203 additions & 2 deletions cloud/scope/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestCreateVPC(t *testing.T) {
t.Cleanup(teardown)
scope := setupClusterScope(clusterName, mockvpc)
scope.IBMVPCCluster.Spec = vpcCluster.Spec
mockvpc.EXPECT().ListVpcs(gomock.AssignableToTypeOf(listVpcsOptions)).Return(vpcCollection, detailedResponse, errors.New("Error when deleting subnet"))
mockvpc.EXPECT().ListVpcs(gomock.AssignableToTypeOf(listVpcsOptions)).Return(vpcCollection, detailedResponse, errors.New("Failed to list VPC"))
_, err := scope.CreateVPC()
g.Expect(err).To(Not(BeNil()))
})
Expand All @@ -201,7 +201,7 @@ func TestCreateVPC(t *testing.T) {
scope := setupClusterScope(clusterName, mockvpc)
scope.IBMVPCCluster.Spec = vpcCluster.Spec
mockvpc.EXPECT().ListVpcs(gomock.AssignableToTypeOf(listVpcsOptions)).Return(vpcCollection, detailedResponse, nil)
mockvpc.EXPECT().CreateVPC(gomock.AssignableToTypeOf(createVPCOptions)).Return(vpc, detailedResponse, errors.New("Error when deleting subnet"))
mockvpc.EXPECT().CreateVPC(gomock.AssignableToTypeOf(createVPCOptions)).Return(vpc, detailedResponse, errors.New("Failed to create VPC"))
_, err := scope.CreateVPC()
g.Expect(err).To(Not(BeNil()))
})
Expand Down Expand Up @@ -713,3 +713,204 @@ func TestDeleteSubnet(t *testing.T) {
})
})
}

func TestCreateLoadBalancer(t *testing.T) {
var (
mockvpc *mock.MockVpc
mockCtrl *gomock.Controller
)

setup := func(t *testing.T) {
t.Helper()
mockCtrl = gomock.NewController(t)
mockvpc = mock.NewMockVpc(mockCtrl)
}
teardown := func() {
mockCtrl.Finish()
}

vpcCluster := infrav1beta1.IBMVPCCluster{
Spec: infrav1beta1.IBMVPCClusterSpec{
ControlPlaneLoadBalancer: &infrav1beta1.VPCLoadBalancerSpec{
Name: "foo-load-balancer",
},
},
Status: infrav1beta1.IBMVPCClusterStatus{
Subnet: infrav1beta1.Subnet{
ID: core.StringPtr("foo-subnet-id"),
},
}}
expectedOutput := &vpcv1.LoadBalancer{
Name: core.StringPtr("foo-load-balancer"),
}

t.Run("Create LoadBalancer", func(t *testing.T) {
listLoadBalancersOptions := &vpcv1.ListLoadBalancersOptions{}
createLoadBalancerOptions := &vpcv1.CreateLoadBalancerOptions{}
loadBalancerCollection := &vpcv1.LoadBalancerCollection{
LoadBalancers: []vpcv1.LoadBalancer{
{
Name: core.StringPtr("foo-load-balancer-1"),
},
},
}
loadBalancer := &vpcv1.LoadBalancer{
Name: core.StringPtr("foo-load-balancer"),
}
detailedResponse := &core.DetailedResponse{}

t.Run("Should create LoadBalancer", func(t *testing.T) {
g := NewWithT(t)
setup(t)
t.Cleanup(teardown)
scope := setupClusterScope(clusterName, mockvpc)
scope.IBMVPCCluster.Spec = vpcCluster.Spec
scope.IBMVPCCluster.Status = vpcCluster.Status
mockvpc.EXPECT().ListLoadBalancers(gomock.AssignableToTypeOf(listLoadBalancersOptions)).Return(loadBalancerCollection, detailedResponse, nil)
mockvpc.EXPECT().CreateLoadBalancer(gomock.AssignableToTypeOf(createLoadBalancerOptions)).Return(loadBalancer, detailedResponse, nil)
out, err := scope.CreateLoadBalancer()
g.Expect(err).To(BeNil())
require.Equal(t, expectedOutput, out)
})

t.Run("Return exsisting LoadBalancer", func(t *testing.T) {
g := NewWithT(t)
setup(t)
t.Cleanup(teardown)
scope := setupClusterScope(clusterName, mockvpc)
vpcClusterCustom := infrav1beta1.IBMVPCCluster{
Spec: infrav1beta1.IBMVPCClusterSpec{
ControlPlaneLoadBalancer: &infrav1beta1.VPCLoadBalancerSpec{
Name: "foo-load-balancer-1",
},
}}
expectedOutput := &vpcv1.LoadBalancer{
Name: core.StringPtr("foo-load-balancer-1"),
}

scope.IBMVPCCluster.Spec = vpcClusterCustom.Spec
mockvpc.EXPECT().ListLoadBalancers(gomock.AssignableToTypeOf(listLoadBalancersOptions)).Return(loadBalancerCollection, detailedResponse, nil)
out, err := scope.CreateLoadBalancer()
g.Expect(err).To(BeNil())
require.Equal(t, expectedOutput, out)
})

t.Run("Error when listing LoadBalancer", func(t *testing.T) {
g := NewWithT(t)
setup(t)
t.Cleanup(teardown)
scope := setupClusterScope(clusterName, mockvpc)
scope.IBMVPCCluster.Spec = vpcCluster.Spec
scope.IBMVPCCluster.Status = vpcCluster.Status
mockvpc.EXPECT().ListLoadBalancers(gomock.AssignableToTypeOf(listLoadBalancersOptions)).Return(loadBalancerCollection, detailedResponse, errors.New("Failed to list LoadBalancer"))
_, err := scope.CreateLoadBalancer()
g.Expect(err).To(Not(BeNil()))
})

t.Run("Error when creating LoadBalancer", func(t *testing.T) {
g := NewWithT(t)
setup(t)
t.Cleanup(teardown)
scope := setupClusterScope(clusterName, mockvpc)
scope.IBMVPCCluster.Spec = vpcCluster.Spec
scope.IBMVPCCluster.Status = vpcCluster.Status
mockvpc.EXPECT().ListLoadBalancers(gomock.AssignableToTypeOf(listLoadBalancersOptions)).Return(loadBalancerCollection, detailedResponse, nil)
mockvpc.EXPECT().CreateLoadBalancer(gomock.AssignableToTypeOf(createLoadBalancerOptions)).Return(loadBalancer, detailedResponse, errors.New("Failed to create LoadBalancer"))
_, err := scope.CreateLoadBalancer()
g.Expect(err).To(Not(BeNil()))
})

t.Run("Error when subnet is nil", func(t *testing.T) {
g := NewWithT(t)
setup(t)
t.Cleanup(teardown)
scope := setupClusterScope(clusterName, mockvpc)
scope.IBMVPCCluster.Spec = vpcCluster.Spec
scope.IBMVPCCluster.Status = vpcCluster.Status
scope.IBMVPCCluster.Status.Subnet.ID = nil
mockvpc.EXPECT().ListLoadBalancers(gomock.AssignableToTypeOf(listLoadBalancersOptions)).Return(loadBalancerCollection, detailedResponse, nil)
_, err := scope.CreateLoadBalancer()
g.Expect(err).To(Not(BeNil()))
})
})
}

func TestDeleteLoadBalancer(t *testing.T) {
var (
mockvpc *mock.MockVpc
mockCtrl *gomock.Controller
)

setup := func(t *testing.T) {
t.Helper()
mockCtrl = gomock.NewController(t)
mockvpc = mock.NewMockVpc(mockCtrl)
}
teardown := func() {
mockCtrl.Finish()
}

vpcCluster := infrav1beta1.IBMVPCCluster{
Spec: infrav1beta1.IBMVPCClusterSpec{
ControlPlaneLoadBalancer: &infrav1beta1.VPCLoadBalancerSpec{
Name: "foo-load-balancer",
},
},
Status: infrav1beta1.IBMVPCClusterStatus{
VPCEndpoint: infrav1beta1.VPCEndpoint{
LBID: core.StringPtr("foo-load-balancer-id"),
},
}}

t.Run("Delete LoadBalancer", func(t *testing.T) {
listLoadBalancersOptions := &vpcv1.ListLoadBalancersOptions{}
loadBalancerCollection := &vpcv1.LoadBalancerCollection{
LoadBalancers: []vpcv1.LoadBalancer{
{
ID: core.StringPtr("foo-load-balancer-id"),
ProvisioningStatus: core.StringPtr("active"),
},
},
}
deleteLoadBalancerOptions := &vpcv1.DeleteLoadBalancerOptions{}
detailedResponse := &core.DetailedResponse{}

t.Run("Should delete LoadBalancer", func(t *testing.T) {
g := NewWithT(t)
setup(t)
t.Cleanup(teardown)
scope := setupClusterScope(clusterName, mockvpc)
scope.IBMVPCCluster.Spec = vpcCluster.Spec
scope.IBMVPCCluster.Status = vpcCluster.Status
mockvpc.EXPECT().ListLoadBalancers(gomock.AssignableToTypeOf(listLoadBalancersOptions)).Return(loadBalancerCollection, detailedResponse, nil)
mockvpc.EXPECT().DeleteLoadBalancer(gomock.AssignableToTypeOf(deleteLoadBalancerOptions)).Return(detailedResponse, nil)
_, err := scope.DeleteLoadBalancer()
g.Expect(err).To(BeNil())
})

t.Run("Error when listing LoadBalancer", func(t *testing.T) {
g := NewWithT(t)
setup(t)
t.Cleanup(teardown)
scope := setupClusterScope(clusterName, mockvpc)
scope.IBMVPCCluster.Spec = vpcCluster.Spec
scope.IBMVPCCluster.Status = vpcCluster.Status
mockvpc.EXPECT().ListLoadBalancers(gomock.AssignableToTypeOf(listLoadBalancersOptions)).Return(loadBalancerCollection, detailedResponse, errors.New("Failed to list LoadBalancer"))
_, err := scope.DeleteLoadBalancer()
g.Expect(err).To(Not(BeNil()))
})

t.Run("Error while deleting LoadBalancer", func(t *testing.T) {
g := NewWithT(t)
setup(t)
t.Cleanup(teardown)
scope := setupClusterScope(clusterName, mockvpc)
scope.IBMVPCCluster.Spec = vpcCluster.Spec
scope.IBMVPCCluster.Status = vpcCluster.Status
mockvpc.EXPECT().ListLoadBalancers(gomock.AssignableToTypeOf(listLoadBalancersOptions)).Return(loadBalancerCollection, detailedResponse, nil)
mockvpc.EXPECT().DeleteLoadBalancer(gomock.AssignableToTypeOf(deleteLoadBalancerOptions)).Return(detailedResponse, errors.New("Could not delete LoadBalancer"))
_, err := scope.DeleteLoadBalancer()
g.Expect(err).To(Not(BeNil()))
})
})
}
7 changes: 4 additions & 3 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package scope

import (
"context"
"fmt"

"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
Expand Down Expand Up @@ -200,11 +201,11 @@ func (m *MachineScope) CreateVPCLoadBalancerPoolMember(internalIP *string, targe
}

if *loadBalancer.ProvisioningStatus != string(infrav1beta1.VPCLoadBalancerStateActive) {
return nil, errors.Wrap(err, "load balancer is not in active state")
return nil, fmt.Errorf("load balancer is not in active state")
}

if len(loadBalancer.Pools) == 0 {
return nil, errors.Wrap(err, "no pools exist for the load balancer")
return nil, fmt.Errorf("no pools exist for the load balancer")
}

options := &vpcv1.CreateLoadBalancerPoolMemberOptions{}
Expand Down Expand Up @@ -273,7 +274,7 @@ func (m *MachineScope) DeleteVPCLoadBalancerPoolMember() error {
mtarget := member.Target.(*vpcv1.LoadBalancerPoolMemberTarget)
if *mtarget.Address == *instance.PrimaryNetworkInterface.PrimaryIP.Address {
if *loadBalancer.ProvisioningStatus != string(infrav1beta1.VPCLoadBalancerStateActive) {
return errors.Wrap(err, "load balancer is not in active state")
return fmt.Errorf("load balancer is not in active state")
}

deleteOptions := &vpcv1.DeleteLoadBalancerPoolMemberOptions{}
Expand Down
Loading

0 comments on commit fe289d3

Please sign in to comment.