From 5a9cb09a8b6851ca869231e7510a81fb45ac4876 Mon Sep 17 00:00:00 2001 From: Dagfinn Olsen Date: Wed, 6 Nov 2024 08:13:41 +0100 Subject: [PATCH 1/2] return an empty array if testdatafile not found, check for empty testdata in k6 tests --- .../tests/enduser/performance/enduser-search.js | 3 +++ .../tests/performancetest_common/readTestdata.js | 15 ++++++++++++--- .../performance/create-dialog-and-search.js | 13 +++++++++++-- .../serviceowner/performance/create-dialog.js | 6 ++++++ .../performance/create-remove-dialog.js | 6 ++++++ 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/tests/k6/tests/enduser/performance/enduser-search.js b/tests/k6/tests/enduser/performance/enduser-search.js index 360d8e7d3..d32d2ba1d 100644 --- a/tests/k6/tests/enduser/performance/enduser-search.js +++ b/tests/k6/tests/enduser/performance/enduser-search.js @@ -17,6 +17,9 @@ export let options = { }; export default function() { + if (!endUsersWithTokens || endUsersWithTokens.length === 0) { + throw new Error('No end users loaded for testing'); + } if ((options.vus === undefined || options.vus === 1) && (options.iterations === undefined || options.iterations === 1)) { enduserSearch(endUsersWithTokens[0]); } diff --git a/tests/k6/tests/performancetest_common/readTestdata.js b/tests/k6/tests/performancetest_common/readTestdata.js index 0fbea3624..e4d8a1d2e 100644 --- a/tests/k6/tests/performancetest_common/readTestdata.js +++ b/tests/k6/tests/performancetest_common/readTestdata.js @@ -9,6 +9,15 @@ import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from "k6/data"; +function readCsv(filename) { + try { + return papaparse.parse(open(filename), { header: true, skipEmptyLines: true }).data; + } catch (error) { + console.log(`Error reading CSV file: ${error}`); + return []; + } +} + const filenameServiceowners = '../performancetest_data/.serviceowners-with-tokens.csv'; if (!__ENV.API_ENVIRONMENT) { throw new Error('API_ENVIRONMENT must be set'); @@ -24,7 +33,7 @@ const filenameEndusersWithTokens = '../performancetest_data/.endusers-with-token * @type {SharedArray} */ export const serviceOwners = new SharedArray('serviceOwners', function () { - return papaparse.parse(open(filenameServiceowners), { header: true, skipEmptyLines: true }).data; + return readCsv(filenameServiceowners); }); /** @@ -36,7 +45,7 @@ export const serviceOwners = new SharedArray('serviceOwners', function () { * @type {SharedArray} */ export const endUsers = new SharedArray('endUsers', function () { - return papaparse.parse(open(filenameEndusers), { header: true, skipEmptyLines: true }).data; + return readCsv(filenameEndusers); }); /** @@ -47,6 +56,6 @@ export const endUsers = new SharedArray('endUsers', function () { * @type {SharedArray} */ export const endUsersWithTokens = new SharedArray('endUsersWithTokens', function () { - return papaparse.parse(open(filenameEndusersWithTokens), { header: true, skipEmptyLines: true }).data; + return readCsv(filenameEndusersWithTokens); }); diff --git a/tests/k6/tests/scenarios/performance/create-dialog-and-search.js b/tests/k6/tests/scenarios/performance/create-dialog-and-search.js index 8329e7402..e9d0dd679 100644 --- a/tests/k6/tests/scenarios/performance/create-dialog-and-search.js +++ b/tests/k6/tests/scenarios/performance/create-dialog-and-search.js @@ -57,9 +57,18 @@ export const options = { }; export function createDialogs() { - createDialog(randomItem(serviceOwners), randomItem(endUsersWithTokens)); + if (!endUsersWithTokens || endUsersWithTokens.length === 0) { + throw new Error('No end users loaded for testing'); + } + if (!serviceOwners || serviceOwners.length === 0) { + throw new Error('No service owners loaded for testing'); + } + createDialog(randomItem(serviceOwners), randomItem(endUsersWithTokens)); } export function enduserSearches() { - enduserSearch(randomItem(endUsersWithTokens)); + if (!endUsersWithTokens || endUsersWithTokens.length === 0) { + throw new Error('No end users loaded for testing'); + } + enduserSearch(randomItem(endUsersWithTokens)); } \ No newline at end of file diff --git a/tests/k6/tests/serviceowner/performance/create-dialog.js b/tests/k6/tests/serviceowner/performance/create-dialog.js index df51bde99..03cbadbc5 100644 --- a/tests/k6/tests/serviceowner/performance/create-dialog.js +++ b/tests/k6/tests/serviceowner/performance/create-dialog.js @@ -13,6 +13,12 @@ export let options = { }; export default function() { + if (!endUsers || endUsers.length === 0) { + throw new Error('No end users loaded for testing'); + } + if (!serviceOwners || serviceOwners.length === 0) { + throw new Error('No service owners loaded for testing'); + } if ((options.vus === undefined || options.vus === 1) && (options.iterations === undefined || options.iterations === 1)) { createDialog(serviceOwners[0], endUsers[0]); } diff --git a/tests/k6/tests/serviceowner/performance/create-remove-dialog.js b/tests/k6/tests/serviceowner/performance/create-remove-dialog.js index 544f40d20..e46134a8a 100644 --- a/tests/k6/tests/serviceowner/performance/create-remove-dialog.js +++ b/tests/k6/tests/serviceowner/performance/create-remove-dialog.js @@ -16,6 +16,12 @@ export let options = { }; export default function() { + if (!endUsers || endUsers.length === 0) { + throw new Error('No end users loaded for testing'); + } + if (!serviceOwners || serviceOwners.length === 0) { + throw new Error('No service owners loaded for testing'); + } if ((options.vus === undefined || options.vus === 1) && (options.iterations === undefined || options.iterations === 1)) { createAndRemoveDialog(serviceOwners[0], endUsers[0]); } From 0385d11d57c342018929db51347f4dac8e331479 Mon Sep 17 00:00:00 2001 From: Dagfinn Olsen Date: Wed, 6 Nov 2024 08:42:54 +0100 Subject: [PATCH 2/2] Document function --- tests/k6/tests/performancetest_common/readTestdata.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/k6/tests/performancetest_common/readTestdata.js b/tests/k6/tests/performancetest_common/readTestdata.js index e4d8a1d2e..538c292a3 100644 --- a/tests/k6/tests/performancetest_common/readTestdata.js +++ b/tests/k6/tests/performancetest_common/readTestdata.js @@ -9,6 +9,11 @@ import papaparse from 'https://jslib.k6.io/papaparse/5.1.1/index.js'; import { SharedArray } from "k6/data"; +/** + * Function to read the CSV file specified by the filename parameter. + * @param {} filename + * @returns + */ function readCsv(filename) { try { return papaparse.parse(open(filename), { header: true, skipEmptyLines: true }).data;