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