Skip to content

Commit

Permalink
Saving prog
Browse files Browse the repository at this point in the history
  • Loading branch information
komer3 committed Mar 18, 2024
1 parent cf367ef commit 04f5dcb
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 11 deletions.
1 change: 1 addition & 0 deletions cloud/scope/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
134 changes: 125 additions & 9 deletions cloud/services/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package services

import (
"context"
"fmt"
"reflect"
"testing"

"github.com/go-logr/logr"
"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) {
Expand All @@ -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.
{
Expand All @@ -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"),

},
}
Expand All @@ -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)
}
})
}
Expand Down
14 changes: 14 additions & 0 deletions mock/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 04f5dcb

Please sign in to comment.