Skip to content

Commit

Permalink
[0.6.1-SNAPSHOT]
Browse files Browse the repository at this point in the history
Alias & Image empty value fixed
  • Loading branch information
GoodforGod committed Aug 25, 2023
1 parent 53118da commit 8e2b935
Show file tree
Hide file tree
Showing 31 changed files with 66 additions and 59 deletions.
2 changes: 1 addition & 1 deletion cassandra/README.md
Original file line number Diff line number Diff line change
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,7 +85,7 @@ protected Optional<CassandraMetadata> findMetadata(@NotNull ExtensionContext con
@NotNull
protected CassandraConnection getConnectionForContainer(CassandraMetadata metadata,
@NotNull CassandraContainer<?> container) {
final String alias = Optional.ofNullable(metadata.networkAlias())
final String alias = Optional.ofNullable(metadata.networkAliasOrDefault())
.filter(a -> !a.isBlank())
.or(() -> (container.getNetworkAliases().isEmpty())
? Optional.empty()
Expand Down
2 changes: 1 addition & 1 deletion cockroachdb/README.md
Original file line number Diff line number Diff line change
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,7 +74,7 @@ protected Optional<CockroachMetadata> findMetadata(@NotNull ExtensionContext con

@NotNull
protected JdbcConnection getConnectionForContainer(CockroachMetadata metadata, @NotNull CockroachContainer container) {
final String alias = Optional.ofNullable(metadata.networkAlias())
final String alias = Optional.ofNullable(metadata.networkAliasOrDefault())
.filter(a -> !a.isBlank())
.or(() -> (container.getNetworkAliases().isEmpty())
? Optional.empty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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;

Expand All @@ -18,7 +19,8 @@ protected AbstractContainerMetadata(boolean network, String alias, String image,
this.runMode = runMode;
this.alias = Optional.ofNullable(getEnvValue("Alias", alias))
.filter(a -> !a.isBlank())
.orElse(networkAliasDefault());
.orElse(null);
this.aliasOrDefault = Optional.ofNullable(this.alias).orElse(networkAliasDefault());
this.image = Optional.ofNullable(getEnvValue("Image", image))
.filter(a -> !a.isBlank())
.orElseThrow(() -> new IllegalArgumentException(
Expand Down Expand Up @@ -54,9 +56,6 @@ private static String getEnvValue(String name, String envOrValue) {
}
}

@NotNull
protected abstract String networkAliasDefault();

@Override
public boolean networkShared() {
return network;
Expand All @@ -67,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 @@ -260,18 +260,12 @@ private void setupBeforeAll(ExtensionContext context) {
.map(c -> c.getNetwork() == Network.SHARED)
.orElse(metadata.networkShared());

var networkAlias = containerFromField.map(c -> c.getNetworkAliases())
.filter(a -> !a.isEmpty())
.map(a -> a.stream()
.filter(alias -> alias.equals(metadata.networkAlias()))
.findFirst()
.orElse(a.get(0)))
.orElse(metadata.networkAlias());
var networkAlias = 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 @@ -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
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();
}
}
2 changes: 1 addition & 1 deletion kafka/README.md
Original file line number Diff line number Diff line change
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,7 +110,7 @@ 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())
final Properties networkProperties = Optional.ofNullable(metadata.networkAliasOrDefault())
.filter(a -> !a.isBlank())
.or(() -> (container.getNetworkAliases().isEmpty())
? Optional.empty()
Expand Down
2 changes: 1 addition & 1 deletion mariadb/README.md
Original file line number Diff line number Diff line change
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ protected MariaDBContainer<?> getContainerDefault(MariadbMetadata metadata) {
.withPassword("mariadb")
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(MariaDBContainer.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 @@ -80,7 +80,7 @@ protected Optional<MariadbMetadata> findMetadata(@NotNull ExtensionContext conte

@NotNull
protected JdbcConnection getConnectionForContainer(MariadbMetadata metadata, @NotNull MariaDBContainer<?> container) {
final String alias = Optional.ofNullable(metadata.networkAlias())
final String alias = Optional.ofNullable(metadata.networkAliasOrDefault())
.or(() -> (container.getNetworkAliases().isEmpty())
? Optional.empty()
: Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1)))
Expand Down
2 changes: 1 addition & 1 deletion mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ testRuntimeOnly "mysql:mysql-connector-java:8.0.33"
`@TestcontainersMysql` - 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 MysqlMetadata(boolean network, String alias, String image, ContainerMode
}

@Override
protected @NotNull String networkAliasDefault() {
public @NotNull String networkAliasDefault() {
return "mysql-" + System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ protected MySQLContainer<?> getContainerDefault(MysqlMetadata metadata) {
.withPassword("mysql")
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(MySQLContainer.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 @@ -79,7 +79,7 @@ protected Optional<MysqlMetadata> findMetadata(@NotNull ExtensionContext context

@NotNull
protected JdbcConnection getConnectionForContainer(MysqlMetadata metadata, @NotNull MySQLContainer<?> container) {
final String alias = Optional.ofNullable(metadata.networkAlias())
final String alias = Optional.ofNullable(metadata.networkAliasOrDefault())
.or(() -> (container.getNetworkAliases().isEmpty())
? Optional.empty()
: Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1)))
Expand Down
2 changes: 1 addition & 1 deletion oracle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Extension tested against image `gvenzl/oracle-xe:18.4.0-faststart` and driver `c
`@TestcontainersOracle` - 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 OracleMetadata(boolean network, String alias, String image, ContainerMode
}

@Override
protected @NotNull String networkAliasDefault() {
public @NotNull String networkAliasDefault() {
return "oracle-" + System.currentTimeMillis();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ protected OracleContainer getContainerDefault(OracleMetadata metadata) {
.withDatabaseName("oracle")
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(OracleContainer.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 @@ -75,7 +75,7 @@ protected Optional<OracleMetadata> findMetadata(@NotNull ExtensionContext contex

@NotNull
protected JdbcConnection getConnectionForContainer(OracleMetadata metadata, @NotNull OracleContainer container) {
final String alias = Optional.ofNullable(metadata.networkAlias())
final String alias = Optional.ofNullable(metadata.networkAliasOrDefault())
.or(() -> (container.getNetworkAliases().isEmpty())
? Optional.empty()
: Optional.of(container.getNetworkAliases().get(container.getNetworkAliases().size() - 1)))
Expand Down
2 changes: 1 addition & 1 deletion postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ testRuntimeOnly "org.postgresql:postgresql:42.6.0"
`@TestcontainersPostgres` - 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 PostgresMetadata(boolean network, String alias, String image, ContainerMo
}

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

0 comments on commit 8e2b935

Please sign in to comment.