[PATCH][Kimchi 0/2] Do not delete pool and network in use by templates or guests

Ramon Medeiros (2): Do not remove storagepools linked to guests Update tests i18n.py | 1 + model/storagepools.py | 21 +++++++++++++++++++++ tests/test_rest.py | 4 +++- 3 files changed, 25 insertions(+), 1 deletion(-) -- 2.5.5

The initial propose was to extend this task to network pools, but, they already check if guests or templates are linked before deleting. Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- i18n.py | 1 + model/storagepools.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/i18n.py b/i18n.py index 7625469..f1db243 100644 --- a/i18n.py +++ b/i18n.py @@ -225,6 +225,7 @@ messages = { "KCHPOOL0036E": _("A volume group named '%(name)s' already exists. Please, choose another name to create the logical pool."), "KCHPOOL0037E": _("Unable to update database with deep scan information due error: %(err)s"), "KCHPOOL0038E": _("No volume group '%(name)s' found. Please, specify an existing volume group to create the logical pool from."), + "KCHPOOL0039E": _("Unable to delete pool %(name)s as it is associated with guests: %(vms)s"), "KCHVOL0001E": _("Storage volume %(name)s already exists"), "KCHVOL0002E": _("Storage volume %(name)s does not exist in storage pool %(pool)s"), diff --git a/model/storagepools.py b/model/storagepools.py index a2dbaec..f8c1fe4 100644 --- a/model/storagepools.py +++ b/model/storagepools.py @@ -471,6 +471,11 @@ class StoragePoolModel(object): if self._pool_used_by_template(name): raise InvalidOperation('KCHPOOL0035E', {'name': name}) + vms = self._get_vms_attach_to_storagepool(name) + if len(vms) > 0: + raise InvalidOperation('KCHPOOL0039E', {'name': name, + 'vms': ",".join(vms)}) + pool = self.get_storagepool(name, self.conn) if pool.isActive(): raise InvalidOperation("KCHPOOL0005E", {'name': name}) @@ -480,6 +485,22 @@ class StoragePoolModel(object): raise OperationFailed("KCHPOOL0011E", {'name': name, 'err': e.get_error_message()}) + def _get_vms_attach_to_storagepool(self, storagepool): + conn = self.conn.get() + + # get storage pool path + stpool = self.get_storagepool(storagepool, self.conn) + path = "".join(xpath_get_text(stpool.XMLDesc(), "/pool/target/path")) + + vms = [] + for dom in conn.listAllDomains(0): + xml = dom.XMLDesc(0) + files = "/domain/devices/disk/source/@file" + for file in xpath_get_text(xml, files): + if file.startswith(path): + vms.append(dom.name()) + return vms + class IsoPoolModel(object): def __init__(self, **kargs): -- 2.5.5

Signed-off-by: Ramon Medeiros <ramonn@linux.vnet.ibm.com> --- tests/test_rest.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_rest.py b/tests/test_rest.py index 80596c2..b6c3203 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -863,9 +863,11 @@ class RestTests(unittest.TestCase): resp = self.request('/plugins/kimchi/storagepools/tmp/deactivate', {}, 'POST') self.assertEquals(200, resp.status) + + # cannot delete storagepool with volumes associate to guests resp = self.request('/plugins/kimchi/storagepools/tmp', {}, 'DELETE') - self.assertEquals(204, resp.status) + self.assertEquals(400, resp.status) def test_vm_iface(self): -- 2.5.5

What you did in test_rest.py was to modify the existing test because you changed the logic. Which is fine. However, you changed a test that was verifying the removal of a storage pool to a test that is verifying that a pool in use by a guest isn't being removed. The test is now just verifying your new condition. You need to add the removal test back in test_rest.py by removing all storage in the VM and then asserting that the removal was succeeded. I suggest you do it just after your modified assertion. Also: contrib/check_i18n.py ./i18n.py Checking for invalid i18n string... Checking for invalid i18n string successfully /bin/pep8 --version 1.6.2 /bin/pep8 --filename '*.py,*.py.in' --exclude="*config.py,*i18n.py,*tests/test_config.py" . ./model/storagepools.py:476:66: W291 trailing whitespace Makefile:1062: recipe for target 'check-local' failed make: *** [check-local] Error 1 [danielhb@arthas kimchi]$ On 08/17/2016 02:30 PM, Ramon Medeiros wrote:
Ramon Medeiros (2): Do not remove storagepools linked to guests Update tests
i18n.py | 1 + model/storagepools.py | 21 +++++++++++++++++++++ tests/test_rest.py | 4 +++- 3 files changed, 25 insertions(+), 1 deletion(-)
participants (2)
-
Daniel Henrique Barboza
-
Ramon Medeiros