Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[0.6.1] #9

Merged
merged 3 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cassandra/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Features:

**Gradle**
```groovy
testImplementation "io.goodforgod:testcontainers-extensions-cassandra:0.6.0"
testImplementation "io.goodforgod:testcontainers-extensions-cassandra:0.6.1"
```

**Maven**
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>testcontainers-extensions-cassandra</artifactId>
<version>0.6.0</version>
<version>0.6.1</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -63,7 +63,7 @@ testImplementation "com.datastax.oss:java-driver-core:4.17.0"
`@TestcontainersCassandra` - allow **automatically start container** with specified image in different modes without the need to configure it.

Available containers modes:
- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes)
- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes)
- `PER_CLASS` - start new container each *test class*.
- `PER_METHOD` - start new container each *test method*.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class CassandraMetadata extends AbstractContainerMetadata {
}

@Override
protected @NotNull String networkAliasDefault() {
public @NotNull String networkAliasDefault() {
return "cassandra-" + System.currentTimeMillis();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ protected CassandraContainer<?> getContainerDefault(CassandraMetadata metadata)
var container = new CassandraContainer<>(dockerImage)
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(CassandraContainer.class))
.withMdc("image", metadata.image())
.withMdc("alias", metadata.networkAlias()))
.withNetworkAliases(metadata.networkAlias())
.withMdc("alias", metadata.networkAliasOrDefault()))
.withNetworkAliases(metadata.networkAliasOrDefault())
.waitingFor(Wait.forListeningPort())
.withStartupTimeout(Duration.ofMinutes(5));

Expand Down Expand Up @@ -85,11 +85,10 @@ protected Optional<CassandraMetadata> findMetadata(@NotNull ExtensionContext con
@NotNull
protected CassandraConnection getConnectionForContainer(CassandraMetadata metadata,
@NotNull CassandraContainer<?> container) {
final String alias = Optional.ofNullable(metadata.networkAlias())
.filter(a -> !a.isBlank())
.or(() -> (container.getNetworkAliases().isEmpty())
? Optional.empty()
: Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1)))
final String alias = container.getNetworkAliases().stream()
.filter(a -> a.equals(metadata.networkAliasOrDefault()))
.findFirst()
.or(() -> container.getNetworkAliases().stream().findFirst())
.orElse(null);

return CassandraConnectionImpl.forContainer(container.getHost(),
Expand Down
6 changes: 3 additions & 3 deletions cockroachdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Features:

**Gradle**
```groovy
testImplementation "io.goodforgod:testcontainers-extensions-cockroachdb:0.6.0"
testImplementation "io.goodforgod:testcontainers-extensions-cockroachdb:0.6.1"
```

**Maven**
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>testcontainers-extensions-cockroachdb</artifactId>
<version>0.6.0</version>
<version>0.6.1</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -62,7 +62,7 @@ testRuntimeOnly "org.postgresql:postgresql:42.6.0"
`@TestcontainersCockroachdb` - allow **automatically start container** with specified image in different modes without the need to configure it.

Available containers modes:
- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes)
- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes)
- `PER_CLASS` - start new container each *test class*.
- `PER_METHOD` - start new container each *test method*.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ final class CockroachMetadata extends JdbcMetadata {
}

@Override
protected @NotNull String networkAliasDefault() {
public @NotNull String networkAliasDefault() {
return "cockroachdb-" + System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ protected CockroachContainer getContainerDefault(CockroachMetadata metadata) {
var container = new CockroachContainer(dockerImage)
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(CockroachContainer.class))
.withMdc("image", metadata.image())
.withMdc("alias", metadata.networkAlias()))
.withNetworkAliases(metadata.networkAlias())
.withMdc("alias", metadata.networkAliasOrDefault()))
.withNetworkAliases(metadata.networkAliasOrDefault())
.withStartupTimeout(Duration.ofMinutes(5));

if (metadata.networkShared()) {
Expand All @@ -74,11 +74,10 @@ protected Optional<CockroachMetadata> findMetadata(@NotNull ExtensionContext con

@NotNull
protected JdbcConnection getConnectionForContainer(CockroachMetadata metadata, @NotNull CockroachContainer container) {
final String alias = Optional.ofNullable(metadata.networkAlias())
.filter(a -> !a.isBlank())
.or(() -> (container.getNetworkAliases().isEmpty())
? Optional.empty()
: Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1)))
final String alias = container.getNetworkAliases().stream()
.filter(a -> a.equals(metadata.networkAliasOrDefault()))
.findFirst()
.or(() -> container.getNetworkAliases().stream().findFirst())
.orElse(null);

return JdbcConnectionImpl.forJDBC(container.getJdbcUrl(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ public abstract class AbstractContainerMetadata implements ContainerMetadata {

private final boolean network;
private final String alias;
private final String aliasOrDefault;
private final String image;
private final ContainerMode runMode;

protected AbstractContainerMetadata(boolean network, String alias, String image, ContainerMode runMode) {
this.network = network;
this.runMode = runMode;
this.alias = Optional.ofNullable(getEnvValue("Alias", alias)).orElse(networkAliasDefault());
this.image = Optional.ofNullable(getEnvValue("Image", image)).orElseThrow(
() -> new IllegalArgumentException(getClass() + " expected image from '" + image + "' but received null"));
this.alias = Optional.ofNullable(getEnvValue("Alias", alias))
.filter(a -> !a.isBlank())
.orElse(null);
this.aliasOrDefault = Optional.ofNullable(this.alias).orElse(networkAliasDefault());
this.image = Optional.ofNullable(getEnvValue("Image", image))
.filter(a -> !a.isBlank())
.orElseThrow(() -> new IllegalArgumentException(
getClass() + " expected image from '" + image + "' but received null"));
}

private static boolean isEnvironmentValue(String value) {
Expand Down Expand Up @@ -50,9 +56,6 @@ private static String getEnvValue(String name, String envOrValue) {
}
}

@NotNull
protected abstract String networkAliasDefault();

@Override
public boolean networkShared() {
return network;
Expand All @@ -63,6 +66,11 @@ public boolean networkShared() {
return alias;
}

@Override
public @NotNull String networkAliasOrDefault() {
return aliasOrDefault;
}

@Override
public @NotNull String image() {
return image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(image, network, alias);
}

@Override
public String toString() {
return "[image=" + image + ", alias=" + alias + ']';
}
}

static final Map<String, Map<SharedKey, ExtensionContainer<?, ?>>> CLASS_TO_SHARED_CONTAINERS = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -263,10 +268,10 @@ private void setupBeforeAll(ExtensionContext context) {
.orElse(a.get(0)))
.orElse(metadata.networkAlias());

var sharedKey = new SharedKey(imageShared, networkShared, networkAlias);
var sharedContainerMap = CLASS_TO_SHARED_CONTAINERS.computeIfAbsent(getClass().getCanonicalName(),
k -> new ConcurrentHashMap<>());

var sharedKey = new SharedKey(imageShared, networkShared, networkAlias);
var extensionContainer = sharedContainerMap.computeIfAbsent(sharedKey, k -> {
var container = containerFromField.orElseGet(() -> {
logger.debug("Getting default container for image: {}", metadata.image());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public interface ContainerMetadata {
@Nullable
String networkAlias();

@NotNull
String networkAliasDefault();

/**
* @see Network
*/
@NotNull
String networkAliasOrDefault();

@NotNull
String image();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ void firstConnection(@ContainerRedisConnection RedisConnection connection) {
assertNull(firstConnection);
assertNotNull(connection);
assertNotNull(connection.params().uri());
assertTrue(connection.paramsInNetwork().isPresent());
assertNotNull(connection.paramsInNetwork().get().uri());
assertNotNull(connection.paramsInNetwork().get().host());
assertFalse(connection.paramsInNetwork().get().host().isBlank());
assertNotNull(sameConnectionChild);
assertEquals(sameConnectionChild, connection);
assertEquals(sameConnectionChild, sameConnectionParent);
Expand All @@ -45,6 +49,10 @@ void firstConnection(@ContainerRedisConnection RedisConnection connection) {
void secondConnection(@ContainerRedisConnection RedisConnection connection) {
assertNotNull(connection);
assertNotNull(connection.params().uri());
assertTrue(connection.paramsInNetwork().isPresent());
assertNotNull(connection.paramsInNetwork().get().uri());
assertNotNull(connection.paramsInNetwork().get().host());
assertFalse(connection.paramsInNetwork().get().host().isBlank());
assertNotNull(firstConnection);
assertNotNull(sameConnectionChild);
assertEquals(sameConnectionChild, connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ final class RedisMetadata extends AbstractContainerMetadata {
}

@Override
protected @NotNull String networkAliasDefault() {
public @NotNull String networkAliasDefault() {
return "redis-" + System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ protected RedisContainer getContainerDefault(RedisMetadata metadata) {
var container = new RedisContainer(dockerImage)
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(RedisContainer.class))
.withMdc("image", metadata.image())
.withMdc("alias", metadata.networkAlias()))
.withNetworkAliases(metadata.networkAlias())
.withMdc("alias", metadata.networkAliasOrDefault()))
.withNetworkAliases(metadata.networkAliasOrDefault())
.waitingFor(Wait.forListeningPort())
.withStartupTimeout(Duration.ofMinutes(5));

Expand All @@ -67,7 +67,7 @@ protected Optional<RedisMetadata> findMetadata(@NotNull ExtensionContext context

@NotNull
protected RedisConnection getConnectionForContainer(@NotNull RedisMetadata metadata, @NotNull RedisContainer container) {
final String alias = Optional.ofNullable(metadata.networkAlias())
final String alias = Optional.ofNullable(metadata.networkAliasOrDefault())
.filter(a -> !a.isBlank())
.or(() -> container.getNetworkAliases().stream()
.filter(a -> a.startsWith("redis"))
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
groupId=io.goodforgod
artifactRootId=testcontainers-extensions
artifactVersion=0.6.0-SNAPSHOT
artifactVersion=0.6.1-SNAPSHOT


##### GRADLE #####
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ protected PostgreSQLContainer<?> getContainerDefault(PostgresJdbcMetadata metada
.withPassword("postgres")
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(PostgreSQLContainer.class))
.withMdc("image", metadata.image())
.withMdc("alias", metadata.networkAlias()))
.withNetworkAliases(metadata.networkAlias());
.withMdc("alias", metadata.networkAliasOrDefault()))
.withNetworkAliases(metadata.networkAliasOrDefault());
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class PostgresJdbcMetadata extends JdbcMetadata {
}

@Override
protected @NotNull String networkAliasDefault() {
public @NotNull String networkAliasDefault() {
return "postgres-" + System.currentTimeMillis();
}
}
6 changes: 3 additions & 3 deletions kafka/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Features:

**Gradle**
```groovy
testImplementation "io.goodforgod:testcontainers-extensions-kafka:0.6.0"
testImplementation "io.goodforgod:testcontainers-extensions-kafka:0.6.1"
```

**Maven**
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>testcontainers-extensions-kafka</artifactId>
<version>0.6.0</version>
<version>0.6.1</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -65,7 +65,7 @@ testRuntimeOnly "org.apache.kafka:kafka-clients:3.5.1"
`@TestcontainersKafka` - allow **automatically start container** with specified image in different modes without the need to configure it.

Available containers modes:
- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes)
- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes)
- `PER_CLASS` - start new container each *test class*.
- `PER_METHOD` - start new container each *test method*.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class KafkaMetadata extends AbstractContainerMetadata {
}

@Override
protected @NotNull String networkAliasDefault() {
public @NotNull String networkAliasDefault() {
return "kafka-" + System.currentTimeMillis();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ protected KafkaContainer getContainerDefault(KafkaMetadata metadata) {
var container = new KafkaContainer(dockerImage)
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(KafkaContainer.class))
.withMdc("image", metadata.image())
.withMdc("alias", metadata.networkAlias()))
.withMdc("alias", metadata.networkAliasOrDefault()))
.withEnv("KAFKA_CONFLUENT_SUPPORT_METRICS_ENABLE", "false")
.withEnv("AUTO_CREATE_TOPICS", "true")
.withEmbeddedZookeeper()
.withNetworkAliases(metadata.networkAlias())
.withNetworkAliases(metadata.networkAliasOrDefault())
.waitingFor(Wait.forListeningPort())
.withStartupTimeout(Duration.ofMinutes(5));

Expand All @@ -110,11 +110,10 @@ protected KafkaConnection getConnectionForContainer(KafkaMetadata metadata, Kafk
final Properties properties = new Properties();
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, container.getBootstrapServers());

final Properties networkProperties = Optional.ofNullable(metadata.networkAlias())
.filter(a -> !a.isBlank())
.or(() -> (container.getNetworkAliases().isEmpty())
? Optional.empty()
: Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1)))
final Properties networkProperties = container.getNetworkAliases().stream()
.filter(a -> a.equals(metadata.networkAliasOrDefault()))
.findFirst()
.or(() -> container.getNetworkAliases().stream().findFirst())
.map(alias -> {
final Properties props = new Properties();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,
Expand Down
6 changes: 3 additions & 3 deletions mariadb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ Features:

**Gradle**
```groovy
testImplementation "io.goodforgod:testcontainers-extensions-mariadb:0.6.0"
testImplementation "io.goodforgod:testcontainers-extensions-mariadb:0.6.1"
```

**Maven**
```xml
<dependency>
<groupId>io.goodforgod</groupId>
<artifactId>testcontainers-extensions-mariadb</artifactId>
<version>0.6.0</version>
<version>0.6.1</version>
<scope>test</scope>
</dependency>
```
Expand Down Expand Up @@ -62,7 +62,7 @@ testRuntimeOnly "org.mariadb.jdbc:mariadb-java-client:3.1.4"
`@TestcontainersMariadb` - allow **automatically start container** with specified image in different modes without the need to configure it.

Available containers modes:
- `PER_RUN` - start container one time per *test execution*. (Containers should have same image to be reused between test classes)
- `PER_RUN` - start container one time per *test execution*. (Containers must have same `image` and `network` to be reused between test classes)
- `PER_CLASS` - start new container each *test class*.
- `PER_METHOD` - start new container each *test method*.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public MariadbMetadata(boolean network, String alias, String image, ContainerMod
}

@Override
protected @NotNull String networkAliasDefault() {
public @NotNull String networkAliasDefault() {
return "mariadb-" + System.currentTimeMillis();
}
}
Loading