Fix vfc-lcm/packages/tests pep8 issue
[vfc/nfvo/lcm.git] / lcm / packages / tests / test_sdc_nf.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 import json
15
16 import mock
17 from django.test import Client
18 from django.test import TestCase
19 from rest_framework import status
20
21 from lcm.packages.sdc_nf_package import SdcNfDistributeThread, SdcNfPkgDeleteThread
22 from lcm.pub.database.models import JobStatusModel, JobModel
23 from lcm.pub.database.models import NfPackageModel, NfInstModel
24 from lcm.pub.msapi import sdc
25 from lcm.pub.utils import restcall, toscaparser
26
27
28 class TestNfPackage(TestCase):
29     def setUp(self):
30         self.client = Client()
31         NfPackageModel.objects.filter().delete()
32         NfInstModel.objects.filter().delete()
33         JobModel.objects.filter().delete()
34         JobStatusModel.objects.filter().delete()
35         self.vnfd_data = {
36             "volume_storages": [
37                 {
38                     "properties": {
39                         "size_of_storage": {
40                             "factor": 10,
41                             "value": 10000000000,
42                             "unit": "GB",
43                             "unit_size": 1000000000
44                         },
45                         "type_of_storage": "volume",
46                         "rdma_enabled": False,
47                         "size": "10 GB"
48                     },
49                     "volume_storage_id": "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7",
50                     "description": ""
51                 }
52             ],
53             "inputs": {},
54             "vdus": [
55                 {
56                     "volume_storages": [
57                         "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7"
58                     ],
59                     "description": "",
60                     "dependencies": [],
61                     "vls": [],
62                     "properties": {
63                         "name": "vNat",
64                         "configurable_properties": {
65                             "test": {
66                                 "additional_vnfc_configurable_properties": {
67                                     "aaa": "1",
68                                     "bbb": "2",
69                                     "ccc": "3"
70                                 }
71                             }
72                         },
73                         "description": "the virtual machine of vNat",
74                         "nfvi_constraints": [
75                             "test"
76                         ],
77                         "boot_order": [
78                             "vNAT_Storage"
79                         ]
80                     },
81                     "vdu_id": "vdu_vNat",
82                     "artifacts": [
83                         {
84                             "artifact_name": "vNatVNFImage",
85                             "type": "tosca.artifacts.nfv.SwImage",
86                             "properties": {
87                                 "operating_system": "linux",
88                                 "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
89                                 "name": "vNatVNFImage",
90                                 "container_format": "bare",
91                                 "min_ram": "1 GB",
92                                 "disk_format": "qcow2",
93                                 "supported_virtualisation_environments": [
94                                     "test_0"
95                                 ],
96                                 "version": "1.0",
97                                 "checksum": "5000",
98                                 "min_disk": "10 GB",
99                                 "size": "10 GB"
100                             },
101                             "file": "/swimages/vRouterVNF_ControlPlane.qcow2"
102                         }
103                     ],
104                     "nfv_compute": {
105                         "flavor_extra_specs": {
106                             "hw:cpu_sockets": "2",
107                             "sw:ovs_dpdk": "true",
108                             "hw:cpu_threads": "2",
109                             "hw:numa_mem.1": "3072",
110                             "hw:numa_mem.0": "1024",
111                             "hw:numa_nodes": "2",
112                             "hw:numa_cpus.0": "0,1",
113                             "hw:numa_cpus.1": "2,3,4,5",
114                             "hw:cpu_cores": "2",
115                             "hw:cpu_threads_policy": "isolate"
116                         },
117                         "cpu_frequency": "2.4 GHz",
118                         "num_cpus": 2,
119                         "mem_size": "10 GB"
120                     },
121                     "local_storages": [],
122                     "image_file": "vNatVNFImage",
123                     "cps": []
124                 }
125             ],
126             "image_files": [
127                 {
128                     "properties": {
129                         "operating_system": "linux",
130                         "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
131                         "name": "vNatVNFImage",
132                         "container_format": "bare",
133                         "min_ram": "1 GB",
134                         "disk_format": "qcow2",
135                         "supported_virtualisation_environments": [
136                             "test_0"
137                         ],
138                         "version": "1.0",
139                         "checksum": "5000",
140                         "min_disk": "10 GB",
141                         "size": "10 GB"
142                     },
143                     "image_file_id": "vNatVNFImage",
144                     "description": ""
145                 }
146             ],
147             "routers": [],
148             "local_storages": [],
149             "vnf_exposed": {
150                 "external_cps": [
151                     {
152                         "key_name": "sriov_plane",
153                         "cp_id": "SRIOV_Port"
154                     }
155                 ],
156                 "forward_cps": []
157             },
158             "vls": [
159                 {
160                     "route_id": "",
161                     "vl_id": "sriov_link",
162                     "route_external": False,
163                     "description": "",
164                     "properties": {
165                         "vl_flavours": {
166                             "vl_id": "aaaa"
167                         },
168                         "connectivity_type": {
169                             "layer_protocol": "ipv4",
170                             "flow_pattern": "flat"
171                         },
172                         "description": "sriov_link",
173                         "test_access": [
174                             "test"
175                         ]
176                     }
177                 }
178             ],
179             "cps": [
180                 {
181                     "vl_id": "sriov_link",
182                     "vdu_id": "vdu_vNat",
183                     "description": "",
184                     "cp_id": "SRIOV_Port",
185                     "properties": {
186                         "address_data": [
187                             {
188                                 "address_type": "ip_address",
189                                 "l3_address_data": {
190                                     "ip_address_type": "ipv4",
191                                     "floating_ip_activated": False,
192                                     "number_of_ip_address": 1,
193                                     "ip_address_assignment": True
194                                 }
195                             }
196                         ],
197                         "description": "sriov port",
198                         "layer_protocol": "ipv4",
199                         "virtual_network_interface_requirements": [
200                             {
201                                 "requirement": {
202                                     "SRIOV": "true"
203                                 },
204                                 "support_mandatory": False,
205                                 "name": "sriov",
206                                 "description": "sriov"
207                             },
208                             {
209                                 "requirement": {
210                                     "SRIOV": "False"
211                                 },
212                                 "support_mandatory": False,
213                                 "name": "normal",
214                                 "description": "normal"
215                             }
216                         ],
217                         "role": "root",
218                         "bitrate_requirement": 10
219                     }
220                 }
221             ],
222             "metadata": {
223                 "vnfSoftwareVersion": "1.0.0",
224                 "vnfProductName": "zte",
225                 "localizationLanguage": [
226                     "english",
227                     "chinese"
228                 ],
229                 "vnfProvider": "zte",
230                 "vnfmInfo": "zte",
231                 "defaultLocalizationLanguage": "english",
232                 "vnfdId": "zte-hss-1.0",
233                 "id": "zte-hss-1.0",
234                 "vnfProductInfoDescription": "hss",
235                 "vnfdVersion": "1.0.0",
236                 "vnfProductInfoName": "hss"
237             }
238         }
239
240     def tearDown(self):
241         pass
242
243     def assert_job_result(self, job_id, job_progress, job_detail):
244         jobs = JobStatusModel.objects.filter(
245             jobid=job_id,
246             progress=job_progress,
247             descp=job_detail)
248         self.assertEqual(1, len(jobs))
249
250     @mock.patch.object(SdcNfDistributeThread, 'run')
251     def test_nf_pkg_distribute_normal(self, mock_run):
252         resp = self.client.post("/api/nslcm/v1/vnfpackage", {"csarId": "1", "vimIds": ["1"]}, format='json')
253         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
254
255     def test_nf_pkg_distribute_when_csar_already_exist(self):
256         NfPackageModel(uuid="1", nfpackageid="1", vnfdid="vcpe_vfw_zte_1_0").save()
257         SdcNfDistributeThread(csar_id="1", vim_ids=["1"], lab_vim_id="", job_id="2").run()
258         self.assert_job_result("2", 255, "NF CSAR(1) already exists.")
259
260     @mock.patch.object(restcall, 'call_req')
261     @mock.patch.object(sdc, 'download_artifacts')
262     @mock.patch.object(toscaparser, 'parse_vnfd')
263     def test_nf_pkg_distribute_when_vnfd_already_exist(self, mock_parse_vnfd, mock_download_artifacts,
264                                                        mock_call_req):
265         mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data)
266         mock_download_artifacts.return_value = "/home/hss.csar"
267         mock_call_req.return_value = [0, json.JSONEncoder().encode([{
268             "uuid": "1",
269             "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/hss.csar"
270         }]), '200']
271         NfPackageModel(uuid="2", nfpackageid="2", vnfdid="zte-hss-1.0").save()
272         SdcNfDistributeThread(csar_id="1", vim_ids=["1"], lab_vim_id="", job_id="2").run()
273         self.assert_job_result("2", 255, "NFD(zte-hss-1.0) already exists.")
274
275     @mock.patch.object(restcall, 'call_req')
276     @mock.patch.object(sdc, 'download_artifacts')
277     @mock.patch.object(toscaparser, 'parse_vnfd')
278     def test_nf_pkg_distribute_successfully(self, mock_parse_vnfd, mock_download_artifacts, mock_call_req):
279         mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data)
280         mock_download_artifacts.return_value = "/home/hss.csar"
281         mock_call_req.return_value = [0, json.JSONEncoder().encode([{
282             "uuid": "1",
283             "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/hss.csar"
284         }]), '200']
285         SdcNfDistributeThread(csar_id="1", vim_ids=["1"], lab_vim_id="", job_id="4").run()
286         self.assert_job_result("4", 100, "CSAR(1) distribute successfully.")
287
288     ###############################################################################################################
289
290     @mock.patch.object(SdcNfPkgDeleteThread, 'run')
291     def test_nf_pkg_delete_normal(self, mock_run):
292         resp = self.client.delete("/api/nslcm/v1/vnfpackage/1")
293         self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
294
295     def test_nf_pkg_normal_delete(self):
296         NfPackageModel(uuid="2", nfpackageid="2", vnfdid="vcpe_vfw_zte_1_0").save()
297         SdcNfPkgDeleteThread(csar_id="2", job_id="2", force_delete=False).run()
298         self.assert_job_result("2", 100, "Delete CSAR(2) successfully.")
299
300     def test_nf_pkg_force_delete(self):
301         NfPackageModel(uuid="1", nfpackageid="1", vnfdid="vcpe_vfw_zte_1_0").save()
302         NfInstModel(nfinstid="1", package_id="1").save()
303         SdcNfPkgDeleteThread(csar_id="1", job_id="2", force_delete=True).run()
304         self.assert_job_result("2", 100, "Delete CSAR(1) successfully.")
305
306     def test_nf_pkg_delete_when_pkg_in_using(self):
307         NfPackageModel(uuid="3", nfpackageid="3", vnfdid="vcpe_vfw_zte_1_0").save()
308         NfInstModel(nfinstid="3", package_id="3").save()
309         SdcNfPkgDeleteThread(csar_id="3", job_id="2", force_delete=False).run()
310         self.assert_job_result("2", 255, "NfInst by csar(3) exists, cannot delete.")
311
312     def test_nf_pkg_get_all(self):
313         NfPackageModel(uuid="3", nfpackageid="3", vnfdid="4").save()
314
315         resp = self.client.get("/api/nslcm/v1/vnfpackage")
316         self.assertEqual(resp.status_code, status.HTTP_200_OK)
317         self.assertEqual({"csars": [{"csarId": "3", "vnfdId": "4"}]}, resp.data)
318
319     def test_nf_pkg_get_one(self):
320         NfPackageModel(uuid="4", nfpackageid="4", vnfdid="5", vendor="6", vnfdversion="7", vnfversion="8").save()
321         NfInstModel(nfinstid="1", package_id="4", nf_name="3").save()
322
323         resp = self.client.get("/api/nslcm/v1/vnfpackage/4")
324         self.assertEqual(resp.status_code, status.HTTP_200_OK)
325         expect_data = {
326             "csarId": "4",
327             "packageInfo": {
328                 "vnfdId": "5",
329                 "vnfdProvider": "6",
330                 "vnfdVersion": "7",
331                 "vnfVersion": "8"
332             },
333             "imageInfo": [],
334             "vnfInstanceInfo": [{
335                 "vnfInstanceId": "1", "vnfInstanceName": "3"
336             }]
337         }
338         self.assertEqual(expect_data, resp.data)