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 lcm.ns.biz.scaleaspect import get_json_data
22 from rest_framework import status
24 from lcm.ns.biz.ns_manual_scale import NSManualScaleService
25 from lcm.ns.const import NS_INST_STATUS
26 from lcm.pub.database.models import NSInstModel, JobModel, NfInstModel
27 from lcm.pub.exceptions import NSLCMException
28 from lcm.pub.msapi import catalog
29 from lcm.pub.utils import restcall
30 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS
36 "ns_scale_aspect": "TIC_CORE_IMS",
42 "vnfd_id": "zte_ims_cscf",
43 "vnf_scale_aspect": "mpu",
47 "vnfd_id": "zte_ims_hss",
48 "vnf_scale_aspect": "fpu",
57 "vnfd_id": "zte_ims_cscf",
58 "vnf_scale_aspect": "mpu",
62 "vnfd_id": "zte_ims_hss",
63 "vnf_scale_aspect": "fpu",
72 "ns_scale_aspect": "TIC_EDGE_EPC",
78 "vnfd_id": "zte_epc_spgw",
79 "vnf_scale_aspect": "gpu",
83 "vnfd_id": "zte_epc_tas",
84 "vnf_scale_aspect": "fpu",
93 "vnfd_id": "zte_epc_spgw",
94 "vnf_scale_aspect": "mpu",
98 "vnfd_id": "zte_epc_tas",
99 "vnf_scale_aspect": "fpu",
110 class TestNsManualScale(TestCase):
112 self.ns_inst_id = str(uuid.uuid4())
113 self.job_id = JobUtil.create_job(
114 "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
115 self.package_id = "7"
116 self.client = Client()
120 nspackage_id=self.package_id,
123 self.init_scaling_map_json()
126 NSInstModel.objects.filter().delete()
127 JobModel.objects.filter().delete()
129 def init_scaling_map_json(self):
130 curdir_path = os.path.dirname(
133 os.path.abspath(__file__))))
134 filename = curdir_path + "/ns/data/scalemapping.json"
135 self.scaling_map_json = get_json_data(filename)
137 def insert_new_ns(self):
138 ns_inst_id = str(uuid.uuid4())
139 job_id = JobUtil.create_job(
140 "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
145 nspackage_id=package_id,
146 nsd_id=package_id).save()
147 return ns_inst_id, job_id
149 def insert_new_nf(self):
150 # Create a third vnf instance
151 self.nf_name = "name_1"
153 self.vnfm_inst_id = "1"
155 package_id = "nf_zte_hss"
156 nf_uuid = "ab34-3g5j-de13-ab85-ij93"
158 NfInstModel.objects.create(
160 nf_name=self.nf_name,
162 vnfm_inst_id=self.vnfm_inst_id,
163 ns_inst_id=self.ns_inst_id,
171 package_id=package_id,
172 vnfd_model='{"metadata": {"vnfdId": "1","vnfdName": "PGW001",'
173 '"vnfProvider": "zte","vnfdVersion": "V00001","vnfVersion": "V5.10.20",'
174 '"productType": "CN","vnfType": "PGW",'
175 '"description": "PGW VNFD description",'
176 '"isShared":true,"vnfExtendType":"driver"}}')
178 @mock.patch.object(NSManualScaleService, 'run')
179 def test_ns_manual_scale(self, mock_run):
181 "scaleType": "SCALE_NS",
183 "scaleNsByStepsData": [{
186 "scalingDirection": "0"
190 response = self.client.post(
191 "/api/nslcm/v1/ns/%s/scale" %
192 self.ns_inst_id, data=data)
193 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
195 def test_ns_manual_scale_error_scaletype(self):
197 "scaleType": "SCALE_ERR",
199 "scaleNsByStepsData": [{
200 "aspectId": "sss_zte",
202 "scalingDirection": "0"
206 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
207 jobs = JobModel.objects.filter(jobid=self.job_id)
208 self.assertEqual(255, jobs[0].progress)
210 def test_ns_manual_scale_error_nsd_id(self):
212 "scaleType": "SCALE_NS",
214 "scaleNsByStepsData": [{
215 "aspectId": "sss_zte",
217 "scalingDirection": "0"
221 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
222 jobs = JobModel.objects.filter(jobid=self.job_id)
223 self.assertEqual(255, jobs[0].progress)
225 def test_ns_manual_scale_error_aspect(self):
227 "scaleType": "SCALE_NS",
229 "scaleNsByStepsData": [{
230 "aspectId": "sss_zte",
232 "scalingDirection": "0"
236 ns_inst_id, job_id = self.insert_new_ns()
237 job_id = JobUtil.create_job(
238 "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
239 NSManualScaleService(ns_inst_id, data, job_id).run()
240 jobs = JobModel.objects.filter(jobid=job_id)
241 self.assertEqual(255, jobs[0].progress)
243 @mock.patch.object(catalog, 'get_scalingmap_json_package')
244 @mock.patch.object(NSManualScaleService, 'do_vnfs_scale')
245 def test_ns_manual_scale_success(self, mock_do_vnfs_scale, mock_get_scalingmap_json_package):
247 "scaleType": "SCALE_NS",
249 "scaleNsByStepsData": [{
250 "aspectId": "TIC_EDGE_IMS",
251 "numberOfSteps": "1",
252 "scalingDirection": "0"
256 mock_get_scalingmap_json_package.return_value = self.scaling_map_json
257 mock_do_vnfs_scale.return_value = JOB_MODEL_STATUS.FINISHED
258 ns_inst_id, job_id = self.insert_new_ns()
259 job_id = JobUtil.create_job(
260 "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
262 NSManualScaleService(ns_inst_id, data, job_id).run()
263 jobs = JobModel.objects.filter(jobid=job_id)
264 self.assertEqual(255, jobs[0].progress)
266 @mock.patch.object(restcall, 'call_req')
267 def test_ns_manual_scale_thread(self, mock_call):
269 "scaleType": "SCALE_NS",
271 "scaleNsByStepsData": [{
274 "scalingDirection": "0"
278 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
280 NSInstModel.objects.get(
281 id=self.ns_inst_id).status,
282 NS_INST_STATUS.ACTIVE)
284 # def test_swagger_ok(self):
285 # resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
286 # self.assertEqual(resp.status_code, status.HTTP_200_OK)
288 @mock.patch.object(NSManualScaleService, 'start')
289 def test_ns_manual_scale_empty_data(self, mock_start):
290 mock_start.side_effect = NSLCMException("NS scale failed.")
291 response = self.client.post(
292 "/api/nslcm/v1/ns/%s/scale" %
293 self.ns_inst_id, data={})
295 response.status_code,
296 status.HTTP_500_INTERNAL_SERVER_ERROR)
297 self.assertIn("error", response.data)
299 @mock.patch.object(NSManualScaleService, 'start')
300 def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
301 mock_start.side_effect = NSLCMException("NS scale failed.")
303 "scaleType": "SCALE_NS",
305 "scaleNsByStepsData": [{
308 "scalingDirection": "0"
312 response = self.client.post("/api/nslcm/v1/ns/11/scale", data=data)
314 response.status_code,
315 status.HTTP_500_INTERNAL_SERVER_ERROR)
316 self.assertIn("error", response.data)