Skip to content

Commit

Permalink
Merge pull request quarkusio#45330 from danilopiazza/mongodb-devservi…
Browse files Browse the repository at this point in the history
…ces-disable-when-hosts-is-present

Disable MongoDB Dev Services when the hosts property is provided
  • Loading branch information
loicmathieu authored Jan 3, 2025
2 parents 4f06e76 + 7a280d3 commit 7a7876c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/src/main/asciidoc/mongodb-dev-services.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ https://github.com/quarkusio/quarkus/tree/main/docs/src/main/asciidoc
= Dev Services for MongoDB

Quarkus supports a feature called Dev Services that allows you to create various datasources without any config. In the case of MongoDB this support extends to the default MongoDB connection.
What that means practically, is that if you have not configured `quarkus.mongodb.connection-string`, Quarkus will automatically start a MongoDB container when running tests or in dev mode,
and automatically configure the connection.
What that means practically, is that if you have not configured `quarkus.mongodb.connection-string` nor `quarkus.mongodb.hosts`, Quarkus will automatically start a MongoDB container when
running tests or in dev mode, and automatically configure the connection.

MongoDB Dev Services is based on link:https://www.testcontainers.org/modules/databases/mongodb/[Testcontainers MongoDB module] that will start a single node replicaset.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@ private RunningDevService startMongo(DockerStatusBuildItem dockerStatusBuildItem

String configPrefix = getConfigPrefix(connectionName);

// TODO: do we need to check the hosts as well?
boolean needToStart = !ConfigUtils.isPropertyNonEmpty(configPrefix + "connection-string");
boolean needToStart = !ConfigUtils.isPropertyNonEmpty(configPrefix + "connection-string")
&& !ConfigUtils.isPropertyNonEmpty(configPrefix + "hosts");
if (!needToStart) {
// a connection string has been provided
log.debug("Not starting devservices for " + (isDefault(connectionName) ? "default datasource" : connectionName)
+ " as a connection string has been provided");
+ " as a connection string and/or server addresses have been provided");
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
quarkus.mongodb.parameter-injection.write-concern.journal=true

quarkus.mongodb.with-connection-string.connection-string=mongodb://localhost:27017

quarkus.mongodb.with-hosts.hosts=localhost:27017
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.quarkus.it.mongodb;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.not;

import jakarta.inject.Inject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import com.mongodb.client.MongoClient;

import io.quarkus.mongodb.MongoClientName;
import io.quarkus.test.common.DevServicesContext;
import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.mongodb.MongoTestResource;

@QuarkusTest
@QuarkusTestResource(value = MongoTestResource.class)
public class DevServicesStartTest {

DevServicesContext context;

@Inject
@MongoClientName("with-connection-string")
MongoClient clientWithConnectionString;

@Inject
@MongoClientName("with-hosts")
MongoClient clientWithHosts;

@ParameterizedTest
@ValueSource(strings = {
"quarkus.mongodb.with-connection-string.connection-string",
"quarkus.mongodb.with-hosts.connection-string"
})
public void testDevServicesNotStarted(String property) {
assertThat(context.devServicesProperties(), not(hasKey(property)));
}

@Test
public void testDevServicesStarted() {
assertThat(context.devServicesProperties(), hasKey("quarkus.mongodb.connection-string"));
}
}

0 comments on commit 7a7876c

Please sign in to comment.