Closed amrutprabhu closed 2 years ago
This is a limitation of @DynamicPropertySource
- it does not work with config import (https://github.com/spring-projects/spring-boot/issues/26148). I see two ways to workaround it:
SpringApplication
testing approach instead of @SpringBootTest
as suggested in the linked issue@SpringBootTest(properties = "spring.cloud.aws.secretsmanager.endpoint=http://localhost:4566")
class AppTests {
@Autowired
Environment environment;
static LocalStackContainer localStackContainer;
@BeforeAll
static void beforeAll() {
MyLocalStackContainer myLocalStackContainer = new MyLocalStackContainer(DockerImageName.parse("localstack/localstack"));
myLocalStackContainer.addFixedExposedPort(4566, 4566);
myLocalStackContainer.withServices(LocalStackContainer.Service.SECRETSMANAGER);
myLocalStackContainer.start();
localStackContainer = myLocalStackContainer;
}
@AfterAll
static void afterAll() {
localStackContainer.stop();
}
@Test
void foo() {
System.out.println("Foo");
}
static class MyLocalStackContainer extends LocalStackContainer {
public MyLocalStackContainer(DockerImageName dockerImageName) {
super(dockerImageName);
}
public void addFixedExposedPort(int hostPort, int containerPort) {
super.addFixedExposedPort(hostPort, containerPort);
}
}
}
Let me know if any of these fits your use case or perhaps you have some other ideas.
Hey @maciejwalkowiak ,
Maybe I didn't understand properly.
The link that you shared shows it does not work for spring.config.import
but I am trying to override the spring.cloud.aws.secretsmanager.endpoint
.
Also what exactly do you mean by Use SpringApplication testing approach instead of @SpringBootTest
? I always wrote integration tests with @SpringBootTest
. Is there another way of testing? do you mean by @ExtendWith(SpringExtension.class)
Sorry for my noob question š
Yes you're overriding the endpoint property but for the purpose of changing beans created in the bootstrap phase for Secrets Manager spring.config.import
implementation.
There is another way of testing but it's not as nice as @SpringBootTest
(see https://github.com/spring-projects/spring-boot/blob/6254ad634ecb0dc73034038f82fcde75fb99117a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigFileApplicationListenerLegacyReproTests.java).
Sorry for my noob question š
No worries this is not a noob question and even if it would be - this is a safe space to ask noob questions :)
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
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. :+1:
Also thank you once again @maciejwalkowiak and @eddumelendez.
@amrutprabhu , thanks a lot for your article, it helped me to find the solution with System.setProperty
, as I was also banging my head against the @DynamicPropertySource
wall.
It's a pity that https://github.com/spring-projects/spring-boot/issues/26148 was just closed, I think at least making the documentation for @DynamicPropertySource
a bit more clear would have helped people not to waste time on it in these cases.
Type: 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.
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.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)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.