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

Implementation of MariaDB container #215

Merged
merged 1 commit into from
Sep 24, 2016
Merged
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
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ Krystian Nowak <krystian.nowak@gmail.com>
Viktor Schulz <vschulz@mail.uni-mannheim.de>
Asaf Mesika <asaf.mesika@gmail.com>
Sergei Egorov <bsideup@gmail.com>
Pete Cornish <outofcoffee@gmail.com>
Pete Cornish <outofcoffee@gmail.com>
Miguel Gonzalez Sanchez <miguel-gonzalez@gmx.de>
59 changes: 59 additions & 0 deletions modules/mariadb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-parent</artifactId>
<version>1.1.6-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>mariadb</artifactId>
<name>TestContainers :: JDBC :: MariaDB</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>testcontainers</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jdbc</artifactId>
<version>${project.version}</version>
</dependency>

<!-- Database driver for testing -->
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>1.4.6</version>
<scope>test</scope>
</dependency>

<!-- Database connection pool for testing -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java6</artifactId>
<version>2.3.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.5.4</version>
</dependency>
<dependency>
<groupId>org.vibur</groupId>
<artifactId>vibur-dbcp</artifactId>
<version>9.0</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.testcontainers.containers;

/**
* Container implementation for the MariaDB project.
*
* @author Miguel Gonzalez Sanchez
*/
public class MariaDBContainer<SELF extends MariaDBContainer<SELF>> extends JdbcDatabaseContainer<SELF> {

public static final String NAME = "mariadb";
public static final String IMAGE = "mariadb";
private static final Integer MARIADB_PORT = 3306;
private static final String MARIADB_USER = "test";
private static final String MARIADB_PASSWORD = "test";
private static final String MARIADB_DATABASE = "test";
private static final String MY_CNF_CONFIG_OVERRIDE_PARAM_NAME = "TC_MY_CNF";

public MariaDBContainer() {
super(IMAGE + ":latest");
}

public MariaDBContainer(String dockerImageName) {
super(dockerImageName);
}

@Override
protected Integer getLivenessCheckPort() {
return getMappedPort(MARIADB_PORT);
}

@Override
protected void configure() {
optionallyMapResourceParameterAsVolume(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, "/etc/mysql/conf.d");

addExposedPort(MARIADB_PORT);
addEnv("MYSQL_DATABASE", MARIADB_DATABASE);
addEnv("MYSQL_USER", MARIADB_USER);
addEnv("MYSQL_PASSWORD", MARIADB_PASSWORD);
addEnv("MYSQL_ROOT_PASSWORD", MARIADB_PASSWORD);
setCommand("mysqld");
setStartupAttempts(3);
}

@Override
public String getDriverClassName() {
return "org.mariadb.jdbc.Driver";
}

@Override
public String getJdbcUrl() {
return "jdbc:mariadb://" + getContainerIpAddress() + ":" + getMappedPort(MARIADB_PORT) + "/test";
}

@Override
public String getUsername() {
return MARIADB_USER;
}

@Override
public String getPassword() {
return MARIADB_PASSWORD;
}

@Override
public String getTestQueryString() {
return "SELECT 1";
}

public SELF withConfigurationOverride(String s) {
parameters.put(MY_CNF_CONFIG_OVERRIDE_PARAM_NAME, s);
return self();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.testcontainers.containers;

/**
* Factory for MariaDB containers.
*/
public class MariaDBContainerProvider extends JdbcDatabaseContainerProvider {
@Override
public boolean supports(String databaseType) {
return databaseType.equals(MariaDBContainer.NAME);
}

@Override
public JdbcDatabaseContainer newInstance(String tag) {
return new MariaDBContainer(MariaDBContainer.IMAGE + ":" + tag);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.testcontainers.containers.MariaDBContainerProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package org.testcontainers.jdbc;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.lang.SystemUtils;
import org.junit.After;
import org.junit.Test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static org.junit.Assume.assumeFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;

/**
*
*/
public class JDBCDriverTest {

@After
public void testCleanup() {
ContainerDatabaseDriver.killContainers();
}

@Test
public void testMariaDBWithVersion() throws SQLException {
performSimpleTest("jdbc:tc:mariadb:10.1.16://hostname/databasename");
}

@Test
public void testMariaDBWithNoSpecifiedVersion() throws SQLException {
performSimpleTest("jdbc:tc:mariadb://hostname/databasename");
}

@Test
public void testMariaDBWithCustomIniFile() throws SQLException {
assumeFalse(SystemUtils.IS_OS_WINDOWS);
HikariDataSource ds = getDataSource("jdbc:tc:mariadb:10.1.16://hostname/databasename?TC_MY_CNF=somepath/mariadb_conf_override", 1);
Statement statement = ds.getConnection().createStatement();
statement.execute("SELECT @@GLOBAL.innodb_file_format");
ResultSet resultSet = statement.getResultSet();

resultSet.next();
String result = resultSet.getString(1);

assertEquals("The InnoDB file format has been set by the ini file content", "Barracuda", result);
}

@Test
public void testMariaDBWithClasspathInitScript() throws SQLException {
performSimpleTest("jdbc:tc:mariadb://hostname/databasename?TC_INITSCRIPT=somepath/init_mariadb.sql");

performTestForScriptedSchema("jdbc:tc:mariadb://hostname/databasename?TC_INITSCRIPT=somepath/init_mariadb.sql");
}

@Test
public void testMariaDBWithClasspathInitFunction() throws SQLException {
performSimpleTest("jdbc:tc:mariadb://hostname/databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction");

performTestForScriptedSchema("jdbc:tc:mariadb://hostname/databasename?TC_INITFUNCTION=org.testcontainers.jdbc.JDBCDriverTest::sampleInitFunction");
}

@Test
public void testMariaDBWithQueryParams() throws SQLException {
performSimpleTestWithCharacterSet("jdbc:tc:mariadb://hostname/databasename?useUnicode=yes&characterEncoding=utf8");
}

private void performSimpleTest(String jdbcUrl) throws SQLException {
HikariDataSource dataSource = getDataSource(jdbcUrl, 1);
new QueryRunner(dataSource).query("SELECT 1", new ResultSetHandler<Object>() {
@Override
public Object handle(ResultSet rs) throws SQLException {
rs.next();
int resultSetInt = rs.getInt(1);
assertEquals("A basic SELECT query succeeds", 1, resultSetInt);
return true;
}
});
dataSource.close();
}

private void performTestForScriptedSchema(String jdbcUrl) throws SQLException {
HikariDataSource dataSource = getDataSource(jdbcUrl, 1);
new QueryRunner(dataSource).query("SELECT foo FROM bar WHERE foo LIKE '%world'", new ResultSetHandler<Object>() {
@Override
public Object handle(ResultSet rs) throws SQLException {
rs.next();
String resultSetString = rs.getString(1);
assertEquals("A basic SELECT query succeeds where the schema has been applied from a script", "hello world", resultSetString);
return true;
}
});
dataSource.close();
}

private void performSimpleTestWithCharacterSet(String jdbcUrl) throws SQLException {
HikariDataSource dataSource = getDataSource(jdbcUrl, 1);
new QueryRunner(dataSource).query("SHOW VARIABLES LIKE 'character\\_set\\_connection'", new ResultSetHandler<Object>() {
@Override
public Object handle(ResultSet rs) throws SQLException {
rs.next();
String resultSetInt = rs.getString(2);
assertEquals("Passing query parameters to set DB connection encoding is successful", "utf8", resultSetInt);
return true;
}
});
dataSource.close();
}

private HikariDataSource getDataSource(String jdbcUrl, int poolSize) {
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setJdbcUrl(jdbcUrl);
hikariConfig.setConnectionTestQuery("SELECT 1");
hikariConfig.setMinimumIdle(1);
hikariConfig.setMaximumPoolSize(poolSize);

return new HikariDataSource(hikariConfig);
}

public static void sampleInitFunction(Connection connection) throws SQLException {
connection.createStatement().execute("CREATE TABLE bar (\n" +
" foo VARCHAR(255)\n" +
");");
connection.createStatement().execute("INSERT INTO bar (foo) VALUES ('hello world');");
connection.createStatement().execute("CREATE TABLE my_counter (\n" +
" n INT\n" +
");");
}
}
Loading