Skip to content

Commit

Permalink
Add app health check support to Dapr Testcontainer (#1213)
Browse files Browse the repository at this point in the history
* Add app health check support to Dapr Testcontainer

Signed-off-by: Artur Ciocanu <[email protected]>

* Some minor cleanup

Signed-off-by: Artur Ciocanu <[email protected]>

* Move waiting to beforeEach, it looks more natural

Signed-off-by: Artur Ciocanu <[email protected]>

---------

Signed-off-by: Artur Ciocanu <[email protected]>
Co-authored-by: Artur Ciocanu <[email protected]>
  • Loading branch information
artur-ciocanu and Artur Ciocanu authored Feb 10, 2025
1 parent 0cec586 commit 22d9874
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

Expand All @@ -56,16 +54,18 @@ public class DaprSpringMessagingIT {
private static final Logger logger = LoggerFactory.getLogger(DaprSpringMessagingIT.class);

private static final String TOPIC = "mockTopic";

private static final Network DAPR_NETWORK = Network.newNetwork();
private static final int APP_PORT = 8080;
private static final String SUBSCRIPTION_MESSAGE_PATTERN = ".*app is subscribed to the following topics.*";

@Container
@ServiceConnection
private static final DaprContainer DAPR_CONTAINER = new DaprContainer("daprio/daprd:1.13.2")
.withAppName("messaging-dapr-app")
.withNetwork(DAPR_NETWORK)
.withComponent(new Component("pubsub", "pubsub.in-memory", "v1", Collections.emptyMap()))
.withAppPort(8080)
.withAppPort(APP_PORT)
.withAppHealthCheckPath("/ready")
.withDaprLogLevel(DaprLogLevel.DEBUG)
.withLogConsumer(outputFrame -> System.out.println(outputFrame.getUtf8String()))
.withAppChannelAddress("host.testcontainers.internal");
Expand All @@ -78,16 +78,16 @@ public class DaprSpringMessagingIT {

@BeforeAll
public static void beforeAll(){
org.testcontainers.Testcontainers.exposeHostPorts(8080);
org.testcontainers.Testcontainers.exposeHostPorts(APP_PORT);
}

@BeforeEach
public void beforeEach() throws InterruptedException {
Thread.sleep(1000);
public void beforeEach() {
// Ensure the subscriptions are registered
Wait.forLogMessage(SUBSCRIPTION_MESSAGE_PATTERN, 1).waitUntilReady(DAPR_CONTAINER);
}

@Test
@Disabled("Test is flaky due to global state in the spring test application.")
public void testDaprMessagingTemplate() throws InterruptedException {
for (int i = 0; i < 10; i++) {
var msg = "ProduceAndReadWithPrimitiveMessageType:" + i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class TestRestController {
private static final Logger LOG = LoggerFactory.getLogger(TestRestController.class);
private final List<CloudEvent<String>> events = new ArrayList<>();

@GetMapping("/")
@GetMapping("/ready")
public String ok() {
return "OK";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
private DaprPlacementContainer placementContainer;
private String appName;
private Integer appPort;
private String appHealthCheckPath;
private boolean shouldReusePlacement;

/**
Expand Down Expand Up @@ -116,6 +117,11 @@ public DaprContainer withAppChannelAddress(String appChannelAddress) {
return this;
}

public DaprContainer withAppHealthCheckPath(String appHealthCheckPath) {
this.appHealthCheckPath = appHealthCheckPath;
return this;
}

public DaprContainer withConfiguration(Configuration configuration) {
this.configuration = configuration;
return this;
Expand Down Expand Up @@ -173,7 +179,7 @@ public DaprContainer withComponent(Component component) {
*/
public DaprContainer withComponent(Path path) {
try {
Map<String, Object> component = this.YAML_MAPPER.loadAs(Files.newInputStream(path), Map.class);
Map<String, Object> component = YAML_MAPPER.loadAs(Files.newInputStream(path), Map.class);

String type = (String) component.get("type");
Map<String, Object> metadata = (Map<String, Object>) component.get("metadata");
Expand Down Expand Up @@ -233,12 +239,12 @@ protected void configure() {

List<String> cmds = new ArrayList<>();
cmds.add("./daprd");
cmds.add("-app-id");
cmds.add("--app-id");
cmds.add(appName);
cmds.add("--dapr-listen-addresses=0.0.0.0");
cmds.add("--app-protocol");
cmds.add(DAPR_PROTOCOL.getName());
cmds.add("-placement-host-address");
cmds.add("--placement-host-address");
cmds.add(placementService + ":50005");

if (appChannelAddress != null && !appChannelAddress.isEmpty()) {
Expand All @@ -251,6 +257,12 @@ protected void configure() {
cmds.add(Integer.toString(appPort));
}

if (appHealthCheckPath != null && !appHealthCheckPath.isEmpty()) {
cmds.add("--enable-app-health-check");
cmds.add("--app-health-check-path");
cmds.add(appHealthCheckPath);
}

if (configuration != null) {
cmds.add("--config");
cmds.add("/dapr-resources/" + configuration.getName() + ".yaml");
Expand Down

0 comments on commit 22d9874

Please sign in to comment.