Skip to content

Commit 4a48f1c

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 7bb74f2 commit 4a48f1c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

tests/install/conftest.py

+39
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,49 @@
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+
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,)))
847

948
@pytest.fixture(scope='function')
1049
def answerfile(request):

0 commit comments

Comments
 (0)