Skip to content
This repository was archived by the owner on Oct 14, 2020. It is now read-only.

Commit 18b31bc

Browse files
committed
[#22] Implement function to retry tests.
1 parent 04caaa6 commit 18b31bc

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

tests/integration/generic/read-only-hook.test.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,58 @@ test(
1111
90
1212
);
1313

14-
// This is necessary to ensure that the HTTP-Server already logged the Request
15-
await new Promise(resolve => setTimeout(resolve, 5000));
16-
17-
const webhook = "http-webhook";
18-
const namespace = "integration-tests";
14+
const WEBHOOK = "http-webhook";
15+
const NAMESPACE = "integration-tests";
1916

2017
const kc = new k8s.KubeConfig();
2118
kc.loadFromDefault();
2219

2320
const k8sApi = kc.makeApiClient(k8s.CoreV1Api);
2421

2522
function containsPod(item) {
26-
return item.metadata.name.includes(webhook)
23+
return item.metadata.name.includes(WEBHOOK)
2724
}
2825

2926
let podName;
30-
await k8sApi.listNamespacedPod(namespace, 'true').then((res) => {
27+
await k8sApi.listNamespacedPod(NAMESPACE, 'true').then((res) => {
3128
let podArray = res.body.items.filter(containsPod);
3229
podName = podArray.pop().metadata.name;
3330
});
3431

35-
const containerName = webhook;
32+
const containerName = WEBHOOK;
33+
34+
let params = {
35+
k8sApi: k8sApi,
36+
podName: podName,
37+
namespace: NAMESPACE,
38+
containerName: containerName
39+
}
40+
const result = await delayedRepeat(isHookTriggered, params, 1000, 10);
3641

37-
let containerLog = await k8sApi.readNamespacedPodLog(podName, namespace, containerName, false);
38-
expect(containerLog.body.includes("path: '/hallo-welt'")).toBe(true);
42+
expect(result).toBe(true)
3943
},
4044
3 * 60 * 1000
4145
);
46+
47+
async function isHookTriggered(params) {
48+
console.log("Fetch Container Logs...")
49+
let containerLog = await params.k8sApi.readNamespacedPodLog(params.podName, params.namespace, params.containerName, false);
50+
return containerLog.body.includes("/hallo-welt");
51+
}
52+
53+
54+
const sleep = durationInMs =>
55+
new Promise(resolve => setTimeout(resolve, durationInMs));
56+
57+
async function delayedRepeat(fun, functionParamObject, intervalInMs, maxRetries,) {
58+
for (let i = 0; i < maxRetries; i++){
59+
const condition = await fun(functionParamObject);
60+
if(condition){
61+
return condition;
62+
}
63+
64+
await sleep(intervalInMs);
65+
}
66+
67+
throw new Error("Reached max retries")
68+
}

0 commit comments

Comments
 (0)