From 2fa00ddbd1f42a20656d5e39d58cdedfbd483dcf Mon Sep 17 00:00:00 2001 From: Richard North Date: Sat, 9 May 2020 12:03:53 +0100 Subject: [PATCH 1/2] Add test to demonstrate #2627 --- .../containers/MySQLRootAccessTest.java | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 modules/mysql/src/test/java/org/testcontainers/containers/MySQLRootAccessTest.java diff --git a/modules/mysql/src/test/java/org/testcontainers/containers/MySQLRootAccessTest.java b/modules/mysql/src/test/java/org/testcontainers/containers/MySQLRootAccessTest.java new file mode 100644 index 00000000000..5bad6e220a6 --- /dev/null +++ b/modules/mysql/src/test/java/org/testcontainers/containers/MySQLRootAccessTest.java @@ -0,0 +1,74 @@ +package org.testcontainers.containers; + +import lombok.extern.slf4j.Slf4j; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.testcontainers.containers.output.Slf4jLogConsumer; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +@Slf4j +@RunWith(Parameterized.class) +public class MySQLRootAccessTest { + + @Parameterized.Parameters(name = "{0}") + public static String[] params() { + return new String[]{ + "mysql:8", + "mysql:5.7.22" + }; + } + + @Parameterized.Parameter() + public String image; + + @Test + // Fails with 8 - cannot create duplicate root account + public void testEasyRootAccountCreation() throws SQLException { + try (MySQLContainer db = new MySQLContainer<>(image) + .withUsername("root") + .withPassword("test") + .withLogConsumer(new Slf4jLogConsumer(log))) { + db.start(); + + Connection connection = DriverManager.getConnection(db.getJdbcUrl(), db.getUsername(), db.getPassword()); + connection.createStatement().execute("SELECT 1"); + connection.createStatement().execute("set sql_log_bin=0"); + } + } + + @Test + // Fails with both, because db.getUsername() == "test" and we're trying to execute a root-only action + public void testEnvVarOnlyRootAccountCreation() throws SQLException { + try (MySQLContainer db = new MySQLContainer<>(image) + .withUsername("test") + .withPassword("test") + .withEnv("MYSQL_ROOT_PASSWORD", "test") + .withLogConsumer(new Slf4jLogConsumer(log))) { + db.start(); + + Connection connection = DriverManager.getConnection(db.getJdbcUrl(), db.getUsername(), db.getPassword()); + connection.createStatement().execute("SELECT 1"); + connection.createStatement().execute("set sql_log_bin=0"); + } + } + + @Test + // Works with both + public void testEnvVarOnlyRootAccountCreationAndHardcodedUser() throws SQLException { + try (MySQLContainer db = new MySQLContainer<>(image) + .withUsername("test") + .withPassword("test") + .withEnv("MYSQL_ROOT_PASSWORD", "test") + .withLogConsumer(new Slf4jLogConsumer(log))) { + db.start(); + + Connection connection = DriverManager.getConnection(db.getJdbcUrl(), "root", "test"); + connection.createStatement().execute("SELECT 1"); + connection.createStatement().execute("set sql_log_bin=0"); + } + } +} From cc3d1ac834861bddc5ab5c12ffa29b2209191fd6 Mon Sep 17 00:00:00 2001 From: Richard North Date: Sat, 9 May 2020 12:29:33 +0100 Subject: [PATCH 2/2] Fix comment --- .../java/org/testcontainers/containers/MySQLRootAccessTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/mysql/src/test/java/org/testcontainers/containers/MySQLRootAccessTest.java b/modules/mysql/src/test/java/org/testcontainers/containers/MySQLRootAccessTest.java index 5bad6e220a6..ce390029a01 100644 --- a/modules/mysql/src/test/java/org/testcontainers/containers/MySQLRootAccessTest.java +++ b/modules/mysql/src/test/java/org/testcontainers/containers/MySQLRootAccessTest.java @@ -26,7 +26,7 @@ public static String[] params() { public String image; @Test - // Fails with 8 - cannot create duplicate root account + // Fails with both - cannot create duplicate root account public void testEasyRootAccountCreation() throws SQLException { try (MySQLContainer db = new MySQLContainer<>(image) .withUsername("root")