Skip to content

Commit

Permalink
fix: avoid crash if testdata file is empty (#1403)
Browse files Browse the repository at this point in the history
return an empty array if testdatafile not found, check for empty
testdata in k6 tests

<!--- Provide a general summary of your changes in the Title above -->

## Description



## Related Issue(s)

- #1402 

## Verification

- [ ] **Your** code builds clean without any errors or warnings
- [ ] Manual testing done (required)
- [ ] Relevant automated test added (if you find this hard, leave it and
we'll help out)

## Documentation

- [ ] Documentation is updated (either in `docs`-directory, Altinnpedia
or a separate linked PR in
[altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if
applicable)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Introduced error handling in various performance testing scripts to
ensure required data arrays are populated before execution.
- Added a new function for reading and parsing CSV files, improving
error handling during file operations.

- **Bug Fixes**
- Enhanced robustness of performance tests by preventing execution with
empty datasets, which could lead to runtime errors.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
dagfinno authored Nov 6, 2024
1 parent 70a63cd commit e0ea0af
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
3 changes: 3 additions & 0 deletions tests/k6/tests/enduser/performance/enduser-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
20 changes: 17 additions & 3 deletions tests/k6/tests/performancetest_common/readTestdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
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;
} 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');
Expand All @@ -24,7 +38,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);
});

/**
Expand All @@ -36,7 +50,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);
});

/**
Expand All @@ -47,6 +61,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);
});

13 changes: 11 additions & 2 deletions tests/k6/tests/scenarios/performance/create-dialog-and-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
6 changes: 6 additions & 0 deletions tests/k6/tests/serviceowner/performance/create-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down

0 comments on commit e0ea0af

Please sign in to comment.