From 8cc76ea7af096f825c867e82256591b179e1f2bb Mon Sep 17 00:00:00 2001 From: James Netherton Date: Mon, 18 Nov 2024 08:49:31 +0000 Subject: [PATCH] Enable Mongo tests to run with container image versions < 7.x --- .../support/mongodb/MongoDbTestResource.java | 4 +- .../mongodb/MongoDbTestSupportUtils.java | 46 +++++++++++++++++++ integration-tests/debezium/pom.xml | 2 +- .../mongodb/DebeziumMongodbTestResource.java | 5 +- 4 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 integration-tests-support/mongodb/src/main/java/org/apache/camel/quarkus/test/support/mongodb/MongoDbTestSupportUtils.java diff --git a/integration-tests-support/mongodb/src/main/java/org/apache/camel/quarkus/test/support/mongodb/MongoDbTestResource.java b/integration-tests-support/mongodb/src/main/java/org/apache/camel/quarkus/test/support/mongodb/MongoDbTestResource.java index 5a2313ca6d17..86e326a3cc34 100644 --- a/integration-tests-support/mongodb/src/main/java/org/apache/camel/quarkus/test/support/mongodb/MongoDbTestResource.java +++ b/integration-tests-support/mongodb/src/main/java/org/apache/camel/quarkus/test/support/mongodb/MongoDbTestResource.java @@ -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); @@ -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()); } diff --git a/integration-tests-support/mongodb/src/main/java/org/apache/camel/quarkus/test/support/mongodb/MongoDbTestSupportUtils.java b/integration-tests-support/mongodb/src/main/java/org/apache/camel/quarkus/test/support/mongodb/MongoDbTestSupportUtils.java new file mode 100644 index 000000000000..357633852a82 --- /dev/null +++ b/integration-tests-support/mongodb/src/main/java/org/apache/camel/quarkus/test/support/mongodb/MongoDbTestSupportUtils.java @@ -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]); + } +} diff --git a/integration-tests/debezium/pom.xml b/integration-tests/debezium/pom.xml index 7badd3013ab1..2df10f4d5e2a 100644 --- a/integration-tests/debezium/pom.xml +++ b/integration-tests/debezium/pom.xml @@ -121,7 +121,7 @@ org.apache.camel.quarkus - camel-quarkus-integration-test-support + camel-quarkus-integration-tests-support-mongodb test diff --git a/integration-tests/debezium/src/test/java/org/apache/camel/quarkus/component/debezium/common/it/mongodb/DebeziumMongodbTestResource.java b/integration-tests/debezium/src/test/java/org/apache/camel/quarkus/component/debezium/common/it/mongodb/DebeziumMongodbTestResource.java index 713c4b482f9d..9741957356be 100644 --- a/integration-tests/debezium/src/test/java/org/apache/camel/quarkus/component/debezium/common/it/mongodb/DebeziumMongodbTestResource.java +++ b/integration-tests/debezium/src/test/java/org/apache/camel/quarkus/component/debezium/common/it/mongodb/DebeziumMongodbTestResource.java @@ -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> { private static final Logger LOG = Logger.getLogger(AbstractDebeziumTestResource.class); private static final String PRIVATE_HOST = "mongodb_private"; @@ -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());