1 # Copyright (c) 2018 Amdocs
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 from azure.pub.exceptions import VimDriverAzureException
18 from azure.pub.utils import restcall
21 class TestAAIClient(unittest.TestCase):
24 self.view = restcall.AAIClient("vmware", "4.0")
26 @mock.patch.object(restcall, "call_req")
27 def test_get_vim(self, mock_call):
28 mock_call.return_value = [0, '{"cloudOwner": "vmware"}']
29 ret = self.view.get_vim(get_all=True)
30 expect_ret = {"cloudOwner": "vmware"}
31 self.assertEqual(expect_ret, ret)
33 @mock.patch.object(restcall.AAIClient, "get_vim")
34 @mock.patch.object(restcall, "call_req")
35 def test_update_identity_url(self, mock_call, mock_getvim):
36 mock_getvim.return_value = {}
37 self.view.update_identity_url()
38 mock_call.assert_called_once()
40 @mock.patch.object(restcall, "call_req")
41 def test_add_tenants(self, mock_call):
42 tenants = {"tenants": [{"name": "admin", "id": "admin-id"}]}
43 self.view.add_tenants(tenants)
44 mock_call.assert_called_once()
46 @mock.patch.object(restcall, "call_req")
47 def test_add_flavors(self, mock_call):
58 "links": [{"href": "http://fake-url"}],
62 self.view.add_flavors(flavors)
63 mock_call.assert_called_once()
65 @mock.patch.object(restcall, "call_req")
66 def test_add_flavors_with_hpa(self, mock_call):
77 "links": [{"href": "http://fake-url"}],
82 self.view._get_ovsdpdk_capabilities = mock.MagicMock()
83 self.view._get_ovsdpdk_capabilities.return_value = {}
84 self.view.add_flavors(flavors)
85 mock_call.assert_called_once()
87 @mock.patch.object(restcall, "call_req")
88 def test_add_images(self, mock_call):
91 "name": "ubuntu-16.04",
95 self.view.add_images(images)
96 mock_call.assert_called_once()
98 @mock.patch.object(restcall, "call_req")
99 def test_add_networks(self, mock_call):
104 "segmentationId": 144
107 self.view.add_networks(networks)
108 mock_call.assert_called_once()
110 @mock.patch.object(restcall, "call_req")
111 def test_add_pservers(self, mock_call):
116 "local_disk_size": 1000,
117 "memory_size": 10240,
118 "host_ip": "10.0.0.7",
122 self.view.add_pservers(pservers)
123 self.assertEqual(mock_call.call_count, 2)
125 @mock.patch.object(restcall, "call_req")
126 def test_del_tenants(self, mock_call):
127 mock_call.return_value = [0]
131 "tenant-id": "tenant-id",
132 "resource-version": "version-1"
136 self.view._del_tenants(rsp)
137 mock_call.assert_called_once()
139 @mock.patch.object(restcall, "call_req")
140 def test_del_flavors(self, mock_call):
141 mock_call.return_value = [0]
145 "flavor-id": "fake-id",
146 "resource-version": "fake-version"
150 self.view._del_flavors(rsp)
151 mock_call.assert_called_once()
153 @mock.patch.object(restcall, "call_req")
154 def test_del_images(self, mock_call):
155 mock_call.return_value = [0]
159 "image-id": "fake-id",
160 "resource-version": "fake-version"
164 self.view._del_images(rsp)
165 mock_call.assert_called_once()
167 @mock.patch.object(restcall, "call_req")
168 def test_del_networks(self, mock_call):
169 mock_call.return_value = [0]
173 "network-uuid": "fake-id",
174 "resource-version": "fake-version"
178 self.view._del_networks(rsp)
179 mock_call.assert_called_once()
181 @mock.patch.object(restcall, "call_req")
182 def test_del_azs(self, mock_call):
183 mock_call.return_value = [0]
185 "availability-zones": {
186 "availability-zone": [{
187 "availability-zone-name": "fake-name",
188 "resource-version": "fake-version"
192 self.view._del_azs(rsp)
193 mock_call.assert_called_once()
195 @mock.patch.object(restcall, "call_req")
196 def test_del_hpa(self, mock_call):
197 mock_call.return_value = [0]
200 "hpa-capabilities": {
202 "resource-version": "v1",
203 "hpa-capability-id": "id2"
207 self.view._del_hpa(rsp)
208 mock_call.assert_called_once()
210 @mock.patch.object(restcall, "call_req")
211 def test_del_vim(self, mock_call):
213 "resource-version": "1"
215 self.view.get_vim = mock.MagicMock()
216 self.view.get_vim.return_value = resp
217 mock_call.return_value = [0, "", "", ""]
218 self.view.delete_vim()
219 mock_call.assert_called_once()
221 @mock.patch.object(restcall, "call_req")
222 def test_del_vim_fail(self, mock_call):
224 "resource-version": "1"
226 self.view.get_vim = mock.MagicMock()
227 self.view.get_vim.return_value = resp
228 mock_call.return_value = [1, "", "", ""]
229 self.assertRaises(VimDriverAzureException, self.view.delete_vim)
231 @mock.patch.object(restcall, "call_req")
232 def test_update_vim(self, mock_call):
234 "resource-version": "1"
236 self.view.get_vim = mock.MagicMock()
237 self.view.get_vim.return_value = resp
245 self.view.update_vim(content)
246 mock_call.assert_called_once()
248 @mock.patch.object(restcall, "call_req")
249 def test_get_hpa(self, mock_call):
250 self.view._get_hpa_basic_capabilities = mock.MagicMock()
251 self.view._get_hpa_basic_capabilities.return_value = {"hpa": "basic"}
252 self.view._get_cpupinning_capabilities = mock.MagicMock()
253 self.view._get_cpupinning_capabilities.return_value = {"hpa": "basic"}
254 self.view._get_cputopology_capabilities = mock.MagicMock()
255 self.view._get_cputopology_capabilities.return_value = {"hpa": "basic"}
256 self.view._get_hugepages_capabilities = mock.MagicMock()
257 self.view._get_hugepages_capabilities.return_value = {"hpa": "basic"}
258 self.view._get_numa_capabilities = mock.MagicMock()
259 self.view._get_numa_capabilities.return_value = {"hpa": "basic"}
260 self.view._get_storage_capabilities = mock.MagicMock()
261 self.view._get_storage_capabilities.return_value = {"hpa": "basic"}
262 self.view._get_instruction_set_capabilities = mock.MagicMock()
263 self.view._get_instruction_set_capabilities.return_value = {
265 self.view._get_pci_passthrough_capabilities = mock.MagicMock()
266 self.view._get_pci_passthrough_capabilities.return_value = {
268 self.view._get_ovsdpdk_capabilities = mock.MagicMock()
269 self.view._get_ovsdpdk_capabilities.return_value = {"hpa": "basic"}
270 ret = self.view._get_hpa_capabilities({"extra_specs": {}})
271 self.assertEqual([{"hpa": "basic"}]*9, ret)
273 @mock.patch.object(restcall, "call_req")
274 def test_get_hpa_basic(self, mock_call):
279 ret = self.view._get_hpa_basic_capabilities(flavor)
280 self.assertEqual(len(ret["hpa-feature-attributes"]), 2)
282 @mock.patch.object(restcall, "call_req")
283 def test_get_hpa_cpupin(self, mock_call):
285 "hw:cpu_policy": "cpu_policy",
286 "hw:cpu_thread_policy": "thread_policy"
288 ret = self.view._get_cpupinning_capabilities(extra)
289 self.assertEqual(len(ret["hpa-feature-attributes"]), 2)
291 @mock.patch.object(restcall, "call_req")
292 def test_get_hpa_cputopo(self, mock_call):
298 ret = self.view._get_cputopology_capabilities(extra)
299 self.assertEqual(len(ret["hpa-feature-attributes"]), 3)
301 @mock.patch.object(restcall, "call_req")
302 def test_get_hpa_hugepage_large(self, mock_call):
304 "hw:mem_page_size": "large"
306 ret = self.view._get_hugepages_capabilities(extra)
308 "2", ret["hpa-feature-attributes"][0]["hpa-attribute-value"])
310 @mock.patch.object(restcall, "call_req")
311 def test_get_hpa_hugepage_small(self, mock_call):
313 "hw:mem_page_size": "small"
315 ret = self.view._get_hugepages_capabilities(extra)
317 "4", ret["hpa-feature-attributes"][0]["hpa-attribute-value"])
319 @mock.patch.object(restcall, "call_req")
320 def test_get_hpa_hugepage_int(self, mock_call):
322 "hw:mem_page_size": 8,
324 ret = self.view._get_hugepages_capabilities(extra)
326 "8", ret["hpa-feature-attributes"][0]["hpa-attribute-value"])
328 @mock.patch.object(restcall, "call_req")
329 def test_get_hpa_hugepage_any(self, mock_call):
331 "hw:mem_page_size": "any",
333 ret = self.view._get_hugepages_capabilities(extra)
334 self.assertEqual(0, len(ret["hpa-feature-attributes"]))
336 @mock.patch.object(restcall, "call_req")
337 def test_get_hpa_numa(self, mock_call):
341 "hw:numa_mem.0": 1024,
343 ret = self.view._get_numa_capabilities(extra)
344 self.assertEqual(3, len(ret["hpa-feature-attributes"]))
346 @mock.patch.object(restcall, "call_req")
347 def test_get_hpa_storage(self, mock_call):
351 ret = self.view._get_storage_capabilities(extra)
352 self.assertEqual(3, len(ret["hpa-feature-attributes"]))
354 @mock.patch.object(restcall, "call_req")
355 def test_get_hpa_instru(self, mock_call):
357 "hw:capabilities:cpu_info:features": "avx",
359 ret = self.view._get_instruction_set_capabilities(extra)
360 self.assertEqual(1, len(ret["hpa-feature-attributes"]))
362 @mock.patch.object(restcall, "call_req")
363 def test_get_hpa_pci(self, mock_call):
365 "pci_passthrough:alias": "gpu-nvidia-x86-0011-0022:1",
367 ret = self.view._get_pci_passthrough_capabilities(extra)
368 self.assertEqual(3, len(ret["hpa-feature-attributes"]))
370 @mock.patch.object(restcall, "call_req")
371 def test_get_hpa_dpdk(self, mock_call):
372 self.view.get_vim = mock.MagicMock()
373 self.view.get_vim.return_value = {
374 "cloud-extra-info": json.dumps({'ovsDpdk': {
375 'libname': 'generic', 'libversion': '17.04'}})
377 ret = self.view._get_ovsdpdk_capabilities()
378 self.assertEqual(1, len(ret["hpa-feature-attributes"]))