|
| 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