forked from kedacore/http-add-on
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_prefix_test.go
317 lines (290 loc) · 8.77 KB
/
path_prefix_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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//go:build e2e
// +build e2e
package path_prefix_test
import (
"fmt"
"sync"
"testing"
"github.com/stretchr/testify/assert"
"k8s.io/client-go/kubernetes"
. "github.com/kedacore/http-add-on/tests/helper"
)
const (
testName = "path-prefix-test"
)
var (
testNamespace = fmt.Sprintf("%s-ns", testName)
deploymentName = fmt.Sprintf("%s-deployment", testName)
deploymentName2 = fmt.Sprintf("%s-deployment-2", testName)
serviceName = fmt.Sprintf("%s-service", testName)
httpScaledObjectName = fmt.Sprintf("%s-http-so", testName)
host = testName
pathPrefix0 = "/123/456"
notPathPrefix0 = "/123/4567"
pathPrefix1 = "/qwe/rty"
notPathPrefix1 = "/qwe/rt"
pathPrefix2 = "/qwe/oty"
minReplicaCount = 0
maxReplicaCount = 1
)
type templateData struct {
TestNamespace string
DeploymentName string
ServiceName string
HTTPScaledObjectName string
Host string
PathPrefix string
PathPrefix0 string
PathPrefix1 string
PathPrefix2 string
MinReplicas int
MaxReplicas int
}
const (
serviceTemplate = `
apiVersion: v1
kind: Service
metadata:
name: {{.ServiceName}}
namespace: {{.TestNamespace}}
labels:
app: {{.DeploymentName}}
spec:
ports:
- port: 8080
targetPort: http
protocol: TCP
name: http
selector:
app: {{.DeploymentName}}
`
deploymentTemplate = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.DeploymentName}}
namespace: {{.TestNamespace}}
labels:
app: {{.DeploymentName}}
spec:
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}
template:
metadata:
labels:
app: {{.DeploymentName}}
spec:
containers:
- name: {{.DeploymentName}}
image: registry.k8s.io/e2e-test-images/agnhost:2.45
args:
- netexec
ports:
- name: http
containerPort: 8080
protocol: TCP
readinessProbe:
httpGet:
path: /
port: http
`
httpScaledObjectTemplate = `
kind: HTTPScaledObject
apiVersion: http.keda.sh/v1alpha1
metadata:
name: {{.HTTPScaledObjectName}}
namespace: {{.TestNamespace}}
spec:
hosts:
- {{.Host}}
pathPrefixes:
- {{.PathPrefix0}}
- {{.PathPrefix1}}
targetPendingRequests: 100
scaledownPeriod: 10
scaleTargetRef:
deployment: {{.DeploymentName}}
service: {{.ServiceName}}
port: 8080
replicas:
min: {{ .MinReplicas }}
max: {{ .MaxReplicas }}
`
serviceTemplate2 = `
apiVersion: v1
kind: Service
metadata:
name: {{.ServiceName}}-2
namespace: {{.TestNamespace}}
labels:
app: {{.DeploymentName}}-2
spec:
ports:
- port: 8080
targetPort: http
protocol: TCP
name: http
selector:
app: {{.DeploymentName}}-2
`
deploymentTemplate2 = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{.DeploymentName}}-2
namespace: {{.TestNamespace}}
labels:
app: {{.DeploymentName}}-2
spec:
replicas: 0
selector:
matchLabels:
app: {{.DeploymentName}}-2
template:
metadata:
labels:
app: {{.DeploymentName}}-2
spec:
containers:
- name: {{.DeploymentName}}
image: registry.k8s.io/e2e-test-images/agnhost:2.45
args:
- netexec
ports:
- name: http
containerPort: 8080
protocol: TCP
readinessProbe:
httpGet:
path: /
port: http
`
httpScaledObjectTemplate2 = `
kind: HTTPScaledObject
apiVersion: http.keda.sh/v1alpha1
metadata:
name: {{.HTTPScaledObjectName}}-2
namespace: {{.TestNamespace}}
spec:
hosts:
- {{.Host}}
pathPrefixes:
- {{.PathPrefix2}}
targetPendingRequests: 100
scaledownPeriod: 10
scaleTargetRef:
deployment: {{.DeploymentName}}-2
service: {{.ServiceName}}-2
port: 8080
replicas:
min: {{ .MinReplicas }}
max: {{ .MaxReplicas }}
`
loadJobTemplate = `
apiVersion: batch/v1
kind: Job
metadata:
name: generate-request
namespace: {{.TestNamespace}}
spec:
template:
spec:
containers:
- name: curl-client
image: curlimages/curl
imagePullPolicy: Always
command: ["curl", "-H", "Host: {{.Host}}", "keda-http-add-on-interceptor-proxy.keda:8080{{.PathPrefix}}"]
restartPolicy: Never
activeDeadlineSeconds: 600
backoffLimit: 5
`
)
func TestCheck(t *testing.T) {
// setup
t.Log("--- setting up ---")
// Create kubernetes resources
kc := GetKubernetesClient(t)
data, templates := getTemplateData()
CreateNamespace(t, kc, testNamespace)
t.Log("--- testing scale of workload1 with prefix0 ---")
testScale(t, kc, data, templates, deploymentName, deploymentName2, pathPrefix0)
t.Log("--- testing not scale ---")
testNotScale(t, kc, data, templates, notPathPrefix0)
t.Log("--- testing scale of workload1 with prefix1 ---")
testScale(t, kc, data, templates, deploymentName, deploymentName2, pathPrefix1)
t.Log("--- testing not scale ---")
testNotScale(t, kc, data, templates, notPathPrefix1)
t.Log("--- testing scale of workload2 with prefix2 ---")
testScale(t, kc, data, templates, deploymentName2, deploymentName, pathPrefix2)
// cleanup
DeleteNamespace(t, testNamespace)
}
func testScale(t *testing.T, kc *kubernetes.Clientset, data templateData, templates []Template, scaledWorkload, notScaledWorkload, pathPrefix string) {
// create resources
KubectlApplyMultipleWithTemplate(t, data, templates)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, scaledWorkload, testNamespace, minReplicaCount, 6, 10),
"replica count should be %d after 1 minute", minReplicaCount)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, notScaledWorkload, testNamespace, minReplicaCount, 6, 10),
"replica count should be %d after 1 minute", minReplicaCount)
data.PathPrefix = pathPrefix
// scale in
KubectlApplyWithTemplate(t, data, "loadJobTemplate", loadJobTemplate)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, scaledWorkload, testNamespace, maxReplicaCount, 6, 10),
"replica count should be %d after 1 minute", maxReplicaCount)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, notScaledWorkload, testNamespace, minReplicaCount, 6, 10),
"replica count should be %d after 1 minute", minReplicaCount)
// scale out
KubectlDeleteWithTemplate(t, data, "loadJobTemplate", loadJobTemplate)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, scaledWorkload, testNamespace, minReplicaCount, 12, 10),
"replica count should be %d after 2 minutes", minReplicaCount)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, notScaledWorkload, testNamespace, minReplicaCount, 6, 10),
"replica count should be %d after 1 minute", minReplicaCount)
// cleanup
KubectlDeleteMultipleWithTemplate(t, data, templates)
}
func testNotScale(t *testing.T, kc *kubernetes.Clientset, data templateData, templates []Template, pathPrefix string) {
// create resources
KubectlApplyMultipleWithTemplate(t, data, templates)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName, testNamespace, minReplicaCount, 6, 10),
"replica count should be %d after 1 minutes", minReplicaCount)
assert.True(t, WaitForDeploymentReplicaReadyCount(t, kc, deploymentName2, testNamespace, minReplicaCount, 6, 10),
"replica count should be %d after 1 minutes", minReplicaCount)
data.PathPrefix = pathPrefix
// not scale
KubectlApplyWithTemplate(t, data, "loadJobTemplate", loadJobTemplate)
assertion := func(workload string, waitGroup *sync.WaitGroup) {
AssertReplicaCountNotChangeDuringTimePeriod(t, kc, workload, testNamespace, minReplicaCount, 60)
waitGroup.Done()
}
wg := sync.WaitGroup{}
wg.Add(2)
go assertion(deploymentName, &wg)
go assertion(deploymentName2, &wg)
wg.Wait()
// cleanup
template := Template{Name: "loadJobTemplate", Config: loadJobTemplate}
KubectlDeleteMultipleWithTemplate(t, data, append(templates, template))
}
func getTemplateData() (templateData, []Template) {
return templateData{
TestNamespace: testNamespace,
DeploymentName: deploymentName,
ServiceName: serviceName,
HTTPScaledObjectName: httpScaledObjectName,
Host: host,
PathPrefix0: pathPrefix0,
PathPrefix1: pathPrefix1,
PathPrefix2: pathPrefix2,
MinReplicas: minReplicaCount,
MaxReplicas: maxReplicaCount,
}, []Template{
{Name: "deploymentTemplate", Config: deploymentTemplate},
{Name: "serviceNameTemplate", Config: serviceTemplate},
{Name: "httpScaledObjectTemplate", Config: httpScaledObjectTemplate},
{Name: "deploymentTemplate2", Config: deploymentTemplate2},
{Name: "serviceNameTemplate2", Config: serviceTemplate2},
{Name: "httpScaledObjectTemplate2", Config: httpScaledObjectTemplate2},
}
}