-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding cpu scaler e2e test: adding cpu.test.ts
Signed-off-by: Ritikaa96 <[email protected]>
- Loading branch information
Showing
1 changed file
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
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' | ||
|
||
const testNamespace = 'cpu-test' | ||
const deployMentFile = tmp.fileSync() | ||
const triggerFile = tmp.fileSync() | ||
|
||
|
||
test.before(t => { | ||
sh.config.silent = true | ||
sh.exec(`kubectl create namespace ${testNamespace}`) | ||
|
||
fs.writeFileSync(deployMentFile.name, deployMentYaml) | ||
t.is( | ||
0, | ||
sh.exec(`kubectl apply -f ${deployMentFile.name} --namespace ${testNamespace}`).code, | ||
'Deploying php deployment should work.' | ||
) | ||
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 => { | ||
const replicaCount = sh.exec( | ||
`kubectl get deployment.apps/php-apache --namespace ${testNamespace} -o jsonpath="{.spec.replicas}"` | ||
).stdout | ||
t.is(replicaCount, '1', 'replica count should start out as 1') | ||
}) | ||
|
||
test.serial(`Creating Job should work`, async t => { | ||
fs.writeFileSync(triggerFile.name, triggerJob) | ||
t.is( | ||
0, | ||
sh.exec(`kubectl apply -f ${triggerFile.name} --namespace ${testNamespace}`).code, | ||
'creating job should work.' | ||
) | ||
}) | ||
|
||
test.serial(`Deployment should scale to 2`, 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 back to 1`, 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') | ||
}) | ||
|
||
test.after.always.cb('clean up workload test related deployments', t => { | ||
const resources = [ | ||
'deployment.apps/php-apache', | ||
'jobs.batch/trigger-job', | ||
] | ||
for (const resource of resources) { | ||
sh.exec(`kubectl delete ${resource} --namespace ${testNamespace}`) | ||
} | ||
sh.exec(`kubectl delete namespace ${testNamespace}`) | ||
t.end() | ||
}) | ||
|
||
const deployMentYaml = `apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: php-apache | ||
spec: | ||
selector: | ||
matchLabels: | ||
run: php-apache | ||
replicas: 1 | ||
template: | ||
metadata: | ||
labels: | ||
run: php-apache | ||
spec: | ||
containers: | ||
- name: php-apache | ||
image: k8s.gcr.io/hpa-example | ||
ports: | ||
- containerPort: 80 | ||
resources: | ||
limits: | ||
cpu: 500m | ||
requests: | ||
cpu: 200m | ||
imagePullPolicy: IfNotPresent | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: php-apache | ||
labels: | ||
run: php-apache | ||
spec: | ||
ports: | ||
- port: 80 | ||
selector: | ||
run: php-apache | ||
--- | ||
apiVersion: keda.sh/v1alpha1 | ||
kind: ScaledObject | ||
metadata: | ||
name: cpu-scaledobject | ||
labels: | ||
run: php-apache | ||
spec: | ||
scaleTargetRef: | ||
name: php-apache | ||
triggers: | ||
- type: cpu | ||
metadata: | ||
type: Utilization | ||
value: "50"` | ||
const triggerJob = `apiVersion: batch/v1 | ||
kind: Job | ||
metadata: | ||
name: trigger-job | ||
spec: | ||
template: | ||
spec: | ||
containers: | ||
- image: jordi/ab | ||
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"] | ||
restartPolicy: Never | ||
activeDeadlineSeconds: 300 | ||
backoffLimit: 2` |