diff options
author | Bin Sun <bins@vmware.com> | 2017-09-18 14:41:45 +0800 |
---|---|---|
committer | Bin Sun <bins@vmware.com> | 2017-09-18 14:42:10 +0800 |
commit | e388cd36954e84ca1bf912ac09ec982c0a2f2e53 (patch) | |
tree | 47284fcdd8402a05f7f50be37af8eb05ac6fdf91 | |
parent | 3280c32a96f7f7cfc54e6d80971b1e637439bd70 (diff) |
Add setting-coverage into VIO plugin for UT
also write some cases to increase the code coverage to 50%
Change-Id: I032c713d9811f123b74717742e04b55e334ab4e8
Issue-Id: MULTICLOUD-91
Signed-off-by: Bin Sun <bins@vmware.com>
-rw-r--r-- | vio/requirements.txt | 1 | ||||
-rw-r--r-- | vio/tox.ini | 15 | ||||
-rw-r--r-- | vio/vio/settings-cover.py | 20 | ||||
-rw-r--r-- | vio/vio/tests/__init__.py | 10 | ||||
-rw-r--r-- | vio/vio/tests/test_flavor_view.py | 158 | ||||
-rw-r--r-- | vio/vio/tests/test_limits_view.py | 67 | ||||
-rw-r--r-- | vio/vio/tests/test_network_view.py | 145 | ||||
-rw-r--r-- | vio/vio/tests/test_operate.py | 75 | ||||
-rw-r--r-- | vio/vio/tests/test_port_view.py | 164 | ||||
-rw-r--r-- | vio/vio/tests/test_server_view.py | 175 | ||||
-rw-r--r-- | vio/vio/tests/test_subnet_view.py | 170 | ||||
-rw-r--r-- | vio/vio/tests/test_tenant_view.py | 67 | ||||
-rw-r--r-- | vio/vio/tests/tests_registry_view.py | 54 |
13 files changed, 1120 insertions, 1 deletions
diff --git a/vio/requirements.txt b/vio/requirements.txt index ca74dbc..6cecedc 100644 --- a/vio/requirements.txt +++ b/vio/requirements.txt @@ -16,6 +16,7 @@ httplib2==0.9.2 openstacksdk==0.9.14 # for unit test +django-nose>=1.4.0 coverage==4.2 mock==2.0.0 unittest_xml_reporting==1.12.0 diff --git a/vio/tox.ini b/vio/tox.ini index bf5d800..966b7bb 100644 --- a/vio/tox.ini +++ b/vio/tox.ini @@ -7,8 +7,21 @@ downloadcache = ~/cache/pip [testenv] deps = -r{toxinidir}/requirements.txt -commands = coverage run --branch manage.py test vio +commands = + /usr/bin/find . -type f -name "*.py[c|o]" -delete + python manage.py test vio [testenv:pep8] deps=flake8 commands=flake8 + +[testenv:py27] +commands = + {[testenv]commands} + +[testenv:cover] +setenv= + DJANGO_SETTINGS_MODULE = vio.settings-cover +commands = + coverage erase + {[testenv]commands} diff --git a/vio/vio/settings-cover.py b/vio/vio/settings-cover.py new file mode 100644 index 0000000..781e0b3 --- /dev/null +++ b/vio/vio/settings-cover.py @@ -0,0 +1,20 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +from vio.settings import * # noqa +from vio.settings import INSTALLED_APPS + +INSTALLED_APPS.append('django_nose') + +TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' + +NOSE_ARGS = [ + '--with-coverage', + '--cover-package=vio', +] diff --git a/vio/vio/tests/__init__.py b/vio/vio/tests/__init__.py new file mode 100644 index 0000000..802f3fb --- /dev/null +++ b/vio/vio/tests/__init__.py @@ -0,0 +1,10 @@ +# Copyright (c) 2017 Wind River Systems, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/vio/vio/tests/test_flavor_view.py b/vio/vio/tests/test_flavor_view.py new file mode 100644 index 0000000..ad06f37 --- /dev/null +++ b/vio/vio/tests/test_flavor_view.py @@ -0,0 +1,158 @@ +# Copyright (c) 2017 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +import json +import mock +from rest_framework import status +from vio.swagger.views.flavor.views import FlavorsView, FlavorView + +from vio.pub.msapi import extsys +from vio.pub.vim.vimapi.nova.OperateFlavors import OperateFlavors +from vio.swagger import nova_utils + +VIM_INFO = {'vimId': 1, 'name': 'name1', 'userName': 'user1', + 'password': '1234', 'url': 'abc', 'tenant': 't1'} + + +class FlavorViewTest(unittest.TestCase): + + def setUp(self): + self.fsv = FlavorsView() + self.fv = FlavorView() + + def tearDown(self): + pass + + @mock.patch.object(nova_utils, 'flavor_formatter') + @mock.patch.object(OperateFlavors, 'find_flavor') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_flavor_list_view_fail(self, mock_vim_info, + 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): + self.query_params = query_params + self.body = body + self.method = method + req = Request({'k': 'v'}, + json.dumps({'name': 'flavor-name', '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, 'list_flavors') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_flavors_get_fail(self, mock_vim_info, + mock_flavors, mock_formatter): + mock_vim_info.return_value = VIM_INFO + + class Flavor: + def __init__(self, id, name): + self.id = id + self.name = name + f1 = Flavor(1, "f1") + f2 = Flavor(2, "f2") + flavors = [f1, f2] + mock_flavors.return_value = flavors + mock_formatter.return_value = flavors + + 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.fsv.get(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(self, mock_vim_info, mock_flavors, mock_formatter): + mock_vim_info.return_value = VIM_INFO + + class Flavor: + def __init__(self, id, name): + self.id = id + self.name = name + f1 = Flavor(1, "f1") + f2 = Flavor(2, "f2") + flavors = [f1, f2] + mock_flavors.return_value = flavors + 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_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): + + mock_vim_info.return_value = VIM_INFO + + class Flavor: + def __init__(self, id, name): + self.id = id + self.name = name + f = Flavor(1, "f1") + + mock_delete_flavor.return_value = f + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "DELETE") + self.assertEqual( + status.HTTP_204_NO_CONTENT, + self.fv.delete(req, "vimid", "tenantid", "flavorid").status_code) + + @mock.patch.object(extsys, 'get_vim_by_id') + def test_flavor_delete_view_fail(self, mock_vim_info): + + mock_vim_info.return_value = VIM_INFO + of = OperateFlavors() + of.delete_flavor = mock.Mock([]) + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "DELETE") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.fv.delete(req, "vimid", "tenantid", "flavorid").status_code) diff --git a/vio/vio/tests/test_limits_view.py b/vio/vio/tests/test_limits_view.py new file mode 100644 index 0000000..953f3b5 --- /dev/null +++ b/vio/vio/tests/test_limits_view.py @@ -0,0 +1,67 @@ +# Copyright (c) 2017 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +# import json +import mock +from rest_framework import status +from vio.swagger.views.limits.views import LimitsView + + +from vio.pub.msapi import extsys +from vio.swagger import nova_utils +from vio.pub.vim.vimapi.nova.OperateLimits import OperateLimits + +VIM_INFO = {'vimId': 1, 'name': 'name1', 'userName': 'user1', + 'password': '1234', 'url': 'abc', 'tenant': 't1'} + + +class LimitsViewTest(unittest.TestCase): + + def setUp(self): + self.lv = LimitsView() + + def tearDown(self): + pass + + @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(self, mock_vim_info, + mock_limits, mock_formatter): + mock_vim_info.return_value = VIM_INFO + mock_limits.return_value = {"name": "name1", "project_id": 1} + mock_formatter.return_value = {"name": "name1", "project_id": 1} + + class Request: + def __init__(self, query_params): + self.query_params = query_params + req = Request({'k': 'v'}) + self.assertEqual( + status.HTTP_200_OK, + self.lv.get(req, "vimid", "tenantid").status_code) + + @mock.patch.object(nova_utils, 'server_limits_formatter') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_server_get_limit_fail(self, mock_vim_info, mock_formatter): + mock_vim_info.return_value = VIM_INFO + ol = OperateLimits() + ol.get_limits = mock.Mock({}) + mock_formatter.return_value = {"name": "name1", "project_id": 1} + + 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.lv.get(req, "vimid", "tenantid").status_code) diff --git a/vio/vio/tests/test_network_view.py b/vio/vio/tests/test_network_view.py new file mode 100644 index 0000000..f3d191b --- /dev/null +++ b/vio/vio/tests/test_network_view.py @@ -0,0 +1,145 @@ +# Copyright (c) 2017 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +import json +import mock +from rest_framework import status +from vio.swagger.views.network.views import CreateNetworkView +from vio.swagger.views.network.views import DeleteNetworkView + +from vio.pub.utils import syscomm +from vio.pub.vim.vimapi.network.OperateNetwork import OperateNetwork + + +class NetworkViewTest(unittest.TestCase): + + def setUp(self): + self.cnv = CreateNetworkView() + self.dnv = DeleteNetworkView() + + def tearDown(self): + pass + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperateNetwork, 'list_networks') + def test_network_lists_view(self, mock_list_networks, mock_fun_name): + mock_list_networks.return_value = {'networks': [ + {"vimName": "name1", "vimId": 1}, + {"vimName": "name2", "vimId": 2}]} + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_200_OK, + self.cnv.get(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_network_lists_view_fail(self, mock_fun_name): + on = OperateNetwork() + on.list_networks = mock.Mock([]) + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.cnv.get(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperateNetwork, 'list_network') + def test_network_list_view_fail(self, mock_list_network, mock_fun_name): + + mock_fun_name.return_value = "fun_name" + mock_list_network.return_value = \ + {"vimName": "name", "vimId": 1, "returnCode": 0} + + class Request: + def __init__(self, query_params, body, method): + self.query_params = query_params + self.body = body + self.method = method + req = Request({'k': 'v'}, + json.dumps([{'name': 'net-name', 'id': 1}]), + "POST") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.cnv.post(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperateNetwork, 'list_network') + def test_network_list_view(self, mock_list_network, mock_fun_name): + mock_list_network.return_value = {'networks': [ + {"vimName": "name1", "vimId": 1}, + {"vimName": "name2", "vimId": 2}]} + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_200_OK, + self.dnv.get(req, "vimid", "tenantid", "networkid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_network_list_view_fail2(self, mock_fun_name): + on = OperateNetwork() + on.list_network = mock.Mock([]) + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.dnv.get(req, "vimid", "tenantid", "networkid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperateNetwork, 'delete_network') + def test_delete_network(self, mock_delete_network, mock_fun_name): + mock_delete_network.return_value = {"name": "name1", "id": 1} + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "DELETE") + self.assertEqual( + status.HTTP_204_NO_CONTENT, + self.dnv.delete(req, "vimid", "tenantid", "networkid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_delete_network_fail(self, mock_fun_name): + op = OperateNetwork() + op.delete_network = mock.Mock() + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "DELETE") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.dnv.delete(req, "vimid", "tenantid", "networkid").status_code) diff --git a/vio/vio/tests/test_operate.py b/vio/vio/tests/test_operate.py new file mode 100644 index 0000000..ac50bfe --- /dev/null +++ b/vio/vio/tests/test_operate.py @@ -0,0 +1,75 @@ +# Copyright (c) 2017 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +from vio.pub.vim.vimapi.network.OperateSubnet import OperateSubnet +from vio.pub.vim.vimapi.network.OperateNetwork import OperateNetwork +from vio.pub.vim.vimapi.network.OperatePort import OperatePort + + +class OperateTest(unittest.TestCase): + + def tearDown(self): + pass + + def test_operatenetwork(self): + + class Network: + def __init__(self, status): + self.status = status + network = Network("ok") + network.id = 1 + network.name = "name1" + network.project_id = 2 + network.provider_segmentation_id = 3 + network.provider_network_type = 4 + network.provider_physical_network = 5 + network.is_shared = 6 + network.is_router_external = 7 + op = OperateNetwork() + self.assertDictContainsSubset({"status": "ok"}, op._convert(network)) + + def test_operatesubnet(self): + class Subnet: + def __init__(self, status): + self.status = status + subnet = Subnet("ok") + subnet.id = 1 + subnet.network_id = 2 + subnet.name = "name1" + subnet.allocation_pools = "pool1" + subnet.gateway_ip = "0.0.0.0" + subnet.dns_nameservers = "server1" + subnet.ip_version = "1.0" + subnet.is_dhcp_enabled = "true" + subnet.host_routes = 1 + subnet.cidr = 1 + + os = OperateSubnet() + self.assertDictContainsSubset({"status": "ok"}, os._convert(subnet)) + + def test_operateport(self): + class Port: + def __init__(self, status): + self.status = status + port = Port("ok") + port.id = 1 + port.network_id = 1 + port.name = "name" + port.binding_vnic_type = 1 + port.mac_address = "aa:aa:aa:aa:aa:aa" + port.subnet_id = 2 + port.security_group_ids = 1 + port.fixed_ips = [{"subnet_id": 1}] + + op = OperatePort() + self.assertDictContainsSubset({"status": "ok"}, op._convert(port)) diff --git a/vio/vio/tests/test_port_view.py b/vio/vio/tests/test_port_view.py new file mode 100644 index 0000000..b8d433d --- /dev/null +++ b/vio/vio/tests/test_port_view.py @@ -0,0 +1,164 @@ +# Copyright (c) 2017 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +import json +import mock +from rest_framework import status +from vio.swagger.views.port.views import CreatePortView, DeletePortView + + +from vio.pub.utils import syscomm +from vio.pub.vim.vimapi.network.OperatePort import OperatePort + + +class PortViewTest(unittest.TestCase): + + def setUp(self): + self.cpv = CreatePortView() + self.dpv = DeletePortView() + + def tearDown(self): + pass + + @mock.patch.object(OperatePort, 'list_port') + @mock.patch.object(syscomm, 'fun_name') + def test_port_list_view(self, mock_list_port, mock_fun_name): + + # mock_fun_name.return_value = "fun_name" + mock_list_port.return_value = {'vimName': 'name1', 'vimId': 1} + + class Request: + def __init__(self, query_params, body, method): + self.query_params = query_params + self.body = body + self.method = method + req = Request({'k': 'v'}, + json.dumps( + {"subnetId": 1, "networkId": 2, "name": "name1"}), + "POST") + self.assertEqual( + status.HTTP_200_OK, + self.cpv.post(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_port_list_view_fail(self, mock_fun_name): + mock_fun_name.return_value = "fun_name" + op = OperatePort() + op.list_port = mock.Mock({}) + + class Request: + def __init__(self, query_params, body, method): + self.query_params = query_params + self.body = body + self.method = method + req = Request({'k': 'v'}, + json.dumps( + {"subnetId": 1, "networkId": 2, "name": "name1"}), + "POST") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.cpv.post(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperatePort, 'list_ports') + def test_port_lists_view(self, mock_list_ports, mock_fun_name): + mock_list_ports.return_value = {'ports': [ + {"vimName": "a", "vimId": 1}, + {"vimName": "b", "vimId": 2}]} + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_200_OK, + self.cpv.get(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_port_lists_view_fail(self, mock_fun_name): + op = OperatePort() + op.list_port = mock.Mock([]) + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.cpv.get(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperatePort, 'list_port') + def test_port_list_view2(self, mock_list_port, mock_fun_name): + mock_list_port.return_value = {"vimName": "a", "vimId": 1},\ + {"vimName": "b", "vimId": 2} + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_200_OK, + self.dpv.get(req, "vimid", "tenantid", "portid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_port_list_view_fail2(self, mock_fun_name): + on = OperatePort() + on.list_port = mock.Mock([]) + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.dpv.get(req, "vimid", "tenantid", "portid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperatePort, 'delete_port') + def test_delete_port(self, mock_delete_port, mock_fun_name): + mock_delete_port.return_value.return_value = {"name": "name1", + "id": 1} + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "DELETE") + self.assertEqual( + status.HTTP_204_NO_CONTENT, + self.dpv.delete(req, "vimid", "tenantid", "portid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_delete_port_fail(self, mock_fun_name): + op = OperatePort() + op.delete_port = mock.Mock() + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "DELETE") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.dpv.delete(req, "vimid", "tenantid", "portid").status_code) diff --git a/vio/vio/tests/test_server_view.py b/vio/vio/tests/test_server_view.py new file mode 100644 index 0000000..236272f --- /dev/null +++ b/vio/vio/tests/test_server_view.py @@ -0,0 +1,175 @@ +# Copyright (c) 2017 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +import json +import mock +from rest_framework import status +from vio.swagger.views.server.views import ListServersView, GetServerView + + +from vio.pub.msapi import extsys +from vio.swagger import nova_utils +from vio.pub.vim.vimapi.nova.OperateServers import OperateServers + +VIM_INFO = {'vimId': 1, 'name': 'name1', 'userName': 'user1', + 'password': '1234', 'url': 'abc', 'tenant': 't1'} + + +class ServerViewTest(unittest.TestCase): + + def setUp(self): + self.lsv = ListServersView() + self.gsv = GetServerView() + + def tearDown(self): + pass + + @mock.patch.object(nova_utils, 'server_formatter') + @mock.patch.object(OperateServers, 'list_server_interfaces') + @mock.patch.object(OperateServers, 'list_servers') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_server_get(self, mock_vim_info, mock_servers, + mock_interfaces, mock_formatter): + mock_vim_info.return_value = VIM_INFO + + class Server: + def __init__(self, id, name): + self.id = id + self.name = name + s1 = Server(1, "s1") + s2 = Server(2, "s2") + servers = [s1, s2] + mock_servers.return_value = servers + mock_interfaces.return_value = servers + mock_formatter.return_value = servers + + class Request: + def __init__(self, query_params): + self.query_params = query_params + req = Request({'k': 'v'}) + self.assertEqual( + status.HTTP_200_OK, + self.lsv.get(req, "vimid", "tenantid").status_code) + + @mock.patch.object(nova_utils, 'server_formatter') + @mock.patch.object(OperateServers, 'list_server_interfaces') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_server_get_fail(self, mock_vim_info, + mock_interfaces, mock_formatter): + mock_vim_info.return_value = VIM_INFO + + class Server: + def __init__(self, id, name): + self.id = id + self.name = name + s1 = Server(1, "s1") + s2 = Server(2, "s2") + servers = [s1, s2] + os = OperateServers() + os.list_servers = mock.Mock(return_value=[]) + mock_interfaces.return_value = servers + mock_formatter.return_value = servers + + 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.lsv.get(req, "vimid", "tenantid").status_code) + + @mock.patch.object(nova_utils, 'server_formatter') + @mock.patch.object(OperateServers, 'list_servers') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_server_get_view_fail(self, mock_vim_info, + mock_servers, mock_formatter): + 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 + + 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.get(req, "vimid", "tenant", "serverid").status_code) + + @mock.patch.object(OperateServers, 'delete_server') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_delete_server_view(self, mock_vim_info, mock_delete_server): + mock_vim_info.return_value = VIM_INFO + + class Project: + def __init__(self, id, name): + self.id = id + self.name = name + p1 = Project(1, "p1") + mock_delete_server.return_value = p1 + + class Request: + def __init__(self, query_params): + self.query_params = query_params + req = Request({'k': 'v'}) + self.assertEqual( + status.HTTP_204_NO_CONTENT, + self.gsv.delete(req, "vimid", "tenantid", "serverid").status_code) + + @mock.patch.object(extsys, 'get_vim_by_id') + def test_delete_server_view_fail(self, mock_vim_info): + mock_vim_info.return_value = VIM_INFO + os = OperateServers() + os.delete_server = mock.Mock(return_value=[]) + + 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) + + @mock.patch.object(nova_utils, 'server_formatter') + @mock.patch.object(OperateServers, 'find_server') + @mock.patch.object(OperateServers, 'get_server') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_server_post_fail(self, mock_vim_info, mock_get_servers, + mock_find_servers, mock_formatter): + mock_vim_info.return_value = VIM_INFO + mock_find_servers.return_value = {"id": 1, "name": "name1"} + mock_get_servers.return_value = {"id": 1, "name": "name1"} + mock_formatter.return_value = {"id": 1, "name": "name1"} + + class Request: + def __init__(self, query_params, body, method): + self.query_params = query_params + self.body = body + self.method = method + req = Request({'k': 'v'}, + json.dumps({"name": "abc", "networkId": 3, + "cidr": 2, "ipVersion": 1}), + "POST") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.lsv.post(req, "vimid", "tenantid").status_code) diff --git a/vio/vio/tests/test_subnet_view.py b/vio/vio/tests/test_subnet_view.py new file mode 100644 index 0000000..772bf53 --- /dev/null +++ b/vio/vio/tests/test_subnet_view.py @@ -0,0 +1,170 @@ +# Copyright (c) 2017 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +import json +import mock +from rest_framework import status +from vio.swagger.views.subnet.views import CreateSubnetView, DeleteSubnetView + + +from vio.pub.utils import syscomm +from vio.pub.vim.vimapi.network.OperateSubnet import OperateSubnet + + +class SubnetViewTest(unittest.TestCase): + + def setUp(self): + self.csv = CreateSubnetView() + self.dsv = DeleteSubnetView() + + def tearDown(self): + pass + + @mock.patch.object(OperateSubnet, 'list_subnet') + @mock.patch.object(syscomm, 'fun_name') + def test_subnet_list_view(self, mock_list_port, mock_fun_name): + + # mock_fun_name = "fun_name" + mock_list_port.return_value = {"vimName": "name", "vimId": 1, + "tenantId": 1} + + class Request: + def __init__(self, query_params, body, method): + self.query_params = query_params + self.body = body + self.method = method + req = Request({'k': 'v'}, + json.dumps({"name": "name1", "networkId": 3, + "cidr": 2, "ipVersion": 1}), + "POST") + self.assertEqual( + status.HTTP_200_OK, + self.csv.post(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperateSubnet, 'list_subnets') + def test_subnet_lists_view(self, mock_list_subnets, mock_fun_name): + + class Subnet: + def __init__(self, id, name): + self.id = id + self.name = name + s1 = Subnet(1, "s1") + s2 = Subnet(2, "s2") + subnets = [s1, s2] + mock_list_subnets.return_value = subnets + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_200_OK, + self.csv.get(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_subnet_lists_view_fail(self, mock_fun_name): + + os = OperateSubnet() + os.list_subnets = mock.Mock(return_value=[]) + + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.csv.get(req, "vimid", "tenantid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperateSubnet, 'list_subnet') + def test_subnet_get_view(self, mock_list_subnet, mock_fun_name): + + class Subnet: + def __init__(self, id, name): + self.id = id + self.name = name + s = Subnet(1, "s1") + + mock_list_subnet.return_value = s + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_200_OK, + self.dsv.get(req, "vimId", "tenantid", "subnetid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_subnet_get_view_fail(self, mock_fun_name): + + os = OperateSubnet() + os.list_subnet = mock.Mock(return_value=[]) + + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "GET") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.dsv.get(req, "vimid", "tenantid", "subnetid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + @mock.patch.object(OperateSubnet, 'delete_subnet') + def test_subnet_delete_view(self, mock_delete_subnet, mock_fun_name): + + class Subnet: + def __init__(self, id, name): + self.id = id + self.name = name + s = Subnet(1, "s1") + + mock_delete_subnet.return_value = s + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "DELETE") + self.assertEqual( + status.HTTP_204_NO_CONTENT, + self.dsv.delete(req, "vimid", "tenantid", "subnetid").status_code) + + @mock.patch.object(syscomm, 'fun_name') + def test_subnet_delete_view_fail(self, mock_fun_name): + + os = OperateSubnet() + os.delete_subnet = mock.Mock(return_value=[]) + + mock_fun_name.return_value = "fun_name" + + class Request: + def __init__(self, query_params, method): + self.query_params = query_params + self.method = method + req = Request({'k': 'v'}, "DELETE") + self.assertEqual( + status.HTTP_500_INTERNAL_SERVER_ERROR, + self.dsv.delete(req, "vimid", "tenantid", "subnetid").status_code) diff --git a/vio/vio/tests/test_tenant_view.py b/vio/vio/tests/test_tenant_view.py new file mode 100644 index 0000000..75df3ec --- /dev/null +++ b/vio/vio/tests/test_tenant_view.py @@ -0,0 +1,67 @@ +# Copyright (c) 2017 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +import mock +from rest_framework import status +from vio.swagger.views.tenant.views import ListTenantsView + + +from vio.pub.msapi import extsys +from vio.pub.vim.vimapi.keystone.OperateTenant import OperateTenant + +VIM_INFO = {'vimId': 1, 'name': 'name1', 'userName': 'user1', + 'password': '1234', 'url': 'abc', 'tenant': 't1'} + + +class TenantViewTest(unittest.TestCase): + + def setUp(self): + self.lvt = ListTenantsView() + + def tearDown(self): + pass + + @mock.patch.object(OperateTenant, 'get_projects') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_tenant_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 + p1 = Project(1, "p1") + p2 = Project(2, "p2") + projects = [p1, p2] + mock_projects.return_value = projects + + class Request: + def __init__(self, query_params): + self.query_params = query_params + req = Request({'query': 'param'}) + self.assertEqual( + status.HTTP_200_OK, self.lvt.get(req, "vimid").status_code) + + @mock.patch.object(extsys, 'get_vim_by_id') + def test_tenant_view_fail(self, mock_vim_info): + mock_vim_info.return_value = VIM_INFO + ot = OperateTenant() + ot.get_projects = mock.Mock(return_value=[]) + + class Request: + def __init__(self, query_params): + self.query_params = query_params + req = Request({'query': 'param'}) + self.assertEqual( + self.lvt.get(req, "vimid").status_code, + status.HTTP_500_INTERNAL_SERVER_ERROR) diff --git a/vio/vio/tests/tests_registry_view.py b/vio/vio/tests/tests_registry_view.py new file mode 100644 index 0000000..03999c3 --- /dev/null +++ b/vio/vio/tests/tests_registry_view.py @@ -0,0 +1,54 @@ +# Copyright (c) 2017 VMware, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + +import unittest +import mock +from rest_framework import status +from vio.swagger.views.registry.views import Registry + + +from vio.pub.msapi import extsys +from vio.pub.vim.vimapi.keystone.OperateTenant import OperateTenant + +VIM_INFO = {'vimId': 1, 'name': 'name1', 'userName': 'user1', + 'password': '1234', 'url': 'abc', 'tenant': 't1'} + + +class RegistryViewTest(unittest.TestCase): + + def setUp(self): + self.reg = Registry() + + def tearDown(self): + pass + + @mock.patch.object(OperateTenant, 'get_projects') + @mock.patch.object(extsys, 'get_vim_by_id') + def test_reg_delete_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 + p1 = Project(1, "p1") + p2 = Project(2, "p2") + projects = [p1, p2] + mock_projects.return_value = projects + + class Request: + def __init__(self, query_params): + self.query_params = query_params + req = Request({'k': 'v'}) + self.assertEqual( + status.HTTP_200_OK, + self.reg.delete(req, "vimid").status_code) |