-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?"); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
|
||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
// |
29 changes: 29 additions & 0 deletions
29
src/test/java/com/testcontainers/fun/cantspellAI/OllamaContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
src/test/java/com/testcontainers/fun/parametrized/JavaGCsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
} |