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

Commit 2128e54

Browse files
committed
Better debugging of failed scans during ci
1 parent 0221577 commit 2128e54

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

tests/integration/helpers.js

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ kc.loadFromDefault();
55

66
const k8sCRDApi = kc.makeApiClient(k8s.CustomObjectsApi);
77
const k8sBatchApi = kc.makeApiClient(k8s.BatchV1Api);
8+
const k8sPodsApi = kc.makeApiClient(k8s.CoreV1Api);
89

910
const namespace = "integration-tests";
1011

@@ -33,28 +34,65 @@ async function getScan(name) {
3334
return scan;
3435
}
3536

37+
async function displayAllLogsForJob(jobName) {
38+
console.log(`Listing logs for Job ${jobName}:`);
39+
const {
40+
body: { items: pods },
41+
} = await k8sPodsApi.listNamespacedPod(
42+
"default",
43+
true,
44+
undefined,
45+
undefined,
46+
undefined,
47+
`job-name=${jobName}`
48+
);
49+
50+
for (const pod of pods) {
51+
for (const container of pod.spec.containers) {
52+
const response = await k8sPodsApi.readNamespacedPodLog(
53+
pod.metadata.name,
54+
"default",
55+
container.name
56+
);
57+
console.log(`Container ${container.name}:`);
58+
console.log(response.body);
59+
}
60+
}
61+
}
62+
3663
async function logJobs() {
3764
try {
3865
const { body: jobs } = await k8sBatchApi.listNamespacedJob(namespace);
3966

67+
console.log("Logging spec & status of jobs in namespace");
68+
4069
for (const job of jobs.items) {
4170
console.log(`Job: '${job.metadata.name}' Spec:`);
4271
console.dir(job.spec);
4372
console.log(`Job: '${job.metadata.name}' Status:`);
4473
console.dir(job.status);
74+
75+
await displayAllLogsForJob(job.metadata.name);
4576
}
4677
} catch (error) {
4778
console.info(`Failed to list Jobs'`);
4879
}
4980
}
5081

82+
async function disasterRecovery(scanName) {
83+
const scan = await getScan(scanName);
84+
console.error("Last Scan State:");
85+
console.dir(scan);
86+
await logJobs();
87+
}
88+
5189
/**
5290
*
5391
* @param {string} name name of the scan. Actual name will be sufixed with a random number to avoid conflicts
5492
* @param {string} scanType type of the scan. Must match the name of a ScanType CRD
5593
* @param {string[]} parameters cli argument to be passed to the scanner
5694
* @param {number} timeout in seconds
57-
* @returns {scan.findings} returns findings { categories, severities, count }
95+
* @returns {scan.findings} returns findings { categories, severities, count }
5896
*/
5997
async function scan(name, scanType, parameters = [], timeout = 180) {
6098
const scanDefinition = {
@@ -88,19 +126,16 @@ async function scan(name, scanType, parameters = [], timeout = 180) {
88126
await deleteScan(actualName);
89127
return status.findings;
90128
} else if (status && status.state === "Errored") {
91-
await deleteScan(actualName);
129+
console.error("Scan Errored");
130+
await disasterRecovery(actualName);
131+
92132
throw new Error(
93133
`Scan failed with description "${status.errorDescription}"`
94134
);
95135
}
96136
}
97-
98137
console.error("Scan Timed out!");
99-
100-
const scan = await getScan(actualName);
101-
console.log("Last Scan State:");
102-
console.dir(scan);
103-
await logJobs();
138+
await disasterRecovery(actualName);
104139

105140
throw new Error("timed out while waiting for scan results");
106141
}

0 commit comments

Comments
 (0)