9def pytest_addoption(parser):
10 group = parser.getgroup(
'redis')
11 group.addoption(
'--redis-host', help=
'Redis host')
12 group.addoption(
'--redis-master-port', type=int, help=
'Redis master port')
14 '--redis-sentinel-port',
16 help=
'Redis sentinel port',
20 help=
'Do not fill redis storage',
25def pytest_configure(config):
26 config.addinivalue_line(
28 'redis_store: per-test redis initialization',
30 config.addinivalue_line(
32 'redis_cluster_store: per-test redis cluster initialization',
34 config.addinivalue_line(
36 'redis_standalone_store: per-test standalone redis initialization',
40def pytest_service_register(register_service):
41 register_service(
'redis', service.create_redis_service)
42 register_service(
'redis-cluster', service.create_cluster_redis_service)
44 'redis-standalone', service.create_standalone_redis_service
48@pytest.fixture(scope='session')
51 ensure_service_started,
52 _redis_service_settings,
54 if not pytestconfig.option.no_redis
and not pytestconfig.option.redis_host:
55 ensure_service_started(
'redis', settings=_redis_service_settings)
58@pytest.fixture(scope='session')
59def redis_cluster_service(
60 pytestconfig, ensure_service_started, _redis_cluster_service_settings
62 if not pytestconfig.option.no_redis
and not pytestconfig.option.redis_host:
63 ensure_service_started(
64 'redis-cluster', settings=_redis_cluster_service_settings
68@pytest.fixture(scope='session')
69async def redis_standalone_service(
70 pytestconfig, ensure_service_started, _redis_standalone_service_settings
72 if not pytestconfig.option.no_redis
and not pytestconfig.option.redis_host:
73 ensure_service_started(
74 'redis-standalone', settings=_redis_standalone_service_settings
82 _redis_execute_commands_from_file,
84 """Returns a Redis client for the test store.
86 Commands from static files are applied before the test; the database is
89 @ingroup userver_testsuite_fixtures
90 Part of the [yandex-taxi-testsuite](https://github.com/yandex/yandex-taxi-testsuite/blob/develop/testsuite/databases/redis/pytest_plugin.py#L79)
92 if pytestconfig.option.no_redis:
96 redis_db = _redis_store
99 _redis_execute_commands_from_file(
'redis_store', redis_db)
113 if pytestconfig.option.no_redis:
117 redis_db = redisdb.StrictRedis(
118 host=redis_sentinels[0][
'host'],
119 port=redis_sentinels[0][
'port'],
126def redis_cluster_store(
128 _redis_cluster_store,
129 _redis_execute_commands_from_file,
131 def _flush_all(redis_db):
132 slot_infos = redis_db.cluster_slots()
133 nodes = redis_db.get_primaries()
134 redis_db.flushall(target_nodes=nodes)
135 redis_db.wait(1, 10, target_nodes=nodes)
137 if pytestconfig.option.no_redis:
141 redis_db = _redis_cluster_store
144 _redis_execute_commands_from_file(
'redis_cluster_store', redis_db)
150@pytest.fixture(scope='session')
151def _redis_masters(pytestconfig, _redis_service_settings):
152 if pytestconfig.option.redis_host:
156 'host': pytestconfig.option.redis_host,
158 pytestconfig.option.redis_master_port
159 or _redis_service_settings.master_ports[0]
164 {
'host': _redis_service_settings.host,
'port': port}
165 for port
in _redis_service_settings.master_ports
169@pytest.fixture(scope='session')
170def redis_sentinels(pytestconfig, _redis_service_settings):
171 if pytestconfig.option.redis_host:
175 'host': pytestconfig.option.redis_host,
177 pytestconfig.option.redis_sentinel_port
178 or _redis_service_settings.sentinel_port
184 'host': _redis_service_settings.host,
185 'port': _redis_service_settings.sentinel_port,
190@pytest.fixture(scope='session')
191def redis_cluster_nodes(_redis_cluster_service_settings):
194 'host': _redis_cluster_service_settings.host,
197 for port
in _redis_cluster_service_settings.cluster_ports
201@pytest.fixture(scope='session')
202def redis_standalone_node(_redis_standalone_service_settings):
204 'host': _redis_standalone_service_settings.host,
205 'port': _redis_standalone_service_settings.port,
209@pytest.fixture(scope='session')
210def redis_cluster_replicas(_redis_cluster_service_settings):
211 return _redis_cluster_service_settings.cluster_replicas
214@pytest.fixture(scope='session')
215def _redis_service_settings():
216 return service.get_service_settings()
219@pytest.fixture(scope='session')
220def _redis_cluster_service_settings():
221 return service.get_cluster_service_settings()
224@pytest.fixture(scope='session')
225def _redis_standalone_service_settings():
226 return service.get_standalone_service_settings()
229def _json_object_hook(dct):
231 return json.dumps(dct[
'$json'])
235@pytest.fixture(scope='session')
241 if pytestconfig.option.no_redis:
245 redis_db = redisdb.StrictRedis(
246 host=_redis_masters[0][
'host'],
247 port=_redis_masters[0][
'port'],
254@pytest.fixture(scope='session')
255def _redis_cluster_store(
257 redis_cluster_service,
260 if pytestconfig.option.no_redis:
264 redis_db = redisdb.RedisCluster(
265 host=redis_cluster_nodes[0][
'host'],
266 port=redis_cluster_nodes[0][
'port'],
273def redis_standalone_store(
275 redis_standalone_service,
276 redis_standalone_node,
277 _redis_execute_commands_from_file,
279 if pytestconfig.option.no_redis:
283 redis_db = redisdb.StrictRedis(
284 host=redis_standalone_node[
'host'],
285 port=redis_standalone_node[
'port'],
289 _redis_execute_commands_from_file(
'redis_standalone_store', redis_db)
296def _redis_execute_commands_from_file(request, load_json):
297 def _execute_commands(markers, redis_db):
300 for mark
in request.node.iter_markers(markers):
301 store_file = mark.kwargs.get(
'file')
302 if store_file
is not None:
303 redis_commands_from_file = load_json(
304 '%s.json' % store_file,
305 object_hook=_json_object_hook,
307 redis_commands.extend(redis_commands_from_file)
310 redis_commands.extend(mark.args)
312 for redis_command
in redis_commands:
313 func = getattr(redis_db, redis_command[0])
314 func(*redis_command[1:])
316 return _execute_commands