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.
16 from django.test import TestCase
17 from rest_framework import status
18 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_to_level import ScaleVnfToLevel
28 class TestNfScaleToLevel(TestCase):
30 self.client = APIClient()
32 "instantiationLevelId": "instantiation_level_2",
35 NfInstModel(nfinstid='98765',
41 vnfSoftwareVersion="V1",
44 status='NOT_INSTANTIATED').save()
47 NfInstModel.objects.filter(nfinstid='98765').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_to_level_not_found(self):
58 url = "/api/vnflcm/v1/vnf_instances/12/scale_to_level"
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/98765/scale_to_level"
66 response = self.client.post(url,
69 self.assertEqual(status.HTTP_409_CONFLICT, response.status_code)
71 def test_scale_to_level_badreq(self):
72 NfInstModel(nfinstid='678',
78 vnfSoftwareVersion="V1",
81 status='INSTANTIATED').save()
82 url = "/api/vnflcm/v1/vnf_instances/678/scale_to_level"
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["port"]["ext_cp"] = "port1"
95 NfInstModel(nfinstid=self.nf_inst_id,
101 vnfSoftwareVersion="V1",
104 status='INSTANTIATED',
105 vnfd_model=json.dumps(const.vnfd_for_scale),
106 vimInfo=json.dumps({}),
107 resInfo=json.dumps(res_cache)).save()
108 StorageInstModel.objects.create(
113 instid=self.nf_inst_id,
116 NetworkInstModel.objects.create(
124 instid=self.nf_inst_id
126 SubNetworkInstModel.objects.create(
135 instid=self.nf_inst_id
137 PortInstModel.objects.create(
147 instid=self.nf_inst_id
149 FlavourInstModel.objects.create(
153 instid=self.nf_inst_id,
155 name="Flavor_sunshine"
157 VmInstModel.objects.create(
162 instid=self.nf_inst_id,
167 r1_apply_grant_result = [
169 json.JSONEncoder().encode(const.instantiate_grant_result),
172 mock_call_req.side_effect = [
173 r1_apply_grant_result,
175 mock_call.side_effect = [
176 const.c1_data_get_tenant_id,
177 const.c7_data_create_flavor,
178 const.c8_data_list_image,
179 const.c9_data_create_vm,
180 const.c10_data_get_vm
183 self.job_id = JobUtil.create_job('NF', 'SCALE', self.nf_inst_id)
184 JobUtil.add_job_status(self.job_id, 0, "VNF_SCALE_READY", )
187 {"instantiationLevelId": "instantiation_level_2"},
188 nf_inst_id=self.nf_inst_id,
192 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).delete()
193 self.assert_job_result(
199 @mock.patch.object(restcall, 'call_req')
200 @mock.patch.object(api, 'call')
201 def test_scale_to_inlevel_vnf_success(self, mock_call, mock_call_req):
202 self.nf_inst_id = '6789'
203 res_cache = {"volume": {}, "flavor": {}, "port": {}}
204 res_cache["port"]["ext_cp"] = "port1"
205 NfInstModel(nfinstid=self.nf_inst_id,
211 vnfSoftwareVersion="V1",
214 status='INSTANTIATED',
215 vnfd_model=json.dumps(const.vnfd_for_scale),
216 vimInfo=json.dumps({}),
217 resInfo=json.dumps(res_cache)).save()
218 StorageInstModel.objects.create(
223 instid=self.nf_inst_id,
226 NetworkInstModel.objects.create(
234 instid=self.nf_inst_id
236 SubNetworkInstModel.objects.create(
245 instid=self.nf_inst_id
247 PortInstModel.objects.create(
257 instid=self.nf_inst_id
259 FlavourInstModel.objects.create(
263 instid=self.nf_inst_id,
265 name="Flavor_sunshine"
267 VmInstModel.objects.create(
272 instid=self.nf_inst_id,
277 VmInstModel.objects.create(
282 instid=self.nf_inst_id,
287 VNFCInstModel.objects.create(
289 instid=self.nf_inst_id,
292 VNFCInstModel.objects.create(
294 instid=self.nf_inst_id,
297 r1_apply_grant_result = [
299 json.JSONEncoder().encode(const.instantiate_grant_result),
302 mock_call_req.side_effect = [
303 r1_apply_grant_result,
305 mock_call.side_effect = [
306 const.c1_data_get_tenant_id,
307 const.c7_data_create_flavor,
308 const.c8_data_list_image,
309 const.c9_data_create_vm,
310 const.c10_data_get_vm
313 self.job_id = JobUtil.create_job('NF', 'SCALE', self.nf_inst_id)
314 JobUtil.add_job_status(self.job_id, 0, "VNF_SCALE_READY", )
317 {"scaleInfo": {"aspectId": "sunshine_aspect",
319 nf_inst_id=self.nf_inst_id,
323 NfInstModel.objects.filter(nfinstid=self.nf_inst_id).delete()
324 self.assert_job_result(