Not able to override AWS endpoint during Integration test
See original GitHub issueType: Bug
Component: Secrets Manager
Describe the bug
I am using the latest 3.0.0-M2 version for spring cloud to pull secrets from the secrets manager.
spring:
cloud:
aws:
endpoint: http://localhost:4566
credentials:
profile:
name: localstack
config:
import:
- aws-secretsmanager:/secret/spring-boot-app;/secret/db-credential
I run a local docker image of localstack , start the application and it all works fines. It pulls the secret and no issues.
Now I am trying to write an integration test using localstack and I am overriding the endpoint properties using @DynamicPropertySource
mechanism.
@Testcontainers
@SpringBootTest
class MainApplicationTests {
@Autowired
Environment environment;
@Container
static LocalStackContainer localStackContainer = new LocalStackContainer(DockerImageName.parse("localstack/localstack"))
.withCopyFileToContainer(MountableFile.forClasspathResource("script.sh"),
"/docker-entrypoint-initaws.d/")
.withServices(LocalStackContainer.Service.SECRETSMANAGER)
.withExposedPorts(4566)
.waitingFor(Wait.forLogMessage(".*localstack.request.aws.*", 2));
@DynamicPropertySource
static void properties(DynamicPropertyRegistry registry) {
registry.add("spring.cloud.aws.secretsmanager.endpoint", () -> localStackContainer.getEndpointOverride(LocalStackContainer.Service.SECRETSMANAGER));
registry.add("spring.cloud.aws.endpoint", () -> "http://localhost:"+localStackContainer.getMappedPort(4566));
registry.add("spring.datasource.url", () -> database.getJdbcUrl());
}
when I run the test, the client is still using the endpoint defined in the properties file ( not the test properties file) and fails with an error that it could not connect to the endpoint http://localhost:4566
( which is hte url from the main properties file)
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:4566 [localhost/127.0.0.1] failed: Connection refused
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
at software.amazon.awssdk.http.apache.internal.conn.ClientConnectionManagerFactory$DelegatingHttpClientConnectionManager.connect(ClientConnectionManagerFactory.java:86)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
I debugged the properties fetch for the database properties override with test containers, it is all correct.
I am not sure why the lambda given in the DynamicPropertySource
to override the properties does not even get invoked and hence it uses the properties from the main properties file and fails.
I am not sure if I am doing something wrong or missing something.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
So I preferred using System.setProperty as @eddumelendez said. And it works and it is cleaner I felt.
But it was nice to know about you can run tests the way that @maciejwalkowiak showed in the example.
I will be writing an article about the AWS Secrets Manager integration using Spring Cloud Secrets Manager soon. Also nice work with the new Spring Cloud AWS 3.0 version. 👍
Also thank you once again @maciejwalkowiak and @eddumelendez.
System.setProperty can be used and still use the random port from testcontainers. See the following example https://github.com/eddumelendez/testcontainers-localstack/blob/main/secretsmanager/src/test/java/com/example/secretsmanager/SecretsmanagerApplicationTests.java
The same applies for parameterstore