-
Notifications
You must be signed in to change notification settings - Fork 101
/
ingress-wrapper_test.go
137 lines (127 loc) · 3.77 KB
/
ingress-wrapper_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package wrappers
import (
"testing"
"github.com/stakater/IngressMonitorController/pkg/kube"
"github.com/stakater/IngressMonitorController/pkg/util"
"k8s.io/api/extensions/v1beta1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
const (
testUrl = "testurl.stackator.com"
)
func createIngressObjectWithPath(ingressName string, namespace string, url string, path string) *v1beta1.Ingress {
ingress := util.CreateIngressObject(ingressName, namespace, url)
ingress.Spec.Rules[0].IngressRuleValue = v1beta1.IngressRuleValue{
HTTP: &v1beta1.HTTPIngressRuleValue{
Paths: []v1beta1.HTTPIngressPath{
v1beta1.HTTPIngressPath{
Path: path,
},
},
},
}
return ingress
}
func createIngressObjectWithAnnotations(ingressName string, namespace string, url string, annotations map[string]string) *v1beta1.Ingress {
ingress := util.CreateIngressObject(ingressName, namespace, url)
ingress.ObjectMeta.SetAnnotations(annotations)
return ingress
}
func TestIngressWrapper_getURL(t *testing.T) {
type fields struct {
ingress *v1beta1.Ingress
namespace string
kubeClient kubernetes.Interface
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "TestGetUrlWithEmptyPath",
fields: fields{
ingress: createIngressObjectWithPath("testIngress", "test", testUrl, "/"),
namespace: "test",
kubeClient: getTestKubeClient(),
},
want: "http://testurl.stackator.com:80/",
},
{
name: "TestGetUrlWithHelloPath",
fields: fields{
ingress: createIngressObjectWithPath("testIngress", "test", testUrl, "/hello"),
namespace: "test",
kubeClient: getTestKubeClient(),
},
want: "http://testurl.stackator.com:80/hello",
},
{
name: "TestGetUrlWithNoPath",
fields: fields{
ingress: util.CreateIngressObject("testIngress", "test", testUrl),
namespace: "test",
kubeClient: getTestKubeClient(),
},
want: "http://testurl.stackator.com:80/",
},
{
name: "TestGetUrlWithForceHTTPSAnnotation",
fields: fields{
ingress: createIngressObjectWithAnnotations("testIngress", "test", testUrl, map[string]string{"monitor.stakater.com/forceHttps": "true"}),
namespace: "test",
kubeClient: getTestKubeClient(),
},
want: "https://testurl.stackator.com:80/",
},
{
name: "TestGetUrlWithForceHTTPSAnnotationOff",
fields: fields{
ingress: createIngressObjectWithAnnotations("testIngress", "test", testUrl, map[string]string{"monitor.stakater.com/forceHttps": "false"}),
namespace: "test",
kubeClient: getTestKubeClient(),
},
want: "http://testurl.stackator.com:80/",
},
{
name: "TestGetUrlWithOverridePathAnnotation",
fields: fields{
ingress: createIngressObjectWithAnnotations("testIngress", "test", testUrl, map[string]string{"monitor.stakater.com/overridePath": "/overriden-path"}),
namespace: "test",
kubeClient: getTestKubeClient(),
},
want: "http://testurl.stackator.com:80/overriden-path",
}, {
name: "TestGetUrlWithWildCardInPath",
fields: fields{
ingress: createIngressObjectWithPath("testIngress", "test", testUrl, "/*"),
namespace: "test",
kubeClient: getTestKubeClient(),
},
want: "http://testurl.stackator.com:80/",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
iw := &IngressWrapper{
Ingress: tt.fields.ingress,
Namespace: tt.fields.namespace,
KubeClient: tt.fields.kubeClient,
}
if got := iw.GetURL(); got != tt.want {
t.Errorf("IngressWrapper.getURL() = %v, want %v", got, tt.want)
}
})
}
}
func getTestKubeClient() kubernetes.Interface {
var kubeClient kubernetes.Interface
_, err := rest.InClusterConfig()
if err != nil {
kubeClient = kube.GetClientOutOfCluster()
} else {
kubeClient = kube.GetClient()
}
return kubeClient
}