Skip to content

Commit

Permalink
Add Unit Tests for getIngressInformation
Browse files Browse the repository at this point in the history
Adds a unit test for the getIngressInformation
function.
  • Loading branch information
Fernando Diaz committed Dec 18, 2018
1 parent c6629ac commit 429110a
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/ingress/controller/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/url"
"os"
"os/exec"
"reflect"
"regexp"
"strings"
text_template "text/template"
Expand Down Expand Up @@ -762,6 +763,23 @@ type ingressInformation struct {
Annotations map[string]string
}

func (info *ingressInformation) Equal(other *ingressInformation) bool {
if info.Namespace != other.Namespace {
return false
}
if info.Rule != other.Rule {
return false
}
if info.Service != other.Service {
return false
}
if !reflect.DeepEqual(info.Annotations, other.Annotations) {
return false
}

return true
}

func getIngressInformation(i, p interface{}) *ingressInformation {
ing, ok := i.(*ingress.Ingress)
if !ok {
Expand Down
74 changes: 74 additions & 0 deletions internal/ingress/controller/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"fmt"

jsoniter "github.com/json-iterator/go"
extensions "k8s.io/api/extensions/v1beta1"
"k8s.io/ingress-nginx/internal/file"
"k8s.io/ingress-nginx/internal/ingress"
"k8s.io/ingress-nginx/internal/ingress/annotations/authreq"
Expand Down Expand Up @@ -843,3 +844,76 @@ func TestOpentracingPropagateContext(t *testing.T) {
}
}
}

func TestGetIngressInformation(t *testing.T) {
validIngress := &ingress.Ingress{}
invalidIngress := "wrongtype"
validPath := "/ok"
invalidPath := 10

info := getIngressInformation(invalidIngress, validPath)
expected := &ingressInformation{}
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}

info = getIngressInformation(validIngress, invalidPath)
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}

// Setup Ingress Resource
validIngress.Namespace = "default"
validIngress.Name = "validIng"
validIngress.Annotations = map[string]string{
"ingress.annotation": "ok",
}
validIngress.Spec.Backend = &extensions.IngressBackend{
ServiceName: "a-svc",
}

info = getIngressInformation(validIngress, validPath)
expected = &ingressInformation{
Namespace: "default",
Rule: "validIng",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
Service: "a-svc",
}
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}

validIngress.Spec.Backend = nil
validIngress.Spec.Rules = []extensions.IngressRule{
{
IngressRuleValue: extensions.IngressRuleValue{
HTTP: &extensions.HTTPIngressRuleValue{
Paths: []extensions.HTTPIngressPath{
{
Path: "/ok",
Backend: extensions.IngressBackend{
ServiceName: "b-svc",
},
},
},
},
},
},
{},
}

info = getIngressInformation(validIngress, validPath)
expected = &ingressInformation{
Namespace: "default",
Rule: "validIng",
Annotations: map[string]string{
"ingress.annotation": "ok",
},
Service: "b-svc",
}
if !info.Equal(expected) {
t.Errorf("Expected %v, but got %v", expected, info)
}
}

0 comments on commit 429110a

Please sign in to comment.