Skip to content

Commit

Permalink
Switch MongoDB tests to use Docker
Browse files Browse the repository at this point in the history
MongoDB is a binary server. The current `log4j-mongodb`
tests download a **generic** binary MongoDB distribution
and try to run it. The distribution is not self-contained
and requires several libraries (e.g., OpenSSL) to be available
on the target host.

Those libraries are not always available in the required version:
e.g., currently MongoDB needs OpenSSL 1, but OpenSSL 3 is bundled
in the most recent Debian.

This PR switches from the binary distribution to the usage of
the **latest** Docker image available.

The advantages of this approach are:

- We always test against the newest server version available.
- The success of the tests does not depend on the libraries
  installed on the host.
- Tests can run in parallel. In the current approach, parallel
  tests failed since each one tried to download MongoDB
  separately.

The main disadvantage is that Docker will be required to test
`log4j-mongodb`. This is the case for the CI, but individual
developers might need to install it too.

Co-authored-by: Volkan Yazıcı <[email protected]>
  • Loading branch information
ppkarwasz and vy committed Sep 13, 2024
1 parent 77340db commit 8c4e45d
Show file tree
Hide file tree
Showing 40 changed files with 610 additions and 532 deletions.
7 changes: 7 additions & 0 deletions BUILDING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ You either need to have a user-level configuration in `~/.m2/toolchains.xml` or
./mvnw verify -Pjava8-tests,!java8-incompat-fixes
----
[#docker]
=== Docker tests
Certain tests use Docker to spawn necessary external services.
Docker tests are configured using the `docker` Maven profile, which is activated by default for the CI environment.
You can locally enable this profile by passing a `-P docker` argument to your `./mvnw` commands.
[#website]
== Building the website
Expand Down
157 changes: 128 additions & 29 deletions log4j-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,56 +30,59 @@
<!-- OSGi and JPMS options -->
<Fragment-Host>org.apache.logging.log4j.core</Fragment-Host>
<!-- Dependency versions -->
<mongodb5.version>5.1.4</mongodb5.version>
<!-- TODO: Remove in next release after 2.24.0. -->
<bnd.baseline.fail.on.missing>false</bnd.baseline.fail.on.missing>
<bnd.baseline.skip>true</bnd.baseline.skip>
<mongodb.version>5.1.3</mongodb.version>
<slf4j2.version>2.0.15</slf4j2.version>
</properties>
<dependencyManagement>
<dependencies>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<version>${mongodb5.version}</version>
<version>${mongodb.version}</version>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>${mongodb5.version}</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-legacy</artifactId>
<version>${mongodb5.version}</version>
<version>${mongodb.version}</version>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongodb5.version}</version>
<version>${mongodb.version}</version>
</dependency>

</dependencies>
</dependencyManagement>
<dependencies>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-mongodb4</artifactId>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api-test</artifactId>
Expand All @@ -91,6 +94,7 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core-test</artifactId>
Expand All @@ -102,40 +106,27 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.process</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>de.flapdoodle.reverse</groupId>
<artifactId>de.flapdoodle.reverse</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- TODO: fix concurrent download of MongoDB distribution. -->
<forkCount>1</forkCount>
<skip>true</skip>
</configuration>
<dependencies>
<dependency>
Expand All @@ -145,6 +136,114 @@
</dependency>
</dependencies>
</plugin>

</plugins>
</build>

<profiles>
<profile>

<id>docker</id>

<!--
~ Only the `ubuntu` CI runners have access to Docker
-->
<activation>
<os>
<family>linux</family>
</os>
<property>
<name>env.CI</name>
<value>true</value>
</property>
</activation>

<build>
<plugins>

<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<verbose>all</verbose>
<startParallel>true</startParallel>
<autoCreateCustomNetworks>true</autoCreateCustomNetworks>
<images>
<image>
<alias>mongo</alias>
<name>mongo:latest</name>
<run>
<ports>
<!--
~ Binds an ephemeral port on the host to port 27017 in the container.
~ Assigns the value of the port to the `mongo.port` property.
-->
<port>localhost:mongo.port:27017</port>
</ports>
</run>
</image>
</images>
</configuration>
<executions>
<execution>
<id>start-mongo</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-mongo</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>${slf4j2.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<reuseForks>true</reuseForks>
<includes>
<include>**/*IT.java</include>
</includes>
<systemPropertyVariables>
<!--
~ Silence the tests.
~ Annotate tests with `@UsingStatusListener` to see debug output on error
-->
<log4j.statusLogger.level>OFF</log4j.statusLogger.level>
<!-- The `mongo.port` variable is created by `docker-maven-plugin` -->
<log4j.mongo.port>${mongo.port}</log4j.mongo.port>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@
import org.apache.logging.log4j.core.LoggerContext;
import org.bson.Document;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public abstract class AbstractMongoDbCappedTest {
abstract class AbstractMongoDbCappedIT {

@Test
public void test(final LoggerContext ctx, final MongoClient mongoClient) {
final Logger logger = ctx.getLogger(AbstractMongoDbCappedTest.class);
protected void test(final LoggerContext ctx, final MongoClient mongoClient) {
final Logger logger = ctx.getLogger(AbstractMongoDbCappedIT.class);
logger.info("Hello log");
final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME);
Assertions.assertNotNull(database);
final MongoCollection<Document> collection = database.getCollection(MongoDbTestConstants.COLLECTION_NAME);
final MongoCollection<Document> collection =
database.getCollection(getClass().getSimpleName());
Assertions.assertNotNull(collection);
final Document first = collection.find().first();
Assertions.assertNotNull(first);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingStatusListener;
import org.bson.Document;
import org.junit.jupiter.api.Test;

@UsingMongoDb
@LoggerContextSource("log4j2-mongodb-additional-fields.xml")
public class MongoDbAdditionalFieldsTest {
@LoggerContextSource("MongoDbAdditionalFields.xml")
// Print debug status logger output upon failure
@UsingStatusListener
class MongoDbAdditionalFieldsIT {

@Test
public void test(final LoggerContext ctx, final MongoClient mongoClient) {
final Logger logger = ctx.getLogger(MongoDbAdditionalFieldsTest.class);
void test(final LoggerContext ctx, final MongoClient mongoClient) {
final Logger logger = ctx.getLogger(MongoDbAdditionalFieldsIT.class);
logger.info("Hello log 1");
logger.info("Hello log 2", new RuntimeException("Hello ex 2"));
final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME);
assertNotNull(database);
final MongoCollection<Document> collection = database.getCollection(MongoDbTestConstants.COLLECTION_NAME);
final MongoCollection<Document> collection =
database.getCollection(getClass().getSimpleName());
assertNotNull(collection);
final FindIterable<Document> found = collection.find();
final Document first = found.first();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,25 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingStatusListener;
import org.bson.Document;
import org.junit.jupiter.api.Test;

@UsingMongoDb
@LoggerContextSource("log4j2-mongodb-auth-failure.xml")
public class MongoDbAuthFailureTest {
@LoggerContextSource("MongoDbAuthFailureIT.xml")
// Print debug status logger output upon failure
@UsingStatusListener
class MongoDbAuthFailureIT {

@Test
public void test(final LoggerContext ctx, final MongoClient mongoClient) {
final Logger logger = ctx.getLogger(MongoDbAuthFailureTest.class);
void test(final LoggerContext ctx, final MongoClient mongoClient) {
final Logger logger = ctx.getLogger(MongoDbAuthFailureIT.class);
logger.info("Hello log");
final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME);
assertNotNull(database);
final MongoCollection<Document> collection = database.getCollection(MongoDbTestConstants.DATABASE_NAME);
final MongoCollection<Document> collection =
database.getCollection(getClass().getSimpleName());
;
assertNotNull(collection);
final Document first = collection.find().first();
assertNull(first);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@
*/
package org.apache.logging.log4j.mongodb;

import com.mongodb.client.MongoClient;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingStatusListener;
import org.junit.jupiter.api.Test;

@UsingMongoDb
@LoggerContextSource("log4j2-mongodb-capped-long.xml")
public class MongoDbCappedLongTest extends AbstractMongoDbCappedTest {
@LoggerContextSource("MongoDbCappedIntIT.xml")
// Print debug status logger output upon failure
@UsingStatusListener
class MongoDbCappedIntIT extends AbstractMongoDbCappedIT {

// test is in superclass
@Test
@Override
protected void test(LoggerContext ctx, MongoClient mongoClient) {
super.test(ctx, mongoClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@
*/
package org.apache.logging.log4j.mongodb;

import com.mongodb.client.MongoClient;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.UsingStatusListener;
import org.junit.jupiter.api.Test;

@UsingMongoDb
@LoggerContextSource("log4j2-mongodb-capped-int.xml")
public class MongoDbCappedIntTest extends AbstractMongoDbCappedTest {
@LoggerContextSource("MongoDbCappedLongIT.xml")
// Print debug status logger output upon failure
@UsingStatusListener
class MongoDbCappedLongIT extends AbstractMongoDbCappedIT {

// test is in superclass
@Test
@Override
protected void test(LoggerContext ctx, MongoClient mongoClient) {
super.test(ctx, mongoClient);
}
}
Loading

0 comments on commit 8c4e45d

Please sign in to comment.