Skip to content

Commit

Permalink
Merge pull request #45 from diagridio/improve-daprcontainer
Browse files Browse the repository at this point in the history
Start DaprPlacementContainer automatically when DaprContainer is started
  • Loading branch information
salaboy authored May 13, 2024
2 parents f594118 + c3f7c78 commit 6a24af3
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<profiles>
<profile>
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/io/diagrid/dapr/DaprContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.util.Objects;
import java.util.Set;

import org.jetbrains.annotations.Nullable;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.images.builder.Transferable;
import org.testcontainers.utility.DockerImageName;
import org.yaml.snakeyaml.DumperOptions;
Expand All @@ -23,7 +25,7 @@

public class DaprContainer extends GenericContainer<DaprContainer> {

public static enum DaprLogLevel {
public enum DaprLogLevel {
error, warn, info, debug
}

Expand Down Expand Up @@ -119,6 +121,9 @@ public List<MetadataEntry> getMetadata() {
private String placementService = "placement:50006";
private static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("daprio/daprd");
private Yaml yaml;
private DaprPlacementContainer placementContainer;
private String placementDockerImageName = "daprio/placement";
private boolean shouldReusePlacement = false;

public DaprContainer(DockerImageName dockerImageName) {
super(dockerImageName);
Expand Down Expand Up @@ -275,6 +280,17 @@ public Map<String, Object> subscriptionToMap(Subscription subscription) {
@Override
protected void configure() {
super.configure();

if (this.placementContainer == null) {
Network daprNetwork = Network.newNetwork();
this.placementContainer = new DaprPlacementContainer(this.placementDockerImageName)
.withNetwork(daprNetwork)
.withNetworkAliases("placement")
.withReuse(this.shouldReusePlacement);
dependsOn(this.placementContainer);
withNetwork(daprNetwork);
}

withCommand(
"./daprd",
"-app-id", appName,
Expand Down Expand Up @@ -339,4 +355,20 @@ public String getPlacementService() {
public static DockerImageName getDefaultImageName() {
return DEFAULT_IMAGE_NAME;
}

public DaprContainer withPlacementImage(String placementDockerImageName) {
this.placementDockerImageName = placementDockerImageName;
return this;
}

public DaprContainer withReusablePlacement(boolean reuse) {
this.shouldReusePlacement = shouldReusePlacement;
return this;
}

public DaprContainer withPlacementContainer(DaprPlacementContainer placementContainer) {
this.placementContainer = placementContainer;
return this;
}

}
17 changes: 15 additions & 2 deletions src/test/java/io/diagrid/dapr/DaprContainerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.dapr.client.domain.State;

import io.restassured.RestAssured;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
Expand All @@ -16,13 +17,14 @@
import static java.util.Collections.singletonMap;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertTrue;

public class DaprContainerTest {

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

@ClassRule()
@ClassRule
public static DaprContainer daprContainer = new DaprContainer("daprio/daprd")
.withAppName("dapr-app")
.withAppPort(8081)
Expand Down Expand Up @@ -72,6 +74,17 @@ public void testStateStoreAPIs() throws Exception {

}

@Test
public void testPlacement() throws Exception {
RestAssured.baseURI = "http://" + daprContainer.getHost() + ":" + daprContainer.getMappedPort(3500);
boolean isPlacementConnected = RestAssured.given()
.get("/v1.0/metadata")
.jsonPath()
.getString("actorRuntime.placement")
.contentEquals("placement: connected");
assertTrue(isPlacementConnected);
}

@Test
public void testPubSubAPIs() throws Exception {

Expand Down

0 comments on commit 6a24af3

Please sign in to comment.