Skip to content

Commit b7cb0a5

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 28fe75f commit b7cb0a5

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

tests/install/conftest.py

+38
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,47 @@
1+
import itertools
12
import logging
23
import os
34
import pytest
45
import tempfile
56

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

846
# auto-collect tests that are dependencies of selected ones
947
# adapted from RKrahl/pytest-dependency#56

0 commit comments

Comments
 (0)