Add tests for find_resource()

Add a couple of tests for utils.find_resource() for the odd resources
and managers without resource_class.

Change-Id: I2ed9b491d1361b5259b3a5f80b4fac787a7087c1
This commit is contained in:
Dean Troyer 2015-10-01 11:26:02 -05:00
parent 83282bc5e1
commit 97659adf53

View File

@ -20,6 +20,7 @@ import mock
from openstackclient.common import exceptions
from openstackclient.common import utils
from openstackclient.tests import fakes
from openstackclient.tests import utils as test_utils
PASSWORD = "Pa$$w0rd"
@ -27,6 +28,18 @@ WASSPORD = "Wa$$p0rd"
DROWSSAP = "dr0w$$aP"
class FakeOddballResource(fakes.FakeResource):
def get(self, attr):
"""get() is needed for utils.find_resource()"""
if attr == 'id':
return self.id
elif attr == 'name':
return self.name
else:
return None
class TestUtils(test_utils.TestCase):
def test_get_password_good(self):
@ -242,6 +255,47 @@ class TestFindResource(test_utils.TestCase):
self.manager.get.assert_called_with(self.name)
self.manager.find.assert_called_with(name=self.name)
def test_find_resource_silly_resource(self):
# We need a resource with no resource_class for this test, start fresh
self.manager = mock.Mock()
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
self.manager.find = mock.Mock(
side_effect=AttributeError(
"'Controller' object has no attribute 'find'",
)
)
silly_resource = FakeOddballResource(
None,
{'id': '12345', 'name': self.name},
loaded=True,
)
self.manager.list = mock.Mock(
return_value=[silly_resource, ],
)
result = utils.find_resource(self.manager, self.name)
self.assertEqual(silly_resource, result)
self.manager.get.assert_called_with(self.name)
self.manager.find.assert_called_with(name=self.name)
def test_find_resource_silly_resource_not_found(self):
# We need a resource with no resource_class for this test, start fresh
self.manager = mock.Mock()
self.manager.get = mock.Mock(side_effect=Exception('Boom!'))
self.manager.find = mock.Mock(
side_effect=AttributeError(
"'Controller' object has no attribute 'find'",
)
)
self.manager.list = mock.Mock(return_value=[])
result = self.assertRaises(exceptions.CommandError,
utils.find_resource,
self.manager,
self.name)
self.assertEqual("Could not find resource legos",
str(result))
self.manager.get.assert_called_with(self.name)
self.manager.find.assert_called_with(name=self.name)
def test_format_dict(self):
expected = "a='b', c='d', e='f'"
self.assertEqual(expected,