Skip to content

Commit

Permalink
Fix Horreum test container
Browse files Browse the repository at this point in the history
  • Loading branch information
barreiro authored and lampajr committed Nov 22, 2024
1 parent 4304c03 commit 9607185
Showing 1 changed file with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
package io.hyperfoil.tools.horreum.infra.common.resources;

import static io.hyperfoil.tools.horreum.infra.common.Const.*;
import static io.hyperfoil.tools.horreum.infra.common.Const.DEFAULT_AMQP_NETWORK_ALIAS;
import static io.hyperfoil.tools.horreum.infra.common.Const.DEFAULT_AMQP_PASSWORD;
import static io.hyperfoil.tools.horreum.infra.common.Const.DEFAULT_AMQP_USERNAME;
import static io.hyperfoil.tools.horreum.infra.common.Const.DEFAULT_HORREUM_NETWORK_ALIAS;
import static io.hyperfoil.tools.horreum.infra.common.Const.HORREUM_DEV_HORREUM_CONTAINER_PORT;
import static io.hyperfoil.tools.horreum.infra.common.Const.HORREUM_DEV_HORREUM_HORREUM_IMAGE;
import static io.hyperfoil.tools.horreum.infra.common.Const.HORREUM_DEV_HORREUM_NETWORK_ALIAS;
import static io.hyperfoil.tools.horreum.infra.common.Const.HORREUM_DEV_KEYCLOAK_NETWORK_ALIAS;
import static io.hyperfoil.tools.horreum.infra.common.Const.HORREUM_DEV_POSTGRES_NETWORK_ALIAS;
import static java.lang.Boolean.parseBoolean;
import static java.time.Duration.ofSeconds;

import java.util.Collections;
import java.util.Map;
Expand All @@ -9,32 +19,38 @@
import org.testcontainers.containers.FixedHostPortGenericContainer;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy;

import io.hyperfoil.tools.horreum.infra.common.HorreumResources;
import io.hyperfoil.tools.horreum.infra.common.ResourceLifecycleManager;

public class HorreumResource implements ResourceLifecycleManager {

private static GenericContainer<?> horreumContainer;
private boolean inContainer = false;

private String networkAlias = "";
private boolean inContainer;
private String networkAlias;

public void init(Map<String, String> initArgs) {
if (!initArgs.containsKey(HORREUM_DEV_HORREUM_HORREUM_IMAGE)) {
throw new RuntimeException("Horreum dev image argument not configured");
}

final String HORREUM_IMAGE = initArgs.get(HORREUM_DEV_HORREUM_HORREUM_IMAGE);
String horreumImage = initArgs.get(HORREUM_DEV_HORREUM_HORREUM_IMAGE);

inContainer = initArgs.containsKey("inContainer") && initArgs.get("inContainer").equals("true");
networkAlias = initArgs.get(HORREUM_DEV_HORREUM_NETWORK_ALIAS);
inContainer = initArgs.containsKey("inContainer") && parseBoolean(initArgs.get("inContainer"));
networkAlias = initArgs.getOrDefault(HORREUM_DEV_HORREUM_NETWORK_ALIAS, DEFAULT_HORREUM_NETWORK_ALIAS);
Network network = HorreumResources.getNetwork();

horreumContainer = (initArgs.containsKey(HORREUM_DEV_HORREUM_CONTAINER_PORT))
? new FixedHostPortGenericContainer<>(HORREUM_IMAGE)
? new FixedHostPortGenericContainer<>(horreumImage)
.withFixedExposedPort(Integer.parseInt(initArgs.get(HORREUM_DEV_HORREUM_CONTAINER_PORT)), 8080)
: new GenericContainer<>(HORREUM_IMAGE).withExposedPorts(8080);
: new GenericContainer<>(horreumImage).withExposedPorts(8080);

// wait for the startup log instead of relying on the port opening
horreumContainer.waitingFor(new LogMessageWaitStrategy()
.withRegEx(".*horreum-backend.*started in.*Listening on.*")
.withStartupTimeout(ofSeconds(60)));

String keycloakHostUrl = initArgs.get("keycloak.host");
String jdbcUrl = initArgs.get("quarkus.datasource.jdbc.url");
if (inContainer) {
Expand Down Expand Up @@ -64,10 +80,10 @@ public void init(Map<String, String> initArgs) {
.withEnv("quarkus.datasource.jdbc.url", jdbcUrl)
.withEnv("quarkus.datasource.migration.jdbc.url", jdbcUrl)
.withEnv("quarkus.datasource.jdbc.additional-jdbc-properties.sslmode", "require")
.withEnv("amqp-host", initArgs.get("amqp.host"))
.withEnv("amqp-port", initArgs.get("amqp.mapped.port"))
.withEnv("amqp-username", initArgs.get("amqp-username"))
.withEnv("amqp-password", initArgs.get("amqp-password"))
.withEnv("amqp-host", initArgs.getOrDefault("amqp.host", DEFAULT_AMQP_NETWORK_ALIAS))
.withEnv("amqp-port", initArgs.getOrDefault("amqp.mapped.port", "5672"))
.withEnv("amqp-username", initArgs.getOrDefault("amqp-username", DEFAULT_AMQP_USERNAME))
.withEnv("amqp-password", initArgs.getOrDefault("amqp-password", DEFAULT_AMQP_PASSWORD))
.withEnv("amqp-reconnect-attempts", "100")
.withEnv("amqp-reconnect-interval", "1000")
.withEnv("quarkus.profile", "test")
Expand All @@ -84,18 +100,16 @@ public Map<String, String> start(Optional<Network> network) {
if (horreumContainer == null) {
return Collections.emptyMap();
}
if (!network.isPresent()) {
if (network.isPresent()) {
horreumContainer.withNetwork(network.get());
horreumContainer.withNetworkAliases(networkAlias);
}

horreumContainer.start();
String horreumContainerName = horreumContainer.getContainerName().replaceAll("/", "");
horreumContainerName = inContainer ? networkAlias : horreumContainerName;
Integer port = horreumContainer.getMappedPort(8080);
String containerName = inContainer ? networkAlias : horreumContainer.getContainerName().replaceAll("/", "");
String port = horreumContainer.getMappedPort(8080).toString();

return Map.of("horreum.container.name", horreumContainerName,
"horreum.container.port", port.toString());
return Map.of("horreum.container.name", containerName, "horreum.container.port", port);
}

@Override
Expand Down

0 comments on commit 9607185

Please sign in to comment.