Merge pull request #3566 from diazjf/unit-locs

Add Unit Tests for getIngressInformation
This commit is contained in:
Kubernetes Prow Robot 2018-12-18 10:04:55 -08:00 committed by GitHub
commit 1b06cf7ed2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 92 additions and 0 deletions

View file

@ -26,6 +26,7 @@ import (
"net/url" "net/url"
"os" "os"
"os/exec" "os/exec"
"reflect"
"regexp" "regexp"
"strings" "strings"
text_template "text/template" text_template "text/template"
@ -762,6 +763,23 @@ type ingressInformation struct {
Annotations map[string]string 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 { func getIngressInformation(i, p interface{}) *ingressInformation {
ing, ok := i.(*ingress.Ingress) ing, ok := i.(*ingress.Ingress)
if !ok { if !ok {

View file

@ -29,6 +29,7 @@ import (
"fmt" "fmt"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
extensions "k8s.io/api/extensions/v1beta1"
"k8s.io/ingress-nginx/internal/file" "k8s.io/ingress-nginx/internal/file"
"k8s.io/ingress-nginx/internal/ingress" "k8s.io/ingress-nginx/internal/ingress"
"k8s.io/ingress-nginx/internal/ingress/annotations/authreq" "k8s.io/ingress-nginx/internal/ingress/annotations/authreq"
@ -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)
}
}