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

Allow mysql customizations #354

Closed
Closed
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
@@ -0,0 +1,69 @@
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 CustomizableMySQLTest {
private static final String DB_NAME = "foo";
private static final String USER = "bar";
private static final String PWD = "baz";
private static final String ROOT_PWD = "qux";

// Add MYSQL_ROOT_HOST environment so that we can root login from anywhere for testing purposes
@Rule
public MySQLContainer mysql = (MySQLContainer) new MySQLContainer("mysql:latest")
.withDatabaseName(DB_NAME)
.withUsername(USER)
.withPassword(PWD)
.withRootPassword(ROOT_PWD)
.withEnv("MYSQL_ROOT_HOST", "%");

@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(PWD);

HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute("SELECT 1");
ResultSet resultSet = statement.getResultSet();

resultSet.next();
int resultSetInt = resultSet.getInt(1);
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
}

@Test
public void testRootSimple() throws SQLException {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl("jdbc:mysql://"
+ mysql.getContainerIpAddress()
+ ":" + mysql.getMappedPort(MySQLContainer.MYSQL_PORT)
+ "/" + DB_NAME);
hikariConfig.setUsername("root");
hikariConfig.setPassword(ROOT_PWD);

HikariDataSource ds = new HikariDataSource(hikariConfig);
Statement statement = ds.getConnection().createStatement();
statement.execute("SELECT 1");
ResultSet resultSet = statement.getResultSet();

resultSet.next();
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 @@ -8,7 +8,11 @@ public class MySQLContainer<SELF extends MySQLContainer<SELF>> extends JdbcDatab
public static final String NAME = "mysql";
public static final String IMAGE = "mysql";
private static final String MY_CNF_CONFIG_OVERRIDE_PARAM_NAME = "TC_MY_CNF";
private static final Integer MYSQL_PORT = 3306;
public static final Integer MYSQL_PORT = 3306;
private String databaseName = "test";
private String username = "test";
private String password = "test";
private String rootPassword = "test";

public MySQLContainer() {
super(IMAGE + ":latest");
Expand All @@ -28,10 +32,10 @@ protected void configure() {
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d", "mysql-default-conf");

addExposedPort(3306);
addEnv("MYSQL_DATABASE", "test");
addEnv("MYSQL_USER", "test");
addEnv("MYSQL_PASSWORD", "test");
addEnv("MYSQL_ROOT_PASSWORD", "test");
addEnv("MYSQL_DATABASE", databaseName);
addEnv("MYSQL_USER", username);
addEnv("MYSQL_PASSWORD", password);
addEnv("MYSQL_ROOT_PASSWORD", rootPassword);
setCommand("mysqld");
setStartupAttempts(3);
}
Expand All @@ -43,17 +47,17 @@ public String getDriverClassName() {

@Override
public String getJdbcUrl() {
return "jdbc:mysql://" + getContainerIpAddress() + ":" + getMappedPort(MYSQL_PORT) + "/test";
return "jdbc:mysql://" + getContainerIpAddress() + ":" + getMappedPort(MYSQL_PORT) + "/" + databaseName;
}

@Override
public String getUsername() {
return "test";
return username;
}

@Override
public String getPassword() {
return "test";
return password;
}

@Override
Expand All @@ -65,4 +69,24 @@ public SELF withConfigurationOverride(String s) {
parameters.put(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, s);
return self();
}

public SELF withDatabaseName(final String databaseName) {
this.databaseName = databaseName;
return self();
}

public SELF withUsername(final String username) {
this.username = username;
return self();
}

public SELF withPassword(final String password) {
this.password = password;
return self();
}

public SELF withRootPassword(final String password) {
this.rootPassword = password;
return self();
}
}