Skip to content

Commit

Permalink
adding e2e test for cpu scaler: updated the cpu.test.ts , Resolved ch…
Browse files Browse the repository at this point in the history
…angelog conflicts, addes waitForDeploymentScaleUp in helper.ts

Signed-off-by: Ritikaa96 <ritika@india.nec.com>
  • Loading branch information
Ritikaa96 committed Jan 16, 2022
1 parent c36830f commit 2dc3f51
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- **Kafka Scaler:** concurrently query brokers for consumer and producer offsets ([#2405](https://github.com/kedacore/keda/pull/2405))
- **External Scaler:** fix wrong calculation of retry backoff duration ([#2416](https://github.com/kedacore/keda/pull/2416))
- **Kafka Scaler:** allow flag `topic` to be optional, where lag of all topics within the consumer group will be used for scaling ([#2409](https://github.com/kedacore/keda/pull/2409))
- **CPU Scaler:** Adding e2e test for the cpu scaler ([#2441](https://github.com/kedacore/keda/pull/2441))

### Breaking Changes

Expand Down
41 changes: 28 additions & 13 deletions tests/scalers/cpu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import * as fs from 'fs'
import * as sh from 'shelljs'
import * as tmp from 'tmp'
import test from 'ava'
import { waitForDeploymentReplicaCount, waitForRollout } from './helpers'
import { isAwaitExpression } from 'typescript'
import { waitForDeploymentReplicaCount, waitForDeploymentScaleUp } from './helpers'

const testNamespace = 'cpu-test'
const deployMentFile = tmp.fileSync()
Expand All @@ -23,7 +22,7 @@ test.before(t => {
t.is(0, sh.exec(`kubectl rollout status deploy/php-apache -n ${testNamespace}`).code, 'Deployment php rolled out succesfully')
})

test.serial('Deployment should have 1 replicas on start', t => {
test.serial('Deployment should have 1 replica on start', t => {
const replicaCount = sh.exec(
`kubectl get deployment.apps/php-apache --namespace ${testNamespace} -o jsonpath="{.spec.replicas}"`
).stdout
Expand All @@ -39,14 +38,23 @@ test.serial(`Creating Job should work`, async t => {
)
})

test.serial(`Deployment should scale to 5 after 3 minutes`, async t => {
const maxReplicaCount = '5'
// check replicacount on constant triggering :
t.true(await waitForDeploymentReplicaCount(5, 'php-apache', testNamespace, 18, 10000), 'Replica count should be 5 after 180 seconds')
test.serial(`Deployment should scale in next 3 minutes`, async t => {
// check for increased replica count on constant triggering :
t.true(await waitForDeploymentScaleUp(2, 'php-apache', testNamespace, 18, 10000), 'Replica count should scale up in next 3 minutes')
})
test.serial(`Deployment should scale back to 1 after 10 minutes`, async t => {

test.serial(`Deleting Job should work`, async t => {
fs.writeFileSync(triggerFile.name, triggerJob)
t.is(
0,
sh.exec(`kubectl delete -f ${triggerFile.name} --namespace ${testNamespace}`).code,
'Deleting job should work.'
)
})

test.serial(`Deployment should scale back to 1 in next 3 minutes`, async t => {
// check for the scale down :
t.true(await waitForDeploymentReplicaCount(1, 'php-apache', testNamespace, 60, 10000), 'Replica count should be 1 after 10 minutes')
t.true(await waitForDeploymentReplicaCount(1, 'php-apache', testNamespace, 18, 10000), 'Replica count should be 1 in next 3 minutes')
})

test.after.always.cb('clean up workload test related deployments', t => {
Expand Down Expand Up @@ -106,6 +114,13 @@ metadata:
labels:
run: php-apache
spec:
advanced:
horizontalPodAutoscalerConfig:
behavior:
scaleDown:
stabilizationWindowSeconds: 0
maxReplicaCount: 3
minReplicaCount: 1
scaleTargetRef:
name: php-apache
triggers:
Expand All @@ -121,10 +136,10 @@ spec:
template:
spec:
containers:
- image: jordi/ab
- image: busybox
name: test
command: ["/bin/sh"]
args: ["-c", "for i in $(seq 1 180);do echo $i;ab -c 5 -n 1000 -v 2 http://php-apache.cpu-test.svc/;sleep 1;done"]
args: ["-c", "for i in $(seq 1 400);do wget -q -O- http://php-apache.cpu-test.svc/;sleep 0.1;done"]
restartPolicy: Never
activeDeadlineSeconds: 300
backoffLimit: 2`
activeDeadlineSeconds: 400
backoffLimit: 3`
16 changes: 16 additions & 0 deletions tests/scalers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@ export async function waitForDeploymentReplicaCount(target: number, name: string
}
return false
}

//In cases where instances final count depends on different factors that could change.(for eg. cpu, memory), check scaling up above target.
export async function waitForDeploymentScaleUp(target: number, name: string, namespace: string, iterations = 10, interval = 3000): Promise<boolean> {
for (let i = 0; i < iterations; i++) {
let replicaCountStr = sh.exec(`kubectl get deployment.apps/${name} --namespace ${namespace} -o jsonpath="{.spec.replicas}"`).stdout
try {
const replicaCount = parseInt(replicaCountStr, 10)
if (replicaCount >= target) {
return true
}
} catch { }

await sleep(interval)
}
return false
}

0 comments on commit 2dc3f51

Please # to comment.