forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postgresql.test.ts
261 lines (240 loc) · 7.78 KB
/
postgresql.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
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
import * as fs from 'fs'
import * as sh from 'shelljs'
import * as tmp from 'tmp'
import test from 'ava'
import { createNamespace } from './helpers'
const testNamespace = 'postgresql-test'
const postgreSQLNamespace = 'postgresql'
const postgreSQLUsername = 'test-user'
const postgreSQLPassword = 'test-password'
const postgreSQLDatabase = 'test_db'
const deploymentName = 'worker'
test.before(t => {
// install postgresql
createNamespace(postgreSQLNamespace)
const postgreSQLTmpFile = tmp.fileSync()
fs.writeFileSync(postgreSQLTmpFile.name, postgresqlDeploymentYaml.replace('{{POSTGRES_USER}}', postgreSQLUsername)
.replace('{{POSTGRES_PASSWORD}}', postgreSQLPassword)
.replace('{{POSTGRES_DB}}', postgreSQLDatabase)
.replace('{{POSTGRES_DB}}', postgreSQLDatabase))
t.is(0, sh.exec(`kubectl apply --namespace ${postgreSQLNamespace} -f ${postgreSQLTmpFile.name}`).code, 'creating a POSTGRES deployment should work.')
// wait for postgresql to load
let postgresqlReadyReplicaCount = '0'
for (let i = 0; i < 30; i++) {
postgresqlReadyReplicaCount = sh.exec(`kubectl get deploy/postgresql -n ${postgreSQLNamespace} -o jsonpath='{.status.readyReplicas}'`).stdout
if (postgresqlReadyReplicaCount != '1') {
sh.exec('sleep 2s')
}
}
t.is('1', postgresqlReadyReplicaCount, 'Postgresql is not in a ready state')
// create table that used by the job and the worker
const postgresqlPod = sh.exec(`kubectl get po -n ${postgreSQLNamespace} -o jsonpath='{.items[0].metadata.name}'`).stdout
t.not(postgresqlPod, '')
const createTableSQL = `CREATE TABLE task_instance (id serial PRIMARY KEY,state VARCHAR(10));`
sh.exec( `kubectl exec -n ${postgreSQLNamespace} ${postgresqlPod} -- psql -U ${postgreSQLUsername} -d ${postgreSQLDatabase} -c "${createTableSQL}"`)
sh.config.silent = true
createNamespace(testNamespace)
// deploy streams consumer app, scaled object etc.
const tmpFile = tmp.fileSync()
const base64ConnectionString = Buffer.from(`postgresql://${postgreSQLUsername}:${postgreSQLPassword}@postgresql.${postgreSQLNamespace}.svc.cluster.local:5432/${postgreSQLDatabase}?sslmode=disable`).toString('base64')
fs.writeFileSync(tmpFile.name, deployYaml.replace('{{POSTGRES_CONNECTION_STRING}}', base64ConnectionString).replace('{{DEPLOYMENT_NAME}}', deploymentName))
t.is(
0,
sh.exec(`kubectl apply -f ${tmpFile.name} --namespace ${testNamespace}`).code,
'creating a deployment should work..'
)
})
test.serial('Deployment should have 0 replicas on start', t => {
const replicaCount = sh.exec(
`kubectl get deployment.apps/${deploymentName} --namespace ${testNamespace} -o jsonpath="{.spec.replicas}"`
).stdout
t.is(replicaCount, '0', 'replica count should start out as 0')
})
test.serial(`Deployment should scale to 5 (the max) then back to 0`, t => {
const tmpFile = tmp.fileSync()
fs.writeFileSync(tmpFile.name, insertRecordsJobYaml)
t.is(
0,
sh.exec(`kubectl apply -f ${tmpFile.name} --namespace ${testNamespace}`).code,
'creating job should work.'
)
let replicaCount = '0'
const maxReplicaCount = '5'
for (let i = 0; i < 30 && replicaCount !== maxReplicaCount; i++) {
replicaCount = sh.exec(
`kubectl get deployment.apps/${deploymentName} --namespace ${testNamespace} -o jsonpath="{.spec.replicas}"`
).stdout
if (replicaCount !== maxReplicaCount) {
sh.exec('sleep 2s')
}
}
t.is(maxReplicaCount, replicaCount, `Replica count should be ${maxReplicaCount} after 60 seconds`)
for (let i = 0; i < 36 && replicaCount !== '0'; i++) {
replicaCount = sh.exec(
`kubectl get deployment.apps/${deploymentName} --namespace ${testNamespace} -o jsonpath="{.spec.replicas}"`
).stdout
if (replicaCount !== '0') {
sh.exec('sleep 5s')
}
}
t.is('0', replicaCount, 'Replica count should be 0 after 3 minutes')
})
test.after.always.cb('clean up postgresql deployment', t => {
const resources = [
'scaledobject.keda.sh/postgresql-scaledobject',
'triggerauthentication.keda.sh/keda-trigger-auth-postgresql-secret',
`deployment.apps/${deploymentName}`,
'secret/postgresql-secrets',
'job/postgresql-insert-job',
]
for (const resource of resources) {
sh.exec(`kubectl delete ${resource} --namespace ${testNamespace}`)
}
sh.exec(`kubectl delete namespace ${testNamespace}`)
// uninstall postgresql
sh.exec(`kubectl delete --namespace ${postgreSQLNamespace} deploy/postgresql`)
sh.exec(`kubectl delete namespace ${postgreSQLNamespace}`)
t.end()
})
const deployYaml = `apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: postgresql-update-worker
name: {{DEPLOYMENT_NAME}}
spec:
replicas: 0
selector:
matchLabels:
app: postgresql-update-worker
template:
metadata:
labels:
app: postgresql-update-worker
spec:
containers:
- image: ghcr.io/kedacore/tests-postgresql
imagePullPolicy: Always
name: postgresql-processor-test
command:
- /app
- update
env:
- name: TASK_INSTANCES_COUNT
value: "10000"
- name: CONNECTION_STRING
valueFrom:
secretKeyRef:
name: postgresql-secrets
key: postgresql_conn_str
---
apiVersion: v1
kind: Secret
metadata:
name: postgresql-secrets
type: Opaque
data:
postgresql_conn_str: {{POSTGRES_CONNECTION_STRING}}
---
apiVersion: keda.sh/v1alpha1
kind: TriggerAuthentication
metadata:
name: keda-trigger-auth-postgresql-secret
spec:
secretTargetRef:
- parameter: connection
name: postgresql-secrets
key: postgresql_conn_str
---
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: postgresql-scaledobject
spec:
scaleTargetRef:
name: worker
pollingInterval: 5
cooldownPeriod: 10
minReplicaCount: 0
maxReplicaCount: 5
triggers:
- type: postgresql
metadata:
targetQueryValue: "4"
query: "SELECT CEIL(COUNT(*) / 5) FROM task_instance WHERE state='running' OR state='queued'"
authenticationRef:
name: keda-trigger-auth-postgresql-secret`
const insertRecordsJobYaml = `apiVersion: batch/v1
kind: Job
metadata:
labels:
app: postgresql-insert-job
name: postgresql-insert-job
spec:
template:
metadata:
labels:
app: postgresql-insert-job
spec:
containers:
- image: ghcr.io/kedacore/tests-postgresql
imagePullPolicy: Always
name: postgresql-processor-test
command:
- /app
- insert
env:
- name: TASK_INSTANCES_COUNT
value: "10000"
- name: CONNECTION_STRING
valueFrom:
secretKeyRef:
name: postgresql-secrets
key: postgresql_conn_str
restartPolicy: Never
backoffLimit: 4`
const postgresqlDeploymentYaml = `apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: postgresql
name: postgresql
spec:
replicas: 1
selector:
matchLabels:
app: postgresql
template:
metadata:
labels:
app: postgresql
spec:
containers:
- image: postgres:10.5
name: postgresql
env:
- name: POSTGRES_USER
value: {{POSTGRES_USER}}
- name: POSTGRES_PASSWORD
value: {{POSTGRES_PASSWORD}}
- name: POSTGRES_DB
value: {{POSTGRES_DB}}
ports:
- name: postgresql
protocol: TCP
containerPort: 5432
---
apiVersion: v1
kind: Service
metadata:
labels:
app: postgresql
name: postgresql
spec:
ports:
- port: 5432
protocol: TCP
targetPort: 5432
selector:
app: postgresql
type: ClusterIP`