|
1 | 1 | from copy import deepcopy
|
| 2 | +import itertools |
2 | 3 | import logging
|
3 | 4 | import os
|
4 | 5 | import pytest
|
5 | 6 | import tempfile
|
6 | 7 |
|
| 8 | +from lib.common import shortened_nodeid |
7 | 9 | from lib.commands import local_cmd, scp, ssh
|
| 10 | +from lib.pool import Pool |
| 11 | + |
| 12 | +try: |
| 13 | + from data import CACHE_IMPORTED_VM |
| 14 | +except ImportError: |
| 15 | + CACHE_IMPORTED_VM = False |
| 16 | +assert CACHE_IMPORTED_VM in [True, False] |
| 17 | + |
| 18 | +def pytest_dependency_override_skip(item: pytest.Item, dependency: str, scope: str) -> bool: |
| 19 | + if not CACHE_IMPORTED_VM: |
| 20 | + return False |
| 21 | + # FIXME duplicates parsing of --hosts, should use separate def instead |
| 22 | + hosts_args = item.config.getoption("hosts") |
| 23 | + hosts_split = [hostlist.split(',') for hostlist in hosts_args] |
| 24 | + hostname_list = list(itertools.chain(*hosts_split)) |
| 25 | + if not hostname_list: |
| 26 | + pytest.fail("pytest_dependency_override_skip requires at least one --hosts parameter") |
| 27 | + pool = Pool(hostname_list[0]) |
| 28 | + host = pool.master |
| 29 | + |
| 30 | + sr_uuid = host.main_sr_uuid() |
| 31 | + dep_nodeid = _expand_scope_relative_nodeid(dependency, scope, item.nodeid) |
| 32 | + if host.cached_vm(shortened_nodeid(dep_nodeid) + ".xva", sr_uuid): |
| 33 | + return True |
| 34 | + return False |
| 35 | + |
| 36 | +def _expand_scope_relative_nodeid(scoped_nodeid, scope, ref_nodeid): |
| 37 | + if scope == 'session' or scope == 'package': |
| 38 | + base = () |
| 39 | + elif scope == 'module': |
| 40 | + base = ref_nodeid.split("::", 1)[:1] |
| 41 | + elif scope == 'class': |
| 42 | + base = ref_nodeid.split("::", 2)[:2] |
| 43 | + else: |
| 44 | + raise RuntimeError(f"Internal error: invalid scope {scope!r}") |
| 45 | + logging.debug("scope: %r base: %r relative: %r", scope, base, scoped_nodeid) |
| 46 | + return "::".join(itertools.chain(base, (scoped_nodeid,))) |
8 | 47 |
|
9 | 48 | @pytest.fixture(scope='function')
|
10 | 49 | def answerfile(request):
|
|
0 commit comments