Fix bug for status code when vnf lcm post request body invalid what client inputs 78/89978/2
authorhongyuzhao <zhao.hongyu@zte.com.cn>
Mon, 17 Jun 2019 06:57:55 +0000 (14:57 +0800)
committerhongyuzhao <zhao.hongyu@zte.com.cn>
Mon, 17 Jun 2019 07:25:05 +0000 (15:25 +0800)
Issue-ID: VFC-1420
Signed-off-by: hongyuzhao <zhao.hongyu@zte.com.cn>
Change-Id: I07bca00f677ebc9367492ed7d9d44be4e02d9877

lcm/lcm/nf/tests/test_change_ext_conn.py
lcm/lcm/nf/tests/test_change_flavour.py
lcm/lcm/nf/tests/test_heal_vnf.py
lcm/lcm/nf/tests/test_instantiate_vnf.py
lcm/lcm/nf/tests/test_operate_vnf.py
lcm/lcm/nf/tests/test_scale_vnf.py
lcm/lcm/nf/tests/test_scale_vnf_to_level.py
lcm/lcm/nf/views/common.py

index 8f304d1..7efb416 100644 (file)
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+import mock
+
 from django.test import TestCase
 from rest_framework import status
 from rest_framework.test import APIClient
 
 from lcm.pub.database.models import NfInstModel
+from lcm.pub.exceptions import NFLCMException
+from lcm.pub.utils.jobutil import JobUtil
 
 
 class TestChangeExtConn(TestCase):
@@ -95,9 +99,20 @@ class TestChangeExtConn(TestCase):
                                     format='json')
         self.failUnlessEqual(status.HTTP_409_CONFLICT, response.status_code)
 
-    def test_change_ext_conn_inner_error(self):
+    def test_change_ext_conn_badreq(self):
         url = "/api/vnflcm/v1/vnf_instances/123/change_ext_conn"
         response = self.client.post(url,
                                     data={},
                                     format='json')
-        self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
+        self.failUnlessEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
+
+    @mock.patch.object(JobUtil, 'create_job')
+    def test_change_ext_conn_inner_error(self, mock_run):
+        mock_run.return_value = NFLCMException('Boom!')
+        url = "/api/vnflcm/v1/vnf_instances/123/change_ext_conn"
+        response = self.client.post(url,
+                                    data=self.req_data,
+                                    format='json')
+        self.assertEqual(
+            status.HTTP_500_INTERNAL_SERVER_ERROR,
+            response.status_code)
index 7c15dfb..15b53f0 100644 (file)
@@ -63,9 +63,9 @@ class TestFlavour(TestCase):
                                     format='json')
         self.failUnlessEqual(status.HTTP_409_CONFLICT, response.status_code)
 
-    def test_change_flavour_inner_error(self):
+    def test_change_flavour_badreq(self):
         url = "/api/vnflcm/v1/vnf_instances/345/change_flavour"
         response = self.client.post(url,
                                     data={},
                                     format='json')
-        self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
+        self.failUnlessEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
index cae9cd3..d5c70da 100644 (file)
@@ -64,7 +64,7 @@ class TestNFInstantiate(TestCase):
         self.failUnlessEqual(status.HTTP_409_CONFLICT, response.status_code)\r
         NfInstModel.objects.filter(nfinstid='1267').delete()\r
 \r
-    def test_heal_vnf_inner_error(self):\r
+    def test_heal_vnf_badreq(self):\r
         NfInstModel(nfinstid='345',\r
                     nf_name='VNF1',\r
                     nf_desc="VNF DESC",\r
@@ -80,7 +80,7 @@ class TestNFInstantiate(TestCase):
                                     data={"additionalParams": "1"},\r
                                     format='json')\r
         NfInstModel.objects.filter(nfinstid='345').delete()\r
-        self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)\r
+        self.failUnlessEqual(status.HTTP_400_BAD_REQUEST, response.status_code)\r
 \r
     @mock.patch.object(HealVnf, 'run')\r
     def test_heal_vnf_success(self, mock_run):\r
index 76fd83b..c624e05 100644 (file)
@@ -42,6 +42,7 @@ from lcm.pub.utils.jobutil import JobUtil
 from lcm.pub.utils.timeutil import now_time\r
 from lcm.pub.utils.notificationsutil import NotificationsUtil\r
 from lcm.pub.vimapi import api\r
+from lcm.pub.exceptions import NFLCMException\r
 \r
 from lcm.nf.biz.instantiate_vnf import InstantiateVnf\r
 \r
@@ -389,3 +390,33 @@ class TestNFInstantiate(TestCase):
             nf_inst_id=self.nf_inst_id,\r
             job_id=self.job_id\r
         ).run()\r
+\r
+    @mock.patch.object(JobUtil, 'create_job')\r
+    def test_instantiate_inner_error(self, mock_run):\r
+        NfInstModel(\r
+            nfinstid='144',\r
+            nf_name='VNF1',\r
+            status='NOT_INSTANTIATED'\r
+        ).save()\r
+        mock_run.return_value = NFLCMException('Boom!')\r
+        response = self.client.post(\r
+            '/api/vnflcm/v1/vnf_instances/144/instantiate',\r
+            data=inst_req_data,\r
+            format='json'\r
+        )\r
+        NfInstModel.objects.filter(nfinstid='144').delete()\r
+        self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)\r
+\r
+    def test_instantiate_badreq(self):\r
+        NfInstModel(\r
+            nfinstid='144',\r
+            nf_name='VNF1',\r
+            status='NOT_INSTANTIATED'\r
+        ).save()\r
+        response = self.client.post(\r
+            '/api/vnflcm/v1/vnf_instances/144/instantiate',\r
+            data={},\r
+            format='json'\r
+        )\r
+        NfInstModel.objects.filter(nfinstid='144').delete()\r
+        self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)\r
index 296abad..cffcf37 100644 (file)
@@ -78,7 +78,7 @@ class TestNFOperate(TestCase):
             status='NOT_INSTANTIATED'\r
         ).delete()\r
 \r
-    def test_operate_vnf_inner_error(self):\r
+    def test_operate_vnf_badreq(self):\r
         NfInstModel(\r
             nfinstid='345',\r
             nf_name='VNF1',\r
@@ -99,7 +99,7 @@ class TestNFOperate(TestCase):
         )\r
         NfInstModel.objects.filter(nfinstid='345').delete()\r
         self.failUnlessEqual(\r
-            status.HTTP_500_INTERNAL_SERVER_ERROR,\r
+            status.HTTP_400_BAD_REQUEST,\r
             response.status_code\r
         )\r
 \r
index 60cabbd..a43c475 100644 (file)
@@ -53,7 +53,7 @@ class TestNfScale(TestCase):
                                     format='json')
         self.failUnlessEqual(status.HTTP_409_CONFLICT, response.status_code)
 
-    def test_scale_vnf_inner_error(self):
+    def test_scale_vnf_badreq(self):
         NfInstModel(nfinstid='678',
                     nf_name='VNF1',
                     nf_desc="VNF DESC",
@@ -69,4 +69,4 @@ class TestNfScale(TestCase):
                                     data={},
                                     format='json')
         NfInstModel.objects.filter(nfinstid='678').delete()
-        self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
+        self.failUnlessEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
index 657a19f..7c7b98d 100644 (file)
@@ -53,7 +53,7 @@ class TestNfScaleToLevel(TestCase):
                                     format='json')
         self.failUnlessEqual(status.HTTP_409_CONFLICT, response.status_code)
 
-    def test_scale_to_level_inner_error(self):
+    def test_scale_to_level_badreq(self):
         NfInstModel(nfinstid='678',
                     nf_name='VNF1',
                     nf_desc="VNF DESC",
@@ -69,4 +69,4 @@ class TestNfScaleToLevel(TestCase):
                                     data={},
                                     format='json')
         NfInstModel.objects.filter(nfinstid='678').delete()
-        self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
+        self.failUnlessEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
index a3c87a3..e24a8bc 100644 (file)
@@ -103,7 +103,7 @@ def deal_vnf_action(logger, opt_type, opt_status, instid, req, req_serializer, a
 
     act_vnf_req_serializer = req_serializer(data=req.data)
     if not act_vnf_req_serializer.is_valid():
-        raise NFLCMException(act_vnf_req_serializer.errors)
+        raise NFLCMExceptionBadRequest(act_vnf_req_serializer.errors)
 
     vnf_insts = NfInstModel.objects.filter(nfinstid=instid)
     if not vnf_insts.exists():