From 04f5dcb785210b11b6ce5f4ef60217e066420273 Mon Sep 17 00:00:00 2001 From: Khaja Omer Date: Mon, 18 Mar 2024 10:55:56 -0500 Subject: [PATCH] Saving prog --- cloud/scope/client.go | 1 + cloud/scope/cluster.go | 3 +- cloud/services/loadbalancers_test.go | 134 +++++++++++++++++++++++++-- mock/client.go | 14 +++ 4 files changed, 141 insertions(+), 11 deletions(-) diff --git a/cloud/scope/client.go b/cloud/scope/client.go index 193e83db6..8be42816d 100644 --- a/cloud/scope/client.go +++ b/cloud/scope/client.go @@ -18,6 +18,7 @@ type LinodeClient interface { CreateNodeBalancerConfig(ctx context.Context, nodebalancerID int, opts linodego.NodeBalancerConfigCreateOptions) (*linodego.NodeBalancerConfig, error) GetInstanceIPAddresses(ctx context.Context, linodeID int) (*linodego.InstanceIPAddressResponse, error) DeleteNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, nodeID int) error + DeleteNodeBalancer(ctx context.Context, nodebalancerID int) error } // LinodeClientBuilder is a function that returns a LinodeClient. diff --git a/cloud/scope/cluster.go b/cloud/scope/cluster.go index 8f1ea0f9f..b2aa50b4a 100644 --- a/cloud/scope/cluster.go +++ b/cloud/scope/cluster.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" - "github.com/linode/linodego" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/util/patch" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" @@ -85,7 +84,7 @@ func NewClusterScope(ctx context.Context, apiKey string, params ClusterScopePara type ClusterScope struct { client k8sClient PatchHelper *patch.Helper - LinodeClient *linodego.Client + LinodeClient LinodeClient Cluster *clusterv1.Cluster LinodeCluster *infrav1alpha1.LinodeCluster } diff --git a/cloud/services/loadbalancers_test.go b/cloud/services/loadbalancers_test.go index e2a4263ed..00ddd3b9b 100644 --- a/cloud/services/loadbalancers_test.go +++ b/cloud/services/loadbalancers_test.go @@ -2,6 +2,7 @@ package services import ( "context" + "fmt" "reflect" "testing" @@ -9,10 +10,12 @@ import ( "github.com/linode/cluster-api-provider-linode/cloud/scope" "github.com/linode/cluster-api-provider-linode/mock" "github.com/linode/linodego" + "github.com/stretchr/testify/assert" "go.uber.org/mock/gomock" infrav1alpha1 "github.com/linode/cluster-api-provider-linode/api/v1alpha1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/utils/ptr" ) func TestCreateNodeBalancer(t *testing.T) { @@ -24,7 +27,7 @@ func TestCreateNodeBalancer(t *testing.T) { wantErr bool expects func(mock *mock.MockLinodeClient) expectedNodeBalancer *linodego.NodeBalancer - expected error + expectedError error }{ // TODO: Add test cases. { @@ -37,15 +40,127 @@ func TestCreateNodeBalancer(t *testing.T) { }, Spec: infrav1alpha1.LinodeClusterSpec{ Network: infrav1alpha1.NetworkSpec{ - NodeBalancerID: nil, + NodeBalancerID: ptr.To(1234), }, }, }, }, expects: func(mock *mock.MockLinodeClient) { mock.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return([]linodego.NodeBalancer{}, nil) - mock.EXPECT().CreateNodeBalancer(gomock.Any(), gomock.Any()).Return(&linodego.NodeBalancer{}, nil) + mock.EXPECT().CreateNodeBalancer(gomock.Any(), gomock.Any()).Return(&linodego.NodeBalancer{ + ID: 1234, + }, nil) }, + expectedNodeBalancer: &linodego.NodeBalancer{ + ID: 1234, + }, + expectedError: nil, + + }, + { + name: "Success - List NodeBalancers returns one nodebalancer and we return that", + clusterScope: &scope.ClusterScope{ + LinodeCluster: &infrav1alpha1.LinodeCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + UID: "test-uid", + }, + Spec: infrav1alpha1.LinodeClusterSpec{ + Network: infrav1alpha1.NetworkSpec{ + NodeBalancerID: ptr.To(1234), + }, + }, + }, + }, + expects: func(mock *mock.MockLinodeClient) { + mock.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return([]linodego.NodeBalancer{ + { + ID: 1234, + Label: ptr.To("test"), + Tags: []string{"test-uid"}, + }, + }, nil) + }, + expectedNodeBalancer: &linodego.NodeBalancer{ + ID: 1234, + Label: ptr.To("test"), + Tags: []string{"test-uid"}, + }, + expectedError: nil, + }, + { + name: "Error - List NodeBalancers returns one nodebalancer but there is a nodebalancer conflict", + clusterScope: &scope.ClusterScope{ + LinodeCluster: &infrav1alpha1.LinodeCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + UID: "test-uid", + }, + Spec: infrav1alpha1.LinodeClusterSpec{ + Network: infrav1alpha1.NetworkSpec{ + NodeBalancerID: ptr.To(1234), + }, + }, + }, + }, + expects: func(mock *mock.MockLinodeClient) { + mock.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return([]linodego.NodeBalancer{ + { + ID: 1234, + Label: ptr.To("test"), + Tags: []string{"test"}, + }, + }, nil) + }, + expectedNodeBalancer: &linodego.NodeBalancer{ + ID: 1234, + Label: ptr.To("test"), + Tags: []string{"test"}, + }, + expectedError: fmt.Errorf("NodeBalancer conflict"), + }, + { + name: "Error - List NodeBalancers returns an error", + clusterScope: &scope.ClusterScope{ + LinodeCluster: &infrav1alpha1.LinodeCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + UID: "test-uid", + }, + Spec: infrav1alpha1.LinodeClusterSpec{ + Network: infrav1alpha1.NetworkSpec{ + NodeBalancerID: ptr.To(1234), + }, + }, + }, + }, + expects: func(mock *mock.MockLinodeClient) { + mock.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("Unable to list NodeBalancers")) + }, + expectedNodeBalancer: nil, + expectedError: fmt.Errorf("Unable to list NodeBalancers"), + }, + { + name: "Error - Create NodeBalancer returns an error", + clusterScope: &scope.ClusterScope{ + LinodeCluster: &infrav1alpha1.LinodeCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-cluster", + UID: "test-uid", + }, + Spec: infrav1alpha1.LinodeClusterSpec{ + Network: infrav1alpha1.NetworkSpec{ + NodeBalancerID: ptr.To(1234), + }, + }, + }, + }, + expects: func(mock *mock.MockLinodeClient) { + mock.EXPECT().ListNodeBalancers(gomock.Any(), gomock.Any()).Return([]linodego.NodeBalancer{}, nil) + mock.EXPECT().CreateNodeBalancer(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("Unable to create NodeBalancer")) + }, + expectedNodeBalancer: nil, + expectedError: fmt.Errorf("Unable to create NodeBalancer"), }, } @@ -59,15 +174,16 @@ func TestCreateNodeBalancer(t *testing.T) { mockLinodeClient := mock.NewMockLinodeClient(ctrl) + testcase.clusterScope.LinodeClient = mockLinodeClient + testcase.expects(mockLinodeClient) got, err := CreateNodeBalancer(context.Background(), testcase.clusterScope, logr.Discard()) - if (err != nil) != testcase.wantErr { - t.Errorf("CreateNodeBalancer() error = %v, wantErr %v", err, testcase.wantErr) - return - } - if !reflect.DeepEqual(got, testcase.want) { - t.Errorf("CreateNodeBalancer() = %v, want %v", got, testcase.want) + if testcase.expectedError != nil { + assert.ErrorContains(t, err, testcase.expectedError.Error()) + } else { + assert.NotEmpty(t, got) + assert.Equal(t, testcase.expectedNodeBalancer, got) } }) } diff --git a/mock/client.go b/mock/client.go index 51e4a6c98..f1f2ed0f4 100644 --- a/mock/client.go +++ b/mock/client.go @@ -104,6 +104,20 @@ func (mr *MockLinodeClientMockRecorder) CreateObjectStorageKey(ctx, opts any) *g return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateObjectStorageKey", reflect.TypeOf((*MockLinodeClient)(nil).CreateObjectStorageKey), ctx, opts) } +// DeleteNodeBalancer mocks base method. +func (m *MockLinodeClient) DeleteNodeBalancer(ctx context.Context, nodebalancerID int) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeleteNodeBalancer", ctx, nodebalancerID) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeleteNodeBalancer indicates an expected call of DeleteNodeBalancer. +func (mr *MockLinodeClientMockRecorder) DeleteNodeBalancer(ctx, nodebalancerID any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteNodeBalancer", reflect.TypeOf((*MockLinodeClient)(nil).DeleteNodeBalancer), ctx, nodebalancerID) +} + // DeleteNodeBalancerNode mocks base method. func (m *MockLinodeClient) DeleteNodeBalancerNode(ctx context.Context, nodebalancerID, configID, nodeID int) error { m.ctrl.T.Helper()