-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Eddú Meléndez <[email protected]>
- Loading branch information
1 parent
fdcc31c
commit baa8770
Showing
13 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ body: | |
- HiveMQ | ||
- InfluxDB | ||
- K3S | ||
- K6 | ||
- Kafka | ||
- LocalStack | ||
- MariaDB | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ body: | |
- HiveMQ | ||
- InfluxDB | ||
- K3S | ||
- K6 | ||
- Kafka | ||
- LocalStack | ||
- MariaDB | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ body: | |
- HiveMQ | ||
- InfluxDB | ||
- K3S | ||
- K6 | ||
- Kafka | ||
- LocalStack | ||
- MariaDB | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# k6 Module | ||
|
||
!!! note | ||
This module is INCUBATING. | ||
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. | ||
See [our contributing guidelines](/contributing/#incubating-modules) for more information on our incubating modules policy. | ||
|
||
Testcontainers module for [k6](https://registry.hub.docker.com/r/grafana/k6). | ||
|
||
[k6](https://k6.io/) is an extensible reliability testing tool built for developer happiness. | ||
|
||
## Basic script execution | ||
|
||
Execute a simple k6 test script, `test.js`, with commandline options and injected script variable. | ||
|
||
Create a simple k6 test script to be executed as part of your tests: | ||
|
||
<!--codeinclude--> | ||
[Setup the container](../../modules/k6/src/test/java/org/testcontainers/k6/K6ContainerTests.java) inside_block:standard_k6 | ||
[Content of `scripts/test.js`](../../modules/k6/src/test/resources/scripts/test.js) inside_block:access_script_vars | ||
<!--/codeinclude--> | ||
|
||
## Adding this module to your project dependencies | ||
|
||
Add the following dependency to your `pom.xml`/`build.gradle` file: | ||
|
||
=== "Gradle" | ||
```groovy | ||
testImplementation "org.testcontainers:k6:{{latest_version}}" | ||
``` | ||
=== "Maven" | ||
```xml | ||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>k6</artifactId> | ||
<version>{{latest_version}}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
description = "Testcontainers :: k6" | ||
|
||
dependencies { | ||
api project(':testcontainers') | ||
|
||
testImplementation 'org.assertj:assertj-core:3.25.2' | ||
} |
88 changes: 88 additions & 0 deletions
88
modules/k6/src/main/java/org/testcontainers/k6/K6Container.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package org.testcontainers.k6; | ||
|
||
import org.apache.commons.io.FilenameUtils; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
import org.testcontainers.utility.MountableFile; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class K6Container extends GenericContainer<K6Container> { | ||
|
||
/** Standard image for k6, as provided by Grafana. */ | ||
private static final DockerImageName K6_IMAGE = DockerImageName.parse("grafana/k6"); | ||
|
||
private String testScript; | ||
|
||
private List<String> cmdOptions = new ArrayList<>(); | ||
|
||
private Map<String, String> scriptVars = new HashMap<>(); | ||
|
||
/** | ||
* Creates a new container instance based upon the provided image name. | ||
*/ | ||
public K6Container(String dockerImageName) { | ||
this(DockerImageName.parse(dockerImageName)); | ||
} | ||
|
||
/** | ||
* Creates a new container instance based upon the provided image. | ||
*/ | ||
public K6Container(DockerImageName dockerImageName) { | ||
super(dockerImageName); | ||
dockerImageName.assertCompatibleWith(K6_IMAGE); | ||
} | ||
|
||
/** | ||
* Specifies the test script to be executed within the container. | ||
* @param testScript file to be copied into the container | ||
* @return the builder | ||
*/ | ||
public K6Container withTestScript(MountableFile testScript) { | ||
this.testScript = "/home/k6/" + FilenameUtils.getName(testScript.getResolvedPath()); | ||
withCopyFileToContainer(testScript, this.testScript); | ||
return self(); | ||
} | ||
|
||
/** | ||
* Specifies additional command line options to be provided to the k6 command. | ||
* @param options command line options | ||
* @return the builder | ||
*/ | ||
public K6Container withCmdOptions(String... options) { | ||
cmdOptions.addAll(Arrays.asList(options)); | ||
return self(); | ||
} | ||
|
||
/** | ||
* Adds a key-value pair for access within test scripts as an environment variable. | ||
* @param key unique identifier for the variable | ||
* @param value value of the variable | ||
* @return the builder | ||
*/ | ||
public K6Container withScriptVar(String key, String value) { | ||
scriptVars.put(key, value); | ||
return self(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
protected void configure() { | ||
List<String> commandParts = new ArrayList<>(); | ||
commandParts.add("run"); | ||
commandParts.addAll(cmdOptions); | ||
for (Map.Entry<String, String> entry : scriptVars.entrySet()) { | ||
commandParts.add("--env"); | ||
commandParts.add(String.format("%s=%s", entry.getKey(), entry.getValue())); | ||
} | ||
commandParts.add(testScript); | ||
|
||
setCommand(commandParts.toArray(new String[] {})); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
modules/k6/src/test/java/org/testcontainers/k6/K6ContainerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.testcontainers.k6; | ||
|
||
import org.junit.Test; | ||
import org.testcontainers.containers.output.WaitingConsumer; | ||
import org.testcontainers.utility.MountableFile; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class K6ContainerTests { | ||
|
||
@Test | ||
public void k6StandardTest() throws Exception { | ||
try ( | ||
// standard_k6 { | ||
K6Container container = new K6Container("grafana/k6:0.49.0") | ||
.withTestScript(MountableFile.forClasspathResource("scripts/test.js")) | ||
.withScriptVar("MY_SCRIPT_VAR", "are cool!") | ||
.withScriptVar("AN_UNUSED_VAR", "unused") | ||
.withCmdOptions("--quiet", "--no-usage-report") | ||
// } | ||
) { | ||
container.start(); | ||
|
||
WaitingConsumer consumer = new WaitingConsumer(); | ||
container.followOutput(consumer); | ||
|
||
// Wait for test script results to be collected | ||
consumer.waitUntil( | ||
frame -> { | ||
return frame.getUtf8String().contains("iteration_duration"); | ||
}, | ||
3, | ||
TimeUnit.SECONDS | ||
); | ||
|
||
assertThat(container.getLogs()).contains("k6 tests are cool!"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<configuration> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<!-- encoders are assigned the type | ||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default --> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} %-5level %logger - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
|
||
<logger name="org.testcontainers" level="INFO"/> | ||
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// access_script_vars { | ||
// The most basic of k6 scripts. | ||
export default function(){ | ||
console.log(`k6 tests ${__ENV.MY_SCRIPT_VAR}`) | ||
} | ||
// } |