Skip to content

Commit 0fc1e94

Browse files
committed
WIP Allow not rerunning a cached dependency
* requires RKrahl/pytest-dependency#77, must document that somewhere (but it's "just convenience")
1 parent 303427d commit 0fc1e94

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/install/conftest.py

+40
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
11
from copy import deepcopy
2+
import itertools
23
import logging
34
import os
45
import pytest
56
import tempfile
67

8+
from lib.common import shortened_nodeid
79
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+
# FIXME: pytest-order has trouble understanding such a dep will be scheduled
19+
def pytest_dependency_override_skip(item: pytest.Item, dependency: str, scope: str) -> bool:
20+
if not CACHE_IMPORTED_VM:
21+
return False
22+
# FIXME duplicates parsing of --hosts, should use separate def instead
23+
hosts_args = item.config.getoption("hosts")
24+
hosts_split = [hostlist.split(',') for hostlist in hosts_args]
25+
hostname_list = list(itertools.chain(*hosts_split))
26+
if not hostname_list:
27+
pytest.fail("pytest_dependency_override_skip requires at least one --hosts parameter")
28+
pool = Pool(hostname_list[0])
29+
host = pool.master
30+
31+
sr_uuid = host.main_sr_uuid()
32+
dep_nodeid = _expand_scope_relative_nodeid(dependency, scope, item.nodeid)
33+
if host.cached_vm(shortened_nodeid(dep_nodeid) + ".xva", sr_uuid):
34+
return True
35+
return False
36+
37+
def _expand_scope_relative_nodeid(scoped_nodeid, scope, ref_nodeid):
38+
if scope == 'session' or scope == 'package':
39+
base = ()
40+
elif scope == 'module':
41+
base = ref_nodeid.split("::", 1)[:1]
42+
elif scope == 'class':
43+
base = ref_nodeid.split("::", 2)[:2]
44+
else:
45+
raise RuntimeError(f"Internal error: invalid scope {scope!r}")
46+
logging.debug("scope: %r base: %r relative: %r", scope, base, scoped_nodeid)
47+
return "::".join(itertools.chain(base, (scoped_nodeid,)))
848

949
@pytest.fixture(scope='function')
1050
def answerfile(request):

0 commit comments

Comments
 (0)