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 rest_framework.test import APIClient
21 from django.test import TestCase
22 from lcm.ns.biz.scaleaspect import get_json_data
23 from rest_framework import status
25 from lcm.ns.biz.ns_manual_scale import NSManualScaleService
26 from lcm.ns.enum import NS_INST_STATUS
27 from lcm.pub.database.models import NSInstModel, JobModel, NfInstModel
28 from lcm.pub.exceptions import NSLCMException
29 from lcm.pub.msapi import catalog
30 from lcm.pub.utils import restcall
31 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS
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 self.apiClient = APIClient()
122 nspackage_id=self.package_id,
125 self.init_scaling_map_json()
128 NSInstModel.objects.filter().delete()
129 JobModel.objects.filter().delete()
131 def init_scaling_map_json(self):
132 curdir_path = os.path.dirname(
135 os.path.abspath(__file__))))
136 filename = curdir_path + "/ns/data/scalemapping.json"
137 self.scaling_map_json = get_json_data(filename)
139 def insert_new_ns(self):
140 ns_inst_id = str(uuid.uuid4())
141 job_id = JobUtil.create_job(
142 "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
147 nspackage_id=package_id,
148 nsd_id=package_id).save()
149 return ns_inst_id, job_id
151 def insert_new_nf(self):
152 # Create a third vnf instance
153 self.nf_name = "name_1"
155 self.vnfm_inst_id = "1"
157 package_id = "nf_zte_hss"
158 nf_uuid = "ab34-3g5j-de13-ab85-ij93"
160 NfInstModel.objects.create(
162 nf_name=self.nf_name,
164 vnfm_inst_id=self.vnfm_inst_id,
165 ns_inst_id=self.ns_inst_id,
173 package_id=package_id,
174 vnfd_model='{"metadata": {"vnfdId": "1","vnfdName": "PGW001",'
175 '"vnfProvider": "zte","vnfdVersion": "V00001","vnfVersion": "V5.10.20",'
176 '"productType": "CN","vnfType": "PGW",'
177 '"description": "PGW VNFD description",'
178 '"isShared":true,"vnfExtendType":"driver"}}')
180 @mock.patch.object(NSManualScaleService, 'run')
181 def test_ns_manual_scale(self, mock_run):
183 "scaleType": "SCALE_NS",
186 "scaleNsByStepsData": [{
189 "scalingDirection": "0"
194 response = self.apiClient.post(
195 "/api/nslcm/v1/ns/%s/scale" %
196 self.ns_inst_id, data=data, format='json')
197 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
199 def test_ns_manual_scale_error_scaletype(self):
201 "scaleType": "SCALE_ERR",
203 "scaleNsByStepsData": [{
204 "aspectId": "sss_zte",
206 "scalingDirection": "0"
210 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
211 jobs = JobModel.objects.filter(jobid=self.job_id)
212 self.assertEqual(255, jobs[0].progress)
214 def test_ns_manual_scale_error_nsd_id(self):
216 "scaleType": "SCALE_NS",
218 "scaleNsByStepsData": [{
219 "aspectId": "sss_zte",
221 "scalingDirection": "0"
225 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
226 jobs = JobModel.objects.filter(jobid=self.job_id)
227 self.assertEqual(255, jobs[0].progress)
229 def test_ns_manual_scale_error_aspect(self):
231 "scaleType": "SCALE_NS",
233 "scaleNsByStepsData": [{
234 "aspectId": "sss_zte",
236 "scalingDirection": "0"
240 ns_inst_id, job_id = self.insert_new_ns()
241 job_id = JobUtil.create_job(
242 "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
243 NSManualScaleService(ns_inst_id, data, job_id).run()
244 jobs = JobModel.objects.filter(jobid=job_id)
245 self.assertEqual(255, jobs[0].progress)
247 @mock.patch.object(catalog, 'get_scalingmap_json_package')
248 @mock.patch.object(NSManualScaleService, 'do_vnfs_scale')
249 def test_ns_manual_scale_success(self, mock_do_vnfs_scale, mock_get_scalingmap_json_package):
251 "scaleType": "SCALE_NS",
253 "scaleNsByStepsData": [{
254 "aspectId": "TIC_EDGE_IMS",
255 "numberOfSteps": "1",
256 "scalingDirection": "0"
260 mock_get_scalingmap_json_package.return_value = self.scaling_map_json
261 mock_do_vnfs_scale.return_value = JOB_MODEL_STATUS.FINISHED
262 ns_inst_id, job_id = self.insert_new_ns()
263 job_id = JobUtil.create_job(
264 "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
266 NSManualScaleService(ns_inst_id, data, job_id).run()
267 jobs = JobModel.objects.filter(jobid=job_id)
268 self.assertEqual(255, jobs[0].progress)
270 @mock.patch.object(restcall, 'call_req')
271 def test_ns_manual_scale_thread(self, mock_call):
273 "scaleType": "SCALE_NS",
275 "scaleNsByStepsData": [{
278 "scalingDirection": "0"
282 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
284 NSInstModel.objects.get(
285 id=self.ns_inst_id).status,
286 NS_INST_STATUS.ACTIVE)
288 # def test_swagger_ok(self):
289 # resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
290 # self.assertEqual(resp.status_code, status.HTTP_200_OK)
292 @mock.patch.object(NSManualScaleService, 'start')
293 def test_ns_manual_scale_empty_data(self, mock_start):
294 mock_start.side_effect = NSLCMException("NS scale failed.")
295 response = self.client.post(
296 "/api/nslcm/v1/ns/%s/scale" %
297 self.ns_inst_id, data={})
299 response.status_code,
300 status.HTTP_500_INTERNAL_SERVER_ERROR)
301 self.assertIn("error", response.data)
303 @mock.patch.object(NSManualScaleService, 'start')
304 def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
305 mock_start.side_effect = NSLCMException("NS scale failed.")
307 "scaleType": "SCALE_NS",
309 "scaleNsByStepsData": [{
312 "scalingDirection": "0"
316 response = self.client.post("/api/nslcm/v1/ns/11/scale", data=data)
318 response.status_code,
319 status.HTTP_500_INTERNAL_SERVER_ERROR)
320 self.assertIn("error", response.data)