Add unit test for ns instantiate 59/59959/3
authorfujinhua <fu.jinhua@zte.com.cn>
Fri, 10 Aug 2018 06:08:08 +0000 (14:08 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Fri, 10 Aug 2018 06:27:20 +0000 (14:27 +0800)
Change-Id: I9423cd90fed4ea9f73a4c7e4225e7b55c0fff2ef
Issue-ID: VFC-1009
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
lcm/ns/ns_instant.py
lcm/ns/tests/test_ns_instant.py

index b10b9c1..698e77e 100644 (file)
@@ -104,7 +104,7 @@ class InstantNSService(object):
             vnf_params_json = json.JSONEncoder().encode(params_vnf)
             plan_input = {
                 'jobId': job_id,
-                'nsInstanceId': self.req_data["nsInstanceId"],
+                'nsInstanceId': self.ns_inst_id,
                 'object_context': dst_plan,
                 'object_additionalParamForNs': params_json,
                 'object_additionalParamForVnf': vnf_params_json
@@ -191,7 +191,8 @@ class InstantNSService(object):
         BuildInWorkflowThread(plan_input).start()
         return dict(data={'jobId': job_id}, status=status.HTTP_200_OK)
 
-    def get_vnf_vim_id(self, vim_id, location_constraints, vnfdid):
+    @staticmethod
+    def get_vnf_vim_id(vim_id, location_constraints, vnfdid):
         for location in location_constraints:
             if "vnfProfileId" in location and vnfdid == location["vnfProfileId"]:
                 return location["locationConstraints"]["vimId"]
@@ -199,7 +200,8 @@ class InstantNSService(object):
             return vim_id
         raise NSLCMException("No Vim info is found for vnf(%s)." % vnfdid)
 
-    def set_vl_vim_id(self, vim_id, location_constraints, plan_dict):
+    @staticmethod
+    def set_vl_vim_id(vim_id, location_constraints, plan_dict):
         if "vls" not in plan_dict:
             logger.debug("No vl is found in nsd.")
             return
index bd84c03..f4df107 100644 (file)
@@ -19,27 +19,35 @@ import mock
 
 from lcm.pub.database.models import NSInstModel
 from lcm.ns.ns_instant import InstantNSService
+from lcm.pub.utils import restcall
 
 
 class TestNsInstant(TestCase):
     def setUp(self):
         self.client = Client()
         NSInstModel.objects.filter().delete()
-        self.ns_inst_id = "2"
+        self.url = "/api/nslcm/v1/ns/2/instantiate"
+        self.req_data = {
+            "additionalParamForNs": {
+                "location": "1",
+                "sdnControllerId": "2"
+            }
+        }
         NSInstModel(id="2", nspackage_id="7", nsd_id="2").save()
 
     def tearDown(self):
         pass
 
     @mock.patch.object(InstantNSService, 'do_biz')
-    def testNsInstantNormal(self, mock_do_biz):
+    def test_ns_instantiate_normal(self, mock_do_biz):
         mock_do_biz.return_value = dict(data={'jobId': "1"}, status=status.HTTP_200_OK)
-        data = {
-            "additionalParamForNs": {
-                "location": "1",
-                "sdnControllerId": "2"
-            }
-        }
-        resp = self.client.post("/api/nslcm/v1/ns/%s/instantiate" % self.ns_inst_id, data=data)
+        resp = self.client.post(self.url, data=self.req_data)
         self.failUnlessEqual(status.HTTP_200_OK, resp.status_code)
         self.assertEqual({'jobId': "1"}, resp.data)
+
+    @mock.patch.object(restcall, 'call_req')
+    def test_ns_instantiate_when_fail_to_parse_nsd(self, mock_call_req):
+        mock_call_req.return_value = [1, "Failed to parse nsd", '500']
+        resp = self.client.post(self.url, data=self.req_data)
+        self.assertEqual(resp.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
+        self.assertIn("error", resp.data)