Commit 2d9eee3
authored
feat: Add ExecWaitStrategy and migrate Postgres from deprecated decorator (#935)
## Problem
The `PostgresContainer` currently uses the deprecated
`@wait_container_is_ready()` decorator, which is slated for removal. The
container was previously migrated away from log-based waiting due to
locale-dependent issues (#703, #695), but still relies on the deprecated
decorator pattern.
## Solution
This PR introduces a new `ExecWaitStrategy` wait strategy and migrates
`PostgresContainer` to use it.
### Changes
1. **New `ExecWaitStrategy` in
`core/testcontainers/core/wait_strategies.py`**
- Executes commands inside containers and waits for expected exit codes
- Follows the modern structured wait strategy pattern
- Reusable for any `DockerContainer` with CLI-based health checks
- Composable with other wait strategies via `CompositeWaitStrategy`
- Includes runtime check to ensure container supports `exec()`
2. **Updated `modules/postgres/testcontainers/postgres/__init__.py`**
- Migrated `_connect()` method from `@wait_container_is_ready` decorator
to `ExecWaitStrategy`
- Uses `psql` command execution to verify database readiness
- Maintains existing behavior and password escaping logic
### Design Consideration: Protocol Limitations
**Issue**: The `WaitStrategyTarget` protocol is designed to support both
`DockerContainer` and `ComposeContainer`, but `ExecWaitStrategy` only
works with `DockerContainer` (which has an `exec()`
method).
**Current Solution**: Runtime check with `hasattr(container, "exec")`
that raises a clear error if used with incompatible containers.
**Discussion Points**:
- Should we have separate protocol types for different container
capabilities?
- Should `ExecWaitStrategy` accept a more specific type (e.g.,
`DockerContainer` directly)?
- Is the runtime check acceptable, or should this be enforced at the
type level?
I've opted for the runtime check to maintain consistency with the
existing `WaitStrategyTarget` protocol pattern, but I'm open to
alternatives if the maintainers prefer a different approach.
# PR Checklist
- [x] Your PR title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) syntax
as we make use of this for detecting Semantic Versioning changes.
- Additions to the community modules do not contribute to SemVer scheme:
all community features will be tagged
[community-feat](https://github.com/testcontainers/testcontainers-python/issues?q=label%3Acommunity-feat+),
but we do not want to release minor or major versions due to features or
breaking changes outside of core.
So please use `fix(postgres):` or `fix(my_new_vector_db):` if you want
to add or modify community modules.
This may change in the future if we have a separate package released
with community modules.
- [x] Your PR allows maintainers to edit your branch, this will speed up
resolving minor issues!
- [x] The new container is implemented under `modules/*`
- Your module follows [PEP 420](https://peps.python.org/pep-0420/) with
implicit namespace packages
(if unsure, look at other existing community modules)
- Your package namespacing follows `testcontainers.<modulename>.*`
and you DO NOT have an `__init__.py` above your module's level.
- Your module has its own tests under `modules/*/tests`
- Your module has a `README.rst` and hooks in the `.. auto-class` and
`.. title` of your container
- Implement the new feature (typically in `__init__.py`) and
corresponding tests.
- [x] Your module is added in `pyproject.toml`
- it is declared under `tool.poetry.packages` - see other community
modules
- it is declared under `tool.poetry.extras` with the same name as your
module name,
we still prefer adding _NO EXTRA DEPENDENCIES_, meaning `mymodule = []`
is the preferred addition
(see the notes at the bottom)
- [x] Your branch is up-to-date (or your branch will be rebased with
`git rebase`)1 parent febccb7 commit 2d9eee3
File tree
2 files changed
+102
-5
lines changed- core/testcontainers/core
- modules/postgres/testcontainers/postgres
2 files changed
+102
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
22 | 26 | | |
23 | 27 | | |
24 | 28 | | |
| |||
779 | 783 | | |
780 | 784 | | |
781 | 785 | | |
| 786 | + | |
| 787 | + | |
| 788 | + | |
| 789 | + | |
| 790 | + | |
| 791 | + | |
| 792 | + | |
| 793 | + | |
| 794 | + | |
| 795 | + | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
| 802 | + | |
| 803 | + | |
| 804 | + | |
| 805 | + | |
| 806 | + | |
| 807 | + | |
| 808 | + | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
| 812 | + | |
| 813 | + | |
| 814 | + | |
| 815 | + | |
| 816 | + | |
| 817 | + | |
| 818 | + | |
| 819 | + | |
| 820 | + | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
| 824 | + | |
| 825 | + | |
| 826 | + | |
| 827 | + | |
| 828 | + | |
| 829 | + | |
| 830 | + | |
| 831 | + | |
| 832 | + | |
| 833 | + | |
| 834 | + | |
| 835 | + | |
| 836 | + | |
| 837 | + | |
| 838 | + | |
| 839 | + | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
782 | 879 | | |
783 | 880 | | |
784 | 881 | | |
| 882 | + | |
785 | 883 | | |
786 | 884 | | |
787 | 885 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
90 | | - | |
91 | 90 | | |
| 91 | + | |
92 | 92 | | |
93 | | - | |
| 93 | + | |
94 | 94 | | |
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
100 | | - | |
101 | | - | |
| 100 | + | |
0 commit comments