github.com/stakater/IngressMonitorController@v1.0.103/pkg/kube/wrappers/routeWrapper_test.go (about) 1 package wrappers 2 3 import ( 4 "testing" 5 6 routev1 "github.com/openshift/api/route/v1" 7 "github.com/stakater/IngressMonitorController/pkg/util" 8 "k8s.io/client-go/kubernetes" 9 ) 10 11 const ( 12 routeTestUrl = "testurl.stackator.com/" 13 ) 14 15 func createRouteObjectWithPath(routeName string, namespace string, url string, path string) *routev1.Route { 16 route := util.CreateRouteObject(routeName, namespace, url) 17 route.Spec.Path = path 18 return route 19 } 20 21 func createRouteObjectWithAnnotations(routeName string, namespace string, url string, annotations map[string]string) *routev1.Route { 22 route := util.CreateRouteObject(routeName, namespace, url) 23 route.ObjectMeta.SetAnnotations(annotations) 24 25 return route 26 } 27 28 func TestRouteWrapper_getURL(t *testing.T) { 29 type fields struct { 30 route *routev1.Route 31 namespace string 32 kubeClient kubernetes.Interface 33 } 34 tests := []struct { 35 name string 36 fields fields 37 want string 38 }{ 39 { 40 name: "TestGetUrlWithEmptyPath", 41 fields: fields{ 42 route: createRouteObjectWithPath("testRoute", "test", routeTestUrl, "/"), 43 namespace: "test", 44 kubeClient: getTestKubeClient(), 45 }, 46 want: "http://testurl.stackator.com/", 47 }, 48 { 49 name: "TestGetUrlWithHelloPath", 50 fields: fields{ 51 route: createRouteObjectWithPath("testRoute", "test", routeTestUrl, "/hello"), 52 namespace: "test", 53 kubeClient: getTestKubeClient(), 54 }, 55 want: "http://testurl.stackator.com/hello", 56 }, 57 { 58 name: "TestGetUrlWithNoPath", 59 fields: fields{ 60 route: util.CreateRouteObject("testRoute", "test", routeTestUrl), 61 namespace: "test", 62 kubeClient: getTestKubeClient(), 63 }, 64 want: "http://testurl.stackator.com/", 65 }, 66 { 67 name: "TestGetUrlWithForceHTTPSAnnotation", 68 fields: fields{ 69 route: createRouteObjectWithAnnotations("testRoute", "test", routeTestUrl, map[string]string{"monitor.stakater.com/forceHttps": "true"}), 70 namespace: "test", 71 kubeClient: getTestKubeClient(), 72 }, 73 want: "https://testurl.stackator.com/", 74 }, 75 { 76 name: "TestGetUrlWithForceHTTPSAnnotationOff", 77 fields: fields{ 78 route: createRouteObjectWithAnnotations("testRoute", "test", routeTestUrl, map[string]string{"monitor.stakater.com/forceHttps": "false"}), 79 namespace: "test", 80 kubeClient: getTestKubeClient(), 81 }, 82 want: "http://testurl.stackator.com/", 83 }, 84 { 85 name: "TestGetUrlWithOverridePathAnnotation", 86 fields: fields{ 87 route: createRouteObjectWithAnnotations("testRoute", "test", routeTestUrl, map[string]string{"monitor.stakater.com/overridePath": "/overriden-path"}), 88 namespace: "test", 89 kubeClient: getTestKubeClient(), 90 }, 91 want: "http://testurl.stackator.com/overriden-path", 92 }, 93 } 94 for _, tt := range tests { 95 t.Run(tt.name, func(t *testing.T) { 96 iw := &RouteWrapper{ 97 Route: tt.fields.route, 98 Namespace: tt.fields.namespace, 99 KubeClient: tt.fields.kubeClient, 100 } 101 if got := iw.GetURL(); got != tt.want { 102 t.Errorf("IngressWrapper.getURL() = %v, want %v", got, tt.want) 103 } 104 }) 105 } 106 }