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 Client
20 from django.test import TestCase
21 from rest_framework import status
22 from lcm.ns.const import NS_INST_STATUS
23 from lcm.ns.ns_manual_scale import NSManualScaleService
24 from lcm.pub.database.models import NSInstModel, JobModel, NfInstModel
25 from lcm.pub.exceptions import NSLCMException
26 from lcm.pub.utils import restcall
27 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS
28 from lcm.pub.msapi import catalog
29 from lcm.pub.utils.scaleaspect import get_json_data
30 from lcm.pub.utils import idutil
37 "ns_scale_aspect": "TIC_CORE_IMS",
43 "vnfd_id": "zte_ims_cscf",
44 "vnf_scale_aspect": "mpu",
48 "vnfd_id": "zte_ims_hss",
49 "vnf_scale_aspect": "fpu",
58 "vnfd_id": "zte_ims_cscf",
59 "vnf_scale_aspect": "mpu",
63 "vnfd_id": "zte_ims_hss",
64 "vnf_scale_aspect": "fpu",
73 "ns_scale_aspect": "TIC_EDGE_EPC",
79 "vnfd_id": "zte_epc_spgw",
80 "vnf_scale_aspect": "gpu",
84 "vnfd_id": "zte_epc_tas",
85 "vnf_scale_aspect": "fpu",
94 "vnfd_id": "zte_epc_spgw",
95 "vnf_scale_aspect": "mpu",
99 "vnfd_id": "zte_epc_tas",
100 "vnf_scale_aspect": "fpu",
111 class TestNsManualScale(TestCase):
113 self.ns_inst_id = str(uuid.uuid4())
114 self.job_id = JobUtil.create_job(
115 "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
116 self.package_id = "7"
117 self.client = Client()
118 idutil.get_auto_id = mock.Mock()
119 idutil.get_auto_id.return_value = random.random()
123 nspackage_id=self.package_id,
126 self.init_scaling_map_json()
129 NSInstModel.objects.filter().delete()
130 JobModel.objects.filter().delete()
132 def init_scaling_map_json(self):
133 curdir_path = os.path.dirname(
136 os.path.abspath(__file__))))
137 filename = curdir_path + "/ns/data/scalemapping.json"
138 self.scaling_map_json = get_json_data(filename)
140 def insert_new_ns(self):
141 ns_inst_id = str(uuid.uuid4())
142 job_id = JobUtil.create_job(
143 "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
148 nspackage_id=package_id,
149 nsd_id=package_id).save()
150 return ns_inst_id, job_id
152 def insert_new_nf(self):
153 # Create a third vnf instance
154 self.nf_name = "name_1"
156 self.vnfm_inst_id = "1"
158 package_id = "nf_zte_hss"
159 nf_uuid = "ab34-3g5j-de13-ab85-ij93"
161 NfInstModel.objects.create(
163 nf_name=self.nf_name,
165 vnfm_inst_id=self.vnfm_inst_id,
166 ns_inst_id=self.ns_inst_id,
174 package_id=package_id,
175 vnfd_model='{"metadata": {"vnfdId": "1","vnfdName": "PGW001",'
176 '"vnfProvider": "zte","vnfdVersion": "V00001","vnfVersion": "V5.10.20",'
177 '"productType": "CN","vnfType": "PGW",'
178 '"description": "PGW VNFD description",'
179 '"isShared":true,"vnfExtendType":"driver"}}')
181 @mock.patch.object(NSManualScaleService, 'run')
182 def test_ns_manual_scale(self, mock_run):
184 "scaleType": "SCALE_NS",
186 "scaleNsByStepsData": [{
189 "scalingDirection": "0"
193 response = self.client.post(
194 "/api/nslcm/v1/ns/%s/scale" %
195 self.ns_inst_id, data=data)
196 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
198 def test_ns_manual_scale_error_scaletype(self):
200 "scaleType": "SCALE_ERR",
202 "scaleNsByStepsData": [{
203 "aspectId": "sss_zte",
205 "scalingDirection": "0"
209 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
210 jobs = JobModel.objects.filter(jobid=self.job_id)
211 self.assertEqual(255, jobs[0].progress)
213 def test_ns_manual_scale_error_nsd_id(self):
215 "scaleType": "SCALE_NS",
217 "scaleNsByStepsData": [{
218 "aspectId": "sss_zte",
220 "scalingDirection": "0"
224 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
225 jobs = JobModel.objects.filter(jobid=self.job_id)
226 self.assertEqual(255, jobs[0].progress)
228 def test_ns_manual_scale_error_aspect(self):
230 "scaleType": "SCALE_NS",
232 "scaleNsByStepsData": [{
233 "aspectId": "sss_zte",
235 "scalingDirection": "0"
239 ns_inst_id, job_id = self.insert_new_ns()
240 job_id = JobUtil.create_job(
241 "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
242 NSManualScaleService(ns_inst_id, data, job_id).run()
243 jobs = JobModel.objects.filter(jobid=job_id)
244 self.assertEqual(255, jobs[0].progress)
246 @mock.patch.object(catalog, 'get_scalingmap_json_package')
247 @mock.patch.object(NSManualScaleService, 'do_vnfs_scale')
248 def test_ns_manual_scale_success(self, mock_do_vnfs_scale, mock_get_scalingmap_json_package):
250 "scaleType": "SCALE_NS",
252 "scaleNsByStepsData": [{
253 "aspectId": "TIC_EDGE_IMS",
254 "numberOfSteps": "1",
255 "scalingDirection": "0"
259 mock_get_scalingmap_json_package.return_value = self.scaling_map_json
260 mock_do_vnfs_scale.return_value = JOB_MODEL_STATUS.FINISHED
261 ns_inst_id, job_id = self.insert_new_ns()
262 job_id = JobUtil.create_job(
263 "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
265 NSManualScaleService(ns_inst_id, data, job_id).run()
266 jobs = JobModel.objects.filter(jobid=job_id)
267 self.assertEqual(255, jobs[0].progress)
269 @mock.patch.object(restcall, 'call_req')
270 def test_ns_manual_scale_thread(self, mock_call):
272 "scaleType": "SCALE_NS",
274 "scaleNsByStepsData": [{
277 "scalingDirection": "0"
281 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
283 NSInstModel.objects.get(
284 id=self.ns_inst_id).status,
285 NS_INST_STATUS.ACTIVE)
287 # def test_swagger_ok(self):
288 # resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
289 # self.assertEqual(resp.status_code, status.HTTP_200_OK)
291 @mock.patch.object(NSManualScaleService, 'start')
292 def test_ns_manual_scale_empty_data(self, mock_start):
293 mock_start.side_effect = NSLCMException("NS scale failed.")
294 response = self.client.post(
295 "/api/nslcm/v1/ns/%s/scale" %
296 self.ns_inst_id, data={})
298 response.status_code,
299 status.HTTP_500_INTERNAL_SERVER_ERROR)
300 self.assertIn("error", response.data)
302 @mock.patch.object(NSManualScaleService, 'start')
303 def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
304 mock_start.side_effect = NSLCMException("NS scale failed.")
306 "scaleType": "SCALE_NS",
308 "scaleNsByStepsData": [{
311 "scalingDirection": "0"
315 response = self.client.post("/api/nslcm/v1/ns/11/scale", data=data)
317 response.status_code,
318 status.HTTP_500_INTERNAL_SERVER_ERROR)
319 self.assertIn("error", response.data)