userver: /data/code/service_template/third_party/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
48@pytest.fixture(scope='session')
49def service_source_dir(pytestconfig) -> pathlib.Path:
50 """
51 Returns the path to the service source directory that is set by command
52 line `--service-source-dir` option.
53
54 Override this fixture to change the way the path to the service
55 source directory is detected by testsuite.
56
57 @ingroup userver_testsuite_fixtures
58 """
59 return pytestconfig.option.service_source_dir
60
61
62@pytest.fixture(scope='session')
63def build_dir(pytestconfig) -> pathlib.Path:
64 """
65 Returns the build directory set by command line `--build-dir` option.
66
67 Override this fixture to change the way the build directory is
68 detected by the testsuite.
69
70 @ingroup userver_testsuite_fixtures
71 """
72 return pytestconfig.option.build_dir
73
74
75@pytest.fixture(scope='session')
76def service_binary(pytestconfig) -> pathlib.Path:
77 """
78 Returns the path to service binary set by command line `--service-binary`
79 option.
80
81 Override this fixture to change the way the path to service binary is
82 detected by the testsuite.
83
84 @ingroup userver_testsuite_fixtures
85 """
86 return pytestconfig.option.service_binary
87
88
89@pytest.fixture(scope='session')
90def service_port(pytestconfig, _original_service_config) -> int:
91 """
92 Returns the main listener port number of the service set by command line
93 `--service-port` option.
94 If no port is specified in the command line option, keeps the original port
95 specified in the static config.
96
97 Override this fixture to change the way the main listener port number is
98 detected by the testsuite.
99
100 @ingroup userver_testsuite_fixtures
101 """
102 return pytestconfig.option.service_port or _get_port(
103 _original_service_config, 'listener', service_port, '--service-port',
104 )
105
106
107@pytest.fixture(scope='session')
108def monitor_port(pytestconfig, _original_service_config) -> int:
109 """
110 Returns the monitor listener port number of the service set by command line
111 `--monitor-port` option.
112 If no port is specified in the command line option, keeps the original port
113 specified in the static config.
114
115 Override this fixture to change the way the monitor listener port number
116 is detected by testsuite.
117
118 @ingroup userver_testsuite_fixtures
119 """
120 return pytestconfig.option.monitor_port or _get_port(
121 _original_service_config,
122 'listener-monitor',
123 monitor_port,
124 '--service-port',
125 )
126
127
128def _get_port(
129 original_service_config, listener_name, port_fixture, option_name,
130) -> int:
131 config_yaml = original_service_config.config_yaml
132 config_vars = original_service_config.config_vars
133 components = config_yaml['components_manager']['components']
134 listener = components.get('server', {}).get(listener_name, {})
135 if not listener:
136 return -1
137 port = listener.get('port', None)
138 if isinstance(port, str) and port.startswith('$'):
139 port = config_vars.get(port[1:], None) or listener.get(
140 'port#fallback', None,
141 )
142 assert port, (
143 f'Please specify '
144 f'components_manager.components.server.{listener_name}.port '
145 f'in the static config, or pass {option_name} pytest option, '
146 f'or override the {port_fixture.__name__} fixture'
147 )
148 return port