Skip to content

Commit baa8770

Browse files
Add k6 module (#8318)
Co-authored-by: Eddú Meléndez <[email protected]>
1 parent fdcc31c commit baa8770

File tree

13 files changed

+216
-0
lines changed

13 files changed

+216
-0
lines changed

.github/ISSUE_TEMPLATE/bug_report.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ body:
3030
- HiveMQ
3131
- InfluxDB
3232
- K3S
33+
- K6
3334
- Kafka
3435
- LocalStack
3536
- MariaDB

.github/ISSUE_TEMPLATE/enhancement.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ body:
3030
- HiveMQ
3131
- InfluxDB
3232
- K3S
33+
- K6
3334
- Kafka
3435
- LocalStack
3536
- MariaDB

.github/ISSUE_TEMPLATE/feature.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ body:
3030
- HiveMQ
3131
- InfluxDB
3232
- K3S
33+
- K6
3334
- Kafka
3435
- LocalStack
3536
- MariaDB

.github/dependabot.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ updates:
148148
ignore:
149149
- dependency-name: "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml"
150150
update-types: [ "version-update:semver-minor", "version-update:semver-patch" ]
151+
- package-ecosystem: "gradle"
152+
directory: "/modules/k6"
153+
schedule:
154+
interval: "weekly"
155+
open-pull-requests-limit: 10
156+
ignore:
157+
- dependency-name: "com.fasterxml.jackson.dataformat:jackson-dataformat-yaml"
158+
update-types: [ "version-update:semver-minor", "version-update:semver-patch" ]
151159
- package-ecosystem: "gradle"
152160
directory: "/modules/kafka"
153161
schedule:

.github/labeler.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@
8787
- changed-files:
8888
- any-glob-to-any-file:
8989
- modules/k3s/**/*
90+
"modules/k6":
91+
- changed-files:
92+
- any-glob-to-any-file:
93+
- modules/k6/**/*
9094
"modules/kafka":
9195
- changed-files:
9296
- any-glob-to-any-file:

.github/settings.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ labels:
160160
- name: modules/k3s
161161
color: '#006b75'
162162

163+
- name: modules/k6
164+
color: '#006b75'
165+
163166
- name: modules/kafka
164167
color: '#006b75'
165168

docs/modules/k6.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# k6 Module
2+
3+
!!! note
4+
This module is INCUBATING.
5+
While it is ready for use and operational in the current version of Testcontainers, it is possible that it may receive breaking changes in the future.
6+
See [our contributing guidelines](/contributing/#incubating-modules) for more information on our incubating modules policy.
7+
8+
Testcontainers module for [k6](https://registry.hub.docker.com/r/grafana/k6).
9+
10+
[k6](https://k6.io/) is an extensible reliability testing tool built for developer happiness.
11+
12+
## Basic script execution
13+
14+
Execute a simple k6 test script, `test.js`, with commandline options and injected script variable.
15+
16+
Create a simple k6 test script to be executed as part of your tests:
17+
18+
<!--codeinclude-->
19+
[Setup the container](../../modules/k6/src/test/java/org/testcontainers/k6/K6ContainerTests.java) inside_block:standard_k6
20+
[Content of `scripts/test.js`](../../modules/k6/src/test/resources/scripts/test.js) inside_block:access_script_vars
21+
<!--/codeinclude-->
22+
23+
## Adding this module to your project dependencies
24+
25+
Add the following dependency to your `pom.xml`/`build.gradle` file:
26+
27+
=== "Gradle"
28+
```groovy
29+
testImplementation "org.testcontainers:k6:{{latest_version}}"
30+
```
31+
=== "Maven"
32+
```xml
33+
<dependency>
34+
<groupId>org.testcontainers</groupId>
35+
<artifactId>k6</artifactId>
36+
<version>{{latest_version}}</version>
37+
<scope>test</scope>
38+
</dependency>
39+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ nav:
8282
- modules/gcloud.md
8383
- modules/hivemq.md
8484
- modules/k3s.md
85+
- modules/k6.md
8586
- modules/kafka.md
8687
- modules/localstack.md
8788
- modules/milvus.md

modules/k6/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
description = "Testcontainers :: k6"
2+
3+
dependencies {
4+
api project(':testcontainers')
5+
6+
testImplementation 'org.assertj:assertj-core:3.25.2'
7+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.testcontainers.k6;
2+
3+
import org.apache.commons.io.FilenameUtils;
4+
import org.testcontainers.containers.GenericContainer;
5+
import org.testcontainers.utility.DockerImageName;
6+
import org.testcontainers.utility.MountableFile;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
public class K6Container extends GenericContainer<K6Container> {
15+
16+
/** Standard image for k6, as provided by Grafana. */
17+
private static final DockerImageName K6_IMAGE = DockerImageName.parse("grafana/k6");
18+
19+
private String testScript;
20+
21+
private List<String> cmdOptions = new ArrayList<>();
22+
23+
private Map<String, String> scriptVars = new HashMap<>();
24+
25+
/**
26+
* Creates a new container instance based upon the provided image name.
27+
*/
28+
public K6Container(String dockerImageName) {
29+
this(DockerImageName.parse(dockerImageName));
30+
}
31+
32+
/**
33+
* Creates a new container instance based upon the provided image.
34+
*/
35+
public K6Container(DockerImageName dockerImageName) {
36+
super(dockerImageName);
37+
dockerImageName.assertCompatibleWith(K6_IMAGE);
38+
}
39+
40+
/**
41+
* Specifies the test script to be executed within the container.
42+
* @param testScript file to be copied into the container
43+
* @return the builder
44+
*/
45+
public K6Container withTestScript(MountableFile testScript) {
46+
this.testScript = "/home/k6/" + FilenameUtils.getName(testScript.getResolvedPath());
47+
withCopyFileToContainer(testScript, this.testScript);
48+
return self();
49+
}
50+
51+
/**
52+
* Specifies additional command line options to be provided to the k6 command.
53+
* @param options command line options
54+
* @return the builder
55+
*/
56+
public K6Container withCmdOptions(String... options) {
57+
cmdOptions.addAll(Arrays.asList(options));
58+
return self();
59+
}
60+
61+
/**
62+
* Adds a key-value pair for access within test scripts as an environment variable.
63+
* @param key unique identifier for the variable
64+
* @param value value of the variable
65+
* @return the builder
66+
*/
67+
public K6Container withScriptVar(String key, String value) {
68+
scriptVars.put(key, value);
69+
return self();
70+
}
71+
72+
/**
73+
* {@inheritDoc}
74+
*/
75+
@Override
76+
protected void configure() {
77+
List<String> commandParts = new ArrayList<>();
78+
commandParts.add("run");
79+
commandParts.addAll(cmdOptions);
80+
for (Map.Entry<String, String> entry : scriptVars.entrySet()) {
81+
commandParts.add("--env");
82+
commandParts.add(String.format("%s=%s", entry.getKey(), entry.getValue()));
83+
}
84+
commandParts.add(testScript);
85+
86+
setCommand(commandParts.toArray(new String[] {}));
87+
}
88+
}

0 commit comments

Comments
 (0)