-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconftest.py
524 lines (453 loc) · 18.2 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import itertools
import logging
import pytest
import tempfile
from packaging import version
from typing import Dict
import lib.config as global_config
from lib.common import wait_for, vm_image, is_uuid
from lib.common import setup_formatted_and_mounted_disk, teardown_formatted_and_mounted_disk
from lib.netutil import is_ipv6
from lib.pool import Pool
from lib.vm import VM
from lib.xo import xo_cli
# Import package-scoped fixtures. Although we need to define them in a separate file so that we can
# then import them in individual packages to fix the buggy package scope handling by pytest, we also
# need to import them in the global conftest.py so that they are recognized as fixtures.
from pkgfixtures import formatted_and_mounted_ext4_disk, sr_disk_wiped
# Do we cache VMs?
try:
from data import CACHE_IMPORTED_VM
except ImportError:
CACHE_IMPORTED_VM = False
assert CACHE_IMPORTED_VM in [True, False]
# pytest hooks
def pytest_addoption(parser):
parser.addoption(
"--hosts",
action="append",
default=[],
help="XCP-ng or XS list of master hosts (comma-separated)",
)
parser.addoption(
"--vm",
action="append",
default=[],
help="VM key or OVA URL for tests that require only one VM",
)
parser.addoption(
"--second-network",
action="store",
default=None,
help="UUID of second network in the A pool, NOT the management network"
)
parser.addoption(
"--ignore-ssh-banner",
action="store_true",
default=False,
help="Ignore SSH banners when SSH commands are executed"
)
parser.addoption(
"--ssh-output-max-lines",
action="store",
default=20,
help="Max lines to output in a ssh log (0 if no limit)"
)
parser.addoption(
"--sr-disk",
action="append",
default=[],
help="Name of an available disk (sdb) or partition device (sdb2) to be formatted and used in storage tests. "
"Set it to 'auto' to let the fixtures auto-detect available disks."
)
parser.addoption(
"--sr-disk-4k",
action="append",
default=[],
help="Name of an available disk (sdb) or partition device (sdb2) with "
"4KiB blocksize to be formatted and used in storage tests. "
"Set it to 'auto' to let the fixtures auto-detect available disks."
)
def pytest_configure(config):
global_config.ignore_ssh_banner = config.getoption('--ignore-ssh-banner')
global_config.ssh_output_max_lines = int(config.getoption('--ssh-output-max-lines'))
def pytest_generate_tests(metafunc):
if "vm_ref" in metafunc.fixturenames:
vms = metafunc.config.getoption("vm")
if not vms:
vms = [None] # no --vm parameter does not mean skip the test, for us, it means use the default
metafunc.parametrize("vm_ref", vms, indirect=True, scope="module")
def pytest_collection_modifyitems(items, config):
# Automatically mark tests based on fixtures they require.
# Check pytest.ini or pytest --markers for marker descriptions.
markable_fixtures = [
'uefi_vm',
'unix_vm',
'windows_vm',
'hostA2',
'hostB1',
'sr_disk',
'sr_disk_4k'
]
for item in items:
fixturenames = getattr(item, 'fixturenames', ())
for fixturename in markable_fixtures:
if fixturename in fixturenames:
item.add_marker(fixturename)
if 'vm_ref' not in fixturenames:
item.add_marker('no_vm')
if item.get_closest_marker('multi_vms'):
# multi_vms implies small_vm
item.add_marker('small_vm')
# BEGIN make test results visible from fixtures
# from https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures
# FIXME we may have to move this into lib/ if fixtures in sub-packages
# want to make use of this feature
PHASE_REPORT_KEY = pytest.StashKey[Dict[str, pytest.CollectReport]]()
@pytest.hookimpl(wrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
# execute all other hooks to obtain the report object
rep = yield
# store test results for each phase of a call, which can
# be "setup", "call", "teardown"
item.stash.setdefault(PHASE_REPORT_KEY, {})[rep.when] = rep
return rep
# END make test results visible from fixtures
# fixtures
def setup_host(hostname_or_ip):
pool = Pool(hostname_or_ip)
h = pool.master
return h
@pytest.fixture(scope='session')
def hosts(pytestconfig):
# a list of master hosts, each from a different pool
hosts_args = pytestconfig.getoption("hosts")
hosts_split = [hostlist.split(',') for hostlist in hosts_args]
hostname_list = list(itertools.chain(*hosts_split))
host_list = [setup_host(hostname_or_ip) for hostname_or_ip in hostname_list]
if not host_list:
pytest.fail("This test requires at least one --hosts parameter")
yield host_list
@pytest.fixture(scope='session')
def registered_xo_cli():
# The fixture is not responsible for establishing the connection.
# We just check that xo-cli is currently registered
try:
xo_cli('server.getAll')
except Exception as e:
raise Exception(f"Check for registered xo_cli failed: {e}")
@pytest.fixture(scope='session')
def hosts_with_xo(hosts, registered_xo_cli):
for h in hosts:
logging.info(">>> Connect host %s" % h)
if not h.skip_xo_config:
h.xo_server_add(h.user, h.password)
else:
h.xo_get_server_id(store=True)
wait_for(h.xo_server_connected, timeout_secs=10)
yield hosts
# teardown
for h in hosts:
if not h.skip_xo_config:
logging.info("<<< Disconnect host %s" % h)
h.xo_server_remove()
@pytest.fixture(scope='session')
def hostA1(hosts):
""" Master of first pool (pool A). """
yield hosts[0]
@pytest.fixture(scope='session')
def host(hostA1):
""" Convenience fixture for hostA1. """
yield hostA1
@pytest.fixture(scope='session')
def hostA2(hostA1):
""" Second host of pool A. """
assert len(hostA1.pool.hosts) > 1, "A second host in first pool is required"
_hostA2 = hostA1.pool.hosts[1]
logging.info(">>> hostA2 present: %s" % _hostA2)
yield _hostA2
@pytest.fixture(scope='session')
def hostB1(hosts):
""" Master of second pool (pool B). """
assert len(hosts) > 1, "A second pool is required"
assert hosts[0].pool.uuid != hosts[1].pool.uuid
_hostB1 = hosts[1]
logging.info(">>> hostB1 present: %s" % _hostB1)
yield _hostB1
@pytest.fixture(scope='session')
def host_at_least_8_3(host):
version_str = "8.3"
if not host.xcp_version >= version.parse(version_str):
pytest.skip(f"This test requires an XCP-ng >= {version_str} host")
@pytest.fixture(scope='session')
def host_less_than_8_3(host):
version_str = "8.3"
if not host.xcp_version < version.parse(version_str):
pytest.skip(f"This test requires an XCP-ng < {version_str} host")
@pytest.fixture(scope='session')
def host_with_hsts(host):
host.enable_hsts_header()
yield host
host.disable_hsts_header()
@pytest.fixture(scope='function')
def xfail_on_xcpng_8_3(host, request):
""" Test that is relevant but expected to fail in current state of XCP-ng 8.3. """
if host.xcp_version >= version.parse("8.3"):
request.node.add_marker(pytest.mark.xfail)
@pytest.fixture(scope='session')
def host_no_ipv6(host):
if is_ipv6(host.hostname_or_ip):
pytest.skip(f"This test requires an IPv4 XCP-ng")
@pytest.fixture(scope='session')
def local_sr_on_hostA1(hostA1):
""" A local SR on the pool's master. """
srs = hostA1.local_vm_srs()
assert len(srs) > 0, "a local SR is required on the pool's master"
# use the first local SR found
sr = srs[0]
logging.info(">> local SR on hostA1 present : %s" % sr.uuid)
yield sr
@pytest.fixture(scope='session')
def local_sr_on_hostA2(hostA2):
""" A local SR on the pool's second host. """
srs = hostA2.local_vm_srs()
assert len(srs) > 0, "a local SR is required on the pool's second host"
# use the first local SR found
sr = srs[0]
logging.info(">> local SR on hostA2 present : %s" % sr.uuid)
yield sr
@pytest.fixture(scope='session')
def local_sr_on_hostB1(hostB1):
""" A local SR on the second pool's master. """
srs = hostB1.local_vm_srs()
assert len(srs) > 0, "a local SR is required on the second pool's master"
# use the first local SR found
sr = srs[0]
logging.info(">> local SR on hostB1 present : %s" % sr.uuid)
yield sr
@pytest.fixture(scope='session')
def sr_disk(pytestconfig, host):
disks = pytestconfig.getoption("sr_disk")
if len(disks) != 1:
pytest.fail("This test requires exactly one --sr-disk parameter")
disk = disks[0]
if disk == "auto":
logging.info(">> Check for the presence of a free disk device on the master host")
disks = host.available_disks()
assert len(disks) > 0, "a free disk device is required on the master host"
disk = disks[0]
logging.info(f">> Found free disk device(s) on hostA1: {' '.join(disks)}. Using {disk}.")
else:
logging.info(f">> Check that disk or block device {disk} is available on the master host")
assert disk in host.available_disks(), \
f"disk or block device {disk} is either not present or already used on master host"
yield disk
@pytest.fixture(scope='session')
def sr_disk_4k(pytestconfig, host):
disks = pytestconfig.getoption("sr_disk_4k")
if len(disks) != 1:
pytest.fail("This test requires exactly one --sr-disks-4k parameter")
disk = disks[0]
if disk == "auto":
logging.info(">> Check for the presence of a free 4KiB block device on the master host")
disks = host.available_disks(4096)
assert len(disks) > 0, "a free 4KiB block device is required on the master host"
disk = disks[0]
logging.info(f">> Found free 4KiB block device(s) on hostA1: {' '.join(disks)}. Using {disk}.")
else:
logging.info(f">> Check that 4KiB block device {disk} is available on the master host")
assert disk in host.available_disks(4096), \
f"4KiB block device {disk} must be available for use on master host"
yield disk
@pytest.fixture(scope='session')
def sr_disk_for_all_hosts(pytestconfig, request, host):
disks = pytestconfig.getoption("sr_disk")
if len(disks) != 1:
pytest.fail("This test requires exactly one --sr-disk parameter")
disk = disks[0]
master_disks = host.available_disks()
assert len(master_disks) > 0, "a free disk device is required on the master host"
if disk != "auto":
assert disk in master_disks, \
f"disk or block device {disk} is either not present or already used on master host"
master_disks = [disk]
candidates = list(master_disks)
for h in host.pool.hosts[1:]:
other_disks = h.available_disks()
candidates = [d for d in candidates if d in other_disks]
if disk == "auto":
assert len(candidates) > 0, \
f"a free disk device is required on all pool members. Pool master has: {' '.join(master_disks)}."
logging.info(f">> Found free disk device(s) on all pool hosts: {' '.join(candidates)}. Using {candidates[0]}.")
else:
assert len(candidates) > 0, \
f"disk or block device {disk} was not found to be present and free on all hosts"
logging.info(f">> Disk or block device {disk} is present and free on all pool members")
yield candidates[0]
@pytest.fixture(scope='session')
def sr_disks_for_all_hosts(pytestconfig, request, host):
disks = pytestconfig.getoption("sr_disk")
assert len(disks) > 0, "This test requires at least one --sr-disk parameter"
# Fetch available disks on the master host
master_disks = host.available_disks()
assert len(master_disks) > 0, "a free disk device is required on the master host"
if "auto" in disks:
candidates = list(master_disks)
else:
# Validate that all specified disks exist on the master host
for disk in disks:
assert disk in master_disks, \
f"Disk or block device {disk} is either not present or already used on the master host"
candidates = list(disks)
# Check if all disks are available on all hosts in the pool
for h in host.pool.hosts[1:]:
other_disks = h.available_disks()
candidates = [d for d in candidates if d in other_disks]
if "auto" in disks:
# Automatically select disks if "auto" is passed
assert len(candidates) > 0, \
f"Free disk devices are required on all pool members. Pool master has: {' '.join(master_disks)}."
logging.info(">> Using free disk device(s) on all pool hosts: %s.", candidates)
else:
# Ensure specified disks are free on all hosts
assert len(candidates) == len(disks), \
f"Some specified disks ({', '.join(disks)}) are not free or available on all hosts."
logging.info(">> Disk(s) %s are present and free on all pool members", candidates)
yield candidates
@pytest.fixture(scope='module')
def vm_ref(request):
ref = request.param
if ref is None:
# get default VM from test if there's one
marker = request.node.get_closest_marker("default_vm")
default_vm = marker.args[0] if marker is not None else None
if default_vm is not None:
logging.info(">> No VM specified on CLI. Using default: %s." % default_vm)
ref = default_vm
else:
# global default
logging.info(">> No VM specified on CLI, and no default found in test definition. Using global default.")
ref = 'mini-linux-x86_64-bios'
if is_uuid(ref):
return ref
elif ref.startswith('http'):
return ref
else:
return vm_image(ref)
@pytest.fixture(scope="module")
def imported_vm(host, vm_ref):
if is_uuid(vm_ref):
vm_orig = VM(vm_ref, host)
name = vm_orig.name()
logging.info(">> Reuse VM %s (%s) on host %s" % (vm_ref, name, host))
else:
vm_orig = host.import_vm(vm_ref, host.main_sr_uuid(), use_cache=CACHE_IMPORTED_VM)
if CACHE_IMPORTED_VM:
# Clone the VM before running tests, so that the original VM remains untouched
logging.info(">> Clone cached VM before running tests")
vm = vm_orig.clone()
# Remove the description, which may contain a cache identifier
vm.param_set('name-description', "")
else:
vm = vm_orig
yield vm
# teardown
if not is_uuid(vm_ref):
logging.info("<< Destroy VM")
vm.destroy(verify=True)
@pytest.fixture(scope="module")
def started_vm(imported_vm):
vm = imported_vm
# may be already running if we skipped the import to use an existing VM
if not vm.is_running():
vm.start()
wait_for(vm.is_running, '> Wait for VM running')
wait_for(vm.try_get_and_store_ip, "> Wait for VM IP", timeout_secs=5 * 60)
return vm
# no teardown
@pytest.fixture(scope="module")
def running_vm(started_vm):
vm = started_vm
wait_for(vm.is_ssh_up, "> Wait for VM SSH up")
return vm
@pytest.fixture(scope='module')
def unix_vm(imported_vm):
vm = imported_vm
if vm.is_windows:
pytest.skip("This test is only compatible with unix VMs.")
yield vm
@pytest.fixture(scope="module")
def running_unix_vm(unix_vm, running_vm):
return running_vm
# no teardown
@pytest.fixture(scope='module')
def windows_vm(imported_vm):
vm = imported_vm
if not vm.is_windows:
pytest.skip("This test is only compatible with Windows VMs.")
yield vm
@pytest.fixture(scope='module')
def uefi_vm(imported_vm):
vm = imported_vm
if not vm.is_uefi:
pytest.skip('This test requires an UEFI VM')
yield vm
@pytest.fixture(scope='session')
def additional_repos(request, hosts):
if request.param is None:
yield []
return
repo_file = '/etc/yum.repos.d/xcp-ng-additional-tester.repo'
url_list = request.param.split(',')
with tempfile.NamedTemporaryFile('wt') as temp:
for id, url in enumerate(url_list):
temp.write("""[xcp-ng-tester-{}]
name=XCP-ng Tester {}
baseurl={}
enabled=1
gpgcheck=0
""".format(id, id, url))
temp.flush()
for host in hosts:
for host_ in host.pool.hosts:
host_.scp(temp.name, repo_file)
yield url_list
for host in hosts:
for host_ in host.pool.hosts:
host_.ssh(['rm', '-f', repo_file])
@pytest.fixture(scope='session')
def second_network(pytestconfig, host):
network_uuids = pytestconfig.getoption("second_network")
if len(network_uuids) != 1:
pytest.fail("This test requires exactly one --second-network parameter!")
network_uuid = network_uuids[0]
pif_uuid = host.xe('pif-list', {'host-uuid': host.uuid, 'network-uuid': network_uuid}, minimal=True)
if not pif_uuid:
pytest.fail("The provided --second-network UUID doesn't exist or doesn't have a PIF on master host")
ipv6 = (host.xe('pif-param-get', {'uuid': pif_uuid, 'param-name': 'primary-address-type'}) == "IPv6")
ip = host.xe('pif-param-get', {'uuid': pif_uuid, 'param-name': 'IPv6' if ipv6 else 'IP'})
if not ip:
pytest.fail("The provided --second-network has a PIF but no IP")
if network_uuid == host.management_network():
pytest.fail("--second-network must NOT be the management network")
return network_uuid
@pytest.fixture(scope='module')
def nfs_iso_device_config():
return global_config.sr_device_config("NFS_ISO_DEVICE_CONFIG", required=['location'])
@pytest.fixture(scope='module')
def cifs_iso_device_config():
return global_config.sr_device_config("CIFS_ISO_DEVICE_CONFIG")
@pytest.fixture(scope='module')
def nfs_iso_sr(host, nfs_iso_device_config):
""" A NFS ISO SR. """
sr = host.sr_create('iso', "ISO-NFS-SR-test", nfs_iso_device_config, shared=True, verify=True)
yield sr
# teardown
sr.forget()
@pytest.fixture(scope='module')
def cifs_iso_sr(host, cifs_iso_device_config):
""" A Samba/CIFS SR. """
sr = host.sr_create('iso', "ISO-CIFS-SR-test", cifs_iso_device_config, shared=True, verify=True)
yield sr
# teardown
sr.forget()