-
Notifications
You must be signed in to change notification settings - Fork 224
Adding DaprSpringBootTest and DaprSidecarContainer annotation for easier ITs authoring #1610
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
Open
artur-ciocanu
wants to merge
4
commits into
dapr:master
Choose a base branch
from
artur-ciocanu:random-port-junit
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+430
−52
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d21b9b
Adding DaprSpringBootTest and DaprSidecarContainer annotation for eas…
artur-ciocanu cda34be
Adding DaprSpringBootTest and DaprSidecarContainer annotation for eas…
artur-ciocanu 0e80974
Move all the helper Dapr SpringBoot annotations to tests, to avoid ex…
artur-ciocanu 7212700
Fix a few issues related to Dapr container usage in ITs.
artur-ciocanu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
64 changes: 64 additions & 0 deletions
64
sdk-tests/src/test/java/io/dapr/testcontainers/spring/DaprContainerFactory.java
This file contains hidden or 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,64 @@ | ||
| /* | ||
| * Copyright 2025 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.testcontainers.spring; | ||
|
|
||
| import io.dapr.testcontainers.DaprContainer; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.ServerSocket; | ||
|
|
||
| import static io.dapr.testcontainers.DaprContainerConstants.DAPR_RUNTIME_IMAGE_TAG; | ||
|
|
||
| /** | ||
| * Factory for creating DaprContainer instances configured for Spring Boot integration tests. | ||
| * | ||
| * <p>This class handles the common setup required for bidirectional communication | ||
| * between Spring Boot applications and the Dapr sidecar in test scenarios.</p> | ||
| */ | ||
| public final class DaprContainerFactory { | ||
|
|
||
| private DaprContainerFactory() { | ||
| // Utility class | ||
| } | ||
|
|
||
| /** | ||
| * Creates a DaprContainer pre-configured for Spring Boot integration tests. | ||
| * This factory method handles the common setup required for bidirectional | ||
| * communication between Spring Boot and the Dapr sidecar: | ||
| * <ul> | ||
| * <li>Allocates a free port for the Spring Boot application</li> | ||
| * <li>Configures the app channel address for container-to-host communication</li> | ||
| * </ul> | ||
| * | ||
| * @param appName the Dapr application name | ||
| * @return a pre-configured DaprContainer for Spring Boot tests | ||
| */ | ||
| public static DaprContainer createForSpringBootTest(String appName) { | ||
| int port = allocateFreePort(); | ||
|
|
||
| return new DaprContainer(DAPR_RUNTIME_IMAGE_TAG) | ||
| .withAppName(appName) | ||
| .withAppPort(port) | ||
| .withAppChannelAddress("host.testcontainers.internal"); | ||
| } | ||
|
|
||
| private static int allocateFreePort() { | ||
| try (ServerSocket socket = new ServerSocket(0)) { | ||
| socket.setReuseAddress(true); | ||
| return socket.getLocalPort(); | ||
| } catch (IOException e) { | ||
| throw new IllegalStateException("Failed to allocate free port", e); | ||
| } | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
sdk-tests/src/test/java/io/dapr/testcontainers/spring/DaprSidecarContainer.java
This file contains hidden or 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,67 @@ | ||
| /* | ||
| * Copyright 2025 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.testcontainers.spring; | ||
|
|
||
| import org.testcontainers.junit.jupiter.Container; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * Marks a static field containing a {@link io.dapr.testcontainers.DaprContainer} | ||
| * for automatic integration with Spring Boot tests. | ||
| * | ||
| * <p>This annotation combines the Testcontainers {@link Container} annotation | ||
| * with Dapr-specific configuration. When used with {@link DaprSpringBootTest}, | ||
| * it automatically:</p> | ||
| * <ul> | ||
| * <li>Manages the container lifecycle via Testcontainers</li> | ||
| * <li>Configures Spring properties (server.port, dapr.http.endpoint, dapr.grpc.endpoint)</li> | ||
| * </ul> | ||
| * | ||
| * <p><b>Important:</b> For tests that require Dapr-to-app communication (like actor tests), | ||
| * you must call {@code Testcontainers.exposeHostPorts(container.getAppPort())} | ||
| * in your {@code @BeforeEach} method before registering actors or making Dapr calls.</p> | ||
| * | ||
| * <p>Example usage:</p> | ||
| * <pre>{@code | ||
| * @DaprSpringBootTest(classes = MyApplication.class) | ||
| * class MyDaprIT { | ||
| * | ||
| * @DaprSidecarContainer | ||
| * private static final DaprContainer DAPR = DaprContainer.createForSpringBootTest("my-app") | ||
| * .withComponent(new Component("statestore", "state.in-memory", "v1", Map.of())); | ||
| * | ||
| * @BeforeEach | ||
| * void setUp() { | ||
| * Testcontainers.exposeHostPorts(DAPR.getAppPort()); | ||
| * } | ||
| * | ||
| * @Test | ||
| * void testSomething() { | ||
| * // Your test code here | ||
| * } | ||
| * } | ||
| * }</pre> | ||
| * | ||
| * @see DaprSpringBootTest | ||
| * @see io.dapr.testcontainers.DaprContainer#createForSpringBootTest(String) | ||
| */ | ||
| @Target(ElementType.FIELD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Container | ||
| public @interface DaprSidecarContainer { | ||
| } |
104 changes: 104 additions & 0 deletions
104
sdk-tests/src/test/java/io/dapr/testcontainers/spring/DaprSpringBootContextInitializer.java
This file contains hidden or 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,104 @@ | ||
| /* | ||
| * Copyright 2025 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.testcontainers.spring; | ||
|
|
||
| import io.dapr.testcontainers.DaprContainer; | ||
| import org.springframework.context.ApplicationContextInitializer; | ||
| import org.springframework.context.ConfigurableApplicationContext; | ||
| import org.springframework.core.env.MapPropertySource; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Spring {@link ApplicationContextInitializer} that configures Dapr-related properties | ||
| * based on the {@link DaprContainer} registered by {@link DaprSpringBootExtension}. | ||
| * | ||
| * <p>This initializer sets the following properties:</p> | ||
| * <ul> | ||
| * <li>{@code server.port} - The port allocated for the Spring Boot application</li> | ||
| * <li>{@code dapr.http.endpoint} - The HTTP endpoint of the Dapr sidecar</li> | ||
| * <li>{@code dapr.grpc.endpoint} - The gRPC endpoint of the Dapr sidecar</li> | ||
| * </ul> | ||
| * | ||
| * <p>This initializer is automatically registered when using {@link DaprSpringBootTest}.</p> | ||
| */ | ||
| public class DaprSpringBootContextInitializer | ||
| implements ApplicationContextInitializer<ConfigurableApplicationContext> { | ||
|
|
||
| private static final String PROPERTY_SOURCE_NAME = "daprTestcontainersProperties"; | ||
|
|
||
| @Override | ||
| public void initialize(ConfigurableApplicationContext applicationContext) { | ||
| DaprContainer container = findContainer(); | ||
|
|
||
| if (container == null) { | ||
| throw new IllegalStateException( | ||
| "No DaprContainer found in registry. Ensure you are using @DaprSpringBootTest " | ||
| + "with a @DaprSidecarContainer annotated field." | ||
| ); | ||
| } | ||
|
|
||
| // Create a property source with lazy resolution for endpoints | ||
| // server.port can be resolved immediately since it's set at container creation time | ||
| // Dapr endpoints are resolved lazily since the container may not be started yet | ||
| applicationContext.getEnvironment().getPropertySources() | ||
| .addFirst(new DaprLazyPropertySource(PROPERTY_SOURCE_NAME, container)); | ||
| } | ||
|
|
||
| private DaprContainer findContainer() { | ||
| // Return the first container in the registry | ||
| // In a test scenario, there should only be one test class running at a time | ||
| return DaprSpringBootExtension.CONTAINER_REGISTRY.values().stream() | ||
| .findFirst() | ||
| .orElse(null); | ||
| } | ||
|
|
||
| /** | ||
| * Custom PropertySource that lazily resolves Dapr container endpoints. | ||
| * This allows the endpoints to be resolved after the container has started. | ||
| */ | ||
| private static class DaprLazyPropertySource extends MapPropertySource { | ||
| private final Map<String, Supplier<Object>> lazyProperties; | ||
|
|
||
| DaprLazyPropertySource(String name, DaprContainer container) { | ||
| super(name, new HashMap<>()); | ||
|
|
||
| this.lazyProperties = new HashMap<>(); | ||
| lazyProperties.put("server.port", container::getAppPort); | ||
| lazyProperties.put("dapr.http.endpoint", container::getHttpEndpoint); | ||
| lazyProperties.put("dapr.grpc.endpoint", container::getGrpcEndpoint); | ||
| } | ||
|
|
||
| @Override | ||
| public Object getProperty(String name) { | ||
| Supplier<Object> supplier = lazyProperties.get(name); | ||
| if (supplier != null) { | ||
| return supplier.get(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean containsProperty(String name) { | ||
| return lazyProperties.containsKey(name); | ||
| } | ||
|
|
||
| @Override | ||
| public String[] getPropertyNames() { | ||
| return lazyProperties.keySet().toArray(new String[0]); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@artur-ciocanu I am surprised that this is the only tests that can be changed.. did you added this here to show it it will work?