userver: /data/code/userver/testsuite/pytest_plugins/pytest_userver/plugins/base.py Source File
Loading...
Searching...
No Matches
base.py
1"""
2Configure the service in testsuite.
3"""
4
5import pathlib
6
7import pytest
8
9
10def pytest_addoption(parser) -> None:
11 group = parser.getgroup('userver')
12 group.addoption(
13 '--build-dir',
14 type=pathlib.Path,
15 help='Path to service build directory.',
16 )
17
18 group = parser.getgroup('Test service')
19 group.addoption(
20 '--service-binary', type=pathlib.Path, help='Path to service binary.',
21 )
22 group.addoption(
23 '--service-port',
24 help=(
25 'Main HTTP port of the service '
26 '(default: use the port from the static config)'
27 ),
28 default=None,
29 type=int,
30 )
31 group.addoption(
32 '--monitor-port',
33 help=(
34 'Monitor HTTP port of the service '
35 '(default: use the port from the static config)'
36 ),
37 default=None,
38 type=int,
39 )
40 group.addoption(
41 '--service-source-dir',
42 type=pathlib.Path,
43 help='Path to service source directory.',
44 default=pathlib.Path('.'),
45 )
46
47
48def pytest_configure(config):
49 config.option.asyncio_mode = 'auto'
50
51
52@pytest.fixture(scope='session')
53def service_source_dir(pytestconfig) -> pathlib.Path:
54 """
55 Returns the path to the service source directory that is set by command
56 line `--service-source-dir` option.
57
58 Override this fixture to change the way the path to the service
59 source directory is detected by testsuite.
60
61 @ingroup userver_testsuite_fixtures
62 """
63 return pytestconfig.option.service_source_dir
64
65
66@pytest.fixture(scope='session')
67def build_dir(pytestconfig) -> pathlib.Path:
68 """
69 Returns the build directory set by command line `--build-dir` option.
70
71 Override this fixture to change the way the build directory is
72 detected by the testsuite.
73
74 @ingroup userver_testsuite_fixtures
75 """
76 return pytestconfig.option.build_dir
77
78
79@pytest.fixture(scope='session')
80def service_binary(pytestconfig) -> pathlib.Path:
81 """
82 Returns the path to service binary set by command line `--service-binary`
83 option.
84
85 Override this fixture to change the way the path to service binary is
86 detected by the testsuite.
87
88 @ingroup userver_testsuite_fixtures
89 """
90 return pytestconfig.option.service_binary
91
92
93@pytest.fixture(scope='session')
94def service_port(pytestconfig, _original_service_config) -> int:
95 """
96 Returns the main listener port number of the service set by command line
97 `--service-port` option.
98 If no port is specified in the command line option, keeps the original port
99 specified in the static config.
100
101 Override this fixture to change the way the main listener port number is
102 detected by the testsuite.
103
104 @ingroup userver_testsuite_fixtures
105 """
106 return pytestconfig.option.service_port or _get_port(
107 _original_service_config, 'listener', service_port, '--service-port',
108 )
109
110
111@pytest.fixture(scope='session')
112def monitor_port(pytestconfig, _original_service_config) -> int:
113 """
114 Returns the monitor listener port number of the service set by command line
115 `--monitor-port` option.
116 If no port is specified in the command line option, keeps the original port
117 specified in the static config.
118
119 Override this fixture to change the way the monitor listener port number
120 is detected by testsuite.
121
122 @ingroup userver_testsuite_fixtures
123 """
124 return pytestconfig.option.monitor_port or _get_port(
125 _original_service_config,
126 'listener-monitor',
127 monitor_port,
128 '--service-port',
129 )
130
131
132def _get_port(
133 original_service_config, listener_name, port_fixture, option_name,
134) -> int:
135 config_yaml = original_service_config.config_yaml
136 config_vars = original_service_config.config_vars
137 components = config_yaml['components_manager']['components']
138 listener = components.get('server', {}).get(listener_name, {})
139 if not listener:
140 return -1
141 port = listener.get('port', None)
142 if isinstance(port, str) and port.startswith('$'):
143 port = config_vars.get(port[1:], None) or listener.get(
144 'port#fallback', None,
145 )
146 assert port, (
147 f'Please specify '
148 f'components_manager.components.server.{listener_name}.port '
149 f'in the static config, or pass {option_name} pytest option, '
150 f'or override the {port_fixture.__name__} fixture'
151 )
152 return port