Skip to content

Commit 44192d7

Browse files
committed
install 1/n: fixture to create VMs from template
1 parent e83bc73 commit 44192d7

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
@@ -354,6 +354,57 @@ def imported_vm(host, vm_ref):
354354
logging.info("<< Destroy VM")
355355
vm.destroy(verify=True)
356356

357+
@pytest.fixture(scope="function")
358+
def create_vms(request, host):
359+
"""
360+
Returns list of VM objects created from `vm_definitions` marker.
361+
362+
`vm_definitions` marker test author to specify one or more VMs,
363+
using one `dict` per VM.
364+
365+
Mandatory keys:
366+
- `name`: name of the VM to create (str)
367+
- `template`: name (or UUID) of template to use (str)
368+
369+
Example:
370+
-------
371+
> @pytest.mark.vm_definitions(dict(name="vm 1", template="Other install media"),
372+
> dict(name="vm 2", template="CentOS 7"))
373+
> def test_foo(create_vms):
374+
> ...
375+
376+
"""
377+
marker = request.node.get_closest_marker("vm_definitions")
378+
if marker is None:
379+
raise Exception("No vm_definitions marker specified.")
380+
for vm_def in marker.args:
381+
assert "name" in vm_def
382+
assert "template" in vm_def
383+
# FIXME should check for extra args
384+
385+
try:
386+
vms = []
387+
for vm_def in marker.args:
388+
vm_name = vm_def["name"]
389+
vm_template = vm_def["template"]
390+
391+
logging.info(">> Install VM %r from template %r", vm_name, vm_template)
392+
393+
vm = host.vm_from_template(vm_name, vm_template)
394+
395+
vms.append(vm)
396+
397+
yield vms
398+
399+
except Exception:
400+
logging.error("exception caught...")
401+
raise
402+
403+
finally:
404+
for vm in vms:
405+
logging.info("<< Destroy VM %s", vm.uuid)
406+
vm.destroy(verify=True)
407+
357408
@pytest.fixture(scope="module")
358409
def running_vm(imported_vm):
359410
vm = imported_vm

lib/host.py

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

252+
def vm_from_template(self, name, template):
253+
params = {
254+
"new-name-label": name,
255+
"template": template,
256+
"sr-uuid": self.main_sr_uuid(),
257+
}
258+
vm_uuid = self.xe('vm-install', params)
259+
return VM(vm_uuid, self)
260+
252261
def pool_has_vm(self, vm_uuid, vm_type='vm'):
253262
if vm_type == 'snapshot':
254263
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
@@ -18,6 +18,9 @@ markers =
1818
unix_vm: tests that require a unix/linux VM to run.
1919
windows_vm: tests that require a Windows VM to run.
2020

21+
# * VM-related markers to give parameters to fixtures
22+
vm_definitions: dicts of VM defs for create_vms fixture.
23+
2124
# * Test targets related to VMs
2225
small_vm: tests that it is enough to run just once, using the smallest possible VM.
2326
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)