Access files saved by handleSummary in a docker-compose

Access files saved by handleSummary in a docker-compose

I have set up handleSummary in my test script that exports an junit.xml file and running test with docker. Looks like the junit.xml is getting saved inside the docker container.

Is there any possible way to export the file to the host machine ?

Yes, use docker volumes to mount folders on the host OS inside the containers:

tested quickly with the below command which did not save any new files to the host machine. Or the files are saved in the working directory specified in k6 Dockerfile and that has to be mapped to the host machine ?

import http from “k6/http”;
import { check } from “k6”;
import { jUnit, textSummary } from ‘https://jslib.k6.io/k6-summary/0.0.1/index.js’;
export default function () {
check(http.get(“https://test-api.k6.io/”), {
“status is 200”: (r) => r.status == 200
});
}
export function handleSummary(data) {
return {
‘junit.xml’: jUnit(data),
‘summary.json’: JSON.stringify(data),
}
}

docker run -i -v c:/Repos/k6/samples/:/test loadimpact/k6:0.31.1 run /test/http_2.js

Because the Dockerfile WORKDIR is /home/k6, this is where the k6 process is started. So you either have to use the absolute paths in handleSummary() like this:

export function handleSummary(data) {
    console.log("toot");
    return {
        '/test/junit.xml': jUnit(data),
        '/test/summary.json': JSON.stringify(data),
    }
}

Or you have to overwrite the workdir with --workdir "/test"

Also, I am not sure how these permissions work on windows, but keep in mind that for good security practices, the k6 Dockerfile specifies a different user account. So if you get some permission errors, you might want to overwrite this user with the --user CLI flag.

Here’s how I do everything on Linux and your script worked:

docker run -it  --user "$(id -u $USER)" --volume "$(pwd)":/test --workdir "/test" loadimpact/k6:0.31.1 run /test/script.js

Run the k6 docker with the current host user ID and mount the current folder as /test, while also making it the working directory in the container.

1 Like