Skip to content

Commit 44363c7

Browse files
committed
install 1/n: fixture to create VMs from template
1 parent d57e393 commit 44363c7

File tree

5 files changed

+71
-0
lines changed

5 files changed

+71
-0
lines changed

conftest.py

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

lib/host.py

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

242+
def vm_from_template(self, name, template):
243+
params = {
244+
"new-name-label": name,
245+
"template": template,
246+
"sr-uuid": self.main_sr_uuid(),
247+
}
248+
vm_uuid = self.xe('vm-install', params)
249+
logging.info("VM UUID: %s created", vm_uuid)
250+
return VM(vm_uuid, self)
251+
242252
def pool_has_vm(self, vm_uuid, vm_type='vm'):
243253
if vm_type == 'snapshot':
244254
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import logging
2+
import pytest
3+
4+
@pytest.mark.vm_definitions(dict(name="vm 1",
5+
template="Other install media",
6+
))
7+
class TestInstallNested:
8+
def test_install_nested_821(self, create_vms):
9+
pass

0 commit comments

Comments
 (0)