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