Skip to content

Commit 09d4b02

Browse files
committed
Test all API calls (GET, DELETE)
1 parent 661b47d commit 09d4b02

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

cloud/linode/fake_linode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func newFake(t *testing.T) *fakeAPI {
4242
}
4343
}
4444

45-
func (f *fakeAPI) Reset() {
46-
*f = *newFake(f.t)
45+
func (f *fakeAPI) ResetRequests() {
46+
f.requests = make(map[fakeRequest]struct{})
4747
}
4848

4949
func (f *fakeAPI) recordRequest(r *http.Request) {

cloud/linode/loadbalancers_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,25 +1449,28 @@ func testMakeLoadBalancerStatus(t *testing.T, client *linodego.Client, _ *fakeAP
14491449
}
14501450

14511451
func testCleanupDoesntCall(t *testing.T, client *linodego.Client, fakeAPI *fakeAPI) {
1452-
ipv4 := "192.168.0.1"
1453-
hostname := "nb-192-168-0-1.newark.nodebalancer.linode.com"
1454-
nb := &linodego.NodeBalancer{
1455-
IPv4: &ipv4,
1456-
Hostname: &hostname,
1452+
region := "us-west"
1453+
nb1, err := client.CreateNodeBalancer(context.TODO(), linodego.NodeBalancerCreateOptions{Region: region})
1454+
if err != nil {
1455+
t.Fatal(err)
1456+
}
1457+
nb2, err := client.CreateNodeBalancer(context.TODO(), linodego.NodeBalancerCreateOptions{Region: region})
1458+
if err != nil {
1459+
t.Fatal(err)
14571460
}
14581461

14591462
svc := &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "test"}}
14601463
svcAnn := &v1.Service{
14611464
ObjectMeta: metav1.ObjectMeta{
14621465
Name: "test",
1463-
Annotations: map[string]string{annLinodeNodeBalancerID: "12345"},
1466+
Annotations: map[string]string{annLinodeNodeBalancerID: strconv.Itoa(nb2.ID)},
14641467
},
14651468
}
1466-
svc.Status.LoadBalancer = *makeLoadBalancerStatus(svc, nb)
1467-
svcAnn.Status.LoadBalancer = *makeLoadBalancerStatus(svc, nb)
1468-
lb := &loadbalancers{client, "us-west", nil}
1469+
svc.Status.LoadBalancer = *makeLoadBalancerStatus(svc, nb1)
1470+
svcAnn.Status.LoadBalancer = *makeLoadBalancerStatus(svcAnn, nb1)
1471+
lb := &loadbalancers{client, region, nil}
14691472

1470-
fakeAPI.Reset()
1473+
fakeAPI.ResetRequests()
14711474
t.Run("non-annotated service shouldn't call the API during cleanup", func(t *testing.T) {
14721475
if err := lb.cleanupOldNodeBalancer(context.TODO(), svc); err != nil {
14731476
t.Fatal(err)
@@ -1477,19 +1480,19 @@ func testCleanupDoesntCall(t *testing.T, client *linodego.Client, fakeAPI *fakeA
14771480
}
14781481
})
14791482

1480-
fakeAPI.Reset()
1483+
fakeAPI.ResetRequests()
14811484
t.Run("annotated service calls the API to load said NB", func(t *testing.T) {
14821485
if err := lb.cleanupOldNodeBalancer(context.TODO(), svcAnn); err != nil {
14831486
t.Fatal(err)
14841487
}
1485-
if len(fakeAPI.requests) != 1 {
1486-
t.Fatalf("unexpected API calls: %v", fakeAPI.requests)
1488+
expectedRequests := map[fakeRequest]struct{}{
1489+
{Path: "/nodebalancers", Body: "", Method: "GET"}: {},
1490+
{Path: fmt.Sprintf("/nodebalancers/%v", nb2.ID), Body: "", Method: "GET"}: {},
1491+
{Path: fmt.Sprintf("/nodebalancers/%v", nb1.ID), Body: "", Method: "DELETE"}: {},
14871492
}
1488-
// TODO(okokes): we need to change this to `/v4/nodebalancer` once we upgrade to linodego v1
1489-
if !fakeAPI.didRequestOccur("GET", "/nodebalancers", "") {
1490-
t.Fatalf("unexpected API calls: %v", fakeAPI.requests)
1493+
if !reflect.DeepEqual(fakeAPI.requests, expectedRequests) {
1494+
t.Fatalf("expected requests %#v, got %#v instead", expectedRequests, fakeAPI.requests)
14911495
}
1492-
// TODO(okokes): check for DELETE on the old NB
14931496
})
14941497
}
14951498

0 commit comments

Comments
 (0)