Merge "vmware: driver races to create instance images"

This commit is contained in:
Jenkins
2014-02-25 03:54:30 +00:00
committed by Gerrit Code Review
2 changed files with 59 additions and 11 deletions

View File

@@ -12,10 +12,13 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from nova.network import model as network_model
from nova import test
from nova import utils
from nova.virt.vmwareapi import error_util
from nova.virt.vmwareapi import vm_util
from nova.virt.vmwareapi import vmops
@@ -101,3 +104,42 @@ class VMwareVMOpsTestCase(test.NoDBTestCase):
value = vmops.VMwareVMOps.decide_linked_clone("yes", False)
self.assertTrue(value,
"image level metadata failed to override global")
def _setup_create_cache_mocks(self):
ops = vmops.VMwareVMOps(mock.Mock(), mock.Mock(), mock.Mock())
base_name = ops._base_folder
ds_name = "datastore"
ds_ref = mock.Mock()
path = vm_util.build_datastore_path(ds_name, base_name)
ops._mkdir = mock.Mock()
return ds_name, ds_ref, ops, path
def test_create_cache_folder(self):
ds_name, ds_ref, ops, path = self._setup_create_cache_mocks()
ops.create_cache_folder(ds_name, ds_ref)
ops._mkdir.assert_called_with(path, ds_ref)
def test_create_cache_folder_with_exception(self):
ds_name, ds_ref, ops, path = self._setup_create_cache_mocks()
ops._mkdir.side_effect = error_util.FileAlreadyExistsException()
ops.create_cache_folder(ds_name, ds_ref)
# assert that the
ops._mkdir.assert_called_with(path, ds_ref)
def test_check_if_folder_file_exists_with_existing(self):
ops = vmops.VMwareVMOps(mock.Mock(), mock.Mock(), mock.Mock())
ops.create_cache_folder = mock.Mock()
ops._file_exists = mock.Mock()
ops._file_exists.return_value = True
ops._check_if_folder_file_exists(mock.Mock(), "datastore",
"folder", "some_file")
ops.create_cache_folder.assert_called_once()
def test_check_if_folder_file_exists_no_existing(self):
ops = vmops.VMwareVMOps(mock.Mock(), mock.Mock(), mock.Mock())
ops.create_cache_folder = mock.Mock()
ops._file_exists = mock.Mock()
ops._file_exists.return_value = True
ops._check_if_folder_file_exists(mock.Mock(), "datastore",
"folder", "some_file")
ops.create_cache_folder.assert_called_once()

View File

@@ -1598,7 +1598,7 @@ class VMwareVMOps(object):
return False
return True
def _path_file_exists(self, ds_browser, ds_path, file_name):
def _file_exists(self, ds_browser, ds_path, file_name):
"""Check if the path and file exists on the datastore."""
client_factory = self._session._get_vim().client.factory
search_spec = vm_util.search_datastore_spec(client_factory, file_name)
@@ -1618,11 +1618,11 @@ class VMwareVMOps(object):
continue
break
if task_info.state == "error":
return False, False
return False
file_exists = (getattr(task_info.result, 'file', False) and
task_info.result.file[0].path == file_name)
return True, file_exists
return file_exists
def _mkdir(self, ds_path, ds_ref):
"""
@@ -1638,21 +1638,27 @@ class VMwareVMOps(object):
createParentDirectories=True)
LOG.debug(_("Created directory with path %s") % ds_path)
def create_cache_folder(self, ds_name, ds_ref):
try:
path = vm_util.build_datastore_path(ds_name, self._base_folder)
self._mkdir(path, ds_ref)
except error_util.FileAlreadyExistsException:
# NOTE(hartsocks): if the temp folder already exists, that
# just means the temp folder was prepped by another process.
pass
def _check_if_folder_file_exists(self, ds_ref, ds_name,
folder_name, file_name):
# Ensure that the cache folder exists
self.create_cache_folder(ds_name, ds_ref)
ds_browser = self._session._call_method(
vim_util, "get_dynamic_property", ds_ref, "Datastore", "browser")
# Check if the folder exists or not. If not, create one
# Check if the file exists or not.
folder_path = vm_util.build_datastore_path(ds_name, folder_name)
folder_exists, file_exists = self._path_file_exists(ds_browser,
folder_path,
file_name)
# Ensure that the cache folder exists
if not folder_exists:
self._mkdir(vm_util.build_datastore_path(ds_name,
self._base_folder),
ds_ref)
file_exists = self._file_exists(ds_browser, folder_path, file_name)
return file_exists