Skip to content

Commit 167b75d

Browse files
authored
Merge pull request #26 from grafana/allow-logging-command-output
Add new input to show all k6 command output
2 parents 575942d + e009732 commit 167b75d

File tree

6 files changed

+131
-115
lines changed

6 files changed

+131
-115
lines changed

.github/workflows/test.yaml

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,48 +7,51 @@ jobs:
77
runs-on: ubuntu-latest
88
env:
99
K6_CLOUD_TOKEN: ${{ secrets.K6_CLOUD_TOKEN }}
10-
K6_CLOUD_PROJECT_ID: ${{ secrets.K6_CLOUD_PROJECT_ID }}
10+
K6_CLOUD_PROJECT_ID: ${{ secrets.K6_CLOUD_PROJECT_ID }}
1111
steps:
12-
- uses: actions/checkout@v4
13-
- name: Setup Grafana k6
14-
uses: grafana/setup-k6-action@main
15-
with:
16-
k6-version: '0.49.0'
17-
- uses: ./
18-
continue-on-error: true
19-
with:
20-
path: |
21-
./dev/protocol*.js
22-
flags: --vus 10 --duration 30s
23-
parallel: true
24-
cloud-run-locally: false
12+
- uses: actions/checkout@v4
13+
- name: Setup Grafana k6
14+
uses: grafana/setup-k6-action@main
15+
- uses: ./
16+
continue-on-error: true
17+
with:
18+
path: |
19+
./dev/protocol*.js
20+
flags: --vus 10 --duration 30s
21+
parallel: true
22+
cloud-run-locally: false
2523
protocol-without-flags:
2624
runs-on: ubuntu-latest
2725
steps:
28-
- uses: actions/checkout@v4
29-
- name: Setup Grafana k6
30-
uses: grafana/setup-k6-action@main
31-
with:
32-
k6-version: '0.49.0'
33-
- uses: ./
34-
continue-on-error: true
35-
with:
36-
path: |
37-
./dev/protocol*.js
26+
- uses: actions/checkout@v4
27+
- name: Setup Grafana k6
28+
uses: grafana/setup-k6-action@main
29+
- uses: ./
30+
continue-on-error: true
31+
with:
32+
path: |
33+
./dev/protocol*.js
34+
show-complete-k6-output:
35+
runs-on: ubuntu-latest
36+
steps:
37+
- uses: actions/checkout@v4
38+
- name: Setup Grafana k6
39+
uses: grafana/setup-k6-action@main
40+
- uses: ./
41+
with:
42+
path: |
43+
./dev/protocol.js
44+
flags: --vus 10 --duration 30s
45+
debug: true
3846
verify-scripts:
3947
runs-on: ubuntu-latest
40-
env:
41-
K6_CLOUD_TOKEN: ${{ secrets.K6_CLOUD_TOKEN }}
42-
K6_CLOUD_PROJECT_ID: ${{ secrets.K6_CLOUD_PROJECT_ID }}
4348
steps:
44-
- uses: actions/checkout@v4
45-
- name: Setup Grafana k6
46-
uses: grafana/setup-k6-action@main
47-
with:
48-
k6-version: '0.49.0'
49-
- uses: ./
50-
continue-on-error: true
51-
with:
52-
path: |
53-
./dev/verify-script-tests/**.js
54-
only-verify-scripts: true
49+
- uses: actions/checkout@v4
50+
- name: Setup Grafana k6
51+
uses: grafana/setup-k6-action@main
52+
- uses: ./
53+
continue-on-error: true
54+
with:
55+
path: |
56+
./dev/verify-script-tests/**.js
57+
only-verify-scripts: true

README.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div align="center">
2-
2+
33
<img
44
src="./images/k6.gif"
55
width="600"
@@ -12,7 +12,7 @@
1212

1313
# run-k6-action
1414

15-
This action allows you to easily execute k6 tests as part of your GitHub Actions workflow.
15+
This action allows you to easily execute k6 tests as part of your GitHub Actions workflow.
1616

1717
It is a wrapper over `k6 run`, with support for globs, parallel execution, fail-fast, and many other features.
1818

@@ -22,19 +22,19 @@ It is a wrapper over `k6 run`, with support for globs, parallel execution, fail-
2222

2323
The following inputs can be used as `step.with` key:
2424

25-
| Name | Type | Required | Default | Description
26-
| --- | --- | --- | --- | --- |
27-
| `path` | string | `true` | `''` | Glob pattern to select one or multiple test scripts to run. <br/> e.g. `./tests/api*.js` <br/> One can also supply multiple patterns by separating them with new line.<br/><code>path: \|<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;./tests/api*.js<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;./tests/app*.js</code>
28-
| `cloud-run-locally` | boolean | `false` | `true` | If `true`, the tests are executed locally and the results are uploaded to Grafana Cloud k6
29-
| `parallel` | boolean | `false` | `false` | If `true` and multiple tests are executed, all them run in parallel.
30-
| `fail-fast` | boolean | `false` | `false` | If `true`, the whole pipeline fails as soon as the first test fails
31-
| `flags` | string | `false` | `''` | Additional flags to be passed on to the `k6 run` command.<br/>e.g. `--vus 10 --duration 20s`
32-
| `cloud-comment-on-pr` | boolean | `false` | `true` | If `true`, the workflow comments a link to the cloud test run on the pull request (if present)
33-
| `only-verify-scripts` | boolean | `false` | `false` | If `true`, only check if the test scripts are valid and skip the test execution'
34-
25+
| Name | Type | Required | Default | Description |
26+
| --------------------- | ------- | -------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
27+
| `path` | string | `true` | `''` | Glob pattern to select one or multiple test scripts to run. <br/> e.g. `./tests/api*.js` <br/> One can also supply multiple patterns by separating them with new line.<br/><code>path: \|<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;./tests/api*.js<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;./tests/app*.js</code> |
28+
| `cloud-run-locally` | boolean | `false` | `true` | If `true`, the tests are executed locally and the results are uploaded to Grafana Cloud k6 |
29+
| `parallel` | boolean | `false` | `false` | If `true` and multiple tests are executed, all them run in parallel. |
30+
| `fail-fast` | boolean | `false` | `false` | If `true`, the whole pipeline fails as soon as the first test fails |
31+
| `flags` | string | `false` | `''` | Additional flags to be passed on to the `k6 run` command.<br/>e.g. `--vus 10 --duration 20s` |
32+
| `cloud-comment-on-pr` | boolean | `false` | `true` | If `true`, the workflow comments a link to the cloud test run on the pull request (if present) |
33+
| `only-verify-scripts` | boolean | `false` | `false` | If `true`, only check if the test scripts are valid and skip the test execution |
34+
| `debug` | boolean | `false` | `false` | If true, the output from k6 will be shown in the action logs, else only the summary will be shown. |
3535
## Usage
3636

37-
Following are some examples of using the workflow.
37+
Following are some examples of using the workflow.
3838

3939
### Basic
4040

@@ -113,7 +113,7 @@ jobs:
113113
fail-fast: false # optional: fail the step early if any test fails (default: true)
114114
```
115115

116-
Comment Grafana cloud k6 test URL on PR
116+
Comment Grafana cloud k6 test URL on PR
117117

118118
```yaml
119119
- uses: grafana/run-k6-action@v1

action.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ inputs:
3434
description: 'If true, only check if the test scripts are valid and skip test execution'
3535
default: "false"
3636
required: false
37+
debug:
38+
description: 'If true, the output from k6 will be shown in the action logs, else only the summary will be shown'
39+
default: "false"
40+
required: false
3741

3842
runs:
3943
using: node20

dist/index.js

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34364,7 +34364,9 @@ async function run() {
3436434364
const cloudRunLocally = core.getInput('cloud-run-locally', { required: false }) === 'true';
3436534365
const onlyVerifyScripts = core.getInput('only-verify-scripts', { required: false }) === 'true';
3436634366
const shouldCommentCloudTestRunUrlOnPR = core.getInput('cloud-comment-on-pr', { required: false }) === 'true';
34367+
const debug = core.getInput('debug', { required: false }) === 'true';
3436734368
const allPromises = [];
34369+
core.debug(`Flag to show k6 progress output set to: ${debug}`);
3436834370
core.debug(`🔍 Found following ${testPaths.length} test run files:`);
3436934371
testPaths.forEach((testPath, index) => {
3437034372
core.debug(`${index + 1}. ${testPath}`);
@@ -34405,7 +34407,6 @@ async function run() {
3440534407
return true;
3440634408
}
3440734409
});
34408-
;
3440934410
let allTestsPassed = true;
3441034411
if (parallel) {
3441134412
const childProcesses = [];
@@ -34516,7 +34517,7 @@ async function run() {
3451634517
});
3451734518
// Parse k6 command output and extract test run URLs if running in cloud mode.
3451834519
// Also, print the output to the console, excluding the progress lines.
34519-
child.stdout?.on('data', (data) => (0, k6OutputParser_1.parseK6Output)(data, TEST_RESULT_URLS_MAP, TOTAL_TEST_RUNS));
34520+
child.stdout?.on('data', (data) => (0, k6OutputParser_1.parseK6Output)(data, TEST_RESULT_URLS_MAP, TOTAL_TEST_RUNS, debug));
3452034521
return child;
3452134522
}
3452234523
}
@@ -34579,7 +34580,7 @@ const REGEX_EXPRESSIONS = {
3457934580
output: /^\s*output:\s*(.+)$/m,
3458034581
outputCloudUrl: /cloud\s*\((.+)\)/,
3458134582
runningIteration: /running \(.*\), \d+\/\d+ VUs, \d+ complete and \d+ interrupted iterations/g,
34582-
// default [ 20% ] 10 VUs 1.0s/5s
34583+
// default [ 20% ] 10 VUs 1.0s/5s
3458334584
// createBrowser [ 61% ] 035/500 VUs 0m36.5s/1m0s 5.00 iters/s
3458434585
executionProgress: /\[\s*(\d+)%\s*\]\s*\d+(\/\d+)? VUs/g,
3458534586
// Init [ 0% ] Loading test script...
@@ -34668,7 +34669,7 @@ function checkIfK6ASCIIArt(data) {
3466834669
return true;
3466934670
}
3467034671
}
34671-
function parseK6Output(data, testRunUrlsMap, totalTestRuns) {
34672+
function parseK6Output(data, testRunUrlsMap, totalTestRuns, debug) {
3467234673
/*
3467334674
* This function is responsible for parsing the output of the k6 command.
3467434675
* It filters out the progress lines and logs the rest of the output.
@@ -34677,35 +34678,38 @@ function parseK6Output(data, testRunUrlsMap, totalTestRuns) {
3467734678
* @param {Buffer} data - The k6 command output data
3467834679
* @param {TestRunUrlsMap | null} testRunUrlsMap - The map containing the script path and output URL. If null, the function will not extract test run URLs.
3467934680
* @param {number} totalTestRuns - The total number of test runs. This is used to determine when all test run URLs have been extracted.
34681+
* @param {boolean} debug - A flag to determine if the k6 progress output should be shown or not.
3468034682
*
3468134683
* @returns {void}
3468234684
*/
3468334685
const dataString = data.toString(), lines = dataString.split('\n');
3468434686
// Extract test run URLs
3468534687
if (testRunUrlsMap && Object.keys(testRunUrlsMap).length < totalTestRuns) {
34686-
if (extractTestRunUrl(dataString, testRunUrlsMap)) {
34687-
// Test URL was extracted successfully and added to the map.
34688-
// Ignore further output parsing for this data.
34689-
return;
34690-
}
34691-
if (checkIfK6ASCIIArt(dataString)) {
34692-
// Ignore the k6 ASCII art.
34693-
// Checking the k6 ASCII art here because it is printed at the start of execution,
34694-
// hence if all the test URLs are extracted, the ASCII art will not be printed.
34688+
const testRunUrlExtracted = extractTestRunUrl(dataString, testRunUrlsMap), k6ASCIIArt = checkIfK6ASCIIArt(dataString);
34689+
if ((testRunUrlExtracted || k6ASCIIArt) && !debug) {
34690+
/*
34691+
If either the test run URL was extracted successfully or the k6 ASCII art was found,
34692+
and the k6 progress output is not to be shown, then return.
34693+
*/
3469534694
return;
3469634695
}
3469734696
}
34698-
const filteredLines = lines.filter((line) => {
34699-
const isRegexMatch = TEST_RUN_PROGRESS_MSG_REGEXES.some((regex) => regex.test(line));
34700-
return !isRegexMatch;
34701-
});
34702-
if (filteredLines.length < lines.length) {
34703-
// ignore empty lines only when progress lines output was ignored.
34704-
if (filteredLines.join("") === "") {
34705-
return;
34697+
if (debug) {
34698+
console.log(dataString);
34699+
}
34700+
else {
34701+
const filteredLines = lines.filter((line) => {
34702+
const isRegexMatch = TEST_RUN_PROGRESS_MSG_REGEXES.some((regex) => regex.test(line));
34703+
return !isRegexMatch;
34704+
});
34705+
if (filteredLines.length < lines.length) {
34706+
// ignore empty lines only when progress lines output was ignored.
34707+
if (filteredLines.join("") === "") {
34708+
return;
34709+
}
3470634710
}
34711+
console.log(filteredLines.join('\n'));
3470734712
}
34708-
console.log(filteredLines.join('\n'));
3470934713
}
3471034714
exports.parseK6Output = parseK6Output;
3471134715

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ export async function run(): Promise<void> {
2424
const cloudRunLocally = core.getInput('cloud-run-locally', { required: false }) === 'true'
2525
const onlyVerifyScripts = core.getInput('only-verify-scripts', { required: false }) === 'true'
2626
const shouldCommentCloudTestRunUrlOnPR = core.getInput('cloud-comment-on-pr', { required: false }) === 'true'
27+
const debug = core.getInput('debug', { required: false }) === 'true'
2728
const allPromises: Promise<void>[] = [];
2829

30+
core.debug(`Flag to show k6 progress output set to: ${debug}`);
31+
2932
core.debug(`🔍 Found following ${testPaths.length} test run files:`);
3033
testPaths.forEach((testPath, index) => {
3134
core.debug(`${index + 1}. ${testPath}`);
@@ -76,7 +79,6 @@ export async function run(): Promise<void> {
7679
return true;
7780
}
7881
});
79-
;
8082

8183
let allTestsPassed = true;
8284

@@ -191,7 +193,7 @@ export async function run(): Promise<void> {
191193
});
192194
// Parse k6 command output and extract test run URLs if running in cloud mode.
193195
// Also, print the output to the console, excluding the progress lines.
194-
child.stdout?.on('data', (data) => parseK6Output(data, TEST_RESULT_URLS_MAP, TOTAL_TEST_RUNS));
196+
child.stdout?.on('data', (data) => parseK6Output(data, TEST_RESULT_URLS_MAP, TOTAL_TEST_RUNS, debug));
195197

196198
return child;
197199
}

0 commit comments

Comments
 (0)