Skip to content

Commit c9015aa

Browse files
Follow example from testcontainers/testcontainers-java#2689 to modify MySQL container as root
1 parent 4cf04f0 commit c9015aa

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

airbyte-integrations/connectors/source-mysql/src/test/java/io/airbyte/integrations/source/mysql/MySqlJdbcStandardTest.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,27 @@
2727
import com.fasterxml.jackson.databind.JsonNode;
2828
import com.google.common.collect.ImmutableMap;
2929
import io.airbyte.commons.json.Jsons;
30-
import io.airbyte.commons.resources.MoreResources;
3130
import io.airbyte.db.Database;
3231
import io.airbyte.db.Databases;
3332
import io.airbyte.integrations.source.jdbc.AbstractJdbcSource;
3433
import io.airbyte.integrations.source.jdbc.test.JdbcSourceStandardTest;
34+
import java.sql.Connection;
35+
import java.sql.DriverManager;
36+
import java.sql.SQLException;
3537
import org.apache.commons.lang3.RandomStringUtils;
3638
import org.jooq.SQLDialect;
3739
import org.junit.jupiter.api.AfterAll;
3840
import org.junit.jupiter.api.AfterEach;
3941
import org.junit.jupiter.api.BeforeAll;
4042
import org.junit.jupiter.api.BeforeEach;
43+
import org.slf4j.Logger;
44+
import org.slf4j.LoggerFactory;
4145
import org.testcontainers.containers.MySQLContainer;
4246

4347
class MySqlJdbcStandardTest extends JdbcSourceStandardTest {
4448

49+
private static final Logger LOGGER = LoggerFactory.getLogger(MySqlJdbcStandardTest.class);
50+
4551
private static final String TEST_USER = "test";
4652
private static final String TEST_PASSWORD = "test";
4753
private static MySQLContainer<?> container;
@@ -50,13 +56,15 @@ class MySqlJdbcStandardTest extends JdbcSourceStandardTest {
5056
private Database database;
5157

5258
@BeforeAll
53-
static void init() {
54-
// test containers withInitScript only accepts scripts that are mounted as resources.
55-
MoreResources.writeResource("init.sql",
56-
"CREATE USER '" + TEST_USER + "'@'%' IDENTIFIED BY '" + TEST_PASSWORD + "';\n"
57-
+ "GRANT ALL PRIVILEGES ON *.* TO '" + TEST_USER + "'@'%';\n");
58-
container = new MySQLContainer<>("mysql:8.0").withInitScript("init.sql").withUsername("root").withPassword("");
59+
static void init() throws SQLException {
60+
container = new MySQLContainer<>("mysql:8.0")
61+
.withUsername(TEST_USER)
62+
.withPassword(TEST_PASSWORD)
63+
.withEnv("MYSQL_ROOT_HOST", "%")
64+
.withEnv("MYSQL_ROOT_PASSWORD", TEST_PASSWORD);
5965
container.start();
66+
Connection connection = DriverManager.getConnection(container.getJdbcUrl(), "root", TEST_PASSWORD);
67+
connection.createStatement().execute("GRANT ALL PRIVILEGES ON *.* TO '" + TEST_USER + "'@'%';\n");
6068
}
6169

6270
@BeforeEach
@@ -76,6 +84,7 @@ public void setup() throws Exception {
7684
config.get("host").asText(),
7785
config.get("port").asText()),
7886
MySqlSource.DRIVER_CLASS,
87+
7988
SQLDialect.MYSQL);
8089

8190
database.query(ctx -> {
@@ -84,6 +93,8 @@ public void setup() throws Exception {
8493
});
8594
database.close();
8695

96+
container.getEnv().forEach(
97+
s -> LOGGER.info("Env: {}", s));
8798
super.setup();
8899
}
89100

airbyte-integrations/connectors/source-mysql/src/test/java/io/airbyte/integrations/source/mysql/MySqlStressTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
import com.fasterxml.jackson.databind.JsonNode;
2828
import com.google.common.collect.ImmutableMap;
2929
import io.airbyte.commons.json.Jsons;
30-
import io.airbyte.commons.resources.MoreResources;
3130
import io.airbyte.db.Database;
3231
import io.airbyte.db.Databases;
3332
import io.airbyte.integrations.source.jdbc.AbstractJdbcSource;
3433
import io.airbyte.integrations.source.jdbc.test.JdbcStressTest;
34+
import java.sql.Connection;
35+
import java.sql.DriverManager;
36+
import java.sql.SQLException;
3537
import java.util.Optional;
3638
import org.apache.commons.lang3.RandomStringUtils;
3739
import org.jooq.SQLDialect;
@@ -53,13 +55,15 @@ class MySqlStressTest extends JdbcStressTest {
5355
private Database database;
5456

5557
@BeforeAll
56-
static void init() {
57-
// test containers withInitScript only accepts scripts that are mounted as resources.
58-
MoreResources.writeResource("init.sql",
59-
"CREATE USER '" + TEST_USER + "'@'%' IDENTIFIED BY '" + TEST_PASSWORD + "';\n"
60-
+ "GRANT ALL PRIVILEGES ON *.* TO '" + TEST_USER + "'@'%';\n");
61-
container = new MySQLContainer<>("mysql:8.0").withInitScript("init.sql").withUsername("root").withPassword("");
58+
static void init() throws SQLException {
59+
container = new MySQLContainer<>("mysql:8.0")
60+
.withUsername(TEST_USER)
61+
.withPassword(TEST_PASSWORD)
62+
.withEnv("MYSQL_ROOT_HOST", "%")
63+
.withEnv("MYSQL_ROOT_PASSWORD", TEST_PASSWORD);
6264
container.start();
65+
Connection connection = DriverManager.getConnection(container.getJdbcUrl(), "root", TEST_PASSWORD);
66+
connection.createStatement().execute("GRANT ALL PRIVILEGES ON *.* TO '" + TEST_USER + "'@'%';\n");
6367
}
6468

6569
@BeforeEach

0 commit comments

Comments
 (0)