I’m struggling to setup a postgresql test-containe...
# kotest
k
I’m struggling to setup a postgresql test-container on MacOS m1. I’m doing the following:
Copy code
class PostgreSQL : PostgreSQLContainer<PostgreSQL>("postgres:latest") {
  init {
    println("waiting for postgres")
    waitingFor(Wait.forListeningPorts())
    println("finished waiting for postgres: running=${this.isRunning}")
  }
}
It passes the waitingFor but the container returns false for isRunning. Am I missing something?
m
This is how i setup with kotest
Copy code
object PostgresListener : TestListener {
    private val container =
        GenericContainer("postgres:16-alpine")
            .withExposedPorts(5432)
            .withEnv("POSTGRES_DB", PropertiesConfig.PostgresProperties().databaseName)
            .withEnv("POSTGRES_USER", PropertiesConfig.PostgresProperties().adminUser)
            .withEnv("POSTGRES_PASSWORD", "postgres")
            .withCreateContainerCmdModifier { cmd ->
                cmd.hostConfig!!.withPortBindings(PortBinding(Ports.Binding.bindPort(5432), ExposedPort(5432)))
            }
            .waitingFor(Wait.forLogMessage(".*ready to accept connections*\\n", 1))

    val dataSource: HikariDataSource by lazy {
        DatabaseConfig.postgresDataSource(DatabaseTestConfig.hikariPostgresConfig(container.host))
    }

    override suspend fun beforeTest(testCase: TestCase) {
        container.start()
        DatabaseConfig.postgresMigrate(dataSource)
    }

    override suspend fun afterTest(
        testCase: TestCase,
        result: TestResult,
    ) {
        container.stop()
    }
}
this is important:
Copy code
.waitingFor(Wait.forLogMessage(".*ready to accept connections*\\n", 1))
because otherwise it wont know when the container is up and running
k
Thanks. That along with the cmd modifier for port mapping did the trick!
🎉 1
m
Its a great approach to create a listener, then you only spin it at once during the test cases 🙂
m
Have you already seen the test containers extension? https://kotest.io/docs/extensions/test_containers.html It provides some wiring so you don't have to implement the listener by yourself.
👍 1