Skip to content

Commit 257ee06

Browse files
author
Rahul Sharma
committed
add unittests for changes
1 parent 4068117 commit 257ee06

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
@@ -800,17 +800,15 @@ func (l *loadbalancers) getSubnetIDForSVC(ctx context.Context, service *v1.Servi
800800
return 0, fmt.Errorf("CCM not configured with VPC, cannot create NodeBalancer with specified annotation")
801801
}
802802
vpcName := strings.Split(Options.VPCNames, ",")[0]
803-
specifiedVPCName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendVPCName]
804-
if ok {
803+
if specifiedVPCName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendVPCName]; ok {
805804
vpcName = specifiedVPCName
806805
}
807806
vpcID, err := GetVPCID(ctx, l.client, vpcName)
808807
if err != nil {
809808
return 0, err
810809
}
811810
subnetName := strings.Split(Options.SubnetNames, ",")[0]
812-
specifiedSubnetName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendSubnetName]
813-
if ok {
811+
if specifiedSubnetName, ok := service.GetAnnotations()[annotations.NodeBalancerBackendSubnetName]; ok {
814812
subnetName = specifiedSubnetName
815813
}
816814
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
@@ -152,6 +152,14 @@ func TestCCMLoadBalancers(t *testing.T) {
152152
name: "Create Load Balancer With Invalid Firewall ACL - NO Allow Or Deny",
153153
f: testCreateNodeBalanceWithNoAllowOrDenyList,
154154
},
155+
{
156+
name: "Create Load Balancer With VPC Backend",
157+
f: testCreateNodeBalancerWithVPCBackend,
158+
},
159+
{
160+
name: "Create Load Balancer With VPC Backend - Overwrite VPC Name and Subnet with Annotation",
161+
f: testCreateNodeBalancerWithVPCAnnotationOverwrite,
162+
},
155163
{
156164
name: "Create Load Balancer With Global Tags set",
157165
f: testCreateNodeBalancerWithGlobalTags,
@@ -481,6 +489,86 @@ func testCreateNodeBalancerWithGlobalTags(t *testing.T, client *linodego.Client,
481489
}
482490
}
483491

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

0 commit comments

Comments
 (0)