Skip to content

Commit 5230187

Browse files
author
Rahul Sharma
committed
add unittests for changes
1 parent e36372f commit 5230187

File tree

2 files changed

+192
-6
lines changed

2 files changed

+192
-6
lines changed

cloud/linode/fake_linode_test.go

Lines changed: 107 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,53 @@ 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+
}
531+
subnets = append(subnets, subnet)
532+
f.subnet[subnet.ID] = &subnet
533+
}
534+
vpc := linodego.VPC{
535+
ID: rand.Intn(9999),
536+
Label: vco.Label,
537+
Description: vco.Description,
538+
Region: vco.Region,
539+
Subnets: subnets,
540+
}
541+
542+
f.vpc[vpc.ID] = &vpc
543+
resp, err := json.Marshal(vpc)
544+
if err != nil {
545+
f.t.Fatal(err)
546+
}
547+
_, _ = w.Write(resp)
548+
})
549+
550+
f.mux.HandleFunc("DELETE /v4/vpcs/{vpcId}", func(w http.ResponseWriter, r *http.Request) {
551+
vpcid, err := strconv.Atoi(r.PathValue("vpcId"))
552+
if err != nil {
553+
f.t.Fatal(err)
554+
}
555+
556+
for k, v := range f.vpc {
557+
if v.ID == vpcid {
558+
for _, s := range v.Subnets {
559+
delete(f.subnet, s.ID)
560+
}
561+
delete(f.vpc, k)
562+
}
563+
}
564+
})
565+
465566
f.mux.HandleFunc("POST /v4/networking/firewalls/{firewallId}/devices", func(w http.ResponseWriter, r *http.Request) {
466567
fdco := linodego.FirewallDeviceCreateOptions{}
467568
if err := json.NewDecoder(r.Body).Decode(&fdco); err != nil {

cloud/linode/loadbalancers_test.go

Lines changed: 85 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,83 @@ 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+
defer func() {
504+
Options.VPCNames = vpcNames
505+
}()
506+
Options.VPCNames = "test1"
507+
_, _ = client.CreateVPC(context.TODO(), linodego.VPCCreateOptions{
508+
Label: "test1",
509+
Description: "",
510+
Region: "us-west",
511+
Subnets: []linodego.VPCSubnetCreateOptions{
512+
{
513+
Label: "default",
514+
IPv4: "10.0.0.0/8",
515+
},
516+
},
517+
})
518+
519+
err := testCreateNodeBalancer(t, client, f, ann, nil)
520+
if err != nil {
521+
t.Fatalf("expected a nil error, got %v", err)
522+
}
523+
}
524+
525+
func testCreateNodeBalancerWithVPCAnnotationOverwrite(t *testing.T, client *linodego.Client, f *fakeAPI) {
526+
// provision multiple vpcs
527+
vpcNames := Options.VPCNames
528+
defer func() {
529+
Options.VPCNames = vpcNames
530+
}()
531+
Options.VPCNames = "test1"
532+
533+
_, _ = client.CreateVPC(context.TODO(), linodego.VPCCreateOptions{
534+
Label: "test1",
535+
Description: "",
536+
Region: "us-west",
537+
Subnets: []linodego.VPCSubnetCreateOptions{
538+
{
539+
Label: "default",
540+
IPv4: "10.0.0.0/8",
541+
},
542+
},
543+
})
544+
545+
_, _ = client.CreateVPC(context.TODO(), linodego.VPCCreateOptions{
546+
Label: "test2",
547+
Description: "",
548+
Region: "us-west",
549+
Subnets: []linodego.VPCSubnetCreateOptions{
550+
{
551+
Label: "subnet1",
552+
IPv4: "10.0.0.0/8",
553+
},
554+
},
555+
})
556+
ann := map[string]string{
557+
annotations.NodeBalancerBackendIPv4Range: "10.100.0.0/30",
558+
annotations.NodeBalancerBackendVPCName: "test2",
559+
//annotations.NodeBalancerBackendSubnetName: "subnet1",
560+
}
561+
err := testCreateNodeBalancer(t, client, f, ann, nil)
562+
if err != nil {
563+
t.Fatalf("expected a nil error, got %v", err)
564+
}
565+
}
566+
482567
func testUpdateLoadBalancerAddNode(t *testing.T, client *linodego.Client, f *fakeAPI) {
483568
svc := &v1.Service{
484569
ObjectMeta: metav1.ObjectMeta{

0 commit comments

Comments
 (0)