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

orpiske 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 2dca200cab3 CAMEL-21072: use the Ollama container from testcontainers
2dca200cab3 is described below

commit 2dca200cab33ba0d7a149c8c171ac44c9119b43a
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Mon Aug 12 11:37:03 2024 +0200

    CAMEL-21072: use the Ollama container from testcontainers
    
    - Also allow adjusting the model used for the test
---
 test-infra/camel-test-infra-ollama/pom.xml         |  7 +++
 .../infra/ollama/commons/OllamaProperties.java     |  3 +-
 .../infra/ollama/services/OllamaContainer.java     | 65 ---------------------
 .../services/OllamaLocalContainerService.java      | 66 ++++++++++------------
 .../infra/ollama/services/OllamaRemoteService.java | 20 ++++++-
 .../test/infra/ollama/services/OllamaService.java  |  2 +-
 ...ervice.java => OllamaServiceConfiguration.java} | 10 +---
 .../ollama/services/OllamaServiceFactory.java      |  9 ++-
 .../infra/ollama/services/container.properties     |  1 -
 9 files changed, 68 insertions(+), 115 deletions(-)

diff --git a/test-infra/camel-test-infra-ollama/pom.xml 
b/test-infra/camel-test-infra-ollama/pom.xml
index 881d88034ce..b736e6fc091 100644
--- a/test-infra/camel-test-infra-ollama/pom.xml
+++ b/test-infra/camel-test-infra-ollama/pom.xml
@@ -43,6 +43,13 @@
             <artifactId>testcontainers</artifactId>
             <version>${testcontainers-version}</version>
         </dependency>
+
+
+        <dependency>
+            <groupId>org.testcontainers</groupId>
+            <artifactId>ollama</artifactId>
+            <version>${testcontainers-version}</version>
+        </dependency>
     </dependencies>
 
 </project>
\ No newline at end of file
diff --git 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/commons/OllamaProperties.java
 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/commons/OllamaProperties.java
index 374c5ee7ac4..1672c4ecc17 100644
--- 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/commons/OllamaProperties.java
+++ 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/commons/OllamaProperties.java
@@ -18,11 +18,10 @@ package org.apache.camel.test.infra.ollama.commons;
 
 public class OllamaProperties {
 
-    public static final String PORT = "ollama.port";
     public static final String CONTAINER = "ollama.container";
     public static final String MODEL = "ollama.model";
 
-    public static final String BASE_URL = "ollama.url";
+    public static final String ENDPOINT = "ollama.endpoint";
 
     private OllamaProperties() {
 
diff --git 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaContainer.java
 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaContainer.java
deleted file mode 100644
index cff7bb1717f..00000000000
--- 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaContainer.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * 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.test.infra.ollama.services;
-
-import java.io.IOException;
-
-import com.github.dockerjava.api.command.InspectContainerResponse;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.testcontainers.containers.GenericContainer;
-import org.testcontainers.containers.output.Slf4jLogConsumer;
-import org.testcontainers.containers.wait.strategy.Wait;
-import org.testcontainers.utility.DockerImageName;
-
-public class OllamaContainer extends GenericContainer<OllamaContainer> {
-
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(OllamaContainer.class);
-
-    private final DockerImageName dockerImageName;
-
-    private final Integer port;
-    private final String model;
-    private final String imageName;
-
-    public OllamaContainer(DockerImageName image, Integer port, String model, 
String imageName) {
-        super(image);
-
-        this.dockerImageName = image;
-        this.port = port;
-        this.model = model;
-        this.imageName = imageName;
-
-        withExposedPorts(port)
-                .withImagePullPolicy(dockerImageName -> 
!dockerImageName.getVersionPart().endsWith(model))
-                .withLogConsumer(new Slf4jLogConsumer(LOGGER))
-                .setWaitStrategy(Wait.forListeningPort());
-    }
-
-    @Override
-    protected void containerIsStarted(InspectContainerResponse containerInfo) {
-        if (!this.dockerImageName.equals(DockerImageName.parse(imageName))) {
-            try {
-                LOGGER.info("Start pulling the '{}' model ... would take 
several minutes ...", model);
-                execInContainer("ollama", "pull", model);
-                LOGGER.info("Model pulling competed!");
-            } catch (IOException | InterruptedException e) {
-                throw new RuntimeException("Error pulling model", e);
-            }
-        }
-    }
-}
diff --git 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaLocalContainerService.java
 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaLocalContainerService.java
index ea97e7673e2..088e6979bad 100644
--- 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaLocalContainerService.java
+++ 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaLocalContainerService.java
@@ -16,74 +16,63 @@
  */
 package org.apache.camel.test.infra.ollama.services;
 
-import java.util.List;
+import java.io.IOException;
 
-import com.github.dockerjava.api.DockerClient;
-import com.github.dockerjava.api.model.Image;
 import org.apache.camel.test.infra.common.LocalPropertyResolver;
 import org.apache.camel.test.infra.common.services.ContainerService;
 import org.apache.camel.test.infra.ollama.commons.OllamaProperties;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.testcontainers.DockerClientFactory;
+import org.testcontainers.ollama.OllamaContainer;
 import org.testcontainers.utility.DockerImageName;
 
 public class OllamaLocalContainerService implements OllamaService, 
ContainerService<OllamaContainer> {
-    private static final Logger LOG = 
LoggerFactory.getLogger(OllamaLocalContainerService.class);
+    private static class DefaultServiceConfiguration implements 
OllamaServiceConfiguration {
+
+        @Override
+        public String modelName() {
+            return 
LocalPropertyResolver.getProperty(OllamaLocalContainerService.class, 
OllamaProperties.MODEL);
+        }
+    }
 
-    public static final String CONTAINER_PORT = 
LocalPropertyResolver.getProperty(
-            OllamaLocalContainerService.class, OllamaProperties.PORT);
+    private static final Logger LOG = 
LoggerFactory.getLogger(OllamaLocalContainerService.class);
 
     public static final String CONTAINER_NAME = 
LocalPropertyResolver.getProperty(
             OllamaLocalContainerService.class, OllamaProperties.CONTAINER);
 
-    public static String OLLAMA_MODEL = LocalPropertyResolver.getProperty(
-            OllamaLocalContainerService.class, OllamaProperties.MODEL);
-
-    public static final String LOCAL_OLLAMA_IMAGE = String.format("tc-%s-%s", 
CONTAINER_NAME, OLLAMA_MODEL);
-
-    private final DockerImageName dockerImageName;
-
     private final OllamaContainer container;
-
-    private final Integer port;
+    private final OllamaServiceConfiguration configuration;
 
     public OllamaLocalContainerService() {
-        port = Integer.valueOf(CONTAINER_PORT);
+        container = initContainer();
 
-        dockerImageName = resolveImageName();
+        configuration = new DefaultServiceConfiguration();
+    }
 
+    public OllamaLocalContainerService(OllamaServiceConfiguration 
serviceConfiguration) {
+        configuration = serviceConfiguration;
         container = initContainer();
     }
 
     protected OllamaContainer initContainer() {
-        return new OllamaContainer(dockerImageName, port, OLLAMA_MODEL, 
LOCAL_OLLAMA_IMAGE);
-    }
-
-    protected DockerImageName resolveImageName() {
-        DockerImageName dockerImageName = 
DockerImageName.parse(CONTAINER_NAME);
-        DockerClient dockerClient = DockerClientFactory.instance().client();
-        List<Image> images = 
dockerClient.listImagesCmd().withReferenceFilter(LOCAL_OLLAMA_IMAGE).exec();
-        if (images.isEmpty()) {
-            return dockerImageName;
-        }
-        return DockerImageName.parse(LOCAL_OLLAMA_IMAGE);
+        return new OllamaContainer(
+                DockerImageName.parse(CONTAINER_NAME)
+                        .asCompatibleSubstituteFor("ollama/ollama"));
     }
 
     @Override
-    public String getBaseUrl() {
-        return "http://"; + container.getHost() + ":" + 
container.getMappedPort(port);
+    public String getEndpoint() {
+        return container.getEndpoint();
     }
 
     @Override
     public String getModel() {
-        return OLLAMA_MODEL;
+        return configuration.modelName();
     }
 
     @Override
     public void registerProperties() {
-        System.setProperty(OllamaProperties.PORT, String.valueOf(port));
-        System.setProperty(OllamaProperties.BASE_URL, 
String.valueOf(getBaseUrl()));
+        System.setProperty(OllamaProperties.ENDPOINT, container.getEndpoint());
     }
 
     @Override
@@ -91,8 +80,15 @@ public class OllamaLocalContainerService implements 
OllamaService, ContainerServ
         LOG.info("Trying to start the Ollama container");
         container.start();
 
+        LOG.info("Pulling the model {}", getModel());
+        try {
+            container.execInContainer("ollama", "pull", getModel());
+        } catch (IOException | InterruptedException e) {
+            throw new RuntimeException(e);
+        }
+
         registerProperties();
-        LOG.info("Ollama instance running at {}", port);
+        LOG.info("Ollama instance running at {}", getEndpoint());
     }
 
     @Override
diff --git 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaRemoteService.java
 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaRemoteService.java
index 190f89ea479..e4df7dea431 100644
--- 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaRemoteService.java
+++ 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaRemoteService.java
@@ -19,8 +19,22 @@ package org.apache.camel.test.infra.ollama.services;
 import org.apache.camel.test.infra.ollama.commons.OllamaProperties;
 
 public class OllamaRemoteService implements OllamaService {
+    private static class DefaultServiceConfiguration implements 
OllamaServiceConfiguration {
+
+        @Override
+        public String modelName() {
+            return System.getProperty(OllamaProperties.MODEL);
+        }
+    }
+
+    private final OllamaServiceConfiguration configuration;
 
     public OllamaRemoteService() {
+        configuration = new DefaultServiceConfiguration();
+    }
+
+    public OllamaRemoteService(OllamaServiceConfiguration 
serviceConfiguration) {
+        configuration = serviceConfiguration;
     }
 
     @Override
@@ -39,13 +53,13 @@ public class OllamaRemoteService implements OllamaService {
     }
 
     @Override
-    public String getBaseUrl() {
-        return System.getProperty(OllamaProperties.BASE_URL);
+    public String getEndpoint() {
+        return System.getProperty(OllamaProperties.ENDPOINT);
     }
 
     @Override
     public String getModel() {
-        return System.getProperty(OllamaProperties.MODEL);
+        return configuration.modelName();
     }
 
 }
diff --git 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaService.java
 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaService.java
index 57282e7a751..015352d5e64 100644
--- 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaService.java
+++ 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaService.java
@@ -20,7 +20,7 @@ import 
org.apache.camel.test.infra.common.services.TestService;
 
 public interface OllamaService extends TestService {
 
-    String getBaseUrl();
+    String getEndpoint();
 
     String getModel();
 }
diff --git 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaService.java
 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaServiceConfiguration.java
similarity index 83%
copy from 
test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaService.java
copy to 
test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaServiceConfiguration.java
index 57282e7a751..2b692f23a7a 100644
--- 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaService.java
+++ 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaServiceConfiguration.java
@@ -14,13 +14,9 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.test.infra.ollama.services;
-
-import org.apache.camel.test.infra.common.services.TestService;
 
-public interface OllamaService extends TestService {
-
-    String getBaseUrl();
+package org.apache.camel.test.infra.ollama.services;
 
-    String getModel();
+public interface OllamaServiceConfiguration {
+    String modelName();
 }
diff --git 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaServiceFactory.java
 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaServiceFactory.java
index 602af9e80a8..2f87a9c11f4 100644
--- 
a/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaServiceFactory.java
+++ 
b/test-infra/camel-test-infra-ollama/src/test/java/org/apache/camel/test/infra/ollama/services/OllamaServiceFactory.java
@@ -25,7 +25,7 @@ public final class OllamaServiceFactory {
     }
 
     public static SimpleTestServiceBuilder<OllamaService> builder() {
-        return new 
SimpleTestServiceBuilder<>("org/apache/camel/test/infra/ollama");
+        return new SimpleTestServiceBuilder<>("ollama");
     }
 
     public static OllamaService createService() {
@@ -34,4 +34,11 @@ public final class OllamaServiceFactory {
                 .addRemoteMapping(OllamaRemoteService::new)
                 .build();
     }
+
+    public static OllamaService 
createServiceWithConfiguration(OllamaServiceConfiguration serviceConfiguration) 
{
+        return builder()
+                .addLocalMapping(() -> new 
OllamaLocalContainerService(serviceConfiguration))
+                .addRemoteMapping(() -> new 
OllamaRemoteService(serviceConfiguration))
+                .build();
+    }
 }
diff --git 
a/test-infra/camel-test-infra-ollama/src/test/resources/org/apache/camel/test/infra/ollama/services/container.properties
 
b/test-infra/camel-test-infra-ollama/src/test/resources/org/apache/camel/test/infra/ollama/services/container.properties
index 610fe3d18ad..03400a1ea4e 100644
--- 
a/test-infra/camel-test-infra-ollama/src/test/resources/org/apache/camel/test/infra/ollama/services/container.properties
+++ 
b/test-infra/camel-test-infra-ollama/src/test/resources/org/apache/camel/test/infra/ollama/services/container.properties
@@ -14,6 +14,5 @@
 ## See the License for the specific language governing permissions and
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
-ollama.port=11434
 ollama.container=ollama/ollama:0.3.5
 ollama.model=orca-mini
\ No newline at end of file

Reply via email to