Adoption of base framework code for azure plugin
[multicloud/azure.git] / azure / azure / tests / test_aai_client.py
1 # Copyright (c) 2018 Amdocs
2 #
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:
6 #
7 #       http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
12
13 import json
14 import mock
15 import unittest
16
17 from azure.pub.exceptions import VimDriverAzureException
18 from azure.pub.utils import restcall
19
20
21 class TestAAIClient(unittest.TestCase):
22
23     def setUp(self):
24         self.view = restcall.AAIClient("vmware", "4.0")
25
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)
32
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()
39
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()
45
46     @mock.patch.object(restcall, "call_req")
47     def test_add_flavors(self, mock_call):
48         flavors = {
49             "flavors": [{
50                 "name": "m1.small",
51                 "id": "1",
52                 "vcpus": 1,
53                 "ram": 512,
54                 "disk": 10,
55                 "ephemeral": 0,
56                 "swap": 0,
57                 "is_public": True,
58                 "links": [{"href": "http://fake-url"}],
59                 "is_disabled": False
60             }]
61         }
62         self.view.add_flavors(flavors)
63         mock_call.assert_called_once()
64
65     @mock.patch.object(restcall, "call_req")
66     def test_add_flavors_with_hpa(self, mock_call):
67         flavors = {
68             "flavors": [{
69                 "name": "onap.small",
70                 "id": "1",
71                 "vcpus": 1,
72                 "ram": 512,
73                 "disk": 10,
74                 "ephemeral": 0,
75                 "swap": 0,
76                 "is_public": True,
77                 "links": [{"href": "http://fake-url"}],
78                 "is_disabled": False,
79                 "extra_specs": {},
80             }]
81         }
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()
86
87     @mock.patch.object(restcall, "call_req")
88     def test_add_images(self, mock_call):
89         images = {
90             "images": [{
91                 "name": "ubuntu-16.04",
92                 "id": "image-id"
93             }]
94         }
95         self.view.add_images(images)
96         mock_call.assert_called_once()
97
98     @mock.patch.object(restcall, "call_req")
99     def test_add_networks(self, mock_call):
100         networks = {
101             "networks": [{
102                 "name": "net-1",
103                 "id": "net-id",
104                 "segmentationId": 144
105             }]
106         }
107         self.view.add_networks(networks)
108         mock_call.assert_called_once()
109
110     @mock.patch.object(restcall, "call_req")
111     def test_add_pservers(self, mock_call):
112         pservers = {
113             "hypervisors": [{
114                 "name": "compute-1",
115                 "vcpus": 100,
116                 "local_disk_size": 1000,
117                 "memory_size": 10240,
118                 "host_ip": "10.0.0.7",
119                 "id": "compute-1-id"
120             }]
121         }
122         self.view.add_pservers(pservers)
123         self.assertEqual(mock_call.call_count, 2)
124
125     @mock.patch.object(restcall, "call_req")
126     def test_del_tenants(self, mock_call):
127         mock_call.return_value = [0]
128         rsp = {
129             "tenants": {
130                 "tenant": [{
131                     "tenant-id": "tenant-id",
132                     "resource-version": "version-1"
133                 }]
134             }
135         }
136         self.view._del_tenants(rsp)
137         mock_call.assert_called_once()
138
139     @mock.patch.object(restcall, "call_req")
140     def test_del_flavors(self, mock_call):
141         mock_call.return_value = [0]
142         rsp = {
143             "flavors": {
144                 "flavor": [{
145                     "flavor-id": "fake-id",
146                     "resource-version": "fake-version"
147                 }]
148             }
149         }
150         self.view._del_flavors(rsp)
151         mock_call.assert_called_once()
152
153     @mock.patch.object(restcall, "call_req")
154     def test_del_images(self, mock_call):
155         mock_call.return_value = [0]
156         rsp = {
157             "images": {
158                 "image": [{
159                     "image-id": "fake-id",
160                     "resource-version": "fake-version"
161                 }]
162             }
163         }
164         self.view._del_images(rsp)
165         mock_call.assert_called_once()
166
167     @mock.patch.object(restcall, "call_req")
168     def test_del_networks(self, mock_call):
169         mock_call.return_value = [0]
170         rsp = {
171             "oam-networks": {
172                 "oam-network": [{
173                     "network-uuid": "fake-id",
174                     "resource-version": "fake-version"
175                 }]
176             }
177         }
178         self.view._del_networks(rsp)
179         mock_call.assert_called_once()
180
181     @mock.patch.object(restcall, "call_req")
182     def test_del_azs(self, mock_call):
183         mock_call.return_value = [0]
184         rsp = {
185             "availability-zones": {
186                 "availability-zone": [{
187                     "availability-zone-name": "fake-name",
188                     "resource-version": "fake-version"
189                 }]
190             }
191         }
192         self.view._del_azs(rsp)
193         mock_call.assert_called_once()
194
195     @mock.patch.object(restcall, "call_req")
196     def test_del_hpa(self, mock_call):
197         mock_call.return_value = [0]
198         rsp = {
199             "flavor-id": "id1",
200             "hpa-capabilities": {
201                 "hpa-capability": [{
202                     "resource-version": "v1",
203                     "hpa-capability-id": "id2"
204                 }]
205             }
206         }
207         self.view._del_hpa(rsp)
208         mock_call.assert_called_once()
209
210     @mock.patch.object(restcall, "call_req")
211     def test_del_vim(self, mock_call):
212         resp = {
213             "resource-version": "1"
214         }
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()
220
221     @mock.patch.object(restcall, "call_req")
222     def test_del_vim_fail(self, mock_call):
223         resp = {
224             "resource-version": "1"
225         }
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)
230
231     @mock.patch.object(restcall, "call_req")
232     def test_update_vim(self, mock_call):
233         resp = {
234             "resource-version": "1"
235         }
236         self.view.get_vim = mock.MagicMock()
237         self.view.get_vim.return_value = resp
238         content = {
239             "tenants": [],
240             "images": [],
241             "flavors": [],
242             "networks": [],
243             "hypervisors": []
244         }
245         self.view.update_vim(content)
246         mock_call.assert_called_once()
247
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 = {
264             "hpa": "basic"}
265         self.view._get_pci_passthrough_capabilities = mock.MagicMock()
266         self.view._get_pci_passthrough_capabilities.return_value = {
267             "hpa": "basic"}
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)
272
273     @mock.patch.object(restcall, "call_req")
274     def test_get_hpa_basic(self, mock_call):
275         flavor = {
276             "vcpus": 1,
277             "ram": 1024
278         }
279         ret = self.view._get_hpa_basic_capabilities(flavor)
280         self.assertEqual(len(ret["hpa-feature-attributes"]), 2)
281
282     @mock.patch.object(restcall, "call_req")
283     def test_get_hpa_cpupin(self, mock_call):
284         extra = {
285             "hw:cpu_policy": "cpu_policy",
286             "hw:cpu_thread_policy": "thread_policy"
287         }
288         ret = self.view._get_cpupinning_capabilities(extra)
289         self.assertEqual(len(ret["hpa-feature-attributes"]), 2)
290
291     @mock.patch.object(restcall, "call_req")
292     def test_get_hpa_cputopo(self, mock_call):
293         extra = {
294             "hw:cpu_sockets": 2,
295             "hw:cpu_cores": 2,
296             "hw:cpu_threads": 4
297         }
298         ret = self.view._get_cputopology_capabilities(extra)
299         self.assertEqual(len(ret["hpa-feature-attributes"]), 3)
300
301     @mock.patch.object(restcall, "call_req")
302     def test_get_hpa_hugepage_large(self, mock_call):
303         extra = {
304             "hw:mem_page_size": "large"
305         }
306         ret = self.view._get_hugepages_capabilities(extra)
307         self.assertIn(
308             "2", ret["hpa-feature-attributes"][0]["hpa-attribute-value"])
309
310     @mock.patch.object(restcall, "call_req")
311     def test_get_hpa_hugepage_small(self, mock_call):
312         extra = {
313             "hw:mem_page_size": "small"
314         }
315         ret = self.view._get_hugepages_capabilities(extra)
316         self.assertIn(
317             "4", ret["hpa-feature-attributes"][0]["hpa-attribute-value"])
318
319     @mock.patch.object(restcall, "call_req")
320     def test_get_hpa_hugepage_int(self, mock_call):
321         extra = {
322             "hw:mem_page_size": 8,
323         }
324         ret = self.view._get_hugepages_capabilities(extra)
325         self.assertIn(
326             "8", ret["hpa-feature-attributes"][0]["hpa-attribute-value"])
327
328     @mock.patch.object(restcall, "call_req")
329     def test_get_hpa_hugepage_any(self, mock_call):
330         extra = {
331             "hw:mem_page_size": "any",
332         }
333         ret = self.view._get_hugepages_capabilities(extra)
334         self.assertEqual(0, len(ret["hpa-feature-attributes"]))
335
336     @mock.patch.object(restcall, "call_req")
337     def test_get_hpa_numa(self, mock_call):
338         extra = {
339             "hw:numa_nodes": 1,
340             "hw:numa_cpus.0": 1,
341             "hw:numa_mem.0": 1024,
342         }
343         ret = self.view._get_numa_capabilities(extra)
344         self.assertEqual(3, len(ret["hpa-feature-attributes"]))
345
346     @mock.patch.object(restcall, "call_req")
347     def test_get_hpa_storage(self, mock_call):
348         extra = {
349             "disk": 10,
350         }
351         ret = self.view._get_storage_capabilities(extra)
352         self.assertEqual(3, len(ret["hpa-feature-attributes"]))
353
354     @mock.patch.object(restcall, "call_req")
355     def test_get_hpa_instru(self, mock_call):
356         extra = {
357             "hw:capabilities:cpu_info:features": "avx",
358         }
359         ret = self.view._get_instruction_set_capabilities(extra)
360         self.assertEqual(1, len(ret["hpa-feature-attributes"]))
361
362     @mock.patch.object(restcall, "call_req")
363     def test_get_hpa_pci(self, mock_call):
364         extra = {
365             "pci_passthrough:alias": "gpu-nvidia-x86-0011-0022:1",
366         }
367         ret = self.view._get_pci_passthrough_capabilities(extra)
368         self.assertEqual(3, len(ret["hpa-feature-attributes"]))
369
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'}})
376         }
377         ret = self.view._get_ovsdpdk_capabilities()
378         self.assertEqual(1, len(ret["hpa-feature-attributes"]))