Skip to content

Commit

Permalink
Added refactored testContainers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMedek committed Nov 27, 2023
1 parent 04f8923 commit 2ab5321
Show file tree
Hide file tree
Showing 16 changed files with 629 additions and 0 deletions.
12 changes: 12 additions & 0 deletions debezium-server-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<name>Debezium Server Redis Sink Adapter</name>
<packaging>jar</packaging>

<properties>
<quarkus.jib.jvm-entrypoint>java</quarkus.jib.jvm-entrypoint>
</properties>

<dependencies>
<dependency>
<groupId>io.debezium</groupId>
Expand All @@ -28,6 +32,10 @@
</dependency>

<!-- Testing -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-container-image-jib</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
Expand Down Expand Up @@ -143,6 +151,10 @@
<test.type>IT</test.type>
</systemProperties>
<runOrder>${runOrder}</runOrder>
<systemPropertyVariables>
<test.version.debezium>${version.debezium}</test.version.debezium>
<test.server.image.group>${quarkus.container-image.group}</test.server.image.group>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.Map;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.debezium.connector.postgresql.connection.PostgresConnection;
Expand All @@ -26,6 +27,7 @@
*
* @author Oren Elias
*/
@Disabled
@QuarkusIntegrationTest
@TestProfile(RedisOffsetTestProfile.class)
@QuarkusTestResource(RedisTestResourceLifecycleManager.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.Map;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusIntegrationTest;
Expand All @@ -22,6 +23,7 @@
*
* @author Oren Elias
*/
@Disabled
@QuarkusIntegrationTest
@TestProfile(RedisSSLStreamTestProfile.class)
public class RedisSSLStreamIT {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.RetryingTest;

Expand All @@ -37,6 +38,7 @@
*
* @author Oren Elias
*/
@Disabled
@QuarkusIntegrationTest
@TestProfile(RedisSchemaHistoryTestProfile.class)
@QuarkusTestResource(RedisTestResourceLifecycleManager.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.debezium.connector.postgresql.connection.PostgresConnection;
Expand All @@ -31,6 +32,7 @@
* @author M Sazzadul Hoque
* @author Yossi Shirizli
*/
@Disabled
@QuarkusIntegrationTest
@TestProfile(RedisStreamTestProfile.class)
@QuarkusTestResource(RedisTestResourceLifecycleManager.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static org.junit.Assert.assertTrue;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.debezium.connector.postgresql.connection.PostgresConnection;
Expand All @@ -18,6 +19,7 @@
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;

@Disabled
@QuarkusIntegrationTest
@TestProfile(RedisStreamMemoryThresholdTestProfile.class)
@QuarkusTestResource(RedisTestResourceLifecycleManager.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;

import io.debezium.util.Testing;
Expand All @@ -28,6 +29,7 @@
*
* @author ggaborg
*/
@Disabled
@QuarkusIntegrationTest
@TestProfile(RedisStreamMessageTestProfile.class)
@QuarkusTestResource(RedisTestResourceLifecycleManager.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.server.redis.wip;

import static io.debezium.server.redis.wip.TestConstants.MYSQL_DATABASE;
import static io.debezium.server.redis.wip.TestConstants.MYSQL_PASSWORD;
import static io.debezium.server.redis.wip.TestConstants.MYSQL_PORT;
import static io.debezium.server.redis.wip.TestConstants.MYSQL_USER;
import static io.debezium.server.redis.wip.TestConstants.REDIS_PORT;
import static io.debezium.server.redis.wip.TestConstants.REDIS_SSL_PORT;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class DebeziumServerConfigBuilder {
private final Map<String, String> config = new HashMap<>();

public DebeziumServerConfigBuilder withValue(String key, String value) {
config.put(key, value);
return this;
}

public List<String> build() {
return config
.entrySet()
.stream()
.map(e -> e.getKey() + "=" + e.getValue())
.collect(Collectors.toList());
}

public static Map<String, String> baseRedisConfig(DebeziumTestContainerWrapper redis) {
return Map.of(
"debezium.sink.type", "redis",
"debezium.sink.redis.address", redis.getContainerAddress() + ":" + REDIS_PORT);
}

public static Map<String, String> baseSslRedisConfig(DebeziumTestContainerWrapper redis) {
return Map.of(
"debezium.sink.type", "redis",
"debezium.sink.redis.address", redis.getContainerAddress() + ":" + REDIS_SSL_PORT,
"debezium.sink.redis.ssl.enabled", "true",
"debezium.source.database.ssl.mode", "disabled",
"debezium.source.offset.storage", "io.debezium.server.redis.RedisOffsetBackingStore");
}

public Map<String, String> baseMySqlConfig(DebeziumTestContainerWrapper mysql) {
Map<String, String> result = new HashMap<>(Map.of("debezium.source.connector.class", "io.debezium.connector.mysql.MySqlConnector",
"debezium.source.offset.flush.interval.ms", "0",
"debezium.source.topic.prefix", "testc",
"debezium.source.database.dbname", MYSQL_DATABASE,
"debezium.source.database.hostname", String.valueOf(mysql.getContainerAddress()),
"debezium.source.database.port", String.valueOf(MYSQL_PORT),
"debezium.source.database.user", MYSQL_USER,
"debezium.source.database.password", MYSQL_PASSWORD,
"debezium.source.database.server.id", "1",
"debezium.source.schema.history.internal", "io.debezium.server.redis.RedisSchemaHistory"));
result.put("debezium.source.offset.storage.file.filename", "offset.dat");
return result;
}

public DebeziumServerConfigBuilder withBaseMySqlRedisConfig(DebeziumTestContainerWrapper redis, DebeziumTestContainerWrapper mysql) {
config.putAll(baseRedisConfig(redis));
config.putAll(baseMySqlConfig(mysql));
return this;
}

public DebeziumServerConfigBuilder withBaseMysqlSslRedisConfig(DebeziumTestContainerWrapper redis, DebeziumTestContainerWrapper mysql) {
config.putAll(baseSslRedisConfig(redis));
config.putAll(baseMySqlConfig(mysql));
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.server.redis.wip;

import static org.awaitility.Awaitility.await;
import static org.testcontainers.containers.output.OutputFrame.OutputType.STDOUT;

import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.output.OutputFrame;

import lombok.NonNull;

public class DebeziumTestContainerWrapper extends GenericContainer<DebeziumTestContainerWrapper> {

private static final Logger LOGGER = LoggerFactory.getLogger(DebeziumTestContainerWrapper.class);

private String networkAlias;

public DebeziumTestContainerWrapper(@NonNull String dockerImageName) {
super(dockerImageName);
}

public void pause() {
LOGGER.info("Pausing container " + getContainerName());
getDockerClient().pauseContainerCmd(getContainerId()).exec();
}

public void unpause() {
LOGGER.info("Unpausing container " + getContainerName());
getDockerClient().unpauseContainerCmd(getContainerId()).exec();
}

public DebeziumTestContainerWrapper withNetworkAlias(String alias) {
this.networkAlias = alias;
return super.withNetworkAliases(alias);
}

public String getContainerAddress() {
return networkAlias;
}

public String getStandardOutput() {
return getLogs(STDOUT);
}

public void waitForContainerLog(String log) {
await()
.atMost(60, TimeUnit.SECONDS)
.until(() -> getLogs(OutputFrame.OutputType.STDOUT).contains(log));
}

public void waitForStop() {
await()
.atMost(60, TimeUnit.SECONDS)
.until(() -> !isRunning());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.server.redis.wip;

public class TestConstants {
private TestConstants() {
// intentionally private
}

public static final int INITIAL_CUSTOMER_COUNT = 4;
public static final int INITIAL_SCHEMA_HISTORY_SIZE = 16;
public static final String LOCALHOST = "localhost";

// REDIS
public static final String REDIS_IMAGE = "redis";
public static final int REDIS_PORT = 6379;
public static final int REDIS_SSL_PORT = 6378;

// MYSQL
public static final String MYSQL_USER = "debezium";
public static final String MYSQL_PASSWORD = "dbz";
public static final String MYSQL_ROOT_PASSWORD = "debezium";
public static final String MYSQL_DATABASE = "inventory";
public static final String MYSQL_PRIVILEGED_USER = "mysqluser";
public static final String MYSQL_PRIVILEGED_PASSWORD = "mysqlpassword";
public static final int MYSQL_PORT = 3306;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.server.redis.wip;

import static io.debezium.server.redis.wip.TestConstants.LOCALHOST;
import static io.debezium.server.redis.wip.TestConstants.MYSQL_PORT;
import static io.debezium.server.redis.wip.TestConstants.MYSQL_PRIVILEGED_PASSWORD;
import static io.debezium.server.redis.wip.TestConstants.MYSQL_PRIVILEGED_USER;
import static io.debezium.server.redis.wip.TestConstants.MYSQL_ROOT_PASSWORD;
import static io.debezium.server.redis.wip.TestConstants.REDIS_IMAGE;
import static io.debezium.server.redis.wip.TestConstants.REDIS_PORT;
import static io.debezium.server.redis.wip.TestProperties.DEBEZIUM_SERVER_IMAGE;

import java.time.Duration;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;

public class TestContainersRedisTestBase {
protected DebeziumTestContainerWrapper redis;
protected DebeziumTestContainerWrapper server;
protected DebeziumTestContainerWrapper mysql;
protected Jedis jedis;

public TestContainersRedisTestBase() {
// provide base configuration for all components
Network network = Network.newNetwork();

mysql = new DebeziumTestContainerWrapper("quay.io/debezium/example-mysql")
.withNetwork(network)
.withNetworkAlias("mysql")
.waitingFor(Wait.forLogMessage(".*mysqld: ready for connections.*", 2))
.withEnv("MYSQL_ROOT_PASSWORD", MYSQL_ROOT_PASSWORD)
.withEnv("MYSQL_USER", MYSQL_PRIVILEGED_USER)
.withEnv("MYSQL_PASSWORD", MYSQL_PRIVILEGED_PASSWORD)
.withExposedPorts(MYSQL_PORT)
.withStartupTimeout(Duration.ofSeconds(180));
redis = new DebeziumTestContainerWrapper(REDIS_IMAGE)
.withClasspathResourceMapping("redis", "/etc/redis", BindMode.READ_ONLY)
.withNetwork(network)
.withNetworkAlias("redis")
.withExposedPorts(REDIS_PORT);
server = new DebeziumTestContainerWrapper(DEBEZIUM_SERVER_IMAGE)
.withNetwork(network)
.withCommand("-jar", "quarkus-run.jar");
}

@BeforeEach
public void setUp() {
mysql.start();
redis.start();
jedis = new Jedis(HostAndPort.from(LOCALHOST + ":" + redis.getMappedPort(REDIS_PORT)));
}

@AfterEach
public void tearDown() {
server.stop();
mysql.stop();
redis.stop();
}

protected void startServerWithEnv(List<String> env) {
server.setEnv(env);
server.start();
}

}
Loading

0 comments on commit 2ab5321

Please sign in to comment.