Debian Codegraph @

t/smoke/conftest.py

raw
1 from __future__ import annotations
2
3 import os
4
5 import pytest
6 from pytest_celery import (LOCALSTACK_CREDS, REDIS_CONTAINER_TIMEOUT, REDIS_ENV, REDIS_IMAGE, REDIS_PORTS,
7 CeleryTestSetup, RedisContainer)
8 from pytest_docker_tools import container, fetch, fxtr
9
10 from celery import Celery
11 from t.smoke.operations.task_termination import TaskTermination
12 from t.smoke.operations.worker_kill import WorkerKill
13 from t.smoke.operations.worker_restart import WorkerRestart
14 from t.smoke.workers.alt import * # noqa
15 from t.smoke.workers.dev import * # noqa
16 from t.smoke.workers.latest import * # noqa
17 from t.smoke.workers.other import * # noqa
18
19
20 class SmokeTestSetup(CeleryTestSetup):
21 def ready(self, *args, **kwargs) -> bool:
22 # Force false, false, true
23 return super().ready(
24 ping=False,
25 control=False,
26 docker=True,
27 )
28
29
30 @pytest.fixture
31 def celery_setup_cls() -> type[CeleryTestSetup]: # type: ignore
32 return SmokeTestSetup
33
34
35 class SuiteOperations(
36 TaskTermination,
37 WorkerKill,
38 WorkerRestart,
39 ):
40 """Optional operations that can be performed with different methods,
41 shared across the smoke tests suite.
42
43 Example Usage:
44 >>> class test_mysuite(SuiteOperations):
45 >>> def test_something(self):
46 >>> self.prepare_worker_with_conditions()
47 >>> assert condition are met
48 """
49
50
51 @pytest.fixture
52 def default_worker_tasks(default_worker_tasks: set) -> set:
53 """Use all of the integration and smoke suites tasks in the smoke tests workers."""
54 from t.integration import tasks as integration_tests_tasks
55 from t.smoke import tasks as smoke_tests_tasks
56
57 default_worker_tasks.add(integration_tests_tasks)
58 default_worker_tasks.add(smoke_tests_tasks)
59 return default_worker_tasks
60
61
62 # When using integration tests tasks that requires a Redis instance,
63 # we use pytest-celery to raise a dedicated Redis container for the smoke tests suite that is configured
64 # to be used by the integration tests tasks.
65
66 redis_command = RedisContainer.command()
67 redis_command.insert(1, "/usr/local/etc/redis/redis.conf")
68
69 redis_image = fetch(repository=REDIS_IMAGE)
70 redis_test_container: RedisContainer = container(
71 image="{redis_image.id}",
72 ports=REDIS_PORTS,
73 environment=REDIS_ENV,
74 network="{default_pytest_celery_network.name}",
75 wrapper_class=RedisContainer,
76 timeout=REDIS_CONTAINER_TIMEOUT,
77 command=redis_command,
78 volumes={
79 os.path.abspath("t/smoke/redis.conf"): {
80 "bind": "/usr/local/etc/redis/redis.conf",
81 "mode": "ro", # Mount as read-only
82 }
83 },
84 )
85
86
87 @pytest.fixture(autouse=True)
88 def set_redis_test_container(redis_test_container: RedisContainer):
89 """Configure the Redis test container to be used by the integration tests tasks."""
90 # get_redis_connection(): will use these settings in the tests environment
91 os.environ["REDIS_HOST"] = "localhost"
92 os.environ["REDIS_PORT"] = str(redis_test_container.port)
93
94
95 @pytest.fixture
96 def default_worker_env(default_worker_env: dict, redis_test_container: RedisContainer) -> dict:
97 """Add the Redis connection details to the worker environment."""
98 # get_redis_connection(): will use these settings when executing tasks in the worker
99 default_worker_env.update(
100 {
101 "REDIS_HOST": redis_test_container.hostname,
102 "REDIS_PORT": 6379,
103 **LOCALSTACK_CREDS,
104 }
105 )
106 return default_worker_env
107
108
109 @pytest.fixture(scope="session", autouse=True)
110 def set_aws_credentials():
111 os.environ.update(LOCALSTACK_CREDS)
112
113
114 @pytest.fixture
115 def default_worker_app(default_worker_app: Celery) -> Celery:
116 app = default_worker_app
117 if app.conf.broker_url and app.conf.broker_url.startswith("sqs"):
118 app.conf.broker_transport_options["region"] = LOCALSTACK_CREDS["AWS_DEFAULT_REGION"]
119 return app
120
121
122 # Override the default redis broker container from pytest-celery
123 default_redis_broker = container(
124 image="{default_redis_broker_image}",
125 ports=fxtr("default_redis_broker_ports"),
126 environment=fxtr("default_redis_broker_env"),
127 network="{default_pytest_celery_network.name}",
128 wrapper_class=RedisContainer,
129 timeout=REDIS_CONTAINER_TIMEOUT,
130 command=redis_command,
131 volumes={
132 os.path.abspath("t/smoke/redis.conf"): {
133 "bind": "/usr/local/etc/redis/redis.conf",
134 "mode": "ro", # Mount as read-only
135 }
136 },
137 )
138
139
140 # Override the default redis backend container from pytest-celery
141 default_redis_backend = container(
142 image="{default_redis_backend_image}",
143 ports=fxtr("default_redis_backend_ports"),
144 environment=fxtr("default_redis_backend_env"),
145 network="{default_pytest_celery_network.name}",
146 wrapper_class=RedisContainer,
147 timeout=REDIS_CONTAINER_TIMEOUT,
148 command=redis_command,
149 volumes={
150 os.path.abspath("t/smoke/redis.conf"): {
151 "bind": "/usr/local/etc/redis/redis.conf",
152 "mode": "ro", # Mount as read-only
153 }
154 },
155 )