Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used mysql/mysql-server:5.7.30 and this fails for me due to Access denied for user 'root'@'172.17.0.1'.

It looks like .withEnv("MYSQL_ROOT_HOST", "%")will also be needed.

}
}
}