-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: move Redis family to clientless validation #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cofin
wants to merge
2
commits into
feat/provider-aware-ci
Choose a base branch
from
feat/clientless-redis-family
base: feat/provider-aware-ci
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,18 +4,72 @@ | |
| from typing import TYPE_CHECKING | ||
|
|
||
| import pytest | ||
| from redis import Redis | ||
| from redis.exceptions import ConnectionError as RedisConnectionError | ||
| from docker.errors import ContainerError | ||
|
|
||
| from pytest_databases.helpers import get_xdist_worker_num | ||
| from pytest_databases.types import ServiceContainer, XdistIsolationLevel | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Generator | ||
| from collections.abc import Generator, Iterator | ||
|
|
||
| from docker.models.containers import Container | ||
|
|
||
| from pytest_databases._service import DockerService | ||
|
|
||
|
|
||
| REDIS_PROBE_IMAGE = "redis:latest" | ||
|
|
||
|
|
||
| def _output_to_bytes(output: bytes | str | Iterator[bytes]) -> bytes: | ||
| if isinstance(output, bytes): | ||
| return output | ||
| if isinstance(output, str): | ||
| return output.encode() | ||
| return b"".join(output) | ||
|
|
||
|
|
||
| def _exec_redis_cli(container: Container, *args: str, db: int = 0) -> tuple[int, bytes]: | ||
| result = container.exec_run([ | ||
| "redis-cli", | ||
| "-h", | ||
| "localhost", | ||
| "-p", | ||
| "6379", | ||
| "-n", | ||
| str(db), | ||
| *args, | ||
| ]) | ||
| return result.exit_code if result.exit_code is not None else -1, _output_to_bytes(result.output) | ||
|
|
||
|
|
||
| def _probe_redis_endpoint( | ||
| docker_service: DockerService, | ||
| service: ServiceContainer, | ||
| *args: str, | ||
| db: int = 0, | ||
| probe_image: str = REDIS_PROBE_IMAGE, | ||
| ) -> tuple[int, bytes]: | ||
| try: | ||
| output = docker_service._client.containers.run( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use the already running container for this? We could modify |
||
| image=probe_image, | ||
| command=[ | ||
| "redis-cli", | ||
| "-h", | ||
| service.host, | ||
| "-p", | ||
| str(service.port), | ||
| "-n", | ||
| str(db), | ||
| *args, | ||
| ], | ||
| network_mode="host", | ||
| remove=True, | ||
| ) | ||
| except ContainerError as exc: | ||
| return exc.exit_status, _output_to_bytes(exc.stderr) if exc.stderr else str(exc).encode() | ||
| return 0, _output_to_bytes(output) | ||
|
|
||
|
|
||
| @dataclasses.dataclass | ||
| class RedisService(ServiceContainer): | ||
| db: int | ||
|
|
@@ -26,16 +80,6 @@ def xdist_redis_isolation_level() -> XdistIsolationLevel: | |
| return "database" | ||
|
|
||
|
|
||
| def redis_responsive(service_container: ServiceContainer) -> bool: | ||
| client = Redis(host=service_container.host, port=service_container.port) | ||
| try: | ||
| return client.ping() | ||
| except (ConnectionError, RedisConnectionError): | ||
| return False | ||
| finally: | ||
| client.close() | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=False, scope="session") | ||
| def redis_port(redis_service: RedisService) -> int: | ||
| return redis_service.port | ||
|
|
@@ -66,9 +110,13 @@ def redis_service( | |
| else: | ||
| name += f"_{worker_num + 1}" | ||
|
|
||
| def _responsive(_service: ServiceContainer) -> bool: | ||
| exit_code, output = _probe_redis_endpoint(docker_service, _service, "PING") | ||
| return exit_code == 0 and output.strip().endswith(b"PONG") | ||
|
|
||
| with docker_service.run( | ||
| redis_image, | ||
| check=redis_responsive, | ||
| check=_responsive, | ||
| container_port=6379, | ||
| name=name, | ||
| transient=xdist_redis_isolation_level == "server", | ||
|
|
@@ -101,9 +149,13 @@ def dragonfly_service( | |
| else: | ||
| name += f"_{worker_num + 1}" | ||
|
|
||
| def _responsive(_service: ServiceContainer) -> bool: | ||
| exit_code, output = _probe_redis_endpoint(docker_service, _service, "PING") | ||
| return exit_code == 0 and output.strip().endswith(b"PONG") | ||
|
|
||
| with docker_service.run( | ||
| dragonfly_image, | ||
| check=redis_responsive, | ||
| check=_responsive, | ||
| container_port=6379, | ||
| name=name, | ||
| transient=xdist_redis_isolation_level == "server", | ||
|
|
@@ -146,9 +198,13 @@ def keydb_service( | |
| else: | ||
| name += f"_{worker_num + 1}" | ||
|
|
||
| def _responsive(_service: ServiceContainer) -> bool: | ||
| exit_code, output = _probe_redis_endpoint(docker_service, _service, "PING") | ||
| return exit_code == 0 and output.strip().endswith(b"PONG") | ||
|
|
||
| with docker_service.run( | ||
| keydb_image, | ||
| check=redis_responsive, | ||
| check=_responsive, | ||
| container_port=6379, | ||
| name=name, | ||
| transient=xdist_redis_isolation_level == "server", | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be the same image as the redis that's currently running. Otherwise it might be an unnecessary pull