Skip to content

Commit

Permalink
captains demos
Browse files Browse the repository at this point in the history
  • Loading branch information
shelajev committed Jun 10, 2024
1 parent 4d4ddba commit 841c841
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<properties>
<java.version>17</java.version>
<testcontainers.version>1.19.8</testcontainers.version>
<awspring.version>3.1.0</awspring.version>
<commons-io.version>1.3.2</commons-io.version>
<wiremock-testcontainers-module.version>1.0-alpha-13</wiremock-testcontainers-module.version>
Expand Down Expand Up @@ -104,6 +105,11 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>ollama</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>kafka</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/testcontainers/catalog/ReproduceMe.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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<CloudflaredContainer> {

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?");
}
}
Original file line number Diff line number Diff line change
@@ -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");
});


}
}
}
}


//
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
52 changes: 52 additions & 0 deletions src/test/java/com/testcontainers/fun/parametrized/JavaGCsTest.java
Original file line number Diff line number Diff line change
@@ -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<DynamicTest> dynamicTestsWithCollection() {
return Arrays.asList(
DynamicTest.dynamicTest("TestSerialGC",
() -> whichGCWillJavaChoose(".*SerialGC.*true.*", 1791)),
DynamicTest.dynamicTest("TestG1GC",
() -> whichGCWillJavaChoose(".*G1GC.*true.*", 1792)));
}
}

0 comments on commit 841c841

Please sign in to comment.