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