diff options
-rw-r--r-- | vio/vio/tests/test_aai_client.py | 198 | ||||
-rw-r--r-- | vio/vio/tests/test_fake_heat_view.py | 15 | ||||
-rw-r--r-- | vio/vio/tests/test_fake_identity_view.py | 17 | ||||
-rw-r--r-- | vio/vio/tests/test_fake_nova_view.py | 153 | ||||
-rw-r--r-- | vio/vio/tests/test_restcall.py | 35 |
5 files changed, 413 insertions, 5 deletions
diff --git a/vio/vio/tests/test_aai_client.py b/vio/vio/tests/test_aai_client.py index 80d5200..17a0047 100644 --- a/vio/vio/tests/test_aai_client.py +++ b/vio/vio/tests/test_aai_client.py @@ -13,6 +13,7 @@ import mock import unittest +from vio.pub.exceptions import VimDriverVioException from vio.pub.utils import restcall @@ -61,6 +62,28 @@ class TestAAIClient(unittest.TestCase): mock_call.assert_called_once() @mock.patch.object(restcall, "call_req") + def test_add_flavors_with_hpa(self, mock_call): + flavors = { + "flavors": [{ + "name": "onap.small", + "id": "1", + "vcpus": 1, + "ram": 512, + "disk": 10, + "ephemeral": 0, + "swap": 0, + "is_public": True, + "links": [{"href": "http://fake-url"}], + "is_disabled": False, + "extra_specs": {}, + }] + } + self.view._get_ovsdpdk_capabilities = mock.MagicMock() + self.view._get_ovsdpdk_capabilities.return_value = {} + self.view.add_flavors(flavors) + mock_call.assert_called_once() + + @mock.patch.object(restcall, "call_req") def test_add_images(self, mock_call): images = { "images": [{ @@ -167,3 +190,178 @@ class TestAAIClient(unittest.TestCase): } self.view._del_azs(rsp) mock_call.assert_called_once() + + @mock.patch.object(restcall, "call_req") + def test_del_hpa(self, mock_call): + mock_call.return_value = [0] + rsp = { + "flavor-id": "id1", + "hpa-capabilities": { + "hpa-capability": [{ + "resource-version": "v1", + "hpa-capability-id": "id2" + }] + } + } + self.view._del_hpa(rsp) + mock_call.assert_called_once() + + @mock.patch.object(restcall, "call_req") + def test_del_vim(self, mock_call): + resp = { + "resource-version": "1" + } + self.view.get_vim = mock.MagicMock() + self.view.get_vim.return_value = resp + mock_call.return_value = [0, "", "", ""] + self.view.delete_vim() + mock_call.assert_called_once() + + @mock.patch.object(restcall, "call_req") + def test_del_vim_fail(self, mock_call): + resp = { + "resource-version": "1" + } + self.view.get_vim = mock.MagicMock() + self.view.get_vim.return_value = resp + mock_call.return_value = [1, "", "", ""] + self.assertRaises(VimDriverVioException, self.view.delete_vim) + + @mock.patch.object(restcall, "call_req") + def test_update_vim(self, mock_call): + resp = { + "resource-version": "1" + } + self.view.get_vim = mock.MagicMock() + self.view.get_vim.return_value = resp + content = { + "tenants": [], + "images": [], + "flavors": [], + "networks": [], + "hypervisors": [] + } + self.view.update_vim(content) + mock_call.assert_called_once() + + @mock.patch.object(restcall, "call_req") + def test_get_hpa(self, mock_call): + self.view._get_hpa_basic_capabilities = mock.MagicMock() + self.view._get_hpa_basic_capabilities.return_value = {"hpa": "basic"} + self.view._get_cpupinning_capabilities = mock.MagicMock() + self.view._get_cpupinning_capabilities.return_value = {"hpa": "basic"} + self.view._get_cputopology_capabilities = mock.MagicMock() + self.view._get_cputopology_capabilities.return_value = {"hpa": "basic"} + self.view._get_hugepages_capabilities = mock.MagicMock() + self.view._get_hugepages_capabilities.return_value = {"hpa": "basic"} + self.view._get_numa_capabilities = mock.MagicMock() + self.view._get_numa_capabilities.return_value = {"hpa": "basic"} + self.view._get_storage_capabilities = mock.MagicMock() + self.view._get_storage_capabilities.return_value = {"hpa": "basic"} + self.view._get_instruction_set_capabilities = mock.MagicMock() + self.view._get_instruction_set_capabilities.return_value = { + "hpa": "basic"} + self.view._get_pci_passthrough_capabilities = mock.MagicMock() + self.view._get_pci_passthrough_capabilities.return_value = { + "hpa": "basic"} + self.view._get_ovsdpdk_capabilities = mock.MagicMock() + self.view._get_ovsdpdk_capabilities.return_value = {"hpa": "basic"} + ret = self.view._get_hpa_capabilities({"extra_specs": {}}) + self.assertEqual([{"hpa": "basic"}]*9, ret) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_basic(self, mock_call): + flavor = { + "vcpus": 1, + "ram": 1024 + } + ret = self.view._get_hpa_basic_capabilities(flavor) + self.assertEqual(len(ret["hpa-feature-attributes"]), 2) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_cpupin(self, mock_call): + extra = { + "hw:cpu_policy": "cpu_policy", + "hw:cpu_thread_policy": "thread_policy" + } + ret = self.view._get_cpupinning_capabilities(extra) + self.assertEqual(len(ret["hpa-feature-attributes"]), 2) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_cputopo(self, mock_call): + extra = { + "hw:cpu_sockets": 2, + "hw:cpu_cores": 2, + "hw:cpu_threads": 4 + } + ret = self.view._get_cputopology_capabilities(extra) + self.assertEqual(len(ret["hpa-feature-attributes"]), 3) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_hugepage_large(self, mock_call): + extra = { + "hw:mem_page_size": "large" + } + ret = self.view._get_hugepages_capabilities(extra) + self.assertIn( + "2", ret["hpa-feature-attributes"][0]["hpa-attribute-value"]) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_hugepage_small(self, mock_call): + extra = { + "hw:mem_page_size": "small" + } + ret = self.view._get_hugepages_capabilities(extra) + self.assertIn( + "4", ret["hpa-feature-attributes"][0]["hpa-attribute-value"]) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_hugepage_int(self, mock_call): + extra = { + "hw:mem_page_size": 8, + } + ret = self.view._get_hugepages_capabilities(extra) + self.assertIn( + "8", ret["hpa-feature-attributes"][0]["hpa-attribute-value"]) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_hugepage_any(self, mock_call): + extra = { + "hw:mem_page_size": "any", + } + ret = self.view._get_hugepages_capabilities(extra) + self.assertEqual(0, len(ret["hpa-feature-attributes"])) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_numa(self, mock_call): + extra = { + "hw:numa_nodes": 1, + "hw:numa_cpus.0": 1, + "hw:numa_mem.0": 1024, + } + ret = self.view._get_numa_capabilities(extra) + self.assertEqual(3, len(ret["hpa-feature-attributes"])) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_storage(self, mock_call): + extra = { + "disk": 10, + } + ret = self.view._get_storage_capabilities(extra) + self.assertEqual(3, len(ret["hpa-feature-attributes"])) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_instru(self, mock_call): + extra = { + "hw:capabilities:cpu_info:features": "avx", + } + ret = self.view._get_instruction_set_capabilities(extra) + self.assertEqual(1, len(ret["hpa-feature-attributes"])) + + @mock.patch.object(restcall, "call_req") + def test_get_hpa_pci(self, mock_call): + extra = { + "pci_passthrough:alias": "gpu-nvidia-x86-0011-0022:1", + } + ret = self.view._get_pci_passthrough_capabilities(extra) + self.assertEqual(3, len(ret["hpa-feature-attributes"])) diff --git a/vio/vio/tests/test_fake_heat_view.py b/vio/vio/tests/test_fake_heat_view.py index 3cdb41a..5e87a61 100644 --- a/vio/vio/tests/test_fake_heat_view.py +++ b/vio/vio/tests/test_fake_heat_view.py @@ -67,18 +67,23 @@ class TestFakeHeatService(unittest.TestCase): def setUp(self): self.view = views.FakeHeatService() - @mock.patch.object(fakeResponse, "getAllStacks") - def test_get_heat_stacks(self, mock_getAllStacks): + def test_get_heat_stack(self): req = mock.Mock() req.META = { "HTTP_X_AUTH_TOKEN": Token } - mock_getAllStacks.return_value = { - "stack": "1234abcd" - } resp = self.view.get(req, "1234abcd", STACK_NAME, STACK_ID) self.assertEqual(200, resp.status_code) + def test_list_stacks(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + resp = self.view.get(req, "1234abcd") + self.assertEqual(200, resp.status_code) + self.assertEqual(1, len(resp.data['stacks'])) + @mock.patch.object(fakeResponse, "createStack") def test_create_service_preview(self, mock_createStack): req = mock.Mock() diff --git a/vio/vio/tests/test_fake_identity_view.py b/vio/vio/tests/test_fake_identity_view.py index e27a8c4..2dde2d6 100644 --- a/vio/vio/tests/test_fake_identity_view.py +++ b/vio/vio/tests/test_fake_identity_view.py @@ -40,6 +40,23 @@ class TestFakeProjects(unittest.TestCase): resp = self.view.get(req, "1234abcd") self.assertEqual(200, resp.status_code) + def test_list_projects(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + resp = self.view.get(req) + self.assertEqual(200, resp.status_code) + self.assertEqual(3, len(resp.data['projects'])) + + def test_list_projects_unauth(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": "invalid" + } + resp = self.view.get(req) + self.assertEqual(401, resp.status_code) + class TestFakeToken(unittest.TestCase): diff --git a/vio/vio/tests/test_fake_nova_view.py b/vio/vio/tests/test_fake_nova_view.py index c1c725d..f07aa47 100644 --- a/vio/vio/tests/test_fake_nova_view.py +++ b/vio/vio/tests/test_fake_nova_view.py @@ -10,6 +10,7 @@ # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +import json import mock import unittest @@ -29,6 +30,9 @@ class TestFakeNovaServer(unittest.TestCase): def setUp(self): self.view = views.FakeNovaServer() + def tearDown(self): + fakeResponse.serverMapps[Server]['status'] = "ACTIVE" + @mock.patch.object(fakeResponse, "show_serverDetail") def test_get_server(self, mock_show_serverDetail): req = mock.Mock() @@ -51,6 +55,146 @@ class TestFakeNovaServer(unittest.TestCase): resp = self.view.delete(req, "abcd", Server) self.assertEqual(204, resp.status_code) + def test_list_servers(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + resp = self.view.get(req, "abcd") + self.assertEqual(200, resp.status_code) + + def test_list_servers_unauth(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": "invalid" + } + resp = self.view.get(req, "abcd") + self.assertEqual(401, resp.status_code) + + def test_operate_server_stop(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + req.body = json.dumps({ + "os-stop": {} + }) + resp = self.view.post(req, "abcd", Server) + self.assertEqual(202, resp.status_code) + self.assertEqual( + "POWERING_OFF", fakeResponse.serverMapps[Server]['status']) + + def test_operate_server_start(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + req.body = json.dumps({ + "os-start": {} + }) + fakeResponse.serverMapps[Server]['status'] = "SHUTDOWN" + resp = self.view.post(req, "abcd", Server) + self.assertEqual(202, resp.status_code) + self.assertEqual( + "POWERING_ON", fakeResponse.serverMapps[Server]['status']) + + def test_operate_server_resume(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + req.body = json.dumps({ + "resume": {} + }) + fakeResponse.serverMapps[Server]['status'] = "SUSPENDED" + resp = self.view.post(req, "abcd", Server) + self.assertEqual(202, resp.status_code) + self.assertEqual( + "ACTIVE", fakeResponse.serverMapps[Server]['status']) + + def test_operate_server_suspend(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + req.body = json.dumps({ + "suspend": {} + }) + resp = self.view.post(req, "abcd", Server) + self.assertEqual(202, resp.status_code) + self.assertEqual( + "SUSPENDED", fakeResponse.serverMapps[Server]['status']) + + def test_operate_server_unpause(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + req.body = json.dumps({ + "unpause": {} + }) + fakeResponse.serverMapps[Server]['status'] = "PAUSED" + resp = self.view.post(req, "abcd", Server) + self.assertEqual(202, resp.status_code) + self.assertEqual( + "ACTIVE", fakeResponse.serverMapps[Server]['status']) + + def test_operate_server_reboot(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + req.body = json.dumps({ + "reboot": {} + }) + resp = self.view.post(req, "abcd", Server) + self.assertEqual(202, resp.status_code) + self.assertEqual( + "REBOOTING", fakeResponse.serverMapps[Server]['status']) + + def test_operate_server_unsupport(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + req.body = json.dumps({ + "invalid": {} + }) + resp = self.view.post(req, "abcd", Server) + self.assertEqual(405, resp.status_code) + + def test_create_server(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + req.body = json.dumps({ + "server": { + "name": "new_name" + } + }) + resp = self.view.post(req, "abcd") + self.assertEqual(202, resp.status_code) + server_id = resp.data['server']['id'] + self.assertEqual( + "BUILDING", fakeResponse.serverMapps[server_id]['status']) + self.assertEqual( + "new_name", fakeResponse.serverMapps[server_id]['name']) + + +class TestFakeNovaHypervisorUptime(unittest.TestCase): + + def setUp(self): + self.view = views.FakeNovaHypervisorsUptime() + + def test_get_hypervisor_uptime(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + resp = self.view.get(req, "tenant1", "hypervisor1") + self.assertEqual(200, resp.status_code) + class TestFakeNovaHypervisors(unittest.TestCase): @@ -123,6 +267,15 @@ class TestFakeFlavorDetail(unittest.TestCase): resp = self.view.get(req, "abcd", "1234") self.assertEqual(200, resp.status_code) + def test_get_flavors_detail(self): + req = mock.Mock() + req.META = { + "HTTP_X_AUTH_TOKEN": Token + } + resp = self.view.get(req, "abcd", "detail") + self.assertEqual(200, resp.status_code) + self.assertEqual(6, len(resp.data["flavors"])) + class TestFakeFlavorList(unittest.TestCase): diff --git a/vio/vio/tests/test_restcall.py b/vio/vio/tests/test_restcall.py index 051dddf..b04aec1 100644 --- a/vio/vio/tests/test_restcall.py +++ b/vio/vio/tests/test_restcall.py @@ -10,6 +10,7 @@ import mock import unittest +import urllib2 from vio.pub.utils import restcall @@ -64,3 +65,37 @@ class TestRestCall(unittest.TestCase): ret = restcall.call_req("http://onap.org/", "user", "pass", restcall.rest_no_auth, "vim", "GET") self.assertEqual(expect_ret, ret) + + @mock.patch("httplib2.Http.request") + def test_call_req_not_200(self, mock_req): + mock_resp = { + "status": "404" + } + resp_content = "hello" + mock_req.return_value = mock_resp, resp_content + expect_ret = [1, resp_content, "404", mock_resp] + ret = restcall.call_req("http://onap.org/", "user", "pass", + restcall.rest_no_auth, "vim", "GET") + self.assertEqual(expect_ret, ret) + + @mock.patch("traceback.format_exc") + @mock.patch("sys.exc_info") + @mock.patch("httplib2.Http.request") + def test_call_req_response_not_ready(self, mock_req, mock_sys, + mock_traceback): + mock_sys.return_value = "httplib.ResponseNotReady" + mock_req.side_effect = [Exception("httplib.ResponseNotReady")] * 3 + expect_ret = [1, "Unable to connect to http://onap.org/vim", "", ""] + ret = restcall.call_req("http://onap.org/", "user", "pass", + restcall.rest_no_auth, "vim", "GET") + self.assertEqual(expect_ret, ret) + self.assertEqual(3, mock_req.call_count) + + @mock.patch("httplib2.Http.request") + def test_call_req_url_err(self, mock_req): + urlerr = urllib2.URLError("urlerror") + mock_req.side_effect = [urlerr] + expect_ret = [2, str(urlerr), "", ""] + ret = restcall.call_req("http://onap.org/", "user", "pass", + restcall.rest_no_auth, "vim", "GET") + self.assertEqual(expect_ret, ret) |