summaryrefslogtreecommitdiff
path: root/openstackclient
diff options
context:
space:
mode:
Diffstat (limited to 'openstackclient')
-rw-r--r--openstackclient/common/utils.py4
-rw-r--r--openstackclient/tests/common/test_utils.py72
2 files changed, 76 insertions, 0 deletions
diff --git a/openstackclient/common/utils.py b/openstackclient/common/utils.py
index 7cd877ef..d7702a3f 100644
--- a/openstackclient/common/utils.py
+++ b/openstackclient/common/utils.py
@@ -74,6 +74,10 @@ def find_resource(manager, name_or_id):
msg = "No %s with a name or ID of '%s' exists." % \
(manager.resource_class.__name__.lower(), name_or_id)
raise exceptions.CommandError(msg)
+ if type(ex).__name__ == 'NoUniqueMatch':
+ msg = "More than one %s exists with the name '%s'." % \
+ (manager.resource_class.__name__.lower(), name_or_id)
+ raise exceptions.CommandError(msg)
else:
raise
diff --git a/openstackclient/tests/common/test_utils.py b/openstackclient/tests/common/test_utils.py
index 0ad4ca9b..3650746b 100644
--- a/openstackclient/tests/common/test_utils.py
+++ b/openstackclient/tests/common/test_utils.py
@@ -57,3 +57,75 @@ class TestUtils(test_utils.TestCase):
self.assertRaises(exceptions.CommandError,
utils.get_password,
mock_stdin)
+
+
+class NoUniqueMatch(Exception):
+ pass
+
+
+class TestFindResource(test_utils.TestCase):
+ def setUp(self):
+ super(TestFindResource, self).setUp()
+ self.name = 'legos'
+ self.expected = mock.Mock()
+ self.manager = mock.Mock()
+ self.manager.resource_class = mock.Mock()
+ self.manager.resource_class.__name__ = 'lego'
+
+ def test_find_resource_get_int(self):
+ self.manager.get = mock.Mock(return_value=self.expected)
+ result = utils.find_resource(self.manager, 1)
+ self.assertEqual(self.expected, result)
+ self.manager.get.assert_called_with(1)
+
+ def test_find_resource_get_int_string(self):
+ self.manager.get = mock.Mock(return_value=self.expected)
+ result = utils.find_resource(self.manager, "2")
+ self.assertEqual(self.expected, result)
+ self.manager.get.assert_called_with(2)
+
+ def test_find_resource_get_uuid(self):
+ uuid = '9a0dc2a0-ad0d-11e3-a5e2-0800200c9a66'
+ self.manager.get = mock.Mock(return_value=self.expected)
+ result = utils.find_resource(self.manager, uuid)
+ self.assertEqual(self.expected, result)
+ self.manager.get.assert_called_with(uuid)
+
+ def test_find_resource_get_whatever(self):
+ self.manager.get = mock.Mock(return_value=self.expected)
+ result = utils.find_resource(self.manager, 'whatever')
+ self.assertEqual(self.expected, result)
+ self.manager.get.assert_called_with('whatever')
+
+ def test_find_resource_find(self):
+ self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
+ self.manager.find = mock.Mock(return_value=self.expected)
+ result = utils.find_resource(self.manager, self.name)
+ self.assertEqual(self.expected, result)
+ self.manager.get.assert_called_with(self.name)
+ self.manager.find.assert_called_with(name=self.name)
+
+ def test_find_resource_find_not_found(self):
+ self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
+ self.manager.find = mock.Mock(side_effect=
+ exceptions.NotFound(404, "2"))
+ result = self.assertRaises(exceptions.CommandError,
+ utils.find_resource,
+ self.manager,
+ self.name)
+ self.assertEqual("No lego with a name or ID of 'legos' exists.",
+ str(result))
+ self.manager.get.assert_called_with(self.name)
+ self.manager.find.assert_called_with(display_name=self.name)
+
+ def test_find_resource_find_no_unique(self):
+ self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
+ self.manager.find = mock.Mock(side_effect=NoUniqueMatch())
+ result = self.assertRaises(exceptions.CommandError,
+ utils.find_resource,
+ self.manager,
+ self.name)
+ self.assertEqual("More than one lego exists with the name 'legos'.",
+ str(result))
+ self.manager.get.assert_called_with(self.name)
+ self.manager.find.assert_called_with(display_name=self.name)