diff options
Diffstat (limited to 'azure/multicloud_azure/tests')
-rw-r--r-- | azure/multicloud_azure/tests/__init__.py | 0 | ||||
-rw-r--r-- | azure/multicloud_azure/tests/test_aai_client.py | 77 | ||||
-rw-r--r-- | azure/multicloud_azure/tests/test_flavor_view.py | 76 | ||||
-rw-r--r-- | azure/multicloud_azure/tests/test_restcall.py | 101 | ||||
-rw-r--r-- | azure/multicloud_azure/tests/test_syscomm.py | 37 |
5 files changed, 291 insertions, 0 deletions
diff --git a/azure/multicloud_azure/tests/__init__.py b/azure/multicloud_azure/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/azure/multicloud_azure/tests/__init__.py diff --git a/azure/multicloud_azure/tests/test_aai_client.py b/azure/multicloud_azure/tests/test_aai_client.py new file mode 100644 index 0000000..c782bd9 --- /dev/null +++ b/azure/multicloud_azure/tests/test_aai_client.py @@ -0,0 +1,77 @@ +# Copyright (c) 2018 Amdocs +# +# 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 mock +import unittest + +from multicloud_azure.pub.utils import restcall + + +class TestAAIClient(unittest.TestCase): + + def setUp(self): + self.view = restcall.AAIClient("vmware", "4.0") + + @mock.patch.object(restcall, "call_req") + def test_get_vim(self, mock_call): + mock_call.return_value = [0, '{"cloudOwner": "vmware"}'] + ret = self.view.get_vim(get_all=True) + expect_ret = {"cloudOwner": "vmware"} + self.assertEqual(expect_ret, ret) + + @mock.patch.object(restcall.AAIClient, "get_vim") + @mock.patch.object(restcall, "call_req") + def test_update_identity_url(self, mock_call, mock_getvim): + mock_getvim.return_value = {} + self.view.update_identity_url() + mock_call.assert_called_once() + + @mock.patch.object(restcall, "call_req") + def test_add_flavors(self, mock_call): + flavors = { + "flavors": [{ + "name": "m1.small", + "id": "1", + "vcpus": 1, + "ram": 512, + "disk": 10, + "ephemeral": 0, + "swap": 0, + "is_public": True, + "links": [{"href": "http://fake-url"}], + "is_disabled": False + }] + } + self.view.add_flavors(flavors) + 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() diff --git a/azure/multicloud_azure/tests/test_flavor_view.py b/azure/multicloud_azure/tests/test_flavor_view.py new file mode 100644 index 0000000..d946ece --- /dev/null +++ b/azure/multicloud_azure/tests/test_flavor_view.py @@ -0,0 +1,76 @@ +# Copyright (c) 2018 Amdocs +# +# 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 multicloud_azure.pub.msapi import extsys +from multicloud_azure.pub.vim.vimapi.compute import OperateFlavors +from multicloud_azure.swagger import compute_utils +from multicloud_azure.swagger.views.flavor.views import FlavorsView +from rest_framework import status + +VIM_INFO = {'cloud_extra_info': 1, 'username': 'user1', + 'password': '1234', 'default_tenant': 't1', + 'cloud_region_id': 'r1'} + + +class FlavorViewTest(unittest.TestCase): + + def setUp(self): + self.fsv = FlavorsView() + + def tearDown(self): + pass + + @mock.patch.object(compute_utils, 'convert_vmsize_aai') + @mock.patch.object(OperateFlavors.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").status_code) + + def test_vmsize_aai(self): + expected = { + 'name': "abc", + 'vcpus': 1, + 'ram': 123, + 'disk': 1234 + } + + class VmSize: + def __init__(self, name, number_of_cores, memory_in_mb, + os_disk_size_in_mb): + self.name = name + self.number_of_cores = number_of_cores + self.memory_in_mb = memory_in_mb + self.os_disk_size_in_mb = os_disk_size_in_mb + v1 = VmSize("abc", 1, 123, 1234) + self.assertEquals(expected, compute_utils.convert_vmsize_aai(v1)) diff --git a/azure/multicloud_azure/tests/test_restcall.py b/azure/multicloud_azure/tests/test_restcall.py new file mode 100644 index 0000000..9f60317 --- /dev/null +++ b/azure/multicloud_azure/tests/test_restcall.py @@ -0,0 +1,101 @@ +# Copyright (c) 2018 Amdocs +# 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 mock +import unittest +import urllib2 + +from multicloud_azure.pub.utils import restcall + + +class TestRestCall(unittest.TestCase): + + def test_combine_url(self): + url = ["http://a.com/test/", "http://a.com/test/", + "http://a.com/test", "http://a.com/test"] + res = ["/resource", "resource", "/resource", "resource"] + expected = "http://a.com/test/resource" + for i in range(len(url)): + self.assertEqual(expected, restcall.combine_url(url[i], res[i])) + + @mock.patch.object(restcall, "call_req") + def test_get_res_from_aai(self, mock_call): + res = "cloud-regions" + content = "" + expect_url = "https://aai.api.simpledemo.openecomp.org:8443/aai/v13" + expect_user = "AAI" + expect_pass = "AAI" + expect_headers = { + 'X-FromAppId': 'MultiCloud', + 'X-TransactionId': '9001', + 'content-type': 'application/json', + 'accept': 'application/json' + } + restcall.get_res_from_aai(res, content=content) + mock_call.assert_called_once_with( + expect_url, expect_user, expect_pass, restcall.rest_no_auth, + res, "GET", content, expect_headers) + + @mock.patch.object(restcall, "call_req") + def test_req_by_msb(self, mock_call): + res = "multicloud" + method = "GET" + content = "no content" + restcall.req_by_msb(res, method, content=content) + expect_url = "http://msb.onap.org:10080/" + mock_call.assert_called_once_with( + expect_url, "", "", restcall.rest_no_auth, res, method, + content) + + @mock.patch("httplib2.Http.request") + def test_call_req_success(self, mock_req): + mock_resp = { + "status": "200" + } + resp_content = "hello" + mock_req.return_value = mock_resp, resp_content + expect_ret = [0, resp_content, "200", mock_resp] + 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) diff --git a/azure/multicloud_azure/tests/test_syscomm.py b/azure/multicloud_azure/tests/test_syscomm.py new file mode 100644 index 0000000..12507a4 --- /dev/null +++ b/azure/multicloud_azure/tests/test_syscomm.py @@ -0,0 +1,37 @@ +# Copyright (c) 2018 Amdocs +# +# 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 multicloud_azure.pub.utils import syscomm + + +class SyscommTest(unittest.TestCase): + + def test_keystone_version(self): + url = "http://a.com/test" + version = "v3" + expected = "http://a.com/test/v3" + self.assertEquals(expected, syscomm.keystoneVersion(url, version)) + + def test_verify_keystone(self): + param = \ + { + "auth": { + "tenantName": "12345", + "passwordCredentials": { + "username": "admin", + "password": "admin" + } + } + } + self.assertEquals(True, syscomm.verifyKeystoneV2(param)) |