From 7cf50aac43c9f62f0a07edad9a8cf841b4ffe401 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 27 Feb 2024 09:24:41 +0100 Subject: [PATCH] test: HandoverIT-POC - Moved general container-defs into base-class --- .../chat/backend/AbstractHandoverIT.java | 127 ++++++++++++ .../kafka/chat/backend/KafkaHandoverIT.java | 186 +++++------------- 2 files changed, 174 insertions(+), 139 deletions(-) diff --git a/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java index fc54c032..f384f7e8 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/AbstractHandoverIT.java @@ -7,18 +7,27 @@ import lombok.Getter; import lombok.Setter; import lombok.ToString; import lombok.extern.slf4j.Slf4j; +import org.awaitility.Awaitility; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.codec.ServerSentEvent; import org.springframework.web.reactive.function.client.WebClient; +import org.testcontainers.containers.BindMode; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.output.Slf4jLogConsumer; +import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.images.ImagePullPolicy; import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.util.retry.Retry; +import java.io.IOException; import java.time.Duration; import java.util.stream.IntStream; @@ -117,6 +126,124 @@ public abstract class AbstractHandoverIT WebClient webClient; + abstract void setUpExtra() throws IOException, InterruptedException; + + @BeforeEach + void setUp() throws Exception + { + setUpExtra(); + haproxy.start(); + backend1.start(); + // backend2.start(); + // backend3.start(); + + Integer port = haproxy.getMappedPort(8400); + webClient = WebClient.create("http://localhost:" + port); + + Awaitility + .await() + .atMost(Duration.ofMinutes(10)) + .until(() -> WebClient + .create("http://localhost:" + backend1.getMappedPort(8080)) + .get() + .uri("/actuator/health") + .exchangeToMono(response -> + { + if (response.statusCode().equals(HttpStatus.OK)) + { + return response + .bodyToMono(StatusTo.class) + .map(StatusTo::getStatus) + .map(status -> status.equalsIgnoreCase("UP")); + } + else + { + return Mono.just(false); + } + }) + .block()); + + haproxy + .getDockerClient() + .killContainerCmd(haproxy.getContainerId()) + .withSignal("HUP") + .exec(); + + + Awaitility + .await() + .atMost(Duration.ofMinutes(10)) + .until(() -> webClient + .get() + .uri("/actuator/health") + .exchangeToMono(response -> + { + if (response.statusCode().equals(HttpStatus.OK)) + { + return response + .bodyToMono(StatusTo.class) + .map(StatusTo::getStatus) + .map(status -> status.equalsIgnoreCase("UP")); + } + else + { + return Mono.just(false); + } + }) + .block()); + } + + Network network = Network.newNetwork(); + + GenericContainer haproxy = + new GenericContainer(DockerImageName.parse("haproxytech/haproxy-debian:2.8")) + .withNetwork(network) + .withNetworkAliases("haproxy") + .withClasspathResourceMapping( + "haproxy.cfg", + "/usr/local/etc/haproxy/haproxy.cfg", + BindMode.READ_ONLY) + .withClasspathResourceMapping( + "sharding.map", + "/usr/local/etc/haproxy/sharding.map", + BindMode.READ_WRITE) + .withExposedPorts(8400, 8401, 8404) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("HAPROXY")); + + abstract String[] getCommandBackend1(); + GenericContainer backend1 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(network) + .withNetworkAliases("backend-1") + .withCommand(getCommandBackend1()) + .withExposedPorts(8080) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-1")); + + abstract String[] getCommandBackend2(); + GenericContainer backend2 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(network) + .withNetworkAliases("backend-2") + .withCommand(getCommandBackend2()) + .withExposedPorts(8080) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-2")); + + abstract String[] getCommandBackend3(); + GenericContainer backend3 = + new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) + .withImagePullPolicy(NEVER_PULL) + .withNetwork(network) + .withNetworkAliases("backend-3") + .withCommand(getCommandBackend3()) + .withExposedPorts(8080) + .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) + .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-3")); + + @EqualsAndHashCode @ToString class User diff --git a/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java b/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java index 585f32ea..7c511fd9 100644 --- a/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java +++ b/src/test/java/de/juplo/kafka/chat/backend/KafkaHandoverIT.java @@ -1,28 +1,21 @@ package de.juplo.kafka.chat.backend; import lombok.extern.slf4j.Slf4j; -import org.awaitility.Awaitility; -import org.junit.jupiter.api.BeforeEach; -import org.springframework.http.HttpStatus; -import org.springframework.web.reactive.function.client.WebClient; import org.testcontainers.containers.*; import org.testcontainers.containers.output.Slf4jLogConsumer; import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.utility.DockerImageName; -import reactor.core.publisher.Mono; import java.io.IOException; -import java.time.Duration; @Slf4j class KafkaHandoverIT extends AbstractHandoverIT { - @BeforeEach - void setUpWebClient() throws IOException, InterruptedException + @Override + void setUpExtra() throws IOException, InterruptedException { kafka.start(); - haproxy.start(); Container.ExecResult result; result = kafka.execInContainer( @@ -53,70 +46,9 @@ class KafkaHandoverIT extends AbstractHandoverIT result.getExitCode(), result.getStdout(), result.getStdout()); - - backend1.start(); - // backend2.start(); - // backend3.start(); - - Integer port = haproxy.getMappedPort(8400); - webClient = WebClient.create("http://localhost:" + port); - - Awaitility - .await() - .atMost(Duration.ofMinutes(10)) - .until(() -> WebClient - .create("http://localhost:" + backend1.getMappedPort(8080)) - .get() - .uri("/actuator/health") - .exchangeToMono(response -> - { - if (response.statusCode().equals(HttpStatus.OK)) - { - return response - .bodyToMono(StatusTo.class) - .map(StatusTo::getStatus) - .map(status -> status.equalsIgnoreCase("UP")); - } - else - { - return Mono.just(false); - } - }) - .block()); - - haproxy - .getDockerClient() - .killContainerCmd(haproxy.getContainerId()) - .withSignal("HUP") - .exec(); - - - Awaitility - .await() - .atMost(Duration.ofMinutes(10)) - .until(() -> webClient - .get() - .uri("/actuator/health") - .exchangeToMono(response -> - { - if (response.statusCode().equals(HttpStatus.OK)) - { - return response - .bodyToMono(StatusTo.class) - .map(StatusTo::getStatus) - .map(status -> status.equalsIgnoreCase("UP")); - } - else - { - return Mono.just(false); - } - }) - .block()); } - Network network = Network.newNetwork(); - KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0")) .withNetwork(network) @@ -126,75 +58,51 @@ class KafkaHandoverIT extends AbstractHandoverIT .waitingFor(Wait.forLogMessage(".*Kafka\\ Server\\ started.*\\n", 1)) .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("KAFKA")); - GenericContainer backend1 = - new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) - .withImagePullPolicy(NEVER_PULL) - .withNetwork(network) - .withNetworkAliases("backend-1") - .withCommand( - "--chat.backend.instance-id=backend-1", - "--chat.backend.services=kafka", - "--chat.backend.kafka.bootstrap-servers=kafka:9999", - "--chat.backend.kafka.instance-uri=http://backend-1:8080", - "--chat.backend.kafka.num-partitions=10", - "--chat.backend.kafka.client-id-prefix=B1", - "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", - "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" - ) - .withExposedPorts(8080) - .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-1")); - - GenericContainer backend2 = - new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) - .withImagePullPolicy(NEVER_PULL) - .withNetwork(network) - .withNetworkAliases("backend-2") - .withCommand( - "--chat.backend.instance-id=backend-2", - "--chat.backend.services=kafka", - "--chat.backend.kafka.bootstrap-servers=kafka:9999", - "--chat.backend.kafka.instance-uri=http://backend-2:8080", - "--chat.backend.kafka.num-partitions=10", - "--chat.backend.kafka.client-id-prefix=B2", - "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", - "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" - ) - .withExposedPorts(8080) - .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-2")); + @Override + String[] getCommandBackend1() + { + return new String[] + { + "--chat.backend.instance-id=backend-1", + "--chat.backend.services=kafka", + "--chat.backend.kafka.bootstrap-servers=kafka:9999", + "--chat.backend.kafka.instance-uri=http://backend-1:8080", + "--chat.backend.kafka.num-partitions=10", + "--chat.backend.kafka.client-id-prefix=B1", + "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", + "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" + }; + } - GenericContainer backend3 = - new GenericContainer(DockerImageName.parse("juplo/chat-backend:0.0.1-SNAPSHOT")) - .withImagePullPolicy(NEVER_PULL) - .withNetwork(network) - .withNetworkAliases("backend-3") - .withCommand( - "--chat.backend.instance-id=backend-3", - "--chat.backend.services=kafka", - "--chat.backend.kafka.bootstrap-servers=kafka:9999", - "--chat.backend.kafka.instance-uri=http://backend-3:8080", - "--chat.backend.kafka.num-partitions=10", - "--chat.backend.kafka.client-id-prefix=B3", - "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", - "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" - ) - .withExposedPorts(8080) - .waitingFor(Wait.forLogMessage(".*Started\\ ChatBackendApplication.*\\n", 1)) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("BACKEND-3")); + @Override + String[] getCommandBackend2() + { + return new String[] + { + "--chat.backend.instance-id=backend-2", + "--chat.backend.services=kafka", + "--chat.backend.kafka.bootstrap-servers=kafka:9999", + "--chat.backend.kafka.instance-uri=http://backend-2:8080", + "--chat.backend.kafka.num-partitions=10", + "--chat.backend.kafka.client-id-prefix=B2", + "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", + "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" + }; + } - GenericContainer haproxy = - new GenericContainer(DockerImageName.parse("haproxytech/haproxy-debian:2.8")) - .withNetwork(network) - .withNetworkAliases("haproxy") - .withClasspathResourceMapping( - "haproxy.cfg", - "/usr/local/etc/haproxy/haproxy.cfg", - BindMode.READ_ONLY) - .withClasspathResourceMapping( - "sharding.map", - "/usr/local/etc/haproxy/sharding.map", - BindMode.READ_WRITE) - .withExposedPorts(8400, 8401, 8404) - .withLogConsumer(new Slf4jLogConsumer(log, true).withPrefix("HAPROXY")); + @Override + String[] getCommandBackend3() + { + return new String[] + { + "--chat.backend.instance-id=backend-3", + "--chat.backend.services=kafka", + "--chat.backend.kafka.bootstrap-servers=kafka:9999", + "--chat.backend.kafka.instance-uri=http://backend-3:8080", + "--chat.backend.kafka.num-partitions=10", + "--chat.backend.kafka.client-id-prefix=B3", + "--chat.backend.kafka.haproxy-runtime-api=haproxy:8401", + "--chat.backend.kafka.haproxy-map=/usr/local/etc/haproxy/sharding.map" + }; + } } -- 2.20.1