Open avaz opened 1 month ago
Here is a reproducible test case for this issue: https://github.com/chrismaeda/testcontainers-python/commit/745231ab17efa03aa249e8271c54ac0730c80b84
As the OP said, the wait_for_logs regex in MySqlContainer._connect does not work for custom mysql images. Since the regex is a string constant, the only workaround that I could figure out is to replace the _connect method on the MySqlContainer instance using a different regex. The pytest method shows that this approach works.
I'm not sure how you want to fix this. The ideal solution seems to be to implement the wait strategies framework in the Java version (https://java.testcontainers.org/features/startup_and_waits/). The least effort solution would be to allow the wait_for_logs regex to be changed at runtime.
Submitted test case as PR https://github.com/testcontainers/testcontainers-python/pull/729
On a high level, this is a discussion on switching from log waiting to which waiting mechanism? I haven't read the entry point for mysql image yet. I assume the goal is to only support custom image based on the library image with the same entry point script
Out of curiosity, why wouldn't the solution that I proposed on my original post be adequate to deal with this issue? I followed the same idea implemented for Postgres as per this commit.
Yes sorry let me implement it for mysql as well, I want to confirm some things before agreeing it is the correct solution first
Describe the bug
The current implementation for
MySqlContainer
to check if the database is running is like below:It expects for two pair of "ready for connections" logs to appear in the logs output. This works for vanilla MySQL Docker image but fails for any custom ones, for instance, an image that has a custom database initilization which is something standard and explained how in the official Docker Hub for MySQL section 'Initializing a fresh instance'.
Below is a example of a custom MySQL docker image:
And the initialization logs output for this image is like below:
Logs from vanilla
mysql:latest
image:In the vanilla there are twice the pair "ready for connections" which makes the regular expression match, but it fails for the custom one. Of course, another approach would be to create a custom testcontainers docker image class but actually this isn't a good idea because the logs for MySql can change anytime and as soon as the official image gets updated testcontainers MySql image class may fail to properly check if the container has started or not.
To Reproduce
Create a docker image like the Dockerfile above and use it in test using testcontainers like below
The test will timeout on waiting for the container to start even that it has already started. I monkey patched the implementation on my local project like below and worked well. The approach below is more reliable as it relies on standard Mysql tooling to check if the service is running or not, also it make use of testcontainers builtin constructs where both tends to be more future-proof.
File
modules/mysql/testcontainers/mysql/__init__.py
I have it running and tested on my project and I would be happy to submit a PR.
Runtime environment
Provide a summary of your runtime environment. Which operating system, python version, and docker version are you using? What is the version of
testcontainers-python
you are using? You can run the following commands to get the relevant information.