Skip to content

Commit

Permalink
Refactor l4_test assertInternalLbResourcesDeleted function
Browse files Browse the repository at this point in the history
  • Loading branch information
panslava committed Sep 9, 2022
1 parent 515e82c commit d82032a
Showing 1 changed file with 65 additions and 20 deletions.
85 changes: 65 additions & 20 deletions pkg/loadbalancers/l4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,8 @@ func TestEnsureInternalLoadBalancerAllPorts(t *testing.T) {
}

func assertInternalLbResources(t *testing.T, l4 *L4, nodeNames []string, resourceAnnotations map[string]string) {
t.Helper()

err := verifyNodesFirewall(l4, nodeNames)
if err != nil {
t.Errorf("verifyNodesFirewall(_, %v) returned error %v, want nil", nodeNames, err)
Expand Down Expand Up @@ -1576,11 +1578,10 @@ func buildExpectedAnnotations(l4 *L4) map[string]string {
}

func assertInternalLbResourcesDeleted(t *testing.T, l4 *L4) {
frName := l4.GetFRName()
t.Helper()

resourceName := l4.namer.L4Backend(l4.Service.Namespace, l4.Service.Name)
hcNameShared := l4.namer.L4HealthCheck(l4.Service.Namespace, l4.Service.Name, true)
hcFwNameShared := l4.namer.L4HealthCheckFirewall(l4.Service.Namespace, l4.Service.Name, true)
hcNameNonShared := l4.namer.L4HealthCheck(l4.Service.Namespace, l4.Service.Name, false)
hcFwNameNonShared := l4.namer.L4HealthCheckFirewall(l4.Service.Namespace, l4.Service.Name, false)

fwNames := []string{
Expand All @@ -1590,33 +1591,77 @@ func assertInternalLbResourcesDeleted(t *testing.T, l4 *L4) {
}

for _, fwName := range fwNames {
firewall, err := l4.cloud.GetFirewall(fwName)
if err == nil || firewall != nil {
t.Errorf("Expected error when looking up firewall rule after deletion")
err := verifyFirewallNotExists(l4, fwName)
if err != nil {
t.Errorf("verifyFirewallNotExists(_, %s) returned error %v, want nil", fwName, err)
}
}

// Check forwarding rule is deleted
frName := l4.GetFRName()
err := verifyForwardingRuleNotExists(l4, frName)
if err != nil {
t.Errorf("verifyForwardingRuleNotExists(_, %s) returned error %v, want nil", frName, err)
}

hcNameShared := l4.namer.L4HealthCheck(l4.Service.Namespace, l4.Service.Name, true)
err = verifyHealthCheckNotExists(l4, hcNameShared)
if err != nil {
t.Errorf("verifyHealthCheckNotExists(_, %s)", hcNameShared)
}

hcNameNonShared := l4.namer.L4HealthCheck(l4.Service.Namespace, l4.Service.Name, true)
err = verifyHealthCheckNotExists(l4, hcNameNonShared)
if err != nil {
t.Errorf("verifyHealthCheckNotExists(_, %s)", hcNameNonShared)
}

err = verifyBackendServiceNotExists(l4, resourceName)
if err != nil {
t.Errorf("verifyBackendServiceNotExists(_, %s)", resourceName)
}

err = verifyAddressNotExists(l4, resourceName)
if err != nil {
t.Errorf("verifyAddressNotExists(_, %s)", resourceName)
}
}

func verifyFirewallNotExists(l4 *L4, fwName string) error {
firewall, err := l4.cloud.GetFirewall(fwName)
if err == nil || firewall != nil {
return fmt.Errorf("firewall %s exists, expected not", fwName)
}
return nil
}

func verifyForwardingRuleNotExists(l4 *L4, frName string) error {
fwdRule, err := l4.cloud.GetRegionForwardingRule(frName, l4.cloud.Region())
if err == nil || fwdRule != nil {
t.Errorf("Expected error when looking up forwarding rule after deletion")
return fmt.Errorf("forwarding rule %s exists, expected not", frName)
}
return nil
}

// Check that HealthChecks are deleted
healthcheck, err := l4.cloud.GetHealthCheck(hcNameShared)
if err == nil || healthcheck != nil {
t.Errorf("Expected error when looking up shared healthcheck after deletion")
func verifyHealthCheckNotExists(l4 *L4, hcName string) error {
healthCheck, err := l4.cloud.GetHealthCheck(hcName)
if err == nil || healthCheck != nil {
return fmt.Errorf("health check %s exists, expected not", hcName)
}
healthcheck, err = l4.cloud.GetHealthCheck(hcNameNonShared)
if err == nil || healthcheck != nil {
t.Errorf("Expected error when looking up non-shared healthcheck after deletion")
}
bs, err := l4.cloud.GetRegionBackendService(resourceName, l4.cloud.Region())
return nil
}

func verifyBackendServiceNotExists(l4 *L4, bsName string) error {
bs, err := l4.cloud.GetRegionBackendService(bsName, l4.cloud.Region())
if err == nil || bs != nil {
t.Errorf("Expected error when looking up backend service after deletion")
return fmt.Errorf("backend service %s exists, expected not", bsName)
}
addr, err := l4.cloud.GetRegionAddress(resourceName, l4.cloud.Region())
return nil
}

func verifyAddressNotExists(l4 *L4, addressName string) error {
addr, err := l4.cloud.GetRegionAddress(addressName, l4.cloud.Region())
if err == nil || addr != nil {
t.Errorf("Expected error when looking up IP address after deletion")
return fmt.Errorf("address %s exists, expected not", addressName)
}
return nil
}

0 comments on commit d82032a

Please sign in to comment.