move heal test json from code to indepandent file 48/85948/2
authormaopengzhang <zhang.maopeng1@zte.com.cn>
Mon, 22 Apr 2019 08:56:16 +0000 (16:56 +0800)
committermaopengzhang <zhang.maopeng1@zte.com.cn>
Mon, 22 Apr 2019 11:27:00 +0000 (19:27 +0800)
move heal test json from code to indepandent file

Change-Id: Iee81fd50c326054d51571698231759b5e2849730
Issue-ID: VFC-1241
Signed-off-by: maopengzhang <zhang.maopeng1@zte.com.cn>
lcm/ns/biz/ns_heal.py
lcm/ns/tests/data/heal_ns.json [new file with mode: 0644]
lcm/ns/tests/data/heal_vnf.json
lcm/ns/tests/data/instantiate_ns_with_pnf.json [new file with mode: 0644]
lcm/ns/tests/data/ns_info_from_aai.json [new file with mode: 0644]
lcm/ns/tests/data/vnfd_model.json [new file with mode: 0644]
lcm/ns/tests/test_ns_create.py
lcm/ns/tests/test_ns_delete.py
lcm/ns/tests/test_ns_heal.py

index a1cdce7..20db772 100644 (file)
@@ -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 (file)
index 0000000..f226ac9
--- /dev/null
@@ -0,0 +1,5 @@
+{
+    "healNsData": {
+        "degreeHealing": "HEAL_RESTORE"
+    }
+}
index 89d116f..3685c80 100644 (file)
@@ -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 (file)
index 0000000..f7dead0
--- /dev/null
@@ -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 (file)
index 0000000..9e89a1d
--- /dev/null
@@ -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 (file)
index 0000000..6851c70
--- /dev/null
@@ -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"
+    }
+}
index a2c363e..f137a5e 100644 (file)
@@ -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']
index 4397ef5..9f15497 100644 (file)
 # 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)
index 88b5f3f..f9b3827 100644 (file)
 # 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)