Merge "Deal with nfPackage"
[vfc/nfvo/catalog.git] / catalog / packages / tests / test_vnf_package.py
1 # Copyright 2018 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 os
16 import json
17 import mock
18 import urllib2
19
20 from rest_framework.test import APIClient
21 from django.test import TestCase
22 from rest_framework import status
23 from catalog.pub.config.config import CATALOG_ROOT_PATH
24 from catalog.packages.biz.vnf_package import VnfPkgUploadThread
25 from catalog.pub.database.models import VnfPackageModel
26
27
28 class MockReq():
29     def read(self):
30         return "1"
31
32     def close(self):
33         pass
34
35
36 class TestVnfPackage(TestCase):
37     def setUp(self):
38         self.client = APIClient()
39         self.vnfd_data = {
40             "volume_storages": [
41                 {
42                     "properties": {
43                         "size_of_storage": {
44                             "factor": 10,
45                             "value": 10000000000,
46                             "unit": "GB",
47                             "unit_size": 1000000000
48                         },
49                         "type_of_storage": "volume",
50                         "rdma_enabled": False,
51                         "size": "10 GB"
52                     },
53                     "volume_storage_id": "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7",
54                     "description": ""
55                 }
56             ],
57             "inputs": {},
58             "vdus": [
59                 {
60                     "volume_storages": [
61                         "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7"
62                     ],
63                     "description": "",
64                     "dependencies": [],
65                     "vls": [],
66                     "properties": {
67                         "name": "vNat",
68                         "configurable_properties": {
69                             "test": {
70                                 "additional_vnfc_configurable_properties": {
71                                     "aaa": "1",
72                                     "bbb": "2",
73                                     "ccc": "3"
74                                 }
75                             }
76                         },
77                         "description": "the virtual machine of vNat",
78                         "nfvi_constraints": [
79                             "test"
80                         ],
81                         "boot_order": [
82                             "vNAT_Storage"
83                         ]
84                     },
85                     "vdu_id": "vdu_vNat",
86                     "artifacts": [
87                         {
88                             "artifact_name": "vNatVNFImage",
89                             "type": "tosca.artifacts.nfv.SwImage",
90                             "properties": {
91                                 "operating_system": "linux",
92                                 "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
93                                 "name": "vNatVNFImage",
94                                 "container_format": "bare",
95                                 "min_ram": "1 GB",
96                                 "disk_format": "qcow2",
97                                 "supported_virtualisation_environments": [
98                                     "test_0"
99                                 ],
100                                 "version": "1.0",
101                                 "checksum": "5000",
102                                 "min_disk": "10 GB",
103                                 "size": "10 GB"
104                             },
105                             "file": "/swimages/vRouterVNF_ControlPlane.qcow2"
106                         }
107                     ],
108                     "nfv_compute": {
109                         "flavor_extra_specs": {
110                             "hw:cpu_sockets": "2",
111                             "sw:ovs_dpdk": "true",
112                             "hw:cpu_threads": "2",
113                             "hw:numa_mem.1": "3072",
114                             "hw:numa_mem.0": "1024",
115                             "hw:numa_nodes": "2",
116                             "hw:numa_cpus.0": "0,1",
117                             "hw:numa_cpus.1": "2,3,4,5",
118                             "hw:cpu_cores": "2",
119                             "hw:cpu_threads_policy": "isolate"
120                         },
121                         "cpu_frequency": "2.4 GHz",
122                         "num_cpus": 2,
123                         "mem_size": "10 GB"
124                     },
125                     "local_storages": [],
126                     "image_file": "vNatVNFImage",
127                     "cps": []
128                 }
129             ],
130             "image_files": [
131                 {
132                     "properties": {
133                         "operating_system": "linux",
134                         "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
135                         "name": "vNatVNFImage",
136                         "container_format": "bare",
137                         "min_ram": "1 GB",
138                         "disk_format": "qcow2",
139                         "supported_virtualisation_environments": [
140                             "test_0"
141                         ],
142                         "version": "1.0",
143                         "checksum": "5000",
144                         "min_disk": "10 GB",
145                         "size": "10 GB"
146                     },
147                     "image_file_id": "vNatVNFImage",
148                     "description": ""
149                 }
150             ],
151             "routers": [],
152             "local_storages": [],
153             "vnf_exposed": {
154                 "external_cps": [
155                     {
156                         "key_name": "sriov_plane",
157                         "cp_id": "SRIOV_Port"
158                     }
159                 ],
160                 "forward_cps": []
161             },
162             "vls": [
163                 {
164                     "route_id": "",
165                     "vl_id": "sriov_link",
166                     "route_external": False,
167                     "description": "",
168                     "properties": {
169                         "vl_flavours": {
170                             "vl_id": "aaaa"
171                         },
172                         "connectivity_type": {
173                             "layer_protocol": "ipv4",
174                             "flow_pattern": "flat"
175                         },
176                         "description": "sriov_link",
177                         "test_access": [
178                             "test"
179                         ]
180                     }
181                 }
182             ],
183             "cps": [
184                 {
185                     "vl_id": "sriov_link",
186                     "vdu_id": "vdu_vNat",
187                     "description": "",
188                     "cp_id": "SRIOV_Port",
189                     "properties": {
190                         "address_data": [
191                             {
192                                 "address_type": "ip_address",
193                                 "l3_address_data": {
194                                     "ip_address_type": "ipv4",
195                                     "floating_ip_activated": False,
196                                     "number_of_ip_address": 1,
197                                     "ip_address_assignment": True
198                                 }
199                             }
200                         ],
201                         "description": "sriov port",
202                         "layer_protocol": "ipv4",
203                         "virtual_network_interface_requirements": [
204                             {
205                                 "requirement": {
206                                     "SRIOV": "true"
207                                 },
208                                 "support_mandatory": False,
209                                 "name": "sriov",
210                                 "description": "sriov"
211                             },
212                             {
213                                 "requirement": {
214                                     "SRIOV": "False"
215                                 },
216                                 "support_mandatory": False,
217                                 "name": "normal",
218                                 "description": "normal"
219                             }
220                         ],
221                         "role": "root",
222                         "bitrate_requirement": 10
223                     }
224                 }
225             ],
226             "metadata": {
227                 "vnfSoftwareVersion": "1.0.0",
228                 "vnfProductName": "zte",
229                 "localizationLanguage": [
230                     "english",
231                     "chinese"
232                 ],
233                 "vnfProvider": "zte",
234                 "vnfmInfo": "zte",
235                 "defaultLocalizationLanguage": "english",
236                 "vnfdId": "zte-hss-1.0",
237                 "id": "zte-hss-1.0",
238                 "vnfProductInfoDescription": "hss",
239                 "vnfdVersion": "1.0.0",
240                 "vnfProductInfoName": "hss"
241             }
242         }
243
244     def tearDown(self):
245         pass
246
247     def test_upload_vnf_pkg(self):
248         data = {'file': open(os.path.join(CATALOG_ROOT_PATH, "empty.txt"), "rb")}
249         VnfPackageModel.objects.create(
250             vnfPackageId="222",
251             onboardingState="CREATED"
252         )
253         response = self.client.put("/api/vnfpkgm/v1/vnf_packages/222/package_content", data=data)
254         self.assertEqual(response.status_code, status.HTTP_202_ACCEPTED)
255
256     @mock.patch.object(urllib2, 'urlopen')
257     def test_upload_nf_pkg_from_uri(self, mock_urlopen):
258         vnf_pkg = VnfPackageModel.objects.create(
259             vnfPackageId="222",
260             onboardingState="CREATED"
261         )
262         req_data = {"addressInformation": "https://127.0.0.1:1234/sdc/v1/hss.csar"}
263         mock_urlopen.return_value = MockReq()
264         vnf_pkg_id = vnf_pkg.vnfPackageId
265         VnfPkgUploadThread(req_data, vnf_pkg_id).run()
266
267     def test_create_vnf_pkg(self):
268         req_data = {
269             "userDefinedData": {"a": "A"}
270         }
271         response = self.client.post("/api/vnfpkgm/v1/vnf_packages", data=req_data, format="json")
272         resp_data = json.loads(response.content)
273         expect_resp_data = {
274             "id": resp_data.get("id"),
275             "onboardingState": "CREATED",
276             "operationalState": "DISABLED",
277             "usageState": "NOT_IN_USE",
278             "userDefinedData": {"a": "A"},
279             "_links": None  # TODO
280         }
281         self.assertEqual(expect_resp_data, resp_data)
282         self.assertEqual(response.status_code, status.HTTP_201_CREATED)