Skip to content

Commit

Permalink
fix(tests): refactored tests to isolate setup for each test
Browse files Browse the repository at this point in the history
  • Loading branch information
kirangodishala committed Jun 3, 2024
1 parent 8db476c commit edba620
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,43 +24,44 @@
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

@Testcontainers
public class BaseContainerTest {

private static final Logger logger = LoggerFactory.getLogger(BaseContainerTest.class);

protected static final Network network = Network.newNetwork();
protected final Network network = Network.newNetwork();

protected static GenericContainer clouddriverContainer;
private final int CLOUDDRIVER_PORT = 7002;

protected GenericContainer<?> clouddriverContainer;

private static DockerImageName dockerImageName;

@BeforeAll
static void setupInit() throws Exception {
static void setupInit() {
String fullDockerImageName = System.getenv("FULL_DOCKER_IMAGE_NAME");

// Skip the tests if there's no docker image. This allows gradlew build to work.
assumeTrue(fullDockerImageName != null);

DockerImageName dockerImageName = DockerImageName.parse(fullDockerImageName);

clouddriverContainer =
new GenericContainer(dockerImageName)
.withNetwork(network)
.withExposedPorts(7002)
.waitingFor(Wait.forHealthcheck().withStartupTimeout(Duration.ofSeconds(90)));
dockerImageName = DockerImageName.parse(fullDockerImageName);
}

@BeforeEach
void init(TestInfo testInfo) {
System.out.println("--------------- Test " + testInfo.getDisplayName());
clouddriverContainer =
new GenericContainer(dockerImageName)
.withNetwork(network)
.withExposedPorts(CLOUDDRIVER_PORT)
.waitingFor(Wait.forHealthcheck().withStartupTimeout(Duration.ofSeconds(120)));
}

void testHealthCheck() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,39 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Testcontainers;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public class MySqlContainerTest extends BaseContainerTest {

private static final String MYSQL_NETWORK_ALIAS = "mysqlHost";
private static final Logger logger = LoggerFactory.getLogger(MySqlContainerTest.class);

private static final int MYSQL_PORT = 3306;
private final String MYSQL_NETWORK_ALIAS = "mysqlHost";

private static final Logger logger = LoggerFactory.getLogger(MySqlContainerTest.class);
private final int MYSQL_PORT = 3306;

private static final MySQLContainer<?> mysql =
new MySQLContainer<>("mysql:8.0.37")
.withDatabaseName("clouddriver")
.withUsername("root")
.withPassword("root")
.withNetwork(network)
.withNetworkAliases(MYSQL_NETWORK_ALIAS)
.withInitScript("mysql_init.sql");
private MySQLContainer<?> mysql;

private static String jdbcUrl = "";
private String jdbcUrl = "";

@BeforeAll
static void setup() throws Exception {
@BeforeEach
void setup() throws Exception {
mysql =
new MySQLContainer<>("mysql:8.0.37")
.withDatabaseName("clouddriver")
.withUsername("root")
.withPassword("root")
.withNetwork(network)
.withNetworkAliases(MYSQL_NETWORK_ALIAS)
.withInitScript("mysql_init.sql");
mysql.start();
jdbcUrl = String.format("jdbc:mysql://%s:%d/clouddriver", MYSQL_NETWORK_ALIAS, MYSQL_PORT);
clouddriverContainer
Expand All @@ -58,12 +61,12 @@ static void setup() throws Exception {
.start();

Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);
clouddriverContainer.start();
clouddriverContainer.followOutput(logConsumer);
clouddriverContainer.start();
}

private static String getSpringApplicationJson() throws JsonProcessingException {
logger.info("***** jdbcUrl: '{}'", jdbcUrl);
private String getSpringApplicationJson() throws JsonProcessingException {
logger.info("--------- jdbcUrl: '{}'", jdbcUrl);
Map<String, String> connectionPool =
Map.of("jdbcUrl", jdbcUrl, "user", "clouddriver_service", "password", "c10uddriver");
Map<String, String> migration =
Expand All @@ -86,7 +89,7 @@ private static String getSpringApplicationJson() throws JsonProcessingException
}

@AfterAll
static void cleanupOnce() {
void cleanupOnce() {
if (clouddriverContainer != null) {
clouddriverContainer.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,39 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Testcontainers;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public class PostgresContainerTest extends BaseContainerTest {

private static final String POSTGRES_NETWORK_ALIAS = "postgresHost";
private static final Logger logger = LoggerFactory.getLogger(PostgresContainerTest.class);

private static final int POSTGRES_PORT = 5432;
private final String POSTGRES_NETWORK_ALIAS = "postgresHost";

private static final Logger logger = LoggerFactory.getLogger(PostgresContainerTest.class);
private final int POSTGRES_PORT = 5432;

private static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>("postgres:15")
.withDatabaseName("clouddriver")
.withUsername("postgres")
.withPassword("postgres")
.withNetwork(network)
.withNetworkAliases(POSTGRES_NETWORK_ALIAS)
.withInitScript("postgres_init.sql");
private PostgreSQLContainer<?> postgres;

private static String jdbcUrl = "";
private String jdbcUrl = "";

@BeforeAll
static void setup() throws Exception {
@BeforeEach
void setup() throws Exception {
postgres =
new PostgreSQLContainer<>("postgres:15")
.withDatabaseName("clouddriver")
.withUsername("postgres")
.withPassword("postgres")
.withNetwork(network)
.withNetworkAliases(POSTGRES_NETWORK_ALIAS)
.withInitScript("postgres_init.sql");
postgres.start();
jdbcUrl =
String.format("jdbc:postgresql://%s:%d/clouddriver", POSTGRES_NETWORK_ALIAS, POSTGRES_PORT);
Expand All @@ -59,12 +62,12 @@ static void setup() throws Exception {
.start();

Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);
clouddriverContainer.start();
clouddriverContainer.followOutput(logConsumer);
clouddriverContainer.start();
}

private static String getSpringApplicationJson() throws JsonProcessingException {
logger.info("***** jdbcUrl: '{}'", jdbcUrl);
private String getSpringApplicationJson() throws JsonProcessingException {
logger.info("----------- jdbcUrl: '{}'", jdbcUrl);
Map<String, String> connectionPool =
Map.of("jdbcUrl", jdbcUrl, "user", "clouddriver_service", "password", "c10uddriver");
Map<String, String> migration =
Expand All @@ -87,7 +90,7 @@ private static String getSpringApplicationJson() throws JsonProcessingException
}

@AfterAll
static void cleanupOnce() {
void cleanupOnce() {
if (clouddriverContainer != null) {
clouddriverContainer.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,57 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers
public class RedisContainerTest extends BaseContainerTest {

private static final Logger logger = LoggerFactory.getLogger(RedisContainerTest.class);

private static final String REDIS_NETWORK_ALIAS = "redisHost";
private final String REDIS_NETWORK_ALIAS = "redisHost";

private static final int REDIS_PORT = 6379;
private final int REDIS_PORT = 6379;

private static final GenericContainer redis =
new GenericContainer(DockerImageName.parse("library/redis:5-alpine"))
.withNetwork(network)
.withNetworkAliases(REDIS_NETWORK_ALIAS)
.withExposedPorts(REDIS_PORT);
private GenericContainer<?> redis;

@BeforeAll
static void setup() throws Exception {
@BeforeEach
void setup() throws Exception {
redis =
new GenericContainer<>(DockerImageName.parse("library/redis:5-alpine"))
.withNetwork(network)
.withNetworkAliases(REDIS_NETWORK_ALIAS)
.withExposedPorts(REDIS_PORT);
redis.start();
clouddriverContainer
.dependsOn(redis)
.withEnv("SPRING_APPLICATION_JSON", getSpringApplicationJson())
.start();

Slf4jLogConsumer logConsumer = new Slf4jLogConsumer(logger);
clouddriverContainer.start();
clouddriverContainer.followOutput(logConsumer);
clouddriverContainer.start();
}

private static String getSpringApplicationJson() throws JsonProcessingException {
private String getSpringApplicationJson() throws JsonProcessingException {
String redisUrl = "redis://" + REDIS_NETWORK_ALIAS + ":" + REDIS_PORT;
logger.info("redisUrl: '{}'", redisUrl);
logger.info("----------- redisUrl: '{}'", redisUrl);
Map<String, String> properties =
Map.of("redis.connection", redisUrl, "services.fiat.baseUrl", "http://nowhere");
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(properties);
}

@AfterAll
static void cleanupOnce() {
void cleanupOnce() {
if (clouddriverContainer != null) {
clouddriverContainer.stop();
}
Expand Down

0 comments on commit edba620

Please sign in to comment.