diff options
Diffstat (limited to 'vio')
-rw-r--r-- | vio/vio/tests/test_flavor_view.py | 46 | ||||
-rw-r--r-- | vio/vio/tests/test_limits_view.py | 7 | ||||
-rw-r--r-- | vio/vio/tests/test_server_view.py | 27 | ||||
-rw-r--r-- | vio/vio/tests/tests_registry_view.py | 61 |
4 files changed, 96 insertions, 45 deletions
diff --git a/vio/vio/tests/test_flavor_view.py b/vio/vio/tests/test_flavor_view.py index ad06f37..a344359 100644 --- a/vio/vio/tests/test_flavor_view.py +++ b/vio/vio/tests/test_flavor_view.py @@ -35,12 +35,12 @@ class FlavorViewTest(unittest.TestCase): @mock.patch.object(nova_utils, 'flavor_formatter') @mock.patch.object(OperateFlavors, 'find_flavor') + @mock.patch.object(OperateFlavors, 'get_flavor') @mock.patch.object(extsys, 'get_vim_by_id') - def test_flavor_list_view_fail(self, mock_vim_info, + def test_flavor_list_view_fail(self, mock_vim_info, mock_get_flavor, mock_find_flavor, mock_formatter): mock_vim_info.return_value = VIM_INFO - mock_formatter.return_value = {"vimName": "name", "vimId": 1} class Request: def __init__(self, query_params, body, method): @@ -48,12 +48,30 @@ class FlavorViewTest(unittest.TestCase): self.body = body self.method = method req = Request({'k': 'v'}, - json.dumps({'name': 'flavor-name', 'id': 1}), "POST") + json.dumps({'name': 'flavor-name', 'flavor_id': 1}), + "POST") self.assertEqual( status.HTTP_500_INTERNAL_SERVER_ERROR, self.fsv.post(req, "vimid", "tenantid").status_code) @mock.patch.object(nova_utils, 'flavor_formatter') + @mock.patch.object(OperateFlavors, 'get_flavor') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_flavor_get_fail(self, mock_vim_info, + mock_get_flavor, mock_formatter): + mock_vim_info.return_value = VIM_INFO + + mock_formatter.return_value = {"id": 1, "name": "nova"} + + class Request: + def __init__(self, query_params): + self.query_params = query_params + req = Request({'k': 'v'}) + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.fv.get(req, "vimid", "tenantid", "flavorid").status_code) + + @mock.patch.object(nova_utils, 'flavor_formatter') @mock.patch.object(OperateFlavors, 'list_flavors') @mock.patch.object(extsys, 'get_vim_by_id') def test_flavors_get_fail(self, mock_vim_info, @@ -102,22 +120,6 @@ class FlavorViewTest(unittest.TestCase): status.HTTP_200_OK, self.fv.get(req, "vimid", "tenantid", "flavorid").status_code) - @mock.patch.object(nova_utils, 'flavor_formatter') - @mock.patch.object(extsys, 'get_vim_by_id') - def test_flavor_get_fail(self, mock_vim_info, mock_formatter): - mock_vim_info.return_value = VIM_INFO - of = OperateFlavors() - of.get_flavor = mock.Mock([]) - mock_formatter.return_value = {"id": 1, "name": "nova"} - - class Request: - def __init__(self, query_params): - self.query_params = query_params - req = Request({'k': 'v'}) - self.assertEqual( - status.HTTP_500_INTERNAL_SERVER_ERROR, - self.fv.get(req, "vimid", "tenantid", "flavorid").status_code) - @mock.patch.object(OperateFlavors, 'delete_flavor') @mock.patch.object(extsys, 'get_vim_by_id') def test_flavor_delete_view(self, mock_vim_info, mock_delete_flavor): @@ -141,12 +143,12 @@ class FlavorViewTest(unittest.TestCase): status.HTTP_204_NO_CONTENT, self.fv.delete(req, "vimid", "tenantid", "flavorid").status_code) + @mock.patch.object(OperateFlavors, 'delete_flavor') @mock.patch.object(extsys, 'get_vim_by_id') - def test_flavor_delete_view_fail(self, mock_vim_info): + def test_flavor_delete_view_fail(self, mock_vim_info, mock_delete_flavor): mock_vim_info.return_value = VIM_INFO - of = OperateFlavors() - of.delete_flavor = mock.Mock([]) + mock_delete_flavor.side_effect = TypeError("wrong type") class Request: def __init__(self, query_params, method): diff --git a/vio/vio/tests/test_limits_view.py b/vio/vio/tests/test_limits_view.py index 953f3b5..6bfb67e 100644 --- a/vio/vio/tests/test_limits_view.py +++ b/vio/vio/tests/test_limits_view.py @@ -51,11 +51,12 @@ class LimitsViewTest(unittest.TestCase): self.lv.get(req, "vimid", "tenantid").status_code) @mock.patch.object(nova_utils, 'server_limits_formatter') + @mock.patch.object(OperateLimits, 'get_limits') @mock.patch.object(extsys, 'get_vim_by_id') - def test_server_get_limit_fail(self, mock_vim_info, mock_formatter): + def test_server_get_limit_fail(self, mock_vim_info, + mock_limits, mock_formatter): mock_vim_info.return_value = VIM_INFO - ol = OperateLimits() - ol.get_limits = mock.Mock({}) + mock_limits.side_effect = KeyError('wrong type') mock_formatter.return_value = {"name": "name1", "project_id": 1} class Request: diff --git a/vio/vio/tests/test_server_view.py b/vio/vio/tests/test_server_view.py index 236272f..f70fc80 100644 --- a/vio/vio/tests/test_server_view.py +++ b/vio/vio/tests/test_server_view.py @@ -87,26 +87,12 @@ class ServerViewTest(unittest.TestCase): self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, self.lsv.get(req, "vimid", "tenantid").status_code) - @mock.patch.object(nova_utils, 'server_formatter') - @mock.patch.object(OperateServers, 'list_servers') + @mock.patch.object(OperateServers, 'get_server') @mock.patch.object(extsys, 'get_vim_by_id') - def test_server_get_view_fail(self, mock_vim_info, - mock_servers, mock_formatter): + def test_server_get_view_fail(self, mock_vim_info, mock_servers): mock_vim_info.return_value = VIM_INFO - class Server: - def __init__(self, id, project_id, name, availability_zone): - self.id = id - self.project_id = project_id - self.name = name - self.availability_zone = availability_zone - s1 = Server(1, "p1", "name1", "nova") - s2 = Server(2, "p2", "name2", "nova") - servers = [s1, s2] - mock_servers.return_value = servers - os = OperateServers() - os.list_server_interfaces = mock.Mock(return_value=[]) - mock_formatter.return_value = servers + mock_servers.side_effect = TypeError('wrong type') class Request: def __init__(self, query_params): @@ -136,16 +122,17 @@ class ServerViewTest(unittest.TestCase): status.HTTP_204_NO_CONTENT, self.gsv.delete(req, "vimid", "tenantid", "serverid").status_code) + @mock.patch.object(OperateServers, 'delete_server') @mock.patch.object(extsys, 'get_vim_by_id') - def test_delete_server_view_fail(self, mock_vim_info): + def test_delete_server_view_fail(self, mock_vim_info, mock_delete_server): mock_vim_info.return_value = VIM_INFO - os = OperateServers() - os.delete_server = mock.Mock(return_value=[]) + mock_delete_server.side_effect = TypeError("wrong type") class Request: def __init__(self, query_params): self.query_params = query_params req = Request({'k': 'v'}) + self.assertEqual( status.HTTP_500_INTERNAL_SERVER_ERROR, self.gsv.delete(req, "vimid", "tenantid", "serverid").status_code) diff --git a/vio/vio/tests/tests_registry_view.py b/vio/vio/tests/tests_registry_view.py index 03999c3..9013332 100644 --- a/vio/vio/tests/tests_registry_view.py +++ b/vio/vio/tests/tests_registry_view.py @@ -18,6 +18,7 @@ from vio.swagger.views.registry.views import Registry from vio.pub.msapi import extsys from vio.pub.vim.vimapi.keystone.OperateTenant import OperateTenant +from vio.pub.vim.vimapi.glance.OperateImage import OperateImage VIM_INFO = {'vimId': 1, 'name': 'name1', 'userName': 'user1', 'password': '1234', 'url': 'abc', 'tenant': 't1'} @@ -52,3 +53,63 @@ class RegistryViewTest(unittest.TestCase): self.assertEqual( status.HTTP_200_OK, self.reg.delete(req, "vimid").status_code) + + @mock.patch.object(OperateTenant, 'get_projects') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_reg_get_tenants_view(self, mock_vim_info, mock_projects): + mock_vim_info.return_value = VIM_INFO + + class Project: + def __init__(self, id, name): + self.id = id + self.name = name + + def to_dict(self): + return {"name": self.name, "id": self.id} + p1 = Project(1, "p1") + p2 = Project(2, "p2") + projects = [p1, p2] + mock_projects.return_value = projects + + self.assertEqual( + {'tenants': [{'id': 1, 'name': 'p1'}, {'id': 2, 'name': 'p2'}]}, + self.reg._get_tenants("vimid")) + + @mock.patch.object(OperateTenant, 'get_projects') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_reg_get_tenants_view_fail(self, mock_vim_info, mock_projects): + mock_vim_info.return_value = VIM_INFO + + mock_projects.side_effect = TypeError("wrong type") + + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.reg._get_tenants("vimid").status_code) + + @mock.patch.object(OperateImage, 'get_vim_images') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_reg_get_images_view(self, mock_vim_info, mock_images): + mock_vim_info.return_value = VIM_INFO + + class Image: + def __init__(self, id, name): + self.id = id + self.name = name + + def to_dict(self): + return {"name": self.name, "id": self.id} + i1 = Image(1, "i1") + i2 = Image(2, "i2") + images = [i1, i2] + mock_images.return_value = images + + class Auth: + def __init__(self, id): + self.id = id + + def get(self, username): + self.username = username + a = Auth(1) + self.assertEqual( + {'images': [{'id': 1, 'name': 'i1'}, {'id': 2, 'name': 'i2'}]}, + self.reg._get_images(a)) |