Add ETag check ut case 44/85944/1
authorfujinhua <fu.jinhua@zte.com.cn>
Mon, 22 Apr 2019 08:32:32 +0000 (16:32 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Mon, 22 Apr 2019 08:32:32 +0000 (16:32 +0800)
Change-Id: I8574a69afbbe05886ee6561630ceab4dc8fc30b4
Issue-ID: VFC-1306
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
lcm/lcm/nf/tests/test_update_vnf.py
lcm/lcm/nf/views/common.py

index 484885a..ce1cd6f 100644 (file)
@@ -15,6 +15,7 @@
 import mock
 from django.test import TestCase
 from rest_framework.test import APIClient
+from rest_framework.test import RequestsClient
 from rest_framework import status
 
 from lcm.pub.utils import restcall
@@ -40,6 +41,29 @@ class TestNFUpdate(TestCase):
                                      format='json')
         self.failUnlessEqual(status.HTTP_404_NOT_FOUND, response.status_code)
 
+    def test_update_vnf_etag_not_match(self):
+        instanceid = "19"
+        NfInstModel(nfinstid=instanceid,
+                    nf_name='VNF1',
+                    nf_desc="VNF DESC",
+                    vnfdid="1",
+                    netype="XGW",
+                    vendor="ZTE",
+                    vnfSoftwareVersion="V1",
+                    version="V1",
+                    package_id="2",
+                    status='INSTANTIATED').save()
+        rc = RequestsClient()
+        response = rc.patch("http://localhost:8801/api/vnflcm/v1/vnf_instances/19",
+                            json=self.upd_data,
+                            headers={
+                                "Accept": "application/json",
+                                "Content-Type": "application/json",
+                                "If-Match": "test_etag"
+                            })
+        NfInstModel.objects.filter(nfinstid=instanceid).delete()
+        self.failUnlessEqual(status.HTTP_412_PRECONDITION_FAILED, response.status_code)
+
     @mock.patch.object(restcall, 'call_req')
     def test_update_vnf_success(self, mock_call_req):
         instanceid = "12"
@@ -57,4 +81,5 @@ class TestNFUpdate(TestCase):
         job_id = JobUtil.create_job('NF', 'UPDATETEST', instanceid)
         UpdateVnf(self.upd_data, instanceid, job_id).run()
         name = NfInstModel.objects.filter(nfinstid=instanceid).get().nf_name
+        NfInstModel.objects.filter(nfinstid=instanceid).delete()
         self.failUnlessEqual("vnf new name", name)
index 57e6a62..4f86426 100644 (file)
@@ -117,11 +117,10 @@ def deal_vnf_action(logger, opt_type, opt_status, instid, req, req_serializer, a
             raise NFLCMExceptionConflict("VNF(%s) is not INSTANTIATED." % instid)
 
     req_etag = None
-    is_upd_vnf_opt = (opt_type == "UpdateVnf")
-    if is_upd_vnf_opt:
+    if opt_type == OPERATION_TYPE.MODIFY_INFO:
         req_etag = req.META.get("HTTP_IF_MATCH")
         logger.debug("req_etag=%s, CACHE_ETAG=%s", req_etag, CACHE_ETAG)
-        if req_etag != CACHE_ETAG:
+        if req_etag and req_etag != CACHE_ETAG:
             raise NFLCMExceptionPreconditionFailed("Etag mismatch")
 
     job_id = JobUtil.create_job('NF', opt_type, instid)
@@ -131,7 +130,7 @@ def deal_vnf_action(logger, opt_type, opt_status, instid, req, req_serializer, a
     act_task(req.data, instid, job_id).start()
 
     resp = Response(data={"jobId": job_id}, status=status.HTTP_202_ACCEPTED)
-    if is_upd_vnf_opt:
+    if opt_type == OPERATION_TYPE.MODIFY_INFO:
         resp["ETag"] = req_etag
     return resp