Skip to content

Commit

Permalink
Allow for mysql connection customizations
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbabcock committed Jun 12, 2017
1 parent e7c6ed2 commit 48658a3
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 8 deletions.
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();
}
}

0 comments on commit 48658a3

Please sign in to comment.