Skip to content

Commit

Permalink
Updates from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
javaducky committed Feb 22, 2024
1 parent c74e506 commit cdc6505
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 70 deletions.
16 changes: 2 additions & 14 deletions docs/modules/k6.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
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.
Using this module, you can execute k6 test scripts, optionally building a custom `k6` binary with [k6 extensions](https://grafana.com/docs/k6/latest/extensions/).

## Basic script execution

Expand All @@ -19,19 +20,6 @@ Create a simple k6 test script to be executed as part of your tests:
[Content of `scripts/test.js`](../../modules/k6/src/test/resources/scripts/test.js) inside_block:access_script_vars
<!--/codeinclude-->

## Customizing k6 using extensions

The extensibility of k6 allows for the creation of custom functionality via [extensions](https://grafana.com/docs/k6/latest/extensions/).
Using the extended k6 image enables scripts to reference extension libraries which will dynamically be compiled into the k6 binary.

!!! warning
Many available extensions as well as the [builder image](https://github.com/szkiba/k6x) are provided by third parties and may not be supported directly by Grafana Labs.

<!--codeinclude-->
[Setup the container](../../modules/k6/src/test/java/org/testcontainers/k6/K6ContainerTests.java) inside_block:extended_k6
[Content of `scripts/extensions.js`](../../modules/k6/src/test/resources/scripts/extensions.js) inside_block:extensions_script
<!--/codeinclude-->

## Adding this module to your project dependencies

Add the following dependency to your `pom.xml`/`build.gradle` file:
Expand Down
29 changes: 10 additions & 19 deletions modules/k6/src/main/java/org/testcontainers/k6/K6Container.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.testcontainers.k6;

import org.apache.commons.io.FilenameUtils;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
import org.testcontainers.utility.MountableFile;

Expand All @@ -14,13 +14,7 @@
public class K6Container extends GenericContainer<K6Container> {

/** Standard image for k6, as provided by Grafana. */
public static final DockerImageName K6_IMAGE = DockerImageName.parse("grafana/k6:0.49.0");

/**
* Extended image allowing for dynamic inclusion of k6 Extensions.
* @see <a href="https://grafana.com/docs/k6/latest/extensions/">k6 Extensions</a>
*/
public static final DockerImageName K6_BUILDER_IMAGE = DockerImageName.parse("szkiba/k6x:v0.4.0");
private static final DockerImageName K6_IMAGE = DockerImageName.parse("grafana/k6");

private String testScript;

Expand All @@ -29,31 +23,28 @@ public class K6Container extends GenericContainer<K6Container> {
private Map<String, String> scriptVars = new HashMap<>();

/**
* Creates a new container instance based upon the {@link #K6_IMAGE}.
* Creates a new container instance based upon the provided image name.
*/
public K6Container() {
this(K6_IMAGE);
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, K6_BUILDER_IMAGE);

setWaitStrategy(Wait.defaultWaitStrategy());
dockerImageName.assertCompatibleWith(K6_IMAGE);
}

/**
* Specifies the test script to be executed within the container.
* @param scriptPath location of script to be copied into the container
* @param testScript file to be copied into the container
* @return the builder
*/
public K6Container withTestScript(String scriptPath) {
this.testScript = "/home/k6/" + scriptPath;
final MountableFile mountableFile = MountableFile.forClasspathResource(scriptPath);
withCopyFileToContainer(mountableFile, this.testScript);
public K6Container withTestScript(MountableFile testScript) {
this.testScript = "/home/k6/" + FilenameUtils.getName(testScript.getResolvedPath());
withCopyFileToContainer(testScript, this.testScript);
return self();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.Test;
import org.testcontainers.containers.output.WaitingConsumer;
import org.testcontainers.utility.MountableFile;

import java.util.concurrent.TimeUnit;

Expand All @@ -14,8 +15,9 @@ public void k6StandardTest() throws Exception {
try (
// standard_k6 {
K6Container container =
new K6Container()
.withTestScript("scripts/test.js")
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")
Expand All @@ -34,27 +36,4 @@ public void k6StandardTest() throws Exception {
}
}

@Test
public void k6ExtendedTest() throws Exception {
try (
// extended_k6 {
K6Container container =
new K6Container(K6Container.K6_BUILDER_IMAGE)
.withTestScript("scripts/extensions.js")
.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 ->
frame.getUtf8String().contains("iteration_duration"), 5, TimeUnit.MINUTES);

assertThat(container.getLogs()).contains("k6 tests extended");
}
}

}
12 changes: 0 additions & 12 deletions modules/k6/src/test/resources/scripts/extensions.js

This file was deleted.

0 comments on commit cdc6505

Please sign in to comment.