Skip to content
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

Merged
merged 5 commits into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
@@ -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", "%");
Copy link
Member

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?


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
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

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

Let's do a try-catch here, catching ContainerException.

container.stop();
}

@NonNull
protected ResultSet performQuery(MySQLContainer containerRule, String sql) throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

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

We can use an else if here and lose one level of nesting.

if("root".equalsIgnoreCase(username)){
Copy link
Member

Choose a reason for hiding this comment

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

Please use username as the object for the method invocation.

addEnv("MYSQL_ALLOW_EMPTY_PASSWORD", "yes");
} else {
throw new RuntimeException("Empty password can be used only with the root user");
}
}
setStartupAttempts(3);
}

Expand Down