forked from Archives/Athou_commafeed
add redis integration test
This commit is contained in:
20
.github/workflows/build.yml
vendored
20
.github/workflows/build.yml
vendored
@@ -32,15 +32,29 @@ jobs:
|
|||||||
# Build & Test
|
# Build & Test
|
||||||
- name: Build with Maven
|
- name: Build with Maven
|
||||||
run: mvn --batch-mode --no-transfer-progress install
|
run: mvn --batch-mode --no-transfer-progress install
|
||||||
|
env:
|
||||||
|
TEST_DATABASE: h2
|
||||||
|
|
||||||
- name: Run integration tests on PostgreSQL
|
- name: Run integration tests on PostgreSQL
|
||||||
run: TEST_DATABASE=postgresql mvn --batch-mode --no-transfer-progress failsafe:integration-test failsafe:verify
|
run: mvn --batch-mode --no-transfer-progress failsafe:integration-test failsafe:verify
|
||||||
|
env:
|
||||||
|
TEST_DATABASE: postgresql
|
||||||
|
|
||||||
- name: Run integration tests on MySQL
|
- name: Run integration tests on MySQL
|
||||||
run: TEST_DATABASE=mysql mvn --batch-mode --no-transfer-progress failsafe:integration-test failsafe:verify
|
run: mvn --batch-mode --no-transfer-progress failsafe:integration-test failsafe:verify
|
||||||
|
env:
|
||||||
|
TEST_DATABASE: mysql
|
||||||
|
|
||||||
- name: Run integration tests on MariaDB
|
- name: Run integration tests on MariaDB
|
||||||
run: TEST_DATABASE=mariadb mvn --batch-mode --no-transfer-progress failsafe:integration-test failsafe:verify
|
run: mvn --batch-mode --no-transfer-progress failsafe:integration-test failsafe:verify
|
||||||
|
env:
|
||||||
|
TEST_DATABASE: mariadb
|
||||||
|
|
||||||
|
- name: Run integration tests with Redis cache enabled
|
||||||
|
run: mvn --batch-mode --no-transfer-progress failsafe:integration-test failsafe:verify
|
||||||
|
env:
|
||||||
|
TEST_DATABASE: h2
|
||||||
|
REDIS: true
|
||||||
|
|
||||||
# Upload artifacts
|
# Upload artifacts
|
||||||
- name: Upload JAR
|
- name: Upload JAR
|
||||||
|
|||||||
@@ -23,6 +23,8 @@
|
|||||||
<mysql.image.version>8.4</mysql.image.version>
|
<mysql.image.version>8.4</mysql.image.version>
|
||||||
<!-- renovate: datasource=docker depName=mariadb -->
|
<!-- renovate: datasource=docker depName=mariadb -->
|
||||||
<mariadb.image.version>11.4.2</mariadb.image.version>
|
<mariadb.image.version>11.4.2</mariadb.image.version>
|
||||||
|
<!-- renovate: datasource=docker depName=redis -->
|
||||||
|
<redis.image.version>7.2</redis.image.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
|
|||||||
@@ -12,19 +12,25 @@ import java.util.Properties;
|
|||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.mockserver.socket.PortFactory;
|
import org.mockserver.socket.PortFactory;
|
||||||
|
import org.testcontainers.containers.GenericContainer;
|
||||||
import org.testcontainers.containers.JdbcDatabaseContainer;
|
import org.testcontainers.containers.JdbcDatabaseContainer;
|
||||||
import org.testcontainers.containers.MariaDBContainer;
|
import org.testcontainers.containers.MariaDBContainer;
|
||||||
import org.testcontainers.containers.MySQLContainer;
|
import org.testcontainers.containers.MySQLContainer;
|
||||||
import org.testcontainers.containers.PostgreSQLContainer;
|
import org.testcontainers.containers.PostgreSQLContainer;
|
||||||
|
import org.testcontainers.utility.DockerImageName;
|
||||||
|
|
||||||
import com.codahale.metrics.MetricRegistry;
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.commafeed.CommaFeedConfiguration.CacheType;
|
||||||
|
|
||||||
import io.dropwizard.testing.ConfigOverride;
|
import io.dropwizard.testing.ConfigOverride;
|
||||||
import io.dropwizard.testing.ResourceHelpers;
|
import io.dropwizard.testing.ResourceHelpers;
|
||||||
import io.dropwizard.testing.junit5.DropwizardAppExtension;
|
import io.dropwizard.testing.junit5.DropwizardAppExtension;
|
||||||
|
import redis.clients.jedis.Jedis;
|
||||||
|
import redis.clients.jedis.JedisPool;
|
||||||
|
|
||||||
public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<CommaFeedConfiguration> {
|
public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<CommaFeedConfiguration> {
|
||||||
private static final String TEST_DATABASE = System.getenv().getOrDefault("TEST_DATABASE", "h2");
|
private static final String TEST_DATABASE = System.getenv().getOrDefault("TEST_DATABASE", "h2");
|
||||||
|
private static final boolean REDIS_ENABLED = Boolean.parseBoolean(System.getenv().getOrDefault("REDIS", "false"));
|
||||||
|
|
||||||
private static final ConfigOverride[] CONFIG_OVERRIDES;
|
private static final ConfigOverride[] CONFIG_OVERRIDES;
|
||||||
private static final List<String> DROP_ALL_STATEMENTS;
|
private static final List<String> DROP_ALL_STATEMENTS;
|
||||||
@@ -32,7 +38,9 @@ public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<Comm
|
|||||||
List<ConfigOverride> overrides = new ArrayList<>();
|
List<ConfigOverride> overrides = new ArrayList<>();
|
||||||
overrides.add(ConfigOverride.config("server.applicationConnectors[0].port", String.valueOf(PortFactory.findFreePort())));
|
overrides.add(ConfigOverride.config("server.applicationConnectors[0].port", String.valueOf(PortFactory.findFreePort())));
|
||||||
|
|
||||||
DatabaseConfiguration config = buildConfiguration(TEST_DATABASE);
|
Properties imageNames = readProperties("/docker-images.properties");
|
||||||
|
|
||||||
|
DatabaseConfiguration config = buildConfiguration(TEST_DATABASE, imageNames.getProperty(TEST_DATABASE));
|
||||||
JdbcDatabaseContainer<?> container = config.container();
|
JdbcDatabaseContainer<?> container = config.container();
|
||||||
if (container != null) {
|
if (container != null) {
|
||||||
container.withDatabaseName("commafeed");
|
container.withDatabaseName("commafeed");
|
||||||
@@ -45,6 +53,16 @@ public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<Comm
|
|||||||
overrides.add(ConfigOverride.config("database.driverClass", container.getDriverClassName()));
|
overrides.add(ConfigOverride.config("database.driverClass", container.getDriverClassName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (REDIS_ENABLED) {
|
||||||
|
GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse(imageNames.getProperty("redis")))
|
||||||
|
.withExposedPorts(6379);
|
||||||
|
redis.start();
|
||||||
|
|
||||||
|
overrides.add(ConfigOverride.config("app.cache", "redis"));
|
||||||
|
overrides.add(ConfigOverride.config("redis.host", redis.getHost()));
|
||||||
|
overrides.add(ConfigOverride.config("redis.port", redis.getMappedPort(6379).toString()));
|
||||||
|
}
|
||||||
|
|
||||||
CONFIG_OVERRIDES = overrides.toArray(new ConfigOverride[0]);
|
CONFIG_OVERRIDES = overrides.toArray(new ConfigOverride[0]);
|
||||||
DROP_ALL_STATEMENTS = config.dropAllStatements();
|
DROP_ALL_STATEMENTS = config.dropAllStatements();
|
||||||
}
|
}
|
||||||
@@ -53,15 +71,7 @@ public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<Comm
|
|||||||
super(CommaFeedApplication.class, ResourceHelpers.resourceFilePath("config.test.yml"), CONFIG_OVERRIDES);
|
super(CommaFeedApplication.class, ResourceHelpers.resourceFilePath("config.test.yml"), CONFIG_OVERRIDES);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static DatabaseConfiguration buildConfiguration(String databaseName) {
|
private static DatabaseConfiguration buildConfiguration(String databaseName, String imageName) {
|
||||||
Properties properties = new Properties();
|
|
||||||
try (InputStream is = CommaFeedDropwizardAppExtension.class.getResourceAsStream("/docker-images.properties")) {
|
|
||||||
properties.load(is);
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException("could not read docker-images.properties", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
String imageName = properties.getProperty(databaseName);
|
|
||||||
if ("postgresql".equals(databaseName)) {
|
if ("postgresql".equals(databaseName)) {
|
||||||
JdbcDatabaseContainer<?> container = new PostgreSQLContainer<>(imageName).withTmpFs(Map.of("/var/lib/postgresql/data", "rw"));
|
JdbcDatabaseContainer<?> container = new PostgreSQLContainer<>(imageName).withTmpFs(Map.of("/var/lib/postgresql/data", "rw"));
|
||||||
return new DatabaseConfiguration(container, List.of("DROP SCHEMA public CASCADE", "CREATE SCHEMA public"));
|
return new DatabaseConfiguration(container, List.of("DROP SCHEMA public CASCADE", "CREATE SCHEMA public"));
|
||||||
@@ -77,6 +87,16 @@ public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<Comm
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Properties readProperties(String path) {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
try (InputStream is = CommaFeedDropwizardAppExtension.class.getResourceAsStream(path)) {
|
||||||
|
properties.load(is);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException("could not read resource " + path, e);
|
||||||
|
}
|
||||||
|
return properties;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void after() {
|
public void after() {
|
||||||
super.after();
|
super.after();
|
||||||
@@ -90,6 +110,13 @@ public class CommaFeedDropwizardAppExtension extends DropwizardAppExtension<Comm
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException("could not cleanup database", e);
|
throw new RuntimeException("could not cleanup database", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clean redis cache after each test
|
||||||
|
if (getConfiguration().getApplicationSettings().getCache() == CacheType.REDIS) {
|
||||||
|
try (JedisPool pool = getConfiguration().getRedisPoolFactory().build(); Jedis jedis = pool.getResource()) {
|
||||||
|
jedis.flushAll();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private record DatabaseConfiguration(JdbcDatabaseContainer<?> container, List<String> dropAllStatements) {
|
private record DatabaseConfiguration(JdbcDatabaseContainer<?> container, List<String> dropAllStatements) {
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
postgresql=postgres:${postgresql.image.version}
|
postgresql=postgres:${postgresql.image.version}
|
||||||
mysql=mysql:${mysql.image.version}
|
mysql=mysql:${mysql.image.version}
|
||||||
mariadb=mariadb:${mariadb.image.version}
|
mariadb=mariadb:${mariadb.image.version}
|
||||||
|
redis=redis:${redis.image.version}
|
||||||
Reference in New Issue
Block a user