|
5 | 5 | "fmt" |
6 | 6 | "net/http" |
7 | 7 | "net/http/httptest" |
| 8 | + "os" |
8 | 9 | "reflect" |
9 | 10 | "strconv" |
10 | 11 | "testing" |
@@ -166,6 +167,10 @@ func TestCCMLoadBalancers(t *testing.T) { |
166 | 167 | name: "makeLoadBalancerStatus", |
167 | 168 | f: testMakeLoadBalancerStatus, |
168 | 169 | }, |
| 170 | + { |
| 171 | + name: "makeLoadBalancerStatusEnvVar", |
| 172 | + f: testMakeLoadBalancerStatusEnvVar, |
| 173 | + }, |
169 | 174 | { |
170 | 175 | name: "Cleanup does not call the API unless Service annotated", |
171 | 176 | f: testCleanupDoesntCall, |
@@ -1448,6 +1453,55 @@ func testMakeLoadBalancerStatus(t *testing.T, client *linodego.Client, _ *fakeAP |
1448 | 1453 | } |
1449 | 1454 | } |
1450 | 1455 |
|
| 1456 | +func testMakeLoadBalancerStatusEnvVar(t *testing.T, client *linodego.Client, _ *fakeAPI) { |
| 1457 | + ipv4 := "192.168.0.1" |
| 1458 | + hostname := "nb-192-168-0-1.newark.nodebalancer.linode.com" |
| 1459 | + nb := &linodego.NodeBalancer{ |
| 1460 | + IPv4: &ipv4, |
| 1461 | + Hostname: &hostname, |
| 1462 | + } |
| 1463 | + |
| 1464 | + svc := &v1.Service{ |
| 1465 | + ObjectMeta: metav1.ObjectMeta{ |
| 1466 | + Name: "test", |
| 1467 | + Annotations: make(map[string]string, 1), |
| 1468 | + }, |
| 1469 | + } |
| 1470 | + |
| 1471 | + expectedStatus := &v1.LoadBalancerStatus{ |
| 1472 | + Ingress: []v1.LoadBalancerIngress{{ |
| 1473 | + Hostname: hostname, |
| 1474 | + IP: ipv4, |
| 1475 | + }}, |
| 1476 | + } |
| 1477 | + status := makeLoadBalancerStatus(svc, nb) |
| 1478 | + if !reflect.DeepEqual(status, expectedStatus) { |
| 1479 | + t.Errorf("expected status for basic service to be %#v; got %#v", expectedStatus, status) |
| 1480 | + } |
| 1481 | + |
| 1482 | + os.Setenv("LINODE_HOSTNAME_ONLY_INGRESS", "true") |
| 1483 | + expectedStatus.Ingress[0] = v1.LoadBalancerIngress{Hostname: hostname} |
| 1484 | + status = makeLoadBalancerStatus(svc, nb) |
| 1485 | + if !reflect.DeepEqual(status, expectedStatus) { |
| 1486 | + t.Errorf("expected status for %q annotated service to be %#v; got %#v", annLinodeHostnameOnlyIngress, expectedStatus, status) |
| 1487 | + } |
| 1488 | + |
| 1489 | + os.Setenv("LINODE_HOSTNAME_ONLY_INGRESS", "false") |
| 1490 | + expectedStatus.Ingress[0] = v1.LoadBalancerIngress{Hostname: hostname} |
| 1491 | + status = makeLoadBalancerStatus(svc, nb) |
| 1492 | + if reflect.DeepEqual(status, expectedStatus) { |
| 1493 | + t.Errorf("expected status for %q annotated service to be %#v; got %#v", annLinodeHostnameOnlyIngress, expectedStatus, status) |
| 1494 | + } |
| 1495 | + |
| 1496 | + os.Setenv("LINODE_HOSTNAME_ONLY_INGRESS", "banana") |
| 1497 | + expectedStatus.Ingress[0] = v1.LoadBalancerIngress{Hostname: hostname} |
| 1498 | + status = makeLoadBalancerStatus(svc, nb) |
| 1499 | + if reflect.DeepEqual(status, expectedStatus) { |
| 1500 | + t.Errorf("expected status for %q annotated service to be %#v; got %#v", annLinodeHostnameOnlyIngress, expectedStatus, status) |
| 1501 | + } |
| 1502 | + os.Unsetenv("LINODE_HOSTNAME_ONLY_INGRESS") |
| 1503 | +} |
| 1504 | + |
1451 | 1505 | func testCleanupDoesntCall(t *testing.T, client *linodego.Client, fakeAPI *fakeAPI) { |
1452 | 1506 | region := "us-west" |
1453 | 1507 | nb1, err := client.CreateNodeBalancer(context.TODO(), linodego.NodeBalancerCreateOptions{Region: region}) |
|
0 commit comments