summaryrefslogtreecommitdiff
path: root/nova/tests/virt/vmwareapi/test_ds_util.py
diff options
context:
space:
mode:
authorVui Lam <vui@vmware.com>2014-06-05 16:16:55 -0700
committerMatthew Booth <mbooth@redhat.com>2014-07-17 11:49:13 +0100
commitfc8a70a02a18ecc26d9cbc24deee1efe23603ecc (patch)
treeecba5c02f5f7d30a6e9986bc3b564a2fb17b6264 /nova/tests/virt/vmwareapi/test_ds_util.py
parent13b8714808af17f0976163ff5c65b509338f6adb (diff)
downloadnova-fc8a70a02a18ecc26d9cbc24deee1efe23603ecc.tar.gz
VMware: consolidate datastore code
This is largely a straight-forward code movement. Given that we have utility classes/code in ds_util.py, it makes sense to move the remaining datastore-related code in vm_util.py to ds_util.py as well. partial blueprint vmware-spawn-refactor Change-Id: I2ede44306acf91a13f2ff6f0d8f210642a1c1e69
Diffstat (limited to 'nova/tests/virt/vmwareapi/test_ds_util.py')
-rw-r--r--nova/tests/virt/vmwareapi/test_ds_util.py97
1 files changed, 97 insertions, 0 deletions
diff --git a/nova/tests/virt/vmwareapi/test_ds_util.py b/nova/tests/virt/vmwareapi/test_ds_util.py
index 06fd242022..2b957e1c5f 100644
--- a/nova/tests/virt/vmwareapi/test_ds_util.py
+++ b/nova/tests/virt/vmwareapi/test_ds_util.py
@@ -13,9 +13,12 @@
# under the License.
import contextlib
+import re
import mock
+from nova import exception
+from nova.openstack.common.gettextutils import _
from nova.openstack.common import units
from nova import test
from nova.tests.virt.vmwareapi import fake
@@ -157,6 +160,100 @@ class DsUtilTestCase(test.NoDBTestCase):
'fake-browser', 'fake-path', 'fake-file')
self.assertFalse(file_exists)
+ def test_get_datastore(self):
+ fake_objects = fake.FakeRetrieveResult()
+ fake_objects.add_object(fake.Datastore())
+ result = ds_util.get_datastore(
+ fake.FakeObjectRetrievalSession(fake_objects))
+
+ self.assertEqual("fake-ds", result.name)
+ self.assertEqual(units.Ti, result.capacity)
+ self.assertEqual(500 * units.Gi, result.freespace)
+
+ def test_get_datastore_with_regex(self):
+ # Test with a regex that matches with a datastore
+ datastore_valid_regex = re.compile("^openstack.*\d$")
+ fake_objects = fake.FakeRetrieveResult()
+ fake_objects.add_object(fake.Datastore("openstack-ds0"))
+ fake_objects.add_object(fake.Datastore("fake-ds0"))
+ fake_objects.add_object(fake.Datastore("fake-ds1"))
+ result = ds_util.get_datastore(
+ fake.FakeObjectRetrievalSession(fake_objects), None, None,
+ datastore_valid_regex)
+ self.assertEqual("openstack-ds0", result.name)
+
+ def test_get_datastore_with_token(self):
+ regex = re.compile("^ds.*\d$")
+ fake0 = fake.FakeRetrieveResult()
+ fake0.add_object(fake.Datastore("ds0", 10 * units.Gi, 5 * units.Gi))
+ fake0.add_object(fake.Datastore("foo", 10 * units.Gi, 9 * units.Gi))
+ setattr(fake0, 'token', 'token-0')
+ fake1 = fake.FakeRetrieveResult()
+ fake1.add_object(fake.Datastore("ds2", 10 * units.Gi, 8 * units.Gi))
+ fake1.add_object(fake.Datastore("ds3", 10 * units.Gi, 1 * units.Gi))
+ result = ds_util.get_datastore(
+ fake.FakeObjectRetrievalSession(fake0, fake1), None, None, regex)
+ self.assertEqual("ds2", result.name)
+
+ def test_get_datastore_with_list(self):
+ # Test with a regex containing whitelist of datastores
+ datastore_valid_regex = re.compile("(openstack-ds0|openstack-ds2)")
+ fake_objects = fake.FakeRetrieveResult()
+ fake_objects.add_object(fake.Datastore("openstack-ds0"))
+ fake_objects.add_object(fake.Datastore("openstack-ds1"))
+ fake_objects.add_object(fake.Datastore("openstack-ds2"))
+ result = ds_util.get_datastore(
+ fake.FakeObjectRetrievalSession(fake_objects), None, None,
+ datastore_valid_regex)
+ self.assertNotEqual("openstack-ds1", result.name)
+
+ def test_get_datastore_with_regex_error(self):
+ # Test with a regex that has no match
+ # Checks if code raises DatastoreNotFound with a specific message
+ datastore_invalid_regex = re.compile("unknown-ds")
+ exp_message = (_("Datastore regex %s did not match any datastores")
+ % datastore_invalid_regex.pattern)
+ fake_objects = fake.FakeRetrieveResult()
+ fake_objects.add_object(fake.Datastore("fake-ds0"))
+ fake_objects.add_object(fake.Datastore("fake-ds1"))
+ # assertRaisesRegExp would have been a good choice instead of
+ # try/catch block, but it's available only from Py 2.7.
+ try:
+ ds_util.get_datastore(
+ fake.FakeObjectRetrievalSession(fake_objects), None, None,
+ datastore_invalid_regex)
+ except exception.DatastoreNotFound as e:
+ self.assertEqual(exp_message, e.args[0])
+ else:
+ self.fail("DatastoreNotFound Exception was not raised with "
+ "message: %s" % exp_message)
+
+ def test_get_datastore_without_datastore(self):
+
+ self.assertRaises(exception.DatastoreNotFound,
+ ds_util.get_datastore,
+ fake.FakeObjectRetrievalSession(None), host="fake-host")
+
+ self.assertRaises(exception.DatastoreNotFound,
+ ds_util.get_datastore,
+ fake.FakeObjectRetrievalSession(None), cluster="fake-cluster")
+
+ def test_get_datastore_no_host_in_cluster(self):
+ self.assertRaises(exception.DatastoreNotFound,
+ ds_util.get_datastore,
+ fake.FakeObjectRetrievalSession(""), 'fake_cluster')
+
+ def test_get_datastore_inaccessible_ds(self):
+ data_store = fake.Datastore()
+ data_store.set("summary.accessible", False)
+
+ fake_objects = fake.FakeRetrieveResult()
+ fake_objects.add_object(data_store)
+
+ self.assertRaises(exception.DatastoreNotFound,
+ ds_util.get_datastore,
+ fake.FakeObjectRetrievalSession(fake_objects))
+
class DatastoreTestCase(test.NoDBTestCase):
def test_ds(self):