From 841c841b441f59434f1d1676c72151c03770bb1c Mon Sep 17 00:00:00 2001 From: shelajev Date: Mon, 10 Jun 2024 09:58:42 +0100 Subject: [PATCH] captains demos --- pom.xml | 6 ++ .../catalog/ContainersConfig.java | 4 +- .../testcontainers/catalog/ReproduceMe.java | 25 ++++++++ .../fun/awaitility/CloudflaredContainer.java | 40 ++++++++++++ .../awaitility/CloudflaredContainerTest.java | 62 +++++++++++++++++++ .../fun/cantspellAI/OllamaContainerTest.java | 29 +++++++++ .../fun/parametrized/JavaGCsTest.java | 52 ++++++++++++++++ 7 files changed, 217 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/testcontainers/catalog/ReproduceMe.java create mode 100644 src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainer.java create mode 100644 src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainerTest.java create mode 100644 src/test/java/com/testcontainers/fun/cantspellAI/OllamaContainerTest.java create mode 100644 src/test/java/com/testcontainers/fun/parametrized/JavaGCsTest.java diff --git a/pom.xml b/pom.xml index 9747232..23907b9 100644 --- a/pom.xml +++ b/pom.xml @@ -17,6 +17,7 @@ 17 + 1.19.8 3.1.0 1.3.2 1.0-alpha-13 @@ -104,6 +105,11 @@ postgresql test + + org.testcontainers + ollama + test + org.testcontainers kafka diff --git a/src/test/java/com/testcontainers/catalog/ContainersConfig.java b/src/test/java/com/testcontainers/catalog/ContainersConfig.java index c25a1ad..43e6434 100644 --- a/src/test/java/com/testcontainers/catalog/ContainersConfig.java +++ b/src/test/java/com/testcontainers/catalog/ContainersConfig.java @@ -22,7 +22,9 @@ public class ContainersConfig { @Bean @ServiceConnection PostgreSQLContainer postgresContainer() { - PostgreSQLContainer selfPostgreSQLContainer = new PostgreSQLContainer<>(parse("postgres:16-alpine")); + PostgreSQLContainer selfPostgreSQLContainer = + new PostgreSQLContainer<>(parse("postgres:16-alpine")); + return selfPostgreSQLContainer; } diff --git a/src/test/java/com/testcontainers/catalog/ReproduceMe.java b/src/test/java/com/testcontainers/catalog/ReproduceMe.java new file mode 100644 index 0000000..6e76f17 --- /dev/null +++ b/src/test/java/com/testcontainers/catalog/ReproduceMe.java @@ -0,0 +1,25 @@ +package com.testcontainers.catalog; + +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.containers.wait.strategy.Wait; + +public class ReproduceMe { + + public static void main(String[] args) { + + PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:15-alpine"); + + postgreSQLContainer.start(); + postgreSQLContainer.getJdbcUrl(); + + GenericContainer redis = new GenericContainer("redis:6-alpine") + .withExposedPorts(6379); + + redis.start(); + + System.out.println(redis.getHost() + ":" + redis.getMappedPort(6379)); + + redis.stop(); + } +} diff --git a/src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainer.java b/src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainer.java new file mode 100644 index 0000000..e19ec6a --- /dev/null +++ b/src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainer.java @@ -0,0 +1,40 @@ +package com.testcontainers.fun.awaitility; + +import org.testcontainers.Testcontainers; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.utility.DockerImageName; + +public class CloudflaredContainer extends GenericContainer { + + private String publicUrl; + + public CloudflaredContainer(DockerImageName dockerImageName, int port) { + super(dockerImageName); + dockerImageName.assertCompatibleWith(DockerImageName.parse("cloudflare/cloudflared")); + withAccessToHost(true); + Testcontainers.exposeHostPorts(port); + withCommand("tunnel", "--url", String.format("http://host.testcontainers.internal:%d", port)); + waitingFor(Wait.forLogMessage(".*Registered tunnel connection.*", 1)); + } + + public String getPublicUrl() { + if (null != publicUrl) { + return publicUrl; + } + String logs = getLogs(); + String[] split = logs.split(String.format("\\n")); + boolean found = false; + for (int i = 0; i < split.length; i++) { + String currentLine = split[i]; + if (currentLine.contains("Your quick Tunnel has been created")) { + found = true; + continue; + } + if (found) { + return publicUrl = currentLine.substring(currentLine.indexOf("http"), currentLine.indexOf(".com") + 4); + } + } + throw new IllegalStateException("Didn't find public url in logs. Has container started?"); + } +} \ No newline at end of file diff --git a/src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainerTest.java b/src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainerTest.java new file mode 100644 index 0000000..afda870 --- /dev/null +++ b/src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainerTest.java @@ -0,0 +1,62 @@ +package com.testcontainers.fun.awaitility; + +import io.restassured.RestAssured; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; +import org.testcontainers.shaded.org.awaitility.Awaitility; +import org.testcontainers.utility.DockerImageName; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class CloudflaredContainerTest { + + + @Test + public void shouldStartAndTunnelToHelloWorld() throws IOException { + try ( + GenericContainer helloworld = new GenericContainer<>( + DockerImageName.parse("testcontainers/helloworld:1.1.0") + ) + .withNetworkAliases("helloworld") + .withExposedPorts(8080, 8081) + .waitingFor(new HttpWaitStrategy()) + ) { + helloworld.start(); + try ( + CloudflaredContainer cloudflare = new CloudflaredContainer( + DockerImageName.parse("cloudflare/cloudflared:2024.5.0"), + helloworld.getFirstMappedPort() + ); + ) { + cloudflare.start(); + String url = cloudflare.getPublicUrl(); + assertThat(url).as("Public url contains 'cloudflare'").contains("cloudflare"); + System.setProperty("networkaddress.cache.ttl", "0"); + + Awaitility.await().pollDelay(10, TimeUnit.SECONDS) + .atMost(30, TimeUnit.SECONDS) + .ignoreExceptions().untilAsserted(() -> { + String body = RestAssured.given().baseUri(url) + .get() + .body() + .print(); + + assertThat(body.trim()).as("the index page contains the title 'Hello world'").contains("Hello world"); + }); + + + } + } + } +} + + +// diff --git a/src/test/java/com/testcontainers/fun/cantspellAI/OllamaContainerTest.java b/src/test/java/com/testcontainers/fun/cantspellAI/OllamaContainerTest.java new file mode 100644 index 0000000..e4b1b4a --- /dev/null +++ b/src/test/java/com/testcontainers/fun/cantspellAI/OllamaContainerTest.java @@ -0,0 +1,29 @@ +package com.testcontainers.fun.cantspellAI; + + +import org.junit.jupiter.api.Test; +import org.testcontainers.ollama.OllamaContainer; + +import static io.restassured.RestAssured.given; +import static org.assertj.core.api.Assertions.assertThat; + +public class OllamaContainerTest { + + @Test + public void withDefaultConfig() { + try ( // container { + OllamaContainer ollama = new OllamaContainer("ollama/ollama:0.1.40") + // } + ) { + ollama.start(); + + String version = given() + .baseUri(ollama.getEndpoint()) + .get("/api/version") + .jsonPath() + .get("version"); + + assertThat(version).isEqualTo("0.1.40"); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/testcontainers/fun/parametrized/JavaGCsTest.java b/src/test/java/com/testcontainers/fun/parametrized/JavaGCsTest.java new file mode 100644 index 0000000..0b6edc8 --- /dev/null +++ b/src/test/java/com/testcontainers/fun/parametrized/JavaGCsTest.java @@ -0,0 +1,52 @@ +package com.testcontainers.fun.parametrized; + + + +import com.github.dockerjava.api.model.HostConfig; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.startupcheck.IndefiniteWaitOneShotStartupCheckStrategy; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; + +public class JavaGCsTest { + + @CsvSource({ + ".*SerialGC.*true.*, 1791", + ".*G1GC.*true.*, 1792"} + ) + @ParameterizedTest + void whichGCWillJavaChoose(String gcRegex, long memoryLimitInMB) throws IOException { + try (var container = + new GenericContainer<>("eclipse-temurin:17-jdk") + .withCreateContainerCmdModifier(createContainerCmd -> { + var hostConfig = new HostConfig(); + hostConfig.withMemory(memoryLimitInMB * 1024L * 1024L); + hostConfig.withCpuCount(1L); + createContainerCmd.withHostConfig(hostConfig); + } + ) + .withCommand("java -XX:+PrintFlagsFinal -version && sleep infinity") + .withStartupCheckStrategy(new IndefiniteWaitOneShotStartupCheckStrategy()) + ) { + container.start(); + var logs = container.getLogs(); + Assertions.assertThat(logs).containsPattern(gcRegex); + } + } + + @TestFactory + Collection dynamicTestsWithCollection() { + return Arrays.asList( + DynamicTest.dynamicTest("TestSerialGC", + () -> whichGCWillJavaChoose(".*SerialGC.*true.*", 1791)), + DynamicTest.dynamicTest("TestG1GC", + () -> whichGCWillJavaChoose(".*G1GC.*true.*", 1792))); + } +} \ No newline at end of file