micronaut-projects / micronaut-test-resources

Test resources support
Apache License 2.0
8 stars 17 forks source link

Postgres container still starting when required properties supplied and when not availble redis not starting up #181

Closed scprek closed 1 year ago

scprek commented 1 year ago

For context, I am trying to create 2 micronaut environments and have the ability to specify remote for connecting to dev resources if needed instead of containers. However these issues still occur if everything is just in application.yml for example I put the postgres URLs in the application.yml file and it still was starting the container.

Expected Behavior

I have postgres, kafka, redis and when I have the following, I expect all containers to start up

Default

./gradlew run

application.yml

micronaut:
  application:
    name: testResourcesAlwaysStarting
datasources:
  test-db:
    driver-class-name: org.postgresql.Driver
    dialect: POSTGRES
netty:
  default:
    allocator:
      max-order: 3
redis:
  caches:
    test:
      quote-expiration-mills: 300000 # 5 min
      valueSerializer: io.micronaut.jackson.serialize.JacksonObjectSerializer
kafka:
  consumers:
    test:
      topic: "test"

Postgres, kafka, and redis containers startup

Remote environment

MICRONAUT_ENVIRONMENTS=remote ./gradlew run

note: I also moved the postgres url/un/pwd into the application.yml file directly and postgres still started

application-remote.yml

datasources:
  test-db:
    url: jdbc:postgresql://<test-path>/test?currentSchema=test
    username: test
    password: test
redis:
  uris: "redis://<path>"
kafka:
  bootstrap:
    servers: "<servers>"

No containers startup

Actual Behavior

Just to point out, in my other service redis and kafka start up containers with and without url/boostraps specified. So I'm wondering if there is some weird case that applies to any containers/combo of containers

Default

> Task :run
 __  __ _                                  _
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.8.6)

12:10:22.104 [default-nioEventLoopGroup-1-2] INFO  i.m.t.e.TestResourcesResolverLoader - Loaded 4 test resources resolvers: io.micronaut.testresources.postgres.PostgreSQLTestResourceProvider, io.micronaut.testresources.kafka.KafkaTestResourceProvider, io.micronaut.testresources.redis.RedisTestResourceProvider, io.micronaut.testresources.testcontainers.GenericTestContainerProvider
12:10:22.669 [default-nioEventLoopGroup-1-5] INFO  o.t.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
12:10:22.672 [default-nioEventLoopGroup-1-5] INFO  i.m.t.testcontainers.TestContainers - Starting test container postgres
12:10:22.693 [default-nioEventLoopGroup-1-5] INFO  o.t.d.DockerClientProviderStrategy - Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
12:10:23.131 [default-nioEventLoopGroup-1-5] INFO  o.t.d.DockerClientProviderStrategy - Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
12:10:23.133 [default-nioEventLoopGroup-1-5] INFO  o.testcontainers.DockerClientFactory - Docker host IP address is localhost
12:10:23.189 [default-nioEventLoopGroup-1-5] INFO  o.testcontainers.DockerClientFactory - Connected to docker:
  Server Version: 20.10.21
  API Version: 1.41
  Operating System: Docker Desktop
  Total Memory: 9951 MB
12:10:23.196 [default-nioEventLoopGroup-1-5] INFO  o.testcontainers.DockerClientFactory - Checking the system...
12:10:23.197 [default-nioEventLoopGroup-1-5] INFO  o.testcontainers.DockerClientFactory - βœ”οΈŽ Docker server version should be at least 1.6.0
12:10:23.292 [default-nioEventLoopGroup-1-5] INFO  🐳 [postgres:latest] - Creating container for image: postgres:latest
12:10:23.538 [default-nioEventLoopGroup-1-5] INFO  🐳 [testcontainers/ryuk:0.3.4] - Creating container for image: testcontainers/ryuk:0.3.4
12:10:23.655 [default-nioEventLoopGroup-1-5] INFO  🐳 [testcontainers/ryuk:0.3.4] - Container testcontainers/ryuk:0.3.4 is starting: 53a0085ccbcf8c0d2e111e9569f7e456f137a26d468b5f46154e1840f21de411
12:10:23.967 [default-nioEventLoopGroup-1-5] INFO  🐳 [testcontainers/ryuk:0.3.4] - Container testcontainers/ryuk:0.3.4 started in PT0.430348S
12:10:24.002 [default-nioEventLoopGroup-1-5] INFO  🐳 [postgres:latest] - Container postgres:latest is starting: b854dcad2824b7c59db9c251f11589bf52bda502e83d9aac07ec94a2fd0d1536
12:10:25.021 [default-nioEventLoopGroup-1-5] INFO  🐳 [postgres:latest] - Container postgres:latest started in PT1.822743S

> Task :run
12:10:25.105 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
12:10:25.237 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.

12:10:25.332 [default-nioEventLoopGroup-1-13] INFO  i.m.t.testcontainers.TestContainers - Starting test container kafka
12:10:25.333 [default-nioEventLoopGroup-1-13] INFO  🐳 [confluentinc/cp-kafka:7.0.4] - Creating container for image: confluentinc/cp-kafka:7.0.4
12:10:25.404 [default-nioEventLoopGroup-1-13] INFO  🐳 [confluentinc/cp-kafka:7.0.4] - Container confluentinc/cp-kafka:7.0.4 is starting: 1acf3d51090047f5030c5663f080d92f0d532c7ea844ce767224b00d5f6b3f6d
12:10:28.337 [default-nioEventLoopGroup-1-13] INFO  🐳 [confluentinc/cp-kafka:7.0.4] - Container confluentinc/cp-kafka:7.0.4 started in PT3.004342S

No redis

docker ps
CONTAINER ID   IMAGE                         COMMAND                   CREATED          STATUS          PORTS                                                        NAMES
adb5b926d80b   confluentinc/cp-kafka:7.0.4   "sh -c '#!/bin/bash\n…"   14 seconds ago   Up 14 seconds   9092/tcp, 0.0.0.0:50149->2181/tcp, 0.0.0.0:50150->9093/tcp   romantic_darwin
607619c4467a   postgres:latest               "docker-entrypoint.s…"    16 seconds ago   Up 15 seconds   0.0.0.0:50128->5432/tcp                                      brave_hopper
36a4d81991d1   testcontainers/ryuk:0.3.4     "/app"                    16 seconds ago   Up 16 seconds   0.0.0.0:50124->8080/tcp                                      testcontainers-ryuk-43d6f303-e892-4d64-b87c-a46416f05512

Remote environment

> Task :run
 __  __ _                                  _
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.8.6)

12:14:54.050 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [remote]

12:14:55.190 [default-nioEventLoopGroup-1-2] INFO  i.m.t.e.TestResourcesResolverLoader - Loaded 4 test resources resolvers: io.micronaut.testresources.postgres.PostgreSQLTestResourceProvider, io.micronaut.testresources.kafka.KafkaTestResourceProvider, io.micronaut.testresources.redis.RedisTestResourceProvider, io.micronaut.testresources.testcontainers.GenericTestContainerProvider
12:14:55.764 [default-nioEventLoopGroup-1-7] INFO  o.t.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
12:14:55.767 [default-nioEventLoopGroup-1-7] INFO  i.m.t.testcontainers.TestContainers - Starting test container postgres
12:14:55.788 [default-nioEventLoopGroup-1-7] INFO  o.t.d.DockerClientProviderStrategy - Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
12:14:56.390 [default-nioEventLoopGroup-1-7] INFO  o.t.d.DockerClientProviderStrategy - Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
12:14:56.392 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - Docker host IP address is localhost
12:14:56.444 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - Connected to docker:
  Server Version: 20.10.21
  API Version: 1.41
  Operating System: Docker Desktop
  Total Memory: 9951 MB
12:14:56.451 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - Checking the system...
12:14:56.451 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - βœ”οΈŽ Docker server version should be at least 1.6.0
12:14:56.575 [default-nioEventLoopGroup-1-7] INFO  🐳 [postgres:latest] - Creating container for image: postgres:latest
12:14:56.824 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Creating container for image: testcontainers/ryuk:0.3.4
12:14:56.941 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Container testcontainers/ryuk:0.3.4 is starting: 07ddfdd9603f914fb73f606d3ff0e0a88368a5de6d2644b2db738e782b857ff4
12:14:57.256 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Container testcontainers/ryuk:0.3.4 started in PT0.432395S
12:14:57.289 [default-nioEventLoopGroup-1-7] INFO  🐳 [postgres:latest] - Container postgres:latest is starting: e8002820750be5059d4dec37334d37496941fe19cd0e56911e01f85b6e2c89ac
12:14:58.318 [default-nioEventLoopGroup-1-7] INFO  🐳 [postgres:latest] - Container postgres:latest started in PT1.8656
$ docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED         STATUS         PORTS                     NAMES
be3d4c8d1f0e   postgres:latest             "docker-entrypoint.s…"   6 seconds ago   Up 5 seconds   0.0.0.0:50509->5432/tcp   angry_snyder
9c64fab588c3   testcontainers/ryuk:0.3.4   "/app"                   6 seconds ago   Up 5 seconds   0.0.0.0:50507->8080/tcp   testcontainers-ryuk-b1f0e291-e3a6-4244-940f-53e5d8a85d3b

However there is a postgres error trying to use the placeholder text in application-remote.yml which indicates it is not actually using the container that was started.

Message: Failed to initialize pool: The connection attempt failed.
Path Taken: DataSource.dataSource(DatasourceConfiguration datasourceConfiguration)
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type  [javax.sql.DataSource]

Message: Failed to initialize pool: The connection attempt failed.
Path Taken: DataSource.dataSource(DatasourceConfiguration datasourceConfiguration)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1921)
        at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:249)
        at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3326)
        at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3684)
        at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:341)
        at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:194)
        at io.micronaut.runtime.Micronaut.start(Micronaut.java:75)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:323)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:298)
        at com.example.ApplicationKt.main(Application.kt:5)
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [javax.sql.DataSource]

Steps To Reproduce

Run commands above in the attached repo.

Environment Information

gradle=7.5.1
micronautVersion=3.8.6
kotlinVersion=1.6.10

$ java --version
openjdk 11.0.14 2022-01-18
OpenJDK Runtime Environment Temurin-11.0.14+9 (build 11.0.14+9)
OpenJDK 64-Bit Server VM Temurin-11.0.14+9 (build 11.0.14+9, mixed mode)

Example Application

https://github.com/scprek/test-resources-always-starting

Version

3.8.6

melix commented 1 year ago

Something is strange: I tried your project and here it does not behave the same:

$ MICRONAUT_ENVIRONMENTS=remote ./gradlew run --scan

> Task :kaptKotlin
Note: Creating bean classes for 2 type elements

> Task :run
 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.8.6)

09:13:10.116 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [remote]
09:13:10.735 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
09:13:11.755 [main] ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: The connection attempt failed.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:331)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:247)
        at org.postgresql.Driver.makeConnection(Driver.java:434)
        at org.postgresql.Driver.connect(Driver.java:291)
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364)
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
        at io.micronaut.configuration.jdbc.hikari.HikariUrlDataSource.<init>(HikariUrlDataSource.java:35)
        at io.micronaut.configuration.jdbc.hikari.DatasourceFactory.dataSource(DatasourceFactory.java:66)
        at io.micronaut.configuration.jdbc.hikari.$DatasourceFactory$DataSource0$Definition.build(Unknown Source)
        at io.micronaut.context.BeanDefinitionDelegate.build(BeanDefinitionDelegate.java:161)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:2737)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1915)
        at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:249)
        at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3326)
        at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3684)
        at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:341)
        at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:194)
        at io.micronaut.runtime.Micronaut.start(Micronaut.java:75)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:323)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:298)
        at com.example.ApplicationKt.main(Application.kt:5)
Caused by: java.net.UnknownHostException: <test-path>
        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
        at java.base/java.net.Socket.connect(Socket.java:633)
        at org.postgresql.core.PGStream.createSocket(PGStream.java:241)
        at org.postgresql.core.PGStream.<init>(PGStream.java:98)
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
        ... 32 common frames omitted
09:13:11.757 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type  [javax.sql.DataSource]

Message: Failed to initialize pool: The connection attempt failed.
Path Taken: DataSource.dataSource(DatasourceConfiguration datasourceConfiguration)
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type  [javax.sql.DataSource]

Message: Failed to initialize pool: The connection attempt failed.
Path Taken: DataSource.dataSource(DatasourceConfiguration datasourceConfiguration)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1921)
        at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:249)
        at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3326)
        at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3684)
        at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:341)
        at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:194)
        at io.micronaut.runtime.Micronaut.start(Micronaut.java:75)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:323)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:298)
        at com.example.ApplicationKt.main(Application.kt:5)
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [javax.sql.DataSource]

Message: Failed to initialize pool: The connection attempt failed.
Path Taken: DataSource.dataSource(DatasourceConfiguration datasourceConfiguration)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2367)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:2737)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1915)
        ... 9 common frames omitted
Caused by: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: The connection attempt failed.
        at com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:596)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:582)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
        at io.micronaut.configuration.jdbc.hikari.HikariUrlDataSource.<init>(HikariUrlDataSource.java:35)
        at io.micronaut.configuration.jdbc.hikari.DatasourceFactory.dataSource(DatasourceFactory.java:66)
        at io.micronaut.configuration.jdbc.hikari.$DatasourceFactory$DataSource0$Definition.build(Unknown Source)
        at io.micronaut.context.BeanDefinitionDelegate.build(BeanDefinitionDelegate.java:161)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        ... 16 common frames omitted
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:331)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:247)
        at org.postgresql.Driver.makeConnection(Driver.java:434)
        at org.postgresql.Driver.connect(Driver.java:291)
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364)
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
        ... 23 common frames omitted
Caused by: java.net.UnknownHostException: <test-path>
        at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:567)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
        at java.base/java.net.Socket.connect(Socket.java:633)
        at org.postgresql.core.PGStream.createSocket(PGStream.java:241)
        at org.postgresql.core.PGStream.<init>(PGStream.java:98)
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
        ... 32 common frames omitted

https://scans.gradle.com/s/z7n7vpwrvrhts

I am confused :/

scprek commented 1 year ago

Yeah so am !!! I also restarted Docker Desktop on Mac just to try and same result

Did you also see redis startup when just doing ./gradlew run, it still doesn't for me.

That's why I asked in chat about the proper cleanup to switch environments. If I check that docker ps is cleaned up, run ./gradlew clean stopTestResourcesService just in case, then MICRONAUT_ENVIRONMENTS=remote ./gradlew run I get the following still.

https://scans.gradle.com/s/yyp2depeyd6he

Operating system | macOS 12.6 (x86_64) 
Java runtime | Eclipse Temurin OpenJDK Runtime Environment 11.0.14+9
Java VM | Eclipse Temurin OpenJDK 64-Bit Server VM 11.0.14+9 (mixed mode)
$ ./gradlew clean stopTestResourcesService

> Task :stopTestResourcesService
Stopping test resources service

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
$ MICRONAUT_ENVIRONMENTS=remote ./gradlew run --scan

06:58:18.945 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [remote]
06:58:18.971 [ForkJoinPool.commonPool-worker-19] INFO  i.m.t.e.TestResourcesResolverLoader - Loaded 4 test resources resolvers: io.micronaut.testresources.postgres.PostgreSQLTestResourceProvider, io.micronaut.testresources.kafka.KafkaTestResourceProvider, io.micronaut.testresources.redis.RedisTestResourceProvider, io.micronaut.testresources.testcontainers.GenericTestContainerProvider
06:58:19.419 [main] INFO  i.m.testresources.server.Application - A Micronaut Test Resources server is listening on port 62542, started in 1125ms

> Task :internalStartTestResourcesService
Test resources server started in standalone mode. You can stop it by running the stopTestResourcesService task.

> Task :kaptKotlin
Note: Creating bean classes for 2 type elements

> Task :run
 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.8.6)

06:58:22.811 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [remote]

06:58:23.896 [default-nioEventLoopGroup-1-2] INFO  i.m.t.e.TestResourcesResolverLoader - Loaded 4 test resources resolvers: io.micronaut.testresources.postgres.PostgreSQLTestResourceProvider, io.micronaut.testresources.kafka.KafkaTestResourceProvider, io.micronaut.testresources.redis.RedisTestResourceProvider, io.micronaut.testresources.testcontainers.GenericTestContainerProvider
06:58:24.403 [default-nioEventLoopGroup-1-7] INFO  o.t.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
06:58:24.406 [default-nioEventLoopGroup-1-7] INFO  i.m.t.testcontainers.TestContainers - Starting test container postgres
06:58:24.424 [default-nioEventLoopGroup-1-7] INFO  o.t.d.DockerClientProviderStrategy - Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
06:58:25.024 [default-nioEventLoopGroup-1-7] INFO  o.t.d.DockerClientProviderStrategy - Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
06:58:25.025 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - Docker host IP address is localhost
06:58:25.062 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - Connected to docker: 
  Server Version: 20.10.21
  API Version: 1.41
  Operating System: Docker Desktop
  Total Memory: 9951 MB
06:58:25.069 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - Checking the system...
06:58:25.070 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - βœ”οΈŽ Docker server version should be at least 1.6.0
06:58:25.187 [default-nioEventLoopGroup-1-7] INFO  🐳 [postgres:latest] - Creating container for image: postgres:latest
06:58:25.421 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Creating container for image: testcontainers/ryuk:0.3.4
06:58:25.535 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Container testcontainers/ryuk:0.3.4 is starting: cf3bb72c9df6e7e18caae62f7eea0bb05348b4e316d6ef5b89481539800980c1
06:58:25.841 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Container testcontainers/ryuk:0.3.4 started in PT0.420722S
06:58:25.874 [default-nioEventLoopGroup-1-7] INFO  🐳 [postgres:latest] - Container postgres:latest is starting: 9f1538d4f838eda734a0c6ff48910df8a222d8092f273ea7ac356650088e27ba
06:58:26.849 [default-nioEventLoopGroup-1-7] INFO  🐳 [postgres:latest] - Container postgres:latest started in PT1.777908S

> Task :run
06:58:26.868 [main] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
06:58:27.924 [main] ERROR com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: The connection attempt failed.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:331)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:247)
        at org.postgresql.Driver.makeConnection(Driver.java:434)
        at org.postgresql.Driver.connect(Driver.java:291)
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364)
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
        at io.micronaut.configuration.jdbc.hikari.HikariUrlDataSource.<init>(HikariUrlDataSource.java:35)
        at io.micronaut.configuration.jdbc.hikari.DatasourceFactory.dataSource(DatasourceFactory.java:66)
        at io.micronaut.configuration.jdbc.hikari.$DatasourceFactory$DataSource0$Definition.build(Unknown Source)
        at io.micronaut.context.BeanDefinitionDelegate.build(BeanDefinitionDelegate.java:161)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:2737)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1915)
        at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:249)
        at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3326)
        at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3684)
        at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:341)
        at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:194)
        at io.micronaut.runtime.Micronaut.start(Micronaut.java:75)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:323)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:298)
        at com.example.ApplicationKt.main(Application.kt:5)
Caused by: java.net.UnknownHostException: <test-path>
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:229)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.base/java.net.Socket.connect(Socket.java:609)
        at org.postgresql.core.PGStream.createSocket(PGStream.java:241)
        at org.postgresql.core.PGStream.<init>(PGStream.java:98)
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
        ... 32 common frames omitted
06:58:27.928 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type  [javax.sql.DataSource]

Message: Failed to initialize pool: The connection attempt failed.
Path Taken: DataSource.dataSource(DatasourceConfiguration datasourceConfiguration)
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type  [javax.sql.DataSource]

Message: Failed to initialize pool: The connection attempt failed.
Path Taken: DataSource.dataSource(DatasourceConfiguration datasourceConfiguration)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1921)
        at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:249)
        at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3326)
        at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3684)
        at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:341)
        at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:194)
        at io.micronaut.runtime.Micronaut.start(Micronaut.java:75)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:323)
        at io.micronaut.runtime.Micronaut.run(Micronaut.java:298)
        at com.example.ApplicationKt.main(Application.kt:5)
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [javax.sql.DataSource]

Message: Failed to initialize pool: The connection attempt failed.
Path Taken: DataSource.dataSource(DatasourceConfiguration datasourceConfiguration)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2367)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:2737)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1915)
        ... 9 common frames omitted
Caused by: com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: The connection attempt failed.
        at com.zaxxer.hikari.pool.HikariPool.throwPoolInitializationException(HikariPool.java:596)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:582)
        at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
        at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81)
        at io.micronaut.configuration.jdbc.hikari.HikariUrlDataSource.<init>(HikariUrlDataSource.java:35)
        at io.micronaut.configuration.jdbc.hikari.DatasourceFactory.dataSource(DatasourceFactory.java:66)
        at io.micronaut.configuration.jdbc.hikari.$DatasourceFactory$DataSource0$Definition.build(Unknown Source)
        at io.micronaut.context.BeanDefinitionDelegate.build(BeanDefinitionDelegate.java:161)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        ... 16 common frames omitted
Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:331)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
        at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:247)
        at org.postgresql.Driver.makeConnection(Driver.java:434)
        at org.postgresql.Driver.connect(Driver.java:291)
        at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138)
        at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:364)
        at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:206)
        at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:476)
        at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561)
        ... 23 common frames omitted
Caused by: java.net.UnknownHostException: <test-path>
        at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:229)
        at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.base/java.net.Socket.connect(Socket.java:609)
        at org.postgresql.core.PGStream.createSocket(PGStream.java:241)
        at org.postgresql.core.PGStream.<init>(PGStream.java:98)
        at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109)
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
        ... 32 common frames omitted

> Task :run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Users/stevecprek/.sdkman/candidates/java/11.0.14-tem/bin/java'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 13s
8 actionable tasks: 8 executed

Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes
Gradle Terms of Service accepted.

Publishing build scan...
https://gradle.com/s/yyp2depeyd6he
melix commented 1 year ago

So here's what I see (logs stripped for clarity, look at the build scans for full output)

$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
$ ./gradlew test --scan

> Task :internalStartTestResourcesService
Test resources server started in standalone mode. You can stop it by running the stopTestResourcesService task.
...
> Task :kaptTestKotlin
...
14:05:24.024 [default-nioEventLoopGroup-1-7] INFO  i.m.t.testcontainers.TestContainers - Starting test container postgres
14:05:25.195 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Creating container for image: testcontainers/ryuk:0.3.4
14:05:25.705 [default-nioEventLoopGroup-1-7] INFO  🐳 [postgres:latest] - Creating container for image: postgres:latest
14:05:26.941 [default-nioEventLoopGroup-1-15] INFO  🐳 [confluentinc/cp-kafka:7.0.4] - Creating container for image: confluentinc/cp-kafka:7.0.4

BUILD SUCCESSFUL in 17s

https://scans.gradle.com/s/x63t4dzvhisca

So the test build passes, let's see that all docker containers are still alive (because you are using sharedServer (why? This should only be used if you want to share the containers between distinct projects):

$ docker ps   
CONTAINER ID   IMAGE                         COMMAND                   CREATED         STATUS         PORTS                                                        NAMES
3dd1d81fff97   confluentinc/cp-kafka:7.0.4   "sh -c '#!/bin/bash\n…"   2 minutes ago   Up 2 minutes   9092/tcp, 0.0.0.0:33784->2181/tcp, 0.0.0.0:33783->9093/tcp   crazy_maxwell
729bdb77ccf2   postgres:latest               "docker-entrypoint.s…"    2 minutes ago   Up 2 minutes   0.0.0.0:33782->5432/tcp                                      stoic_haibt
14b667aea756   testcontainers/ryuk:0.3.4     "/app"                    2 minutes ago   Up 2 minutes   0.0.0.0:33781->8080/tcp                                      testcontainers-ryuk-4b64c960-14c6-4c7d-a3ce-09a667a184b8

So let's stop the test resources service:

$ ./gradlew stopTRS

> Task :stopTestResourcesService
Stopping test resources service

BUILD SUCCESSFUL in 632ms
$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

As expected, all containers are shutdown. Now let's run with the production environment:

$ MICRONAUT_ENVIRONMENTS=remote ./gradlew run --scan
14:12:12.358 [ForkJoinPool.commonPool-worker-8] INFO  i.m.t.e.TestResourcesResolverLoader - Loaded 4 test resources resolvers: io.micronaut.testresources.postgres.PostgreSQLTestResourceProvider, io.micronaut.testresources.kafka.KafkaTestResourceProvider, io.micronaut.testresources.redis.RedisTestResourceProvider, io.micronaut.testresources.testcontainers.GenericTestContainerProvider
...
14:12:13.751 [default-nioEventLoopGroup-1-7] INFO  i.m.t.testcontainers.TestContainers - Starting test container postgres

https://scans.gradle.com/s/m374zcgfjwdt4

So I see the same behavior as you do. That's good news, because initially I wasn't seeing a container starting in logs, which makes sense because I didn't have shutdown the test resources service (I didn't notice you used sharedServer=true).

I also checked with sharedServer=false and I'm seeing the same behavior (as expected), but now I can investigate.

scprek commented 1 year ago

Sounds good. So I didn't notice that micronaut launch created a gradle with sharedServer set to true. My other service def does not have it.

I ran with sharedServer=false and also hit just like you.

melix commented 1 year ago

Right, it's probably Micronaut Launch generating sharedServer=true because you are using kafka, and that it makes sense to share the test kafka server between projects. Anyway, unrelated to this issue.

melix commented 1 year ago

Ok I have found the problem, and it's a nasty one. You are missing one property datasources.test-db.driverClassName, and it's not your fault, it seems that launch.micronaut.io generates datasources.test-db.driver-class-name. Switch to driverClassName and it will behave as expected.

melix commented 1 year ago

Closing, now tracked at https://github.com/micronaut-projects/micronaut-starter/issues/1650

scprek commented 1 year ago

Ok, I thought maybe that db-type: postgres would help too, i specified that in the other services just in case dialect or the other methods were not working.

After fixing postgres config field, @melix I still don't see redis starting with "default" run ./gradlew run I've noticed a similar issue with my service but instead of redis, kafka doesn't start. I was thinking it might be the same problem.

Context above in the original submission "Default" sections, but as you can see

07:44:53.554 [default-nioEventLoopGroup-1-2] INFO  i.m.t.e.TestResourcesResolverLoader - Loaded 4 test resources resolvers: io.micronaut.testresources.postgres.PostgreSQLTestResourceProvider, io.micronaut.testresources.kafka.KafkaTestResourceProvider, io.micronaut.testresources.redis.RedisTestResourceProvider, io.micronaut.testresources.testcontainers.GenericTestContainerProvider
melix commented 1 year ago

Note that the test resources service will only start when the property is actually queried, so it can be because of laziness.

scprek commented 1 year ago

Ahhhhh amazing thanks. I forgot I had eager init in my other service! I tossed it in this demo project and see redis starting for local, however I see a similar issue with remote environment.

I believe the issue is micronaut test resources is only looking for redis.uri not redis.uris (cluster_mode). When I switch from uris to uri the redis container does not startup. But it also seems like micronaut is now expecting to see uri?

Screen Shot 2023-03-09 at 8 01 57 AM

Gradle Scan: https://gradle.com/s/uebxjaihye4nk

$ MICRONAUT_ENVIRONMENTS=remote ./gradlew run --scan
07:57:16.301 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [remote]
07:57:16.328 [ForkJoinPool.commonPool-worker-9] INFO  i.m.t.e.TestResourcesResolverLoader - Loaded 4 test resources resolvers: io.micronaut.testresources.postgres.PostgreSQLTestResourceProvider, io.micronaut.testresources.kafka.KafkaTestResourceProvider, io.micronaut.testresources.redis.RedisTestResourceProvider, io.micronaut.testresources.testcontainers.GenericTestContainerProvider
07:57:16.806 [main] INFO  i.m.testresources.server.Application - A Micronaut Test Resources server is listening on port 50798, started in 1254ms

> Task :kaptKotlin
Note: Creating bean classes for 2 type elements

> Task :run
 __  __ _                                  _   
|  \/  (_) ___ _ __ ___  _ __   __ _ _   _| |_ 
| |\/| | |/ __| '__/ _ \| '_ \ / _` | | | | __|
| |  | | | (__| | | (_) | | | | (_| | |_| | |_ 
|_|  |_|_|\___|_|  \___/|_| |_|\__,_|\__,_|\__|
  Micronaut (v3.8.6)

07:57:21.302 [main] INFO  i.m.context.env.DefaultEnvironment - Established active environments: [remote]

07:57:22.432 [default-nioEventLoopGroup-1-2] INFO  i.m.t.e.TestResourcesResolverLoader - Loaded 4 test resources resolvers: io.micronaut.testresources.postgres.PostgreSQLTestResourceProvider, io.micronaut.testresources.kafka.KafkaTestResourceProvider, io.micronaut.testresources.redis.RedisTestResourceProvider, io.micronaut.testresources.testcontainers.GenericTestContainerProvider
07:57:23.137 [default-nioEventLoopGroup-1-7] INFO  o.t.utility.ImageNameSubstitutor - Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
07:57:23.143 [default-nioEventLoopGroup-1-7] INFO  i.m.t.testcontainers.TestContainers - Starting test container redis
07:57:23.163 [default-nioEventLoopGroup-1-7] INFO  o.t.d.DockerClientProviderStrategy - Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
07:57:23.655 [default-nioEventLoopGroup-1-7] INFO  o.t.d.DockerClientProviderStrategy - Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
07:57:23.657 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - Docker host IP address is localhost
07:57:23.698 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - Connected to docker: 
  Server Version: 20.10.22
  API Version: 1.41
  Operating System: Docker Desktop
  Total Memory: 9951 MB
07:57:23.703 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - Checking the system...
07:57:23.703 [default-nioEventLoopGroup-1-7] INFO  o.testcontainers.DockerClientFactory - βœ”οΈŽ Docker server version should be at least 1.6.0
07:57:23.804 [default-nioEventLoopGroup-1-7] INFO  🐳 [redis:latest] - Creating container for image: redis:latest
07:57:24.071 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Creating container for image: testcontainers/ryuk:0.3.4
07:57:24.201 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Container testcontainers/ryuk:0.3.4 is starting: ae97d12e907446f459e04b7ac81d11bc1a5abe355388f012a99fa58cb853f66f
07:57:24.515 [default-nioEventLoopGroup-1-7] INFO  🐳 [testcontainers/ryuk:0.3.4] - Container testcontainers/ryuk:0.3.4 started in PT0.445604S
07:57:24.547 [default-nioEventLoopGroup-1-7] INFO  🐳 [redis:latest] - Container redis:latest is starting: 3736c4886b61f53e3ac4210efc8873dddb5bb6416676b2abcb2cd14ec302af31
07:57:24.806 [default-nioEventLoopGroup-1-7] INFO  🐳 [redis:latest] - Container redis:latest started in PT1.102013S

> Task :run
07:57:24.820 [main] ERROR io.micronaut.runtime.Micronaut - Error starting Micronaut server: Bean definition [com.example.$RedisExample$Definition$Intercepted] could not be loaded: Error instantiating bean of type  [io.lettuce.core.cluster.RedisClusterClient]

Message: URI must not be null
Path Taken: RedisClusterClient.redisClient(AbstractRedisConfiguration config,ClientResources defaultClientResources) --> RedisClusterClient.redisClient([AbstractRedisConfiguration config],ClientResources defaultClientResources)
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [com.example.$RedisExample$Definition$Intercepted] could not be loaded: Error instantiating bean of type  [io.lettuce.core.cluster.RedisClusterClient]

Message: URI must not be null
Path Taken: RedisClusterClient.redisClient(AbstractRedisConfiguration config,ClientResources defaultClientResources) --> RedisClusterClient.redisClient([AbstractRedisConfiguration config],ClientResources defaultClientResources)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1921)
        at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:249)
        at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3326)
        at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3684)
        at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:341)
        at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:194)
        at io.micronaut.runtime.Micronaut.start(Micronaut.java:75)
        at com.example.Application.main(Application.kt:12)
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Error instantiating bean of type  [io.lettuce.core.cluster.RedisClusterClient]

Message: URI must not be null
Path Taken: RedisClusterClient.redisClient(AbstractRedisConfiguration config,ClientResources defaultClientResources) --> RedisClusterClient.redisClient([AbstractRedisConfiguration config],ClientResources defaultClientResources)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2367)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2879)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2800)
        at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1617)
        at io.micronaut.context.AbstractBeanResolutionContext.getBean(AbstractBeanResolutionContext.java:66)
        at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBean(AbstractInitializableBeanDefinition.java:2065)
        at io.micronaut.context.AbstractInitializableBeanDefinition.getBeanForConstructorArgument(AbstractInitializableBeanDefinition.java:1297)
        at io.micronaut.configuration.lettuce.$DefaultRedisClusterClientFactory$RedisClient0$Definition.build(Unknown Source)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2879)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2800)
        at io.micronaut.context.DefaultBeanContext.findBean(DefaultBeanContext.java:1680)
        at io.micronaut.context.DefaultBeanContext.findBean(DefaultBeanContext.java:1655)
        at io.micronaut.context.DefaultBeanContext.findBean(DefaultBeanContext.java:878)
        at io.micronaut.context.BeanLocator.findBean(BeanLocator.java:291)
        at io.micronaut.configuration.lettuce.RedisConnectionUtil.findRedisClusterClient(RedisConnectionUtil.java:125)
        at io.micronaut.configuration.lettuce.RedisConnectionUtil.openBytesRedisConnection(RedisConnectionUtil.java:109)
        at io.micronaut.configuration.lettuce.cache.RedisCache.<init>(RedisCache.java:81)
        at io.micronaut.configuration.lettuce.cache.$RedisCache$Definition.build(Unknown Source)
        at io.micronaut.context.BeanDefinitionDelegate.build(BeanDefinitionDelegate.java:161)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2879)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2853)
        at io.micronaut.context.DefaultBeanContext.addCandidateToList(DefaultBeanContext.java:3511)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistrations(DefaultBeanContext.java:3457)
        at io.micronaut.context.DefaultBeanContext.getBeanRegistrations(DefaultBeanContext.java:3427)
        at io.micronaut.context.DefaultBeanContext.getBeansOfType(DefaultBeanContext.java:1381)
        at io.micronaut.context.AbstractBeanResolutionContext.getBeansOfType(AbstractBeanResolutionContext.java:72)
        at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBeansOfType(AbstractInitializableBeanDefinition.java:2161)
        at io.micronaut.context.AbstractInitializableBeanDefinition.getBeansOfTypeForConstructorArgument(AbstractInitializableBeanDefinition.java:1437)
        at io.micronaut.cache.$DefaultCacheManager$Definition.build(Unknown Source)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2879)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2800)
        at io.micronaut.context.DefaultBeanContext.getBean(DefaultBeanContext.java:1617)
        at io.micronaut.context.AbstractBeanResolutionContext.getBean(AbstractBeanResolutionContext.java:66)
        at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBean(AbstractInitializableBeanDefinition.java:2065)
        at io.micronaut.context.AbstractInitializableBeanDefinition.getBeanForConstructorArgument(AbstractInitializableBeanDefinition.java:1297)
        at io.micronaut.cache.interceptor.$CacheInterceptor$Definition.build(Unknown Source)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2879)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistration(DefaultBeanContext.java:2853)
        at io.micronaut.context.DefaultBeanContext.addCandidateToList(DefaultBeanContext.java:3511)
        at io.micronaut.context.DefaultBeanContext.resolveBeanRegistrations(DefaultBeanContext.java:3457)
        at io.micronaut.context.DefaultBeanContext.getBeanRegistrations(DefaultBeanContext.java:3427)
        at io.micronaut.context.AbstractBeanResolutionContext.getBeanRegistrations(AbstractBeanResolutionContext.java:96)
        at io.micronaut.context.AbstractInitializableBeanDefinition.resolveBeanRegistrations(AbstractInitializableBeanDefinition.java:2190)
        at io.micronaut.context.AbstractInitializableBeanDefinition.getBeanRegistrationsForConstructorArgument(AbstractInitializableBeanDefinition.java:1461)
        at com.example.$RedisExample$Definition$Intercepted$Definition.build(Unknown Source)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
        at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
        at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
        at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
        at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
        at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:2746)
        at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1915)
        ... 7 common frames omitted
Caused by: java.lang.IllegalArgumentException: URI must not be null
        at io.lettuce.core.internal.LettuceAssert.notNull(LettuceAssert.java:71)
        at io.lettuce.core.RedisURI.buildRedisUriFromUri(RedisURI.java:751)
        at io.lettuce.core.RedisURI.create(RedisURI.java:308)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
        at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
        at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
        at io.micronaut.configuration.lettuce.AbstractRedisConfiguration.setUris(AbstractRedisConfiguration.java:80)
        at io.micronaut.configuration.lettuce.$DefaultRedisConfiguration$Definition.injectBean(Unknown Source)
        at io.micronaut.configuration.lettuce.$DefaultRedisConfiguration$Definition.build(Unknown Source)
        at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
        ... 88 common frames omitted

> Task :run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Users/stevecprek/.sdkman/candidates/java/11.0.14-tem/bin/java'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 20s
8 actionable tasks: 7 executed, 1 up-to-date

Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes
Gradle Terms of Service accepted.

Publishing build scan...
https://gradle.com/s/uebxjaihye4nk
scprek commented 1 year ago

Maybe it’s because both uri and uris is specified due to test resources

melix commented 1 year ago

Re-opening because investigation showed that the postgres issue may be fixable in test resources (see #183 ).

We may need a separate issue for the redis case, because I didn't investigate that one yet.

melix commented 1 year ago

@scprek for redis I can confirm this is because of laziness. If I use this for your main app, then you can see it starting:

import io.micronaut.runtime.Micronaut.build
fun main(args: Array<String>) {
    build()
        .eagerInitSingletons(true)
        .args(*args)
        .start()
}
scprek commented 1 year ago

Yep understood. I got that working with the same but test resources doesn't look for redis.uris is my issue.

Guessing it should check both uri and uris

melix commented 1 year ago

Feel free to add a PR to add support for redis.uris too (and maybe a PR ;))

scprek commented 1 year ago

Opened the redis ticket so will close the bug