Skip to content

Commit 5cdebde

Browse files
author
Rahul Sharma
committed
add unittests for changes
1 parent 9a876ca commit 5cdebde

File tree

3 files changed

+198
-10
lines changed

3 files changed

+198
-10
lines changed

cloud/linode/fake_linode_test.go

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ import (
1919
const apiVersion = "v4"
2020

2121
type fakeAPI struct {
22-
t *testing.T
23-
nb map[string]*linodego.NodeBalancer
24-
nbc map[string]*linodego.NodeBalancerConfig
25-
nbn map[string]*linodego.NodeBalancerNode
26-
fw map[int]*linodego.Firewall // map of firewallID -> firewall
27-
fwd map[int]map[int]*linodego.FirewallDevice // map of firewallID -> firewallDeviceID:FirewallDevice
22+
t *testing.T
23+
nb map[string]*linodego.NodeBalancer
24+
nbc map[string]*linodego.NodeBalancerConfig
25+
nbn map[string]*linodego.NodeBalancerNode
26+
fw map[int]*linodego.Firewall // map of firewallID -> firewall
27+
fwd map[int]map[int]*linodego.FirewallDevice // map of firewallID -> firewallDeviceID:FirewallDevice
28+
nbvpcc map[string]*linodego.NodeBalancerVPCConfig
29+
vpc map[int]*linodego.VPC
30+
subnet map[int]*linodego.VPCSubnet
2831

2932
requests map[fakeRequest]struct{}
3033
mux *http.ServeMux
@@ -44,6 +47,9 @@ func newFake(t *testing.T) *fakeAPI {
4447
nbn: make(map[string]*linodego.NodeBalancerNode),
4548
fw: make(map[int]*linodego.Firewall),
4649
fwd: make(map[int]map[int]*linodego.FirewallDevice),
50+
nbvpcc: make(map[string]*linodego.NodeBalancerVPCConfig),
51+
vpc: make(map[int]*linodego.VPC),
52+
subnet: make(map[int]*linodego.VPCSubnet),
4753
requests: make(map[fakeRequest]struct{}),
4854
mux: http.NewServeMux(),
4955
}
@@ -117,6 +123,54 @@ func (f *fakeAPI) setupRoutes() {
117123
_, _ = w.Write(rr)
118124
})
119125

126+
f.mux.HandleFunc("GET /v4/vpcs", func(w http.ResponseWriter, r *http.Request) {
127+
res := 0
128+
data := []linodego.VPC{}
129+
filter := r.Header.Get("X-Filter")
130+
if filter == "" {
131+
for _, v := range f.vpc {
132+
data = append(data, *v)
133+
}
134+
} else {
135+
var fs map[string]string
136+
err := json.Unmarshal([]byte(filter), &fs)
137+
if err != nil {
138+
f.t.Fatal(err)
139+
}
140+
for _, v := range f.vpc {
141+
if v.Label != "" && fs["label"] != "" && v.Label == fs["label"] {
142+
data = append(data, *v)
143+
}
144+
}
145+
}
146+
147+
resp := paginatedResponse[linodego.VPC]{
148+
Page: 1,
149+
Pages: 1,
150+
Results: res,
151+
Data: data,
152+
}
153+
rr, _ := json.Marshal(resp)
154+
_, _ = w.Write(rr)
155+
})
156+
157+
f.mux.HandleFunc("GET /v4/vpcs/{vpcId}/subnets", func(w http.ResponseWriter, r *http.Request) {
158+
res := 0
159+
vpcID, err := strconv.Atoi(r.PathValue("vpcId"))
160+
if err != nil {
161+
f.t.Fatal(err)
162+
}
163+
164+
resp := paginatedResponse[linodego.VPCSubnet]{
165+
Page: 1,
166+
Pages: 1,
167+
Results: res,
168+
Data: f.vpc[vpcID].Subnets,
169+
}
170+
rr, _ := json.Marshal(resp)
171+
_, _ = w.Write(rr)
172+
})
173+
120174
f.mux.HandleFunc("GET /v4/nodebalancers/{nodeBalancerId}", func(w http.ResponseWriter, r *http.Request) {
121175
nb, found := f.nb[r.PathValue("nodeBalancerId")]
122176
if !found {
@@ -462,6 +516,54 @@ func (f *fakeAPI) setupRoutes() {
462516
_, _ = w.Write(resp)
463517
})
464518

519+
f.mux.HandleFunc("POST /v4/vpcs", func(w http.ResponseWriter, r *http.Request) {
520+
vco := linodego.VPCCreateOptions{}
521+
if err := json.NewDecoder(r.Body).Decode(&vco); err != nil {
522+
f.t.Fatal(err)
523+
}
524+
525+
subnets := []linodego.VPCSubnet{}
526+
for _, s := range vco.Subnets {
527+
subnet := linodego.VPCSubnet{
528+
ID: rand.Intn(9999),
529+
IPv4: s.IPv4,
530+
Label: s.Label,
531+
}
532+
subnets = append(subnets, subnet)
533+
f.subnet[subnet.ID] = &subnet
534+
}
535+
vpc := linodego.VPC{
536+
ID: rand.Intn(9999),
537+
Label: vco.Label,
538+
Description: vco.Description,
539+
Region: vco.Region,
540+
Subnets: subnets,
541+
}
542+
543+
f.vpc[vpc.ID] = &vpc
544+
resp, err := json.Marshal(vpc)
545+
if err != nil {
546+
f.t.Fatal(err)
547+
}
548+
_, _ = w.Write(resp)
549+
})
550+
551+
f.mux.HandleFunc("DELETE /v4/vpcs/{vpcId}", func(w http.ResponseWriter, r *http.Request) {
552+
vpcid, err := strconv.Atoi(r.PathValue("vpcId"))
553+
if err != nil {
554+
f.t.Fatal(err)
555+
}
556+
557+
for k, v := range f.vpc {
558+
if v.ID == vpcid {
559+
for _, s := range v.Subnets {
560+
delete(f.subnet, s.ID)
561+
}
562+
delete(f.vpc, k)
563+
}
564+
}
565+
})
566+
465567
f.mux.HandleFunc("POST /v4/networking/firewalls/{firewallId}/devices", func(w http.ResponseWriter, r *http.Request) {
466568
fdco := linodego.FirewallDeviceCreateOptions{}
467569
if err := json.NewDecoder(r.Body).Decode(&fdco); err != nil {

cloud/linode/loadbalancers.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -788,17 +788,15 @@ func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Servi
788788
return 0, fmt.Errorf("CCM not configured with VPC, cannot create NodeBalancer with specified annotation")
789789
}
790790
vpcName := strings.Split(Options.VPCNames, ",")[0]
791-
specifiedVPCName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendVPCName]
792-
if ok {
791+
if specifiedVPCName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendVPCName]; ok {
793792
vpcName = specifiedVPCName
794793
}
795794
vpcID, err := GetVPCID(ctx, l.client, vpcName)
796795
if err != nil {
797796
return 0, err
798797
}
799798
subnetName := strings.Split(Options.SubnetNames, ",")[0]
800-
specifiedSubnetName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendSubnetName]
801-
if ok {
799+
if specifiedSubnetName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendSubnetName]; ok {
802800
subnetName = specifiedSubnetName
803801
}
804802
return GetSubnetID(ctx, l.client, vpcID, subnetName)

cloud/linode/loadbalancers_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ func TestCCMLoadBalancers(t *testing.T) {
150150
name: "Create Load Balancer With Invalid Firewall ACL - NO Allow Or Deny",
151151
f: testCreateNodeBalanceWithNoAllowOrDenyList,
152152
},
153+
{
154+
name: "Create Load Balancer With VPC Backend",
155+
f: testCreateNodeBalancerWithVPCBackend,
156+
},
157+
{
158+
name: "Create Load Balancer With VPC Backend - Overwrite VPC Name and Subnet with Annotation",
159+
f: testCreateNodeBalancerWithVPCAnnotationOverwrite,
160+
},
153161
{
154162
name: "Create Load Balancer With Global Tags set",
155163
f: testCreateNodeBalancerWithGlobalTags,
@@ -479,6 +487,86 @@ func testCreateNodeBalancerWithGlobalTags(t *testing.T, client *linodego.Client,
479487
}
480488
}
481489

490+
func testCreateNodeBalancerWithVPCBackend(t *testing.T, client *linodego.Client, f *fakeAPI) {
491+
// test when no VPCs are present
492+
ann := map[string]string{
493+
annotations.NodeBalancerBackendIPv4Range: "10.100.0.0/30",
494+
}
495+
if err := testCreateNodeBalancer(t, client, f, ann, nil); err == nil {
496+
t.Fatalf("expected nodebalancer creation to fail")
497+
}
498+
499+
f.ResetRequests()
500+
501+
// provision vpc and test again
502+
vpcNames := Options.VPCNames
503+
subnetNames := Options.SubnetNames
504+
defer func() {
505+
Options.VPCNames = vpcNames
506+
Options.SubnetNames = subnetNames
507+
}()
508+
Options.VPCNames = "test1"
509+
Options.SubnetNames = "default"
510+
_, _ = client.CreateVPC(context.TODO(), linodego.VPCCreateOptions{
511+
Label: "test1",
512+
Description: "",
513+
Region: "us-west",
514+
Subnets: []linodego.VPCSubnetCreateOptions{
515+
{
516+
Label: "default",
517+
IPv4: "10.0.0.0/8",
518+
},
519+
},
520+
})
521+
522+
err := testCreateNodeBalancer(t, client, f, ann, nil)
523+
if err != nil {
524+
t.Fatalf("expected a nil error, got %v", err)
525+
}
526+
}
527+
528+
func testCreateNodeBalancerWithVPCAnnotationOverwrite(t *testing.T, client *linodego.Client, f *fakeAPI) {
529+
// provision multiple vpcs
530+
vpcNames := Options.VPCNames
531+
defer func() {
532+
Options.VPCNames = vpcNames
533+
}()
534+
Options.VPCNames = "test1"
535+
536+
_, _ = client.CreateVPC(context.TODO(), linodego.VPCCreateOptions{
537+
Label: "test1",
538+
Description: "",
539+
Region: "us-west",
540+
Subnets: []linodego.VPCSubnetCreateOptions{
541+
{
542+
Label: "default",
543+
IPv4: "10.0.0.0/8",
544+
},
545+
},
546+
})
547+
548+
_, _ = client.CreateVPC(context.TODO(), linodego.VPCCreateOptions{
549+
Label: "test2",
550+
Description: "",
551+
Region: "us-west",
552+
Subnets: []linodego.VPCSubnetCreateOptions{
553+
{
554+
Label: "subnet1",
555+
IPv4: "10.0.0.0/8",
556+
},
557+
},
558+
})
559+
ann := map[string]string{
560+
annotations.NodeBalancerBackendIPv4Range: "10.100.0.0/30",
561+
annotations.NodeBalancerBackendVPCName: "test2",
562+
annotations.NodeBalancerBackendSubnetName: "subnet1",
563+
}
564+
err := testCreateNodeBalancer(t, client, f, ann, nil)
565+
if err != nil {
566+
t.Fatalf("expected a nil error, got %v", err)
567+
}
568+
}
569+
482570
func testUpdateLoadBalancerAddNode(t *testing.T, client *linodego.Client, f *fakeAPI) {
483571
svc := &v1.Service{
484572
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)