diff options
author | fengyuanxing <feng.yuanxing@zte.com.cn> | 2017-08-31 14:14:59 +0800 |
---|---|---|
committer | fengyuanxing <feng.yuanxing@zte.com.cn> | 2017-08-31 14:14:59 +0800 |
commit | 50466ee1d64acbacea2eb8f818e5fbd94aec8a31 (patch) | |
tree | e5228b82e73a54ccbbf7727eb01910c35b206055 | |
parent | dd8ce89b4ac028cf19c86c3afd132fabffe5b703 (diff) |
Modify test by mock job
Change-Id: I6facca25657974091ffd61bb9f4d32f6f8b3b8f2
Issue-Id: VFC-152
Signed-off-by: fengyuanxing <feng.yuanxing@zte.com.cn>
-rw-r--r-- | catalog/packages/nf_package.py | 15 | ||||
-rw-r--r-- | catalog/packages/tests.py | 95 |
2 files changed, 61 insertions, 49 deletions
diff --git a/catalog/packages/nf_package.py b/catalog/packages/nf_package.py index 494d6e83..c3d2e7d9 100644 --- a/catalog/packages/nf_package.py +++ b/catalog/packages/nf_package.py @@ -97,12 +97,7 @@ class NfDistributeThread(threading.Thread): if NfPackageModel.objects.filter(nfpackageid=self.csar_id): raise NSLCMException("NF CSAR(%s) already exists." % self.csar_id) - artifact = sdc.get_artifact(sdc.ASSETTYPE_RESOURCES, self.csar_id) - local_path = os.path.join(CATALOG_ROOT_PATH, self.csar_id) - local_file_name = sdc.download_artifacts(artifact["toscaModelURL"], local_path) - - vnfd_json = toscaparser.parse_vnfd(local_file_name) - vnfd = json.JSONDecoder().decode(vnfd_json) + vnfd,local_file_name,vnfd_json = self.get_vnfd(self.csar_id) nfd_id = vnfd["metadata"]["id"] if NfPackageModel.objects.filter(vnfdid=nfd_id): @@ -126,6 +121,14 @@ class NfDistributeThread(threading.Thread): JobUtil.add_job_status(self.job_id, 100, "CSAR(%s) distribute successfully." % self.csar_id) + def get_vnfd(self, csar_id): + artifact = sdc.get_artifact(sdc.ASSETTYPE_RESOURCES, self.csar_id) + local_path = os.path.join(CATALOG_ROOT_PATH, self.csar_id) + local_file_name = sdc.download_artifacts(artifact["toscaModelURL"], local_path) + + vnfd_json = toscaparser.parse_vnfd(local_file_name) + vnfd = json.JSONDecoder().decode(vnfd_json) + return vnfd,local_file_name,vnfd_json def rollback_distribute(self): try: diff --git a/catalog/packages/tests.py b/catalog/packages/tests.py index b2deba66..d6faf761 100644 --- a/catalog/packages/tests.py +++ b/catalog/packages/tests.py @@ -17,9 +17,10 @@ import mock import catalog.pub.utils.restcall import json from catalog.packages.ns_package import NsPackage -from catalog.packages import nf_package +from catalog.packages.nf_package import NfPackage +from catalog.packages.nf_package import NfDistributeThread from django.test import Client -from catalog.pub.database.models import NSDModel, NfPackageModel +from catalog.pub.database.models import NSDModel, NfPackageModel, JobStatusModel, JobModel from rest_framework import status @@ -39,36 +40,38 @@ class PackageTest(unittest.TestCase): "csarId": self.nf_csarId } - self.nsd_json = { - "vnffgs": [ - { - "vnffg_id": "vnffg1", - "description": "", - "members": [ - "path1", - "path2" - ], - "properties": { + self.vnfd_json = { + "metadata": { + "id": "zte_xgw_51610", "vendor": "zte", - "connection_point": [ - "m6000_data_in", - "m600_tunnel_cp", - "m6000_data_out" - ], - "version": "1.0", - "constituent_vnfs": [ - "VFW", - "VNAT" - ], - "number_of_endpoints": 3, - "dependent_virtual_link": [ - "sfc_data_network", - "ext_datanet_net", - "ext_mnet_net" - ] - } + "version": "5.16.10", + "vnfd_version": "1.1.0", + "name": "zte_xgw", + "domain_type": "CN", + "vnf_type": "XGW", + "is_shared": "false", + "cross_dc": "false", + "vmnumber_overquota_alarm": "false", + "description": "", + "vnf_extend_type": "driver&script", + "plugin_info": "zte_cn_plugin_v6.16.10", + "script_info": "script/cn.py", + "adjust_vnf_capacity": "true", + "custom_properties": "", + }, + "reserved_total": { + "vmnum": 10, + "vcpunum": 20, + "memorysize": 1000, + "portnum": 30, + "hdsize": 1024, + "shdsize": 2048, + "isreserve": 0, + }, } - ], + + self.nsd_json = { + "inputs": { "sfc_data_network": { "type": "string", @@ -442,12 +445,11 @@ class PackageTest(unittest.TestCase): self.assertEquals([],response.data) @mock.patch.object(NsPackage,'get_nsd') - def test_ns_distribute(self, mock_get_nsd): + def test_ns_distribute_2(self, mock_get_nsd): local_file_name = "/url/local/filename" nsd = json.JSONEncoder().encode(self.nsd_json) mock_get_nsd.return_value = self.nsd_json,local_file_name,nsd - response = self.client.post("/api/nfvocatalog/v1/nspackages",self.nsdata) - + response = self.client.post("/api/catalog/v1/nspackages",self.nsdata) self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content) self.assertIsNotNone(NSDModel.objects.filter(id=self.ns_csarId)) @@ -458,22 +460,22 @@ class PackageTest(unittest.TestCase): nsdModel = NSDModel.objects.filter(nsd_id="VCPE_NS") self.assertSequenceEqual(nsdModel,[]) - #nsd_id = nsdModel.nsd_id - #self.assertEqual(self.nsd_json["metadata"]["id"], nsdModel.nsd_id) - #self.assertEqual(self.nsd_json["metadata"]["name"], nsdModel.name) - #self.assertEqual(self.nsd_json["metadata"]["version"], nsdModel.version) - #self.assertEqual(self.nsd_json["metadata"]["description"], nsdModel.description) - #self.assertEqual(self.nsd_json["metadata"]["vendor"], nsdModel.vendor) def test_ns_distribute(self): response = self.client.post("/api/catalog/v1/nspackages",self.nsdata) #self.assertEqual(status.HTTP_200_OK, response.status_code, response.content) - - def test_nf_distribute(self): + @mock.patch.object(NfDistributeThread, 'get_vnfd') + def test_nf_distribute(self, mock_get_vnfd): #response = self.client.post("/api/catalog/v1/vnfpackages",self.nfdata) - #self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content) - pass + local_file_name = "/url/local/filename" + vnfd = json.JSONEncoder().encode(self.vnfd_json) + mock_get_vnfd.return_value = self.vnfd_json,local_file_name,vnfd + + NfDistributeThread(str(self.nf_csarId), ["1"], "1", "5").run() + #self.assert_job_result("5") + self.assert_job_result("5", 100, "CSAR(456) distribute successfully.") + def test_ns_package_delete(self): response = self.client.delete("/api/catalog/v1/nspackages/" + str(self.ns_csarId)) @@ -483,3 +485,10 @@ class PackageTest(unittest.TestCase): #response = self.client.delete("/api/catalog/v1/vnfpackages/" + str(self.nf_csarId)) #self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content) pass + + def assert_job_result(self, job_id, job_progress, job_detail): + jobs = JobStatusModel.objects.filter( + jobid=job_id, + progress=job_progress, + descp=job_detail) + self.assertEqual(1, len(jobs)) |