Skip to content

Commit c96ead3

Browse files
ppkarwaszvy
andcommitted
Switch MongoDB tests to use Docker
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]>
1 parent bf97552 commit c96ead3

40 files changed

+611
-533
lines changed

BUILDING.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ You either need to have a user-level configuration in `~/.m2/toolchains.xml` or
8585
./mvnw verify -Pjava8-tests,!java8-incompat-fixes
8686
----
8787
88+
[#docker]
89+
=== Docker tests
90+
91+
Certain tests use Docker to spawn necessary external services.
92+
Docker tests are configured using the `docker` Maven profile, which is activated by default for the CI environment.
93+
You can locally enable this profile by passing a `-P docker` argument to your `./mvnw` commands.
94+
8895
[#website]
8996
== Building the website
9097

log4j-mongodb/pom.xml

Lines changed: 128 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,56 +30,59 @@
3030
<!-- OSGi and JPMS options -->
3131
<Fragment-Host>org.apache.logging.log4j.core</Fragment-Host>
3232
<!-- Dependency versions -->
33-
<mongodb5.version>5.1.3</mongodb5.version>
34-
<!-- TODO: Remove in next release after 2.24.0. -->
35-
<bnd.baseline.fail.on.missing>false</bnd.baseline.fail.on.missing>
36-
<bnd.baseline.skip>true</bnd.baseline.skip>
33+
<mongodb.version>5.1.4</mongodb.version>
34+
<slf4j2.version>2.0.15</slf4j2.version>
3735
</properties>
3836
<dependencyManagement>
3937
<dependencies>
38+
4039
<dependency>
4140
<groupId>org.mongodb</groupId>
4241
<artifactId>bson</artifactId>
43-
<version>${mongodb5.version}</version>
42+
<version>${mongodb.version}</version>
4443
</dependency>
44+
4545
<dependency>
4646
<groupId>org.mongodb</groupId>
4747
<artifactId>mongodb-driver-core</artifactId>
48-
<version>${mongodb5.version}</version>
49-
</dependency>
50-
<dependency>
51-
<groupId>org.mongodb</groupId>
52-
<artifactId>mongodb-driver-legacy</artifactId>
53-
<version>${mongodb5.version}</version>
48+
<version>${mongodb.version}</version>
5449
</dependency>
50+
5551
<dependency>
5652
<groupId>org.mongodb</groupId>
5753
<artifactId>mongodb-driver-sync</artifactId>
58-
<version>${mongodb5.version}</version>
54+
<version>${mongodb.version}</version>
5955
</dependency>
56+
6057
</dependencies>
6158
</dependencyManagement>
6259
<dependencies>
60+
6361
<dependency>
6462
<groupId>org.apache.logging.log4j</groupId>
6563
<artifactId>log4j-api</artifactId>
6664
</dependency>
65+
6766
<dependency>
6867
<groupId>org.apache.logging.log4j</groupId>
6968
<artifactId>log4j-mongodb4</artifactId>
7069
</dependency>
70+
7171
<dependency>
7272
<groupId>org.mongodb</groupId>
7373
<artifactId>bson</artifactId>
7474
</dependency>
75+
7576
<dependency>
7677
<groupId>org.mongodb</groupId>
7778
<artifactId>mongodb-driver-core</artifactId>
7879
</dependency>
80+
7981
<dependency>
8082
<groupId>org.mongodb</groupId>
8183
<artifactId>mongodb-driver-sync</artifactId>
8284
</dependency>
85+
8386
<dependency>
8487
<groupId>org.apache.logging.log4j</groupId>
8588
<artifactId>log4j-api-test</artifactId>
@@ -91,6 +94,7 @@
9194
</exclusion>
9295
</exclusions>
9396
</dependency>
97+
9498
<dependency>
9599
<groupId>org.apache.logging.log4j</groupId>
96100
<artifactId>log4j-core-test</artifactId>
@@ -102,40 +106,27 @@
102106
</exclusion>
103107
</exclusions>
104108
</dependency>
109+
105110
<dependency>
106111
<groupId>org.apache.commons</groupId>
107112
<artifactId>commons-lang3</artifactId>
108113
<scope>test</scope>
109114
</dependency>
110-
<dependency>
111-
<groupId>de.flapdoodle.embed</groupId>
112-
<artifactId>de.flapdoodle.embed.mongo</artifactId>
113-
<scope>test</scope>
114-
</dependency>
115-
<dependency>
116-
<groupId>de.flapdoodle.embed</groupId>
117-
<artifactId>de.flapdoodle.embed.process</artifactId>
118-
<scope>test</scope>
119-
</dependency>
120-
<dependency>
121-
<groupId>de.flapdoodle.reverse</groupId>
122-
<artifactId>de.flapdoodle.reverse</artifactId>
123-
<scope>test</scope>
124-
</dependency>
115+
125116
<dependency>
126117
<groupId>org.junit.jupiter</groupId>
127118
<artifactId>junit-jupiter-api</artifactId>
128119
<scope>test</scope>
129120
</dependency>
121+
130122
</dependencies>
131123
<build>
132124
<plugins>
133125
<plugin>
134126
<groupId>org.apache.maven.plugins</groupId>
135127
<artifactId>maven-surefire-plugin</artifactId>
136128
<configuration>
137-
<!-- TODO: fix concurrent download of MongoDB distribution. -->
138-
<forkCount>1</forkCount>
129+
<skip>true</skip>
139130
</configuration>
140131
<dependencies>
141132
<dependency>
@@ -145,6 +136,114 @@
145136
</dependency>
146137
</dependencies>
147138
</plugin>
139+
148140
</plugins>
149141
</build>
142+
143+
<profiles>
144+
<profile>
145+
146+
<id>docker</id>
147+
148+
<!--
149+
~ Only the `ubuntu` CI runners have access to Docker
150+
-->
151+
<activation>
152+
<os>
153+
<family>linux</family>
154+
</os>
155+
<property>
156+
<name>env.CI</name>
157+
<value>true</value>
158+
</property>
159+
</activation>
160+
161+
<build>
162+
<plugins>
163+
164+
<plugin>
165+
<groupId>io.fabric8</groupId>
166+
<artifactId>docker-maven-plugin</artifactId>
167+
<configuration>
168+
<verbose>all</verbose>
169+
<startParallel>true</startParallel>
170+
<autoCreateCustomNetworks>true</autoCreateCustomNetworks>
171+
<images>
172+
<image>
173+
<alias>mongo</alias>
174+
<name>mongo:latest</name>
175+
<run>
176+
<ports>
177+
<!--
178+
~ Binds an ephemeral port on the host to port 27017 in the container.
179+
~ Assigns the value of the port to the `mongo.port` property.
180+
-->
181+
<port>localhost:mongo.port:27017</port>
182+
</ports>
183+
</run>
184+
</image>
185+
</images>
186+
</configuration>
187+
<executions>
188+
<execution>
189+
<id>start-mongo</id>
190+
<goals>
191+
<goal>start</goal>
192+
</goals>
193+
</execution>
194+
<execution>
195+
<id>stop-mongo</id>
196+
<goals>
197+
<goal>stop</goal>
198+
</goals>
199+
</execution>
200+
</executions>
201+
</plugin>
202+
203+
<plugin>
204+
<groupId>org.apache.maven.plugins</groupId>
205+
<artifactId>maven-failsafe-plugin</artifactId>
206+
<dependencies>
207+
<dependency>
208+
<groupId>org.junit.jupiter</groupId>
209+
<artifactId>junit-jupiter-engine</artifactId>
210+
<version>${junit-jupiter.version}</version>
211+
</dependency>
212+
<dependency>
213+
<groupId>org.slf4j</groupId>
214+
<artifactId>slf4j-nop</artifactId>
215+
<version>${slf4j2.version}</version>
216+
</dependency>
217+
</dependencies>
218+
<executions>
219+
<execution>
220+
<goals>
221+
<goal>integration-test</goal>
222+
<goal>verify</goal>
223+
</goals>
224+
<configuration>
225+
<reuseForks>true</reuseForks>
226+
<includes>
227+
<include>**/*IT.java</include>
228+
</includes>
229+
<systemPropertyVariables>
230+
<!--
231+
~ Silence the tests.
232+
~ Annotate tests with `@UsingStatusListener` to see debug output on error
233+
-->
234+
<log4j.statusLogger.level>OFF</log4j.statusLogger.level>
235+
<!-- The `mongo.port` variable is created by `docker-maven-plugin` -->
236+
<log4j.mongo.port>${mongo.port}</log4j.mongo.port>
237+
</systemPropertyVariables>
238+
</configuration>
239+
</execution>
240+
</executions>
241+
</plugin>
242+
243+
</plugins>
244+
</build>
245+
246+
</profile>
247+
</profiles>
248+
150249
</project>

log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/AbstractMongoDbCappedTest.java renamed to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/AbstractMongoDbCappedIT.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@
2323
import org.apache.logging.log4j.core.LoggerContext;
2424
import org.bson.Document;
2525
import org.junit.jupiter.api.Assertions;
26-
import org.junit.jupiter.api.Test;
2726

28-
public abstract class AbstractMongoDbCappedTest {
27+
abstract class AbstractMongoDbCappedIT {
2928

30-
@Test
31-
public void test(final LoggerContext ctx, final MongoClient mongoClient) {
32-
final Logger logger = ctx.getLogger(AbstractMongoDbCappedTest.class);
29+
protected void test(final LoggerContext ctx, final MongoClient mongoClient) {
30+
final Logger logger = ctx.getLogger(AbstractMongoDbCappedIT.class);
3331
logger.info("Hello log");
3432
final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME);
3533
Assertions.assertNotNull(database);
36-
final MongoCollection<Document> collection = database.getCollection(MongoDbTestConstants.COLLECTION_NAME);
34+
final MongoCollection<Document> collection =
35+
database.getCollection(getClass().getSimpleName());
3736
Assertions.assertNotNull(collection);
3837
final Document first = collection.find().first();
3938
Assertions.assertNotNull(first);

log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAdditionalFieldsTest.java renamed to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAdditionalFieldsIT.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,25 @@
2626
import org.apache.logging.log4j.Logger;
2727
import org.apache.logging.log4j.core.LoggerContext;
2828
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
29+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
2930
import org.bson.Document;
3031
import org.junit.jupiter.api.Test;
3132

3233
@UsingMongoDb
33-
@LoggerContextSource("log4j2-mongodb-additional-fields.xml")
34-
public class MongoDbAdditionalFieldsTest {
34+
@LoggerContextSource("MongoDbAdditionalFields.xml")
35+
// Print debug status logger output upon failure
36+
@UsingStatusListener
37+
class MongoDbAdditionalFieldsIT {
3538

3639
@Test
37-
public void test(final LoggerContext ctx, final MongoClient mongoClient) {
38-
final Logger logger = ctx.getLogger(MongoDbAdditionalFieldsTest.class);
40+
void test(final LoggerContext ctx, final MongoClient mongoClient) {
41+
final Logger logger = ctx.getLogger(MongoDbAdditionalFieldsIT.class);
3942
logger.info("Hello log 1");
4043
logger.info("Hello log 2", new RuntimeException("Hello ex 2"));
4144
final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME);
4245
assertNotNull(database);
43-
final MongoCollection<Document> collection = database.getCollection(MongoDbTestConstants.COLLECTION_NAME);
46+
final MongoCollection<Document> collection =
47+
database.getCollection(getClass().getSimpleName());
4448
assertNotNull(collection);
4549
final FindIterable<Document> found = collection.find();
4650
final Document first = found.first();

log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthFailureTest.java renamed to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthFailureIT.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,25 @@
2525
import org.apache.logging.log4j.Logger;
2626
import org.apache.logging.log4j.core.LoggerContext;
2727
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
28+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
2829
import org.bson.Document;
2930
import org.junit.jupiter.api.Test;
3031

3132
@UsingMongoDb
32-
@LoggerContextSource("log4j2-mongodb-auth-failure.xml")
33-
public class MongoDbAuthFailureTest {
33+
@LoggerContextSource("MongoDbAuthFailureIT.xml")
34+
// Print debug status logger output upon failure
35+
@UsingStatusListener
36+
class MongoDbAuthFailureIT {
3437

3538
@Test
36-
public void test(final LoggerContext ctx, final MongoClient mongoClient) {
37-
final Logger logger = ctx.getLogger(MongoDbAuthFailureTest.class);
39+
void test(final LoggerContext ctx, final MongoClient mongoClient) {
40+
final Logger logger = ctx.getLogger(MongoDbAuthFailureIT.class);
3841
logger.info("Hello log");
3942
final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME);
4043
assertNotNull(database);
41-
final MongoCollection<Document> collection = database.getCollection(MongoDbTestConstants.DATABASE_NAME);
44+
final MongoCollection<Document> collection =
45+
database.getCollection(getClass().getSimpleName());
46+
;
4247
assertNotNull(collection);
4348
final Document first = collection.find().first();
4449
assertNull(first);

log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedLongTest.java renamed to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedIntIT.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@
1616
*/
1717
package org.apache.logging.log4j.mongodb;
1818

19+
import com.mongodb.client.MongoClient;
20+
import org.apache.logging.log4j.core.LoggerContext;
1921
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
22+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
23+
import org.junit.jupiter.api.Test;
2024

2125
@UsingMongoDb
22-
@LoggerContextSource("log4j2-mongodb-capped-long.xml")
23-
public class MongoDbCappedLongTest extends AbstractMongoDbCappedTest {
26+
@LoggerContextSource("MongoDbCappedIntIT.xml")
27+
// Print debug status logger output upon failure
28+
@UsingStatusListener
29+
class MongoDbCappedIntIT extends AbstractMongoDbCappedIT {
2430

25-
// test is in superclass
31+
@Test
32+
@Override
33+
protected void test(LoggerContext ctx, MongoClient mongoClient) {
34+
super.test(ctx, mongoClient);
35+
}
2636
}

log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedIntTest.java renamed to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedLongIT.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@
1616
*/
1717
package org.apache.logging.log4j.mongodb;
1818

19+
import com.mongodb.client.MongoClient;
20+
import org.apache.logging.log4j.core.LoggerContext;
1921
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
22+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
23+
import org.junit.jupiter.api.Test;
2024

2125
@UsingMongoDb
22-
@LoggerContextSource("log4j2-mongodb-capped-int.xml")
23-
public class MongoDbCappedIntTest extends AbstractMongoDbCappedTest {
26+
@LoggerContextSource("MongoDbCappedLongIT.xml")
27+
// Print debug status logger output upon failure
28+
@UsingStatusListener
29+
class MongoDbCappedLongIT extends AbstractMongoDbCappedIT {
2430

25-
// test is in superclass
31+
@Test
32+
@Override
33+
protected void test(LoggerContext ctx, MongoClient mongoClient) {
34+
super.test(ctx, mongoClient);
35+
}
2636
}

0 commit comments

Comments
 (0)