Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Dapr Container default wait strategy based on healthz/outbound #1105

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sdk-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<springboot.version>3.3.1</springboot.version>
<logback-classic.version>1.4.12</logback-classic.version>
<wiremock.version>3.9.1</wiremock.version>
<testcontainers-dapr.version>${dapr.sdk.alpha.version}</testcontainers-dapr.version>
<testcontainers-test.version>1.20.0</testcontainers-test.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -214,6 +216,17 @@
<version>4.0.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.dapr</groupId>
<artifactId>testcontainers-dapr</artifactId>
<version>${testcontainers-dapr.version}</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${testcontainers-test.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

package io.dapr.it.testcontainers;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
import io.dapr.client.DaprClient;
import io.dapr.client.DaprClientBuilder;
import io.dapr.client.domain.Metadata;
Expand All @@ -23,10 +23,10 @@
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.testcontainers.Testcontainers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.io.IOException;
import java.util.Map;
Expand All @@ -43,11 +43,12 @@
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@Testcontainers
@WireMockTest(httpPort = 8081)
public class DaprContainerTest {

// Time-to-live for messages published.
Expand All @@ -57,10 +58,7 @@ public class DaprContainerTest {
private static final String PUBSUB_NAME = "pubsub";
private static final String PUBSUB_TOPIC_NAME = "topic";

@ClassRule
public static WireMockRule wireMockRule = new WireMockRule(wireMockConfig().port(8081));

@ClassRule
@Container
public static DaprContainer daprContainer = new DaprContainer("daprio/daprd")
.withAppName("dapr-app")
.withAppPort(8081)
Expand All @@ -69,16 +67,15 @@ public class DaprContainerTest {
/**
* Sets the Dapr properties for the test.
*/
@BeforeClass
@BeforeAll
public static void setDaprProperties() {
configStub();
Testcontainers.exposeHostPorts(8081);
org.testcontainers.Testcontainers.exposeHostPorts(8081);
System.setProperty("dapr.grpc.port", Integer.toString(daprContainer.getGrpcPort()));
System.setProperty("dapr.http.port", Integer.toString(daprContainer.getHttpPort()));
}

private static void configStub() {

stubFor(any(urlMatching("/dapr/subscribe"))
.willReturn(aResponse().withBody("[]").withStatus(200)));

Expand All @@ -98,19 +95,20 @@ private static void configStub() {
@Test
public void testDaprContainerDefaults() {
assertEquals(
"The pubsub and kvstore component should be configured by default",
2,
daprContainer.getComponents().size());
daprContainer.getComponents().size(),
"The pubsub and kvstore component should be configured by default"
);
assertEquals(
"A subscription should be configured by default if none is provided",
1,
daprContainer.getSubscriptions().size());
daprContainer.getSubscriptions().size(),
"A subscription should be configured by default if none is provided");
}

@Test
public void testStateStore() throws Exception {
try (DaprClient client = (new DaprClientBuilder()).build()) {
client.waitForSidecar(5000).block();
client.waitForSidecar(1000).block();

String value = "value";
// Save state
Expand All @@ -127,7 +125,7 @@ public void testStateStore() throws Exception {
public void testPlacement() throws Exception {
// Here we are just waiting for Dapr to be ready
try (DaprClient client = (new DaprClientBuilder()).build()) {
client.waitForSidecar(5000).block();
client.waitForSidecar(1000).block();
}

OkHttpClient client = new OkHttpClient.Builder().build();
Expand All @@ -143,13 +141,12 @@ public void testPlacement() throws Exception {
throw new IOException("Unexpected response: " + response.code());
}
}

}

@Test
public void testPubSub() throws Exception {
try (DaprClient client = (new DaprClientBuilder()).build()) {
client.waitForSidecar(5000).block();
client.waitForSidecar(1000).block();

String message = "message content";
Map<String, String> metadata = singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,23 @@
limitations under the License.
*/

package io.dapr.testcontainers;
package io.dapr.it.testcontainers;

import org.junit.Assert;
import org.junit.ClassRule;
import org.junit.Test;
import io.dapr.testcontainers.DaprPlacementContainer;
import org.junit.jupiter.api.Test;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Testcontainers
public class DaprPlacementContainerTest {

@ClassRule
public static DaprPlacementContainer placement = new DaprPlacementContainer("daprio/placement");
@Container
private static final DaprPlacementContainer PLACEMENT_CONTAINER = new DaprPlacementContainer("daprio/placement");

@Test
public void testDaprPlacementContainerDefaults() {
Assert.assertEquals("The default port is set", 50005, placement.getPort());
assertEquals(50005, PLACEMENT_CONTAINER.getPort(), "The default port is set");
}
}
5 changes: 5 additions & 0 deletions testcontainers-dapr/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.DockerImageName;
import org.yaml.snakeyaml.DumperOptions;
Expand All @@ -38,6 +40,10 @@ public class DaprContainer extends GenericContainer<DaprContainer> {

private static final int DAPRD_DEFAULT_HTTP_PORT = 3500;
private static final int DAPRD_DEFAULT_GRPC_PORT = 50001;
private static final WaitStrategy WAIT_STRATEGY = Wait.forHttp("/v1.0/healthz/outbound")
.forPort(DAPRD_DEFAULT_HTTP_PORT)
.forStatusCodeMatching(statusCode -> statusCode >= 200 && statusCode <= 399);

private final Set<Component> components = new HashSet<>();
private final Set<Subscription> subscriptions = new HashSet<>();
private DaprProtocol protocol = DaprProtocol.HTTP;
Expand All @@ -59,23 +65,9 @@ public class DaprContainer extends GenericContainer<DaprContainer> {
public DaprContainer(DockerImageName dockerImageName) {
super(dockerImageName);
dockerImageName.assertCompatibleWith(DEFAULT_IMAGE_NAME);
// For susbcriptions the container needs to access the app channel
withAccessToHost(true);
// Here we don't want to wait for the Dapr sidecar to be ready, as the sidecar
// needs to
// connect with the application for susbcriptions

withExposedPorts(DAPRD_DEFAULT_HTTP_PORT, DAPRD_DEFAULT_GRPC_PORT);

}

private static Yaml getYamlMapper() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
Representer representer = new Representer(options);
representer.addClassTag(MetadataEntry.class, Tag.MAP);
return new Yaml(representer);
setWaitStrategy(WAIT_STRATEGY);
}

/**
Expand Down Expand Up @@ -124,8 +116,6 @@ public DaprContainer withComponent(Component component) {
return this;
}



/**
* Adds a Dapr component from a YAML file.
* @param path Path to the YAML file.
Expand Down Expand Up @@ -197,6 +187,7 @@ public Map<String, Object> componentToMap(Component component) {
if (!component.getMetadata().isEmpty()) {
componentSpec.put("metadata", component.getMetadata());
}

componentProps.put("spec", componentSpec);
return Collections.unmodifiableMap(componentProps);
}
Expand Down Expand Up @@ -231,6 +222,7 @@ protected void configure() {
if (getNetwork() == null) {
withNetwork(Network.newNetwork());
}

if (this.placementContainer == null) {
this.placementContainer = new DaprPlacementContainer(this.placementDockerImageName)
.withNetwork(getNetwork())
Expand All @@ -253,10 +245,12 @@ protected void configure() {
cmds.add("--app-channel-address");
cmds.add(appChannelAddress);
}

if (appPort != null) {
cmds.add("--app-port");
cmds.add(Integer.toString(appPort));
}

cmds.add("--log-level");
cmds.add(daprLogLevel.toString());
cmds.add("-components-path");
Expand Down Expand Up @@ -338,4 +332,13 @@ public boolean equals(Object o) {
public int hashCode() {
return super.hashCode();
}

private static Yaml getYamlMapper() {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
options.setPrettyFlow(true);
Representer representer = new Representer(options);
representer.addClassTag(MetadataEntry.class, Tag.MAP);
return new Yaml(representer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public enum DaprProtocol {
HTTP("http"),
GRPC("grpc");

private String name;
private final String name;

DaprProtocol(String name) {
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
package io.dapr.testcontainers;

public class Subscription {
private String name;
private String pubsubName;
private String topic;
private String route;
private final String name;
private final String pubsubName;
private final String topic;
private final String route;

/**
* Creates a new subscription.
Expand Down
Loading
Loading