1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from django.test import Client
19 from django.test import TestCase
20 from rest_framework import status
22 from lcm.packages.sdc_nf_package import SdcNfDistributeThread, SdcNfPkgDeleteThread
23 from lcm.pub.database.models import JobStatusModel, JobModel
24 from lcm.pub.database.models import NfPackageModel, NfInstModel
25 from lcm.pub.msapi import sdc
26 from lcm.pub.utils import restcall, toscaparser, idutil
29 class TestNfPackage(TestCase):
31 self.client = Client()
32 NfPackageModel.objects.filter().delete()
33 NfInstModel.objects.filter().delete()
34 JobModel.objects.filter().delete()
35 idutil.get_auto_id = mock.Mock()
36 idutil.get_auto_id.return_value = random.random()
37 JobStatusModel.objects.filter().delete()
46 "unit_size": 1000000000
48 "type_of_storage": "volume",
49 "rdma_enabled": False,
52 "volume_storage_id": "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7",
60 "vNAT_Storage_6wdgwzedlb6sq18uzrr41sof7"
67 "configurable_properties": {
69 "additional_vnfc_configurable_properties": {
76 "description": "the virtual machine of vNat",
87 "artifact_name": "vNatVNFImage",
88 "type": "tosca.artifacts.nfv.SwImage",
90 "operating_system": "linux",
91 "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
92 "name": "vNatVNFImage",
93 "container_format": "bare",
95 "disk_format": "qcow2",
96 "supported_virtualisation_environments": [
104 "file": "/swimages/vRouterVNF_ControlPlane.qcow2"
108 "flavor_extra_specs": {
109 "hw:cpu_sockets": "2",
110 "sw:ovs_dpdk": "true",
111 "hw:cpu_threads": "2",
112 "hw:numa_mem.1": "3072",
113 "hw:numa_mem.0": "1024",
114 "hw:numa_nodes": "2",
115 "hw:numa_cpus.0": "0,1",
116 "hw:numa_cpus.1": "2,3,4,5",
118 "hw:cpu_threads_policy": "isolate"
120 "cpu_frequency": "2.4 GHz",
124 "local_storages": [],
125 "image_file": "vNatVNFImage",
132 "operating_system": "linux",
133 "sw_image": "/swimages/vRouterVNF_ControlPlane.qcow2",
134 "name": "vNatVNFImage",
135 "container_format": "bare",
137 "disk_format": "qcow2",
138 "supported_virtualisation_environments": [
146 "image_file_id": "vNatVNFImage",
151 "local_storages": [],
155 "key_name": "sriov_plane",
156 "cp_id": "SRIOV_Port"
164 "vl_id": "sriov_link",
165 "route_external": False,
171 "connectivity_type": {
172 "layer_protocol": "ipv4",
173 "flow_pattern": "flat"
175 "description": "sriov_link",
184 "vl_id": "sriov_link",
185 "vdu_id": "vdu_vNat",
187 "cp_id": "SRIOV_Port",
191 "address_type": "ip_address",
193 "ip_address_type": "ipv4",
194 "floating_ip_activated": False,
195 "number_of_ip_address": 1,
196 "ip_address_assignment": True
200 "description": "sriov port",
201 "layer_protocol": "ipv4",
202 "virtual_network_interface_requirements": [
207 "support_mandatory": False,
209 "description": "sriov"
215 "support_mandatory": False,
217 "description": "normal"
221 "bitrate_requirement": 10
226 "vnfSoftwareVersion": "1.0.0",
227 "vnfProductName": "zte",
228 "localizationLanguage": [
232 "vnfProvider": "zte",
234 "defaultLocalizationLanguage": "english",
235 "vnfdId": "zte-hss-1.0",
237 "vnfProductInfoDescription": "hss",
238 "vnfdVersion": "1.0.0",
239 "vnfProductInfoName": "hss"
246 def assert_job_result(self, job_id, job_progress, job_detail):
247 jobs = JobStatusModel.objects.filter(
249 progress=job_progress,
251 self.assertEqual(1, len(jobs))
253 @mock.patch.object(SdcNfDistributeThread, 'run')
254 def test_nf_pkg_distribute_normal(self, mock_run):
255 resp = self.client.post("/api/nslcm/v1/vnfpackage", {"csarId": "1", "vimIds": ["1"]}, format='json')
256 self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
258 def test_nf_pkg_distribute_when_csar_already_exist(self):
259 NfPackageModel(uuid="1", nfpackageid="1", vnfdid="vcpe_vfw_zte_1_0").save()
260 SdcNfDistributeThread(csar_id="1", vim_ids=["1"], lab_vim_id="", job_id="2").run()
261 self.assert_job_result("2", 255, "NF CSAR(1) already exists.")
263 @mock.patch.object(restcall, 'call_req')
264 @mock.patch.object(sdc, 'download_artifacts')
265 @mock.patch.object(toscaparser, 'parse_vnfd')
266 def test_nf_pkg_distribute_when_vnfd_already_exist(self, mock_parse_vnfd, mock_download_artifacts,
268 mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data)
269 mock_download_artifacts.return_value = "/home/hss.csar"
270 mock_call_req.return_value = [0, json.JSONEncoder().encode([{
272 "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/hss.csar"
274 NfPackageModel(uuid="2", nfpackageid="2", vnfdid="zte-hss-1.0").save()
275 SdcNfDistributeThread(csar_id="1", vim_ids=["1"], lab_vim_id="", job_id="2").run()
276 self.assert_job_result("2", 255, "NFD(zte-hss-1.0) already exists.")
278 @mock.patch.object(restcall, 'call_req')
279 @mock.patch.object(sdc, 'download_artifacts')
280 @mock.patch.object(toscaparser, 'parse_vnfd')
281 def test_nf_pkg_distribute_successfully(self, mock_parse_vnfd, mock_download_artifacts, mock_call_req):
282 mock_parse_vnfd.return_value = json.JSONEncoder().encode(self.vnfd_data)
283 mock_download_artifacts.return_value = "/home/hss.csar"
284 mock_call_req.return_value = [0, json.JSONEncoder().encode([{
286 "toscaModelURL": "https://127.0.0.1:1234/sdc/v1/hss.csar"
288 SdcNfDistributeThread(csar_id="1", vim_ids=["1"], lab_vim_id="", job_id="4").run()
289 self.assert_job_result("4", 100, "CSAR(1) distribute successfully.")
291 ###############################################################################################################
293 @mock.patch.object(SdcNfPkgDeleteThread, 'run')
294 def test_nf_pkg_delete_normal(self, mock_run):
295 resp = self.client.delete("/api/nslcm/v1/vnfpackage/1")
296 self.assertEqual(resp.status_code, status.HTTP_202_ACCEPTED)
298 def test_nf_pkg_normal_delete(self):
299 NfPackageModel(uuid="2", nfpackageid="2", vnfdid="vcpe_vfw_zte_1_0").save()
300 SdcNfPkgDeleteThread(csar_id="2", job_id="2", force_delete=False).run()
301 self.assert_job_result("2", 100, "Delete CSAR(2) successfully.")
303 def test_nf_pkg_force_delete(self):
304 NfPackageModel(uuid="1", nfpackageid="1", vnfdid="vcpe_vfw_zte_1_0").save()
305 NfInstModel(nfinstid="1", package_id="1").save()
306 SdcNfPkgDeleteThread(csar_id="1", job_id="2", force_delete=True).run()
307 self.assert_job_result("2", 100, "Delete CSAR(1) successfully.")
309 def test_nf_pkg_delete_when_pkg_in_using(self):
310 NfPackageModel(uuid="3", nfpackageid="3", vnfdid="vcpe_vfw_zte_1_0").save()
311 NfInstModel(nfinstid="3", package_id="3").save()
312 SdcNfPkgDeleteThread(csar_id="3", job_id="2", force_delete=False).run()
313 self.assert_job_result("2", 255, "NfInst by csar(3) exists, cannot delete.")
315 def test_nf_pkg_get_all(self):
316 NfPackageModel(uuid="3", nfpackageid="3", vnfdid="4").save()
318 resp = self.client.get("/api/nslcm/v1/vnfpackage")
319 self.assertEqual(resp.status_code, status.HTTP_200_OK)
320 self.assertEqual({"csars": [{"csarId": "3", "vnfdId": "4"}]}, resp.data)
322 @mock.patch.object(restcall, 'call_req')
323 def test_nf_pkg_get_one(self, mock_call_req):
324 NfPackageModel(uuid="4", nfpackageid="4", vnfdid="5", vendor="6", vnfdversion="7", vnfversion="8").save()
325 NfInstModel(nfinstid="1", package_id="4", nf_name="3").save()
326 mock_call_req.return_value = [0, json.JSONEncoder().encode({
335 resp = self.client.get("/api/nslcm/v1/vnfpackage/4")
336 self.assertEqual(resp.status_code, status.HTTP_200_OK)
346 "vnfInstanceInfo": [{
347 "vnfInstanceId": "1", "vnfInstanceName": "3"
350 self.assertEqual(expect_data, resp.data)