@@ -4,61 +4,56 @@ import (
44 "testing"
55
66 "github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
7+ "github.com/google/go-cmp/cmp"
78 "github.com/stretchr/testify/assert"
89)
910
1011func TestGetRouteForEndpointAnnotations (t * testing.T ) {
1112 tests := []struct {
1213 name string
1314
14- routingSuffix string
15- endpoint v1alpha1.Endpoint
16- meta DevWorkspaceMetadata
17- annotations map [string ]string
15+ annotations map [string ]string
1816
19- expectedAnnotationsKeys [ ]string
17+ expectedAnnotations map [ string ]string
2018 }{
2119 {
22- name : "nil " ,
20+ name : "Gets default OpenShift route annotation when annotations aren't defined " ,
2321
2422 annotations : nil ,
2523
26- expectedAnnotationsKeys : [ ]string {
27- "controller.devfile.io/endpoint_name" ,
28- "haproxy.router.openshift.io/rewrite-target" ,
24+ expectedAnnotations : map [ string ]string {
25+ "controller.devfile.io/endpoint_name" : "Endpoint" ,
26+ "haproxy.router.openshift.io/rewrite-target" : "/" ,
2927 },
3028 },
3129 {
32- name : "empty" ,
30+ name : "Gets default OpenShift route annotation when annotations are empty" ,
3331
3432 annotations : map [string ]string {},
3533
36- expectedAnnotationsKeys : [ ]string {
37- "controller.devfile.io/endpoint_name" ,
38- "haproxy.router.openshift.io/rewrite-target" ,
34+ expectedAnnotations : map [ string ]string {
35+ "controller.devfile.io/endpoint_name" : "Endpoint" ,
36+ "haproxy.router.openshift.io/rewrite-target" : "/" ,
3937 },
4038 },
4139 {
42- name : "defined" ,
40+ name : "Gets default OpenShift route annotation when annotations are defined" ,
4341
4442 annotations : map [string ]string {
4543 "example.com/extra" : "val" ,
4644 },
4745
48- expectedAnnotationsKeys : []string {
49- "controller.devfile.io/endpoint_name" ,
50- "example.com/extra" },
46+ expectedAnnotations : map [string ]string {
47+ "controller.devfile.io/endpoint_name" : "Endpoint" ,
48+ "example.com/extra" : "val" ,
49+ },
5150 },
5251 }
5352
5453 for _ , tt := range tests {
5554 t .Run (tt .name , func (t * testing.T ) {
5655 route := getRouteForEndpoint ("routingSuffix" , v1alpha1.Endpoint {Name : "Endpoint" }, DevWorkspaceMetadata {DevWorkspaceId : "WorkspaceTest" }, tt .annotations )
57- for _ , expected := range tt .expectedAnnotationsKeys {
58- _ , ok := route .Annotations [expected ]
59- assert .True (t , ok , "Key %s does not exist" , expected )
60- assert .Equal (t , len (tt .expectedAnnotationsKeys ), len (route .Annotations ))
61- }
56+ assert .Equal (t , tt .expectedAnnotations , route .Annotations , "Annotations should match: Diff: %s" , cmp .Diff (tt .expectedAnnotations , route .Annotations ))
6257 })
6358 }
6459}
@@ -67,59 +62,52 @@ func TestGetIngressForEndpointAnnotations(t *testing.T) {
6762 tests := []struct {
6863 name string
6964
70- routingSuffix string
71- endpoint v1alpha1.Endpoint
72- meta DevWorkspaceMetadata
73- annotations map [string ]string
65+ annotations map [string ]string
7466
75- expectedAnnotationsKeys [ ]string
67+ expectedAnnotations map [ string ]string
7668 }{
7769 {
78- name : "nil " ,
70+ name : "Gets default Kubernetes ingress annotation when annotations aren't defined " ,
7971
8072 annotations : nil ,
8173
82- expectedAnnotationsKeys : [ ]string {
83- "controller.devfile.io/endpoint_name" ,
84- "kubernetes.io/ingress.class" ,
85- "nginx.ingress.kubernetes.io/rewrite-target" ,
86- "nginx.ingress.kubernetes.io/ssl-redirect" ,
74+ expectedAnnotations : map [ string ]string {
75+ "controller.devfile.io/endpoint_name" : "Endpoint" ,
76+ "kubernetes.io/ingress.class" : "nginx" ,
77+ "nginx.ingress.kubernetes.io/rewrite-target" : "/" ,
78+ "nginx.ingress.kubernetes.io/ssl-redirect" : "false" ,
8779 },
8880 },
8981 {
90- name : "empty" ,
82+ name : "Gets default Kubernetes ingress annotation when annotations are empty" ,
9183
9284 annotations : map [string ]string {},
9385
94- expectedAnnotationsKeys : [ ]string {
95- "controller.devfile.io/endpoint_name" ,
96- "kubernetes.io/ingress.class" ,
97- "nginx.ingress.kubernetes.io/rewrite-target" ,
98- "nginx.ingress.kubernetes.io/ssl-redirect" ,
86+ expectedAnnotations : map [ string ]string {
87+ "controller.devfile.io/endpoint_name" : "Endpoint" ,
88+ "kubernetes.io/ingress.class" : "nginx" ,
89+ "nginx.ingress.kubernetes.io/rewrite-target" : "/" ,
90+ "nginx.ingress.kubernetes.io/ssl-redirect" : "false" ,
9991 },
10092 },
10193 {
102- name : "defined" ,
94+ name : "Gets default Kubernetes ingress annotation when annotations are defined" ,
10395
10496 annotations : map [string ]string {
10597 "kubernetes.io/ingress.class" : "traefik" ,
10698 },
10799
108- expectedAnnotationsKeys : [ ]string {
109- "controller.devfile.io/endpoint_name" ,
110- "kubernetes.io/ingress.class" ,
100+ expectedAnnotations : map [ string ]string {
101+ "controller.devfile.io/endpoint_name" : "Endpoint" ,
102+ "kubernetes.io/ingress.class" : "traefik" ,
111103 },
112104 },
113105 }
114106
115107 for _ , tt := range tests {
116108 t .Run (tt .name , func (t * testing.T ) {
117109 ingress := getIngressForEndpoint ("routingSuffix" , v1alpha1.Endpoint {Name : "Endpoint" }, DevWorkspaceMetadata {DevWorkspaceId : "WorkspaceTest" }, tt .annotations )
118- for _ , expected := range tt .expectedAnnotationsKeys {
119- _ , ok := ingress .Annotations [expected ]
120- assert .True (t , ok , "Key %s does not exist" , expected )
121- assert .Equal (t , len (tt .expectedAnnotationsKeys ), len (ingress .Annotations ))
122- }
110+ assert .Equal (t , tt .expectedAnnotations , ingress .Annotations , "Annotations should match: Diff: %s" , cmp .Diff (tt .expectedAnnotations , ingress .Annotations ))
123111 })
124112 }
125113}
0 commit comments