From 9d6203bee3fc17e19af5f023e15f682aadf5c30a Mon Sep 17 00:00:00 2001 From: Rahul Sharma Date: Thu, 19 Dec 2024 17:31:11 -0500 Subject: [PATCH] [test] : add unittests for vpc.go file (#281) * add unittests for vpc.go file * allow ingest.codecov.io as well for codecov to work * don't run vpc tests in parallel as they modify global var data --- .github/workflows/ci.yml | 1 + cloud/linode/vpc_test.go | 149 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 cloud/linode/vpc_test.go diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1491fed7..e3760d9f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/cloud/linode/vpc_test.go b/cloud/linode/vpc_test.go new file mode 100644 index 00000000..9e99b675 --- /dev/null +++ b/cloud/linode/vpc_test.go @@ -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) { + 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") + }) +}