testcontainers / testcontainers-hs

Docker containers for your integration tests! http://hackage.haskell.org/package/testcontainers
MIT License
58 stars 13 forks source link

Examples with popular database images #52

Closed sigrdrifa closed 5 months ago

sigrdrifa commented 5 months ago

Hey,

just came across this library and it's amazing, I had no idea this existed for Haskell!

I've been looking through the examples and I can see some examples with running nginx, redis and rabbitMQ but I was wondering if there are any examples for using this with popular databases like MySQL and/or PostgresDB? I know that testcontainers have official modules for those and I was wondering if testcontainers-hs supported them or if there was a way to invoke them?

Thanks!

rvem commented 5 months ago

AFAICS, there are no existing helpers. Here is an example for Postgres

setupDBContainer :: MonadDocker m => m (Text, Int)
setupDBContainer = do
  let
    pgImageTag = 14
    pgUser = "postgres"
    pgPassword = "postgres"
    pgDbName = "postgres"
  pgContainer <- run $ containerRequest (fromTag [int||postgres:#{pgImageTag}|])
    & setEnv [("POSTGRES_PASSWORD", pgPassword)]
    & setSuffixedName "db"
    & setExpose [5432]
    & setWaitingFor (waitForLogLine Stderr ("database system is ready to accept connections" `LT.isInfixOf`))
  pure $ containerAddress pgContainer 5432

And then use it in your test-suite

withContainers setupDBContainer $ \(pgHost, pgPort) -> testCase "DB test" $ do
  ...
sigrdrifa commented 5 months ago

Thanks so much for the example!

I was specifically interested in Postgres so thank you for the example, and I might see if I can write a postgres helper and do a PR :)