-
-
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 3 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 |
---|---|---|
|
@@ -4,7 +4,9 @@ | |
import com.zaxxer.hikari.HikariDataSource; | ||
import lombok.NonNull; | ||
import org.apache.commons.lang.SystemUtils; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.testcontainers.containers.MySQLContainer; | ||
|
@@ -43,6 +45,9 @@ public class SimpleMySQLTest { | |
.withConfigurationOverride("somepath/mysql_conf_override"); | ||
*/ | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
@Test | ||
public void testSimple() throws SQLException { | ||
MySQLContainer mysql = (MySQLContainer) new MySQLContainer() | ||
|
@@ -128,6 +133,20 @@ public void testMySQL8() throws SQLException { | |
} | ||
} | ||
|
||
@Test | ||
public void testEmptyPasswordWithNonRootUser() { | ||
thrown.expect(RuntimeException.class); | ||
|
||
MySQLContainer container = (MySQLContainer) new MySQLContainer("mysql:5.5") | ||
.withDatabaseName("TEST") | ||
.withUsername("test") | ||
.withPassword("") | ||
.withEnv("MYSQL_ROOT_HOST", "%"); | ||
|
||
container.start(); | ||
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. Let's do a try-catch here, catching |
||
container.stop(); | ||
} | ||
|
||
@NonNull | ||
protected ResultSet performQuery(MySQLContainer containerRule, String sql) throws SQLException { | ||
HikariConfig hikariConfig = new HikariConfig(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,8 +41,16 @@ protected void configure() { | |
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 { | ||
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. We can use an |
||
if("root".equalsIgnoreCase(username)){ | ||
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. Please use |
||
addEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "yes"); | ||
} else { | ||
throw new RuntimeException("Empty password can be used only with the root user"); | ||
} | ||
} | ||
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?