Skip to content

Commit c276c09

Browse files
committed
Fix K6 summary handling
1 parent 31c9dd7 commit c276c09

File tree

4 files changed

+41
-61
lines changed

4 files changed

+41
-61
lines changed

.github/workflows/action-run-k6-tests.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ on:
2121
jobs:
2222
k6-test:
2323
runs-on: ubuntu-latest
24+
permissions:
25+
checks: write
26+
pull-requests: write
2427

2528
steps:
2629
- name: Checkout code
@@ -30,7 +33,7 @@ jobs:
3033
uses: grafana/k6-action@v0.3.1
3134
with:
3235
filename: ${{ inputs.testSuitePath }}
33-
#flags: --quiet --log-output=stdout --include-system-env-vars
36+
flags: --quiet --log-output=stdout --include-system-env-vars
3437
env:
3538
API_ENVIRONMENT: ${{ inputs.environment }}
3639
API_VERSION: ${{ inputs.apiVersion }}
@@ -41,9 +44,11 @@ jobs:
4144
uses: actions/upload-artifact@v4
4245
with:
4346
name: k6-summary-report
44-
path: summary.json
47+
path: junit.xml
4548

4649
- name: 'Publish test results'
4750
uses: EnricoMi/publish-unit-test-result-action@v2
51+
if: always()
4852
with:
49-
junit_files: 'junit.xml'
53+
files: |
54+
junit.xml

tests/k6/common/report.js

+25-56
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,33 @@
1-
export { textSummary } from 'https://jslib.k6.io/k6-summary/0.0.1/index.js';
1+
export { textSummary } from 'https://jslib.k6.io/k6-summary/0.1.0/index.js';
22

3-
let replacements = {
4-
'&': '&',
5-
'<': '&lt;',
6-
'>': '&gt;',
7-
"'": '&#39;',
8-
'"': '&quot;',
9-
};
3+
export function generateJUnitXML(k6Json) {
4+
const xmlDoc = [];
5+
xmlDoc.push('<?xml version="1.0" encoding="UTF-8" ?>');
6+
xmlDoc.push('<testsuites>');
107

11-
function escapeHTML(str) {
12-
return str.replace(/[&<>'"]/g, function (char) {
13-
return replacements[char];
14-
});
15-
}
8+
function processGroup(group) {
9+
if (group.name) { // skip root group
10+
xmlDoc.push(`<testsuite name="${group.name}" tests="${group.checks.length}">`);
1611

17-
/**
18-
* Generate a junit xml string from the summary of a k6 run considering each checks as a test case
19-
* @param {*} data
20-
* @param {String} suiteName Name of the test ex., filename
21-
* @returns junit xml string
22-
*/
23-
export function generateJUnitXML(data, suiteName) {
12+
group.checks.forEach(check => {
13+
let failed = check.fails > 0 ;
14+
xmlDoc.push(`<testcase classname="${group.name}" name="${check.name}">`);
15+
if (failed) {
16+
xmlDoc.push(`<failure message="Check failed. See output K6 task for more details.">${check.name}</failure>`);
17+
}
18+
xmlDoc.push('</testcase>');
19+
});
20+
}
2421

25-
let failures = 0;
26-
let cases = [];
27-
let time = (data.state.testRunDurationMs) / 1000;
28-
let checks = [];
29-
if (data.root_group.checks.length > 0) {
30-
checks = data.root_group.checks;
31-
} else if (data.root_group.hasOwnProperty('groups') && data.root_group.groups.length > 0) {
32-
let groups = data.root_group.groups;
33-
groups.forEach((group) => {
34-
if (group.groups.length > 0) {
35-
let subGroups = group.groups;
36-
subGroups.forEach((subGroup) => {
37-
subGroup.checks.forEach((check) => {
38-
checks.push(check);
39-
});
40-
});
41-
} else {
42-
group.checks.forEach((check) => {
43-
checks.push(check);
44-
});
45-
}
22+
group.groups.forEach(subGroup => {
23+
processGroup(subGroup);
4624
});
25+
26+
xmlDoc.push('</testsuite>');
4727
}
48-
checks.forEach((check) => {
49-
if (check.passes >= 1 && check.fails === 0) {
50-
cases.push(`<testcase classname="${escapeHTML(check.name)}" name="${escapeHTML(check.name)}" time="0"/>`);
51-
} else {
52-
failures++;
53-
let errmsg = "See the output of the &quot;Run K6&quot; step to see more details";
54-
cases.push(`<testcase classname="${escapeHTML(check.name)}" name="${escapeHTML(check.name)}" time="0"><failure message="${errmsg}"/></testcase>`);
55-
}
56-
});
5728

58-
return (
59-
`<?xml version="1.0" encoding="UTF-8" ?>\n` +
60-
`<testsuites>\n` +
61-
`<testsuite package="${escapeHTML(suiteName)}" name="${escapeHTML(suiteName)}" id="0" tests="${cases.length}" failures="${failures}" time="${time}">\n` +
62-
`${cases.join('\n')}\n</testsuite>\n</testsuites>`
63-
);
29+
processGroup(k6Json.root_group);
30+
31+
xmlDoc.push('</testsuites>');
32+
return xmlDoc.join('\n');
6433
}

tests/k6/common/summary.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { generateJUnitXML, textSummary } from "./report.js";
33
export default function (data) {
44
return {
55
'stdout': textSummary(data, { indent: ' ', enableColors: true }) + "\n",
6-
'summary.json': JSON.stringify(data),
7-
'junit.xml': generateJUnitXML(data, 'dialogporten')
6+
//'summary.txt': textSummary(data, { indent: ' ', enableColors: false }) + "\n",
7+
//'summary.json': JSON.stringify(data),
8+
'junit.xml': generateJUnitXML(data)
89
};
910
}

tests/k6/suites/createremove-perf-10-vus-1-min.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { default as run, setup as testSetup } from "../tests/serviceowner/performance/createremove-no-delay.js";
2+
import { default as summary } from "../common/summary.js";
23
export let options = {
34
vus: 10, // Number of virtual users
45
duration: '1m', // Test duration
@@ -9,3 +10,7 @@ export function setup() {
910
}
1011

1112
export default function (data) { run(data); }
13+
14+
export function handleSummary(data) {
15+
return summary(data);
16+
}

0 commit comments

Comments
 (0)