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()
39 "tenant": 'tenantname_1'
45 def assert_job_result(self, job_id, job_progress, job_detail):
46 jobs = JobStatusModel.objects.filter(jobid=job_id,
47 progress=job_progress,
49 self.assertEqual(1, len(jobs))
51 def test_create_vnf_identifier_when_vnf_is_exist(self):
52 NfInstModel.objects.create(nfinstid='1111',
59 status='NOT_INSTANTIATED',
60 nf_desc='vFW in Nanjing TIC Edge',
62 create_time=now_time())
65 "vnfInstanceName": "vFW_01",
66 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
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)
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]
79 "vnfInstanceName": "vFW_01",
80 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
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())
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)
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,
99 descp="INST_VNF_READY")
100 self.assertEqual(1, len(jobs))
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.")
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',
114 status='NOT_INSTANTIATED',
115 nf_desc='vFW in Nanjing TIC Edge',
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")
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.")
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',
136 status='NOT_INSTANTIATED',
137 nf_desc='vFW in Nanjing TIC Edge',
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")
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")
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',
160 status='NOT_INSTANTIATED',
161 nf_desc='vFW in Nanjing TIC Edge',
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")
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")
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',
185 status='NOT_INSTANTIATED',
186 nf_desc='vFW in Nanjing TIC Edge',
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")
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.")