From 74d75f15a2ad98c70b3efec7eed29a404ff214d8 Mon Sep 17 00:00:00 2001 From: Sami Ekblad Date: Tue, 9 Jul 2024 23:13:16 +0300 Subject: [PATCH] Refactored to use Testcontainers in dev only --- pom.xml | 5 +- .../java/com/example/demo/OllamaService.java | 50 ------------------- .../example/demo/DemoApplicationTests.java | 13 ----- .../com/example/demo/DevDemoApplication.java | 41 +++++++++++++++ 4 files changed, 45 insertions(+), 64 deletions(-) delete mode 100644 src/main/java/com/example/demo/OllamaService.java delete mode 100644 src/test/java/com/example/demo/DemoApplicationTests.java create mode 100644 src/test/java/com/example/demo/DevDemoApplication.java diff --git a/pom.xml b/pom.xml index 3e5bb67..8f94a15 100644 --- a/pom.xml +++ b/pom.xml @@ -65,15 +65,18 @@ org.springframework.boot spring-boot-testcontainers + test org.testcontainers ollama + test - + org.apache.commons commons-compress 1.26.0 + test org.springframework.boot diff --git a/src/main/java/com/example/demo/OllamaService.java b/src/main/java/com/example/demo/OllamaService.java deleted file mode 100644 index c1ff410..0000000 --- a/src/main/java/com/example/demo/OllamaService.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.example.demo; - -import com.github.dockerjava.api.model.*; -import jakarta.annotation.PostConstruct; -import jakarta.annotation.PreDestroy; -import org.springframework.ai.chat.client.ChatClient; -import org.springframework.stereotype.Service; -import org.springframework.web.context.annotation.ApplicationScope; -import org.testcontainers.ollama.OllamaContainer; - -import java.io.IOException; - -@Service -@ApplicationScope -public class OllamaService { - - private final OllamaContainer ollama; - - private final ChatClient.Builder chatClientBuilder; - - public OllamaService(ChatClient.Builder chatClientBuilder) { - ollama = new OllamaContainer("ollama/ollama:0.1.48") - .withCreateContainerCmdModifier(cmd -> - { - cmd.withBinds(Bind.parse("ollama:/root/.ollama")); - cmd.withPortBindings(new PortBinding(Ports.Binding.bindPort(11434), new ExposedPort(11434))); - }); - - this.chatClientBuilder = chatClientBuilder; - } - - @PostConstruct - public void start() throws IOException, InterruptedException { - ollama.start(); - ollama.execInContainer("ollama", "pull", "mistral"); - } - @PreDestroy - public void stop() { - if (ollama.isRunning()) { - ollama.stop(); - } - } - - public ChatClient getChatClient() { - if (ollama.isRunning()) { - return chatClientBuilder.build(); - } - return null; - } -} diff --git a/src/test/java/com/example/demo/DemoApplicationTests.java b/src/test/java/com/example/demo/DemoApplicationTests.java deleted file mode 100644 index 2778a6a..0000000 --- a/src/test/java/com/example/demo/DemoApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.demo; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class DemoApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/src/test/java/com/example/demo/DevDemoApplication.java b/src/test/java/com/example/demo/DevDemoApplication.java new file mode 100644 index 0000000..9b21a77 --- /dev/null +++ b/src/test/java/com/example/demo/DevDemoApplication.java @@ -0,0 +1,41 @@ +package com.example.demo; + +import com.github.dockerjava.api.model.Bind; +import com.github.dockerjava.api.model.ExposedPort; +import com.github.dockerjava.api.model.PortBinding; +import com.github.dockerjava.api.model.Ports; +import jakarta.annotation.PostConstruct; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.testcontainers.context.ImportTestcontainers; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.testcontainers.ollama.OllamaContainer; + +import java.io.IOException; +import java.util.logging.Logger; + + +public class DevDemoApplication extends DemoApplication { + + public static void main(String[] args) { + + SpringApplication.run(DevDemoApplication.class, args); + } + + @ServiceConnection + OllamaContainer ollama = new OllamaContainer("ollama/ollama:0.1.48") + .withReuse(true) + .withCreateContainerCmdModifier(cmd -> + { + cmd.withBinds(Bind.parse("ollama:/root/.ollama")); + cmd.withPortBindings(new PortBinding(Ports.Binding.bindPort(11434), new ExposedPort(11434))); + }); + + + @PostConstruct + public void start() throws IOException, InterruptedException { + Logger.getGlobal().info("Starting Ollama container and loading model."); + ollama.start(); + ollama.execInContainer("ollama", "pull", "mistral"); + } + +}