If a virtual machine is deleted, its snapshots should also be deleted.
Update the function "VMModel.delete" in order to delete snapshots
before deleting the virtual machine.
Signed-off-by: Crístian Viana <vianac(a)linux.vnet.ibm.com>
---
src/kimchi/model/vms.py | 16 ++++++++++++++++
tests/test_model.py | 3 +--
2 files changed, 17 insertions(+), 2 deletions(-)
diff --git a/src/kimchi/model/vms.py b/src/kimchi/model/vms.py
index d194049..811739e 100644
--- a/src/kimchi/model/vms.py
+++ b/src/kimchi/model/vms.py
@@ -268,6 +268,8 @@ class VMModel(object):
self.storagepool = model.storagepools.StoragePoolModel(**kargs)
self.storagevolume = model.storagevolumes.StorageVolumeModel(**kargs)
self.storagevolumes = model.storagevolumes.StorageVolumesModel(**kargs)
+ self.vmsnapshot = model.vmsnapshots.VMSnapshotModel(**kargs)
+ self.vmsnapshots = model.vmsnapshots.VMSnapshotsModel(**kargs)
def update(self, name, params):
dom = self.get_vm(name, self.conn)
@@ -752,6 +754,20 @@ class VMModel(object):
if info['state'] == 'running':
self.poweroff(name)
+ # delete existing snapshots before deleting VM
+
+ # libvirt's Test driver does not support the function
+ # "virDomainListAllSnapshots", so "VMSnapshots.get_list" will
raise
+ # "OperationFailed" in that case.
+ try:
+ snapshot_names = self.vmsnapshots.get_list(name)
+ except OperationFailed, e:
+ kimchi_log.error('cannot list snapshots: %s; '
+ 'skipping snapshot deleting...' % e.message)
+ else:
+ for s in snapshot_names:
+ self.vmsnapshot.delete(name, s)
+
try:
dom.undefine()
except libvirt.libvirtError as e:
diff --git a/tests/test_model.py b/tests/test_model.py
index cbcaed7..86ab24a 100644
--- a/tests/test_model.py
+++ b/tests/test_model.py
@@ -122,10 +122,9 @@ class ModelTests(unittest.TestCase):
self.assertRaises(NotFoundError, inst.currentvmsnapshot_lookup,
u'kimchi-vm')
+ # this snapshot should be deleted when its VM is deleted
params = {'name': u'mysnap'}
task = inst.vmsnapshots_create(u'kimchi-vm', params)
- rollback.prependDefer(inst.vmsnapshot_delete,
- u'kimchi-vm', params['name'])
inst.task_wait(task['id'])
task = inst.task_lookup(task['id'])
self.assertEquals('finished', task['status'])
--
1.9.3