From c5932366dbff4f9fd520527c8e2bbb3cef8e5f9b Mon Sep 17 00:00:00 2001 From: Manuel de Brito Fontes Date: Sat, 21 Apr 2018 18:40:02 -0300 Subject: [PATCH] Add test for store helper ListIngresses --- internal/ingress/controller/store/store.go | 3 + .../ingress/controller/store/store_test.go | 103 ++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/internal/ingress/controller/store/store.go b/internal/ingress/controller/store/store.go index ac0c24d93f..ac4cceab22 100644 --- a/internal/ingress/controller/store/store.go +++ b/internal/ingress/controller/store/store.go @@ -617,16 +617,19 @@ func (s k8sStore) ListIngresses() []*extensions.Ingress { if !class.IsValid(ing) { continue } + for ri, rule := range ing.Spec.Rules { if rule.HTTP == nil { continue } + for pi, path := range rule.HTTP.Paths { if path.Path == "" { ing.Spec.Rules[ri].HTTP.Paths[pi].Path = "/" } } } + ingresses = append(ingresses, ing) } diff --git a/internal/ingress/controller/store/store_test.go b/internal/ingress/controller/store/store_test.go index 37a13e508c..42e8047a28 100644 --- a/internal/ingress/controller/store/store_test.go +++ b/internal/ingress/controller/store/store_test.go @@ -740,3 +740,106 @@ func TestUpdateSecretIngressMap(t *testing.T) { } }) } + +func TestListIngresses(t *testing.T) { + s := newStore(t) + + ingEmptyClass := &v1beta1.Ingress{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-1", + Namespace: "testns", + }, + Spec: v1beta1.IngressSpec{ + Backend: &v1beta1.IngressBackend{ + ServiceName: "demo", + ServicePort: intstr.FromInt(80), + }, + Rules: []v1beta1.IngressRule{ + { + Host: "foo.bar", + }, + }, + }, + } + s.listers.Ingress.Add(ingEmptyClass) + + ingressToIgnore := &v1beta1.Ingress{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-2", + Namespace: "testns", + Annotations: map[string]string{ + "kubernetes.io/ingress.class": "something", + }, + }, + Spec: v1beta1.IngressSpec{ + Backend: &v1beta1.IngressBackend{ + ServiceName: "demo", + ServicePort: intstr.FromInt(80), + }, + }, + } + s.listers.Ingress.Add(ingressToIgnore) + + ingressWithoutPath := &v1beta1.Ingress{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-3", + Namespace: "testns", + }, + Spec: v1beta1.IngressSpec{ + Rules: []v1beta1.IngressRule{ + { + Host: "foo.bar", + IngressRuleValue: v1beta1.IngressRuleValue{ + HTTP: &v1beta1.HTTPIngressRuleValue{ + Paths: []v1beta1.HTTPIngressPath{ + { + Backend: v1beta1.IngressBackend{ + ServiceName: "demo", + ServicePort: intstr.FromInt(80), + }, + }, + }, + }, + }, + }, + }, + }, + } + s.listers.Ingress.Add(ingressWithoutPath) + + ingressWithNginxClass := &v1beta1.Ingress{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-4", + Namespace: "testns", + Annotations: map[string]string{ + "kubernetes.io/ingress.class": "nginx", + }, + }, + Spec: v1beta1.IngressSpec{ + Rules: []v1beta1.IngressRule{ + { + Host: "foo.bar", + IngressRuleValue: v1beta1.IngressRuleValue{ + HTTP: &v1beta1.HTTPIngressRuleValue{ + Paths: []v1beta1.HTTPIngressPath{ + { + Path: "/demo", + Backend: v1beta1.IngressBackend{ + ServiceName: "demo", + ServicePort: intstr.FromInt(80), + }, + }, + }, + }, + }, + }, + }, + }, + } + s.listers.Ingress.Add(ingressWithNginxClass) + + ingresses := s.ListIngresses() + if s := len(ingresses); s != 3 { + t.Errorf("Expected 3 Ingresses but got %v", s) + } +}