Skip to content

Commit 19910ff

Browse files
authored
Use Testcontainers for running MongoDB registry unit tests
The flapdoodle library used for starting an embedded Mongo DB before running the unit tests of the Mongo DB based registry doesn't support Mongo DB versions > 4.0. The testcontainers framework provides more flexibility in that regard. Consequently, the GitHub Actions workflow has been changed to no longer cache embedded Mongo DB artifacts. Signed-off-by: Kai Hudalla <[email protected]>
1 parent 5e7f033 commit 19910ff

File tree

6 files changed

+51
-148
lines changed

6 files changed

+51
-148
lines changed

.github/workflows/ci.yml

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2020 Contributors to the Eclipse Foundation
1+
# Copyright (c) 2020, 2021 Contributors to the Eclipse Foundation
22
#
33
# See the NOTICE file(s) distributed with this work for additional
44
# information regarding copyright ownership.
@@ -43,13 +43,6 @@ jobs:
4343
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
4444
restore-keys: |
4545
${{ runner.os }}-maven-
46-
- name: Cache embedded MongoDB for unit tests
47-
uses: actions/cache@v2
48-
with:
49-
path: ~/.embedmongo/**/*.tgz
50-
key: ${{ runner.os }}-mongodb-${{ hashFiles('**/pom.xml') }}
51-
restore-keys: |
52-
${{ runner.os }}-mongodb-
5346
- name: Set up JDK 11
5447
uses: actions/setup-java@v1
5548
with:

bom/pom.xml

+13-27
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
<californium.version>2.6.0</californium.version>
3636
<commons-cli.version>1.2</commons-cli.version>
3737
<dispatch-router.image.name>quay.io/interconnectedcloud/qdrouterd:1.14.0</dispatch-router.image.name>
38-
<flapdoodle.version>2.2.0</flapdoodle.version>
3938
<guava.version>28.0-jre</guava.version>
4039
<gson.version>2.8.5</gson.version>
4140
<h2.version>1.4.200</h2.version>
@@ -57,7 +56,7 @@
5756
<mchange-commons.version>0.2.15</mchange-commons.version>
5857
<micrometer.version>1.6.2</micrometer.version>
5958
<mockito.version>3.6.28</mockito.version>
60-
<mongodb.version>4.0.20</mongodb.version>
59+
<mongodb-image.name>mongo:4.2.11</mongodb-image.name>
6160
<netty.version>4.1.49.Final</netty.version>
6261
<netty.tcnative.version>2.0.30.Final</netty.tcnative.version>
6362
<opentracing.version>0.33.0</opentracing.version>
@@ -547,6 +546,18 @@
547546
<version>${jaeger.version}</version>
548547
<scope>runtime</scope>
549548
</dependency>
549+
<dependency>
550+
<groupId>io.jaegertracing</groupId>
551+
<artifactId>jaeger-core</artifactId>
552+
<version>${jaeger.version}</version>
553+
<scope>runtime</scope>
554+
</dependency>
555+
<dependency>
556+
<groupId>io.jaegertracing</groupId>
557+
<artifactId>jaeger-thrift</artifactId>
558+
<version>${jaeger.version}</version>
559+
<scope>runtime</scope>
560+
</dependency>
550561

551562
<!-- Infinispan -->
552563
<dependency>
@@ -803,12 +814,6 @@
803814
<version>${project.version}</version>
804815
<scope>test</scope>
805816
</dependency>
806-
<dependency>
807-
<groupId>de.flapdoodle.embed</groupId>
808-
<artifactId>de.flapdoodle.embed.mongo</artifactId>
809-
<version>${flapdoodle.version}</version>
810-
<scope>test</scope>
811-
</dependency>
812817
<dependency>
813818
<groupId>org.apache.qpid</groupId>
814819
<artifactId>qpid-jms-client</artifactId>
@@ -869,31 +874,12 @@
869874
<version>${hamcrest-core.version}</version>
870875
<scope>test</scope>
871876
</dependency>
872-
<dependency>
873-
<groupId>org.testcontainers</groupId>
874-
<artifactId>testcontainers-bom</artifactId>
875-
<version>${testcontainers.version}</version>
876-
<type>pom</type>
877-
<scope>import</scope>
878-
</dependency>
879877
<dependency>
880878
<groupId>org.infinispan</groupId>
881879
<artifactId>infinispan-query-dsl</artifactId>
882880
<version>${infinispan.version}</version>
883881
<scope>test</scope>
884882
</dependency>
885-
<dependency>
886-
<groupId>io.jaegertracing</groupId>
887-
<artifactId>jaeger-core</artifactId>
888-
<version>${jaeger.version}</version>
889-
<scope>runtime</scope>
890-
</dependency>
891-
<dependency>
892-
<groupId>io.jaegertracing</groupId>
893-
<artifactId>jaeger-thrift</artifactId>
894-
<version>${jaeger.version}</version>
895-
<scope>runtime</scope>
896-
</dependency>
897883
</dependencies>
898884
</dependencyManagement>
899885

services/device-registry-jdbc/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<dependency>
6666
<groupId>org.testcontainers</groupId>
6767
<artifactId>postgresql</artifactId>
68+
<version>${testcontainers.version}</version>
6869
<scope>test</scope>
6970
</dependency>
7071
<dependency>

services/device-registry-mongodb/pom.xml

+25-104
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,6 @@
3232
<groupId>org.eclipse.hono</groupId>
3333
<artifactId>hono-service-device-registry-base</artifactId>
3434
</dependency>
35-
<dependency>
36-
<groupId>org.eclipse.hono</groupId>
37-
<artifactId>hono-service-device-registry-base</artifactId>
38-
<version>${project.version}</version>
39-
<classifier>tests</classifier>
40-
<type>test-jar</type>
41-
<scope>test</scope>
42-
</dependency>
4335
<dependency>
4436
<groupId>io.vertx</groupId>
4537
<artifactId>vertx-mongo-client</artifactId>
@@ -49,104 +41,31 @@
4941
<artifactId>vertx-auth-mongo</artifactId>
5042
</dependency>
5143
<dependency>
52-
<groupId>de.flapdoodle.embed</groupId>
53-
<artifactId>de.flapdoodle.embed.mongo</artifactId>
44+
<groupId>org.eclipse.hono</groupId>
45+
<artifactId>hono-service-device-registry-base</artifactId>
46+
<version>${project.version}</version>
47+
<classifier>tests</classifier>
48+
<type>test-jar</type>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.testcontainers</groupId>
53+
<artifactId>mongodb</artifactId>
54+
<version>${testcontainers.version}</version>
5455
<scope>test</scope>
5556
</dependency>
5657
</dependencies>
5758

5859
<build>
5960
<plugins>
60-
<plugin>
61-
<groupId>com.github.joelittlejohn.embedmongo</groupId>
62-
<artifactId>embedmongo-maven-plugin</artifactId>
63-
<version>0.4.2</version>
64-
<executions>
65-
<execution>
66-
<id>start-mongodb</id>
67-
<phase>pre-integration-test</phase>
68-
<goals>
69-
<goal>start</goal>
70-
</goals>
71-
<configuration>
72-
<randomPort>true</randomPort>
73-
<!-- optional, default is false, if true allocates a random port and overrides embedmongo.port -->
74-
75-
<version>${mongodb.version}</version>
76-
<!-- optional, default 2.2.1 -->
77-
78-
<features>ONLY_WITH_SSL, ONLY_WINDOWS_2008_SERVER, NO_HTTP_INTERFACE_ARG</features>
79-
<!-- optional, default is none. Enables flapdoodle.embed.mongo features, for example to build Windows
80-
download URLs since 3.6 -->
81-
82-
<!-- databaseDirectory>/tmp/mongotest</databaseDirectory -->
83-
<!-- optional, default is a new dir in java.io.tmpdir -->
84-
85-
<logging>none</logging>
86-
<!-- optional (file|console|none), default console -->
87-
88-
<logFile>${project.build.directory}/myfile.log</logFile>
89-
<!-- optional, can be used when logging=file, default is ./embedmongo.log -->
90-
91-
<logFileEncoding>utf-8</logFileEncoding>
92-
<!-- optional, can be used when logging=file, default is utf-8 -->
93-
94-
<bindIp>127.0.0.1</bindIp>
95-
<!-- optional, default is to listen on all interfaces -->
96-
97-
<!-- downloadPath>http://internal-mongo-repo/</downloadPath -->
98-
<!-- optional, default is http://fastdl.mongodb.org/ -->
99-
100-
<unixSocketPrefix>${user.home}/.embedmongo</unixSocketPrefix>
101-
<!-- optional, default is /tmp -->
102-
103-
<storageEngine>wiredTiger</storageEngine>
104-
<!--optional, one of wiredTiger or mmapv1 (default is mmapv1) -->
105-
106-
<skip>false</skip>
107-
<!-- optional, skips this plugin entirely, use on the command line like -Dembedmongo.skip -->
108-
109-
</configuration>
110-
</execution>
111-
<execution>
112-
<id>stop-mongodb</id>
113-
<phase>post-integration-test</phase>
114-
<goals>
115-
<goal>stop</goal>
116-
</goals>
117-
</execution>
118-
</executions>
119-
</plugin>
12061
<plugin>
12162
<groupId>org.apache.maven.plugins</groupId>
12263
<artifactId>maven-surefire-plugin</artifactId>
12364
<configuration>
124-
<skip>true</skip>
125-
</configuration>
126-
</plugin>
127-
<plugin>
128-
<groupId>org.apache.maven.plugins</groupId>
129-
<artifactId>maven-failsafe-plugin</artifactId>
130-
<configuration>
131-
<includes>
132-
<include>**/*Test.java</include>
133-
</includes>
134-
<systemPropertyVariables>
135-
<mongodb.port>${embedmongo.port}</mongodb.port>
136-
<vertx.logger-delegate-factory-class-name>io.vertx.core.logging.SLF4JLogDelegateFactory</vertx.logger-delegate-factory-class-name>
137-
<!-- javax.net.debug>ssl:handshake</javax.net.debug -->
138-
</systemPropertyVariables>
65+
<systemProperties>
66+
<mongoDbImageName>${mongodb-image.name}</mongoDbImageName>
67+
</systemProperties>
13968
</configuration>
140-
<executions>
141-
<execution>
142-
<id>run-tests</id>
143-
<phase>integration-test</phase>
144-
<goals>
145-
<goal>integration-test</goal>
146-
<goal>verify</goal>
147-
</goals>
148-
</execution>
149-
</executions>
15069
</plugin>
15170
</plugins>
15271
</build>
@@ -203,15 +122,6 @@
203122
</build>
204123
</profile>
205124

206-
<profile>
207-
<id>jaeger</id>
208-
<dependencies>
209-
<dependency>
210-
<groupId>io.jaegertracing</groupId>
211-
<artifactId>jaeger-client</artifactId>
212-
</dependency>
213-
</dependencies>
214-
</profile>
215125
<profile>
216126
<id>docker-push-image</id>
217127
<build>
@@ -232,6 +142,17 @@
232142
</plugins>
233143
</build>
234144
</profile>
145+
146+
<profile>
147+
<id>jaeger</id>
148+
<dependencies>
149+
<dependency>
150+
<groupId>io.jaegertracing</groupId>
151+
<artifactId>jaeger-client</artifactId>
152+
</dependency>
153+
</dependencies>
154+
</profile>
155+
235156
</profiles>
236157

237158
</project>

services/device-registry-mongodb/src/test/java/org/eclipse/hono/deviceregistry/mongodb/service/MongoDbTestUtils.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import java.util.Objects;
1616

1717
import org.eclipse.hono.deviceregistry.mongodb.config.MongoDbConfigProperties;
18+
import org.testcontainers.containers.MongoDBContainer;
19+
import org.testcontainers.utility.DockerImageName;
1820

1921
import io.vertx.core.Vertx;
2022
import io.vertx.ext.mongo.MongoClient;
@@ -24,8 +26,13 @@
2426
*/
2527
public final class MongoDbTestUtils {
2628

27-
private static final String MONGO_DB_HOST = System.getProperty("mongodb.host", "127.0.0.1");
28-
private static final int MONGO_DB_PORT = Integer.getInteger("mongodb.port", -1);
29+
private static final MongoDBContainer MONGO_DB_CONTAINER;
30+
private static final String MONGO_DB_IMAGE_NAME = System.getProperty("mongoDbImageName", "mongo:4.2");
31+
32+
static {
33+
MONGO_DB_CONTAINER = new MongoDBContainer(DockerImageName.parse(MONGO_DB_IMAGE_NAME));
34+
MONGO_DB_CONTAINER.start();
35+
}
2936

3037
private MongoDbTestUtils() {
3138
// prevent instantiation
@@ -44,13 +51,8 @@ public static MongoClient getMongoClient(final Vertx vertx, final String dbName)
4451
Objects.requireNonNull(vertx);
4552
Objects.requireNonNull(dbName);
4653

47-
if (MONGO_DB_PORT == -1) {
48-
throw new RuntimeException("Missing 'mongodb.port' system property; ensure test is run via maven.");
49-
}
5054
final MongoDbConfigProperties mongoDbConfig = new MongoDbConfigProperties()
51-
.setHost(MongoDbTestUtils.MONGO_DB_HOST)
52-
.setPort(MongoDbTestUtils.MONGO_DB_PORT)
53-
.setDbName(dbName);
55+
.setConnectionString(MONGO_DB_CONTAINER.getReplicaSetUrl(dbName));
5456
return MongoClient.createShared(vertx, mongoDbConfig.getMongoClientConfig());
5557
}
5658
}

tests/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ Test cases are run against Docker images of Hono server + (Apache Qpid Dispatch
466466
<build>
467467
<skip>${hono.mongodb.disabled}</skip>
468468
<imagePullPolicy>IfNotPresent</imagePullPolicy>
469-
<from>mongo:${mongodb.version}</from>
469+
<from>${mongodb-image.name}</from>
470470
<assembly>
471471
<mode>dir</mode>
472472
<basedir>/</basedir>

0 commit comments

Comments
 (0)