-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support empty password for mysql #899
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.testcontainers.junit; | ||
|
||
import com.zaxxer.hikari.HikariConfig; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.testcontainers.containers.MySQLContainer; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
|
||
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals; | ||
|
||
public class EmptyPasswordMysqlTest { | ||
private static final String DB_NAME = "foo"; | ||
private static final String USER = "root"; // MySQL allows empty password with root only | ||
|
||
// Add MYSQL_ROOT_HOST environment so that we can root login from anywhere for testing purposes | ||
@Rule | ||
public MySQLContainer mysql = (MySQLContainer) new MySQLContainer("mysql:5.5") | ||
.withDatabaseName(DB_NAME) | ||
.withUsername(USER) | ||
.withPassword("") | ||
.withEnv("MYSQL_ROOT_HOST", "%"); | ||
|
||
kiview marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Test | ||
public void testSimple() throws SQLException { | ||
HikariConfig hikariConfig = new HikariConfig(); | ||
hikariConfig.setJdbcUrl("jdbc:mysql://" | ||
+ mysql.getContainerIpAddress() | ||
+ ":" + mysql.getMappedPort(MySQLContainer.MYSQL_PORT) | ||
+ "/" + DB_NAME); | ||
hikariConfig.setUsername(USER); | ||
hikariConfig.setPassword(""); | ||
|
||
HikariDataSource ds = new HikariDataSource(hikariConfig); | ||
Statement statement = ds.getConnection().createStatement(); | ||
statement.execute("SELECT 1"); | ||
ResultSet resultSet = statement.getResultSet(); | ||
|
||
assertEquals("There is a result", resultSet.next(), true); | ||
int resultSetInt = resultSet.getInt(1); | ||
assertEquals("A basic SELECT query succeeds", 1, resultSetInt); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ public class MySQLContainer<SELF extends MySQLContainer<SELF>> extends JdbcDatab | |
private String databaseName = "test"; | ||
private String username = "test"; | ||
private String password = "test"; | ||
private static final String MYSQL_ROOT_USER = "root"; | ||
|
||
public MySQLContainer() { | ||
super(IMAGE + ":" + DEFAULT_TAG); | ||
|
@@ -36,13 +37,20 @@ protected Set<Integer> getLivenessCheckPorts() { | |
|
||
@Override | ||
protected void configure() { | ||
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d", "mysql-default-conf"); | ||
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d", | ||
"mysql-default-conf"); | ||
|
||
addExposedPort(MYSQL_PORT); | ||
addEnv("MYSQL_DATABASE", databaseName); | ||
addEnv("MYSQL_USER", username); | ||
addEnv("MYSQL_PASSWORD", password); | ||
addEnv("MYSQL_ROOT_PASSWORD", password); | ||
if (password != null && !password.isEmpty()) { | ||
addEnv("MYSQL_PASSWORD", password); | ||
addEnv("MYSQL_ROOT_PASSWORD", password); | ||
} else if (MYSQL_ROOT_USER.equalsIgnoreCase(username)) { | ||
addEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "yes"); | ||
} else { | ||
throw new ContainerLaunchException("Empty password can be used only with the root user"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just found out, that we catch all exceptions and throw There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kiview: should we check the cause of the exception to make sure that the ContainerLaunchException is thrown by this method instead of others There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking something like this at first also. But in the end, we just want to check the public API and the container behaves as expected if we receive this exception, so I'd be good with it. |
||
} | ||
setStartupAttempts(3); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering, if we'd always need to add this and so we could put it into
configure()
as well?