From 90a1e898b6a1ce151f81409b49b1c7a2a866a3bc Mon Sep 17 00:00:00 2001 From: thim81 Date: Wed, 1 Sep 2021 15:51:15 +0200 Subject: [PATCH] K6 JSON handleSummary() (#17) * Added option to generate a K6 summary through the handleSummary() callback. Co-authored-by: Tim <> --- CHANGELOG.md | 6 +++- README.md | 13 ++++++++ bin/postman-to-k6.js | 4 +++ lib/convert/object.js | 5 ++++ lib/generate/HandleSummary.js | 22 ++++++++++++++ lib/render/index.js | 13 ++++++++ test/int/handlesummary.js | 12 ++++++++ test/int/snapshots/handlesummary.js.md | 36 +++++++++++++++++++++++ test/int/snapshots/handlesummary.js.snap | Bin 0 -> 436 bytes 9 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 lib/generate/HandleSummary.js create mode 100644 test/int/handlesummary.js create mode 100644 test/int/snapshots/handlesummary.js.md create mode 100644 test/int/snapshots/handlesummary.js.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index fad88bb..2cda4e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased](https://github.com/apideck-libraries/postman-to-k6/compare/v1.6.0...HEAD) +## [Unreleased](https://github.com/apideck-libraries/postman-to-k6/compare/v1.6.1...HEAD) + +### Added + +- Added option to generate a K6 summary through the handleSummary() callback. ### Changed diff --git a/README.md b/README.md index 41387dd..758b79b 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,19 @@ Pass [K6 parameter options](https://k6.io/docs/javascript-api/k6-http/params) as $ postman-to-k6 collection.json --k6-params k6-params.json -o k6-script.js ``` +### K6 Handle Summary as JSON + +Output the [K6 summary](https://k6.io/docs/results-visualization/end-of-test-summary/#handlesummary-callback) as a file in JSON format. +This will add the K6 `handleSummary(data)` to the generated script, providing the functionality that K6 will store the summary output as JSON file locally. + +| Flag | Verbose | Default | +| ---- | -------------------------- | ------- | +| | `--k6-handle-summary-json` | N/A | + +```shell +$ postman-to-k6 collection.json --k6-handle-summary-json summary-report.json -o k6-script.js +``` + ### Separate Split requests into separate files, for easier rearrangement of the logic. diff --git a/bin/postman-to-k6.js b/bin/postman-to-k6.js index adc4b3b..006f796 100755 --- a/bin/postman-to-k6.js +++ b/bin/postman-to-k6.js @@ -37,6 +37,7 @@ program .option('--oauth1-version ', 'OAuth1 version.') .option('--oauth1-realm ', 'OAuth1 realm.') .option('-s, --separate', 'Generate a separate file for each request.') + .option('--k6-handle-summary-json ', 'Output the K6 handle summary as a JSON file.') .action(run) .parse(process.argv); @@ -104,6 +105,9 @@ function translateOptions(options) { pre: options.skipPre, post: options.skipPost, }, + k6HandleSummary: { + json: options.k6HandleSummaryJson + }, }; } diff --git a/lib/convert/object.js b/lib/convert/object.js index 0a4928e..d4c69c5 100644 --- a/lib/convert/object.js +++ b/lib/convert/object.js @@ -6,6 +6,7 @@ const Globals = require('../generate/Globals'); const postman = require('postman-collection'); const render = require('../render'); const separate = require('../generate/separate'); +const Handlesummary = require('../generate/HandleSummary'); const transformer = require('postman-collection-transformer'); async function convertObject(collection, options = {}) { @@ -49,6 +50,9 @@ async function convertObject(collection, options = {}) { if (options.separate) { separate(node, result); } + if (options.k6HandleSummary) { + Handlesummary(options.k6HandleSummary, result); + } return render(result); } @@ -66,6 +70,7 @@ const upgradeOptions = { outputVersion: '2.1.0', retainIds: true, }; + function upgrade(collection) { return new Promise((resolve, reject) => { transformer.convert(collection, upgradeOptions, (error, result) => { diff --git a/lib/generate/HandleSummary.js b/lib/generate/HandleSummary.js new file mode 100644 index 0000000..2dbc7f0 --- /dev/null +++ b/lib/generate/HandleSummary.js @@ -0,0 +1,22 @@ +function HandleSummary(k6HandleSummaryOptions, result) { + result.handleSummary = []; + if (k6HandleSummaryOptions.json) { + injectJsonHandleSummary(k6HandleSummaryOptions.json, result); + } + if (k6HandleSummaryOptions.junit) { + injectJunitHandleSummary(k6HandleSummaryOptions.junit, result); + } +} + +function injectJsonHandleSummary(jsonPath, result) { + const jsonOut = `"${jsonPath}": JSON.stringify(data)`; + result.handleSummary.push(jsonOut); +} + +function injectJunitHandleSummary(junitPath, result) { + result.imports.set('jUnit', { base: './libs/shim/handleSummary.js' }); + const junitOut = `"${junitPath}": jUnit(data)`; + result.handleSummary.push(junitOut); +} + +module.exports = HandleSummary; diff --git a/lib/render/index.js b/lib/render/index.js index 1d36eab..0838c50 100644 --- a/lib/render/index.js +++ b/lib/render/index.js @@ -18,6 +18,7 @@ function render(result) { data(result), initial(result), files(result), + handleSummary(result), logic(result), ] .filter(section => section) @@ -194,6 +195,18 @@ function fileLoad(path) { );`; } +function handleSummary(result) { + if (result.handleSummary && result.handleSummary.length > 0) { + return `export function handleSummary(data) { + console.log('Preparing the end-of-test summary: '); + return { + ${aid.indent(result.handleSummary.join(',\n'))} + } +} +`; + } +} + function logic(result) { return `export default function() { ${aid.indent(outer(result))} diff --git a/test/int/handlesummary.js b/test/int/handlesummary.js new file mode 100644 index 0000000..948033d --- /dev/null +++ b/test/int/handlesummary.js @@ -0,0 +1,12 @@ +import test from 'ava'; +import convertFile from 'convert/file'; + +test('k6 handle-summary json', async t => { + const options = { + k6HandleSummary: { + json: 'k6-handle-summary.json', + }, + }; + const [main] = await convertFile('test/material/2/request.json', options); + t.snapshot(main); +}); diff --git a/test/int/snapshots/handlesummary.js.md b/test/int/snapshots/handlesummary.js.md new file mode 100644 index 0000000..9abf0f6 --- /dev/null +++ b/test/int/snapshots/handlesummary.js.md @@ -0,0 +1,36 @@ +# Snapshot report for `test/int/handlesummary.js` + +The actual snapshot is saved in `handlesummary.js.snap`. + +Generated by [AVA](https://ava.li). + +## k6 handle-summary json + +> Snapshot 1 + + `// Auto-generated by the postman-to-k6 converter␊ + ␊ + import "./libs/shim/core.js";␊ + ␊ + export let options = { maxRedirects: 4 };␊ + ␊ + const Request = Symbol.for("request");␊ + postman[Symbol.for("initial")]({␊ + options␊ + });␊ + ␊ + export function handleSummary(data) {␊ + console.log("Preparing the end-of-test summary: ");␊ + return {␊ + "k6-handle-summary.json": JSON.stringify(data)␊ + };␊ + }␊ + ␊ + export default function() {␊ + postman[Request]({␊ + name: "TestRequest",␊ + method: "GET",␊ + address: "http://example.com/"␊ + });␊ + }␊ + ` diff --git a/test/int/snapshots/handlesummary.js.snap b/test/int/snapshots/handlesummary.js.snap new file mode 100644 index 0000000000000000000000000000000000000000..8012b2f25251a927d99368ff934519c4a59cfcb9 GIT binary patch literal 436 zcmV;l0ZaZtRzVNehz)?dlLJYp&4|x+GD$vyOllWviCH^o`PG zWgQlmo*yL%t_QhA0^5ipPjHGqdkpt6EH-*++oHBE z?oN?e!P~IQKUr6v-9*Of#*rdHhUqcdsD?KG{VRh7roZPZBi*nWDuL;c-vyp z+EvQGJM4s0W;KLIBXe8l6uJD^IETmr;E;M}1_J