From e4336dfdfacbc77511516c171bc045933eac101e Mon Sep 17 00:00:00 2001 From: Rahul Sharma Date: Fri, 31 May 2024 21:19:20 +0000 Subject: [PATCH] update unittests --- .github/filters.yml | 2 + .../v1alpha2/linodecluster_webhook_test.go | 57 ++++++- cloud/services/loadbalancers_test.go | 156 ++++++++++++++---- 3 files changed, 174 insertions(+), 41 deletions(-) diff --git a/.github/filters.yml b/.github/filters.yml index 326b5dede..18dd31996 100644 --- a/.github/filters.yml +++ b/.github/filters.yml @@ -20,6 +20,8 @@ kubeadm_self-healing: - templates/flavors/kubeadm/self-healing/* kubeadm_vpcless: - templates/flavors/kubeadm/vpcless/* +kubeadm_konnectivity: + - templates/flavors/kubeadm/konnectivity/* k3s: - templates/flavors/k3s/default/* diff --git a/api/infrastructure/v1alpha2/linodecluster_webhook_test.go b/api/infrastructure/v1alpha2/linodecluster_webhook_test.go index d2839c645..8c9c87867 100644 --- a/api/infrastructure/v1alpha2/linodecluster_webhook_test.go +++ b/api/infrastructure/v1alpha2/linodecluster_webhook_test.go @@ -17,17 +17,56 @@ limitations under the License. package v1alpha2 import ( - . "github.com/onsi/ginkgo/v2" -) + "context" + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "go.uber.org/mock/gomock" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -var _ = Describe("LinodeCluster Webhook", func() { + "github.com/linode/cluster-api-provider-linode/mock" - Context("When creating LinodeCluster under Conversion Webhook", func() { - It("Should get the converted version of LinodeCluster", func() { + . "github.com/linode/cluster-api-provider-linode/mock/mocktest" +) - // TODO(user): Add your logic here +func TestValidateLinodeCluster(t *testing.T) { + t.Parallel() - }) - }) + var ( + cluster = LinodeCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "example", + Namespace: "example", + }, + Spec: LinodeClusterSpec{ + Region: "example", + Network: NetworkSpec{ + LoadBalancerType: "NodeBalancer", + Konnectivity: true, + }, + }, + } + ) -}) + NewSuite(t, mock.MockLinodeClient{}).Run( + OneOf( + Path( + Call("valid", func(ctx context.Context, mck Mock) { + mck.LinodeClient.EXPECT().GetRegion(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() + }), + Result("success", func(ctx context.Context, mck Mock) { + assert.NoError(t, cluster.validateLinodeCluster(ctx, mck.LinodeClient)) + }), + ), + ), + OneOf( + Path(Call("invalid region", func(ctx context.Context, mck Mock) { + mck.LinodeClient.EXPECT().GetRegion(gomock.Any(), gomock.Any()).Return(nil, errors.New("invalid region")).AnyTimes() + })), + ), + Result("error", func(ctx context.Context, mck Mock) { + assert.Error(t, cluster.validateLinodeCluster(ctx, mck.LinodeClient)) + }), + ) +} diff --git a/cloud/services/loadbalancers_test.go b/cloud/services/loadbalancers_test.go index 8ed0741e5..87a6c2c3f 100644 --- a/cloud/services/loadbalancers_test.go +++ b/cloud/services/loadbalancers_test.go @@ -39,6 +39,7 @@ func TestCreateNodeBalancer(t *testing.T) { Spec: infrav1alpha2.LinodeClusterSpec{ Network: infrav1alpha2.NetworkSpec{ NodeBalancerID: ptr.To(1234), + Konnectivity: true, }, }, }, @@ -185,14 +186,14 @@ func TestCreateNodeBalancerConfig(t *testing.T) { t.Parallel() tests := []struct { - name string - clusterScope *scope.ClusterScope - expectedConfig *linodego.NodeBalancerConfig - expectedError error - expects func(*mock.MockLinodeClient) + name string + clusterScope *scope.ClusterScope + expectedConfigs []*linodego.NodeBalancerConfig + expectedError error + expects func(*mock.MockLinodeClient) }{ { - name: "Success - Create NodeBalancerConfig using default LB port", + name: "Success - Create NodeBalancerConfig using default LB ports", clusterScope: &scope.ClusterScope{ LinodeClient: nil, LinodeCluster: &infrav1alpha2.LinodeCluster{ @@ -203,16 +204,26 @@ func TestCreateNodeBalancerConfig(t *testing.T) { Spec: infrav1alpha2.LinodeClusterSpec{ Network: infrav1alpha2.NetworkSpec{ NodeBalancerID: ptr.To(1234), + Konnectivity: true, }, }, }, }, - expectedConfig: &linodego.NodeBalancerConfig{ - Port: defaultApiserverLBPort, - Protocol: linodego.ProtocolTCP, - Algorithm: linodego.AlgorithmRoundRobin, - Check: linodego.CheckConnection, - NodeBalancerID: 1234, + expectedConfigs: []*linodego.NodeBalancerConfig{ + { + Port: defaultApiserverLBPort, + Protocol: linodego.ProtocolTCP, + Algorithm: linodego.AlgorithmRoundRobin, + Check: linodego.CheckConnection, + NodeBalancerID: 1234, + }, + { + Port: defaultKonnectivityLBPort, + Protocol: linodego.ProtocolTCP, + Algorithm: linodego.AlgorithmRoundRobin, + Check: linodego.CheckConnection, + NodeBalancerID: 1234, + }, }, expects: func(mockClient *mock.MockLinodeClient) { mockClient.EXPECT().CreateNodeBalancerConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(&linodego.NodeBalancerConfig{ @@ -222,10 +233,17 @@ func TestCreateNodeBalancerConfig(t *testing.T) { Check: linodego.CheckConnection, NodeBalancerID: 1234, }, nil) + mockClient.EXPECT().CreateNodeBalancerConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(&linodego.NodeBalancerConfig{ + Port: defaultKonnectivityLBPort, + Protocol: linodego.ProtocolTCP, + Algorithm: linodego.AlgorithmRoundRobin, + Check: linodego.CheckConnection, + NodeBalancerID: 1234, + }, nil) }, }, { - name: "Success - Create NodeBalancerConfig using assigned LB port", + name: "Success - Create NodeBalancerConfig using assigned LB ports", clusterScope: &scope.ClusterScope{ LinodeClient: nil, LinodeCluster: &infrav1alpha2.LinodeCluster{ @@ -235,18 +253,29 @@ func TestCreateNodeBalancerConfig(t *testing.T) { }, Spec: infrav1alpha2.LinodeClusterSpec{ Network: infrav1alpha2.NetworkSpec{ - NodeBalancerID: ptr.To(1234), - ApiserverLoadBalancerPort: 80, + NodeBalancerID: ptr.To(1234), + Konnectivity: true, + ApiserverLoadBalancerPort: 80, + KonnectivityLoadBalancerPort: 90, }, }, }, }, - expectedConfig: &linodego.NodeBalancerConfig{ - Port: 80, - Protocol: linodego.ProtocolTCP, - Algorithm: linodego.AlgorithmRoundRobin, - Check: linodego.CheckConnection, - NodeBalancerID: 1234, + expectedConfigs: []*linodego.NodeBalancerConfig{ + { + Port: 80, + Protocol: linodego.ProtocolTCP, + Algorithm: linodego.AlgorithmRoundRobin, + Check: linodego.CheckConnection, + NodeBalancerID: 1234, + }, + { + Port: 90, + Protocol: linodego.ProtocolTCP, + Algorithm: linodego.AlgorithmRoundRobin, + Check: linodego.CheckConnection, + NodeBalancerID: 1234, + }, }, expects: func(mockClient *mock.MockLinodeClient) { mockClient.EXPECT().CreateNodeBalancerConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(&linodego.NodeBalancerConfig{ @@ -256,10 +285,17 @@ func TestCreateNodeBalancerConfig(t *testing.T) { Check: linodego.CheckConnection, NodeBalancerID: 1234, }, nil) + mockClient.EXPECT().CreateNodeBalancerConfig(gomock.Any(), gomock.Any(), gomock.Any()).Return(&linodego.NodeBalancerConfig{ + Port: 90, + Protocol: linodego.ProtocolTCP, + Algorithm: linodego.AlgorithmRoundRobin, + Check: linodego.CheckConnection, + NodeBalancerID: 1234, + }, nil) }, }, { - name: "Error - CreateNodeBalancerConfig() returns and error", + name: "Error - CreateNodeBalancerConfig() returns an error", clusterScope: &scope.ClusterScope{ LinodeClient: nil, LinodeCluster: &infrav1alpha2.LinodeCluster{ @@ -270,16 +306,26 @@ func TestCreateNodeBalancerConfig(t *testing.T) { Spec: infrav1alpha2.LinodeClusterSpec{ Network: infrav1alpha2.NetworkSpec{ NodeBalancerID: ptr.To(1234), + Konnectivity: true, }, }, }, }, - expectedConfig: &linodego.NodeBalancerConfig{ - Port: defaultApiserverLBPort, - Protocol: linodego.ProtocolTCP, - Algorithm: linodego.AlgorithmRoundRobin, - Check: linodego.CheckConnection, - NodeBalancerID: 1234, + expectedConfigs: []*linodego.NodeBalancerConfig{ + { + Port: defaultApiserverLBPort, + Protocol: linodego.ProtocolTCP, + Algorithm: linodego.AlgorithmRoundRobin, + Check: linodego.CheckConnection, + NodeBalancerID: 1234, + }, + { + Port: defaultKonnectivityLBPort, + Protocol: linodego.ProtocolTCP, + Algorithm: linodego.AlgorithmRoundRobin, + Check: linodego.CheckConnection, + NodeBalancerID: 1234, + }, }, expectedError: fmt.Errorf("error creating NodeBalancerConfig"), expects: func(mockClient *mock.MockLinodeClient) { @@ -306,7 +352,7 @@ func TestCreateNodeBalancerConfig(t *testing.T) { assert.ErrorContains(t, err, testcase.expectedError.Error()) } else { assert.NotEmpty(t, got) - assert.Equal(t, testcase.expectedConfig, got[0]) + assert.Equal(t, testcase.expectedConfigs, got) } }) } @@ -695,18 +741,21 @@ func TestDeleteNodeFromNB(t *testing.T) { Spec: infrav1alpha2.LinodeClusterSpec{ ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"}, Network: infrav1alpha2.NetworkSpec{ - NodeBalancerID: ptr.To(1234), - ApiserverNodeBalancerConfigID: ptr.To(5678), + NodeBalancerID: ptr.To(1234), + ApiserverNodeBalancerConfigID: ptr.To(5678), + Konnectivity: true, + KonnectivityNodeBalancerConfigID: ptr.To(4444), }, }, }, }, expects: func(mockClient *mock.MockLinodeClient) { mockClient.EXPECT().DeleteNodeBalancerNode(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) + mockClient.EXPECT().DeleteNodeBalancerNode(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) }, }, { - name: "Error - Deleting Node from NodeBalancer", + name: "Error - Deleting Apiserver Node from NodeBalancer", machineScope: &scope.MachineScope{ Machine: &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ @@ -745,6 +794,49 @@ func TestDeleteNodeFromNB(t *testing.T) { mockClient.EXPECT().DeleteNodeBalancerNode(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("error deleting node from NodeBalancer")) }, }, + { + name: "Error - Deleting Konnectivity Node from NodeBalancer", + machineScope: &scope.MachineScope{ + Machine: &clusterv1.Machine{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-machine", + UID: "test-uid", + Labels: map[string]string{ + clusterv1.MachineControlPlaneLabel: "true", + }, + }, + }, + LinodeMachine: &infrav1alpha1.LinodeMachine{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-machine", + UID: "test-uid", + }, + Spec: infrav1alpha1.LinodeMachineSpec{ + InstanceID: ptr.To(123), + }, + }, + LinodeCluster: &infrav1alpha2.LinodeCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + UID: "test-uid", + }, + Spec: infrav1alpha2.LinodeClusterSpec{ + ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "1.2.3.4"}, + Network: infrav1alpha2.NetworkSpec{ + NodeBalancerID: ptr.To(1234), + ApiserverNodeBalancerConfigID: ptr.To(5678), + Konnectivity: true, + KonnectivityNodeBalancerConfigID: ptr.To(4444), + }, + }, + }, + }, + expectedError: fmt.Errorf("error deleting node from NodeBalancer"), + expects: func(mockClient *mock.MockLinodeClient) { + mockClient.EXPECT().DeleteNodeBalancerNode(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil) + mockClient.EXPECT().DeleteNodeBalancerNode(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fmt.Errorf("error deleting node from NodeBalancer")) + }, + }, } for _, tt := range tests { testcase := tt