Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#2807]: Improvement(IT): manage mysql containers in ContainerSuite (#2813)

Eric Chang6d598bc2052b
diff --git a/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/integration/test/TestJdbcAbstractIT.java b/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/integration/test/TestJdbcAbstractIT.java
index 9749bab7f2..ec03143b93 100644
--- a/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/integration/test/TestJdbcAbstractIT.java
+++ b/catalogs/catalog-jdbc-common/src/test/java/com/datastrato/gravitino/catalog/jdbc/integration/test/TestJdbcAbstractIT.java
@@ -47,7 +47,7 @@ public abstract class TestJdbcAbstractIT {
protected static final String TEST_DB_NAME = RandomNameUtils.genRandomName("test_db_");
- public static void startup() {
+ public static void startup() throws Exception {
CONTAINER.start();
HashMap<String, String> properties = Maps.newHashMap();
properties.put(JdbcConfig.JDBC_DRIVER.getKey(), CONTAINER.getDriverClassName());
diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/AuditCatalogMysqlIT.java b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/AuditCatalogMysqlIT.java
index 713790d276..ec38006b7e 100644
--- a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/AuditCatalogMysqlIT.java
+++ b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/AuditCatalogMysqlIT.java
@@ -13,10 +13,13 @@ import com.datastrato.gravitino.auth.AuthenticatorType;
import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
import com.datastrato.gravitino.catalog.mysql.integration.test.service.MysqlService;
import com.datastrato.gravitino.client.GravitinoMetalake;
+import com.datastrato.gravitino.integration.test.container.ContainerSuite;
+import com.datastrato.gravitino.integration.test.container.MySQLContainer;
import com.datastrato.gravitino.integration.test.util.AbstractIT;
import com.datastrato.gravitino.integration.test.util.GravitinoITUtils;
import com.datastrato.gravitino.integration.test.util.ITUtils;
import com.datastrato.gravitino.integration.test.util.JdbcDriverDownloader;
+import com.datastrato.gravitino.integration.test.util.TestDatabaseName;
import com.datastrato.gravitino.rel.Column;
import com.datastrato.gravitino.rel.Schema;
import com.datastrato.gravitino.rel.Table;
@@ -26,30 +29,28 @@ import com.google.common.collect.Maps;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.sql.SQLException;
import java.util.Collections;
import java.util.Map;
-import java.util.Random;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
-import org.testcontainers.containers.MySQLContainer;
@Tag("gravitino-docker-it")
public class AuditCatalogMysqlIT extends AbstractIT {
-
+ private static final ContainerSuite containerSuite = ContainerSuite.getInstance();
public static final String metalakeName = GravitinoITUtils.genRandomName("audit_mysql_metalake");
private static final String expectUser = System.getProperty("user.name");
public static final String DOWNLOAD_JDBC_DRIVER_URL =
"https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.27/mysql-connector-java-8.0.27.jar";
- public static final String mysqlImageName = "mysql:8.0";
- protected static final String TEST_DB_NAME = new Random().nextInt(1000) + "_test_db";
+ protected static TestDatabaseName TEST_DB_NAME;
private static final String provider = "jdbc-mysql";
private static MysqlService mysqlService;
- private static MySQLContainer<?> MYSQL_CONTAINER;
+ private static MySQLContainer MYSQL_CONTAINER;
private static GravitinoMetalake metalake;
@BeforeAll
@@ -65,13 +66,10 @@ public class AuditCatalogMysqlIT extends AbstractIT {
JdbcDriverDownloader.downloadJdbcDriver(DOWNLOAD_JDBC_DRIVER_URL, tmpPath.toString());
}
- MYSQL_CONTAINER =
- new MySQLContainer<>(mysqlImageName)
- .withDatabaseName(TEST_DB_NAME)
- .withUsername("root")
- .withPassword("root");
- MYSQL_CONTAINER.start();
- mysqlService = new MysqlService(MYSQL_CONTAINER);
+ containerSuite.startMySQLContainer(TestDatabaseName.MYSQL_AUDIT_CATALOG_MYSQL_IT);
+ MYSQL_CONTAINER = containerSuite.getMySQLContainer();
+ TEST_DB_NAME = TestDatabaseName.MYSQL_AUDIT_CATALOG_MYSQL_IT;
+ mysqlService = new MysqlService(containerSuite.getMySQLContainer(), TEST_DB_NAME);
createMetalake();
}
@@ -80,13 +78,13 @@ public class AuditCatalogMysqlIT extends AbstractIT {
AbstractIT.stopIntegrationTest();
client.dropMetalake(NameIdentifier.of(metalakeName));
mysqlService.close();
- MYSQL_CONTAINER.stop();
}
@Test
public void testAuditCatalog() throws Exception {
String catalogName = GravitinoITUtils.genRandomName("audit_mysql_catalog");
Catalog catalog = createCatalog(catalogName);
+
Assertions.assertEquals(expectUser, catalog.auditInfo().creator());
Assertions.assertEquals(catalog.auditInfo().creator(), catalog.auditInfo().lastModifier());
Assertions.assertEquals(
@@ -144,14 +142,17 @@ public class AuditCatalogMysqlIT extends AbstractIT {
Assertions.assertEquals(expectUser, table.auditInfo().lastModifier());
}
- private static Catalog createCatalog(String catalogName) {
+ private static Catalog createCatalog(String catalogName) throws SQLException {
Map<String, String> catalogProperties = Maps.newHashMap();
catalogProperties.put(
JdbcConfig.JDBC_URL.getKey(),
StringUtils.substring(
- MYSQL_CONTAINER.getJdbcUrl(), 0, MYSQL_CONTAINER.getJdbcUrl().lastIndexOf("/")));
- catalogProperties.put(JdbcConfig.JDBC_DRIVER.getKey(), MYSQL_CONTAINER.getDriverClassName());
+ MYSQL_CONTAINER.getJdbcUrl(TEST_DB_NAME),
+ 0,
+ MYSQL_CONTAINER.getJdbcUrl(TEST_DB_NAME).lastIndexOf("/")));
+ catalogProperties.put(
+ JdbcConfig.JDBC_DRIVER.getKey(), MYSQL_CONTAINER.getDriverClassName(TEST_DB_NAME));
catalogProperties.put(JdbcConfig.USERNAME.getKey(), MYSQL_CONTAINER.getUsername());
catalogProperties.put(JdbcConfig.PASSWORD.getKey(), MYSQL_CONTAINER.getPassword());
diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/CatalogMysqlIT.java b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/CatalogMysqlIT.java
index 944822488e..649d20b510 100644
--- a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/CatalogMysqlIT.java
+++ b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/CatalogMysqlIT.java
@@ -18,10 +18,13 @@ import com.datastrato.gravitino.client.GravitinoMetalake;
import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
import com.datastrato.gravitino.exceptions.NotFoundException;
import com.datastrato.gravitino.exceptions.SchemaAlreadyExistsException;
+import com.datastrato.gravitino.integration.test.container.ContainerSuite;
+import com.datastrato.gravitino.integration.test.container.MySQLContainer;
import com.datastrato.gravitino.integration.test.util.AbstractIT;
import com.datastrato.gravitino.integration.test.util.GravitinoITUtils;
import com.datastrato.gravitino.integration.test.util.ITUtils;
import com.datastrato.gravitino.integration.test.util.JdbcDriverDownloader;
+import com.datastrato.gravitino.integration.test.util.TestDatabaseName;
import com.datastrato.gravitino.rel.Column;
import com.datastrato.gravitino.rel.Column.ColumnImpl;
import com.datastrato.gravitino.rel.Schema;
@@ -46,6 +49,7 @@ import com.google.common.collect.Maps;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
@@ -62,12 +66,11 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.condition.EnabledIf;
-import org.testcontainers.containers.MySQLContainer;
-import org.testcontainers.shaded.org.apache.commons.lang3.RandomUtils;
@Tag("gravitino-docker-it")
@TestInstance(Lifecycle.PER_CLASS)
public class CatalogMysqlIT extends AbstractIT {
+ private static final ContainerSuite containerSuite = ContainerSuite.getInstance();
private static final String provider = "jdbc-mysql";
public static final String DOWNLOAD_JDBC_DRIVER_URL =
"https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.27/mysql-connector-java-8.0.27.jar";
@@ -93,9 +96,9 @@ public class CatalogMysqlIT extends AbstractIT {
private MysqlService mysqlService;
- private MySQLContainer<?> MYSQL_CONTAINER;
+ private MySQLContainer MYSQL_CONTAINER;
- protected final String TEST_DB_NAME = RandomUtils.nextInt(0, 10000) + "_test_db";
+ private TestDatabaseName TEST_DB_NAME;
public static final String defaultMysqlImageName = "mysql:8.0";
@@ -106,7 +109,7 @@ public class CatalogMysqlIT extends AbstractIT {
}
@BeforeAll
- public void startup() throws IOException {
+ public void startup() throws IOException, SQLException {
if (!ITUtils.EMBEDDED_TEST_MODE.equals(testMode)) {
String gravitinoHome = System.getenv("GRAVITINO_HOME");
@@ -114,13 +117,17 @@ public class CatalogMysqlIT extends AbstractIT {
JdbcDriverDownloader.downloadJdbcDriver(DOWNLOAD_JDBC_DRIVER_URL, tmpPath.toString());
}
- MYSQL_CONTAINER =
- new MySQLContainer<>(mysqlImageName)
- .withDatabaseName(TEST_DB_NAME)
- .withUsername("root")
- .withPassword("root");
- MYSQL_CONTAINER.start();
- mysqlService = new MysqlService(MYSQL_CONTAINER);
+ TEST_DB_NAME = TestDatabaseName.MYSQL_CATALOG_MYSQL_IT;
+
+ if (mysqlImageName.equals("mysql:5.7")) {
+ containerSuite.startMySQLVersion5Container(TestDatabaseName.MYSQL_CATALOG_MYSQL_IT);
+ MYSQL_CONTAINER = containerSuite.getMySQLVersion5Container();
+ } else {
+ containerSuite.startMySQLContainer(TEST_DB_NAME);
+ MYSQL_CONTAINER = containerSuite.getMySQLContainer();
+ }
+
+ mysqlService = new MysqlService(MYSQL_CONTAINER, TEST_DB_NAME);
createMetalake();
createCatalog();
createSchema();
@@ -131,11 +138,10 @@ public class CatalogMysqlIT extends AbstractIT {
clearTableAndSchema();
client.dropMetalake(NameIdentifier.of(metalakeName));
mysqlService.close();
- MYSQL_CONTAINER.stop();
}
@AfterEach
- private void resetSchema() {
+ public void resetSchema() {
clearTableAndSchema();
createSchema();
}
@@ -161,14 +167,17 @@ public class CatalogMysqlIT extends AbstractIT {
metalake = loadMetalake;
}
- private void createCatalog() {
+ private void createCatalog() throws SQLException {
Map<String, String> catalogProperties = Maps.newHashMap();
catalogProperties.put(
JdbcConfig.JDBC_URL.getKey(),
StringUtils.substring(
- MYSQL_CONTAINER.getJdbcUrl(), 0, MYSQL_CONTAINER.getJdbcUrl().lastIndexOf("/")));
- catalogProperties.put(JdbcConfig.JDBC_DRIVER.getKey(), MYSQL_CONTAINER.getDriverClassName());
+ MYSQL_CONTAINER.getJdbcUrl(TEST_DB_NAME),
+ 0,
+ MYSQL_CONTAINER.getJdbcUrl(TEST_DB_NAME).lastIndexOf("/")));
+ catalogProperties.put(
+ JdbcConfig.JDBC_DRIVER.getKey(), MYSQL_CONTAINER.getDriverClassName(TEST_DB_NAME));
catalogProperties.put(JdbcConfig.USERNAME.getKey(), MYSQL_CONTAINER.getUsername());
catalogProperties.put(JdbcConfig.PASSWORD.getKey(), MYSQL_CONTAINER.getPassword());
@@ -769,7 +778,8 @@ public class CatalogMysqlIT extends AbstractIT {
"Created by gravitino client",
ImmutableMap.<String, String>builder().build());
- // Try to drop a database, and cascade equals to false, it should not be allowed.
+ // Try to drop a database, and cascade equals to false, it should not be
+ // allowed.
catalog.asSchemas().dropSchema(NameIdentifier.of(metalakeName, catalogName, schemaName), false);
// Check the database still exists
catalog.asSchemas().loadSchema(NameIdentifier.of(metalakeName, catalogName, schemaName));
diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/MysqlTableOperationsIT.java b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/MysqlTableOperationsIT.java
index 0d577eadfe..fd4cf52fa3 100644
--- a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/MysqlTableOperationsIT.java
+++ b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/MysqlTableOperationsIT.java
@@ -37,13 +37,11 @@ import org.junit.jupiter.api.Test;
@Tag("gravitino-docker-it")
public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
-
private static Type VARCHAR = Types.VarCharType.of(255);
private static Type INT = Types.IntegerType.get();
@Test
public void testOperationTable() {
-
String tableName = RandomStringUtils.randomAlphabetic(16) + "_op_table";
String tableComment = "test_comment";
List<JdbcColumn> columns = new ArrayList<>();
@@ -81,7 +79,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
Index[] indexes = new Index[] {Indexes.unique("test", new String[][] {{"col_1"}, {"col_2"}})};
// create table
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
columns.toArray(new JdbcColumn[0]),
tableComment,
@@ -91,17 +89,18 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
indexes);
// list table
- List<String> tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME);
+ List<String> tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME.toString());
Assertions.assertTrue(tables.contains(tableName));
// load table
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
// rename table
String newName = "new_table";
- Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.rename(TEST_DB_NAME, tableName, newName));
- Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.load(TEST_DB_NAME, newName));
+ Assertions.assertDoesNotThrow(
+ () -> TABLE_OPERATIONS.rename(TEST_DB_NAME.toString(), tableName, newName));
+ Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), newName));
// alter table
JdbcColumn newColumn =
@@ -113,7 +112,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
.withDefaultValue(Literals.of("hello test", VARCHAR))
.build();
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
newName,
TableChange.addColumn(
new String[] {newColumn.name()},
@@ -123,7 +122,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
newColumn.defaultValue()),
TableChange.setProperty(MYSQL_ENGINE_KEY, "InnoDB"));
properties.put(MYSQL_ENGINE_KEY, "InnoDB");
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), newName);
List<JdbcColumn> alterColumns =
new ArrayList<JdbcColumn>() {
{
@@ -141,36 +140,40 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
GravitinoRuntimeException gravitinoRuntimeException =
Assertions.assertThrows(
GravitinoRuntimeException.class,
- () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME, newName, setProperty));
+ () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME.toString(), newName, setProperty));
Assertions.assertTrue(
StringUtils.contains(
gravitinoRuntimeException.getMessage(), "Unknown storage engine 'ABC'"));
// delete column
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME, newName, TableChange.deleteColumn(new String[] {newColumn.name()}, true));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, newName);
+ TEST_DB_NAME.toString(),
+ newName,
+ TableChange.deleteColumn(new String[] {newColumn.name()}, true));
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), newName);
assertionsTableInfo(newName, tableComment, columns, properties, indexes, load);
TableChange deleteColumn = TableChange.deleteColumn(new String[] {newColumn.name()}, false);
IllegalArgumentException illegalArgumentException =
Assertions.assertThrows(
IllegalArgumentException.class,
- () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME, newName, deleteColumn));
+ () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME.toString(), newName, deleteColumn));
Assertions.assertEquals(
"Delete column does not exist: " + newColumn.name(), illegalArgumentException.getMessage());
Assertions.assertDoesNotThrow(
() ->
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
newName,
TableChange.deleteColumn(new String[] {newColumn.name()}, true)));
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME, newName, TableChange.deleteColumn(new String[] {newColumn.name()}, true));
- Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));
+ TEST_DB_NAME.toString(),
+ newName,
+ TableChange.deleteColumn(new String[] {newColumn.name()}, true));
+ Assertions.assertDoesNotThrow(() -> TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), newName));
Assertions.assertThrows(
- NoSuchTableException.class, () -> TABLE_OPERATIONS.drop(TEST_DB_NAME, newName));
+ NoSuchTableException.class, () -> TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), newName));
}
@Test
@@ -202,9 +205,9 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
.withComment("name")
.withDefaultValue(Literals.NULL)
.build();
- // `col_1` int NOT NULL COMMENT 'id' ,
- // `col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'name' ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ // `col_1` int NOT NULL COMMENT 'id' ,
+ // `col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'name' ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
columns.add(col_3);
Map<String, String> properties = new HashMap<>();
@@ -215,7 +218,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
};
// create table
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
columns.toArray(new JdbcColumn[0]),
tableComment,
@@ -223,17 +226,18 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
null,
Distributions.NONE,
indexes);
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
assertionsTableInfo(tableName, tableComment, columns, properties, indexes, load);
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
TableChange.updateColumnType(new String[] {col_1.name()}, VARCHAR));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
- // After modifying the type, some attributes of the corresponding column are not supported.
+ // After modifying the type, some attributes of the corresponding column are not
+ // supported.
columns.clear();
col_1 =
JdbcColumn.builder()
@@ -250,15 +254,15 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
String newComment = "new_comment";
// update table comment and column comment
- // `col_1` int NOT NULL COMMENT 'id' ,
- // `col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'new_comment' ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ // `col_1` int NOT NULL COMMENT 'id' ,
+ // `col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'new_comment' ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
TableChange.updateColumnType(new String[] {col_1.name()}, INT),
TableChange.updateColumnComment(new String[] {col_2.name()}, newComment));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
columns.clear();
col_1 =
@@ -288,16 +292,17 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
String newColName_2 = "new_col_2";
// rename column
// update table comment and column comment
- // `new_col_1` int NOT NULL COMMENT 'id' ,
- // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'new_comment' ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ // `new_col_1` int NOT NULL COMMENT 'id' ,
+ // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'new_comment'
+ // ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
TableChange.renameColumn(new String[] {col_1.name()}, newColName_1),
TableChange.renameColumn(new String[] {col_2.name()}, newColName_2));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
columns.clear();
col_1 =
@@ -326,13 +331,13 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
newComment = "txt3";
String newCol2Comment = "xxx";
// update column position 、comment and add column、set table properties
- // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'xxx' ,
- // `new_col_1` int NOT NULL COMMENT 'id' ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
- // `col_4` varchar(255) NOT NULL COMMENT 'txt4' ,
- // `col_5` varchar(255) COMMENT 'hello world' DEFAULT 'hello world' ,
+ // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'xxx' ,
+ // `new_col_1` int NOT NULL COMMENT 'id' ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ // `col_4` varchar(255) NOT NULL COMMENT 'txt4' ,
+ // `col_5` varchar(255) COMMENT 'hello world' DEFAULT 'hello world' ,
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
TableChange.updateColumnPosition(
new String[] {newColName_1}, TableChange.ColumnPosition.after(newColName_2)),
@@ -341,7 +346,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
TableChange.updateColumnComment(new String[] {newColName_2}, newCol2Comment),
TableChange.addColumn(
new String[] {"col_5"}, VARCHAR, "txt5", Literals.of("hello world", VARCHAR)));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
columns.clear();
@@ -374,19 +379,19 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
.build());
assertionsTableInfo(tableName, newComment, columns, properties, indexes, load);
- // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'xxx' ,
- // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
- // `col_4` varchar(255) NULL COMMENT 'txt4' ,
- // `col_5` varchar(255) COMMENT 'hello world' DEFAULT 'hello world' ,
- // `new_col_1` int NOT NULL COMMENT 'id' ,
+ // `new_col_2` varchar(255) NOT NULL DEFAULT 'hello world' COMMENT 'xxx' ,
+ // `col_3` varchar(255) NULL DEFAULT NULL COMMENT 'name' ,
+ // `col_4` varchar(255) NULL COMMENT 'txt4' ,
+ // `col_5` varchar(255) COMMENT 'hello world' DEFAULT 'hello world' ,
+ // `new_col_1` int NOT NULL COMMENT 'id' ,
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
TableChange.updateColumnPosition(new String[] {columns.get(1).name()}, null),
TableChange.updateColumnNullability(
new String[] {columns.get(3).name()}, !columns.get(3).nullable()));
- load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
col_1 = columns.remove(1);
JdbcColumn col3 = columns.remove(1);
JdbcColumn col_4 = columns.remove(1);
@@ -420,7 +425,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
IllegalArgumentException exception =
Assertions.assertThrows(
IllegalArgumentException.class,
- () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME, tableName, updateColumn));
+ () -> TABLE_OPERATIONS.alterTable(TEST_DB_NAME.toString(), tableName, updateColumn));
Assertions.assertTrue(
exception.getMessage().contains("with null default value cannot be changed to not null"));
}
@@ -472,7 +477,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
};
// create table
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
columns.toArray(new JdbcColumn[0]),
tableComment,
@@ -481,11 +486,11 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
Distributions.NONE,
indexes);
- JdbcTable loaded = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ JdbcTable loaded = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
assertionsTableInfo(tableName, tableComment, columns, properties, indexes, loaded);
TABLE_OPERATIONS.alterTable(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
TableChange.updateColumnDefaultValue(
new String[] {columns.get(0).name()},
@@ -498,7 +503,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
TableChange.updateColumnDefaultValue(
new String[] {columns.get(3).name()}, Literals.of("world", Types.VarCharType.of(255))));
- loaded = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ loaded = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
Assertions.assertEquals(
Literals.decimalLiteral(Decimal.of("1.234", 10, 2)), loaded.columns()[0].defaultValue());
Assertions.assertEquals(Literals.longLiteral(1L), loaded.columns()[1].defaultValue());
@@ -556,7 +561,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
};
// create table
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
columns.toArray(new JdbcColumn[0]),
tableComment,
@@ -565,7 +570,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
Distributions.NONE,
indexes);
- JdbcTable loaded = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ JdbcTable loaded = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
assertionsTableInfo(tableName, tableComment, columns, properties, indexes, loaded);
}
@@ -654,7 +659,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
// create table
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
columns.toArray(new JdbcColumn[0]),
tableComment,
@@ -663,7 +668,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
Distributions.NONE,
Indexes.EMPTY_INDEXES);
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
assertionsTableInfo(tableName, tableComment, columns, Collections.emptyMap(), null, load);
}
@@ -698,7 +703,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
IllegalArgumentException.class,
() -> {
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
jdbcCols,
tableComment,
@@ -720,7 +725,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
public void testCreateMultipleTables() {
String test_table_1 = "test_table_1";
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
test_table_1,
new JdbcColumn[] {
JdbcColumn.builder()
@@ -762,7 +767,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
Distributions.NONE,
Indexes.EMPTY_INDEXES);
- tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME);
+ tables = TABLE_OPERATIONS.listTables(TEST_DB_NAME.toString());
Assertions.assertFalse(tables.contains(test_table_2));
}
@@ -770,7 +775,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
public void testLoadTableDefaultProperties() {
String test_table_1 = RandomNameUtils.genRandomName("properties_table_");
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
test_table_1,
new JdbcColumn[] {
JdbcColumn.builder()
@@ -785,7 +790,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
null,
Distributions.NONE,
Indexes.EMPTY_INDEXES);
- JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME, test_table_1);
+ JdbcTable load = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), test_table_1);
Assertions.assertEquals("InnoDB", load.properties().get(MYSQL_ENGINE_KEY));
}
@@ -827,9 +832,16 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
Indexes.unique("uk_1", new String[][] {{"col_1"}})
};
TABLE_OPERATIONS.create(
- TEST_DB_NAME, tableName, columns, comment, properties, null, Distributions.NONE, indexes);
+ TEST_DB_NAME.toString(),
+ tableName,
+ columns,
+ comment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
- JdbcTable table = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ JdbcTable table = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
assertionsTableInfo(
tableName,
comment,
@@ -837,7 +849,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
properties,
indexes,
table);
- TABLE_OPERATIONS.drop(TEST_DB_NAME, tableName);
+ TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), tableName);
// Test create increment key for primary index.
indexes =
@@ -846,9 +858,16 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
Indexes.unique("uk_2", new String[][] {{"col_2"}})
};
TABLE_OPERATIONS.create(
- TEST_DB_NAME, tableName, columns, comment, properties, null, Distributions.NONE, indexes);
+ TEST_DB_NAME.toString(),
+ tableName,
+ columns,
+ comment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
- table = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ table = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
assertionsTableInfo(
tableName,
comment,
@@ -856,14 +875,21 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
properties,
indexes,
table);
- TABLE_OPERATIONS.drop(TEST_DB_NAME, tableName);
+ TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), tableName);
// Test create increment key for col_1 + col_3 uk.
indexes = new Index[] {Indexes.unique("uk_2_3", new String[][] {{"col_1"}, {"col_3"}})};
TABLE_OPERATIONS.create(
- TEST_DB_NAME, tableName, columns, comment, properties, null, Distributions.NONE, indexes);
+ TEST_DB_NAME.toString(),
+ tableName,
+ columns,
+ comment,
+ properties,
+ null,
+ Distributions.NONE,
+ indexes);
- table = TABLE_OPERATIONS.load(TEST_DB_NAME, tableName);
+ table = TABLE_OPERATIONS.load(TEST_DB_NAME.toString(), tableName);
assertionsTableInfo(
tableName,
comment,
@@ -871,7 +897,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
properties,
indexes,
table);
- TABLE_OPERATIONS.drop(TEST_DB_NAME, tableName);
+ TABLE_OPERATIONS.drop(TEST_DB_NAME.toString(), tableName);
// Test create auto increment fail
IllegalArgumentException exception =
@@ -879,7 +905,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
IllegalArgumentException.class,
() ->
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
columns,
comment,
@@ -913,7 +939,7 @@ public class MysqlTableOperationsIT extends TestMysqlAbstractIT {
IllegalArgumentException.class,
() ->
TABLE_OPERATIONS.create(
- TEST_DB_NAME,
+ TEST_DB_NAME.toString(),
tableName,
newColumns,
comment,
diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlAbstractIT.java b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlAbstractIT.java
index eab3d2858d..456f43054c 100644
--- a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlAbstractIT.java
+++ b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/TestMysqlAbstractIT.java
@@ -4,37 +4,61 @@
*/
package com.datastrato.gravitino.catalog.mysql.integration.test;
+import com.datastrato.gravitino.catalog.jdbc.config.JdbcConfig;
import com.datastrato.gravitino.catalog.jdbc.integration.test.TestJdbcAbstractIT;
+import com.datastrato.gravitino.catalog.jdbc.utils.DataSourceUtils;
import com.datastrato.gravitino.catalog.mysql.converter.MysqlColumnDefaultValueConverter;
import com.datastrato.gravitino.catalog.mysql.converter.MysqlExceptionConverter;
import com.datastrato.gravitino.catalog.mysql.converter.MysqlTypeConverter;
import com.datastrato.gravitino.catalog.mysql.operation.MysqlDatabaseOperations;
import com.datastrato.gravitino.catalog.mysql.operation.MysqlTableOperations;
+import com.datastrato.gravitino.integration.test.container.ContainerSuite;
+import com.datastrato.gravitino.integration.test.container.MySQLContainer;
+import com.datastrato.gravitino.integration.test.util.TestDatabaseName;
+import com.google.common.collect.Maps;
+import java.sql.SQLException;
import java.util.Collections;
+import java.util.Map;
+import javax.sql.DataSource;
import org.junit.jupiter.api.BeforeAll;
-import org.testcontainers.containers.MySQLContainer;
public class TestMysqlAbstractIT extends TestJdbcAbstractIT {
-
public static final String defaultMysqlImageName = "mysql:8.0";
+ protected static final ContainerSuite containerSuite = ContainerSuite.getInstance();
+ protected static TestDatabaseName TEST_DB_NAME;
@BeforeAll
- public static void startup() {
- CONTAINER =
- new MySQLContainer<>(defaultMysqlImageName)
- .withDatabaseName(TEST_DB_NAME)
- .withUsername("root")
- .withPassword("root");
+ public static void startup() throws Exception {
+ ContainerSuite containerSuite = ContainerSuite.getInstance();
+ TEST_DB_NAME = TestDatabaseName.MYSQL_MYSQL_ABSTRACT_IT;
+ containerSuite.startMySQLContainer(TEST_DB_NAME);
+ DataSource dataSource = DataSourceUtils.createDataSource(getMySQLCatalogProperties());
+
DATABASE_OPERATIONS = new MysqlDatabaseOperations();
TABLE_OPERATIONS = new MysqlTableOperations();
JDBC_EXCEPTION_CONVERTER = new MysqlExceptionConverter();
- TestJdbcAbstractIT.startup();
- DATABASE_OPERATIONS.initialize(DATA_SOURCE, JDBC_EXCEPTION_CONVERTER, Collections.emptyMap());
+ DATABASE_OPERATIONS.initialize(dataSource, JDBC_EXCEPTION_CONVERTER, Collections.emptyMap());
TABLE_OPERATIONS.initialize(
- DATA_SOURCE,
+ dataSource,
JDBC_EXCEPTION_CONVERTER,
new MysqlTypeConverter(),
new MysqlColumnDefaultValueConverter(),
Collections.emptyMap());
}
+
+ private static Map<String, String> getMySQLCatalogProperties() throws SQLException {
+ Map<String, String> catalogProperties = Maps.newHashMap();
+
+ MySQLContainer mySQLContainer = containerSuite.getMySQLContainer();
+
+ String jdbcUrl = mySQLContainer.getJdbcUrl(TEST_DB_NAME);
+
+ catalogProperties.put(JdbcConfig.JDBC_URL.getKey(), jdbcUrl);
+ catalogProperties.put(
+ JdbcConfig.JDBC_DRIVER.getKey(), mySQLContainer.getDriverClassName(TEST_DB_NAME));
+ catalogProperties.put(JdbcConfig.USERNAME.getKey(), mySQLContainer.getUsername());
+ catalogProperties.put(JdbcConfig.PASSWORD.getKey(), mySQLContainer.getPassword());
+
+ return catalogProperties;
+ }
}
diff --git a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/service/MysqlService.java b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/service/MysqlService.java
index 0205bbdb5f..228bdb2149 100644
--- a/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/service/MysqlService.java
+++ b/catalogs/catalog-jdbc-mysql/src/test/java/com/datastrato/gravitino/catalog/mysql/integration/test/service/MysqlService.java
@@ -8,6 +8,8 @@ import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.catalog.jdbc.JdbcSchema;
import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
+import com.datastrato.gravitino.integration.test.container.MySQLContainer;
+import com.datastrato.gravitino.integration.test.util.TestDatabaseName;
import com.datastrato.gravitino.meta.AuditInfo;
import java.sql.Connection;
import java.sql.DriverManager;
@@ -19,13 +21,12 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
-import org.testcontainers.containers.MySQLContainer;
public class MysqlService {
private Connection connection;
- public MysqlService(MySQLContainer<?> mysqlContainer) {
+ public MysqlService(MySQLContainer mysqlContainer, TestDatabaseName testDBName) {
String username = mysqlContainer.getUsername();
String password = mysqlContainer.getPassword();
@@ -33,7 +34,9 @@ public class MysqlService {
connection =
DriverManager.getConnection(
StringUtils.substring(
- mysqlContainer.getJdbcUrl(), 0, mysqlContainer.getJdbcUrl().lastIndexOf("/")),
+ mysqlContainer.getJdbcUrl(testDBName),
+ 0,
+ mysqlContainer.getJdbcUrl(testDBName).lastIndexOf("/")),
username,
password);
} catch (Exception e) {
diff --git a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlAbstractIT.java b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlAbstractIT.java
index f93b703fe9..4976f12f95 100644
--- a/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlAbstractIT.java
+++ b/catalogs/catalog-jdbc-postgresql/src/test/java/com/datastrato/gravitino/catalog/postgresql/integration/test/TestPostgreSqlAbstractIT.java
@@ -23,7 +23,7 @@ public class TestPostgreSqlAbstractIT extends TestJdbcAbstractIT {
public static final String DEFAULT_POSTGRES_IMAGE = "postgres:13";
@BeforeAll
- public static void startup() {
+ public static void startup() throws Exception {
CONTAINER =
new PostgreSQLContainer<>(DEFAULT_POSTGRES_IMAGE)
.withDatabaseName(TEST_DB_NAME)
diff --git a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/ContainerSuite.java b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/ContainerSuite.java
index 0eeb4962a2..2bd2ebbe8b 100644
--- a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/ContainerSuite.java
+++ b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/ContainerSuite.java
@@ -5,6 +5,7 @@
package com.datastrato.gravitino.integration.test.container;
import com.datastrato.gravitino.integration.test.util.CloseableGroup;
+import com.datastrato.gravitino.integration.test.util.TestDatabaseName;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.RemoveNetworkCmd;
import com.github.dockerjava.api.model.Info;
@@ -25,7 +26,8 @@ public class ContainerSuite implements Closeable {
public static final Logger LOG = LoggerFactory.getLogger(ContainerSuite.class);
private static volatile ContainerSuite instance = null;
- // The subnet must match the configuration in `dev/docker/tools/mac-docker-connector.conf`
+ // The subnet must match the configuration in
+ // `dev/docker/tools/mac-docker-connector.conf`
public static final String CONTAINER_NETWORK_SUBNET = "10.20.30.0/28";
private static final String CONTAINER_NETWORK_GATEWAY = "10.20.30.1";
private static final String CONTAINER_NETWORK_IPRANGE = "10.20.30.0/28";
@@ -38,6 +40,9 @@ public class ContainerSuite implements Closeable {
private static volatile KafkaContainer kafkaContainer;
private static volatile DorisContainer dorisContainer;
+ private static volatile MySQLContainer mySQLContainer;
+ private static volatile MySQLContainer mySQLVersion5Container;
+
protected static final CloseableGroup closer = CloseableGroup.create();
private ContainerSuite() {
@@ -150,6 +155,59 @@ public class ContainerSuite implements Closeable {
}
}
+ public void startMySQLContainer(TestDatabaseName testDatabaseName) {
+ if (mySQLContainer == null) {
+ synchronized (ContainerSuite.class) {
+ if (mySQLContainer == null) {
+ // Start MySQL container
+ MySQLContainer.Builder mysqlBuilder =
+ MySQLContainer.builder()
+ .withHostName("gravitino-ci-mysql")
+ .withEnvVars(
+ ImmutableMap.<String, String>builder()
+ .put("MYSQL_ROOT_PASSWORD", "root")
+ .build())
+ .withExposePorts(ImmutableSet.of(MySQLContainer.MYSQL_PORT))
+ .withNetwork(network);
+
+ MySQLContainer container = closer.register(mysqlBuilder.build());
+ container.start();
+ mySQLContainer = container;
+ }
+ }
+ }
+ synchronized (MySQLContainer.class) {
+ mySQLContainer.createDatabase(testDatabaseName);
+ }
+ }
+
+ public void startMySQLVersion5Container(TestDatabaseName testDatabaseName) {
+ if (mySQLVersion5Container == null) {
+ synchronized (ContainerSuite.class) {
+ if (mySQLVersion5Container == null) {
+ // Start MySQL container
+ MySQLContainer.Builder mysqlBuilder =
+ MySQLContainer.builder()
+ .withImage("mysql:5.7")
+ .withHostName("gravitino-ci-mysql-v5")
+ .withEnvVars(
+ ImmutableMap.<String, String>builder()
+ .put("MYSQL_ROOT_PASSWORD", "root")
+ .build())
+ .withExposePorts(ImmutableSet.of(MySQLContainer.MYSQL_PORT))
+ .withNetwork(network);
+
+ MySQLContainer container = closer.register(mysqlBuilder.build());
+ container.start();
+ mySQLVersion5Container = container;
+ }
+ }
+ }
+ synchronized (MySQLContainer.class) {
+ mySQLVersion5Container.createDatabase(testDatabaseName);
+ }
+ }
+
public void startKafkaContainer() {
if (kafkaContainer == null) {
synchronized (ContainerSuite.class) {
@@ -192,7 +250,16 @@ public class ContainerSuite implements Closeable {
return dorisContainer;
}
- // Let containers assign addresses in a fixed subnet to avoid `mac-docker-connector` needing to
+ public MySQLContainer getMySQLContainer() {
+ return mySQLContainer;
+ }
+
+ public MySQLContainer getMySQLVersion5Container() {
+ return mySQLVersion5Container;
+ }
+
+ // Let containers assign addresses in a fixed subnet to avoid
+ // `mac-docker-connector` needing to
// refresh the configuration
private static Network createDockerNetwork() {
DockerClient dockerClient = DockerClientFactory.instance().client();
diff --git a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/MySQLContainer.java b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/MySQLContainer.java
new file mode 100644
index 0000000000..6547c9497a
--- /dev/null
+++ b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/container/MySQLContainer.java
@@ -0,0 +1,158 @@
+/*
+ * Copyright 2023 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.integration.test.container;
+
+import static java.lang.String.format;
+import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
+
+import com.datastrato.gravitino.integration.test.util.TestDatabaseName;
+import com.google.common.collect.ImmutableSet;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang3.StringUtils;
+import org.rnorth.ducttape.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.Container;
+import org.testcontainers.containers.Network;
+
+public class MySQLContainer extends BaseContainer {
+ public static final Logger LOG = LoggerFactory.getLogger(MySQLContainer.class);
+
+ public static final String DEFAULT_IMAGE = "mysql:8.0";
+ public static final String HOST_NAME = "gravitino-ci-mysql";
+ public static final int MYSQL_PORT = 3306;
+ public static final String USER_NAME = "root";
+ public static final String PASSWORD = "root";
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ protected MySQLContainer(
+ String image,
+ String hostName,
+ Set<Integer> ports,
+ Map<String, String> extraHosts,
+ Map<String, String> filesToMount,
+ Map<String, String> envVars,
+ Optional<Network> network) {
+ super(image, hostName, ports, extraHosts, filesToMount, envVars, network);
+ }
+
+ @Override
+ protected void setupContainer() {
+ super.setupContainer();
+ withLogConsumer(new PrintingContainerLog(format("%-14s| ", "MySQLContainer")));
+ }
+
+ @Override
+ public void start() {
+ super.start();
+ Preconditions.check("MySQL container startup failed!", checkContainerStatus(5));
+ }
+
+ @Override
+ protected boolean checkContainerStatus(int retryLimit) {
+ int nRetry = 0;
+ boolean isMySQLContainerReady = false;
+ int sleepTimeMillis = 20_00;
+ while (nRetry++ < retryLimit) {
+ try {
+ String[] commandAndArgs =
+ new String[] {
+ "mysqladmin",
+ "ping",
+ "-h",
+ "localhost",
+ "-u",
+ getUsername(),
+ String.format("-p%s", getPassword())
+ };
+ Container.ExecResult execResult = executeInContainer(commandAndArgs);
+ if (execResult.getExitCode() != 0) {
+ String message =
+ format(
+ "Command [%s] exited with %s",
+ String.join(" ", commandAndArgs), execResult.getExitCode());
+ LOG.error("{}", message);
+ LOG.error("stderr: {}", execResult.getStderr());
+ LOG.error("stdout: {}", execResult.getStdout());
+ } else {
+ LOG.info("MySQL container startup success!");
+ isMySQLContainerReady = true;
+ break;
+ }
+ LOG.info(
+ "MySQL container is not ready, recheck({}/{}) after {}ms",
+ nRetry,
+ retryLimit,
+ sleepTimeMillis);
+ await().atLeast(sleepTimeMillis, TimeUnit.MILLISECONDS);
+ } catch (RuntimeException e) {
+ LOG.error(e.getMessage(), e);
+ }
+ }
+
+ return isMySQLContainerReady;
+ }
+
+ public void createDatabase(TestDatabaseName testDatabaseName) {
+ String mySQLJdbcUrl =
+ StringUtils.substring(
+ getJdbcUrl(testDatabaseName), 0, getJdbcUrl(testDatabaseName).lastIndexOf("/"));
+
+ // change password for root user, Gravitino API must set password in catalog properties
+ try (Connection connection =
+ DriverManager.getConnection(mySQLJdbcUrl, USER_NAME, getPassword());
+ Statement statement = connection.createStatement()) {
+
+ String query = String.format("CREATE DATABASE IF NOT EXISTS %s;", testDatabaseName);
+ // FIXME: String, which is used in SQL, can be unsafe
+ statement.execute(query);
+ LOG.info(String.format("MySQL container database %s has been created", testDatabaseName));
+ } catch (Exception e) {
+ LOG.error(e.getMessage(), e);
+ }
+ }
+
+ public String getUsername() {
+ return USER_NAME;
+ }
+
+ public String getPassword() {
+ return PASSWORD;
+ }
+
+ public String getJdbcUrl(TestDatabaseName testDatabaseName) {
+ return format("jdbc:mysql://%s:%d/%s", getContainerIpAddress(), MYSQL_PORT, testDatabaseName);
+ }
+
+ public String getDriverClassName(TestDatabaseName testDatabaseName) throws SQLException {
+ return DriverManager.getDriver(getJdbcUrl(testDatabaseName)).getClass().getName();
+ }
+
+ public static class Builder
+ extends BaseContainer.Builder<MySQLContainer.Builder, MySQLContainer> {
+
+ private Builder() {
+ this.image = DEFAULT_IMAGE;
+ this.hostName = HOST_NAME;
+ this.exposePorts = ImmutableSet.of(MYSQL_PORT);
+ }
+
+ @Override
+ public MySQLContainer build() {
+ return new MySQLContainer(
+ image, hostName, exposePorts, extraHosts, filesToMount, envVars, network);
+ }
+ }
+}
diff --git a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java
index 898a52882f..d9c95872ed 100644
--- a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java
+++ b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/AbstractIT.java
@@ -14,6 +14,8 @@ import com.datastrato.gravitino.client.GravitinoAdminClient;
import com.datastrato.gravitino.config.ConfigConstants;
import com.datastrato.gravitino.integration.test.MiniGravitino;
import com.datastrato.gravitino.integration.test.MiniGravitinoContext;
+import com.datastrato.gravitino.integration.test.container.ContainerSuite;
+import com.datastrato.gravitino.integration.test.container.MySQLContainer;
import com.datastrato.gravitino.server.GravitinoServer;
import com.datastrato.gravitino.server.ServerConfig;
import com.datastrato.gravitino.server.web.JettyServerConfig;
@@ -33,12 +35,15 @@ import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.testcontainers.containers.MySQLContainer;
@ExtendWith(PrintFuncNameExtension.class)
public class AbstractIT {
+ protected static final ContainerSuite containerSuite = ContainerSuite.getInstance();
+
private static final Logger LOG = LoggerFactory.getLogger(AbstractIT.class);
protected static GravitinoAdminClient client;
@@ -56,12 +61,11 @@ public class AbstractIT {
protected static boolean ignoreIcebergRestService = true;
- private static final String MYSQL_DOCKER_IMAGE_VERSION = "mysql:8.0";
private static final String DOWNLOAD_JDBC_DRIVER_URL =
"https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.26/mysql-connector-java-8.0.26.jar";
- private static final String META_DATA = "metadata";
- private static MySQLContainer<?> MYSQL_CONTAINER;
+ private static TestDatabaseName META_DATA;
+ private static MySQLContainer MYSQL_CONTAINER;
protected static String serverUri;
@@ -110,7 +114,7 @@ public class AbstractIT {
}
private static void setMySQLBackend() {
- String mysqlUrl = MYSQL_CONTAINER.getJdbcUrl();
+ String mysqlUrl = MYSQL_CONTAINER.getJdbcUrl(META_DATA);
customConfigs.put(Configs.ENTITY_STORE_KEY, "relational");
customConfigs.put(Configs.ENTITY_RELATIONAL_STORE_KEY, "JDBCBackend");
customConfigs.put(Configs.ENTITY_RELATIONAL_JDBC_BACKEND_URL_KEY, mysqlUrl);
@@ -146,6 +150,13 @@ public class AbstractIT {
}
}
+ @ParameterizedTest
+ @CsvSource({
+ "embedded, jdbcBackend",
+ "embedded, kvBackend",
+ "deploy, jdbcBackend",
+ "deploy, kvBackend"
+ })
@BeforeAll
public static void startIntegrationTest() throws Exception {
testMode =
@@ -157,12 +168,9 @@ public class AbstractIT {
if ("true".equals(System.getenv("jdbcBackend"))) {
// Start MySQL docker instance.
- MYSQL_CONTAINER =
- new MySQLContainer<>(MYSQL_DOCKER_IMAGE_VERSION)
- .withDatabaseName(META_DATA)
- .withUsername("root")
- .withPassword("root");
- MYSQL_CONTAINER.start();
+ META_DATA = TestDatabaseName.MYSQL_JDBC_BACKEND;
+ containerSuite.startMySQLContainer(META_DATA);
+ MYSQL_CONTAINER = containerSuite.getMySQLContainer();
setMySQLBackend();
}
@@ -229,10 +237,6 @@ public class AbstractIT {
}
customConfigs.clear();
LOG.info("Tearing down Gravitino Server");
-
- if (MYSQL_CONTAINER != null) {
- MYSQL_CONTAINER.stop();
- }
}
public static GravitinoAdminClient getGravitinoClient() {
diff --git a/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/TestDatabaseName.java b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/TestDatabaseName.java
new file mode 100644
index 0000000000..65f2891581
--- /dev/null
+++ b/integration-test-common/src/test/java/com/datastrato/gravitino/integration/test/util/TestDatabaseName.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.integration.test.util;
+
+/**
+ * An enum representing the different test database names used for testing purposes in the Gravitino
+ * project.
+ *
+ * <p>This enum provides a set of predefined database names that can be used in test cases to
+ * specify the target MySQL database for testing various functionalities and components of
+ * Gravitino.
+ *
+ * <p>The available test database names are:
+ *
+ * <ul>
+ * <li>{@link #MYSQL_JDBC_BACKEND}: Represents the MySQL database used for testing the JDBC
+ * backend of Gravitino.
+ * <li>{@link #MYSQL_MYSQL_ABSTRACT_IT}: Represents the MySQL database used for testing the
+ * MysqlAbstractIT and its subclasses.
+ * <li>{@link #MYSQL_AUDIT_CATALOG_MYSQL_IT}: Represents the MySQL database used for testing the
+ * AuditCatalogMysqlIT.
+ * <li>{@link #MYSQL_CATALOG_MYSQL_IT}: Represents the MySQL database used for testing the catalog
+ * integration with MySQL.
+ * </ul>
+ */
+public enum TestDatabaseName {
+ /** Represents the MySQL database used for JDBC backend of Gravitino. */
+ MYSQL_JDBC_BACKEND,
+
+ /** Represents the MySQL database for MysqlAbstractIT and its subclasses. */
+ MYSQL_MYSQL_ABSTRACT_IT,
+
+ /** Represents the MySQL database for AudtCatalogMysqlIT. */
+ MYSQL_AUDIT_CATALOG_MYSQL_IT,
+
+ /** Represents the MySQL database used for testing the catalog integration with MySQL. */
+ MYSQL_CATALOG_MYSQL_IT
+}