1 # Copyright (C) 2019 ZTE. All Rights Reserved.
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.
17 from django.test import TestCase
18 from rest_framework import status
19 from rest_framework.test import APIClient
20 from lcm.pub.database.models import NfInstModel, JobStatusModel, StorageInstModel, NetworkInstModel, \
21 SubNetworkInstModel, PortInstModel, FlavourInstModel, VmInstModel, VNFCInstModel
22 from lcm.pub.utils import restcall
23 from lcm.pub.vimapi import api
24 from lcm.pub.utils.jobutil import JobUtil
25 from lcm.nf.biz.scale_vnf import ScaleVnf
28 class TestNfScale(TestCase):
30 self.client = APIClient()
31 NfInstModel(nfinstid='12345',
37 vnfSoftwareVersion="V1",
40 status='NOT_INSTANTIATED').save()
43 "aspectId": "sunshine_aspect"
47 NfInstModel.objects.filter(nfinstid='12345').delete()
49 def assert_job_result(self, job_id, job_progress, job_detail):
50 jobs = JobStatusModel.objects.filter(
52 progress=job_progress,
55 self.assertEqual(1, len(jobs))
57 def test_scale_vnf_not_found(self):
58 url = "/api/vnflcm/v1/vnf_instances/12/scale"
59 response = self.client.post(url,
62 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code)
64 def test_scale_vnf_conflict(self):
65 url = "/api/vnflcm/v1/vnf_instances/12345/scale"
66 response = self.client.post(url,
69 self.assertEqual(status.HTTP_409_CONFLICT, response.status_code)
71 def test_scale_vnf_badreq(self):
72 NfInstModel(nfinstid='678',
78 vnfSoftwareVersion="V1",
81 status='INSTANTIATED').save()
82 url = "/api/vnflcm/v1/vnf_instances/678/scale"
83 response = self.client.post(url,
86 NfInstModel.objects.filter(nfinstid='678').delete()
87 self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
89 @mock.patch.object(restcall, 'call_req')
90 @mock.patch.object(api, 'call')
91 def test_scale_out_vnf_success(self, mock_call, mock_call_req):
92 self.nf_inst_id = '6789'
93 res_cache = {"volume": {}, "flavor": {}, "port": {}}
94 # res_cache["volume"]["volume_storage1"] = "vol1"
95 # res_cache["flavor"]["vdu1Id"] = "flavor1"
96 res_cache["port"]["ext_cp"] = "port1"
97 NfInstModel(nfinstid=self.nf_inst_id,
103 vnfSoftwareVersion="V1",
106 status='INSTANTIATED',
107 vnfd_model=json.dumps(const.vnfd_for_scale),
108 vimInfo=json.dumps({}),
109 resInfo=json.dumps(res_cache)).save()
110 StorageInstModel.objects.create(
115 instid=self.nf_inst_id,
118 NetworkInstModel.objects.create(
126 instid=self.nf_inst_id
128 SubNetworkInstModel.objects.create(
137 instid=self.nf_inst_id
139 PortInstModel.objects.create(
149 instid=self.nf_inst_id
151 FlavourInstModel.objects.create(
155 instid=self.nf_inst_id,
157 name="Flavor_sunshine"
159 VmInstModel.objects.create(
164 instid=self.nf_inst_id,
169 r1_apply_grant_result = [
171 json.JSONEncoder().encode(const.instantiate_grant_result),
174 mock_call_req.side_effect = [
175 r1_apply_grant_result,
177 mock_call.side_effect = [
178 const.c1_data_get_tenant_id,
179 const.c7_data_create_flavor,
180 const.c8_data_list_image,
181 const.c9_data_create_vm,
182 const.c10_data_get_vm
185 self.job_id = JobUtil.create_job('NF', 'SCALE', self.nf_inst_id)
186 JobUtil.add_job_status(self.job_id, 0, "VNF_SCALE_READY", )
189 {"type": "SCALE_OUT",
190 "aspectId": "sunshine_aspect"},
191 nf_inst_id=self.nf_inst_id,
195 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).delete()
196 self.assert_job_result(
202 @mock.patch.object(restcall, 'call_req')
203 @mock.patch.object(api, 'call')
204 def test_scale_in_vnf_success(self, mock_call, mock_call_req):
205 self.nf_inst_id = '6789'
206 res_cache = {"volume": {}, "flavor": {}, "port": {}}
207 # res_cache["volume"]["volume_storage1"] = "vol1"
208 # res_cache["flavor"]["vdu1Id"] = "flavor1"
209 res_cache["port"]["ext_cp"] = "port1"
210 NfInstModel(nfinstid=self.nf_inst_id,
216 vnfSoftwareVersion="V1",
219 status='INSTANTIATED',
220 vnfd_model=json.dumps(const.vnfd_for_scale),
221 vimInfo=json.dumps({}),
222 resInfo=json.dumps(res_cache)).save()
223 StorageInstModel.objects.create(
228 instid=self.nf_inst_id,
231 NetworkInstModel.objects.create(
239 instid=self.nf_inst_id
241 SubNetworkInstModel.objects.create(
250 instid=self.nf_inst_id
252 PortInstModel.objects.create(
262 instid=self.nf_inst_id
264 FlavourInstModel.objects.create(
268 instid=self.nf_inst_id,
270 name="Flavor_sunshine"
272 VmInstModel.objects.create(
277 instid=self.nf_inst_id,
282 VmInstModel.objects.create(
287 instid=self.nf_inst_id,
292 VNFCInstModel.objects.create(
294 instid=self.nf_inst_id,
297 VNFCInstModel.objects.create(
299 instid=self.nf_inst_id,
302 r1_apply_grant_result = [
304 json.JSONEncoder().encode(const.instantiate_grant_result),
307 mock_call_req.side_effect = [
308 r1_apply_grant_result,
310 mock_call.side_effect = [
311 const.c1_data_get_tenant_id,
312 const.c7_data_create_flavor,
313 const.c8_data_list_image,
314 const.c9_data_create_vm,
315 const.c10_data_get_vm
318 self.job_id = JobUtil.create_job('NF', 'SCALE', self.nf_inst_id)
319 JobUtil.add_job_status(self.job_id, 0, "VNF_SCALE_READY", )
323 "aspectId": "sunshine_aspect"},
324 nf_inst_id=self.nf_inst_id,
328 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).delete()
329 self.assert_job_result(