Add ETag check ok ut case
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / tests / test_update_vnf.py
1 # Copyright 2019 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import mock
16 from django.test import TestCase
17 from rest_framework.test import APIClient
18 from rest_framework.test import RequestsClient
19 from rest_framework import status
20
21 from lcm.pub.utils import restcall
22 from lcm.pub.database.models import NfInstModel
23 from lcm.pub.utils.jobutil import JobUtil
24 from lcm.nf.biz.update_vnf import UpdateVnf
25
26
27 class TestNFUpdate(TestCase):
28     def setUp(self):
29         self.client = APIClient()
30         self.rc = RequestsClient()
31         self.upd_data = {
32             "vnfInstanceName": "vnf new name",
33             "vnfInstanceDescription": "new description"
34         }
35
36     def tearDown(self):
37         pass
38
39     def test_update_vnf_not_exist(self):
40         response = self.client.patch("/api/vnflcm/v1/vnf_instances/1111",
41                                      data=self.upd_data,
42                                      format='json')
43         self.failUnlessEqual(status.HTTP_404_NOT_FOUND, response.status_code)
44
45     def test_update_vnf_etag_not_match(self):
46         instanceid = "19"
47         NfInstModel(nfinstid=instanceid,
48                     nf_name='VNF1',
49                     nf_desc="VNF DESC",
50                     vnfdid="1",
51                     netype="XGW",
52                     vendor="ZTE",
53                     vnfSoftwareVersion="V1",
54                     version="V1",
55                     package_id="2",
56                     status='INSTANTIATED').save()
57         response = self.rc.patch("http://localhost:8801/api/vnflcm/v1/vnf_instances/19",
58                                  json=self.upd_data,
59                                  headers={
60                                      "Accept": "application/json",
61                                      "Content-Type": "application/json",
62                                      "If-Match": "test_etag"
63                                  })
64         NfInstModel.objects.filter(nfinstid=instanceid).delete()
65         self.failUnlessEqual(status.HTTP_412_PRECONDITION_FAILED, response.status_code)
66
67     @mock.patch.object(UpdateVnf, 'run')
68     def test_update_vnf_etag_match(self, mock_run):
69         instanceid = "18"
70         NfInstModel(nfinstid=instanceid,
71                     nf_name='VNF1',
72                     nf_desc="VNF DESC",
73                     vnfdid="1",
74                     netype="XGW",
75                     vendor="ZTE",
76                     vnfSoftwareVersion="V1",
77                     version="V1",
78                     package_id="2",
79                     status='INSTANTIATED').save()
80         resp = self.client.get("/api/vnflcm/v1/vnf_instances/18", format='json')
81         etag = resp["ETag"]
82         response = self.rc.patch("http://localhost:8801/api/vnflcm/v1/vnf_instances/18",
83                                  json=self.upd_data,
84                                  headers={
85                                      "Accept": "application/json",
86                                      "Content-Type": "application/json",
87                                      "If-Match": etag
88                                  })
89         NfInstModel.objects.filter(nfinstid=instanceid).delete()
90         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
91
92     @mock.patch.object(restcall, 'call_req')
93     def test_update_vnf_success(self, mock_call_req):
94         instanceid = "12"
95         NfInstModel(nfinstid=instanceid,
96                     nf_name='VNF1',
97                     nf_desc="VNF DESC",
98                     vnfdid="1",
99                     netype="XGW",
100                     vendor="ZTE",
101                     vnfSoftwareVersion="V1",
102                     version="V1",
103                     package_id="2",
104                     status='INSTANTIATED').save()
105         mock_call_req.return_value = [0, {}, status.HTTP_202_ACCEPTED]
106         job_id = JobUtil.create_job('NF', 'UPDATETEST', instanceid)
107         UpdateVnf(self.upd_data, instanceid, job_id).run()
108         name = NfInstModel.objects.filter(nfinstid=instanceid).get().nf_name
109         NfInstModel.objects.filter(nfinstid=instanceid).delete()
110         self.failUnlessEqual("vnf new name", name)