From def479eaf8d12c6989870258371ffa449b158512 Mon Sep 17 00:00:00 2001 From: shelajev Date: Mon, 23 Sep 2024 16:09:13 +0300 Subject: [PATCH] containers via code! --- pom.xml | 10 ++- .../java/com/testcontainers/TestMain.java | 70 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/testcontainers/TestMain.java diff --git a/pom.xml b/pom.xml index a589480..efb6cde 100644 --- a/pom.xml +++ b/pom.xml @@ -175,7 +175,15 @@ org.springframework.boot spring-boot-maven-plugin - + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + com.testcontainers.TestMain + test + + diff --git a/src/test/java/com/testcontainers/TestMain.java b/src/test/java/com/testcontainers/TestMain.java new file mode 100644 index 0000000..f66e308 --- /dev/null +++ b/src/test/java/com/testcontainers/TestMain.java @@ -0,0 +1,70 @@ +package com.testcontainers; + +import com.testcontainers.fun.awaitility.CloudflaredContainer; +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.images.builder.ImageFromDockerfile; +import org.testcontainers.lifecycle.Startables; +import org.testcontainers.utility.DockerImageName; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Scanner; + +import static org.testcontainers.utility.DockerImageName.parse; + +public class TestMain { + + public static void main(String[] args) { + Network network = Network.newNetwork(); + + PostgreSQLContainer postgres = + new PostgreSQLContainer<>(parse("postgres:16-alpine")) + .withNetwork(network).withNetworkAliases("postgres"); + + KafkaContainer kafka = new KafkaContainer(parse("confluentinc/cp-kafka:7.5.0")) + .withNetwork(network).withNetworkAliases("kafka"); + + LocalStackContainer localStack = new LocalStackContainer(parse("localstack/localstack:2.3")) + .withNetwork(network).withNetworkAliases("localstack"); + + + Path dockerfile = Paths.get("Dockerfile"); + GenericContainer app = new GenericContainer<>(new ImageFromDockerfile("gatling-demo-app", false) + .withFileFromPath("Dockerfile", Paths.get("Dockerfile")) + .withFileFromPath("target/java-local-development-workshop-0.0.1-SNAPSHOT.jar", Paths.get("target/java-local-development-workshop-0.0.1-SNAPSHOT.jar")) + ) + + .withExposedPorts(8080) + .withEnv("SPRING_KAFKA_BOOTSTRAP_SERVERS", "BROKER://kafka:9092") + .withEnv("SPRING_DATASOURCE_URL", "jdbc:postgresql://postgres:5432/test") + .withEnv("SPRING_DATASOURCE_USERNAME", "test") + .withEnv("SPRING_DATASOURCE_PASSWORD", "test") + .withEnv("SPRING_CLOUD_AWS_CREDENTIALS_ACCESS-KEY", localStack.getAccessKey()) + .withEnv("SPRING_CLOUD_AWS_CREDENTIALS_SECRET-KEY", localStack.getSecretKey()) + .withEnv("SPRING_CLOUD_AWS_REGION_STATIC", localStack.getRegion()) + .withEnv("SPRING_CLOUD_AWS_ENDPOINT", "localstack:4566") + + .withNetwork(network) + .waitingFor(Wait.forHttp("/actuator/health")); + + Startables.deepStart(postgres, kafka, localStack).join(); + + app.start(); + + CloudflaredContainer cloudflaredContainer = new CloudflaredContainer(parse("cloudflare/cloudflared"), app.getMappedPort(8080)); + cloudflaredContainer.start(); + + String publicUrl = cloudflaredContainer.getPublicUrl(); + + System.out.println(publicUrl); + + + new Scanner(System.in).nextLine(); + + } +}