-
Notifications
You must be signed in to change notification settings - Fork 69
/
custom_route_test.go
166 lines (149 loc) · 5.5 KB
/
custom_route_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package kourier
import (
"context"
"fmt"
"net/http"
"strings"
"testing"
"github.com/openshift-knative/serverless-operator/serving/ingress/pkg/reconciler/ingress/resources"
"github.com/openshift-knative/serverless-operator/test"
"github.com/openshift-knative/serverless-operator/test/servinge2e"
routev1 "github.com/openshift/api/route/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
duckv1 "knative.dev/pkg/apis/duck/v1"
pkgTest "knative.dev/pkg/test"
"knative.dev/pkg/test/spoof"
servingv1beta1 "knative.dev/serving/pkg/apis/serving/v1beta1"
)
const (
serviceName = "custom-route-test"
domainMappingName = "mydomain-test.example.com"
)
// TestCustomOpenShiftRoute verifies user-defined OpenShift Route could work.
// 1. Create Kservice with disableRoute annotation.
// 2. Verify operator did not create OpenShift Route.
// 3. Create the OpenShift Route manually.
// 4. Verify the access.
// 5. Create DomainMapping with disableRoute annotation.
// 6. Verify operator did not create OpenShift Route for domainmapping.
// 7. Create the OpenShift Route manually.
// 8. Verify the access for customdomain.
func TestCustomOpenShiftRoute(t *testing.T) {
caCtx := test.SetupClusterAdmin(t)
test.CleanupOnInterrupt(t, func() { test.CleanupAll(t, caCtx) })
defer test.CleanupAll(t, caCtx)
// Create Kservice with disable Annotation.
ksvc := test.Service(serviceName, test.Namespace, pkgTest.ImagePath(test.HelloworldGoImg), nil, nil)
ksvc.ObjectMeta.Annotations = map[string]string{resources.DisableRouteAnnotation: "true"}
ksvc = test.WithServiceReadyOrFail(caCtx, ksvc)
// Verify that operator did not create OpenShift route.
routes, err := caCtx.Clients.Route.Routes(test.IngressNamespace).List(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", resources.OpenShiftIngressLabelKey, ksvc.Name),
})
if err != nil {
t.Fatalf("Failed to list routes: %v", err)
}
if len(routes.Items) != 0 {
t.Fatalf("Unexpected routes found: %v", routes)
}
// Create OpenShift Route manually
route := &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: "myroute",
Namespace: test.IngressNamespace,
},
Spec: routev1.RouteSpec{
Host: ksvc.Status.URL.Host,
Port: &routev1.RoutePort{
TargetPort: intstr.FromString(resources.HTTPPort),
},
To: routev1.RouteTargetReference{
Kind: "Service",
Name: "kourier",
},
TLS: &routev1.TLSConfig{
Termination: routev1.TLSTerminationEdge,
InsecureEdgeTerminationPolicy: routev1.InsecureEdgeTerminationPolicyAllow,
},
},
}
route, err = caCtx.Clients.Route.Routes(test.IngressNamespace).Create(context.Background(), route, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating OpenShift Route: %v", err)
}
caCtx.AddToCleanup(func() error {
t.Logf("Cleaning up OpenShift Route %s", route.Name)
return caCtx.Clients.Route.Routes(route.Namespace).Delete(context.Background(), route.Name, metav1.DeleteOptions{})
})
servinge2e.WaitForRouteServingText(t, caCtx, ksvc.Status.URL.URL(), helloworldText)
// Create DomainMapping with disable Annotation.
dm := &servingv1beta1.DomainMapping{
ObjectMeta: metav1.ObjectMeta{
Name: domainMappingName,
Namespace: test.Namespace,
Annotations: map[string]string{resources.DisableRouteAnnotation: "true"},
},
Spec: servingv1beta1.DomainMappingSpec{
Ref: duckv1.KReference{
Kind: "Service",
Name: serviceName,
APIVersion: "serving.knative.dev/v1",
},
},
}
dm = withDomainMappingReadyOrFail(caCtx, dm)
// Verify that operator did not create OpenShift route.
routes, err = caCtx.Clients.Route.Routes(test.IngressNamespace).List(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("%s=%s", resources.OpenShiftIngressLabelKey, dm.Name),
})
if err != nil {
t.Fatalf("Failed to list routes: %v", err)
}
if len(routes.Items) != 0 {
t.Fatalf("Unexpected routes found: %v", routes)
}
// Create OpenShift Route manually
route = &routev1.Route{
ObjectMeta: metav1.ObjectMeta{
Name: "myroute-for-dm",
Namespace: test.IngressNamespace,
},
Spec: routev1.RouteSpec{
Host: dm.Status.URL.Host,
Port: &routev1.RoutePort{
TargetPort: intstr.FromString(resources.HTTPPort),
},
To: routev1.RouteTargetReference{
Kind: "Service",
Name: "kourier",
},
},
}
route, err = caCtx.Clients.Route.Routes(test.IngressNamespace).Create(context.Background(), route, metav1.CreateOptions{})
if err != nil {
t.Fatalf("Error creating OpenShift Route: %v", err)
}
caCtx.AddToCleanup(func() error {
t.Logf("Cleaning up OpenShift Route %s", route.Name)
return caCtx.Clients.Route.Routes(route.Namespace).Delete(context.Background(), route.Name, metav1.DeleteOptions{})
})
routerIP := lookupOpenShiftRouterIP(caCtx)
sc, err := newSpoofClientWithTLS(caCtx, domainMappingName, routerIP.String(), nil)
if err != nil {
t.Fatalf("Error creating a Spoofing Client: %v", err)
}
req, err := http.NewRequest(http.MethodGet, "http://"+domainMappingName, nil)
if err != nil {
t.Fatalf("Error creating an HTTP GET request: %v", err)
}
// Retry until OpenShift Route becomes ready.
resp, err := sc.Poll(req, spoof.IsStatusOK)
if err != nil {
t.Fatalf("Error polling custom domain: %v", err)
}
const expectedResponse = "Hello World!"
if resp.StatusCode != 200 || strings.TrimSpace(string(resp.Body)) != expectedResponse {
t.Fatalf("Expecting a HTTP 200 response with %q, got %d: %s", expectedResponse, resp.StatusCode, string(resp.Body))
}
}