Skip to content

Commit

Permalink
update unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulait committed May 31, 2024
1 parent f096fb6 commit e4336df
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .github/filters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand Down
57 changes: 48 additions & 9 deletions api/infrastructure/v1alpha2/linodecluster_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}),
)
}
156 changes: 124 additions & 32 deletions cloud/services/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestCreateNodeBalancer(t *testing.T) {
Spec: infrav1alpha2.LinodeClusterSpec{
Network: infrav1alpha2.NetworkSpec{
NodeBalancerID: ptr.To(1234),
Konnectivity: true,
},
},
},
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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) {
Expand All @@ -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)
}
})
}
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e4336df

Please sign in to comment.