diff --git a/nova/tests/unit/virt/libvirt/test_driver.py b/nova/tests/unit/virt/libvirt/test_driver.py index bafd05d800..eb49f9910a 100644 --- a/nova/tests/unit/virt/libvirt/test_driver.py +++ b/nova/tests/unit/virt/libvirt/test_driver.py @@ -16241,8 +16241,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' @@ -16250,6 +16251,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') @@ -16260,16 +16262,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' @@ -16277,6 +16281,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) @@ -16292,8 +16297,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' @@ -16303,6 +16310,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') @@ -16316,8 +16324,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' @@ -16326,6 +16336,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() diff --git a/nova/virt/libvirt/driver.py b/nova/virt/libvirt/driver.py index 53a4a4075e..c09d8de0a0 100644 --- a/nova/virt/libvirt/driver.py +++ b/nova/virt/libvirt/driver.py @@ -1098,7 +1098,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. @@ -1119,6 +1120,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)