From 57ef2db0e5fd76394ad3389b4681e7b25851f8bf Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Tue, 3 Jun 2025 07:57:09 +0530 Subject: [PATCH 1/8] Ollama - Testcontainers Module in Java --- testContainers/pom.xml | 54 ++++++++++++++++ testContainers/src/main/resources/logback.xml | 22 +++++++ .../ollama/OllamaContainerLiveTest.java | 61 +++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 testContainers/pom.xml create mode 100644 testContainers/src/main/resources/logback.xml create mode 100644 testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java diff --git a/testContainers/pom.xml b/testContainers/pom.xml new file mode 100644 index 0000000..03d3584 --- /dev/null +++ b/testContainers/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + com.kodesastra + kodesastra_blog + 1.0-SNAPSHOT + + + testContainers + + + + + org.testcontainers + testcontainers-bom + 1.21.1 + pom + import + + + + + + org.testcontainers + ollama + test + + + ch.qos.logback + logback-classic + 1.5.18 + + + ch.qos.logback + logback-core + 1.5.18 + + + org.slf4j + slf4j-api + 2.0.17 + + + + + 21 + 21 + UTF-8 + + + \ No newline at end of file diff --git a/testContainers/src/main/resources/logback.xml b/testContainers/src/main/resources/logback.xml new file mode 100644 index 0000000..4bd09a2 --- /dev/null +++ b/testContainers/src/main/resources/logback.xml @@ -0,0 +1,22 @@ + + + + + + %d{HH:mm:ss.SSS} %-5level %logger - %msg%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java b/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java new file mode 100644 index 0000000..757efc1 --- /dev/null +++ b/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java @@ -0,0 +1,61 @@ +package com.ks.testcontainers.ollama; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testcontainers.containers.Container; +import org.testcontainers.ollama.OllamaContainer; + +public class OllamaContainerLiveTest { + private static Logger logger = LoggerFactory.getLogger(OllamaContainerLiveTest.class); + + static OllamaContainer ollamaContainer; + private static final String OLLAMA_IMAGE = "ollama/ollama:latest"; + private static final String LLM = "tinyllama:1.1b"; + + @BeforeAll + static void init() throws IOException, InterruptedException { + + ollamaContainer = new OllamaContainer(OLLAMA_IMAGE); + ollamaContainer.withCreateContainerCmdModifier((cmd) -> + cmd.getHostConfig().withDeviceRequests(null)); + + ollamaContainer.start(); + + ollamaContainer.execInContainer("ollama", "pull", LLM); + } + + @AfterAll + static void cleanUp() { + if (ollamaContainer != null) { + try { + ollamaContainer.stop(); + } catch (Exception e) { + logger.error("Error stopping Ollama container: {}", e.getMessage()); + } + } + } + + @Test + void test() throws IOException, InterruptedException { + String prompt = """ + The sun is a star located at the center of our solar system. + It provides the Earth with heat and light, making life possible. + The sun is composed mostly of hydrogen and helium + and generates energy through nuclear fusion." + Question: What two gases make up most of the sun? + Please answer strictly from the information provided. + Keep the answer short and concise." + """; + Container.ExecResult execResult = + ollamaContainer.execInContainer("ollama", "run", LLM, prompt); + assertEquals(0, execResult.getExitCode(), "Exit code should be 0"); + logger.info("Exec Result: {}", execResult.getStdout()); + } +} \ No newline at end of file From 47b8c42d0a2d9589b3a3d40e9f336f6b8f23e2cb Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Tue, 3 Jun 2025 23:56:34 +0530 Subject: [PATCH 2/8] Ollama - Testcontainers Module in Java --- .../ks/testcontainers/ollama/OllamaContainerLiveTest.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java b/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java index 757efc1..9230350 100644 --- a/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java +++ b/testContainers/src/test/java/com/ks/testcontainers/ollama/OllamaContainerLiveTest.java @@ -13,7 +13,7 @@ import org.testcontainers.ollama.OllamaContainer; public class OllamaContainerLiveTest { - private static Logger logger = LoggerFactory.getLogger(OllamaContainerLiveTest.class); + private static final Logger logger = LoggerFactory.getLogger(OllamaContainerLiveTest.class); static OllamaContainer ollamaContainer; private static final String OLLAMA_IMAGE = "ollama/ollama:latest"; @@ -45,13 +45,15 @@ static void cleanUp() { @Test void test() throws IOException, InterruptedException { String prompt = """ + Context: The sun is a star located at the center of our solar system. It provides the Earth with heat and light, making life possible. The sun is composed mostly of hydrogen and helium and generates energy through nuclear fusion." Question: What two gases make up most of the sun? - Please answer strictly from the information provided. - Keep the answer short and concise." + Instructions: + Please answer strictly from the context provided in the prompt and no other additional + should be provided.Also, keep the answer short and concise." """; Container.ExecResult execResult = ollamaContainer.execInContainer("ollama", "run", LLM, prompt); From 23d3202454874f64542ad858c23dbec28a1e15eb Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Wed, 4 Jun 2025 21:13:10 +0530 Subject: [PATCH 3/8] Ollama - Testcontainers Module in Java --- pom.xml | 2 ++ springboot-modules/spring-ai/pom.xml | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 84bebef..1d1da21 100644 --- a/pom.xml +++ b/pom.xml @@ -99,6 +99,7 @@ springboot-modules + testContainers @@ -112,6 +113,7 @@ 5.10.3 5.10.3 2.7.3 + 1.21.1 \ No newline at end of file diff --git a/springboot-modules/spring-ai/pom.xml b/springboot-modules/spring-ai/pom.xml index 07d249d..d87384e 100644 --- a/springboot-modules/spring-ai/pom.xml +++ b/springboot-modules/spring-ai/pom.xml @@ -71,8 +71,6 @@ --> - 21 - 21 UTF-8 From a618d02ddbde8d3b23b2d4a54c212c89fa1de147 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Wed, 4 Jun 2025 21:17:37 +0530 Subject: [PATCH 4/8] Ollama - Testcontainers Module in Java --- testContainers/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 testContainers/README.md diff --git a/testContainers/README.md b/testContainers/README.md new file mode 100644 index 0000000..2c91d7f --- /dev/null +++ b/testContainers/README.md @@ -0,0 +1,9 @@ +# **Published Articles** + +## **Testcontainers** + +This module contains source codes related to the articles on Testcontainers. +- [**Ollama - Testcontainers Module in Java**](https://www.kodesastra.com/2025/06/ollama-testcontainers-module-in-java.html) + + + From 528eced11c954264fb480e57ae2054b573dc3556 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Wed, 4 Jun 2025 21:41:18 +0530 Subject: [PATCH 5/8] Ollama - Testcontainers Module in Java --- testContainers/pom.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/testContainers/pom.xml b/testContainers/pom.xml index 03d3584..5171ea8 100644 --- a/testContainers/pom.xml +++ b/testContainers/pom.xml @@ -46,9 +46,7 @@ - 21 - 21 - UTF-8 + UTF-8 \ No newline at end of file From 83b6da7a93abb407e736c91a9bd04e88d1b78847 Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Wed, 4 Jun 2025 21:47:26 +0530 Subject: [PATCH 6/8] Ollama - Testcontainers Module in Java --- pom.xml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pom.xml b/pom.xml index 1d1da21..516c8cd 100644 --- a/pom.xml +++ b/pom.xml @@ -103,8 +103,6 @@ - 17 - 17 3.25.0 3.5.0 3.13.0 From 2fc0f47493fa560be93fbdd9848615775217566d Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Wed, 4 Jun 2025 21:52:50 +0530 Subject: [PATCH 7/8] Ollama - Testcontainers Module in Java --- springboot-modules/spring-ai/pom.xml | 1 + testContainers/pom.xml | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/springboot-modules/spring-ai/pom.xml b/springboot-modules/spring-ai/pom.xml index d87384e..7b68a7b 100644 --- a/springboot-modules/spring-ai/pom.xml +++ b/springboot-modules/spring-ai/pom.xml @@ -72,6 +72,7 @@ UTF-8 + 17 \ No newline at end of file diff --git a/testContainers/pom.xml b/testContainers/pom.xml index 5171ea8..8631abb 100644 --- a/testContainers/pom.xml +++ b/testContainers/pom.xml @@ -46,7 +46,8 @@ - UTF-8 + UTF-8 + 17 \ No newline at end of file From 211a3a30eb0c053e41d5d5faf268aa47d140277d Mon Sep 17 00:00:00 2001 From: parthiv39731 <70740707+parthiv39731@users.noreply.github.com> Date: Wed, 4 Jun 2025 21:56:05 +0530 Subject: [PATCH 8/8] Ollama - Testcontainers Module in Java --- .../advisors/{AdvisorUnitTest.java => AdvisorLiveTest.java} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/{AdvisorUnitTest.java => AdvisorLiveTest.java} (96%) diff --git a/springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorUnitTest.java b/springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorLiveTest.java similarity index 96% rename from springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorUnitTest.java rename to springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorLiveTest.java index 1367259..36e3d30 100644 --- a/springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorUnitTest.java +++ b/springboot-modules/spring-ai/src/test/java/com/kodesastra/ai/advisors/AdvisorLiveTest.java @@ -16,9 +16,9 @@ @SpringBootTest @ActiveProfiles("sa") -public class AdvisorUnitTest { +public class AdvisorLiveTest { - private static final Logger LOGGER = LoggerFactory.getLogger(AdvisorUnitTest.class); + private static final Logger LOGGER = LoggerFactory.getLogger(AdvisorLiveTest.class); @Autowired ChatModel chatModel;