Skip to content

Commit

Permalink
Add unit test to validate if IG is created for NEG only ingress
Browse files Browse the repository at this point in the history
  • Loading branch information
freehan committed May 27, 2020
1 parent bbb7a36 commit 8b677dd
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package controller

import (
"fmt"
"net/http"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -53,6 +54,7 @@ import (
var (
nodePortCounter = 30000
clusterUID = "aaaaa"
fakeZone = "zone-a"
)

// newLoadBalancerController create a loadbalancer controller.
Expand All @@ -76,7 +78,7 @@ func newLoadBalancerController() *LoadBalancerController {
// TODO(rramkumar): Fix this so we don't have to override with our fake
lbc.instancePool = instances.NewNodePool(instances.NewFakeInstanceGroups(sets.NewString(), namer), namer)
lbc.l7Pool = loadbalancers.NewLoadBalancerPool(fakeGCE, namer, events.RecorderProducerMock{}, namer_util.NewFrontendNamerFactory(namer, ""))
lbc.instancePool.Init(&instances.FakeZoneLister{Zones: []string{"zone-a"}})
lbc.instancePool.Init(&instances.FakeZoneLister{Zones: []string{fakeZone}})

lbc.hasSynced = func() bool { return true }

Expand Down Expand Up @@ -175,6 +177,38 @@ func TestIngressSyncError(t *testing.T) {
}
}

// TestNEGOnlyIngress asserts that `sync` will not create IG when there is only NEG backends for the ingress
func TestNEGOnlyIngress(t *testing.T) {
lbc := newLoadBalancerController()

svc := test.NewService(types.NamespacedName{Name: "my-service", Namespace: "default"}, api_v1.ServiceSpec{
Type: api_v1.ServiceTypeNodePort,
Ports: []api_v1.ServicePort{{Port: 80}},
})
negAnnotation := annotations.NegAnnotation{Ingress: true}
svc.Annotations = map[string]string{
annotations.NEGAnnotationKey: negAnnotation.String(),
}
addService(lbc, svc)
someBackend := backend("my-service", intstr.FromInt(80))
ing := test.NewIngress(types.NamespacedName{Name: "my-ingress", Namespace: "default"},
v1beta1.IngressSpec{
Backend: &someBackend,
})
addIngress(lbc, ing)

ingStoreKey := getKey(ing, t)
err := lbc.sync(ingStoreKey)
if err != nil {
t.Fatalf("lbc.sync(%v) = %v, want nil", ingStoreKey, err)
}

ig, err := lbc.instancePool.Get(lbc.ctx.ClusterNamer.InstanceGroup(), fakeZone)
if err != nil && !utils.IsHTTPErrorCode(err, http.StatusNotFound) {
t.Errorf("lbc.ctx.Cloud.ListInstanceGroups(%v) = %v, %v, want [], http.StatusNotFound", fakeZone, ig, err)
}
}

// TestIngressCreateDeleteFinalizer asserts that `sync` will will not return an
// error for a good ingress config. It also tests garbage collection for
// Ingresses that need to be deleted, and keep the ones that don't, depending
Expand Down

0 comments on commit 8b677dd

Please sign in to comment.