use testcontainers/docker to run jdbc sessions tests with Mariadb remote

Signed-off-by: olivier lamy <oliver.lamy@gmail.com>
This commit is contained in:
olivier lamy 2020-05-23 15:09:28 +10:00 committed by Olivier Lamy
parent 0b8306976a
commit c05bd6c26d
6 changed files with 202 additions and 53 deletions

View File

@ -60,6 +60,8 @@ public class DatabaseAdaptor
private String _connectionUrl;
private Driver _driver;
private DataSource _datasource;
private String _username;
private String _password;
private String _jndiName;
@ -257,6 +259,26 @@ public class DatabaseAdaptor
return _connectionUrl;
}
public String getUsername()
{
return _username;
}
public void setUsername(String username)
{
this._username = username;
}
public String getPassword()
{
return _password;
}
public void setPassword(String password)
{
this._password = password;
}
public void initialize()
throws Exception
{
@ -302,7 +324,8 @@ public class DatabaseAdaptor
if (_datasource != null)
return _datasource.getConnection();
else
return DriverManager.getConnection(_connectionUrl);
return _username == null ? DriverManager.getConnection(_connectionUrl)
: DriverManager.getConnection(_connectionUrl, _username, _password);
}
/**

View File

@ -63,6 +63,7 @@
<localRepoPath>${project.build.directory}/local-repo</localRepoPath>
<settingsPath>src/it/settings.xml</settingsPath>
<surefire.rerunFailingTestsCount>0</surefire.rerunFailingTestsCount>
<testcontainers.version>1.14.2</testcontainers.version>
</properties>
<licenses>
@ -1083,12 +1084,12 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.14.1</version>
<version>${testcontainers.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.14.1</version>
<version>${testcontainers.version}</version>
</dependency>
<!-- Old Deps -->
<dependency>

View File

@ -8,9 +8,9 @@
</parent>
<artifactId>test-jdbc-sessions</artifactId>
<name>Jetty Tests :: Sessions :: JDBC</name>
<url>http://www.eclipse.org/jetty</url>
<properties>
<bundle-symbolic-name>${project.groupId}.sessions.jdbc</bundle-symbolic-name>
<mariadb.docker.version>10.3.6</mariadb.docker.version>
</properties>
<build>
<plugins>
@ -22,6 +22,15 @@
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<mariadb.enabled>false</mariadb.enabled>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
@ -60,5 +69,57 @@
<artifactId>jetty-test-helper</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mariadb</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
<id>remote-session-tests</id>
<activation>
<property>
<name>mariadb.enabled</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<mariadb.docker.version>${mariadb.docker.version}</mariadb.docker.version>
<mariadb.enabled>true</mariadb.enabled>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>

View File

@ -33,6 +33,11 @@ import java.util.Set;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.ClassLoadingObjectInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.MariaDBContainer;
import org.testcontainers.containers.output.Slf4jLogConsumer;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -42,9 +47,14 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/
public class JdbcTestHelper
{
public static final String DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver";
public static final String DEFAULT_CONNECTION_URL = "jdbc:derby:memory:sessions;create=true";
public static final String DEFAULT_SHUTDOWN_URL = "jdbc:derby:memory:sessions;drop=true";
public static final boolean USE_MARIADB = Boolean.getBoolean("mariadb.enabled");
private static final Logger LOG = LoggerFactory.getLogger(JdbcTestHelper.class);
private static final Logger MARIADB_LOG = LoggerFactory.getLogger("org.eclipse.jetty.server.session.MariaDbLogs");
public static String DRIVER_CLASS = "org.apache.derby.jdbc.EmbeddedDriver";
public static String DEFAULT_CONNECTION_URL = "jdbc:derby:memory:sessions;create=true";
public static String DEFAULT_SHUTDOWN_URL = "jdbc:derby:memory:sessions;drop=true";
public static final int STALE_INTERVAL = 1;
public static final String EXPIRY_COL = "extime";
@ -60,6 +70,33 @@ public class JdbcTestHelper
public static final String COOKIE_COL = "cooktime";
public static final String CREATE_COL = "ctime";
static MariaDBContainer MARIAD_DB =
new MariaDBContainer("mariadb:" + System.getProperty("mariadb.docker.version", "10.3.6"))
.withDatabaseName("sessions");
static
{
if (USE_MARIADB)
{
try
{
long start = System.currentTimeMillis();
MARIAD_DB.withLogConsumer(new Slf4jLogConsumer(MARIADB_LOG)).start();
String containerIpAddress = MARIAD_DB.getContainerIpAddress();
int mariadbPort = MARIAD_DB.getMappedPort(3306);
DEFAULT_CONNECTION_URL = MARIAD_DB.getJdbcUrl();
DRIVER_CLASS = MARIAD_DB.getDriverClassName();
LOG.info("Mariadb container started for {}:{} - {}ms", containerIpAddress, mariadbPort,
System.currentTimeMillis() - start);
}
catch (Exception e)
{
LOG.error(e.getMessage(), e);
throw new RuntimeException(e.getMessage(), e);
}
}
}
static
{
System.setProperty("derby.system.home", MavenTestingUtils.getTargetFile("test-derby").getAbsolutePath());
@ -68,6 +105,14 @@ public class JdbcTestHelper
public static void shutdown(String connectionUrl)
throws Exception
{
if (USE_MARIADB)
{
try (Connection connection = getConnection())
{
connection.prepareStatement("truncate table " + TABLE).executeUpdate();
}
return;
}
if (connectionUrl == null)
connectionUrl = DEFAULT_SHUTDOWN_URL;
@ -84,14 +129,34 @@ public class JdbcTestHelper
}
}
public static DatabaseAdaptor buildDatabaseAdaptor()
{
DatabaseAdaptor da = new DatabaseAdaptor();
if (USE_MARIADB)
{
da.setUsername(MARIAD_DB.getUsername());
da.setPassword(MARIAD_DB.getPassword());
}
da.setDriverInfo(DRIVER_CLASS, DEFAULT_CONNECTION_URL);
return da;
}
public static Connection getConnection()
throws Exception
{
Class.forName(DRIVER_CLASS);
return USE_MARIADB ? DriverManager.getConnection(DEFAULT_CONNECTION_URL,
MARIAD_DB.getUsername(),
MARIAD_DB.getPassword())
: DriverManager.getConnection(DEFAULT_CONNECTION_URL);
}
/**
* @return a fresh JDBCSessionDataStoreFactory
*/
public static SessionDataStoreFactory newSessionDataStoreFactory()
{
DatabaseAdaptor da = new DatabaseAdaptor();
da.setDriverInfo(DRIVER_CLASS, DEFAULT_CONNECTION_URL);
return newSessionDataStoreFactory(da);
return newSessionDataStoreFactory(buildDatabaseAdaptor());
}
public static SessionDataStoreFactory newSessionDataStoreFactory(DatabaseAdaptor da)
@ -123,8 +188,7 @@ public class JdbcTestHelper
public static void prepareTables() throws SQLException
{
DatabaseAdaptor da = new DatabaseAdaptor();
da.setDriverInfo(DRIVER_CLASS, DEFAULT_CONNECTION_URL);
DatabaseAdaptor da = buildDatabaseAdaptor();
JDBCSessionDataStore.SessionTableSchema sessionTableSchema = newSessionTableSchema();
sessionTableSchema.setDatabaseAdaptor(da);
@ -163,11 +227,8 @@ public class JdbcTestHelper
public static boolean existsInSessionTable(String id, boolean verbose)
throws Exception
{
Class.forName(DRIVER_CLASS);
Connection con = null;
try
try (Connection con = getConnection())
{
con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);
PreparedStatement statement = con.prepareStatement("select * from " +
TABLE +
" where " + ID_COL + " = ?");
@ -186,21 +247,15 @@ public class JdbcTestHelper
else
return result.next();
}
finally
{
if (con != null)
con.close();
}
}
@SuppressWarnings("unchecked")
public static boolean checkSessionPersisted(SessionData data)
throws Exception
{
Class.forName(DRIVER_CLASS);
PreparedStatement statement = null;
ResultSet result = null;
try (Connection con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);)
try (Connection con = getConnection())
{
statement = con.prepareStatement("select * from " + TABLE +
" where " + ID_COL + " = ? and " + CONTEXT_COL +
@ -264,9 +319,7 @@ public class JdbcTestHelper
public static void insertSession(SessionData data) throws Exception
{
Class.forName(DRIVER_CLASS);
try (Connection con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);)
try (Connection con = getConnection())
{
PreparedStatement statement = con.prepareStatement("insert into " + TABLE +
" (" + ID_COL + ", " + CONTEXT_COL + ", virtualHost, " + LAST_NODE_COL +
@ -310,8 +363,7 @@ public class JdbcTestHelper
long cookieSet, long lastSaved)
throws Exception
{
Class.forName(DRIVER_CLASS);
try (Connection con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);)
try (Connection con = getConnection())
{
PreparedStatement statement = con.prepareStatement("insert into " + TABLE +
" (" + ID_COL + ", " + CONTEXT_COL + ", virtualHost, " + LAST_NODE_COL +
@ -343,12 +395,9 @@ public class JdbcTestHelper
public static Set<String> getSessionIds()
throws Exception
{
HashSet<String> ids = new HashSet<String>();
Class.forName(DRIVER_CLASS);
Connection con = null;
try
HashSet<String> ids = new HashSet<>();
try (Connection con = getConnection())
{
con = DriverManager.getConnection(DEFAULT_CONNECTION_URL);
PreparedStatement statement = con.prepareStatement("select " + ID_COL + " from " + TABLE);
ResultSet result = statement.executeQuery();
while (result.next())
@ -357,10 +406,5 @@ public class JdbcTestHelper
}
return ids;
}
finally
{
if (con != null)
con.close();
}
}
}

View File

@ -29,6 +29,7 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.eclipse.jetty.server.session.JdbcTestHelper.USE_MARIADB;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ -60,6 +61,11 @@ public class SessionTableSchemaTest
return true; //test special handling for oracle
}
};
if (USE_MARIADB)
{
_da.setUsername(JdbcTestHelper.MARIAD_DB.getUsername());
_da.setPassword(JdbcTestHelper.MARIAD_DB.getPassword());
}
_da.setDriverInfo(JdbcTestHelper.DRIVER_CLASS, JdbcTestHelper.DEFAULT_CONNECTION_URL);
_tableSchema = JdbcTestHelper.newSessionTableSchema();
_tableSchema.setDatabaseAdaptor(_da);
@ -84,8 +90,7 @@ public class SessionTableSchemaTest
public static void insertSessionWithoutAttributes(String id, String contextPath, String vhost)
throws Exception
{
Class.forName(JdbcTestHelper.DRIVER_CLASS);
try (Connection con = DriverManager.getConnection(JdbcTestHelper.DEFAULT_CONNECTION_URL);)
try (Connection con = JdbcTestHelper.getConnection())
{
PreparedStatement statement = con.prepareStatement("insert into " + JdbcTestHelper.TABLE +
" (" + JdbcTestHelper.ID_COL + ", " + JdbcTestHelper.CONTEXT_COL + ", virtualHost, " + JdbcTestHelper.LAST_NODE_COL +
@ -119,8 +124,10 @@ public class SessionTableSchemaTest
_da.initialize();
_tableSchema.prepareTables();
String id = Long.toString(System.nanoTime());
//insert a fake session at the root context
insertSessionWithoutAttributes("1234", "/", "0.0.0.0");
insertSessionWithoutAttributes(id, "/", "0.0.0.0");
//test if it can be seen
try (Connection con = _da.getConnection())
@ -130,7 +137,7 @@ public class SessionTableSchemaTest
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
//test the load statement
PreparedStatement s = _tableSchema.getLoadStatement(con, "1234", sc);
PreparedStatement s = _tableSchema.getLoadStatement(con, id, sc);
ResultSet rs = s.executeQuery();
assertTrue(rs.next());
}
@ -144,8 +151,10 @@ public class SessionTableSchemaTest
_da.initialize();
_tableSchema.prepareTables();
String id = Long.toString(System.nanoTime());
//insert a fake session at the root context
insertSessionWithoutAttributes("1234", "/", "0.0.0.0");
insertSessionWithoutAttributes(id, "/", "0.0.0.0");
//test if it can be seen
try (Connection con = _da.getConnection())
@ -154,7 +163,7 @@ public class SessionTableSchemaTest
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
PreparedStatement s = _tableSchema.getCheckSessionExistsStatement(con, sc);
s.setString(1, "1234");
s.setString(1, id);
ResultSet rs = s.executeQuery();
assertTrue(rs.next());
}
@ -168,8 +177,10 @@ public class SessionTableSchemaTest
_da.initialize();
_tableSchema.prepareTables();
String id = Long.toString(System.nanoTime());
//insert a fake session at the root context
insertSessionWithoutAttributes("1234", "/", "0.0.0.0");
insertSessionWithoutAttributes(id, "/", "0.0.0.0");
//test if it can be deleted
try (Connection con = _da.getConnection())
@ -177,10 +188,10 @@ public class SessionTableSchemaTest
ContextHandler handler = new ContextHandler();
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
PreparedStatement s = _tableSchema.getDeleteStatement(con, "1234", sc);
PreparedStatement s = _tableSchema.getDeleteStatement(con, id, sc);
assertEquals(1, s.executeUpdate());
assertFalse(JdbcTestHelper.existsInSessionTable("1234", false));
assertFalse(JdbcTestHelper.existsInSessionTable(id, false));
}
}
@ -192,8 +203,10 @@ public class SessionTableSchemaTest
_da.initialize();
_tableSchema.prepareTables();
String id = Long.toString(System.nanoTime());
//insert a fake session at the root context
insertSessionWithoutAttributes("1234", "/", "0.0.0.0");
insertSessionWithoutAttributes(id, "/", "0.0.0.0");
try (Connection con = _da.getConnection())
{
@ -206,7 +219,7 @@ public class SessionTableSchemaTest
(System.currentTimeMillis() + 100L));
ResultSet rs = s.executeQuery();
assertTrue(rs.next());
assertEquals("1234", rs.getString(1));
assertEquals(id, rs.getString(1));
}
}
@ -218,8 +231,10 @@ public class SessionTableSchemaTest
_da.initialize();
_tableSchema.prepareTables();
String id = Long.toString(System.nanoTime());
//insert a fake session at the root context
insertSessionWithoutAttributes("1234", "/", "0.0.0.0");
insertSessionWithoutAttributes(id, "/", "0.0.0.0");
try (Connection con = _da.getConnection())
{
@ -231,7 +246,7 @@ public class SessionTableSchemaTest
(System.currentTimeMillis() + 100L));
ResultSet rs = s.executeQuery();
assertTrue(rs.next());
assertEquals("1234", rs.getString(1));
assertEquals(id, rs.getString(1));
}
}
@ -243,8 +258,10 @@ public class SessionTableSchemaTest
_da.initialize();
_tableSchema.prepareTables();
String id = Long.toString(System.nanoTime());
//insert a fake session at the root context
insertSessionWithoutAttributes("1234", "/", "0.0.0.0");
insertSessionWithoutAttributes(id, "/", "0.0.0.0");
try (Connection con = _da.getConnection())
{
@ -252,7 +269,7 @@ public class SessionTableSchemaTest
handler.setContextPath("/");
SessionContext sc = new SessionContext("0", handler.getServletContext());
PreparedStatement s = _tableSchema.getUpdateStatement(con,
"1234",
id,
sc);
s.setString(1, "0");//should be my node id

View File

@ -0,0 +1,3 @@
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.log.org.eclipse.jetty.server.session.MariaDbLogs=error
org.slf4j.simpleLogger.log.org.eclipse.jetty.server.session.JdbcTestHelper=info