-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
359 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 228 additions & 0 deletions
228
src/test/java/org/replicadb/mariadb/MariaDB2PostgresTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
package org.replicadb.mariadb; | ||
|
||
import org.apache.commons.cli.ParseException; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.junit.ClassRule; | ||
import org.junit.jupiter.api.*; | ||
import org.replicadb.ReplicaDB; | ||
import org.replicadb.cli.ReplicationMode; | ||
import org.replicadb.cli.ToolOptions; | ||
import org.replicadb.utils.ScriptRunner; | ||
import org.testcontainers.containers.MariaDBContainer; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.nio.file.Paths; | ||
import java.sql.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@Testcontainers | ||
class MariaDB2PostgresTest { | ||
private static final Logger LOG = LogManager.getLogger(MariaDB2PostgresTest.class); | ||
private static final String RESOURECE_DIR = Paths.get("src", "test", "resources").toFile().getAbsolutePath(); | ||
private static final String REPLICADB_CONF_FILE = "/replicadb.conf"; | ||
private static final String MARIADB_SOURCE_FILE = "/mariadb/mariadb-source.sql"; | ||
private static final String POSTGRES_SINK_FILE = "/sinks/pg-sink.sql"; | ||
private static final String USER_PASSWD_DB = "replicadb"; | ||
|
||
private Connection mariadbConn; | ||
private Connection postgresConn; | ||
|
||
@ClassRule | ||
private static final MariaDBContainer mariadb = new MariaDBContainer("mariadb:10.2") | ||
.withDatabaseName(USER_PASSWD_DB) | ||
.withUsername(USER_PASSWD_DB) | ||
.withPassword(USER_PASSWD_DB); | ||
|
||
@ClassRule | ||
public static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:9.6") | ||
.withDatabaseName(USER_PASSWD_DB) | ||
.withUsername(USER_PASSWD_DB) | ||
.withPassword(USER_PASSWD_DB); | ||
|
||
@BeforeAll | ||
static void setUp() throws SQLException, IOException { | ||
// Start the mariadb container | ||
mariadb.start(); | ||
postgres.start(); | ||
// Create tables | ||
/*MariaDB*/ | ||
Connection con = DriverManager.getConnection(mariadb.getJdbcUrl(), mariadb.getUsername(), mariadb.getPassword()); | ||
ScriptRunner runner = new ScriptRunner(con, false, true); | ||
runner.runScript(new BufferedReader(new FileReader(RESOURECE_DIR + MARIADB_SOURCE_FILE))); | ||
LOG.info("Creating MariaDB source tables"); | ||
con.close(); | ||
/*Postgres*/ | ||
con = DriverManager.getConnection(postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword()); | ||
runner = new ScriptRunner(con, false, true); | ||
runner.runScript(new BufferedReader(new FileReader(RESOURECE_DIR + POSTGRES_SINK_FILE))); | ||
LOG.info("Creating Postgres sink tables"); | ||
con.close(); | ||
} | ||
|
||
@BeforeEach | ||
void before() throws SQLException { | ||
this.mariadbConn = DriverManager.getConnection(mariadb.getJdbcUrl(), mariadb.getUsername(), mariadb.getPassword()); | ||
this.postgresConn = DriverManager.getConnection(postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword()); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() throws SQLException { | ||
// Truncate sink table and close connections | ||
postgresConn.createStatement().execute("TRUNCATE TABLE T_SINK"); | ||
this.mariadbConn.close(); | ||
this.postgresConn.close(); | ||
} | ||
|
||
|
||
public int countSinkRows() throws SQLException { | ||
Statement stmt = postgresConn.createStatement(); | ||
ResultSet rs = stmt.executeQuery("select count(*) from t_sink"); | ||
rs.next(); | ||
int count = rs.getInt(1); | ||
LOG.info(count); | ||
return count; | ||
} | ||
|
||
|
||
@Test | ||
void testMariadbVersion102() throws SQLException { | ||
Statement stmt = mariadbConn.createStatement(); | ||
ResultSet rs = stmt.executeQuery("SELECT VERSION()"); | ||
rs.next(); | ||
String version = rs.getString(1); | ||
LOG.info(version); | ||
assertTrue(version.contains("10.2")); | ||
} | ||
|
||
@Test | ||
void testPostgresConnection() throws SQLException { | ||
Statement stmt = postgresConn.createStatement(); | ||
ResultSet rs = stmt.executeQuery("SELECT 1"); | ||
rs.next(); | ||
String version = rs.getString(1); | ||
LOG.info(version); | ||
assertTrue(version.contains("1")); | ||
} | ||
|
||
@Test | ||
void testMariadbInit() throws SQLException { | ||
Statement stmt = mariadbConn.createStatement(); | ||
ResultSet rs = stmt.executeQuery("select count(*) from t_source"); | ||
rs.next(); | ||
String version = rs.getString(1); | ||
LOG.info(version); | ||
assertTrue(version.contains("4096")); | ||
} | ||
|
||
@Test | ||
void testMariadb2PostgresComplete() throws ParseException, IOException, SQLException { | ||
String[] args = { | ||
"--options-file", RESOURECE_DIR + REPLICADB_CONF_FILE, | ||
"--source-connect", mariadb.getJdbcUrl(), | ||
"--source-user", mariadb.getUsername(), | ||
"--source-password", mariadb.getPassword(), | ||
"--sink-connect", postgres.getJdbcUrl(), | ||
"--sink-user", postgres.getUsername(), | ||
"--sink-password", postgres.getPassword() | ||
}; | ||
ToolOptions options = new ToolOptions(args); | ||
Assertions.assertEquals(0, ReplicaDB.processReplica(options)); | ||
assertEquals(4096,countSinkRows()); | ||
} | ||
|
||
@Test | ||
void testMariadb2PostgresCompleteAtomic() throws ParseException, IOException, SQLException { | ||
String[] args = { | ||
"--options-file", RESOURECE_DIR + REPLICADB_CONF_FILE, | ||
"--source-connect", mariadb.getJdbcUrl(), | ||
"--source-user", mariadb.getUsername(), | ||
"--source-password", mariadb.getPassword(), | ||
"--sink-connect", postgres.getJdbcUrl(), | ||
"--sink-user", postgres.getUsername(), | ||
"--sink-password", postgres.getPassword(), | ||
"--mode", ReplicationMode.COMPLETE_ATOMIC.getModeText() | ||
}; | ||
ToolOptions options = new ToolOptions(args); | ||
assertEquals(0, ReplicaDB.processReplica(options)); | ||
assertEquals(4096,countSinkRows()); | ||
|
||
} | ||
|
||
@Test | ||
void testMariadb2PostgresIncremental() throws ParseException, IOException, SQLException { | ||
String[] args = { | ||
"--options-file", RESOURECE_DIR + REPLICADB_CONF_FILE, | ||
"--source-connect", mariadb.getJdbcUrl(), | ||
"--source-user", mariadb.getUsername(), | ||
"--source-password", mariadb.getPassword(), | ||
"--sink-connect", postgres.getJdbcUrl(), | ||
"--sink-user", postgres.getUsername(), | ||
"--sink-password", postgres.getPassword(), | ||
"--mode", ReplicationMode.INCREMENTAL.getModeText() | ||
}; | ||
ToolOptions options = new ToolOptions(args); | ||
assertEquals(0, ReplicaDB.processReplica(options)); | ||
assertEquals(4096,countSinkRows()); | ||
|
||
} | ||
|
||
@Test | ||
void testMariadb2PostgresCompleteParallel() throws ParseException, IOException, SQLException { | ||
String[] args = { | ||
"--options-file", RESOURECE_DIR + REPLICADB_CONF_FILE, | ||
"--source-connect", mariadb.getJdbcUrl(), | ||
"--source-user", mariadb.getUsername(), | ||
"--source-password", mariadb.getPassword(), | ||
"--sink-connect", postgres.getJdbcUrl(), | ||
"--sink-user", postgres.getUsername(), | ||
"--sink-password", postgres.getPassword(), | ||
"--jobs", "4" | ||
}; | ||
ToolOptions options = new ToolOptions(args); | ||
assertEquals(0, ReplicaDB.processReplica(options)); | ||
assertEquals(4096,countSinkRows()); | ||
} | ||
|
||
@Test | ||
void testMariadb2PostgresCompleteAtomicParallel() throws ParseException, IOException, SQLException { | ||
String[] args = { | ||
"--options-file", RESOURECE_DIR + REPLICADB_CONF_FILE, | ||
"--source-connect", mariadb.getJdbcUrl(), | ||
"--source-user", mariadb.getUsername(), | ||
"--source-password", mariadb.getPassword(), | ||
"--sink-connect", postgres.getJdbcUrl(), | ||
"--sink-user", postgres.getUsername(), | ||
"--sink-password", postgres.getPassword(), | ||
"--mode", ReplicationMode.COMPLETE_ATOMIC.getModeText(), | ||
"--jobs", "4" | ||
}; | ||
ToolOptions options = new ToolOptions(args); | ||
assertEquals(0, ReplicaDB.processReplica(options)); | ||
assertEquals(4096,countSinkRows()); | ||
} | ||
|
||
@Test | ||
void testMariadb2PostgresIncrementalParallel() throws ParseException, IOException, SQLException { | ||
String[] args = { | ||
"--options-file", RESOURECE_DIR + REPLICADB_CONF_FILE, | ||
"--source-connect", mariadb.getJdbcUrl(), | ||
"--source-user", mariadb.getUsername(), | ||
"--source-password", mariadb.getPassword(), | ||
"--sink-connect", postgres.getJdbcUrl(), | ||
"--sink-user", postgres.getUsername(), | ||
"--sink-password", postgres.getPassword(), | ||
"--mode", ReplicationMode.INCREMENTAL.getModeText(), | ||
"--jobs", "4" | ||
}; | ||
ToolOptions options = new ToolOptions(args); | ||
assertEquals(0, ReplicaDB.processReplica(options)); | ||
assertEquals(4096,countSinkRows()); | ||
} | ||
} |
Oops, something went wrong.