Replies: 1 comment
-
Answering to myself about the second issue I mentioned. I found out that we can put healthcheck on services of our docker compose configuration, for example: mssql:
image: mcr.microsoft.com/mssql/server:2022-latest
environment:
SA_PASSWORD: "YourStrong@Passw0rd"
ACCEPT_EULA: "Y"
healthcheck:
test: "/opt/mssql-tools/bin/sqlcmd -U 'SA' -P 'YourStrong@Passw0rd' -Q 'SELECT 1'"
interval: 10s
timeout: 5s
retries: 5
ports:
- "1433:1433"
volumes:
# Mount local volumes to keep state of the database
- mssql_data:/var/opt/mssql Doing so, my pytest fixture actually yield only once the container is ready, i.e: when the service is healthy.
From now I'm wondering if this method will be enough to cover cases where we need to check the logs to know if a service is healthy, does this can be actually achieved using the docker compose I'm also not a big fan of having to replace my connection URL Maybe it could be interesting to add some guides/recipes to the documentation of the library to cover such cases? It might be easier for developers instead of having to figure out the technical API. |
Beta Was this translation helpful? Give feedback.
-
To test my code against a MS SQL database I spin up a test container using
testcontainers
andpytest
Using this fixture in my test allow me to spin my database for tests duration and the get the connection URL.
Recently I wanted to switch to docker compose instead so I could define my services in a single place and use that same configuration for both automated tests and running my app in dev mode locally. I got my configuration in a
docker-compose.yml
in my project root. My first try was the following:At this point I got two issues:
Get the connection URL
For this one, my solution is to simply read the connection url I specified directly in my docker compose file for my app. So I add another fixture for getting my docker compose configuration.
Here, no magic anymore but this seems all good to me.
Loosing specific container classes behaviors
The second issue is that my solution is actually missing the part which wait for the database to be ready. When using
SqlServerContainer
class, it embeds a_connect()
function which is called onstart()
and which wait for the database to be actually ready by trying several times to run a SQL query.If I run my tests without this wait, it obviously fail at connecting to the DB.
So I tried a small hack by calling manually the
_connect()
function passing the compose container asself
.Unfortunately this does not work because
ComposeContainer
andDockerContainer
have not the same interface.Does someone already encountered this use case? Does one already find a workaround or a pattern? Is there some improvements we could bring into the library?
Beta Was this translation helpful? Give feedback.
All reactions