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, Client
20 from rest_framework import status
22 from lcm.nf.vnfs.const import c1_data_get_tenant_id, c4_data_create_network, c2_data_create_volume, \
23 c5_data_create_subnet, c3_data_get_volume, c6_data_create_port, c7_data_create_flavor, c8_data_list_image, \
24 c9_data_create_vm, c10_data_get_vm, inst_req_data, vnfpackage_info
25 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
26 from lcm.pub.database.models import NfInstModel, JobStatusModel
27 from lcm.pub.utils import restcall
28 from lcm.pub.utils.jobutil import JobUtil
29 from lcm.pub.utils.timeutil import now_time
30 from lcm.pub.vimapi import api
33 class TestNFInstantiate(TestCase):
35 self.client = Client()
40 "tenant": 'tenantname_1'
48 def assert_job_result(self, job_id, job_progress, job_detail):
49 jobs = JobStatusModel.objects.filter(jobid=job_id,
50 progress=job_progress,
52 self.assertEqual(1, len(jobs))
54 @mock.patch.object(restcall, 'call_req')
55 def test_create_vnf_identifier(self, mock_call_req):
56 r2_get_vnfpackage_from_catalog = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
57 mock_call_req.side_effect = [r2_get_vnfpackage_from_catalog]
60 "vnfInstanceName": "vFW_01",
61 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
63 response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
64 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
65 context = json.loads(response.content)
66 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
68 @mock.patch.object(InstVnf, 'run')
69 def test_instantiate_vnf(self, mock_run):
70 mock_run.re.return_value = None
71 response = self.client.post("/api/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
72 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
74 def test_instantiate_vnf_when_inst_id_not_exist(self):
75 self.nf_inst_id = str(uuid.uuid4())
76 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
77 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
78 jobs = JobStatusModel.objects.filter(jobid=self.job_id,
80 descp="INST_VNF_READY")
81 self.assertEqual(1, len(jobs))
83 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
84 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
86 @mock.patch.object(restcall, 'call_req')
87 def test_instantiate_vnf_when_get_rawdata_by_csarid_failed(self, mock_call_req):
88 NfInstModel.objects.create(nfinstid='1111',
95 status='NOT_INSTANTIATED',
96 nf_desc='vFW in Nanjing TIC Edge',
98 create_time=now_time())
99 r1_get_vnfpackage_by_vnfdid = [1, json.JSONEncoder().encode(vnfpackage_info), '200']
100 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid]
101 self.nf_inst_id = '1111'
102 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
103 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
105 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
106 self.assert_job_result(self.job_id, 255, "Failed to query vnf CSAR(111) from catalog.")
108 @mock.patch.object(restcall, 'call_req')
109 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
110 NfInstModel.objects.create(nfinstid='1111',
117 status='NOT_INSTANTIATED',
118 nf_desc='vFW in Nanjing TIC Edge',
120 create_time=now_time())
121 r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
122 r2_apply_grant_result = [1, json.JSONEncoder().encode(self.grant_result), '200']
123 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
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, "Nf instancing apply grant exception")
131 @mock.patch.object(restcall, 'call_req')
132 @mock.patch.object(api, 'call')
133 def test_instantiate_vnf_when_unexpected_exception(self, mock_call, mock_call_req):
134 NfInstModel.objects.create(nfinstid='1111',
141 status='NOT_INSTANTIATED',
142 nf_desc='vFW in Nanjing TIC Edge',
144 create_time=now_time())
145 r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
146 r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
147 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
148 mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume]
149 self.nf_inst_id = '1111'
150 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
151 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
153 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
154 self.assert_job_result(self.job_id, 255, "unexpected exception")
156 @mock.patch.object(restcall, 'call_req')
157 @mock.patch.object(api, 'call')
158 def test_instantiate_vnf_success(self, mock_call, mock_call_req):
159 NfInstModel.objects.create(nfinstid='1111',
166 status='NOT_INSTANTIATED',
167 nf_desc='vFW in Nanjing TIC Edge',
169 create_time=now_time())
170 r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
171 r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
172 r3_lcm_notify_result = [0, json.JSONEncoder().encode(''), '200']
173 mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result, r3_lcm_notify_result]
174 mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
175 c4_data_create_network, c5_data_create_subnet, c6_data_create_port,
176 c7_data_create_flavor, c8_data_list_image, c9_data_create_vm, c10_data_get_vm]
177 self.nf_inst_id = '1111'
178 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
179 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
181 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
182 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")