Skip to content
This repository has been archived by the owner on Jan 3, 2023. It is now read-only.

Commit

Permalink
Adding more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhiljindal committed Mar 13, 2018
1 parent f422948 commit 50c253a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/kubemci/pkg/gcp/forwardingrule/fake_forwardingrulesyncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ func (f *FakeForwardingRuleSyncer) ListLoadBalancerStatuses() ([]status.LoadBala
}

func (f *FakeForwardingRuleSyncer) RemoveClustersFromStatus(clusters []string) error {
// TODO: Implement this.
removeClusters := make(map[string]bool, len(clusters))
for _, c := range clusters {
removeClusters[c] = true
}
for i, fr := range f.EnsuredForwardingRules {
newClusters := []string{}
for _, c := range fr.Clusters {
if _, has := removeClusters[c]; !has {
newClusters = append(newClusters, c)
}
}
f.EnsuredForwardingRules[i].Clusters = newClusters
}
return nil
}
78 changes: 78 additions & 0 deletions app/kubemci/pkg/gcp/loadbalancer/loadbalancersyncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,84 @@ func TestDeleteLoadBalancer(t *testing.T) {
}
}

func TestRemoveFromClusters(t *testing.T) {
lbName := "lb-name"
nodePort := int64(32211)
igName := "my-fake-ig"
igZone := "my-fake-zone"
zoneLink := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/fake-project/zones/%s", igZone)
clusters := []string{"cluster1", "cluster2"}
lbc := newLoadBalancerSyncer(lbName)
ipAddress := &compute.Address{
Name: "ipAddressName",
Address: "1.2.3.4",
}
// Reserve a global address. User is supposed to do this before calling CreateLoadBalancer.
lbc.ipp.ReserveGlobalAddress(ipAddress)
ing, err := setupLBCForCreateIng(lbc, nodePort, igName, igZone, zoneLink, ipAddress)
if err != nil {
t.Fatalf("%s", err)
}
if err := lbc.CreateLoadBalancer(ing, true /*forceUpdate*/, clusters); err != nil {
t.Fatalf("unexpected error %s", err)
}
fhc := lbc.hcs.(*healthcheck.FakeHealthCheckSyncer)
if len(fhc.EnsuredHealthChecks) == 0 {
t.Errorf("unexpected health checks not created")
}
fbs := lbc.bss.(*backendservice.FakeBackendServiceSyncer)
if len(fbs.EnsuredBackendServices) == 0 {
t.Errorf("unexpected backend services not created")
}
fum := lbc.ums.(*urlmap.FakeURLMapSyncer)
if len(fum.EnsuredURLMaps) == 0 {
t.Errorf("unexpected url maps not created")
}
ftp := lbc.tps.(*targetproxy.FakeTargetProxySyncer)
if len(ftp.EnsuredTargetProxies) == 0 {
t.Errorf("unexpected target proxies not created")
}
ffr := lbc.frs.(*forwardingrule.FakeForwardingRuleSyncer)
if len(ffr.EnsuredForwardingRules) == 0 {
t.Errorf("unexpected forwarding rules not created")
}
ffw := lbc.fws.(*firewallrule.FakeFirewallRuleSyncer)
if len(ffw.EnsuredFirewallRules) == 0 {
t.Errorf("unexpected firewall rules not created")
}
// Verify that the load balancer is spread to both clusters.
if err := verifyClusters(lbName, lbc.frs, clusters); err != nil {
t.Errorf("%s", err)
}

// Remove the load balancer from the first cluster and verify that the load balancer status is updated appropriately.
removeClients := map[string]kubeclient.Interface{
"cluster1": lbc.clients["cluster1"],
}
if err := lbc.RemoveFromClusters(ing, removeClients, false /*forceUpdate*/); err != nil {
t.Errorf("unexpected error in removing existing load balancer from clusters: %s", err)
}
if err := verifyClusters(lbName, lbc.frs, []string{"cluster2"}); err != nil {
t.Errorf("%s", err)
}

// Cleanup.
if err := lbc.DeleteLoadBalancer(ing); err != nil {
t.Fatalf("unexpected error in deleting load balancer: %s", err)
}
}

func verifyClusters(lbName string, frs forwardingrule.ForwardingRuleSyncerInterface, expectedClusters []string) error {
status, err := frs.GetLoadBalancerStatus(lbName)
if err != nil {
return fmt.Errorf("unexpected error in getting load balancer status: %s", err)
}
if !reflect.DeepEqual(status.Clusters, expectedClusters) {
return fmt.Errorf("unexpected list of clusters, expected: %v, got: %v", expectedClusters, status.Clusters)
}
return nil
}

func TestFormatLoadBalancersList(t *testing.T) {
testCases := []struct {
statuses []status.LoadBalancerStatus
Expand Down

0 comments on commit 50c253a

Please sign in to comment.