Skip to content

Commit

Permalink
Add unit test for ingress-class handling (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
pleshakov authored Feb 9, 2018
1 parent a3a7ca4 commit d4e782b
Showing 1 changed file with 146 additions and 0 deletions.
146 changes: 146 additions & 0 deletions nginx-controller/controller/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package controller

import (
extensions "k8s.io/api/extensions/v1beta1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

func TestIsNginxIngress(t *testing.T) {
ingressClass := "ing-ctrl"

var testsWithoutIngressClassOnly = []struct {
lbc *LoadBalancerController
ing *extensions.Ingress
expected bool
}{
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: false,
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: ""},
},
},
true,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: false,
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: "gce"},
},
},
false,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: false,
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: ingressClass},
},
},
true,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: false,
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{},
},
},
true,
},
}

var testsWithIngressClassOnly = []struct {
lbc *LoadBalancerController
ing *extensions.Ingress
expected bool
}{
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: true,
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: ""},
},
},
false,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: true,
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: "gce"},
},
},
false,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: true,
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{ingressClassKey: ingressClass},
},
},
true,
},
{
&LoadBalancerController{
ingressClass: ingressClass,
useIngressClassOnly: true,
},
&extensions.Ingress{
ObjectMeta: meta_v1.ObjectMeta{
Annotations: map[string]string{},
},
},
false,
},

}


for _, test := range testsWithoutIngressClassOnly {
if result := test.lbc.isNginxIngress(test.ing); result != test.expected {
classAnnotation := "N/A"
if class, exists := test.ing.Annotations[ingressClassKey]; exists {
classAnnotation = class
}
t.Errorf("lbc.isNginxIngress(ing), lbc.ingressClass=%v, lbc.useIngressClassOnly=%v, ing.Annotations['%v']=%v; got %v, expected %v",
test.lbc.ingressClass, test.lbc.useIngressClassOnly, ingressClassKey, classAnnotation, result, test.expected)
}
}

for _, test := range testsWithIngressClassOnly {
if result := test.lbc.isNginxIngress(test.ing); result != test.expected {
classAnnotation := "N/A"
if class, exists := test.ing.Annotations[ingressClassKey]; exists {
classAnnotation = class
}
t.Errorf("lbc.isNginxIngress(ing), lbc.ingressClass=%v, lbc.useIngressClassOnly=%v, ing.Annotations['%v']=%v; got %v, expected %v",
test.lbc.ingressClass, test.lbc.useIngressClassOnly, ingressClassKey, classAnnotation, result, test.expected)
}
}

}

0 comments on commit d4e782b

Please sign in to comment.