This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 6c4d63890a1c CAMEL-23493: Migrate camel-kudu tests to Testcontainers 
(#29834)
6c4d63890a1c is described below

commit 6c4d63890a1c90d315b7c5985406d9baa109b5a1
Author: Guillaume Nodet <[email protected]>
AuthorDate: Mon Nov 16 14:32:58 2026 +0100

    CAMEL-23493: Migrate camel-kudu tests to Testcontainers (#29834)
    
    Fixes [CAMEL-23493](https://issues.apache.org/jira/browse/CAMEL-23493): 
Most kudu tests are silently failing due to automatic skip.
    
    The existing test infrastructure uses `kudu-test-utils` (`KuduTestHarness`) 
which calls `FakeDNS.install()` referencing 
`sun.net.spi.nameservice.NameService` — an internal class removed in JDK 9+. On 
JDK 21 this causes a `ClassNotFoundException`. The original 
`IntegrationKuduConfiguration.beforeEach()` swallowed *any* exception and 
silently set `hasKuduHarness=false`, causing all 10 integration tests 
(`KuduProducerTest` ×5, `KuduScanTest` ×5) to be skipped via 
`Assumptions.assumeTrue( [...]
    
    _This PR was opened by Hermes Agent on behalf of gnodet._
    
    Co-authored-by: Hermes Agent <[email protected]>
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 components/camel-kudu/pom.xml                      |  65 +-----
 .../camel/component/kudu/AbstractKuduTest.java     |  11 +-
 .../kudu/IntegrationKuduConfiguration.java         | 229 ++++++++++++++++-----
 3 files changed, 182 insertions(+), 123 deletions(-)

diff --git a/components/camel-kudu/pom.xml b/components/camel-kudu/pom.xml
index 01ee03bb4e2a..1fd99a67e97e 100644
--- a/components/camel-kudu/pom.xml
+++ b/components/camel-kudu/pom.xml
@@ -31,45 +31,14 @@
     <name>Camel :: Kudu</name>
     <description>Camel Apache Kudu support</description>
 
-    <properties>
-        <camel.surefire.fork.vmargs>--add-opens 
java.base/java.net=ALL-UNNAMED</camel.surefire.fork.vmargs>
-    </properties>
     <profiles>
         <profile>
-            <!-- kudu-binary is only available for Linux or macOS on x86 64 -->
-            <id>NotLinux</id>
-            <activation>
-                <os>
-                    <family>!Linux</family>
-                </os>
-            </activation>
-            <properties>
-                <maven.test.skip>true</maven.test.skip>
-                <os.detected.classifier>linux-x86_64</os.detected.classifier> 
<!-- Fake classifier to allow dependency resolution. kudu-binary will not be 
executed anyway on Windows -->
-            </properties>
-        </profile>
-        <profile>
-            <!-- kudu-binary is only available for Linux or macOS on x86 64 -->
-            <id>Notx86</id>
-            <activation>
-                <os>
-                    <arch>!amd64</arch>
-                </os>
-            </activation>
-            <properties>
-                <maven.test.skip>true</maven.test.skip>
-                <os.detected.classifier>linux-x86_64</os.detected.classifier> 
<!-- Fake classifier to allow dependency resolution. kudu-binary will not be 
executed anyway on Windows -->
-            </properties>
-        </profile>
-        <profile>
-            <!-- kudu-binary is only available for Linux or macOS on x86 64 -->
             <id>release</id>
             <activation>
                 <activeByDefault>false</activeByDefault>
             </activation>
             <properties>
                 <maven.test.skip>true</maven.test.skip>
-                <os.detected.classifier>linux-x86_64</os.detected.classifier> 
<!-- Fake classifier to allow dependency resolution. kudu-binary will not be 
executed anyway on Windows -->
             </properties>
         </profile>
     </profiles>
@@ -99,11 +68,6 @@
             <artifactId>camel-test-junit6</artifactId>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.junit.jupiter</groupId>
-            <artifactId>junit-jupiter-migrationsupport</artifactId>
-            <scope>test</scope>
-        </dependency>
         <dependency>
             <groupId>org.mockito</groupId>
             <artifactId>mockito-core</artifactId>
@@ -111,32 +75,11 @@
             <scope>test</scope>
         </dependency>
 
-        <!-- Spin a kudu cluster to run integration tests -->
-        <!-- as described in
-        
https://kudu.apache.org/docs/developing.html#_using_the_kudu_binary_test_jar -->
-        <dependency>
-            <groupId>org.apache.kudu</groupId>
-            <artifactId>kudu-test-utils</artifactId>
-            <version>${kudu-version}</version>
-            <scope>test</scope>
-            <exclusions>
-                <exclusion>
-                    <groupId>org.apache.logging.log4j</groupId>
-                    <artifactId>log4j-api</artifactId>
-                </exclusion>
-                <exclusion>
-                    <groupId>org.apache.logging.log4j</groupId>
-                    <artifactId>log4j-slf4j2-impl</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
-
+        <!-- Testcontainers for spinning up a Kudu cluster (master + tablet 
server) -->
         <dependency>
-            <groupId>org.apache.kudu</groupId>
-            <artifactId>kudu-binary</artifactId>
-            <version>${kudu-version}</version>
-            <!--suppress UnresolvedMavenProperty -->
-            <classifier>${os.detected.classifier}</classifier>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>testcontainers</artifactId>
+            <version>${testcontainers-version}</version>
             <scope>test</scope>
         </dependency>
     </dependencies>
diff --git 
a/components/camel-kudu/src/test/java/org/apache/camel/component/kudu/AbstractKuduTest.java
 
b/components/camel-kudu/src/test/java/org/apache/camel/component/kudu/AbstractKuduTest.java
index c0ecb6f291c1..e79b8912bd26 100644
--- 
a/components/camel-kudu/src/test/java/org/apache/camel/component/kudu/AbstractKuduTest.java
+++ 
b/components/camel-kudu/src/test/java/org/apache/camel/component/kudu/AbstractKuduTest.java
@@ -40,12 +40,12 @@ public abstract class AbstractKuduTest extends 
CamelTestSupport {
     private static final Logger LOG = 
LoggerFactory.getLogger(AbstractKuduTest.class);
 
     /**
-     * This is the class that connects our Camel test with the Kudu testing 
framework to spin up a Kudu local endpoint.
+     * Shared Kudu cluster started once per test class via Testcontainers.
      */
     @RegisterExtension
-    public IntegrationKuduConfiguration ikc = new 
IntegrationKuduConfiguration();
+    static IntegrationKuduConfiguration ikc = new 
IntegrationKuduConfiguration();
 
-    private Integer id = 1;
+    private int id = 1;
 
     protected void createTestTable(String tableName) {
         LOG.trace("Creating table {}.", tableName);
@@ -77,7 +77,7 @@ public abstract class AbstractKuduTest extends 
CamelTestSupport {
 
     @Override
     public void doPreSetup() {
-        Assumptions.assumeTrue(ikc.hasKuduHarness(), "Skipping the test 
because the Kudu harness is not runnable");
+        Assumptions.assumeTrue(ikc.isAvailable(), "Skipping test: Kudu 
containers are not available (Docker not running?)");
     }
 
     @Override
@@ -87,7 +87,7 @@ public abstract class AbstractKuduTest extends 
CamelTestSupport {
 
     @Override
     public void doPostTearDown() {
-        if (ikc.hasKuduHarness()) {
+        if (ikc.isAvailable()) {
             deleteTestTable("TestTable");
         }
     }
@@ -121,7 +121,6 @@ public abstract class AbstractKuduTest extends 
CamelTestSupport {
 
             client.newSession().apply(insert);
         } catch (KuduException e) {
-
             LOG.error(e.getMessage(), e);
         }
         LOG.trace("Row inserted on table {}.", tableName);
diff --git 
a/components/camel-kudu/src/test/java/org/apache/camel/component/kudu/IntegrationKuduConfiguration.java
 
b/components/camel-kudu/src/test/java/org/apache/camel/component/kudu/IntegrationKuduConfiguration.java
index 85394195fcdc..348cb2bc5448 100644
--- 
a/components/camel-kudu/src/test/java/org/apache/camel/component/kudu/IntegrationKuduConfiguration.java
+++ 
b/components/camel-kudu/src/test/java/org/apache/camel/component/kudu/IntegrationKuduConfiguration.java
@@ -16,93 +16,210 @@
  */
 package org.apache.camel.component.kudu;
 
+import java.net.Inet4Address;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
+import java.util.Enumeration;
+import java.util.function.Consumer;
+
+import com.github.dockerjava.api.command.CreateContainerCmd;
+import com.github.dockerjava.api.model.ExposedPort;
+import com.github.dockerjava.api.model.HostConfig;
+import com.github.dockerjava.api.model.Ports;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
-import org.apache.camel.model.ModelCamelContext;
 import org.apache.kudu.client.KuduClient;
-import org.apache.kudu.test.KuduTestHarness;
-import org.junit.jupiter.api.extension.AfterEachCallback;
-import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.AfterAllCallback;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
 import org.junit.jupiter.api.extension.ExtensionContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.testcontainers.DockerClientFactory;
+import org.testcontainers.containers.GenericContainer;
+import org.testcontainers.containers.Network;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.containers.wait.strategy.Wait;
 
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 
 /**
- * Use this class to run tests against a local basic Kudu server. This local 
kudu server is spinned up by
- * https://kudu.apache.org/docs/developing.html#_using_the_kudu_binary_test_jar
+ * JUnit 5 extension that starts a local Kudu cluster (one master + one tablet 
server) using Testcontainers and the
+ * official {@code apache/kudu} Docker image.
+ *
+ * <p>
+ * Based on <a 
href="https://github.com/apache/kudu/blob/master/docker/quickstart.yml";> Kudu 
quickstart.yml</a>.
  */
-public class IntegrationKuduConfiguration implements BeforeEachCallback, 
AfterEachCallback {
+public class IntegrationKuduConfiguration implements BeforeAllCallback, 
AfterAllCallback {
+
     private static final Logger LOG = 
LoggerFactory.getLogger(IntegrationKuduConfiguration.class);
 
-    private final Internal internal = new Internal();
-    private boolean hasKuduHarness;
+    static final String KUDU_IMAGE = "apache/kudu:1.18.0-ubuntu";
 
-    public IntegrationKuduConfiguration() {
-    }
+    private static final int KUDU_MASTER_RPC_PORT = 7051;
+    private static final int KUDU_MASTER_HTTP_PORT = 8051;
+    private static final int KUDU_TABLET_RPC_PORT = 7050;
+    private static final int KUDU_TABLET_HTTP_PORT = 8050;
 
-    public KuduClient getClient() {
-        return internal.getClient();
-    }
+    private static final String KUDU_MASTER_ALIAS = "kudu-master";
+    private static final String KUDU_TABLET_ALIAS = "kudu-tserver";
 
-    public void setupCamelContext(ModelCamelContext context) {
-        internal.setupCamelContext(context);
-    }
+    private GenericContainer<?> masterContainer;
+    private GenericContainer<?> tabletContainer;
+    private KuduClient kuduClient;
+    private boolean available;
+    private Network network;
 
     @Override
-    public void afterEach(ExtensionContext context) {
-        if (hasKuduHarness) {
-            internal.after();
+    public void beforeAll(ExtensionContext context) {
+        if (!DockerClientFactory.instance().isDockerAvailable()) {
+            LOG.warn("Docker is not available; Kudu integration tests will be 
skipped");
+            available = false;
+            return;
+        }
+        try {
+            startContainers();
+            available = true;
+        } catch (Exception e) {
+            LOG.warn("Failed to start Kudu containers; tests will be skipped: 
{}", e.getMessage(), e);
+            available = false;
         }
     }
 
+    private void startContainers() {
+        network = Network.newNetwork();
+        final String advertisingIp = resolveAdvertisingIpAddress();
+        LOG.info("Kudu advertising IP address: {}", advertisingIp);
+
+        masterContainer = new GenericContainer<>(KUDU_IMAGE)
+                .withCommand("master")
+                .withEnv("MASTER_ARGS",
+                        "--unlock_unsafe_flags=true "
+                                        + "--default_num_replicas=1 "
+                                        + "--rpc_advertised_addresses=" + 
advertisingIp + ":" + KUDU_MASTER_RPC_PORT
+                                        + " --webserver_advertised_addresses=" 
+ advertisingIp + ":" + KUDU_MASTER_HTTP_PORT)
+                .withEnv("KUDU_MASTERS", KUDU_MASTER_ALIAS)
+                .withExposedPorts(KUDU_MASTER_RPC_PORT, KUDU_MASTER_HTTP_PORT)
+                .withNetwork(network)
+                .withNetworkAliases(KUDU_MASTER_ALIAS)
+                .withLogConsumer(new Slf4jLogConsumer(LOG))
+                .waitingFor(Wait.forListeningPort());
+        masterContainer.start();
+
+        // Fix the tablet server ports so we can advertise them before the 
container starts.
+        // See 
https://github.com/testcontainers/testcontainers-java/issues/3967
+        Consumer<CreateContainerCmd> portBinder = cmd -> {
+            Ports portBindings = new Ports();
+            portBindings.bind(ExposedPort.tcp(KUDU_TABLET_RPC_PORT), 
Ports.Binding.bindPort(KUDU_TABLET_RPC_PORT));
+            portBindings.bind(ExposedPort.tcp(KUDU_TABLET_HTTP_PORT), 
Ports.Binding.bindPort(KUDU_TABLET_HTTP_PORT));
+            HostConfig hostConfig = HostConfig.newHostConfig()
+                    .withPortBindings(portBindings)
+                    .withNetworkMode(network.getId());
+            cmd.withHostName(KUDU_TABLET_ALIAS).withHostConfig(hostConfig);
+        };
+
+        tabletContainer = new GenericContainer<>(KUDU_IMAGE)
+                .withCommand("tserver")
+                .withEnv("TSERVER_ARGS",
+                        "--unlock_unsafe_flags=true "
+                                         + "--rpc_advertised_addresses=" + 
advertisingIp + ":" + KUDU_TABLET_RPC_PORT
+                                         + " 
--webserver_advertised_addresses=" + advertisingIp + ":" + 
KUDU_TABLET_HTTP_PORT)
+                .withEnv("KUDU_MASTERS", KUDU_MASTER_ALIAS)
+                .withExposedPorts(KUDU_TABLET_RPC_PORT, KUDU_TABLET_HTTP_PORT)
+                .withNetwork(network)
+                .withNetworkAliases(KUDU_TABLET_ALIAS)
+                .withCreateContainerCmdModifier(portBinder)
+                .withLogConsumer(new Slf4jLogConsumer(LOG))
+                .waitingFor(Wait.forListeningPort());
+        tabletContainer.start();
+
+        final String masterAuthority = masterContainer.getHost() + ":" + 
masterContainer.getMappedPort(KUDU_MASTER_RPC_PORT);
+        LOG.info("Kudu master RPC accessible at {}", masterAuthority);
+        LOG.info("Kudu tablet server RPC accessible at {}:{}", 
tabletContainer.getHost(), KUDU_TABLET_RPC_PORT);
+
+        kuduClient = new KuduClient.KuduClientBuilder(masterAuthority).build();
+    }
+
     @Override
-    public void beforeEach(ExtensionContext context) {
-        try {
-            internal.before();
-            hasKuduHarness = true;
-        } catch (Exception e) {
-            hasKuduHarness = false;
-            LOG.debug("Kudu harness is not runnable because: {}", 
e.getMessage(), e);
+    public void afterAll(ExtensionContext context) {
+        if (kuduClient != null) {
+            try {
+                kuduClient.shutdown();
+            } catch (Exception e) {
+                LOG.debug("Error shutting down Kudu client: {}", 
e.getMessage());
+            }
+        }
+        if (tabletContainer != null) {
+            tabletContainer.stop();
+        }
+        if (masterContainer != null) {
+            masterContainer.stop();
+        }
+        if (network != null) {
+            network.close();
         }
     }
 
-    public boolean hasKuduHarness() {
-        return hasKuduHarness;
+    public KuduClient getClient() {
+        return kuduClient;
     }
 
-    static class Internal extends KuduTestHarness {
-
-        /**
-         * Setup the Camel Context and make sure all KuduEndpoints use the 
special KuduClient that connects to the local
-         * server.
-         *
-         * @param context
-         */
-        public void setupCamelContext(CamelContext context) {
-            KuduClient client = this.getClient();
-            assertNotNull(client);
-            assertNotNull(context);
-            for (Endpoint endpoint : context.getEndpoints()) {
-                if (endpoint instanceof KuduEndpoint) {
-                    ((KuduEndpoint) endpoint).setKuduClient(this.getClient());
-                }
+    public boolean isAvailable() {
+        return available;
+    }
+
+    /**
+     * Inject the shared KuduClient into all KuduEndpoints of the given 
CamelContext, bypassing URI-based client
+     * creation. The client is marked as user-managed so Camel does not shut 
it down between test methods.
+     */
+    public void setupCamelContext(CamelContext context) {
+        KuduClient client = this.getClient();
+        assertNotNull(client);
+        assertNotNull(context);
+        for (Endpoint endpoint : context.getEndpoints()) {
+            if (endpoint instanceof KuduEndpoint kuduEndpoint) {
+                kuduEndpoint.setKuduClient(client);
+                kuduEndpoint.setUserManagedClient(true);
             }
         }
+    }
 
-        /**
-         * Needed because we have to shutdown the local cluster, but as Camel 
already closed the client, it will always
-         * throw an IllegalStateException.
-         */
-        @Override
-        public void after() {
-            try {
-                super.after();
-            } catch (java.lang.IllegalStateException e) {
-                //Camel already closed the client so an exception will be 
thrown
-                //no need to worry
+    /**
+     * Resolves the host IP address that Kudu containers should advertise so 
that the Kudu Java client (running on the
+     * host) can reach them.
+     */
+    private static String resolveAdvertisingIpAddress() {
+        String dockerHostIp = 
DockerClientFactory.instance().dockerHostIpAddress();
+        if (!"localhost".equals(dockerHostIp)) {
+            // Docker is running remotely; use its IP.
+            return dockerHostIp;
+        }
+        // Docker is local — resolve the first non-loopback, non-docker IPv4 
address.
+        try {
+            Enumeration<NetworkInterface> interfaces = 
NetworkInterface.getNetworkInterfaces();
+            while (interfaces.hasMoreElements()) {
+                NetworkInterface iface = interfaces.nextElement();
+                if (iface.isLoopback() || !iface.isUp()) {
+                    continue;
+                }
+                String name = iface.getName();
+                if (name.startsWith("docker") || name.startsWith("br-")) {
+                    continue;
+                }
+                Enumeration<InetAddress> addresses = iface.getInetAddresses();
+                while (addresses.hasMoreElements()) {
+                    InetAddress addr = addresses.nextElement();
+                    if (addr instanceof Inet4Address && 
!addr.isLinkLocalAddress()) {
+                        return addr.getHostAddress();
+                    }
+                }
             }
+        } catch (SocketException e) {
+            throw new IllegalStateException("Failed to resolve host IP address 
for Kudu advertising", e);
         }
+        // the host name may resolve to a loopback address (such as 
127.0.1.1), which the containers cannot reach
+        throw new IllegalStateException(
+                "Could not resolve a non-loopback, non-docker IPv4 host 
address for Kudu advertising: "
+                                        + "the host needs a network interface 
that the containers can reach");
     }
 }

Reply via email to