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