Skip to content

Commit

Permalink
containers via code!
Browse files Browse the repository at this point in the history
  • Loading branch information
shelajev committed Sep 23, 2024
1 parent 3e9d4e9 commit def479e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.testcontainers.TestMain</mainClass>
<classpathScope>test</classpathScope>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
70 changes: 70 additions & 0 deletions src/test/java/com/testcontainers/TestMain.java
Original file line number Diff line number Diff line change
@@ -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();

}
}

0 comments on commit def479e

Please sign in to comment.