forked from kedacore/keda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rabbitmq-queue-amqp.test.ts
106 lines (96 loc) · 3.06 KB
/
rabbitmq-queue-amqp.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
import * as sh from 'shelljs'
import test from 'ava'
import { RabbitMQHelper } from './rabbitmq-helpers'
import {waitForDeploymentReplicaCount} from "./helpers";
const testNamespace = 'rabbitmq-queue-amqp-test'
const rabbitmqNamespace = 'rabbitmq-amqp-test'
const queueName = 'hello'
const username = "test-user"
const password = "test-password"
const vhost = "test-vh"
const connectionString = `amqp://${username}:${password}@rabbitmq.${rabbitmqNamespace}.svc.cluster.local/${vhost}`
const messageCount = 500
test.before(t => {
RabbitMQHelper.installRabbit(t, username, password, vhost, rabbitmqNamespace)
sh.config.silent = true
// create deployment
RabbitMQHelper.createDeployment(t, testNamespace, deployYaml, connectionString, connectionString, queueName)
})
test.serial('Deployment should have 0 replicas on start', t => {
const replicaCount = sh.exec(
`kubectl get deployment.apps/test-deployment --namespace ${testNamespace} -o jsonpath="{.spec.replicas}"`
).stdout
t.is(replicaCount, '0', 'replica count should start out as 0')
})
test.serial(`Deployment should scale to 4 with ${messageCount} messages on the queue then back to 0`, async t => {
RabbitMQHelper.publishMessages(t, testNamespace, connectionString, messageCount, queueName)
// with messages published, the consumer deployment should start receiving the messages
t.true(await waitForDeploymentReplicaCount(4, 'test-deployment', testNamespace, 20, 5000), 'Replica count should be 4 after 10 seconds')
t.true(await waitForDeploymentReplicaCount(0, 'test-deployment', testNamespace, 50, 5000), 'Replica count should be 0 after 3 minutes')
})
test.after.always.cb('clean up rabbitmq-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 ${testNamespace}`)
}
sh.exec(`kubectl delete namespace ${testNamespace}`)
RabbitMQHelper.uninstallRabbit(rabbitmqNamespace)
t.end()
})
const deployYaml = `apiVersion: v1
kind: Secret
metadata:
name: test-secrets
data:
RabbitMqHost: {{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:
labels:
app: test-deployment
spec:
containers:
- name: rabbitmq-consumer
image: ghcr.io/kedacore/tests-rabbitmq
imagePullPolicy: Always
command:
- receive
args:
- '{{CONNECTION_STRING}}'
envFrom:
- secretRef:
name: test-secrets
---
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: test-scaledobject
spec:
scaleTargetRef:
name: test-deployment
pollingInterval: 5
cooldownPeriod: 10
minReplicaCount: 0
maxReplicaCount: 4
triggers:
- type: rabbitmq
metadata:
queueName: {{QUEUE_NAME}}
hostFromEnv: RabbitMqHost
mode: QueueLength
value: '50'`