From c8c469f7cf1ee14f51e9fa3761f1dbabc715d0aa Mon Sep 17 00:00:00 2001 From: maopengzhang Date: Mon, 22 Apr 2019 16:56:16 +0800 Subject: [PATCH] move heal test json from code to indepandent file move heal test json from code to indepandent file Change-Id: Iee81fd50c326054d51571698231759b5e2849730 Issue-ID: VFC-1241 Signed-off-by: maopengzhang --- lcm/ns/biz/ns_heal.py | 34 ++---- lcm/ns/tests/data/heal_ns.json | 5 + lcm/ns/tests/data/heal_vnf.json | 14 +-- lcm/ns/tests/data/instantiate_ns_with_pnf.json | 21 ++++ lcm/ns/tests/data/ns_info_from_aai.json | 7 ++ lcm/ns/tests/data/vnfd_model.json | 14 +++ lcm/ns/tests/test_ns_create.py | 15 +-- lcm/ns/tests/test_ns_delete.py | 20 ++-- lcm/ns/tests/test_ns_heal.py | 158 ++++--------------------- 9 files changed, 105 insertions(+), 183 deletions(-) create mode 100644 lcm/ns/tests/data/heal_ns.json create mode 100644 lcm/ns/tests/data/instantiate_ns_with_pnf.json create mode 100644 lcm/ns/tests/data/ns_info_from_aai.json create mode 100644 lcm/ns/tests/data/vnfd_model.json diff --git a/lcm/ns/biz/ns_heal.py b/lcm/ns/biz/ns_heal.py index a1cdce70..20db7722 100644 --- a/lcm/ns/biz/ns_heal.py +++ b/lcm/ns/biz/ns_heal.py @@ -83,10 +83,8 @@ class NSHealService(threading.Thread): vnf_heal_params = self.prepare_vnf_heal_params(self.heal_vnf_data) status = self.do_vnf_or_ns_heal(vnf_heal_params, 15) if status is JOB_MODEL_STATUS.FINISHED: - logger.info('nf[%s] heal handle end' % - vnf_heal_params.get('vnfInstanceId')) - self.update_job(90, - desc='nf[%s] heal handle end' % vnf_heal_params.get('vnfInstanceId')) + logger.info('nf[%s] heal handle end' % vnf_heal_params.get('vnfInstanceId')) + self.update_job(90, desc='nf[%s] heal handle end' % vnf_heal_params.get('vnfInstanceId')) else: logger.error('nf heal failed') raise NSLCMException('nf heal failed') @@ -95,10 +93,8 @@ class NSHealService(threading.Thread): for ns_heal_param in ns_heal_params: status = self.do_vnf_or_ns_heal(ns_heal_param, 15) if status is JOB_MODEL_STATUS.FINISHED: - logger.info('nf[%s] heal handle end' % - ns_heal_param.get('vnfInstanceId')) - self.update_job(90, - desc='nf[%s] heal handle end' % ns_heal_param.get('vnfInstanceId')) + logger.info('nf[%s] heal handle end' % ns_heal_param.get('vnfInstanceId')) + self.update_job(90, desc='nf[%s] heal handle end' % ns_heal_param.get('vnfInstanceId')) else: logger.error('nf heal failed') raise NSLCMException('nf heal failed') @@ -118,37 +114,31 @@ class NSHealService(threading.Thread): logger.error('degreeHealing does not exist.') raise NSLCMException('degreeHealing does not exist.') ns_instance_id = self.ns_instance_id - cause = '' - action = ignore_case_get(ns_data, 'actionsHealing') + cause = 'vm is down' + # action = ignore_case_get(ns_data, 'actionsHealing') if degree_healing == "HEAL_RESTORE": - ns_inst_infos = NfInstModel.objects.filter( - ns_inst_id=self.ns_instance_id) + ns_inst_infos = NfInstModel.objects.filter(ns_inst_id=self.ns_instance_id) if not ns_inst_infos.exists(): - raise NSLCMException( - 'NSInsts(%s) does not exist' % self.ns_instance_id) - + raise NSLCMException('NSInsts(%s) does not exist' % self.ns_instance_id) result_arr = [] for ns_inst_info in ns_inst_infos: - vnfc_insts = VNFCInstModel.objects.filter( - nfinstid=ns_inst_info.nfinstid) + vnfc_insts = VNFCInstModel.objects.filter(nfinstid=ns_inst_info.nfinstid) # If a condition is not met, will it all terminate? if not vnfc_insts.exists(): - raise NSLCMException( - 'vnfcinsts(%s) does not exist' % ns_inst_info.nfinstid) + raise NSLCMException('vnfcinsts(%s) does not exist' % ns_inst_info.nfinstid) for vnfc_inst in vnfc_insts: vm_id = vnfc_inst.vmid vdu_id = vnfc_inst.vduid vm_inst_info = VmInstModel.objects.filter(vmid=vm_id) if not vm_inst_info.exists(): - raise NSLCMException( - 'vminstinfo(%s) does not exist' % vm_id) + raise NSLCMException('vminstinfo(%s) does not exist' % vm_id) vm_name = vm_inst_info[0].vmname result = { "vnfInstanceId": ns_instance_id, "cause": cause, "additionalParams": { - "action": action, + "action": "restartvm", "actionvminfo": { "vmid": vm_id, "vduid": vdu_id, diff --git a/lcm/ns/tests/data/heal_ns.json b/lcm/ns/tests/data/heal_ns.json new file mode 100644 index 00000000..f226ac9b --- /dev/null +++ b/lcm/ns/tests/data/heal_ns.json @@ -0,0 +1,5 @@ +{ + "healNsData": { + "degreeHealing": "HEAL_RESTORE" + } +} diff --git a/lcm/ns/tests/data/heal_vnf.json b/lcm/ns/tests/data/heal_vnf.json index 89d116fb..3685c80c 100644 --- a/lcm/ns/tests/data/heal_vnf.json +++ b/lcm/ns/tests/data/heal_vnf.json @@ -1,14 +1,14 @@ { - "healVnfData": [{ + "healVnfData": { "vnfInstanceId": "default", "cause": "vm is down", "additionalParams": { - "action": "restartvm", - "actionvminfo": { - "vmid": "33", + "action": "restartvm", + "actionvminfo": { + "vmid": "33", "vduid": "", "vmname": "xgw-smp11" - } - } - }] + } + } + } } diff --git a/lcm/ns/tests/data/instantiate_ns_with_pnf.json b/lcm/ns/tests/data/instantiate_ns_with_pnf.json new file mode 100644 index 00000000..f7dead0a --- /dev/null +++ b/lcm/ns/tests/data/instantiate_ns_with_pnf.json @@ -0,0 +1,21 @@ +{ + "additionalParamForNs": { + "sdnControllerId": "2", + "location": "CPE-DC_Region" + }, + "locationConstraints": [{ + "vnfProfileId": "zte_ran_cucp_0001", + "locationConstraints": {} + }, + { + "vnfProfileId": "zte_ran_cuup_0001", + "locationConstraints": {} + } + ], + "addpnfData": [{ + "pnfId": 1, + "pnfName": "test_pnf", + "pnfdId": "zte_ran_du_0001", + "pnfProfileId": "du" + }] +} diff --git a/lcm/ns/tests/data/ns_info_from_aai.json b/lcm/ns/tests/data/ns_info_from_aai.json new file mode 100644 index 00000000..9e89a1d8 --- /dev/null +++ b/lcm/ns/tests/data/ns_info_from_aai.json @@ -0,0 +1,7 @@ +{ + "service-instance-id": "service-instance-id-9b9348f2-f75d-4559-823d-db7ac138ed34", + "service-instance-name": "service-instance-name-9b9348f2-f75d-4559-823d-db7ac138ed34", + "service-type": "service-type-9b9348f2-f75d-4559-823d-db7ac138ed34", + "service-role": "service-role-9b9348f2-f75d-4559-823d-db7ac138ed34", + "resource-version": "1505350720009" +} diff --git a/lcm/ns/tests/data/vnfd_model.json b/lcm/ns/tests/data/vnfd_model.json new file mode 100644 index 00000000..6851c708 --- /dev/null +++ b/lcm/ns/tests/data/vnfd_model.json @@ -0,0 +1,14 @@ +{ + "metadata": { + "vnfdId": "1", + "vnfdName": "PGW001", + "vnfProvider": "zte", + "vnfdVersion": "V00001", + "vnfVersion": "V5.10.20", + "productType": "CN", + "vnfType": "PGW", + "description": "PGW VNFD description", + "isShared": true, + "vnfExtendType": "driver" + } +} diff --git a/lcm/ns/tests/test_ns_create.py b/lcm/ns/tests/test_ns_create.py index a2c363e9..f137a5e2 100644 --- a/lcm/ns/tests/test_ns_create.py +++ b/lcm/ns/tests/test_ns_create.py @@ -17,19 +17,16 @@ import os import uuid from django.test import TestCase -from rest_framework import status -from rest_framework.test import APIClient - -from lcm.ns.biz.ns_create import CreateNSService from lcm.pub.database.models import NSInstModel +from lcm.ns.biz.ns_create import CreateNSService from lcm.pub.utils import restcall, fileutil +from rest_framework import status +from rest_framework.test import APIClient class TestNsInstantiate(TestCase): def setUp(self): self.client = APIClient() - self.nsd_id = str(uuid.uuid4()) - self.ns_package_id = str(uuid.uuid4()) self.cur_path = os.path.dirname(os.path.abspath(__file__)) self.create_ns_json = fileutil.read_json_file(self.cur_path + '/data/create_ns.json') @@ -39,13 +36,13 @@ class TestNsInstantiate(TestCase): @mock.patch.object(restcall, 'call_req') def test_create_ns(self, mock_call_req): nspackage_info = { - "csarId": self.ns_package_id, + "csarId": str(uuid.uuid4()), "packageInfo": {} } r1_query_nspackage_from_catalog = [0, json.JSONEncoder().encode(nspackage_info), '201'] r2_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201'] mock_call_req.side_effect = [r1_query_nspackage_from_catalog, r2_create_ns_to_aai] - self.create_ns_json["csarId"] = self.nsd_id + self.create_ns_json["csarId"] = str(uuid.uuid4()) response = self.client.post("/api/nslcm/v1/ns", data=self.create_ns_json, format='json') self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code) @@ -77,7 +74,7 @@ class TestNsInstantiate(TestCase): @mock.patch.object(restcall, 'call_req') def test_create_ns_when_ns_name_exist(self, mock_call_req): nspackage_info = json.JSONEncoder().encode({ - "csarId": self.ns_package_id, + "csarId": str(uuid.uuid4()), "packageInfo": {} }) mock_call_req.return_value = [0, nspackage_info, '200'] diff --git a/lcm/ns/tests/test_ns_delete.py b/lcm/ns/tests/test_ns_delete.py index 4397ef52..9f15497c 100644 --- a/lcm/ns/tests/test_ns_delete.py +++ b/lcm/ns/tests/test_ns_delete.py @@ -13,20 +13,20 @@ # limitations under the License. import json -import uuid - import mock +import os +import uuid from django.test import TestCase, Client -from rest_framework import status - from lcm.pub.database.models import NSInstModel -from lcm.pub.utils import restcall +from lcm.pub.utils import restcall, fileutil +from rest_framework import status class TestNsDelelete(TestCase): def setUp(self): self.client = Client() self.ns_inst_id = str(uuid.uuid1()) + self.cur_path = os.path.dirname(os.path.abspath(__file__)) NSInstModel.objects.filter().delete() NSInstModel(id=self.ns_inst_id, nspackage_id="7", nsd_id="2").save() @@ -35,14 +35,8 @@ class TestNsDelelete(TestCase): @mock.patch.object(restcall, 'call_req') def test_delete_ns(self, mock_call_req): - ns_info = { - "service-instance-id": "service-instance-id-9b9348f2-f75d-4559-823d-db7ac138ed34", - "service-instance-name": "service-instance-name-9b9348f2-f75d-4559-823d-db7ac138ed34", - "service-type": "service-type-9b9348f2-f75d-4559-823d-db7ac138ed34", - "service-role": "service-role-9b9348f2-f75d-4559-823d-db7ac138ed34", - "resource-version": "1505350720009" - } - r1_query_ns_to_aai = [0, json.JSONEncoder().encode(ns_info), '200'] + ns_info_aai = fileutil.read_json_file(self.cur_path + '/data/ns_info_aai.json') + r1_query_ns_to_aai = [0, json.JSONEncoder().encode(ns_info_aai), '200'] r2_delete_ns_to_aai = [0, json.JSONEncoder().encode({}), '200'] mock_call_req.side_effect = [r1_query_ns_to_aai, r2_delete_ns_to_aai] response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id) diff --git a/lcm/ns/tests/test_ns_heal.py b/lcm/ns/tests/test_ns_heal.py index 88b5f3f3..f9b3827f 100644 --- a/lcm/ns/tests/test_ns_heal.py +++ b/lcm/ns/tests/test_ns_heal.py @@ -12,46 +12,30 @@ # See the License for the specific language governing permissions and # limitations under the License. +from django.test import TestCase import json - import mock -from django.test import Client -from django.test import TestCase +import os +from rest_framework.test import APIClient from rest_framework import status - from lcm.ns.biz.ns_heal import NSHealService from lcm.ns.enum import NS_INST_STATUS from lcm.pub.database.models import NSInstModel, NfInstModel from lcm.pub.exceptions import NSLCMException from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE +from lcm.pub.utils import fileutil from lcm.ns_vnfs.biz.heal_vnfs import NFHealService class TestHealNsViews(TestCase): def setUp(self): - + self.cur_path = os.path.dirname(os.path.abspath(__file__)) self.ns_inst_id = '1' self.nf_inst_id = '1' self.nf_uuid = '1-1-1' - self.job_id = JobUtil.create_job("NS", JOB_TYPE.HEAL_VNF, self.ns_inst_id) - - self.client = Client() - - model = json.dumps({ - "metadata": { - "vnfdId": "1", - "vnfdName": "PGW001", - "vnfProvider": "zte", - "vnfdVersion": "V00001", - "vnfVersion": "V5.10.20", - "productType": "CN", - "vnfType": "PGW", - "description": "PGW VNFD description", - "isShared": True, - "vnfExtendType": "driver" - } - }) + self.client = APIClient() + model = json.dumps(fileutil.read_json_file(self.cur_path + '/data/vnfd_model.json')) NSInstModel.objects.filter().delete() NfInstModel.objects.filter().delete() NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save() @@ -75,56 +59,26 @@ class TestHealNsViews(TestCase): @mock.patch.object(NSHealService, 'run') def test_heal_vnf_url(self, mock_run): - - data = { - "healVnfData": [{ - "vnfInstanceId": self.nf_inst_id, - "cause": "vm is down", - "additionalParams": { - "action": "restartvm", - "actionvminfo": { - "vmid": "33", - "vduid": "", - "vmname": "xgw-smp11" - } - } - }] - } - - response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data) + heal_vnf_json = fileutil.read_json_file(self.cur_path + '/data/heal_vnf.json') + heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id + response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=heal_vnf_json, format='json') self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data) self.assertIsNotNone(response.data) self.assertIn("jobId", response.data) self.assertNotIn("error", response.data) - response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id) self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) # add healNsData @mock.patch.object(NSHealService, 'run') def test_heal_ns_url(self, mock_run): - - data = { - "healNsData": { - "vnfInstanceId": self.nf_inst_id, - "cause": "", - "additionalParams": { - "action": "vmreset", - "actionvminfo": { - "vmid": "33", - "vduid": "", - "vmname": "xgw-smp11" - } - } - } - } - - response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data) + heal_ns_json = fileutil.read_json_file(self.cur_path + '/data/heal_ns.json') + heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id + response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=heal_ns_json, format='json') self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data) self.assertIsNotNone(response.data) self.assertIn("jobId", response.data) self.assertNotIn("error", response.data) - response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id) self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code) @@ -132,23 +86,9 @@ class TestHealNsViews(TestCase): @mock.patch.object(NSHealService, 'wait_job_finish') @mock.patch.object(NSHealService, 'update_job') def test_heal_vnf_thread(self, mock_start, mock_wait, mock_update): - - data = { - "healVnfData": [{ - "vnfInstanceId": self.nf_inst_id, - "cause": "vm is down", - "additionalParams": { - "action": "restartvm", - "actionvminfo": { - "vmid": "33", - "vduid": "", - "vmname": "xgw-smp11" - } - } - }] - } - - NSHealService(self.ns_inst_id, data, self.job_id).run() + heal_vnf_json = fileutil.read_json_file(self.cur_path + '/data/heal_vnf.json') + heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id + NSHealService(self.ns_inst_id, heal_vnf_json, self.job_id).run() self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING) # add healNsData @@ -156,46 +96,18 @@ class TestHealNsViews(TestCase): @mock.patch.object(NSHealService, 'wait_job_finish') @mock.patch.object(NSHealService, 'update_job') def test_heal_ns_thread(self, mock_start, mock_wait, mock_update): - - data = { - "healNsData": { - "vnfInstanceId": self.nf_inst_id, - "cause": "", - "additionalParams": { - "action": "vmreset", - "actionvminfo": { - "vmid": "33", - "vduid": "", - "vmname": "xgw-smp11" - } - } - } - } - - NSHealService(self.ns_inst_id, data, self.job_id).run() + heal_ns_json = fileutil.read_json_file(self.cur_path + '/data/heal_ns.json') + heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id + NSHealService(self.ns_inst_id, heal_ns_json, self.job_id).run() self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING) @mock.patch.object(NSHealService, "start") def test_heal_vnf_non_existing_ns(self, mock_start): mock_start.side_effect = NSLCMException("NS Not Found") - ns_inst_id = "2" - - data = { - "healVnfData": [{ - "vnfInstanceId": self.nf_inst_id, - "cause": "vm is down", - "additionalParams": { - "action": "restartvm", - "actionvminfo": { - "vmid": "33", - "vmname": "xgw-smp11" - } - } - }] - } - - response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=data) + heal_vnf_json = fileutil.read_json_file(self.cur_path + '/data/heal_vnf.json') + heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id + response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=heal_vnf_json, format='json') self.assertEqual(response.data["error"], "NS Not Found") self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertIn("error", response.data) @@ -204,25 +116,10 @@ class TestHealNsViews(TestCase): @mock.patch.object(NSHealService, "start") def test_heal_ns_heal_non_existing_ns(self, mock_start): mock_start.side_effect = NSLCMException("NS Not Found") - ns_inst_id = "2" - - data = { - "healNsData": { - "vnfInstanceId": self.nf_inst_id, - "cause": "", - "additionalParams": { - "action": "vmreset", - "actionvminfo": { - "vmid": "33", - "vduid": "", - "vmname": "xgw-smp11" - } - } - } - } - - response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=data) + heal_ns_json = fileutil.read_json_file(self.cur_path + '/data/heal_ns.json') + heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id + response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=heal_ns_json, format='json') self.assertEqual(response.data["error"], "NS Not Found") self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertIn("error", response.data) @@ -230,9 +127,6 @@ class TestHealNsViews(TestCase): @mock.patch.object(NSHealService, "start") def test_heal_vnf_empty_post(self, mock_start): mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.") - - data = {} - - response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data) + response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data={}) self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR) self.assertIn("error", response.data) -- 2.16.6