Skip to content

Commit 95fd89b

Browse files
committed
install 1/n: fixture to create VMs from template
1 parent d410ac3 commit 95fd89b

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

conftest.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,57 @@ def imported_vm(host, vm_ref):
338338
logging.info("<< Destroy VM")
339339
vm.destroy(verify=True)
340340

341+
@pytest.fixture(scope="function")
342+
def create_vms(request, host):
343+
"""
344+
Returns list of VM objects created from `vm_definitions` marker.
345+
346+
`vm_definitions` marker test author to specify one or more VMs,
347+
using one `dict` per VM.
348+
349+
Mandatory keys:
350+
- `name`: name of the VM to create (str)
351+
- `template`: name (or UUID) of template to use (str)
352+
353+
Example:
354+
-------
355+
> @pytest.mark.vm_definitions(dict(name="vm 1", template="Other install media"),
356+
> dict(name="vm 2", template="CentOS 7"))
357+
> def test_foo(create_vms):
358+
> ...
359+
360+
"""
361+
markers = request.node.get_closest_marker("vm_definitions")
362+
if markers is None:
363+
raise Exception("No vm_definitions marker specified.")
364+
for marker in markers.args:
365+
assert "name" in marker
366+
assert "template" in marker
367+
# FIXME should check for extra args
368+
369+
try:
370+
vms = []
371+
for marker in markers.args:
372+
vm_name = marker["name"]
373+
vm_template = marker["template"]
374+
375+
logging.info(">> Install VM %r from template %r", vm_name, vm_template)
376+
377+
vm = host.vm_from_template(vm_name, vm_template)
378+
379+
vms.append(vm)
380+
381+
yield vms
382+
383+
except Exception:
384+
logging.error("exception caught...")
385+
raise
386+
387+
finally:
388+
for vm in vms:
389+
logging.info("<< Destroy VM %s", vm.uuid)
390+
vm.destroy(verify=True)
391+
341392
@pytest.fixture(scope="module")
342393
def running_vm(imported_vm):
343394
vm = imported_vm

lib/host.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,15 @@ def import_vm(self, uri, sr_uuid=None, use_cache=False):
238238
vm.param_set('name-description', cache_key)
239239
return vm
240240

241+
def vm_from_template(self, name, template):
242+
params = {
243+
"new-name-label": name,
244+
"template": template,
245+
"sr-uuid": self.main_sr_uuid(),
246+
}
247+
vm_uuid = self.xe('vm-install', params)
248+
return VM(vm_uuid, self)
249+
241250
def pool_has_vm(self, vm_uuid, vm_type='vm'):
242251
if vm_type == 'snapshot':
243252
return self.xe('snapshot-list', {'uuid': vm_uuid}, minimal=True) == vm_uuid

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ markers =
1919
unix_vm: tests that require a unix/linux VM to run.
2020
windows_vm: tests that require a Windows VM to run.
2121

22+
# * VM-related markers to give parameters to fixtures
23+
vm_definitions: dicts of VM defs for create_vms fixture.
24+
2225
# * Test targets related to VMs
2326
small_vm: tests that it is enough to run just once, using the smallest possible VM.
2427
big_vm: tests that it would be good to run with a big VM.

tests/install/__init__.py

Whitespace-only changes.

tests/install/test_install.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import logging
2+
import pytest
3+
4+
class TestInstallNested:
5+
@pytest.mark.vm_definitions(
6+
dict(name="vm 1",
7+
template="Other install media",
8+
))
9+
def test_install_nested_821_uefi(self, create_vms):
10+
assert len(create_vms) == 1
11+
host_vm = create_vms[0]

0 commit comments

Comments
 (0)