-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial Testcontainers integration for Dapr (#1085)
* initial testcontainers pr Signed-off-by: salaboy <Salaboy@gmail.com> * fixing variable reference Signed-off-by: salaboy <Salaboy@gmail.com> * adding equals to spotbug issues Signed-off-by: salaboy <Salaboy@gmail.com> * adding http port to run tests Signed-off-by: salaboy <Salaboy@gmail.com> * updating pom Signed-off-by: salaboy <Salaboy@gmail.com> * fixing style Signed-off-by: salaboy <Salaboy@gmail.com> * extracting classes Signed-off-by: salaboy <Salaboy@gmail.com> * removing restassured dependency Signed-off-by: salaboy <Salaboy@gmail.com> * refactoring IT out to sdk-tests Signed-off-by: salaboy <Salaboy@gmail.com> * adding correct wiremock dep version Signed-off-by: salaboy <Salaboy@gmail.com> * missing header Signed-off-by: salaboy <Salaboy@gmail.com> * fixing spotbugs issue Signed-off-by: salaboy <Salaboy@gmail.com> * adding hashcode too Signed-off-by: salaboy <Salaboy@gmail.com> * testing configure method Signed-off-by: salaboy <Salaboy@gmail.com> * making inmutable collections and maps Signed-off-by: salaboy <Salaboy@gmail.com> * checkstyle Signed-off-by: salaboy <Salaboy@gmail.com> * removing space Signed-off-by: salaboy <Salaboy@gmail.com> * Refactor tracking of alpha artifact version Signed-off-by: Artur Souza <asouza.pro@gmail.com> Signed-off-by: salaboy <Salaboy@gmail.com> * Use Usage: docker compose [OPTIONS] COMMAND Define and run multi-container applications with Docker Options: --all-resources Include all resources, even those not used by services --ansi string Control when to print ANSI control characters ("never"|"always"|"auto") (default "auto") --compatibility Run compose in backward compatibility mode --dry-run Execute command in dry run mode --env-file stringArray Specify an alternate environment file -f, --file stringArray Compose configuration files --parallel int Control max parallelism, -1 for unlimited (default -1) --profile stringArray Specify a profile to enable --progress string Set type of progress output (auto, tty, plain, quiet) (default "auto") --project-directory string Specify an alternate working directory (default: the path of the, first specified, Compose file) -p, --project-name string Project name Commands: attach Attach local standard input, output, and error streams to a service's running container build Build or rebuild services config Parse, resolve and render compose file in canonical format cp Copy files/folders between a service container and the local filesystem create Creates containers for a service down Stop and remove containers, networks events Receive real time events from containers exec Execute a command in a running container images List images used by the created containers kill Force stop service containers logs View output from containers ls List running compose projects pause Pause services port Print the public port for a port binding ps List containers pull Pull service images push Push service images restart Restart service containers rm Removes stopped service containers run Run a one-off command on a service scale Scale services start Start services stats Display a live stream of container(s) resource usage statistics stop Stop services top Display the running processes unpause Unpause services up Create and start containers version Show the Docker Compose version information wait Block until the first service container stops watch Watch build context for service and rebuild/refresh containers when files are updated Run 'docker compose COMMAND --help' for more information on a command. instead of docker-compose Signed-off-by: Artur Souza <asouza.pro@gmail.com> Signed-off-by: salaboy <Salaboy@gmail.com> * removing version from docker compose Signed-off-by: salaboy <Salaboy@gmail.com> * Update README.md Signed-off-by: Artur Souza <asouza.pro@gmail.com> --------- Signed-off-by: salaboy <Salaboy@gmail.com> Signed-off-by: Artur Souza <asouza.pro@gmail.com> Co-authored-by: Artur Souza <asouza.pro@gmail.com>
- Loading branch information
1 parent
5618af5
commit e30dc2d
Showing
24 changed files
with
1,148 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
examples/src/main/java/io/dapr/examples/bindings/http/docker-compose-single-kafka.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
version: '3' | ||
services: | ||
zookeeper: | ||
image: confluentinc/cp-zookeeper:7.4.4 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
examples/src/main/java/io/dapr/examples/querystate/docker-compose-single-mongo.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
version: '3' | ||
services: | ||
mongo: | ||
image: mongo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
examples/src/main/java/io/dapr/examples/state/docker-compose-single-mongo.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
version: '3' | ||
services: | ||
mongo: | ||
image: mongo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
sdk-tests/src/test/java/io/dapr/it/testcontainers/DaprContainerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright 2024 The Dapr Authors | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package io.dapr.it.testcontainers; | ||
|
||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import io.dapr.client.DaprClient; | ||
import io.dapr.client.DaprClientBuilder; | ||
import io.dapr.client.domain.Metadata; | ||
import io.dapr.client.domain.State; | ||
|
||
import io.dapr.testcontainers.DaprContainer; | ||
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 java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.any; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.post; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
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 java.util.Collections.singletonMap; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class DaprContainerTest { | ||
|
||
// Time-to-live for messages published. | ||
private static final String MESSAGE_TTL_IN_SECONDS = "1000"; | ||
private static final String STATE_STORE_NAME = "kvstore"; | ||
private static final String KEY = "my-key"; | ||
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 | ||
public static DaprContainer daprContainer = new DaprContainer("daprio/daprd") | ||
.withAppName("dapr-app") | ||
.withAppPort(8081) | ||
.withAppChannelAddress("host.testcontainers.internal"); | ||
|
||
/** | ||
* Sets the Dapr properties for the test. | ||
*/ | ||
@BeforeClass | ||
public static void setDaprProperties() { | ||
configStub(); | ||
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))); | ||
|
||
stubFor(get(urlMatching("/dapr/config")) | ||
.willReturn(aResponse().withBody("[]").withStatus(200))); | ||
|
||
stubFor(any(urlMatching("/([a-z1-9]*)")) | ||
.willReturn(aResponse().withBody("[]").withStatus(200))); | ||
|
||
// create a stub | ||
stubFor(post(urlEqualTo("/events")) | ||
.willReturn(aResponse().withBody("event received!").withStatus(200))); | ||
|
||
configureFor("localhost", 8081); | ||
} | ||
|
||
@Test | ||
public void testDaprContainerDefaults() { | ||
assertEquals( | ||
"The pubsub and kvstore component should be configured by default", | ||
2, | ||
daprContainer.getComponents().size()); | ||
assertEquals( | ||
"A subscription should be configured by default if none is provided", | ||
1, | ||
daprContainer.getSubscriptions().size()); | ||
} | ||
|
||
@Test | ||
public void testStateStore() throws Exception { | ||
try (DaprClient client = (new DaprClientBuilder()).build()) { | ||
client.waitForSidecar(5000).block(); | ||
|
||
String value = "value"; | ||
// Save state | ||
client.saveState(STATE_STORE_NAME, KEY, value).block(); | ||
|
||
// Get the state back from the state store | ||
State<String> retrievedState = client.getState(STATE_STORE_NAME, KEY, String.class).block(); | ||
|
||
assertEquals("The value retrieved should be the same as the one stored", value, retrievedState.getValue()); | ||
} | ||
} | ||
|
||
@Test | ||
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(); | ||
} | ||
|
||
OkHttpClient client = new OkHttpClient.Builder().build(); | ||
|
||
String url = "http://" + daprContainer.getHost() + ":" + daprContainer.getMappedPort(3500); | ||
Request request = new Request.Builder().url(url + "/v1.0/metadata").build(); | ||
|
||
try (Response response = client.newCall(request).execute()) { | ||
if (response.isSuccessful()) { | ||
assertTrue(response.body().string().contains("placement: connected")); | ||
|
||
} else { | ||
throw new IOException("Unexpected response: " + response.code()); | ||
} | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void testPubSub() throws Exception { | ||
try (DaprClient client = (new DaprClientBuilder()).build()) { | ||
client.waitForSidecar(5000).block(); | ||
|
||
String message = "message content"; | ||
Map<String, String> metadata = singletonMap(Metadata.TTL_IN_SECONDS, MESSAGE_TTL_IN_SECONDS); | ||
client.publishEvent(PUBSUB_NAME, PUBSUB_TOPIC_NAME, message, metadata).block(); | ||
} | ||
|
||
verify(getRequestedFor(urlMatching("/dapr/config"))); | ||
verify(postRequestedFor(urlEqualTo("/events")).withHeader("Content-Type", equalTo("application/cloudevents+json"))); | ||
} | ||
} |
Oops, something went wrong.