|
1 | 1 | import os |
2 | 2 | import uuid |
| 3 | +from functools import cached_property |
| 4 | +from pathlib import Path |
3 | 5 |
|
4 | 6 | import apypie |
5 | 7 | import paramiko |
|
16 | 18 | SSH_CONFIG='./.tmp/ssh-config' |
17 | 19 |
|
18 | 20 |
|
| 21 | +class UserParameters: |
| 22 | + def __init__(self, config): |
| 23 | + self._config = config |
| 24 | + |
| 25 | + @property |
| 26 | + def _obsah_state(self) -> Path: |
| 27 | + # mirror what foremanctl / obsah does |
| 28 | + if state := os.environ.get('OBSAH_STATE'): |
| 29 | + return Path(state) |
| 30 | + |
| 31 | + if base := os.environ.get('OBSAH_BASE'): |
| 32 | + root = Path(base) |
| 33 | + else: |
| 34 | + root = self._config.rootpath |
| 35 | + return root / '.var' / 'lib' / 'foremanctl' |
| 36 | + |
| 37 | + def _read_parameters(self): |
| 38 | + params_file = self._obsah_state / 'parameters.yaml' |
| 39 | + if params_file.exists(): |
| 40 | + with params_file.open('r') as f: |
| 41 | + return yaml.safe_load(f) |
| 42 | + |
| 43 | + return None |
| 44 | + |
| 45 | + @cached_property |
| 46 | + def features(self): |
| 47 | + params = self._read_parameters() |
| 48 | + if params: |
| 49 | + return set(params.get('features', [])) |
| 50 | + return set() |
| 51 | + |
| 52 | + |
19 | 53 | def pytest_addoption(parser): |
20 | 54 | parser.addoption("--certificate-source", action="store", default="default", choices=('default', 'installer', 'custom_server'), help="Certificate source used during deployment") |
21 | 55 | parser.addoption("--database-mode", action="store", default="internal", choices=('internal', 'external'), help="Whether the database is internal or external") |
@@ -206,34 +240,18 @@ def wait_for_metadata_generate(foremanapi): |
206 | 240 | wait_for_tasks(foremanapi, 'label = Actions::Katello::Repository::MetadataGenerate') |
207 | 241 |
|
208 | 242 |
|
209 | | -def is_iop_enabled(): |
210 | | - test_dir = os.path.dirname(os.path.abspath(__file__)) |
211 | | - foremanctl_dir = os.path.dirname(test_dir) |
212 | | - params_file = os.path.join(foremanctl_dir, '.var', 'lib', 'foremanctl', 'parameters.yaml') |
213 | | - |
214 | | - if os.path.exists(params_file): |
215 | | - with open(params_file, 'r') as f: |
216 | | - params = yaml.safe_load(f) |
217 | | - features = params.get('features', []) |
218 | | - if isinstance(features, str): |
219 | | - features = features.split() |
220 | | - return 'iop' in features |
221 | | - |
222 | | - return False |
223 | | - |
224 | | - |
225 | 243 | def pytest_configure(config): |
226 | | - config.addinivalue_line("markers", "iop: tests requiring IOP to be enabled") |
| 244 | + config.addinivalue_line("markers", "feature(name): mark a test as requiring a feature") |
227 | 245 |
|
| 246 | + config.user_parameters = UserParameters(config) |
228 | 247 |
|
229 | | -def pytest_collection_modifyitems(config, items): |
230 | | - if is_iop_enabled(): |
231 | | - return |
232 | 248 |
|
233 | | - skip_iop = pytest.mark.skip(reason="IOP not enabled - skipping IOP tests ('iop' not in enabled_features)") |
234 | | - for item in items: |
235 | | - if "iop" in item.keywords: |
236 | | - item.add_marker(skip_iop) |
| 249 | +def pytest_runtest_setup(item): |
| 250 | + feature_markers = set(mark.args[0] for mark in item.iter_markers(name="feature")) |
| 251 | + if feature_markers: |
| 252 | + missing = feature_markers - item.config.user_parameters.features |
| 253 | + if missing: |
| 254 | + pytest.skip("test requires feature(s) {!r}".format(missing)) |
237 | 255 |
|
238 | 256 |
|
239 | 257 | class ResolveAdapter(HTTPAdapter): |
|
0 commit comments