Skip to content

Commit 165d8a9

Browse files
committed
install 1/n: fixture to create VMs from template
1 parent b7aabf4 commit 165d8a9

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
@@ -331,6 +331,57 @@ def imported_vm(host, vm_ref):
331331
logging.info("<< Destroy VM")
332332
vm.destroy(verify=True)
333333

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

lib/host.py

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

247+
def vm_from_template(self, name, template):
248+
params = {
249+
"new-name-label": name,
250+
"template": template,
251+
"sr-uuid": self.main_sr_uuid(),
252+
}
253+
vm_uuid = self.xe('vm-install', params)
254+
return VM(vm_uuid, self)
255+
247256
def pool_has_vm(self, vm_uuid, vm_type='vm'):
248257
if vm_type == 'snapshot':
249258
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.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 TestNested:
5+
@pytest.mark.vm_definitions(
6+
dict(name="vm 1",
7+
template="Other install media",
8+
))
9+
def test_nested_821_uefi(self, create_vms):
10+
assert len(create_vms) == 1
11+
host_vm = create_vms[0]

0 commit comments

Comments
 (0)