Skip to content

Commit

Permalink
chore: Fix Kubedock Testcontainers steps
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Oct 26, 2023
1 parent 8494d90 commit 3dab171
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
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_URL} |
| url | ${YAKS_TESTCONTAINERS_POSTGRESQL_LOCAL_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 @@ -83,6 +83,7 @@ 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("app.kubernetes.io/name", "kafka")
.withLabel("app.kubernetes.io/part-of", TestContainersSettings.getTestName())
.withLabel("app.openshift.io/connects-to", TestContainersSettings.getTestId())
Expand Down Expand Up @@ -122,11 +123,16 @@ private void setConnectionSettings(KafkaContainer kafkaContainer, TestContext co
}

String containerId = kafkaContainer.getContainerId().substring(0, 12);
String containerName = kafkaContainer.getContainerName();

if (containerName.startsWith("/")) {
containerName = containerName.substring(1);
}

context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_HOST", kafkaContainer.getHost());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_CONTAINER_IP", kafkaContainer.getHost());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_CONTAINER_ID", containerId);
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_CONTAINER_NAME", kafkaContainer.getContainerName());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_CONTAINER_NAME", containerName);
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_SERVICE_PORT", String.valueOf(kafkaContainer.getMappedPort(KafkaContainer.KAFKA_PORT)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_PORT", String.valueOf(kafkaContainer.getMappedPort(KafkaContainer.KAFKA_PORT)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_LOCAL_BOOTSTRAP_SERVERS", kafkaContainer.getBootstrapServers());
Expand All @@ -135,10 +141,10 @@ private void setConnectionSettings(KafkaContainer kafkaContainer, TestContext co
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_SERVICE_NAME", "kafka");
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_BOOTSTRAP_SERVERS", kafkaContainer.getBootstrapServers());
} else {
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_SERVICE_NAME", String.format("kd-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_BOOTSTRAP_SERVERS", String.format("kd-%s:%s", containerId, kafkaContainer.getMappedPort(KafkaContainer.KAFKA_PORT)));
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_KUBE_DOCK_HOST", String.format("kd-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "KAFKA_KUBE_DOCK_HOST", String.format("yaks-kafka-%s", containerId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ 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("app.kubernetes.io/name", "build")
.withLabel("app.kubernetes.io/part-of", TestContainersSettings.getTestName())
.withLabel("app.openshift.io/connects-to", TestContainersSettings.getTestId())
Expand Down Expand Up @@ -137,11 +138,16 @@ private void exposeConnectionSettings(AWS2Container aws2Container, TestContext c
URI serviceEndpoint = aws2Container.getServiceEndpoint();

String containerId = aws2Container.getContainerId().substring(0, 12);
String containerName = aws2Container.getContainerName();

if (containerName.startsWith("/")) {
containerName = containerName.substring(1);
}

context.setVariable(getEnvVarName("HOST"), aws2Container.getHost());
context.setVariable(getEnvVarName("CONTAINER_IP"), aws2Container.getHost());
context.setVariable(getEnvVarName("CONTAINER_ID"), containerId);
context.setVariable(getEnvVarName("CONTAINER_NAME"), aws2Container.getContainerName());
context.setVariable(getEnvVarName("CONTAINER_NAME"), containerName);
context.setVariable(getEnvVarName("REGION"), aws2Container.getRegion());
context.setVariable(getEnvVarName("ACCESS_KEY"), aws2Container.getAccessKey());
context.setVariable(getEnvVarName("SECRET_KEY"), aws2Container.getSecretKey());
Expand All @@ -152,8 +158,8 @@ private void exposeConnectionSettings(AWS2Container aws2Container, TestContext c
context.setVariable(getEnvVarName("SERVICE_NAME"), "localstack");
context.setVariable(getEnvVarName("SERVICE_URL"), String.format("http://%s:%s", aws2Container.getHostIpAddress(), serviceEndpoint.getPort()));
} else {
context.setVariable(getEnvVarName("SERVICE_NAME"), String.format("kd-%s", containerId));
context.setVariable(getEnvVarName("SERVICE_URL"), String.format("http://kd-%s:%s", containerId, serviceEndpoint.getPort()));
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()));
}

services.forEach(service -> {
Expand All @@ -163,27 +169,26 @@ private void exposeConnectionSettings(AWS2Container aws2Container, TestContext c
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("kd-%s", containerId), getNamespace(context));
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://kd-%s:%s", containerId, serviceEndpoint.getPort()));
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://yaks-aws2-%s:%s", containerId, serviceEndpoint.getPort()));
}
} else {
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://kd-%s:%s", containerId, serviceEndpoint.getPort()));
context.setVariable(getEnvVarName(String.format("%s_URL", serviceName)), String.format("http://yaks-aws2-%s:%s", containerId, 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("KUBE_DOCK_SERVICE_URL"), String.format("http://kd-%s:%s", containerId, serviceEndpoint.getPort()));
context.setVariable(getEnvVarName("KUBE_DOCK_HOST"), String.format("kd-%s", containerId));
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));

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 @@ -83,6 +83,7 @@ public void setEnvSettings(DataTable settings) {
public void startMongo() {
mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo").withTag(mongoDBVersion))
.withLabel("app", "yaks")
.withLabel("com.joyrex2001.kubedock.name-prefix", "yaks-mongodb")
.withLabel("app.kubernetes.io/name", "mongodb")
.withLabel("app.kubernetes.io/part-of", TestContainersSettings.getTestName())
.withLabel("app.openshift.io/connects-to", TestContainersSettings.getTestId())
Expand Down Expand Up @@ -120,10 +121,15 @@ public void stopMongo() {
private void setConnectionSettings(MongoDBContainer mongoDBContainer, TestContext context) {
if (mongoDBContainer.isRunning()) {
String containerId = mongoDBContainer.getContainerId().substring(0, 12);
String containerName = mongoDBContainer.getContainerName();

if (containerName.startsWith("/")) {
containerName = containerName.substring(1);
}

context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_CONTAINER_IP", mongoDBContainer.getHost());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_CONTAINER_ID", containerId);
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_CONTAINER_NAME", mongoDBContainer.getContainerName());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_CONTAINER_NAME", containerName);
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_PORT", mongoDBContainer.getMappedPort(27017));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_LOCAL_URL", mongoDBContainer.getReplicaSetUrl());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_LOCAL_URL", mongoDBContainer.getReplicaSetUrl());
Expand All @@ -133,13 +139,13 @@ private void setConnectionSettings(MongoDBContainer mongoDBContainer, TestContex
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_URL", mongoDBContainer.getReplicaSetUrl());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_URL", mongoDBContainer.getReplicaSetUrl());
} else {
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_NAME", String.format("kd-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_URL", String.format("mongodb://kd-%s:%d/test", containerId, mongoDBContainer.getMappedPort(27017)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_URL", String.format("mongodb://kd-%s:%d/test", containerId, mongoDBContainer.getMappedPort(27017)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_NAME", String.format("yaks-mongodb-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_SERVICE_URL", String.format("mongodb://yaks-mongodb-%s:%d/test", containerId, mongoDBContainer.getMappedPort(27017)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_URL", String.format("mongodb://yaks-mongodb-%s:%d/test", containerId, mongoDBContainer.getMappedPort(27017)));
}


context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_KUBE_DOCK_HOST", String.format("kd-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "MONGODB_KUBE_DOCK_HOST", String.format("yaks-mongodb-%s", containerId));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void startPostgresql() {
.withPassword(password)
.withDatabaseName(databaseName)
.withLabel("app", "yaks")
.withLabel("com.joyrex2001.kubedock.name-prefix", "yaks-postgresql")
.withLabel("app.kubernetes.io/name", "postgresql")
.withLabel("app.kubernetes.io/part-of", TestContainersSettings.getTestName())
.withLabel("app.openshift.io/connects-to", TestContainersSettings.getTestId())
Expand Down Expand Up @@ -169,11 +170,16 @@ private void setConnectionSettings(PostgreSQLContainer<?> postgreSQLContainer, T
}

String containerId = postgreSQLContainer.getContainerId().substring(0, 12);
String containerName = postgreSQLContainer.getContainerName();

if (containerName.startsWith("/")) {
containerName = containerName.substring(1);
}

context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_HOST", postgreSQLContainer.getHost());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_CONTAINER_IP", postgreSQLContainer.getHost());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_CONTAINER_ID", containerId);
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_CONTAINER_NAME", postgreSQLContainer.getContainerName());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_CONTAINER_NAME", containerName);
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_SERVICE_PORT", String.valueOf(postgreSQLContainer.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_PORT", String.valueOf(postgreSQLContainer.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT)));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_USERNAME", postgreSQLContainer.getUsername());
Expand All @@ -188,11 +194,11 @@ private void setConnectionSettings(PostgreSQLContainer<?> postgreSQLContainer, T
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_SERVICE_URL", postgreSQLContainer.getJdbcUrl());
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_URL", postgreSQLContainer.getJdbcUrl());
} else {
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_SERVICE_NAME", String.format("kd-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_SERVICE_URL", String.format("jdbc:postgresql://kd-%s:%s/%s", containerId, postgreSQLContainer.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT), postgreSQLContainer.getDatabaseName()));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_URL", String.format("jdbc:postgresql://kd-%s:%s/%s", containerId, postgreSQLContainer.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT), postgreSQLContainer.getDatabaseName()));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_SERVICE_NAME", String.format("yaks-postgresql-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_SERVICE_URL", String.format("jdbc:postgresql://yaks-postgresql-%s:%s/%s", containerId, postgreSQLContainer.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT), postgreSQLContainer.getDatabaseName()));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_URL", String.format("jdbc:postgresql://yaks-postgresql-%s:%s/%s", containerId, postgreSQLContainer.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT), postgreSQLContainer.getDatabaseName()));
}

context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_KUBE_DOCK_HOST", String.format("kd-%s", containerId));
context.setVariable(TestContainersSteps.TESTCONTAINERS_VARIABLE_PREFIX + "POSTGRESQL_KUBE_DOCK_HOST", String.format("yaks-postgresql-%s", containerId));
}
}
Loading

0 comments on commit 3dab171

Please sign in to comment.