|
| 1 | +package com.testcontainers; |
| 2 | + |
| 3 | +import com.testcontainers.fun.awaitility.CloudflaredContainer; |
| 4 | +import org.testcontainers.containers.GenericContainer; |
| 5 | +import org.testcontainers.containers.KafkaContainer; |
| 6 | +import org.testcontainers.containers.Network; |
| 7 | +import org.testcontainers.containers.PostgreSQLContainer; |
| 8 | +import org.testcontainers.containers.localstack.LocalStackContainer; |
| 9 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 10 | +import org.testcontainers.images.builder.ImageFromDockerfile; |
| 11 | +import org.testcontainers.lifecycle.Startables; |
| 12 | +import org.testcontainers.utility.DockerImageName; |
| 13 | + |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.nio.file.Paths; |
| 16 | +import java.util.Scanner; |
| 17 | + |
| 18 | +import static org.testcontainers.utility.DockerImageName.parse; |
| 19 | + |
| 20 | +public class TestMain { |
| 21 | + |
| 22 | + public static void main(String[] args) { |
| 23 | + Network network = Network.newNetwork(); |
| 24 | + |
| 25 | + PostgreSQLContainer<?> postgres = |
| 26 | + new PostgreSQLContainer<>(parse("postgres:16-alpine")) |
| 27 | + .withNetwork(network).withNetworkAliases("postgres"); |
| 28 | + |
| 29 | + KafkaContainer kafka = new KafkaContainer(parse("confluentinc/cp-kafka:7.5.0")) |
| 30 | + .withNetwork(network).withNetworkAliases("kafka"); |
| 31 | + |
| 32 | + LocalStackContainer localStack = new LocalStackContainer(parse("localstack/localstack:2.3")) |
| 33 | + .withNetwork(network).withNetworkAliases("localstack"); |
| 34 | + |
| 35 | + |
| 36 | + Path dockerfile = Paths.get("Dockerfile"); |
| 37 | + GenericContainer<?> app = new GenericContainer<>(new ImageFromDockerfile("gatling-demo-app", false) |
| 38 | + .withFileFromPath("Dockerfile", Paths.get("Dockerfile")) |
| 39 | + .withFileFromPath("target/java-local-development-workshop-0.0.1-SNAPSHOT.jar", Paths.get("target/java-local-development-workshop-0.0.1-SNAPSHOT.jar")) |
| 40 | + ) |
| 41 | + |
| 42 | + .withExposedPorts(8080) |
| 43 | + .withEnv("SPRING_KAFKA_BOOTSTRAP_SERVERS", "BROKER://kafka:9092") |
| 44 | + .withEnv("SPRING_DATASOURCE_URL", "jdbc:postgresql://postgres:5432/test") |
| 45 | + .withEnv("SPRING_DATASOURCE_USERNAME", "test") |
| 46 | + .withEnv("SPRING_DATASOURCE_PASSWORD", "test") |
| 47 | + .withEnv("SPRING_CLOUD_AWS_CREDENTIALS_ACCESS-KEY", localStack.getAccessKey()) |
| 48 | + .withEnv("SPRING_CLOUD_AWS_CREDENTIALS_SECRET-KEY", localStack.getSecretKey()) |
| 49 | + .withEnv("SPRING_CLOUD_AWS_REGION_STATIC", localStack.getRegion()) |
| 50 | + .withEnv("SPRING_CLOUD_AWS_ENDPOINT", "localstack:4566") |
| 51 | + |
| 52 | + .withNetwork(network) |
| 53 | + .waitingFor(Wait.forHttp("/actuator/health")); |
| 54 | + |
| 55 | + Startables.deepStart(postgres, kafka, localStack).join(); |
| 56 | + |
| 57 | + app.start(); |
| 58 | + |
| 59 | + CloudflaredContainer cloudflaredContainer = new CloudflaredContainer(parse("cloudflare/cloudflared"), app.getMappedPort(8080)); |
| 60 | + cloudflaredContainer.start(); |
| 61 | + |
| 62 | + String publicUrl = cloudflaredContainer.getPublicUrl(); |
| 63 | + |
| 64 | + System.out.println(publicUrl); |
| 65 | + |
| 66 | + |
| 67 | + new Scanner(System.in).nextLine(); |
| 68 | + |
| 69 | + } |
| 70 | +} |
0 commit comments