Skip to content

Commit

Permalink
WIP Allow not rerunning a cached dependency
Browse files Browse the repository at this point in the history
* requires RKrahl/pytest-dependency#77, must
  document that somewhere (but it's "just convenience")
  • Loading branch information
ydirson committed Jun 12, 2024
1 parent 7bb74f2 commit 4a48f1c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/install/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
from copy import deepcopy
import itertools
import logging
import os
import pytest
import tempfile

from lib.common import shortened_nodeid
from lib.commands import local_cmd, scp, ssh
from lib.pool import Pool

try:
from data import CACHE_IMPORTED_VM
except ImportError:
CACHE_IMPORTED_VM = False
assert CACHE_IMPORTED_VM in [True, False]

def pytest_dependency_override_skip(item: pytest.Item, dependency: str, scope: str) -> bool:
if not CACHE_IMPORTED_VM:
return False
# FIXME duplicates parsing of --hosts, should use separate def instead
hosts_args = item.config.getoption("hosts")
hosts_split = [hostlist.split(',') for hostlist in hosts_args]
hostname_list = list(itertools.chain(*hosts_split))
if not hostname_list:
pytest.fail("pytest_dependency_override_skip requires at least one --hosts parameter")
pool = Pool(hostname_list[0])
host = pool.master

sr_uuid = host.main_sr_uuid()
dep_nodeid = _expand_scope_relative_nodeid(dependency, scope, item.nodeid)
if host.cached_vm(shortened_nodeid(dep_nodeid) + ".xva", sr_uuid):
return True
return False

def _expand_scope_relative_nodeid(scoped_nodeid, scope, ref_nodeid):
if scope == 'session' or scope == 'package':
base = ()
elif scope == 'module':
base = ref_nodeid.split("::", 1)[:1]
elif scope == 'class':
base = ref_nodeid.split("::", 2)[:2]
else:
raise RuntimeError(f"Internal error: invalid scope {scope!r}")
logging.debug("scope: %r base: %r relative: %r", scope, base, scoped_nodeid)
return "::".join(itertools.chain(base, (scoped_nodeid,)))

@pytest.fixture(scope='function')
def answerfile(request):
Expand Down

0 comments on commit 4a48f1c

Please sign in to comment.