Skip to content

Commit 00e6296

Browse files
committed
install 1/n: fixture to create VMs from template
1 parent c1710cd commit 00e6296

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

conftest.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,58 @@ def imported_vm(host, vm_ref):
307307
logging.info("<< Destroy VM")
308308
vm.destroy(verify=True)
309309

310+
@pytest.fixture(scope="class")
311+
def create_vms(request, host):
312+
"""
313+
Returns list of VM objects created from `vm_definitions` marker.
314+
315+
`vm_definitions` marker test author to specify one or more VMs,
316+
using one `dict` per VM.
317+
318+
Mandatory keys:
319+
- `name`: name of the VM to create (str)
320+
- `template`: name (or UUID) of template to use (str)
321+
322+
Example:
323+
-------
324+
> @pytest.mark.vm_definitions(dict(name="vm 1", template="Other install media"),
325+
> dict(name="vm 2", template="CentOS 7"))
326+
> def test_foo(create_vms):
327+
> ...
328+
329+
"""
330+
vm_name = "Test VM from scratch"
331+
markers = request.node.get_closest_marker("vm_definitions")
332+
if markers is None:
333+
raise Exception("No vm_definitions marker specified.")
334+
for marker in markers.args:
335+
assert "name" in marker
336+
assert "template" in marker
337+
# FIXME should check for extra args
338+
339+
try:
340+
vms = []
341+
for marker in markers.args:
342+
vm_name = marker["name"]
343+
vm_template = marker["template"]
344+
345+
logging.info(">> Install VM %r from template %r", vm_name, vm_template)
346+
347+
vm = host.vm_from_template(vm_name, vm_template)
348+
349+
vms.append(vm)
350+
351+
yield vms
352+
353+
except Exception:
354+
logging.error("exception caught...")
355+
raise
356+
357+
finally:
358+
for vm in vms:
359+
logging.info("<< Destroy VM %s", vm.uuid)
360+
vm.destroy(verify=True)
361+
310362
@pytest.fixture(scope="module")
311363
def running_vm(imported_vm):
312364
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
@@ -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_install.py

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

0 commit comments

Comments
 (0)