Fix vfc-vnflcm unit tests grant response data
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / tests / test_vnf_create.py
1 # Copyright 2017 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 json
16 import uuid
17
18 import mock
19 from django.test import TestCase
20 from rest_framework import status
21 from rest_framework.test import APIClient
22
23 from lcm.nf.const import c1_data_get_tenant_id, c4_data_create_network, c2_data_create_volume, \
24     c5_data_create_subnet, c3_data_get_volume, c6_data_create_port, c7_data_create_flavor, c8_data_list_image, \
25     c9_data_create_vm, c10_data_get_vm, inst_req_data, vnfpackage_info
26 from lcm.nf.vnf_create.inst_vnf import InstVnf
27 from lcm.pub.database.models import NfInstModel, JobStatusModel
28 from lcm.pub.utils import restcall
29 from lcm.pub.utils.jobutil import JobUtil
30 from lcm.pub.utils.timeutil import now_time
31 from lcm.pub.vimapi import api
32
33
34 class TestNFInstantiate(TestCase):
35     def setUp(self):
36         self.client = APIClient()
37         self.grant_result = {
38             "vimid": 'vimid_1',
39             "tenant": 'tenantname_1'
40         }
41
42     def tearDown(self):
43         pass
44
45     def assert_job_result(self, job_id, job_progress, job_detail):
46         jobs = JobStatusModel.objects.filter(jobid=job_id,
47                                              progress=job_progress,
48                                              descp=job_detail)
49         self.assertEqual(1, len(jobs))
50
51     def test_create_vnf_identifier_when_vnf_is_exist(self):
52         NfInstModel.objects.create(nfinstid='1111',
53                                    nf_name='vFW_01',
54                                    package_id='222',
55                                    version='',
56                                    vendor='',
57                                    netype='',
58                                    vnfd_model='',
59                                    status='NOT_INSTANTIATED',
60                                    nf_desc='vFW in Nanjing TIC Edge',
61                                    vnfdid='111',
62                                    create_time=now_time())
63         data = {
64             "vnfdId": "111",
65             "vnfInstanceName": "vFW_01",
66             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
67         }
68         response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
69         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
70         context = json.loads(response.content)
71         self.assertEqual({'error': 'VNF is already exist.'}, context)
72
73     @mock.patch.object(restcall, 'call_req')
74     def test_create_vnf_identifier(self, mock_call_req):
75         r2_get_vnfpackage_from_catalog = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
76         mock_call_req.side_effect = [r2_get_vnfpackage_from_catalog]
77         data = {
78             "vnfdId": "111",
79             "vnfInstanceName": "vFW_01",
80             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
81         }
82         response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
83         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
84         context = json.loads(response.content)
85         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
86
87     @mock.patch.object(InstVnf, 'run')
88     def test_instantiate_vnf(self, mock_run):
89         mock_run.re.return_value = None
90         response = self.client.post("/api/vnflcm/v1/vnf_instances/12/instantiate", data=inst_req_data, format='json')
91         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
92
93     def test_instantiate_vnf_when_inst_id_not_exist(self):
94         self.nf_inst_id = str(uuid.uuid4())
95         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
96         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
97         jobs = JobStatusModel.objects.filter(jobid=self.job_id,
98                                              progress=0,
99                                              descp="INST_VNF_READY")
100         self.assertEqual(1, len(jobs))
101         data = inst_req_data
102         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
103         self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
104
105     @mock.patch.object(restcall, 'call_req')
106     def test_instantiate_vnf_when_get_packageinfo_by_csarid_failed(self, mock_call_req):
107         NfInstModel.objects.create(nfinstid='1111',
108                                    nf_name='vFW_01',
109                                    package_id='222',
110                                    version='',
111                                    vendor='',
112                                    netype='',
113                                    vnfd_model='',
114                                    status='NOT_INSTANTIATED',
115                                    nf_desc='vFW in Nanjing TIC Edge',
116                                    vnfdid='111',
117                                    create_time=now_time())
118         r1_get_vnfpackage_by_vnfdid = [1, json.JSONEncoder().encode(vnfpackage_info), '200']
119         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid]
120         self.nf_inst_id = '1111'
121         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
122         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
123         data = inst_req_data
124         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
125         self.assert_job_result(self.job_id, 255, "Failed to query vnf CSAR(111) from catalog.")
126
127     @mock.patch.object(restcall, 'call_req')
128     def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
129         NfInstModel.objects.create(nfinstid='1111',
130                                    nf_name='vFW_01',
131                                    package_id='222',
132                                    version='',
133                                    vendor='',
134                                    netype='',
135                                    vnfd_model='',
136                                    status='NOT_INSTANTIATED',
137                                    nf_desc='vFW in Nanjing TIC Edge',
138                                    vnfdid='111',
139                                    create_time=now_time())
140         r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
141         r2_apply_grant_result = [1, json.JSONEncoder().encode(self.grant_result), '200']
142         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
143         self.nf_inst_id = '1111'
144         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
145         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
146         data = inst_req_data
147         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
148         self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
149
150     @mock.patch.object(restcall, 'call_req')
151     @mock.patch.object(api, 'call')
152     def test_instantiate_vnf_when_unexpected_exception(self, mock_call, mock_call_req):
153         NfInstModel.objects.create(nfinstid='1111',
154                                    nf_name='vFW_01',
155                                    package_id='222',
156                                    version='',
157                                    vendor='',
158                                    netype='',
159                                    vnfd_model='',
160                                    status='NOT_INSTANTIATED',
161                                    nf_desc='vFW in Nanjing TIC Edge',
162                                    vnfdid='111',
163                                    create_time=now_time())
164         r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
165         r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
166         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
167         mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume]
168         self.nf_inst_id = '1111'
169         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
170         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
171         data = inst_req_data
172         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
173         self.assert_job_result(self.job_id, 255, "unexpected exception")
174
175     @mock.patch.object(restcall, 'call_req')
176     @mock.patch.object(api, 'call')
177     def test_instantiate_vnf_success(self, mock_call, mock_call_req):
178         NfInstModel.objects.create(nfinstid='1111',
179                                    nf_name='vFW_01',
180                                    package_id='222',
181                                    version='',
182                                    vendor='',
183                                    netype='',
184                                    vnfd_model='',
185                                    status='NOT_INSTANTIATED',
186                                    nf_desc='vFW in Nanjing TIC Edge',
187                                    vnfdid='111',
188                                    create_time=now_time())
189         r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
190         r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
191         r3_lcm_notify_result = [0, json.JSONEncoder().encode(''), '200']
192         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result, r3_lcm_notify_result]
193         mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
194                                  c4_data_create_network, c5_data_create_subnet, c6_data_create_port,
195                                  c7_data_create_flavor, c8_data_list_image, c9_data_create_vm, c10_data_get_vm]
196         self.nf_inst_id = '1111'
197         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
198         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
199         data = inst_req_data
200         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
201         self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")