aSemy
aSemy

Reputation: 7159

Spring Boot Cloud in a TestContainer - connect to a URL without LoadBalancer/service discovery

I am writing an integration test. I have a Spring Boot 2.5 application running in a Testcontainer. I also have StubRunnerExtension running a WireMock.

I need the Spring Boot app to connect to the stubbed WireMock server.

The error is happening because Spring thinks that host.testcontainers.internal is the name of a service. It isn't - it's the special Testcontainer hostname provided exactly for this situation (connecting to the host from a Testcontainer).

The Wiremock stub is definitely running and connectable. If I docker exec -it into the container during runtime, I can connect to it and get a valid response with curl http://host.testcontainers.internal.

I have tried many, many, many types of config to disable the Spring Boot loadbalancer, either in application.yml, bootstrap.yml, and environment variables. These are definitely loaded by the Spring Application - but they don't do anything to help.

Trying to disable the discovery clients doesn't do anything

eureka.client.enabled=false
eureka.cloud.discovery.enabled=false
spring.cloud.discovery.reactive.enabled=false
spring.cloud.discovery.blocking.enabled=false
spring.cloud.config.failFast=false

How can I configure my Spring Boot app to connect to a URL? This has to be an integration test - I can't edit the source code of the application. I don't need discovery at all, if I can hardcode "service_name=http://host.testcontainers.internal" that would be fine.


Here's the rest config:

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.web.client.RestTemplate;


@Configuration
public class RestConfig {

  @Bean
  @LoadBalanced
  public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.build();
  }
}

@Test
fun integrationtest() {
    // my Spring Boot server
    val server = SpringBootAppTC()
    server.start()

    // verify the stubs are running
    stubRunnerExtension.findAllRunningStubs().allServicesNames.forEach {
      logger.info("stub [$it] is running")
    }
    
    assertTrue(server.isRunning)

   // assertions...
}

companion object {

    @JvmField
    @RegisterExtension
    val stubRunnerExtension: StubRunnerExtension = StubRunnerExtension()...

}

No servers available for service: host.testcontainers.internal
    at org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient.execute(BlockingLoadBalancerClient.java:79)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:179)
    at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$RedeliveryTask.run(RedeliveryErrorHandler.java:712)
    at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$RedeliveryTask.doRun(RedeliveryErrorHandler.java:804)
    at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:81)
    at org.apache.camel.component.bean.AbstractBeanProcessor.process(AbstractBeanProcessor.java:146)
    at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:286)
    at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:316)
    at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:494)
    at org.apache.camel.support.ObjectHelper.invokeMethodSafe(ObjectHelper.java:376)
    at java.base/java.lang.reflect.Method.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at my.application.RestApiService(RestApiService.java:39)
    at java.base/java.util.TimerThread.run(Unknown Source)
    at java.base/java.util.TimerThread.mainLoop(Unknown Source)
    at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:76)
    at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:209)
    at org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:398)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:184)
    at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:64)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:651)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:751)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:776)
    at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:66)
    at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
    at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:77)
    at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:93)
    at org.springframework.boot.actuate.metrics.web.client.MetricsClientHttpRequestInterceptor.intercept(MetricsClientHttpRequestInterceptor.java:86)
    at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:93)
    at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:56)

Here's how I've tried to configure SimpleDiscoveryClient in the EnvironmentVariables of the Testcontainer:

"SPRING_APPLICATION_JSON" to """
{
  "spring": {
    "cloud": {
      "discovery": {
        "client": {
          "simple": {
            "instances": {
              "contract-service": [
                {
                  "uri": "http://host.testcontainers.internal:60104"
                }
              ]
            }
          }
        }
      }
    }
  }
}

Versions

Upvotes: 1

Views: 1559

Answers (1)

aSemy
aSemy

Reputation: 7159

I have resolved this by

  1. setting
  • spring.cloud.discovery.client.simple.instances.contract-service[0].uri=http://host.testcontainers.internal:$PORT in the Testcontainer SPRING_APPLICATION_JSON (docs)
  • spring.cloud.discovery.enabled=true
  • eureka.client.enabled=false
  • spring.cloud.config.failFast=false (docs, not sure if fail-fast is important)
  1. using org.testcontainers.Testcontainers.exposeHostPorts(...) before the Spring Boot container is started (docs)
  2. setting my custom Spring Boot app config to point at the discoverable service name endpoint.contract.base=http://contract-service:$PORT

EDIT: I also found that even though I had disabled Eureka, I still needed this dependency: org.springframework.cloud:spring-cloud-starter-netflix-eureka-client. Without it, discovery was completely disabled and the Spring Boot app could directly connect to host.testcontainers.internal - no muss no fuss.

Stripped down code to demonstrate:

Test class

import kotlin.test.assertEquals
import kotlin.test.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.RegisterExtension
import org.slf4j.LoggerFactory
import org.springframework.cloud.contract.stubrunner.junit.StubRunnerExtension
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties
import org.testcontainers.containers.GenericContainer
import org.testcontainers.containers.output.OutputFrame
import org.testcontainers.containers.output.WaitingConsumer
import org.testcontainers.containers.wait.strategy.Wait
import org.testcontainers.junit.jupiter.Testcontainers


@Testcontainers
class MyIntegrationTest {

  init {
    // see https://www.testcontainers.org/features/networking/#exposing-host-ports-to-the-container
    org.testcontainers.Testcontainers.exposeHostPorts(contractServicePort)
  }

  @Test
  fun `test Contract Service stub`() {

    // verify that the contract-service stub is running, and the hardcoded port is correct
    val contractServiceUrl = stubRunnerExtension.findStubUrl("contract-service")
    assertEquals(contractServicePort, contractServiceUrl.port)

    // manually construct the URL for contract-service
    val contractServiceDiscoverableName = "http://contract-service:$contractServicePort"
    val contractServiceUri = "http://host.testcontainers.internal:$contractServicePort"
    // (see https://www.testcontainers.org/features/networking/#exposing-host-ports-to-the-container)

    // now we have all the pieces, we can create the my SB app test container
    val mySpringBootApp =
      MySpringBootApplicationTestContainer(contractServiceDiscoverableName, contractServiceUri)
    // manually start container
    mySpringBootApp.start()
    assertTrue(mySpringBootApp.isRunning)

    // verify the stubs are running
    stubRunnerExtension.findAllRunningStubs().allServicesNames.forEach {
      logger.info("Stub '$it' is running ")
    }

    val logConsumer = WaitingConsumer()
    mySpringBootApp.followOutput(logConsumer, OutputFrame.OutputType.STDOUT)
    logConsumer.waitUntil { frame ->
      frame.utf8String.contains("just putting something here so the test doesn't quit immediately and I can investigate")
    }

    // todo - verification of output
  }

  companion object {

    private val logger = LoggerFactory.getLogger(MyIntegrationTest::class.java)

    /** Hardcode a port for the contract-service mock */
    private const val contractServicePort: Int = 60104

    /** Download stubs from maven */
    @JvmField
    @RegisterExtension
    val stubRunnerExtension: StubRunnerExtension =
      StubRunnerExtension()
        .stubsMode(StubRunnerProperties.StubsMode.LOCAL)
        .failOnNoStubs(true)
        .downloadStub("my.company:contract-service:4.3.1")
        .withPort(contractServicePort)
  }
}

Testcontainer definition

class MySpringBootApplicationTestContainer(
  private val contractServiceDiscoverableName: String,
  private val contractServiceUri: String,
  imageName: String = "my.project/my-spring-boot-application"
) : GenericContainer<MySpringBootApplicationTestContainer>(imageName) {

  init {
    waitingFor(
      Wait.forLogMessage(
        ".*Started MySpringBootApplication.*".toRegex(RegexOption.IGNORE_CASE).pattern, 1
      )
    )
  }

  override fun configure() {
    super.configure()

    // set Spring Boot environment variables
    withEnv(
      mapOf(
        "spring.cloud.discovery.enabled" to "true",
        "eureka.client.enabled" to "false",

        // https://cloud.spring.io/spring-cloud-contract/reference/html/project-features.html#features-stub-runner-cloud-stubbing-profiles
        "spring.cloud.config.failFast" to "false",
        "SPRING_APPLICATION_JSON" to
            """
              {
                "spring": {
                  "cloud": {
                    "discovery": {
                      "client": {
                        "simple": {
                          "instances": {
                            "contract-service": [
                              {
                                "uri": "$contractServiceUri"
                              }
                            ]
                          }
                        }
                      }
                    }
                  }
                }
              }
            """.trimIndent(),

        // set the test containers
        "endpoint.contract.base" to contractServiceDiscoverableName,
      )

      // (note I'm setting environment variables in configure() because there are
      // other test containers that MySpringBootApplicationTestContainer depends on,
      // and I need to wait for them to start before fetching their URIs
    )
  }
}

Resolved application properties

If I log all application properties on Spring Boot startup (with this), the relevant settings are:

endpoint.contract.base: http://contract-service:60104
eureka.client.enabled: false
java.vm.version: 11.0.11+9-LTS
jdk.debug: release
line.separator: 

loadbalancer.client.name: contract-service

spring.cloud.client.hostname: 574557a76be5
spring.cloud.client.ip-address: 172.17.0.5
spring.cloud.config.failFast: false
spring.cloud.discovery.client.simple.instances.contract-service[0].uri: http://host.testcontainers.internal:60104
spring.cloud.discovery.enabled: true

I don't know where loadbalancer.client.name: contract-service is coming from or being set. I don't know if the spring.cloud.client.* props are important or relevant.

Upvotes: 0

Related Questions