Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] : add unittests for vpc.go file #281

Merged
merged 3 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
storage.googleapis.com:443
cli.codecov.io:443
api.codecov.io:443
ingest.codecov.io:443
raw.githubusercontent.com:443
get.helm.sh:443

Expand Down
149 changes: 149 additions & 0 deletions cloud/linode/vpc_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package linode

import (
"context"
"errors"
"net/http"
"reflect"
"sort"
"testing"

"github.com/golang/mock/gomock"
"github.com/linode/linode-cloud-controller-manager/cloud/linode/client/mocks"
"github.com/linode/linodego"
"github.com/stretchr/testify/assert"
)

func TestGetAllVPCIDs(t *testing.T) {
tests := []struct {
name string
vpcIDs map[string]int
want []int
}{
{
name: "multiple vpcs present",
vpcIDs: map[string]int{"test1": 1, "test2": 2, "test3": 3},
want: []int{1, 2, 3},
},
{
name: "no vpc present",
vpcIDs: map[string]int{},
want: []int{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vpcIDs = tt.vpcIDs
got := GetAllVPCIDs()
sort.Ints(got)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllVPCIDs() = %v, want %v", got, tt.want)
}
})
}
}

func TestGetVPCID(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq: any reason to not use tabular test format for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ListVPCs() call and its response is changing in each test run, hence I avoided tabular format. Not sure if we can achieve that with tabular format unless we do if/else with bunch of conditions to decide which mock call to use.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah i see. I'm okay with that

t.Run("vpcID in cache", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
vpcIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
got, err := GetVPCID(context.TODO(), client, "test3")
if err != nil {
t.Errorf("GetVPCID() error = %v", err)
return
}
if got != vpcIDs["test3"] {
t.Errorf("GetVPCID() = %v, want %v", got, vpcIDs["test3"])
}
})

t.Run("vpcID not in cache and listVPCs return error", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
vpcIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
client.EXPECT().ListVPCs(gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPC{}, errors.New("error"))
got, err := GetVPCID(context.TODO(), client, "test4")
assert.Error(t, err)
if got != 0 {
t.Errorf("GetVPCID() = %v, want %v", got, 0)
}
})

t.Run("vpcID not in cache and listVPCs return nothing", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
vpcIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
client.EXPECT().ListVPCs(gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPC{}, nil)
got, err := GetVPCID(context.TODO(), client, "test4")
assert.ErrorIs(t, err, vpcLookupError{"test4"})
if got != 0 {
t.Errorf("GetVPCID() = %v, want %v", got, 0)
}
})

t.Run("vpcID not in cache and listVPCs return vpc info", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
vpcIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
client.EXPECT().ListVPCs(gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPC{{ID: 4, Label: "test4"}}, nil)
got, err := GetVPCID(context.TODO(), client, "test4")
assert.NoError(t, err)
if got != 4 {
t.Errorf("GetVPCID() = %v, want %v", got, 4)
}
})
}

func TestGetVPCIPAddresses(t *testing.T) {
t.Run("vpc id not found", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
vpcIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
client.EXPECT().ListVPCs(gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPC{}, nil)
_, err := GetVPCIPAddresses(context.TODO(), client, "test4")
assert.Error(t, err)
})

t.Run("vpc id found but listing ip addresses fails with 404 error", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
vpcIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCIP{}, &linodego.Error{Code: http.StatusNotFound, Message: "[404] [label] VPC not found"})
_, err := GetVPCIPAddresses(context.TODO(), client, "test3")
assert.Error(t, err)
_, exists := vpcIDs["test3"]
assert.False(t, exists, "test3 key should get deleted from vpcIDs map")
})

t.Run("vpc id found but listing ip addresses fails with 500 error", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
vpcIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCIP{}, &linodego.Error{Code: http.StatusInternalServerError, Message: "[500] [label] Internal Server Error"})
_, err := GetVPCIPAddresses(context.TODO(), client, "test1")
assert.Error(t, err)
_, exists := vpcIDs["test1"]
assert.True(t, exists, "test1 key should not get deleted from vpcIDs map")
})

t.Run("vpc id found and listing vpc ipaddresses succeeds", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client := mocks.NewMockClient(ctrl)
vpcIDs = map[string]int{"test1": 1, "test2": 2, "test3": 3}
client.EXPECT().ListVPCs(gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPC{{ID: 10, Label: "test10"}}, nil)
client.EXPECT().ListVPCIPAddresses(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return([]linodego.VPCIP{}, nil)
_, err := GetVPCIPAddresses(context.TODO(), client, "test10")
assert.NoError(t, err)
_, exists := vpcIDs["test10"]
assert.True(t, exists, "test10 key should be present in vpcIDs map")
})
}
Loading