From 3e9d4e977d6a67a8bca4fe4ed7b5c73c6c5ccf72 Mon Sep 17 00:00:00 2001 From: shelajev Date: Mon, 23 Sep 2024 15:52:56 +0300 Subject: [PATCH] containerized app --- Dockerfile | 4 ++ .../catalog/ContainersConfig.java | 18 ++++-- .../awaitility/CloudflaredContainerTest.java | 62 ------------------- 3 files changed, 17 insertions(+), 67 deletions(-) create mode 100644 Dockerfile delete mode 100644 src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainerTest.java diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..82cbffe --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM azul/zulu-openjdk:17-latest +ARG JAR_FILE=target/*.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/src/test/java/com/testcontainers/catalog/ContainersConfig.java b/src/test/java/com/testcontainers/catalog/ContainersConfig.java index 43e6434..a49987a 100644 --- a/src/test/java/com/testcontainers/catalog/ContainersConfig.java +++ b/src/test/java/com/testcontainers/catalog/ContainersConfig.java @@ -3,6 +3,7 @@ import static org.testcontainers.utility.DockerImageName.parse; import com.testcontainers.catalog.domain.FileStorageService; +import net.bytebuddy.asm.Advice; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.boot.testcontainers.service.connection.ServiceConnection; @@ -11,33 +12,40 @@ import org.springframework.test.context.DynamicPropertyRegistry; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.KafkaContainer; +import org.testcontainers.containers.Network; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.containers.localstack.LocalStackContainer; +import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.elasticsearch.ElasticsearchContainer; +import org.testcontainers.lifecycle.Startables; import org.testcontainers.utility.MountableFile; @TestConfiguration(proxyBeanMethods = false) public class ContainersConfig { + static Network network = Network.newNetwork(); + @Bean @ServiceConnection PostgreSQLContainer postgresContainer() { PostgreSQLContainer selfPostgreSQLContainer = - new PostgreSQLContainer<>(parse("postgres:16-alpine")); - - + new PostgreSQLContainer<>(parse("postgres:16-alpine")) + .withNetwork(network).withNetworkAliases("postgres"); return selfPostgreSQLContainer; } @Bean @ServiceConnection KafkaContainer kafkaContainer() { - return new KafkaContainer(parse("confluentinc/cp-kafka:7.5.0")).withReuse(true); + return new KafkaContainer(parse("confluentinc/cp-kafka:7.5.0")) + .withReuse(true).withNetwork(network).withNetworkAliases("kafka"); + } @Bean("localstackContainer") LocalStackContainer localstackContainer(DynamicPropertyRegistry registry) { - LocalStackContainer localStack = new LocalStackContainer(parse("localstack/localstack:2.3")).withReuse(true); + LocalStackContainer localStack = new LocalStackContainer(parse("localstack/localstack:2.3")) + .withNetwork(network).withNetworkAliases("localstack").withReuse(true); registry.add("spring.cloud.aws.credentials.access-key", localStack::getAccessKey); registry.add("spring.cloud.aws.credentials.secret-key", localStack::getSecretKey); registry.add("spring.cloud.aws.region.static", localStack::getRegion); diff --git a/src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainerTest.java b/src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainerTest.java deleted file mode 100644 index afda870..0000000 --- a/src/test/java/com/testcontainers/fun/awaitility/CloudflaredContainerTest.java +++ /dev/null @@ -1,62 +0,0 @@ -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"); - }); - - - } - } - } -} - - -//