Skip to content

Commit f640b63

Browse files
author
Rahul Sharma
committed
add unittests
1 parent b5bdc78 commit f640b63

File tree

2 files changed

+214
-1
lines changed

2 files changed

+214
-1
lines changed

cloud/linode/vpc.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,13 @@ func getNodeBalancerBackendIPv4SubnetID(client client.Client) (int, error) {
170170

171171
func validateVPCSubnetFlags() error {
172172
switch {
173+
case len(Options.VPCIDs) > 0 && len(Options.SubnetIDs) == 0:
174+
return fmt.Errorf("vpc-ids cannot be set without subnet-ids")
173175
case len(Options.SubnetIDs) > 0 && len(Options.VPCIDs) == 0:
174176
return fmt.Errorf("subnet-ids cannot be set without vpc-ids")
175-
case len(Options.SubnetNames) > 0 && len(Options.VPCNames) == 0:
177+
// since subnet-names defaults to "default", we also check if route-controller is enabled
178+
// and if so, we require vpc-names to be set
179+
case len(Options.SubnetNames) > 0 && len(Options.VPCNames) == 0 && Options.EnableRouteController:
176180
return fmt.Errorf("subnet-names cannot be set without vpc-names")
177181
case len(Options.VPCIDs) > 0 && len(Options.VPCNames) > 0:
178182
return fmt.Errorf("cannot have both vpc-ids and vpc-names set")

cloud/linode/vpc_test.go

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,212 @@ func TestGetNodeBalancerBackendIPv4SubnetID(t *testing.T) {
288288
}
289289
})
290290
}
291+
292+
func Test_validateVPCSubnetFlags(t *testing.T) {
293+
tests := []struct {
294+
name string
295+
vpcIDs []int
296+
vpcNames []string
297+
subnetIDs []int
298+
subnetNames []string
299+
wantErr bool
300+
}{
301+
{
302+
name: "invalid flags with vpc-names and vpc-ids set",
303+
vpcIDs: []int{1, 2},
304+
vpcNames: []string{"vpc1", "vpc2"},
305+
subnetIDs: []int{1},
306+
subnetNames: []string{},
307+
wantErr: true,
308+
},
309+
{
310+
name: "invalid flags with subnet-names and subnet-ids set",
311+
vpcIDs: []int{},
312+
vpcNames: []string{"vpc1", "vpc2"},
313+
subnetIDs: []int{1, 2},
314+
subnetNames: []string{"subnet1", "subnet2"},
315+
wantErr: true,
316+
},
317+
{
318+
name: "invalid flags with subnet-names and no vpc-names",
319+
vpcIDs: []int{},
320+
vpcNames: []string{},
321+
subnetIDs: []int{},
322+
subnetNames: []string{"subnet1", "subnet2"},
323+
wantErr: true,
324+
},
325+
{
326+
name: "invalid flags with subnet-ids and no vpc-ids",
327+
vpcIDs: []int{},
328+
vpcNames: []string{},
329+
subnetIDs: []int{1, 2},
330+
subnetNames: []string{},
331+
wantErr: true,
332+
},
333+
{
334+
name: "invalid flags with vpc-ids and no subnet-ids",
335+
vpcIDs: []int{1, 2},
336+
vpcNames: []string{},
337+
subnetIDs: []int{},
338+
subnetNames: []string{},
339+
wantErr: true,
340+
},
341+
{
342+
name: "valid flags with vpc-names and subnet-names",
343+
vpcIDs: []int{},
344+
vpcNames: []string{"vpc1", "vpc2"},
345+
subnetIDs: []int{},
346+
subnetNames: []string{"subnet1", "subnet2"},
347+
wantErr: false,
348+
},
349+
{
350+
name: "valid flags with vpc-ids and subnet-ids",
351+
vpcIDs: []int{1, 2},
352+
vpcNames: []string{},
353+
subnetIDs: []int{1, 2},
354+
subnetNames: []string{},
355+
wantErr: false,
356+
},
357+
}
358+
for _, tt := range tests {
359+
t.Run(tt.name, func(t *testing.T) {
360+
Options.VPCIDs = tt.vpcIDs
361+
Options.VPCNames = tt.vpcNames
362+
Options.SubnetIDs = tt.subnetIDs
363+
Options.SubnetNames = tt.subnetNames
364+
if err := validateVPCSubnetFlags(); (err != nil) != tt.wantErr {
365+
t.Errorf("validateVPCSubnetFlags() error = %v, wantErr %v", err, tt.wantErr)
366+
}
367+
})
368+
}
369+
}
370+
371+
func Test_resolveSubnetNames(t *testing.T) {
372+
t.Run("empty subnet ids", func(t *testing.T) {
373+
ctrl := gomock.NewController(t)
374+
defer ctrl.Finish()
375+
client := mocks.NewMockClient(ctrl)
376+
optionsSubnetIDs := Options.SubnetIDs
377+
currSubnetIDs := subnetIDs
378+
defer func() {
379+
Options.SubnetIDs = optionsSubnetIDs
380+
subnetIDs = currSubnetIDs
381+
}()
382+
Options.SubnetIDs = []int{}
383+
// client.EXPECT().GetVPCSubnet(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(&linodego.VPCSubnet{}, errors.New("error"))
384+
subnetNames, err := resolveSubnetNames(client, 10)
385+
if err != nil {
386+
t.Errorf("resolveSubnetNames() error = %v", err)
387+
return
388+
}
389+
if len(subnetNames) != 0 {
390+
t.Errorf("resolveSubnetNames() = %v, want %v", subnetNames, []string{})
391+
}
392+
})
393+
394+
t.Run("fails getting subnet ids", func(t *testing.T) {
395+
ctrl := gomock.NewController(t)
396+
defer ctrl.Finish()
397+
client := mocks.NewMockClient(ctrl)
398+
optionsSubnetIDs := Options.SubnetIDs
399+
currSubnetIDs := subnetIDs
400+
defer func() {
401+
Options.SubnetIDs = optionsSubnetIDs
402+
subnetIDs = currSubnetIDs
403+
}()
404+
Options.SubnetIDs = []int{1}
405+
client.EXPECT().GetVPCSubnet(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(&linodego.VPCSubnet{}, errors.New("error"))
406+
_, err := resolveSubnetNames(client, 10)
407+
require.Error(t, err)
408+
})
409+
410+
t.Run("correctly resolves subnet names", func(t *testing.T) {
411+
ctrl := gomock.NewController(t)
412+
defer ctrl.Finish()
413+
client := mocks.NewMockClient(ctrl)
414+
optionsSubnetIDs := Options.SubnetIDs
415+
currSubnetIDs := subnetIDs
416+
defer func() {
417+
Options.SubnetIDs = optionsSubnetIDs
418+
subnetIDs = currSubnetIDs
419+
}()
420+
Options.SubnetIDs = []int{1}
421+
client.EXPECT().GetVPCSubnet(gomock.Any(), gomock.Any(), gomock.Any()).Times(1).Return(&linodego.VPCSubnet{ID: 1, Label: "subnet1"}, nil)
422+
subnet, err := resolveSubnetNames(client, 10)
423+
require.NoError(t, err)
424+
require.Equal(t, []string{"subnet1"}, subnet, "Expected subnet names to match")
425+
})
426+
}
427+
428+
func Test_validateAndSetVPCSubnetFlags(t *testing.T) {
429+
t.Run("invalid flags", func(t *testing.T) {
430+
ctrl := gomock.NewController(t)
431+
defer ctrl.Finish()
432+
client := mocks.NewMockClient(ctrl)
433+
Options.VPCIDs = []int{1, 2}
434+
Options.SubnetIDs = []int{}
435+
err := validateAndSetVPCSubnetFlags(client)
436+
require.Error(t, err)
437+
})
438+
439+
t.Run("valid flags with vpc-ids and subnet-ids", func(t *testing.T) {
440+
ctrl := gomock.NewController(t)
441+
defer ctrl.Finish()
442+
client := mocks.NewMockClient(ctrl)
443+
currVPCIDs := Options.VPCIDs
444+
currSubnetIDs := Options.SubnetIDs
445+
defer func() {
446+
Options.VPCIDs = currVPCIDs
447+
Options.SubnetIDs = currSubnetIDs
448+
vpcIDs = map[string]int{}
449+
subnetIDs = map[string]int{}
450+
}()
451+
Options.VPCIDs = []int{1}
452+
Options.SubnetIDs = []int{1, 2}
453+
client.EXPECT().GetVPC(gomock.Any(), gomock.Any()).Times(1).Return(&linodego.VPC{ID: 1, Label: "test"}, nil)
454+
client.EXPECT().GetVPCSubnet(gomock.Any(), gomock.Any(), gomock.Any()).Times(2).Return(&linodego.VPCSubnet{ID: 1, Label: "subnet1"}, nil)
455+
err := validateAndSetVPCSubnetFlags(client)
456+
require.NoError(t, err)
457+
})
458+
459+
t.Run("error while making linode api call", func(t *testing.T) {
460+
ctrl := gomock.NewController(t)
461+
defer ctrl.Finish()
462+
client := mocks.NewMockClient(ctrl)
463+
currVPCIDs := Options.VPCIDs
464+
currSubnetIDs := Options.SubnetIDs
465+
defer func() {
466+
Options.VPCIDs = currVPCIDs
467+
Options.SubnetIDs = currSubnetIDs
468+
vpcIDs = map[string]int{}
469+
subnetIDs = map[string]int{}
470+
}()
471+
Options.VPCIDs = []int{1}
472+
Options.SubnetIDs = []int{1, 2}
473+
Options.VPCNames = []string{}
474+
Options.SubnetNames = []string{}
475+
client.EXPECT().GetVPC(gomock.Any(), gomock.Any()).Times(1).Return(nil, errors.New("error"))
476+
err := validateAndSetVPCSubnetFlags(client)
477+
require.Error(t, err)
478+
})
479+
480+
t.Run("valid flags with vpc-names and subnet-names", func(t *testing.T) {
481+
ctrl := gomock.NewController(t)
482+
defer ctrl.Finish()
483+
client := mocks.NewMockClient(ctrl)
484+
currVPCNames := Options.VPCNames
485+
currSubnetNames := Options.SubnetNames
486+
defer func() {
487+
Options.VPCNames = currVPCNames
488+
Options.SubnetNames = currSubnetNames
489+
vpcIDs = map[string]int{}
490+
subnetIDs = map[string]int{}
491+
}()
492+
Options.VPCNames = []string{"vpc1"}
493+
Options.SubnetNames = []string{"subnet1", "subnet2"}
494+
Options.VPCIDs = []int{}
495+
Options.SubnetIDs = []int{}
496+
err := validateAndSetVPCSubnetFlags(client)
497+
require.NoError(t, err)
498+
})
499+
}

0 commit comments

Comments
 (0)