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