Skip to content

Commit bfaf993

Browse files
committed
feat: e2e test for cloud.stream being set
1 parent e2057d3 commit bfaf993

7 files changed

Lines changed: 178 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: test
5+
namespace: k6-tests
6+
data:
7+
test.js: |
8+
import http from "k6/http";
9+
import { check, sleep } from "k6";
10+
import { Trend, Counter } from "k6/metrics";
11+
12+
const BASE_URL = __ENV.BASE_URL || 'http://localhost:3333';
13+
14+
export const options = {
15+
stages: [
16+
{ duration: '5s', target: 5 },
17+
{ duration: '10s', target: 5 },
18+
{ duration: '5s', target: 0 },
19+
],
20+
cloud: {
21+
projectID: $PROJECT_ID,
22+
name: 'k6-operator-e2e-cloud-stream'
23+
}
24+
};
25+
26+
const pizzas = new Counter('quickpizza_number_of_pizzas');
27+
const ingredients = new Trend('quickpizza_ingredients');
28+
29+
export function setup() {
30+
console.log("Starting folks!")
31+
}
32+
33+
export default function () {
34+
let restrictions = {
35+
maxCaloriesPerSlice: 500,
36+
mustBeVegetarian: false,
37+
excludedIngredients: ["pepperoni"],
38+
excludedTools: ["knife"],
39+
maxNumberOfToppings: 6,
40+
minNumberOfToppings: 2
41+
}
42+
let res = http.post(`/api/pizza`, JSON.stringify(restrictions), {
43+
headers: {
44+
'Content-Type': 'application/json',
45+
'X-User-ID': 23423,
46+
},
47+
});
48+
check(res, { "status is 200": (res) => res.status === 200 });
49+
console.log(`${res.json().pizza.name} (${res.json().pizza.ingredients.length} ingredients)`);
50+
pizzas.add(1);
51+
ingredients.add(res.json().pizza.ingredients.length);
52+
sleep(1);
53+
}
54+
55+
export function teardown(){
56+
console.log("That's all folks!")
57+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: kustomize.config.k8s.io/v1beta1
2+
kind: Kustomization
3+
resources:
4+
- ../../latest/
5+
- configmap.yaml
6+
- namespace.yaml
7+
- secret.yaml
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: k6-tests
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: cloud-token
5+
namespace: k6-operator-system
6+
labels:
7+
k6cloud: token
8+
data:
9+
token: $CLOUD_TOKEN

e2e/cloud-stream/test.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Environment } from 'k6/x/environment';
2+
import { sleep, fail } from 'k6';
3+
4+
export const options = {
5+
setupTimeout: '60s',
6+
};
7+
8+
const PARENT = "./"
9+
10+
const env = new Environment({
11+
name: "cloud-stream",
12+
implementation: "vcluster",
13+
initFolder: PARENT + "manifests",
14+
})
15+
16+
export function setup() {
17+
console.log("init returns", env.init());
18+
sleep(0.5);
19+
}
20+
21+
export default function () {
22+
let err = env.apply(PARENT + "testrun.yaml");
23+
console.log("apply testrun returns", err);
24+
25+
err = env.wait({
26+
kind: "TestRun",
27+
name: "k6-sample",
28+
namespace: "k6-tests",
29+
status_key: "stage",
30+
status_value: "started",
31+
}, {
32+
timeout: "1m",
33+
interval: "10s",
34+
});
35+
36+
if (err != null) {
37+
fail("wait for started returns " + err);
38+
}
39+
40+
let allPods = env.getN("pods", {
41+
"namespace": "k6-tests",
42+
"app": "k6",
43+
"k6_cr": "k6-sample",
44+
});
45+
46+
let runnerPods = env.getN("pods", {
47+
"namespace": "k6-tests",
48+
"app": "k6",
49+
"k6_cr": "k6-sample",
50+
"runner": "true",
51+
});
52+
53+
// there should be N runner pods + initializer + starter
54+
if (runnerPods != 1 || allPods != runnerPods + 2) {
55+
fail("wrong number of pods:" + runnerPods + "/" + allPods);
56+
}
57+
58+
err = env.wait({
59+
kind: "TestRun",
60+
name: "k6-sample",
61+
namespace: "k6-tests",
62+
status_key: "stage",
63+
status_value: "finished",
64+
}, {
65+
timeout: "5m",
66+
interval: "10s",
67+
});
68+
69+
if (err != null) {
70+
fail("wait for finished returns " + err);
71+
}
72+
}
73+
74+
export function teardown() {
75+
console.log("delete returns", env.delete());
76+
}

e2e/cloud-stream/testrun.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
apiVersion: k6.io/v1alpha1
3+
kind: TestRun
4+
metadata:
5+
name: k6-sample
6+
namespace: k6-tests
7+
spec:
8+
parallelism: 1
9+
script:
10+
configMap:
11+
name: "test"
12+
file: "test.js"
13+
cloud:
14+
stream: true
15+
runner:
16+
env:
17+
- name: BASE_URL
18+
value: https://pizza.grafana.fun

e2e/tests.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ tests:
9999
- "pkg/cloud/"
100100
requires_env: [CLOUD_TOKEN, PROJECT_ID]
101101

102+
- name: cloud-stream
103+
source_patterns:
104+
- "pkg/cloud/"
105+
- "pkg/resources/jobs/runner.go"
106+
- "internal/controller/k6_initialize.go"
107+
requires_env: [CLOUD_TOKEN, PROJECT_ID]
108+
102109
- name: initializer-disabled
103110
source_patterns:
104111
- "internal/controller/k6_initialize.go"

0 commit comments

Comments
 (0)