Merge "Fixes "Hyper-V destroy vm fails on Windows Server 2008R2""

This commit is contained in:
Jenkins 2015-07-20 15:48:59 +00:00 committed by Gerrit Code Review
commit c4defdb168
2 changed files with 29 additions and 1 deletions

View File

@ -136,6 +136,22 @@ class PathUtilsTestCase(test_base.HyperVBaseTestCase):
def test_force_unmount_smb_share(self):
self._test_unmount_smb_share(force=True)
@mock.patch('shutil.rmtree')
def test_rmtree(self, mock_rmtree):
class WindowsError(Exception):
def __init__(self, winerror=None):
self.winerror = winerror
mock_rmtree.side_effect = [WindowsError(
pathutils.ERROR_DIR_IS_NOT_EMPTY), True]
fake_windows_error = WindowsError
with mock.patch('__builtin__.WindowsError',
fake_windows_error, create=True):
self._pathutils.rmtree(mock.sentinel.FAKE_PATH)
mock_rmtree.assert_has_calls([mock.call(mock.sentinel.FAKE_PATH),
mock.call(mock.sentinel.FAKE_PATH)])
@mock.patch('os.path.join')
def test_get_instances_sub_dir(self, fake_path_join):

View File

@ -16,6 +16,7 @@
import os
import shutil
import sys
import time
if sys.platform == 'win32':
import wmi
@ -45,6 +46,7 @@ CONF.register_opts(hyperv_opts, 'hyperv')
CONF.import_opt('instances_path', 'nova.compute.manager')
ERROR_INVALID_NAME = 123
ERROR_DIR_IS_NOT_EMPTY = 145
class PathUtils(object):
@ -99,7 +101,17 @@ class PathUtils(object):
self.rename(src, os.path.join(dest_dir, fname))
def rmtree(self, path):
shutil.rmtree(path)
# This will be removed once support for Windows Server 2008R2 is
# stopped
for i in range(5):
try:
shutil.rmtree(path)
return
except WindowsError as e:
if e.winerror == ERROR_DIR_IS_NOT_EMPTY:
time.sleep(1)
else:
raise e
def get_instances_dir(self, remote_server=None):
local_instance_path = os.path.normpath(CONF.instances_path)