Skip to content
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

SpringApplication.from(…).with(…) adds its sources to every context that's created #35873

Closed
jbeaken opened this issue Jun 13, 2023 · 7 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@jbeaken
Copy link

jbeaken commented Jun 13, 2023

Using Testcontainers at Development Time I have

@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfiguration {

    @Bean
    public KeycloakContainer keycloakContainer(DynamicPropertyRegistry registry) {
        KeycloakContainer keycloakContainer = new KeycloakContainer()...
        keycloakContainer.start();

        registry.add("keycloak.server.external-url", () -> keycloakContainer.getAuthServerUrl());
        registry.add("keycloak.server.internal-url", () -> keycloakContainer.getAuthServerUrl());
        registry.add("keycloak.dev-portal-realm", () -> "master");

        return keycloakContainer;
    }
}

with

public class DMATestApplication {
        public static void main(String[] args) {
            SpringApplication.from(DeveloperManagerApplication::main).with(ContainersConfiguration.class).run(args);;
        }

Upon running
gradle bootTestRun it throws
No qualifying bean of type 'org.springframework.test.context.DynamicPropertyRegistry' available:

	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1824)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1383)

While reading the docs it suggests it is possible, when @ServiceConnection isn't supported

Using a @ServiceConnection is recommended whenever possible, however, dynamic properties can be a useful fallback for technologies that don’t yet have @ServiceConnection support. 

Does anyone know if dynamic properties are supported?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 13, 2023
@wilkinsona
Copy link
Member

The DynamicPropertyRegistry should be auto-configured by TestcontainersPropertySourceAutoConfiguration. It's not clear from what you've described thus far why that isn't happening. If you would like us to spend some more time investigating, please spend some time providing a complete yet minimal sample that reproduces the problem. You can share it with us by pushing it to a separate repository on GitHub or by zipping it up and attaching it to this issue.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label Jun 13, 2023
@jbeaken
Copy link
Author

jbeaken commented Jun 13, 2023

Hi, thanks for the speedy reply. Yes, I was about to create a sample project, but during the process I discovered the problem. It is due to the project using spring cloud kubernetes

implementation 'org.springframework.cloud:spring-cloud-starter-kubernetes-client-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap' 	//Use bootstrap.yml to load configmap and secret

The introduction of the bootstrap context from spring cloud for some reason prevents TestcontainersPropertySourceAutoConfiguration from kicking in. I am looking into it further

My guess is that

@ConditionalOnClass(DynamicPropertyRegistry.class)
public class TestcontainersPropertySourceAutoConfiguration {

As the bootstrap classpath doesn't include DynamicPropertyRegistry.class, the condition is failing

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 13, 2023
@scottfrederick scottfrederick added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Jun 13, 2023
@jbeaken
Copy link
Author

jbeaken commented Jun 13, 2023

Defining the bean in the bootstrap phase by using

  @Bean
    DynamicPropertyRegistry dynamicPropertyRegistry(ConfigurableEnvironment environment) {
        return TestcontainersPropertySource.attach(environment);
    }

doesn't work as there is then a conflict when the normal application context phase kicks in and tries to create a DynamicPropertyRegistry again, so failing

The bean 'dynamicPropertyRegistry', defined in class path resource [org/springframework/boot/testcontainers/properties/TestcontainersPropertySourceAutoConfiguration.class], could not be registered. A bean with that name has already been defined in com.elsevier.titan.developermanager.application.ContainersConfiguration and overriding is disabled.

Does this just mean you cannot combine DynamicPropertyRegistry with spring cloud bootstrap?

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 13, 2023
@jbeaken
Copy link
Author

jbeaken commented Jun 13, 2023

Using

spring:
  main:
    allow-bean-definition-overriding: true

allows the above to work, that is

@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfiguration {

    @Bean
    @Primary
    DynamicPropertyRegistry dynamicPropertyRegistry(ConfigurableEnvironment environment) {
        return TestcontainersPropertySource.attach(environment);
    }

    @Bean
    public KeycloakContainer keycloakContainer(DynamicPropertyRegistry registry) {....

But this doesn't work, as the properties are lost some how,

anyhow, I really can't see what else to try, except remove spring cloud bootstrap.

@wilkinsona
Copy link
Member

the bootstrap classpath

AFAIK, there's no separation between the classes that are available to the bootstrap context and the classes that are available to the main context.

anyhow, I really can't see what else to try, except remove spring cloud bootstrap.

We might be able to suggest something if you provide the minimal sample that was requested above.

@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Jun 13, 2023
@jbeaken
Copy link
Author

jbeaken commented Jun 13, 2023

thanks, here's a minimal : https://github.com/jbeaken/dynamic

run

gradle bootTestRun

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 13, 2023
@wilkinsona wilkinsona self-assigned this Jun 14, 2023
@wilkinsona
Copy link
Member

Thanks for the sample.

The problem's caused by a bug in the new SpringApplication.from support that causes Spring Cloud's bootstrap context to be polluted with your ContainerConfiguration class. You can work around that by not using from:

public static void main(String[] args) {
    new SpringApplicationBuilder(DemoApplication.class).sources(ContainerConfiguration.class).run(args);
}

This avoids the problem but comes at the cost of not being able to reuse the main method in your application's main code.

@wilkinsona wilkinsona changed the title Using Testcontainers at Development Time doesnt support DynamicPropertyRegistry SpringApplication.from(…).with(…) adds its sources to every context that's created Jun 14, 2023
@wilkinsona wilkinsona added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels Jun 14, 2023
@wilkinsona wilkinsona added this to the 3.1.x milestone Jun 14, 2023
@wilkinsona wilkinsona added the for: team-attention An issue we'd like other members of the team to review label Jun 14, 2023
@wilkinsona wilkinsona modified the milestones: 3.1.x, 3.1.1 Jun 16, 2023
@wilkinsona wilkinsona removed the for: team-attention An issue we'd like other members of the team to review label Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants