1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 from django.test import TestCase
20 from rest_framework import status
21 from rest_framework.test import APIClient
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
34 class TestNFInstantiate(TestCase):
36 self.client = APIClient()
41 "tenant": 'tenantname_1'
49 def assert_job_result(self, job_id, job_progress, job_detail):
50 jobs = JobStatusModel.objects.filter(jobid=job_id,
51 progress=job_progress,
53 self.assertEqual(1, len(jobs))
55 def test_create_vnf_identifier_when_vnf_is_exist(self):
56 NfInstModel.objects.create(nfinstid='1111',
63 status='NOT_INSTANTIATED',
64 nf_desc='vFW in Nanjing TIC Edge',
66 create_time=now_time())
69 "vnfInstanceName": "vFW_01",
70 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
72 response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
73 self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
74 context = json.loads(response.content)
75 self.assertEqual({'error': 'VNF is already exist.'}, context)
77 @mock.patch.object(restcall, 'call_req')
78 def test_create_vnf_identifier(self, mock_call_req):
79 r2_get_vnfpackage_from_catalog = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
80 mock_call_req.side_effect = [r2_get_vnfpackage_from_catalog]
83 "vnfInstanceName": "vFW_01",
84 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
86 response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
87 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
88 context = json.loads(response.content)
89 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
91 @mock.patch.object(InstVnf, 'run')
92 def test_instantiate_vnf(self, mock_run):
93 mock_run.re.return_value = None
94 response = self.client.post("/api/vnflcm/v1/vnf_instances/12/instantiate", data=inst_req_data, format='json')
95 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
97 def test_instantiate_vnf_when_inst_id_not_exist(self):
98 self.nf_inst_id = str(uuid.uuid4())
99 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
100 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
101 jobs = JobStatusModel.objects.filter(jobid=self.job_id,
103 descp="INST_VNF_READY")
104 self.assertEqual(1, len(jobs))
106 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
107 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
109 @mock.patch.object(restcall, 'call_req')
110 def test_instantiate_vnf_when_get_packageinfo_by_csarid_failed(self, mock_call_req):
111 NfInstModel.objects.create(nfinstid='1111',
118 status='NOT_INSTANTIATED',
119 nf_desc='vFW in Nanjing TIC Edge',
121 create_time=now_time())
122 r1_get_vnfpackage_by_vnfdid = [1, json.JSONEncoder().encode(vnfpackage_info), '200']
123 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid]
124 self.nf_inst_id = '1111'
125 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
126 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
128 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
129 self.assert_job_result(self.job_id, 255, "Failed to query vnf CSAR(111) from catalog.")
131 @mock.patch.object(restcall, 'call_req')
132 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
133 NfInstModel.objects.create(nfinstid='1111',
140 status='NOT_INSTANTIATED',
141 nf_desc='vFW in Nanjing TIC Edge',
143 create_time=now_time())
144 r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
145 r2_apply_grant_result = [1, json.JSONEncoder().encode(self.grant_result), '200']
146 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
147 self.nf_inst_id = '1111'
148 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
149 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
151 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
152 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
154 @mock.patch.object(restcall, 'call_req')
155 @mock.patch.object(api, 'call')
156 def test_instantiate_vnf_when_unexpected_exception(self, mock_call, mock_call_req):
157 NfInstModel.objects.create(nfinstid='1111',
164 status='NOT_INSTANTIATED',
165 nf_desc='vFW in Nanjing TIC Edge',
167 create_time=now_time())
168 r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
169 r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
170 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
171 mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume]
172 self.nf_inst_id = '1111'
173 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
174 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
176 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
177 self.assert_job_result(self.job_id, 255, "unexpected exception")
179 @mock.patch.object(restcall, 'call_req')
180 @mock.patch.object(api, 'call')
181 def test_instantiate_vnf_success(self, mock_call, mock_call_req):
182 NfInstModel.objects.create(nfinstid='1111',
189 status='NOT_INSTANTIATED',
190 nf_desc='vFW in Nanjing TIC Edge',
192 create_time=now_time())
193 r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
194 r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
195 r3_lcm_notify_result = [0, json.JSONEncoder().encode(''), '200']
196 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result, r3_lcm_notify_result]
197 mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
198 c4_data_create_network, c5_data_create_subnet, c6_data_create_port,
199 c7_data_create_flavor, c8_data_list_image, c9_data_create_vm, c10_data_get_vm]
200 self.nf_inst_id = '1111'
201 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
202 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
204 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
205 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")