Skip to content

Commit 169de39

Browse files
committed
VM caching on export/import
1 parent 0b125e1 commit 169de39

File tree

3 files changed

+44
-13
lines changed

3 files changed

+44
-13
lines changed

conftest.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,13 @@ def create_vms(request, host):
389389
> ...
390390
391391
"""
392+
# Do we cache VMs?
393+
try:
394+
from data import CACHE_IMPORTED_VM
395+
except ImportError:
396+
CACHE_IMPORTED_VM = False
397+
assert CACHE_IMPORTED_VM in [True, False]
398+
392399
marker = request.node.get_closest_marker("vm_definitions")
393400
if marker is None:
394401
raise Exception("No vm_definitions marker specified.")
@@ -408,7 +415,7 @@ def create_vms(request, host):
408415
if "template" in vm_def:
409416
_create_vm(vm_def, host, vms, vdis, vbds)
410417
elif "image" in vm_def:
411-
_import_vm(vm_def, host, vms)
418+
_import_vm(vm_def, host, vms, use_cache=CACHE_IMPORTED_VM)
412419
yield vms
413420

414421
except Exception:
@@ -456,10 +463,19 @@ def _create_vm(marker, host, vms, vdis, vbds):
456463
logging.info("Setting param %s", param_def)
457464
vm.param_set(**param_def)
458465

459-
def _import_vm(marker, host, vms):
466+
def _import_vm(marker, host, vms, *, use_cache):
460467
vm_name = marker["name"]
461468
vm_image = marker["image"]
462-
vm = host.import_vm(vm_image)
469+
base_vm = host.import_vm(vm_image, sr_uuid=host.main_sr_uuid(), use_cache=use_cache)
470+
471+
if use_cache:
472+
# Clone the VM before running tests, so that the original VM remains untouched
473+
logging.info(">> Clone cached VM before running tests")
474+
vm = base_vm.clone()
475+
# Remove the description, which may contain a cache identifier
476+
vm.param_set('name-description', "")
477+
else:
478+
vm = base_vm
463479
vms.append(vm)
464480

465481
@pytest.fixture(scope="module")

lib/basevm.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,18 @@ def get_sr(self):
6767
assert sr.attached_to_host(self.host)
6868
return sr
6969

70-
def export(self, filepath, compress='none'):
71-
logging.info("Export VM %s to %s with compress=%s" % (self.uuid, filepath, compress))
72-
params = {
73-
'uuid': self.uuid,
74-
'compress': compress,
75-
'filename': filepath
76-
}
77-
self.host.xe('vm-export', params)
70+
def export(self, filepath, compress='none', use_cache=False):
71+
72+
if use_cache:
73+
logging.info("Export VM %s to cache for %r as a clone" % (self.uuid, filepath))
74+
clone = self.clone()
75+
logging.info(f"Marking VM {clone.uuid} as cached")
76+
clone.param_set('name-description', self.host.vm_cache_key(filepath))
77+
else:
78+
logging.info("Export VM %s to %s with compress=%s" % (self.uuid, filepath, compress))
79+
params = {
80+
'uuid': self.uuid,
81+
'compress': compress,
82+
'filename': filepath
83+
}
84+
self.host.xe('vm-export', params)

tests/install/test.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@
88
from lib.host import Host
99
from lib.pool import Pool
1010

11+
try:
12+
from data import CACHE_IMPORTED_VM
13+
except ImportError:
14+
CACHE_IMPORTED_VM = False
15+
assert CACHE_IMPORTED_VM in [True, False]
16+
1117
class TestNested:
1218
@pytest.mark.vm_definitions(
1319
dict(name="vm 1",
@@ -111,7 +117,8 @@ def test_install_821_uefi(self, iso_remaster, create_vms):
111117
# FIXME move to fixture
112118
# FIXME where to store?
113119
host_vm.host.ssh(["rm -f test_install_821_uefi-vm1.xva"])
114-
host_vm.export("test_install_821_uefi-vm1.xva", "zstd")
120+
host_vm.export("test_install_821_uefi-vm1.xva", "zstd",
121+
use_cache=CACHE_IMPORTED_VM)
115122

116123
@pytest.mark.vm_definitions(
117124
dict(name="vm 1",
@@ -218,4 +225,5 @@ def test_firstboot_821_uefi(self, create_vms):
218225
# FIXME move to fixture
219226
# FIXME where to store?
220227
host_vm.host.ssh(["rm -f test_firstboot_821_uefi-vm1.xva"])
221-
host_vm.export("test_firstboot_821_uefi-vm1.xva", "zstd")
228+
host_vm.export("test_firstboot_821_uefi-vm1.xva", "zstd",
229+
use_cache=CACHE_IMPORTED_VM)

0 commit comments

Comments
 (0)