Skip to content

Switch MongoDB tests to use Docker #2922

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

Merged
merged 1 commit into from
Sep 13, 2024
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
7 changes: 7 additions & 0 deletions BUILDING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ To remedy this, you can execute the following:
printf '127.0.0.1 %s\n::1 %s\n' `hostname` `hostname` | sudo tee -a /etc/hosts
----

[#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
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,10 @@ public MergeStrategy defaultMergeStrategy() {
@SingletonFactory
@Named("StatusLogger")
@ConditionalOnMissingBinding
public Level defaultStatusLevel() {
return Level.ERROR;
public Level defaultStatusLevel(PropertyEnvironment environment) {
return environment
.getProperty(CoreProperties.StatusLoggerProperties.class)
.level();
}

@SingletonFactory
Expand Down
162 changes: 145 additions & 17 deletions log4j-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,63 @@
~ OSGi and JPMS options
-->
<Fragment-Host>org.apache.logging.log4j.core</Fragment-Host>

<!-- Dependency properties -->
<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>${mongodb.version}</version>
</dependency>

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

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<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-core</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 @@ -69,6 +103,7 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core-test</artifactId>
Expand All @@ -88,31 +123,19 @@
</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>
Expand All @@ -122,8 +145,7 @@
<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 @@ -142,4 +164,110 @@
</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