forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
azure-queue.test.ts
151 lines (139 loc) · 4.28 KB
/
azure-queue.test.ts
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
import * as async from 'async'
import * as azure from 'azure-storage'
import * as fs from 'fs'
import * as sh from 'shelljs'
import * as tmp from 'tmp'
import test from 'ava'
const defaultNamespace = 'azure-queue-test'
const connectionString = process.env['TEST_STORAGE_CONNECTION_STRING']
test.before(t => {
if (!connectionString) {
t.fail('TEST_STORAGE_CONNECTION_STRING environment variable is required for queue tests')
}
sh.config.silent = true
const base64ConStr = Buffer.from(connectionString).toString('base64')
const tmpFile = tmp.fileSync()
fs.writeFileSync(tmpFile.name, deployYaml.replace('{{CONNECTION_STRING_BASE64}}', base64ConStr))
sh.exec(`kubectl create namespace ${defaultNamespace}`)
t.is(
0,
sh.exec(`kubectl apply -f ${tmpFile.name} --namespace ${defaultNamespace}`).code,
'creating a deployment should work.'
)
})
test.serial('Deployment should have 0 replicas on start', t => {
const replicaCount = sh.exec(
`kubectl get deployment.apps/test-deployment --namespace ${defaultNamespace} -o jsonpath="{.spec.replicas}"`
).stdout
t.is(replicaCount, '0', 'replica count should start out as 0')
})
test.serial.cb(
'Deployment should scale to 4 with 10,000 messages on the queue then back to 0',
t => {
// add 10,000 messages
const queueSvc = azure.createQueueService(connectionString)
queueSvc.messageEncoder = new azure.QueueMessageEncoder.TextBase64QueueMessageEncoder()
queueSvc.createQueueIfNotExists('queue-name', err => {
t.falsy(err, 'unable to create queue')
async.mapLimit(
Array(10000).keys(),
200,
(n, cb) => queueSvc.createMessage('queue-name', `test ${n}`, cb),
() => {
let replicaCount = '0'
for (let i = 0; i < 30 && replicaCount !== '4'; i++) {
replicaCount = sh.exec(
`kubectl get deployment.apps/test-deployment --namespace ${defaultNamespace} -o jsonpath="{.spec.replicas}"`
).stdout
if (replicaCount !== '4') {
sh.exec('sleep 1s')
}
}
t.is('4', replicaCount, 'Replica count should be 4 after 30 seconds')
for (let i = 0; i < 50 && replicaCount !== '0'; i++) {
replicaCount = sh.exec(
`kubectl get deployment.apps/test-deployment --namespace ${defaultNamespace} -o jsonpath="{.spec.replicas}"`
).stdout
if (replicaCount !== '0') {
sh.exec('sleep 5s')
}
}
t.is('0', replicaCount, 'Replica count should be 0 after 3 minutes')
t.end()
}
)
})
}
)
test.after.always.cb('clean up azure-queue deployment', t => {
const resources = [
'scaledobject.keda.sh/test-scaledobject',
'secret/test-secrets',
'deployment.apps/test-deployment',
]
for (const resource of resources) {
sh.exec(`kubectl delete ${resource} --namespace ${defaultNamespace}`)
}
sh.exec(`kubectl delete namespace ${defaultNamespace}`)
// delete test queue
const queueSvc = azure.createQueueService(connectionString)
queueSvc.deleteQueueIfExists('queue-name', err => {
t.falsy(err, 'should delete test queue successfully')
t.end()
})
})
const deployYaml = `apiVersion: v1
kind: Secret
metadata:
name: test-secrets
labels:
data:
AzureWebJobsStorage: {{CONNECTION_STRING_BASE64}}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: test-deployment
labels:
app: test-deployment
spec:
replicas: 0
selector:
matchLabels:
app: test-deployment
template:
metadata:
name:
namespace:
labels:
app: test-deployment
spec:
containers:
- name: test-deployment
image: docker.io/kedacore/tests-azure-queue:824031e
resources:
ports:
env:
- name: FUNCTIONS_WORKER_RUNTIME
value: node
- name: AzureWebJobsStorage
valueFrom:
secretKeyRef:
name: test-secrets
key: AzureWebJobsStorage
---
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: test-scaledobject
spec:
scaleTargetRef:
name: test-deployment
pollingInterval: 5
maxReplicaCount: 4
cooldownPeriod: 10
triggers:
- type: azure-queue
metadata:
queueName: queue-name
connectionFromEnv: AzureWebJobsStorage`