forked from knative/serving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_to_service_test.go
248 lines (209 loc) · 7.8 KB
/
service_to_service_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//go:build e2e
// +build e2e
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package e2e
import (
"net/url"
"strconv"
"strings"
"testing"
netapi "knative.dev/networking/pkg/apis/networking"
"knative.dev/pkg/test/logstream"
"knative.dev/serving/pkg/apis/autoscaling"
"knative.dev/serving/pkg/apis/serving"
rtesting "knative.dev/serving/pkg/testing/v1"
"knative.dev/serving/test"
v1test "knative.dev/serving/test/v1"
)
// testCases for table-driven testing.
var testCases = []struct {
// name of the test case, which will be inserted in names of routes, configurations, etc.
// Use a short name here to avoid hitting the 63-character limit in names
// (e.g., "service-to-service-call-svc-cluster-local-uagkdshh-frkml-service" is too long.)
name string
// suffix to be trimmed from TARGET_HOST.
suffix string
}{
{"fqdn", ""},
{"short", ".cluster.local"},
{"shortest", ".svc.cluster.local"},
}
// testcases for table-driven testing.
var testInjection = []struct {
name string
// injectA indicates whether istio sidecar injection is enabled for httpproxy service
// injectB indicates whether istio sidecar injection is enabled for helloworld service
injectA bool
injectB bool
}{
{"both-disabled", false, false},
{"a-disabled", false, true},
{"b-disabled", true, false},
{"both-enabled", true, true},
}
// In this test, we set up two apps: helloworld and httpproxy.
// helloworld is a simple app that displays a plaintext string.
// httpproxy is a proxy that redirects request to internal service of helloworld app
// with FQDN {route}.{namespace}.svc.cluster.local, or {route}.{namespace}.svc, or
// {route}.{namespace}.
// The expected result is that the request sent to httpproxy app is successfully redirected
// to helloworld app.
func TestServiceToServiceCall(t *testing.T) {
t.Parallel()
clients := Setup(t)
t.Log("Creating a Service for the helloworld test app.")
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: test.HelloWorld,
}
test.EnsureTearDown(t, clients, &names)
withInternalVisibility := rtesting.WithServiceLabel(
netapi.VisibilityLabelKey, serving.VisibilityClusterLocal)
resources, err := v1test.CreateServiceReady(t, clients, &names, withInternalVisibility)
if err != nil {
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err)
}
if resources.Route.Status.URL.Host == "" {
t.Fatalf("Route is missing .Status.URL: %#v", resources.Route.Status)
}
if resources.Route.Status.Address == nil {
t.Fatalf("Route is missing .Status.Address: %#v", resources.Route.Status)
}
// Check that the target Route's Domain matches its cluster local address.
if want, got := resources.Route.Status.Address.URL, resources.Route.Status.URL; got.String() != want.String() {
t.Errorf("Route.Status.URL.Host = %v, want %v", got, want)
}
t.Logf("helloworld internal domain is %s.", resources.Route.Status.URL.Host)
// if cluster-local-domain-tls is enabled, this will return the CA used to sign the certificates.
// TestProxyToHelloworld will use this CA to verify the https connection
secret, err := GetCASecret(clients)
if err != nil {
t.Fatal(err.Error())
}
// helloworld app and its route are ready. Running the test cases now.
for _, tc := range testCases {
helloworldURL := &url.URL{
Scheme: resources.Route.Status.URL.Scheme,
Host: strings.TrimSuffix(resources.Route.Status.URL.Host, tc.suffix),
Path: resources.Route.Status.URL.Path,
}
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if !test.ServingFlags.DisableLogStream {
cancel := logstream.Start(t)
defer cancel()
}
TestProxyToHelloworld(t, clients, helloworldURL, true, false, secret)
})
}
}
func testSvcToSvcCallViaActivator(t *testing.T, clients *test.Clients, injectA bool, injectB bool) {
t.Log("Creating helloworld Service")
testNames := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: test.HelloWorld,
}
withInternalVisibility := rtesting.WithServiceLabel(
netapi.VisibilityLabelKey, serving.VisibilityClusterLocal)
withIstioSidecarInject := rtesting.WithServiceLabel("sidecar.istio.io/inject", strconv.FormatBool(injectB))
test.EnsureTearDown(t, clients, &testNames)
resources, err := v1test.CreateServiceReady(t, clients, &testNames,
rtesting.WithConfigAnnotations(map[string]string{
autoscaling.TargetBurstCapacityKey: "-1",
}), withInternalVisibility, withIstioSidecarInject)
if err != nil {
t.Fatal("Failed to create a service:", err)
}
// Wait for the activator endpoints to equalize.
if err := waitForActivatorEndpoints(&TestContext{
t: t,
resources: resources,
clients: clients,
}); err != nil {
t.Fatal("Never got Activator endpoints in the service:", err)
}
// if cluster-local-domain-tls is enabled, this will return the CA used to sign the certificates.
// TestProxyToHelloworld will use this CA to verify the https connection
secret, err := GetCASecret(clients)
if err != nil {
t.Fatal(err.Error())
}
// Send request to helloworld app via httpproxy service
TestProxyToHelloworld(t, clients, resources.Route.Status.URL.URL(), injectA, false, secret)
}
// Same test as TestServiceToServiceCall but before sending requests
// we're waiting for target app to be scaled to zero
func TestSvcToSvcViaActivator(t *testing.T) {
t.Parallel()
clients := Setup(t)
for _, tc := range testInjection {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if !test.ServingFlags.DisableLogStream {
cancel := logstream.Start(t)
defer cancel()
}
testSvcToSvcCallViaActivator(t, clients, tc.injectA, tc.injectB)
})
}
}
// This test is similar to TestServiceToServiceCall, but creates an external accessible helloworld service instead.
// It verifies that the helloworld service is accessible internally from both internal domain and external domain.
// But it's only accessible from external via the external domain
func TestCallToPublicService(t *testing.T) {
t.Parallel()
clients := Setup(t)
t.Log("Creating a Service for the helloworld test app.")
names := test.ResourceNames{
Service: test.ObjectNameForTest(t),
Image: test.HelloWorld,
}
test.EnsureTearDown(t, clients, &names)
resources, err := v1test.CreateServiceReady(t, clients, &names)
if err != nil {
t.Fatalf("Failed to create initial Service: %v: %v", names.Service, err)
}
if resources.Route.Status.URL.Host == "" {
t.Fatalf("Route is missing .Status.URL: %#v", resources.Route.Status)
}
if resources.Route.Status.Address == nil {
t.Fatalf("Route is missing .Status.Address: %#v", resources.Route.Status)
}
gatewayTestCases := []struct {
name string
url *url.URL
accessibleExternally bool
}{
{"local_address", resources.Route.Status.Address.URL.URL(), false},
{"external_address", resources.Route.Status.URL.URL(), true},
}
// if cluster-local-domain-tls is enabled, this will return the CA used to sign the certificates.
// TestProxyToHelloworld will use this CA to verify the https connection
secret, err := GetCASecret(clients)
if err != nil {
t.Fatal(err.Error())
}
for _, tc := range gatewayTestCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
if !test.ServingFlags.DisableLogStream {
cancel := logstream.Start(t)
defer cancel()
}
TestProxyToHelloworld(t, clients, tc.url, false, tc.accessibleExternally, secret)
})
}
}