Skip to content

Commit

Permalink
Enable Mongo tests to run with container image versions < 7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnetherton committed Nov 18, 2024
1 parent 066b9de commit 8cc76ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.TestcontainersConfiguration;

import static org.apache.camel.quarkus.test.support.mongodb.MongoDbTestSupportUtils.getMongoScriptExecutable;

public class MongoDbTestResource implements QuarkusTestResourceLifecycleManager {
private static final Logger LOGGER = LoggerFactory.getLogger(MongoDbTestResource.class);

Expand Down Expand Up @@ -104,7 +106,7 @@ private void execScriptInContainer(String script) throws Exception {
String[] cmds = cmd.split("\\n\\n");

for (int i = 0; i < cmds.length; i++) {
Container.ExecResult er = container.execInContainer(new String[] { "mongosh", "--eval", cmds[i] });
Container.ExecResult er = container.execInContainer(getMongoScriptExecutable(MONGO_IMAGE), "--eval", cmds[i]);
if (er.getExitCode() != 0) {
throw new IllegalStateException("Exec exit code " + er.getExitCode() + ". " + er.getStderr());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.quarkus.test.support.mongodb;

public class MongoDbTestSupportUtils {
private MongoDbTestSupportUtils() {
// Utility class
}

public static String getMongoScriptExecutable(String imageName) {
int major = getMongoMajorVersion(imageName);
if (major < 6) {
return "mongo";
}
return "mongosh";
}

static int getMongoMajorVersion(String imageName) {
String[] imageNameParts = imageName.split(":");
if (imageNameParts.length == 1) {
// Assume it's an image name without a tag. E.g 'latest'.
return 999;
}

String[] versionParts = imageNameParts[1].split("\\.");
if (versionParts.length == 0) {
throw new IllegalArgumentException("Invalid image version: " + imageName);
}

return Integer.parseInt(versionParts[0]);
}
}
2 changes: 1 addition & 1 deletion integration-tests/debezium/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-integration-test-support</artifactId>
<artifactId>camel-quarkus-integration-tests-support-mongodb</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;

import static org.apache.camel.quarkus.test.support.mongodb.MongoDbTestSupportUtils.getMongoScriptExecutable;

public class DebeziumMongodbTestResource extends AbstractDebeziumTestResource<GenericContainer<?>> {
private static final Logger LOG = Logger.getLogger(AbstractDebeziumTestResource.class);
private static final String PRIVATE_HOST = "mongodb_private";
Expand Down Expand Up @@ -79,7 +81,8 @@ private void execScriptInContainer() throws Exception {
String script = IOUtils.toString(resource, StandardCharsets.UTF_8);
script = script.replace("%container-host%", getHostPort());
for (String cmd : script.split("\\n\\n")) {
Container.ExecResult er = container.execInContainer("mongosh", "--port", String.valueOf(DB_PORT), "--eval", cmd);
Container.ExecResult er = container.execInContainer(getMongoScriptExecutable(MONGO_IMAGE_NAME), "--port",
String.valueOf(DB_PORT), "--eval", cmd);
if (er.getExitCode() != 0) {
LOG.errorf("Error executing MongoDB command: %s", cmd);
LOG.error(er.getStdout());
Expand Down

0 comments on commit 8cc76ea

Please sign in to comment.