Skip to content

Instantly share code, notes, and snippets.

@polarnik
Created February 13, 2024 19:51
Show Gist options
  • Save polarnik/701c0c3f79d3d2a2d40ac6a341dca914 to your computer and use it in GitHub Desktop.
Save polarnik/701c0c3f79d3d2a2d40ac6a341dca914 to your computer and use it in GitHub Desktop.
@Testcontainers
@Isolated
@Execution(SAME_THREAD)
@Slf4j
@Getter
@Setter
@TestMethodOrder(MethodOrderer.MethodName.class)
@IndicativeSentencesGeneration(separator = " ➡️ ", generator = CostomNameGenerator.class)
public abstract class AbstractTest {
//...
private GenericContainer getk6(Network network, Logger logger) {
String pathToConfig = System.getProperty("project.projectDir") + "/config";
createDirectoryAndGrandAccess(getPathToResult());
var config = ConfigFactory.create(TestConfig.class);
GenericContainer container = new GenericContainer(DockerImageName.parse(config.k6Image()))
.withEnv("K6_INFLUXDB_USERNAME", config.K6_INFLUXDB_USERNAME())
.withEnv("K6_INFLUXDB_PASSWORD", config.K6_INFLUXDB_PASSWORD())
.withEnv("K6_INFLUXDB_PUSH_INTERVAL", config.K6_INFLUXDB_PUSH_INTERVAL())
.withEnv("K6_NO_USAGE_REPORT", "true")
.withEnv("BUILD_ID", config.BUILD_ID())
.withEnv("JOB_NAME", config.JOB_NAME())
.withEnv("NODE_NAME", config.NODE_NAME())
.withEnv("BUILD_URL", config.BUILD_URL())
.withEnv("SERVER_VERSION", config.SERVER_VERSION())
.withEnv("TEST_CONFIG", getTEST_CONFIG())
.withEnv("TEST_ENVIRONMENT", getTEST_ENVIRONMENT())
.withEnv("TEST_SCIM_GROUP_SIZE", String.valueOf(getTEST_SCIM_GROUP_SIZE()))
.withEnv("TEST_NAME", getTestName())
.withEnv("K6_ITERATIONS", String.valueOf(getK6_ITERATIONS()))
.withEnv("K6_RPS", String.valueOf(getK6_RPS()))
.withEnv("K6_VUS", String.valueOf(getK6_VUS()))
.withEnv("K6_THROW", "true")
.withEnv("TARGET_DURATION", String.valueOf(getTARGET_DURATION()))
.withEnv("K6_SUMMARY_EXPORT", "summary.json")
.withFileSystemBind(getScriptPathInProject(), getScriptPathInDocker(), BindMode.READ_ONLY)
.withFileSystemBind(pathToConfig, "/tmp/config", BindMode.READ_ONLY)
.withFileSystemBind(getPathToResult(), "/tmp/results", BindMode.READ_WRITE)
.withWorkingDirectory("/tmp/results");
if(config.K6_INFLUXDB_ADDRESS().isEmpty()) {
container.withCommand("run" +
" " + getScriptPathInDocker() + "/" + getK6_SCENARIO_PATH());
} else {
container.withCommand("run" +
" --out csv=/tmp/results/metrics.csv" +
" --out influxdb=" + config.K6_INFLUXDB_ADDRESS() +
" " + getScriptPathInDocker() + "/" + getK6_SCENARIO_PATH());
}
if (envs != null && !envs.isEmpty()) {
for (Map.Entry<String, String> envVal : envs.entrySet()) {
container.withEnv(envVal.getKey(), envVal.getValue());
}
}
log.info("scriptPath: {}", getScriptPathInProject());
log.info("scenario: {}", getK6_SCENARIO_PATH());
log.info("/tmp/config {}", pathToConfig);
log.info("/tmp/results {}", getPathToResult());
if (config.USE_PROXY() && network != null) {
container
.withNetwork(network)
.withEnv("NO_PROXY", "raw.githubusercontent.com,jslib.k6.io")
.withEnv("HTTP_PROXY", "mitmproxy:8080")
.withEnv("HTTPS_PROXY", "mitmproxy:8080")
;
}
if (getK6_DURATION_MINUTE() > 0) {
container.withEnv("K6_DURATION", String.valueOf(getK6_DURATION_MINUTE()) + "m");
}
Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);
container.withLogConsumer(logConsumer);
return container;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment