Skip to content

Commit

Permalink
chore: Fix Testcontainers service name handling
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Oct 27, 2023
1 parent 3dab171 commit 30cc7bd
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 64 deletions.
2 changes: 1 addition & 1 deletion examples/aws-kinesis/amazonKinesisClient.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import software.amazon.awssdk.services.kinesis.KinesisClient

KinesisClient kinesisClient = KinesisClient
.builder()
.endpointOverride(URI.create("${YAKS_TESTCONTAINERS_LOCALSTACK_KINESIS_LOCAL_URL}"))
.endpointOverride(URI.create("${YAKS_TESTCONTAINERS_LOCALSTACK_KINESIS_URL}"))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(
"${YAKS_TESTCONTAINERS_LOCALSTACK_ACCESS_KEY}",
Expand Down
3 changes: 2 additions & 1 deletion examples/aws-s3/amazonS3Client.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ import software.amazon.awssdk.services.s3.S3Client

S3Client s3 = S3Client
.builder()
.endpointOverride(URI.create("${YAKS_TESTCONTAINERS_LOCALSTACK_S3_LOCAL_URL}".replace('localhost', '127.0.0.1')))
.endpointOverride(URI.create("${YAKS_TESTCONTAINERS_LOCALSTACK_S3_URL}"))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(
"${YAKS_TESTCONTAINERS_LOCALSTACK_ACCESS_KEY}",
"${YAKS_TESTCONTAINERS_LOCALSTACK_SECRET_KEY}")
))
.forcePathStyle(true)
.region(Region.of("${YAKS_TESTCONTAINERS_LOCALSTACK_REGION}"))
.build()

Expand Down
2 changes: 1 addition & 1 deletion examples/postgresql/postgresql.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Feature: PostgreSQL
Scenario: Connect and insert
Given Database connection
| driver | ${YAKS_TESTCONTAINERS_POSTGRESQL_DRIVER} |
| url | ${YAKS_TESTCONTAINERS_POSTGRESQL_LOCAL_URL} |
| url | ${YAKS_TESTCONTAINERS_POSTGRESQL_URL} |
| username | ${YAKS_TESTCONTAINERS_POSTGRESQL_USERNAME} |
| password | ${YAKS_TESTCONTAINERS_POSTGRESQL_PASSWORD} |
When execute SQL update: INSERT INTO todo VALUES (1, 'Write YAKS test', 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class KafkaSettings {
private static final String KAFKA_VERSION_ENV = KAFKA_ENV_PREFIX + "KAFKA_VERSION";
private static final String KAFKA_VERSION_DEFAULT = "7.5.1";

private static final String KAFKA_SERVICE_NAME_PROPERTY = KAFKA_PROPERTY_PREFIX + "service.name";
private static final String KAFKA_SERVICE_NAME_ENV = KAFKA_ENV_PREFIX + "KAFKA_SERVICE_NAME";
private static final String KAFKA_SERVICE_NAME_DEFAULT = "yaks-kafka";

private static final String KAFKA_IMAGE_NAME_PROPERTY = KAFKA_PROPERTY_PREFIX + "image.name";
private static final String KAFKA_IMAGE_NAME_ENV = KAFKA_ENV_PREFIX + "KAFKA_IMAGE_NAME";
private static final String KAFKA_IMAGE_NAME_DEFAULT = "confluentinc/cp-kafka";
Expand Down Expand Up @@ -59,6 +63,15 @@ public static String getVersion() {
System.getenv(KAFKA_VERSION_ENV) != null ? System.getenv(KAFKA_VERSION_ENV) : KAFKA_VERSION_DEFAULT);
}

/**
* Kafka service name.
* @return
*/
public static String getServiceName() {
return System.getProperty(KAFKA_SERVICE_NAME_PROPERTY,
System.getenv(KAFKA_SERVICE_NAME_ENV) != null ? System.getenv(KAFKA_SERVICE_NAME_ENV) : KAFKA_SERVICE_NAME_DEFAULT);
}

/**
* Time in seconds to wait for the container to startup and accept connections.
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.cucumber.java.en.Given;
import org.citrusframework.yaks.YaksSettings;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.utility.DockerImageName;

import static org.citrusframework.container.FinallySequence.Builder.doFinally;
Expand All @@ -54,6 +55,8 @@ public class KafkaSteps {

private int startupTimeout = KafkaSettings.getStartupTimeout();

private String serviceName = KafkaSettings.getServiceName();

private Map<String, String> env = new HashMap<>();

@Before
Expand All @@ -69,6 +72,11 @@ public void setKafkaVersion(String version) {
this.kafkaVersion = version;
}

@Given("^Kafka service name (^\\s+)$")
public void setKafkaServiceName(String serviceName) {
this.serviceName = serviceName;
}

@Given("^Kafka container startup timeout is (\\d+)(?: s| seconds)$")
public void setStartupTimeout(int timeout) {
this.startupTimeout = timeout;
Expand All @@ -83,11 +91,12 @@ public void setEnvSettings(DataTable settings) {
public void startKafka() {
kafkaContainer = new KafkaContainer(DockerImageName.parse(KafkaSettings.getImageName()).withTag(kafkaVersion))
.withLabel("app", "yaks")
.withLabel("com.joyrex2001.kubedock.name-prefix", "yaks-kafka")
.withLabel("com.joyrex2001.kubedock.name-prefix", serviceName)
.withLabel("app.kubernetes.io/name", "kafka")
.withLabel("app.kubernetes.io/part-of", TestContainersSettings.getTestName())
.withLabel("app.openshift.io/connects-to", TestContainersSettings.getTestId())
.withNetworkAliases("kafka")
.withNetwork(Network.newNetwork())
.withNetworkAliases(serviceName)
.withEnv(env)
.withStartupTimeout(Duration.of(startupTimeout, SECONDS));

Expand Down Expand Up @@ -138,13 +147,13 @@ private void setConnectionSettings(KafkaContainer kafkaContainer, TestContext co
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_LOCAL_BOOTSTRAP_SERVERS", kafkaContainer.getBootstrapServers());

if (YaksSettings.isLocal() || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_SERVICE_NAME", "kafka");
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_SERVICE_NAME", serviceName);
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_BOOTSTRAP_SERVERS", kafkaContainer.getBootstrapServers());
} else {
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_SERVICE_NAME", String.format("yaks-kafka-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_BOOTSTRAP_SERVERS", String.format("yaks-kafka-%s:%s", containerId, kafkaContainer.getMappedPort(KafkaContainer.KAFKA_PORT)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_SERVICE_NAME", serviceName);
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_BOOTSTRAP_SERVERS", String.format("%s:%s", serviceName, kafkaContainer.getMappedPort(KafkaContainer.KAFKA_PORT)));
}

context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_KUBE_DOCK_HOST", String.format("yaks-kafka-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_KUBE_DOCK_HOST", serviceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class LocalStackSettings {
private static final String VERSION_ENV = LOCALSTACK_ENV_PREFIX + "VERSION";
public static final String VERSION_DEFAULT = "2.3.2";

private static final String SERVICE_NAME_PROPERTY = LOCALSTACK_PROPERTY_PREFIX + "service.name";
private static final String SERVICE_NAME_ENV = LOCALSTACK_ENV_PREFIX + "SERVICE_NAME";
public static final String SERVICE_NAME_DEFAULT = "yaks-localstack";

private static final String STARTUP_TIMEOUT_PROPERTY = LOCALSTACK_PROPERTY_PREFIX + "startup.timeout";
private static final String STARTUP_TIMEOUT_ENV = LOCALSTACK_ENV_PREFIX + "STARTUP_TIMEOUT";
private static final String STARTUP_TIMEOUT_DEFAULT = "180";
Expand All @@ -46,6 +50,15 @@ public static String getVersion() {
System.getenv(VERSION_ENV) != null ? System.getenv(VERSION_ENV) : VERSION_DEFAULT);
}

/**
* LocalStack service name.
* @return the service name.
*/
public static String getServiceName() {
return System.getProperty(SERVICE_NAME_PROPERTY,
System.getenv(SERVICE_NAME_ENV) != null ? System.getenv(SERVICE_NAME_ENV) : SERVICE_NAME_DEFAULT);
}

/**
* Time in seconds to wait for the container to startup and accept connections.
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,26 @@
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import io.cucumber.datatable.DataTable;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.Given;
import org.citrusframework.Citrus;
import org.citrusframework.TestCaseRunner;
import org.citrusframework.annotations.CitrusFramework;
import org.citrusframework.annotations.CitrusResource;
import org.citrusframework.context.TestContext;
import io.cucumber.datatable.DataTable;
import io.cucumber.java.Before;
import io.cucumber.java.Scenario;
import io.cucumber.java.en.Given;
import org.citrusframework.yaks.YaksSettings;
import org.citrusframework.yaks.kubernetes.KubernetesSettings;
import org.citrusframework.yaks.kubernetes.KubernetesSupport;
import org.citrusframework.yaks.kubernetes.KubernetesVariableNames;
import org.citrusframework.yaks.testcontainers.aws2.AWS2Container;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;

import static org.citrusframework.container.FinallySequence.Builder.doFinally;
import static java.time.temporal.ChronoUnit.SECONDS;
import static org.citrusframework.container.FinallySequence.Builder.doFinally;

public class LocalStackSteps {

Expand All @@ -65,6 +64,8 @@ public class LocalStackSteps {

private Map<String, String> env = new HashMap<>();

private String serviceName = LocalStackSettings.getServiceName();

@Before
public void before(Scenario scenario) {
if (aws2Container == null && citrus.getCitrusContext().getReferenceResolver().isResolvable(AWS2Container.class)) {
Expand All @@ -79,6 +80,11 @@ public void setLocalStackVersion(String version) {
this.localStackVersion = version;
}

@Given("^LocalStack service name (^\\s+)$")
public void setLocalStackServiceName(String serviceName) {
this.serviceName = serviceName;
}

@Given("^LocalStack env settings$")
public void setEnvSettings(DataTable settings) {
this.env.putAll(settings.asMap());
Expand All @@ -99,10 +105,12 @@ public void startLocalStack() {
aws2Container = new AWS2Container(localStackVersion)
.withServices(services.toArray(AWS2Container.AWS2Service[]::new))
.withLabel("app", "yaks")
.withLabel("com.joyrex2001.kubedock.name-prefix", "yaks-aws2")
.withLabel("com.joyrex2001.kubedock.name-prefix", serviceName)
.withLabel("app.kubernetes.io/name", "build")
.withLabel("app.kubernetes.io/part-of", TestContainersSettings.getTestName())
.withLabel("app.openshift.io/connects-to", TestContainersSettings.getTestId())
.withNetwork(Network.newNetwork())
.withNetworkAliases(serviceName)
.withEnv(env)
.waitingFor(Wait.forListeningPort()
.withStartupTimeout(Duration.of(startupTimeout, SECONDS)));
Expand Down Expand Up @@ -155,36 +163,28 @@ private void exposeConnectionSettings(AWS2Container aws2Container, TestContext c
context.setVariable(getEnvVarName("SERVICE_LOCAL_URL"), String.format("http://localhost:%s", serviceEndpoint.getPort()));

if (YaksSettings.isLocal() || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(getEnvVarName("SERVICE_NAME"), "localstack");
context.setVariable(getEnvVarName("SERVICE_URL"), String.format("http://%s:%s", aws2Container.getHostIpAddress(), serviceEndpoint.getPort()));
context.setVariable(getEnvVarName("SERVICE_NAME"), serviceName);
context.setVariable(getEnvVarName("SERVICE_URL"), String.format("http://localhost:%s", serviceEndpoint.getPort()));
} else {
context.setVariable(getEnvVarName("SERVICE_NAME"), String.format("yaks-aws2-%s", containerId));
context.setVariable(getEnvVarName("SERVICE_URL"), String.format("http://yaks-aws2-%s:%s", containerId, serviceEndpoint.getPort()));
context.setVariable(getEnvVarName("SERVICE_NAME"), serviceName);
context.setVariable(getEnvVarName("SERVICE_URL"), String.format("http://%s:%s", serviceName, serviceEndpoint.getPort()));
}

services.forEach(service -> {
String serviceName = service.getServiceName().toUpperCase(Locale.US);
String aws2ServiceName = service.getServiceName().toUpperCase(Locale.US);

if (YaksSettings.isLocal() || !TestContainersSettings.isKubedockEnabled()) {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://%s:%s", aws2Container.getHostIpAddress(), serviceEndpoint.getPort()));
} else if (service == AWS2Container.AWS2Service.S3) {
// Explicitly use cluster IP address in order to enable path-style access on S3 service
Optional<String> clusterIp = KubernetesSupport.getServiceClusterIp(citrus, String.format("yaks-aws2-%s", containerId), getNamespace(context));
if (clusterIp.isPresent()) {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://%s:%s", clusterIp.get(), serviceEndpoint.getPort()));
} else {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://yaks-aws2-%s:%s", containerId, serviceEndpoint.getPort()));
}
context.setVariable(getEnvVarName(String.format("%s_URL", aws2ServiceName)), String.format("http://localhost:%s", serviceEndpoint.getPort()));
} else {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://yaks-aws2-%s:%s", containerId, serviceEndpoint.getPort()));
context.setVariable(getEnvVarName(String.format("%s_URL", aws2ServiceName)), String.format("http://%s:%s", serviceName, serviceEndpoint.getPort()));
}

context.setVariable(getEnvVarName(String.format("%s_LOCAL_URL", serviceName)), String.format("http://localhost:%s", serviceEndpoint.getPort()));
context.setVariable(getEnvVarName(String.format("%s_PORT", serviceName)), serviceEndpoint.getPort());
context.setVariable(getEnvVarName(String.format("%s_LOCAL_URL", aws2ServiceName)), String.format("http://localhost:%s", serviceEndpoint.getPort()));
context.setVariable(getEnvVarName(String.format("%s_PORT", aws2ServiceName)), serviceEndpoint.getPort());
});

context.setVariable(getEnvVarName("KUBE_DOCK_SERVICE_URL"), String.format("http://yaks-aws2-%s:%s", containerId, serviceEndpoint.getPort()));
context.setVariable(getEnvVarName("KUBE_DOCK_HOST"), String.format("yaks-aws2-%s", containerId));
context.setVariable(getEnvVarName("KUBE_DOCK_SERVICE_URL"), String.format("http://%s:%s", serviceName, serviceEndpoint.getPort()));
context.setVariable(getEnvVarName("KUBE_DOCK_HOST"), serviceName);

for (Map.Entry<Object, Object> connectionProperty : aws2Container.getConnectionProperties().entrySet()) {
context.setVariable(connectionProperty.getKey().toString(), connectionProperty.getValue().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class MongoDBSettings {
private static final String VERSION_ENV = MONGODB_ENV_PREFIX + "VERSION";
private static final String VERSION_DEFAULT = "4.0.10";

private static final String SERVICE_NAME_PROPERTY = MONGODB_PROPERTY_PREFIX + "service.name";
private static final String SERVICE_NAME_ENV = MONGODB_ENV_PREFIX + "SERVICE_NAME";
private static final String SERVICE_NAME_DEFAULT = "yaks-mongodb";

private static final String STARTUP_TIMEOUT_PROPERTY = MONGODB_PROPERTY_PREFIX + "startup.timeout";
private static final String STARTUP_TIMEOUT_ENV = MONGODB_ENV_PREFIX + "STARTUP_TIMEOUT";
private static final String STARTUP_TIMEOUT_DEFAULT = "180";
Expand All @@ -46,6 +50,15 @@ public static String getMongoDBVersion() {
System.getenv(VERSION_ENV) != null ? System.getenv(VERSION_ENV) : VERSION_DEFAULT);
}

/**
* MongoDB service name.
* @return
*/
public static String getServiceName() {
return System.getProperty(SERVICE_NAME_PROPERTY,
System.getenv(SERVICE_NAME_ENV) != null ? System.getenv(SERVICE_NAME_ENV) : SERVICE_NAME_DEFAULT);
}

/**
* Time in seconds to wait for the container to startup and accept connections.
* @return
Expand Down
Loading

0 comments on commit 30cc7bd

Please sign in to comment.