]> xenbits.xensource.com Git - osstest/openstack-nova.git/commitdiff
Ensure that instance directory is removed after success migration/resize
authorMaciej Józefczyk <maciej.jozefczyk@corp.ovh.com>
Thu, 23 Feb 2017 11:56:04 +0000 (12:56 +0100)
committerLee Yarwood <lyarwood@redhat.com>
Fri, 3 Mar 2017 10:18:24 +0000 (10:18 +0000)
Nova recreates instance directory on source host after successful migration/resize.
This patch removes directory of migrated instance from source host.

Change-Id: Ic683f83e428106df64be42287e2c5f3b40e73da4
Closes-Bug: #1666831
(cherry picked from commit 6347baf3d09036525b7f6df991ae440d558f9cc3)

nova/tests/unit/virt/libvirt/test_driver.py
nova/virt/libvirt/driver.py

index fdcb872a83cdd6444f9f7a5c8d8399cd89150061..f7ea4bb503641bcb95fd8a077e759d3c9f162b75 100644 (file)
@@ -16310,8 +16310,9 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
         with test.nested(
                 mock.patch.object(os.path, 'exists'),
                 mock.patch.object(libvirt_utils, 'get_instance_path'),
-                mock.patch.object(utils, 'execute')) as (
-                mock_exists, mock_get_path, mock_exec):
+                mock.patch.object(utils, 'execute'),
+                mock.patch.object(shutil, 'rmtree')) as (
+                mock_exists, mock_get_path, mock_exec, mock_rmtree):
             mock_exists.return_value = True
             mock_get_path.return_value = '/fake/inst'
 
@@ -16319,6 +16320,7 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
             mock_get_path.assert_called_once_with(ins_ref)
             mock_exec.assert_called_once_with('rm', '-rf', '/fake/inst_resize',
                                               delay_on_retry=True, attempts=5)
+            mock_rmtree.assert_not_called()
 
     def test_cleanup_resize_not_same_host(self):
         CONF.set_override('policy_dirs', [], group='oslo_policy')
@@ -16329,16 +16331,18 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
         drvr = libvirt_driver.LibvirtDriver(fake.FakeVirtAPI(), False)
         drvr.image_backend = mock.Mock()
         drvr.image_backend.by_name.return_value = drvr.image_backend
+        drvr.image_backend.exists.return_value = False
 
         with test.nested(
                 mock.patch.object(os.path, 'exists'),
                 mock.patch.object(libvirt_utils, 'get_instance_path'),
                 mock.patch.object(utils, 'execute'),
+                mock.patch.object(shutil, 'rmtree'),
                 mock.patch.object(drvr, '_undefine_domain'),
                 mock.patch.object(drvr, 'unplug_vifs'),
                 mock.patch.object(drvr, 'unfilter_instance')
-        ) as (mock_exists, mock_get_path, mock_exec, mock_undef,
-              mock_unplug, mock_unfilter):
+        ) as (mock_exists, mock_get_path, mock_exec, mock_rmtree,
+              mock_undef, mock_unplug, mock_unfilter):
             mock_exists.return_value = True
             mock_get_path.return_value = '/fake/inst'
 
@@ -16346,6 +16350,7 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
             mock_get_path.assert_called_once_with(ins_ref)
             mock_exec.assert_called_once_with('rm', '-rf', '/fake/inst_resize',
                                               delay_on_retry=True, attempts=5)
+            mock_rmtree.assert_called_once_with('/fake/inst')
             mock_undef.assert_called_once_with(ins_ref)
             mock_unplug.assert_called_once_with(ins_ref, fake_net)
             mock_unfilter.assert_called_once_with(ins_ref, fake_net)
@@ -16361,8 +16366,10 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
                 mock.patch.object(os.path, 'exists'),
                 mock.patch.object(libvirt_utils, 'get_instance_path'),
                 mock.patch.object(utils, 'execute'),
+                mock.patch.object(shutil, 'rmtree'),
                 mock.patch.object(drvr.image_backend, 'remove_snap')) as (
-                mock_exists, mock_get_path, mock_exec, mock_remove):
+                mock_exists, mock_get_path, mock_exec, mock_rmtree,
+                mock_remove):
             mock_exists.return_value = True
             mock_get_path.return_value = '/fake/inst'
 
@@ -16372,6 +16379,7 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
                                               delay_on_retry=True, attempts=5)
             mock_remove.assert_called_once_with(
                     libvirt_utils.RESIZE_SNAPSHOT_NAME, ignore_errors=True)
+            self.assertFalse(mock_rmtree.called)
 
     def test_cleanup_resize_snap_backend_image_does_not_exist(self):
         CONF.set_override('policy_dirs', [], group='oslo_policy')
@@ -16385,8 +16393,10 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
                 mock.patch.object(os.path, 'exists'),
                 mock.patch.object(libvirt_utils, 'get_instance_path'),
                 mock.patch.object(utils, 'execute'),
+                mock.patch.object(shutil, 'rmtree'),
                 mock.patch.object(drvr.image_backend, 'remove_snap')) as (
-                mock_exists, mock_get_path, mock_exec, mock_remove):
+                mock_exists, mock_get_path, mock_exec, mock_rmtree,
+                mock_remove):
             mock_exists.return_value = True
             mock_get_path.return_value = '/fake/inst'
 
@@ -16395,6 +16405,7 @@ class LibvirtDriverTestCase(test.NoDBTestCase):
             mock_exec.assert_called_once_with('rm', '-rf', '/fake/inst_resize',
                                               delay_on_retry=True, attempts=5)
             self.assertFalse(mock_remove.called)
+            mock_rmtree.called_once_with('/fake/inst')
 
     def test_get_instance_disk_info_exception(self):
         instance = self._create_instance()
index 0aad4105a43681c16a30d0ba7a05e55e2558333d..d41c9b1ba949a4cd0c3f61f3c56d4e0eb104b4dd 100644 (file)
@@ -1110,7 +1110,8 @@ class LibvirtDriver(driver.ComputeDriver):
             host=CONF.host)
 
     def _cleanup_resize(self, instance, network_info):
-        target = libvirt_utils.get_instance_path(instance) + '_resize'
+        inst_base = libvirt_utils.get_instance_path(instance)
+        target = inst_base + '_resize'
 
         if os.path.exists(target):
             # Deletion can fail over NFS, so retry the deletion as required.
@@ -1131,6 +1132,16 @@ class LibvirtDriver(driver.ComputeDriver):
             root_disk.remove_snap(libvirt_utils.RESIZE_SNAPSHOT_NAME,
                                   ignore_errors=True)
 
+        # NOTE(mjozefcz):
+        # self.image_backend.image for some backends recreates instance
+        # directory and image disk.info - remove it here if exists
+        if os.path.exists(inst_base) and not root_disk.exists():
+            try:
+                shutil.rmtree(inst_base)
+            except OSError as e:
+                if e.errno != errno.ENOENT:
+                    raise
+
         if instance.host != CONF.host:
             self._undefine_domain(instance)
             self.unplug_vifs(instance, network_info)