Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -62,10 +62,12 @@ protected Set<Integer> getLivenessCheckPorts() {
@Override
protected void configure() {
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d",
"mysql-default-conf");
"mysql-default-conf");

addEnv("MYSQL_DATABASE", databaseName);
addEnv("MYSQL_USER", username);
if (!MYSQL_ROOT_USER.equalsIgnoreCase(username)) {
addEnv("MYSQL_USER", username);
}
if (password != null && !password.isEmpty()) {
addEnv("MYSQL_PASSWORD", password);
addEnv("MYSQL_ROOT_PASSWORD", password);
Expand Down Expand Up @@ -98,12 +100,12 @@ public String getJdbcUrl() {
protected String constructUrlForConnection(String queryString) {
String url = super.constructUrlForConnection(queryString);

if (! url.contains("useSSL=")) {
if (!url.contains("useSSL=")) {
String separator = url.contains("?") ? "&" : "?";
url = url + separator + "useSSL=false";
}

if (! url.contains("allowPublicKeyRetrieval=")) {
if (!url.contains("allowPublicKeyRetrieval=")) {
url = url + "&allowPublicKeyRetrieval=true";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 MySQLRootAccountTest {

@Parameterized.Parameters(name = "{0}")
public static String[] params() {
return new String[]{
"mysql:8",
"mysql:5"
};
}

@Parameterized.Parameter()
public String image;

@Test
public void testRootAccountUsageWithDefaultPassword() throws SQLException {
testWithDB(new MySQLContainer<>(image).withUsername("root"));
}

@Test
public void testRootAccountUsageWithEmptyPassword() throws SQLException {
testWithDB(new MySQLContainer<>(image).withUsername("root").withPassword(""));
}

@Test
public void testRootAccountUsageWithCustomPassword() throws SQLException {
testWithDB(new MySQLContainer<>(image).withUsername("root").withPassword("not-default"));
}

private void testWithDB(MySQLContainer<?> db) throws SQLException {
try {
db.withLogConsumer(new Slf4jLogConsumer(log)).start();
Connection connection = DriverManager.getConnection(db.getJdbcUrl(), db.getUsername(), db.getPassword());
connection.createStatement().execute("SELECT 1");
connection.createStatement().execute("set sql_log_bin=0"); // requires root
} finally {
db.close();
}
}
}