Getting flavor info from OOF
[vfc/gvnfm/vnflcm.git] / lcm / lcm / samples / tests.py
1 # Copyright 2017 ZTE Corporation.
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 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import mock
16 import unittest
17 import json
18 from rest_framework.test import APIClient
19 from rest_framework import status
20 from lcm.pub.utils import restcall
21
22 inst_res_url = "/api/vnflcm/v1/resources/inst"
23 term_res_url = "/api/vnflcm/v1/resources/term"
24 inst_res_data = {
25     "vdus": [
26         {
27             "type": "tosca.nodes.nfv.Vdu.Compute",
28             "description": "vbng",
29             "vdu_id": "VDU_vbng_0",
30             "virtual_compute": {
31                 "virtual_memory": {
32                     "virtual_mem_size": "4096 MB",
33                     "vdu_memory_requirements": {
34                         "memoryPageSize": "2 MB",
35                         "numberOfPages": "1024"
36                     }
37                 },
38                 "virtual_cpu": {
39                     "num_virtual_cpu": "2",
40                     "cpu_architecture": "generic"
41                 },
42             },
43             "virtual_storage": {
44                 "type_of_storage": "root",
45                 "size_of_storage": "40 GB"
46             },
47             "artifacts": [
48                 {
49                     "artifact_name": "sw_image",
50                     "deploy_path": "",
51                     "type": "tosca.artifacts.nfv.SwImage",
52                     "repository": "",
53                     "file": "ubuntu_16.04"
54                 },
55             ],
56             "image_file": "cirros.img",
57             "local_storages": [
58                 "intel_local_storages_1"
59             ],
60             "dependencies": [
61             ],
62             "vls": [
63                 "vl_vNat"
64             ],
65             "cps": [
66                 "cp_vNat"
67             ],
68             "properties": {
69                 "user_data": "just test user data",
70                 "meta_data": "just test meta data",
71                 "inject_files": [
72                     {
73                         "source_path": "artifacts/keys/authorized_keys",
74                         "source_data_base64": "c3NoLXJzYSBBQUFBQjNOemFDMXljMkVBQUFBREFRQUJBQUFCQVFER3g2U0tyQXVDejFWOEtHZXZa\ndWVrc0xkV29QV0pQNnozcjI5WjdUbVBWRU9qTSs3UElQZVNzMkJWUngzcm5IWkJBbGFzTXJaK2ZK\nQlMyNXRzOXZmQytJdGV6UWFoL2hyOXZya213eENSNTRMYjg0cG9XK3NUb1BlRjZpNWVaWTdXK2pX\nSmZMYUZTRng5ZDJ2cDR6ZXMvZk9sVDNOdllDWGJuLzNRZHJ5UW9HbDdWRkk4b2VtWnlwVmNpa1pY\nRWxKZWVLZ0FWZFN3bnJ6dXF0Ty90bWJYY0FlU2JZdlZqa2k4eXdZY3NXTVZNWVdVV2hoKzFCQUI2\na1huVHNJV3F6cnEwUGZ2eSs4MVdEd3RpcXNxbWQ5M0hZOGhFMHNjQnJYRkJaelFTL0FZZklGQmxF\ndUZOZExjemNobnRqYlowbjdkbURYazh6SHRDWllOazdrd2I4ay8gbHVsaWFuaGFvQGxsdS1udWMx\nCg==\n",
75                         "dest_path": "/home/ubuntu/.ssh/authorized_keys"
76                     }
77                 ],
78                 "configurable_properties": {
79                     "test": {
80                         "additional_vnfc_configurable_properties": {
81                             "aaa": "1"
82                         }
83                     }
84                 },
85                 "name": "vNat",
86                 "location_info": {
87                     "vimid": "f1e33529-4a88-4155-9d7a-893cf2c80527",
88                     "tenant": "vnfm",
89                     "availability_zone": "zone1",
90                     "host": "host1",
91                     "vnfId": "",
92                     "vnfName": "",
93                     "cloudOwner": "",
94                     "cloudRegionId": "",
95                     "vduInfo": [
96                         {
97                             "vduName": "VDU_vbng_0",
98                             "flavorName": "flavor_1",
99                             "directive": "",
100                         },
101                     ]
102                 },
103                 "descrption": "the virtual machine of vNat",
104                 "boot_order": [
105                     "vNAT_Storage"
106                 ]
107             }
108         }
109     ],
110     "volume_storages": [
111         {
112             "volume_storage_id": "volume_storage1",
113             "description": "",
114             "properties": {
115                 "size": "100 GB",
116                 "volume_id": "",
117                 "volume_name": "volumeStorage1",
118                 "custom_volume_type": "type1",
119                 "disk_type": "data",
120                 "delete_on_termination_vm": True,
121                 "location_info": {
122                     "vimid": "f1e33529-4a88-4155-9d7a-893cf2c80527",
123                     "tenant": "vnfm",
124                     "availability_zone": "zone1"
125                 },
126                 "is_predefined": False,
127                 "is_shared": False,
128             },
129             "image_file": [
130                 "volume_image"
131             ]
132         }
133     ],
134     "local_storages": [
135         {
136             "local_storage_id": "omp_local_disk1",
137             "description": "",
138             "properties": {
139                 "size": "100 GB",
140                 "disk_type": "root",
141             },
142         }
143     ],
144     "routers": [
145     ],
146     "image_files": [
147         {
148             "description": "",
149             "properties": {
150                 "operating_system": "unbant",
151                 "sw_image": "/swimages/xenial-snat.qcow2",
152                 "name": "cirros.img",
153                 "checksum": "5000",
154                 "min_ram": "1 GB",
155                 "disk_format": "qcow2",
156                 "version": "1.0",
157                 "container_format": "bare",
158                 "min_disk": "10 GB",
159                 "size": "10 GB"
160             },
161             "image_file_id": "cirros.img"
162         }
163     ],
164     "vnf_exposed": {
165         "external_cps": [
166             {
167                 "key_name": "sriov_plane",
168                 "cp_id": "sriov-support"
169             }
170         ],
171         "forward_cps": [
172         ]
173     },
174     'vls': [
175         {
176             "vl_id": "vl_vNat",
177             "description": "",
178             "properties": {
179                 "connectivity_type": {
180                     "layer_protocol": "ipv4"
181                 },
182                 "vl_profile": {
183                     "networkName": "vNat_big",
184                     "networkType": "vlan",
185                     "physicalNetwork": "physnet1",
186                     "segmentationId": "30",
187                     "vlanTransparent": False,
188                     "cidr": "10.43.38.0/24",
189                     "gatewayIp": "10.43.38.1",
190                     "dhcpEnabled": False,
191                     "startIp": "10.43.38.2",
192                     "endIp": "10.43.38.254"
193                 },
194                 "is_predefined": False,
195                 "vendor": "zte",
196                 "netmask": "255.255.255.0",
197                 "mtu": 1500,
198                 "vds_name": "vds1",
199                 "host_routes": [
200                     {
201                         "destination": "10.43.26.0/24",
202                         "nexthop": "10.41.23.1"
203                     }
204                 ],
205                 "location_info": {
206                     "vimid": "f1e33529-4a88-4155-9d7a-893cf2c80527",
207                     "tenant": "vnfm"
208                 },
209                 "cloud_type": "IaaS"
210             },
211             "route_id": "router01",
212             "route_external": False
213         }
214     ],
215     "cps": [
216         {
217
218             "properties": {
219                 "name": "vNat_big_subnet_port",
220                 "protocol_data": [
221                     {
222                         "address_data": {
223                             "l3_address_data": {
224                                 "fixed_ip_address": ["10.43.38.253"]
225                             },
226                         },
227                     }
228                 ],
229                 "vnic_type": "direct",
230                 "role": "root",
231                 "virtual_network_interface_requirements": [
232                     {
233                         "network_interface_requirements": {
234                             "interfaceType": '{"schema-version": "0", "schema-location":"", "platform-id": "generic", "mandatory": false, "configuration-value": "SR-IOV"}'
235                         },
236                         "support_mandatory": False,
237                         "name": "sriov-support",
238                         "description": "sriov"
239                     }
240                 ],
241                 "layer_protocol": "ipv4",
242                 "description": "sriov port"
243             },
244             "vl_id": "vl_vNat",
245             "description": "",
246             "cp_id": "cp_vNat",
247             "vdu_id": "VDU_vbng_0"
248         }
249     ],
250     "metadata": {
251         "vnfSoftwareVersion": "1.0.0",
252         "vnfmType": "gvnfmdriver",
253         "vnfProductName": "openNAT",
254         "localizationLanguage": "[english, chinese]",
255         "vnfProvider": "intel",
256         "vnfmInfo": "GVNFM",
257         "defaultLocalizationLanguage": "english",
258         "csarVersion": "1.0.0",
259         "vnfdId": "openNAT-1.0",
260         "csarProvider": "intel",
261         "vnfProductInfoDescription": "openNAT",
262         "version": "1.0.0",
263         "vnfdVersion": "1.0.0",
264         "vnfProductInfoName": "openNAT",
265         "vendor": "intel",
266         "csarType": "NFAR",
267         "id": "openNAT-1.0"
268     }
269 }
270 NOT_PREDEFINED = 1
271 term_res_data = {
272     "volume": [{"is_predefined": NOT_PREDEFINED, "vim_id": "1", "tenant_id": "2", "res_id": "3"}],
273     "network": [{"is_predefined": NOT_PREDEFINED, "vim_id": "2", "tenant_id": "3", "res_id": "4"}],
274     "subnet": [{"is_predefined": NOT_PREDEFINED, "vim_id": "3", "tenant_id": "4", "res_id": "5"}],
275     "port": [{"is_predefined": NOT_PREDEFINED, "vim_id": "4", "tenant_id": "5", "res_id": "6"}],
276     "flavor": [{"is_predefined": NOT_PREDEFINED, "vim_id": "5", "tenant_id": "6", "res_id": "7"}],
277     "vm": [{"is_predefined": NOT_PREDEFINED, "vim_id": "6", "tenant_id": "7", "res_id": "8"}]
278 }
279
280
281 c0_data_get_tenant_id = {
282     "tenants": [
283         {
284             "id": "1",
285             "name": "vnfm",
286         }
287     ]
288 }
289
290 c1_data_create_volume = {
291     "vimId": "f1e33529-4a88-4155-9d7a-893cf2c80527",
292     "nodeId": "",
293     "id": "234",
294     "name": "volume1"
295 }
296
297 c1_data_get_volume = {
298     "vimId": "f1e33529-4a88-4155-9d7a-893cf2c80527",
299     "status": "AVAILABLE"
300 }
301
302 c2_data_create_network = {
303     "vimId": "f1e33529-4a88-4155-9d7a-893cf2c80527",
304     "nodeId": "",
305     "id": "234"
306 }
307
308 c3_data_create_subnet = {
309     "vimId": "f1e33529-4a88-4155-9d7a-893cf2c80527",
310     "id": "345"
311 }
312
313 c4_data_create_port = {
314     "vimId": "f1e33529-4a88-4155-9d7a-893cf2c80527",
315     "nodeId": "",
316     "id": "456"
317 }
318 c5_data_create_flavor = {
319     "vimId": "f1e33529-4a88-4155-9d7a-893cf2c80527",
320     "nodeId": "",
321     "id": "6456",
322 }
323
324 c5_data_get_flavor = {
325     "flavor": [
326         {
327             "flavor-id": "111111",
328             "flavor-name": "flavor_1",
329         }
330     ]
331 }
332
333 c6_data_list_image = {
334     "images": [
335         {
336             "name": "ubuntu_16.04",
337             "id": "678"
338         }
339     ]
340 }
341
342 c6_data_create_vm = {
343     "id": "789",
344     "name": "vm"
345 }
346
347 c6_data_get_vm = {
348     "id": "789",
349     "status": "Active",
350     "name": "vm"
351 }
352
353
354 class SampleViewTest(unittest.TestCase):
355     def setUp(self):
356         self.client = APIClient()
357
358     def tearDown(self):
359         pass
360
361     def test_sample(self):
362         response = self.client.get("/samples/")
363         self.assertEqual(status.HTTP_200_OK, response.status_code, response.content)
364         resp_data = json.loads(response.content)
365         self.assertEqual({"status": "active"}, resp_data)
366
367     @mock.patch.object(restcall, 'call_req')
368     def test_inst_res(self, mock_call_req):
369         r0_data_get_tenant_id = [0, json.JSONEncoder().encode(c0_data_get_tenant_id), '200']
370         r1_data_create_volume = [0, json.JSONEncoder().encode(c1_data_create_volume), '200']
371         r1_data_get_volume = [0, json.JSONEncoder().encode(c1_data_get_volume), '200']
372         r2_data_create_network = [0, json.JSONEncoder().encode(c2_data_create_network), '200']
373         r3_data_create_subnet = [0, json.JSONEncoder().encode(c3_data_create_subnet), '200']
374         r4_data_create_port = [0, json.JSONEncoder().encode(c4_data_create_port), '200']
375         r5_data_get_flavor = [0, json.JSONEncoder().encode(c5_data_get_flavor), '200']
376         r5_data_create_flavor = [0, json.JSONEncoder().encode(c5_data_create_flavor), '200']
377         r6_data_list_image = [0, json.JSONEncoder().encode(c6_data_list_image), '200']
378         r6_data_create_vm = [0, json.JSONEncoder().encode(c6_data_create_vm), '200']
379         r6_data_get_vm = [0, json.JSONEncoder().encode(c6_data_get_vm), '200']
380
381         mock_call_req.side_effect = [r0_data_get_tenant_id,
382                                      r1_data_create_volume, r1_data_get_volume,
383                                      r2_data_create_network,
384                                      r3_data_create_subnet,
385                                      r4_data_create_port,
386                                      r5_data_get_flavor, r5_data_create_flavor,
387                                      r6_data_list_image, r6_data_create_vm, r6_data_get_vm]
388         resp = self.client.post(inst_res_url, data=json.dumps(inst_res_data), content_type='application/json')
389         self.failUnlessEqual(status.HTTP_204_NO_CONTENT, resp.status_code)
390
391     @mock.patch.object(restcall, 'call_req')
392     def test_term_res(self, mock_call_req):
393         r0_data_delete = [0, json.JSONEncoder().encode({"vim_id": "123"}), '200']
394         mock_call_req.side_effect = [r0_data_delete,
395                                      r0_data_delete,
396                                      r0_data_delete,
397                                      r0_data_delete,
398                                      r0_data_delete,
399                                      r0_data_delete,
400                                      r0_data_delete]
401         resp = self.client.post(term_res_url, data=json.dumps(term_res_data), content_type='application/json')
402         self.failUnlessEqual(status.HTTP_204_NO_CONTENT, resp.status_code)