Skip to content

Commit

Permalink
Fixes to k6 scripts (#568)
Browse files Browse the repository at this point in the history
- Update threshold to use checks metrics instead of errors
- Simplified the stopInterationOnFail method and updated call points.
  • Loading branch information
SandGrainOne authored Jul 4, 2024
1 parent bf04c37 commit 39c74d5
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 315 deletions.
40 changes: 21 additions & 19 deletions test/k6/readme.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
## k6 test project for automated tests
# Automated tests using k6

# Getting started


Install pre-requisites
## Install k6
## Install prerequisites

*We recommend running the tests through a docker container.*

From the command line:

> docker pull grafana/k6
```
docker pull grafana/k6
```

Further information on [installing k6 for running in docker is available here.](https://k6.io/docs/get-started/installation/#docker)


Alternatively, it is possible to run the tests directly on your machine as well.

[General installation instructions are available here.](https://k6.io/docs/get-started/installation/)
Expand All @@ -25,32 +21,38 @@ Alternatively, it is possible to run the tests directly on your machine as well.

All tests are defined in `src/tests` and in the top of each test file an example of the cmd to run the test is available.

The command should be run from the root of the k6 folder.
The command should be run from the k6 folder.

>$> cd /altinn-notifications/test/k6
```
$> cd /altinn-notifications/test/k6
```

Run test suite by specifying filename.

For example:

>$> podman compose run k6 run /src/tests/orders_email.js `
-e tokenGeneratorUserName=autotest `
```
$> podman compose run k6 run /src/tests/orders_email.js `
-e tokenGeneratorUserName=*** `
-e tokenGeneratorUserPwd=*** `
-e env=*** `
-e toAddress=*** `
-e emailRecipient=*** `
-e ninRecipient=*** `
-e runFullTestSet=true
```

The comand consists of three sections
The command consists of three parts

`podman compose run` to run the test in a docker container

`k6 run {path to test file}` pointing to the test file you want to run e.g. `/src/tests/orders_email.js.js`
`k6 run {path to test file}` pointing to the test file you want to run e.g. `/src/tests/orders_email.js`

The last part is all script parameters provided as environment variables for the container.
```
-e tokenGeneratorUserName=autotest
-e tokenGeneratorUserName=***
-e tokenGeneratorUserPwd=***
-e env=***
-e toAddress=***
-e emailRecipient=***
-e ninRecipient=***
-e runFullTestSet=true
````
all environment variables that should be included in the request.
10 changes: 3 additions & 7 deletions test/k6/src/api/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
buildHeaderWithBearer
} from "../apiHelpers.js";
import { platformAuthentication } from "../config.js";
import { stopIterationOnFail, addErrorCount } from "../errorhandler.js";
import { stopIterationOnFail } from "../errorhandler.js";


export function exchangeToAltinnToken(token, test) {
Expand All @@ -17,12 +17,8 @@ export function exchangeToAltinnToken(token, test) {
"// Setup // Authentication towards Altinn 3 Success": (r) =>
r.status === 200,
});
addErrorCount(success);
stopIterationOnFail(
"// Setup // Authentication towards Altinn 3 Failed",
success,
res
);

stopIterationOnFail("// Setup // Authentication towards Altinn 3 Failed", success);

return res.body;
}
10 changes: 3 additions & 7 deletions test/k6/src/api/maskinporten.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import KJUR from "https://unpkg.com/[email protected]/lib/jsrsasign.js";

import { buildHeaderWithContentType} from "../apiHelpers.js";
import * as config from "../config.js";
import { stopIterationOnFail, addErrorCount } from "../errorhandler.js";
import { stopIterationOnFail } from "../errorhandler.js";

const encodedJwk = __ENV.encodedJwk;
const mpClientId = __ENV.mpClientId;
Expand Down Expand Up @@ -40,12 +40,8 @@ export function generateAccessToken(scopes) {
"// Setup // Authentication towards Maskinporten Success": (r) =>
r.status === 200,
});
addErrorCount(success);
stopIterationOnFail(
"// Setup // Authentication towards Maskinporten Failed",
success,
res
);

stopIterationOnFail("// Setup // Authentication towards Maskinporten Failed", success);

let accessToken = JSON.parse(res.body)['access_token'];
return accessToken;
Expand Down
2 changes: 1 addition & 1 deletion test/k6/src/api/token-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function generateToken(endpoint) {
var response = http.get(endpoint, params);

if (response.status != 200) {
stopIterationOnFail("Token generation failed", false, response);
stopIterationOnFail("Token generation failed", false);
}

var token = response.body;
Expand Down
23 changes: 5 additions & 18 deletions test/k6/src/errorhandler.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
import { Counter } from "k6/metrics";
import { fail } from "k6";

let ErrorCount = new Counter("errors");

//Adds a count to the error counter when value of success is false
export function addErrorCount(success) {
if (!success) {
ErrorCount.add(1);
}
}

/**
* Stops k6 iteration when success is false and prints test name with response code
* @param {String} testName
* @param {boolean} success
* @param {JSON} res
* @param {String} failReason The reason for stopping the tests
* @param {boolean} success The result of a check
*/
export function stopIterationOnFail(testName, success, res) {
if (!success && res != null) {
fail(testName + ": Response code: " + res.status + ". Response message: " + res.body);
} else if (!success) {
fail(testName);
export function stopIterationOnFail(failReason, success) {
if (!success) {
fail(failReason);
}
}
Loading

0 comments on commit 39c74d5

Please sign in to comment.