diff --git a/nginx-controller/controller/controller_test.go b/nginx-controller/controller/controller_test.go new file mode 100644 index 0000000000..6bd853811f --- /dev/null +++ b/nginx-controller/controller/controller_test.go @@ -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) + } + } + +}