Skip to content

Commit 113beab

Browse files
committed
jobs: avoid running tests running hosts_with_xo in 'main' jobs
With traffic rules tests, we are adding more tests depending on XO (traffic rules are managed by XO). In order to avoid this strong dependency in 'main' jobs, automatically mark tests using 'hosts_with_xo' fixture with 'hosts_with_xo' mark. It permits to avoid running them in 'main' jobs, and run them in 'advanced-network' jobs instead of (for networks tests). Additionally, restrict the use of xo_cli() function to tests that are using 'hosts_with_xo' fixture. It is done by modifying a global in lib.xo module, and checking it at xo_cli() beginning. Based on idea from @stormi Signed-off-by: Sebastien Rodot <sebastien.rodot@vates.tech>
1 parent 8a13f54 commit 113beab

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

conftest.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from lib.vbd import VBD
3838
from lib.vdi import VDI
3939
from lib.vm import VM, vm_cache_key_from_def
40-
from lib.xo import xo_cli
40+
from lib.xo import _allow_xo_cli, xo_cli
4141

4242
# Import package-scoped fixtures. Although we need to define them in a separate file so that we can
4343
# then import them in individual packages to fix the buggy package scope handling by pytest, we also
@@ -202,6 +202,7 @@ def pytest_collection_modifyitems(items: list[pytest.Item], config: pytest.Confi
202202
'hostB1',
203203
'unused_512B_disks',
204204
'unused_4k_disks',
205+
'hosts_with_xo',
205206
]
206207

207208
# -------------
@@ -367,12 +368,15 @@ def registered_xo_cli() -> None:
367368
# The fixture is not responsible for establishing the connection.
368369
# We just check that xo-cli is currently registered
369370
try:
371+
old_allow_xo_cli = _allow_xo_cli(True)
370372
xo_cli('server.getAll')
373+
_allow_xo_cli(old_allow_xo_cli)
371374
except Exception as e:
372-
raise Exception(f"Check for registered xo_cli failed: {e}")
375+
pytest.fail(f"Check for registered xo_cli failed: {e}")
373376

374377
@pytest.fixture(scope='session')
375378
def hosts_with_xo(hosts: list[Host], registered_xo_cli: None) -> Generator[list[Host], None, None]:
379+
old_allow_xo_cli = _allow_xo_cli(True)
376380
for h in hosts:
377381
logging.info(">>> Connect host %s" % h)
378382
if not h.skip_xo_config:
@@ -386,6 +390,7 @@ def hosts_with_xo(hosts: list[Host], registered_xo_cli: None) -> Generator[list[
386390
if not h.skip_xo_config:
387391
logging.info("<<< Disconnect host %s" % h)
388392
h.xo_server_remove()
393+
_allow_xo_cli(old_allow_xo_cli)
389394

390395
@pytest.fixture(scope='session')
391396
def hostA1(hosts: list[Host]) -> Generator[Host, None, None]:

jobs.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ class JobData(TypedDict):
4444
"tests/xapi_plugins",
4545
"tests/install/test_fixtures.py",
4646
],
47-
"markers": "(small_vm or no_vm) and not flaky and not reboot and not complex_prerequisites",
47+
"markers": "(small_vm or no_vm) and not flaky and not reboot "
48+
"and not hosts_with_xo and not complex_prerequisites",
4849
},
4950
"main-multi-unix": {
5051
"description": "a group of tests that need to run on the largest variety of VMs - unix split",
@@ -82,13 +83,14 @@ class JobData(TypedDict):
8283
"A pool with at least 1 host (if more, with same network configuration).",
8384
"At least 2 free NICs on every host.",
8485
"A small VM that can be imported on the SRs.",
86+
"xo-cli locally installed, in $PATH, and registered to an XO instance.",
8587
],
8688
"nb_pools": 1,
8789
"params": {
8890
"--vm": "single/small_vm",
8991
},
9092
"paths": ["tests/network"],
91-
"markers": "complex_prerequisites",
93+
"markers": "complex_prerequisites or hosts_with_xo",
9294
},
9395
"packages": {
9496
"description": "tests that packages can be installed correctly",

lib/xo.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pytest
2+
13
import json
24

35
from data import TOOLS
@@ -6,6 +8,20 @@
68

79
from typing import Literal, overload
810

11+
__allow_xo_cli = False
12+
def _allow_xo_cli(value: bool) -> bool:
13+
"""
14+
Permit to configure the usage of xo_cli function (returns the previous value).
15+
This function shoudln't be called directly.
16+
If you need xo_cli(), use the hosts_with_xo fixture.
17+
"""
18+
global __allow_xo_cli
19+
20+
old = __allow_xo_cli
21+
__allow_xo_cli = value
22+
23+
return old
24+
925
@overload
1026
def xo_cli(action: str, args: dict[str, str] = {}, *, check: bool = True, use_json: Literal[False] = False) -> str:
1127
...
@@ -14,6 +30,9 @@ def xo_cli(action: str, args: dict[str, str] = {}, *, check: bool = True, use_js
1430
...
1531

1632
def xo_cli(action: str, args: dict[str, str] = {}, *, check: bool = True, use_json: bool = False) -> JSONType | str:
33+
if not __allow_xo_cli:
34+
pytest.fail("xo_cli function requires hosts_with_xo fixture usage.")
35+
1736
cmd = [TOOLS.get('xo-cli', 'xo-cli'), action]
1837
if action != 'list-objects' and use_json:
1938
cmd += ['--json']

pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ markers =
3838

3939
# * Other markers
4040
reboot: tests that reboot one or more hosts.
41+
hosts_with_xo: tests that require XO to be available via xo-cli tool.
4142
flaky: flaky tests. Usually pass, but sometimes fail unexpectedly.
4243
complex_prerequisites: tests whose prerequisites are complex and may require special attention.
4344
quicktest: runs `quicktest`.

0 commit comments

Comments
 (0)