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.
18 from django.test import Client
19 from django.test import TestCase
20 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
25 from lcm.pub.exceptions import NSLCMException
26 from lcm.pub.utils import restcall
27 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
34 "ns_scale_aspect": "TIC_CORE_IMS",
40 "vnfd_id": "zte_ims_cscf",
41 "vnf_scale_aspect": "mpu",
45 "vnfd_id": "zte_ims_hss",
46 "vnf_scale_aspect": "fpu",
55 "vnfd_id": "zte_ims_cscf",
56 "vnf_scale_aspect": "mpu",
60 "vnfd_id": "zte_ims_hss",
61 "vnf_scale_aspect": "fpu",
70 "ns_scale_aspect": "TIC_EDGE_EPC",
76 "vnfd_id": "zte_epc_spgw",
77 "vnf_scale_aspect": "gpu",
81 "vnfd_id": "zte_epc_tas",
82 "vnf_scale_aspect": "fpu",
91 "vnfd_id": "zte_epc_spgw",
92 "vnf_scale_aspect": "mpu",
96 "vnfd_id": "zte_epc_tas",
97 "vnf_scale_aspect": "fpu",
108 class TestNsManualScale(TestCase):
110 self.ns_inst_id = str(uuid.uuid4())
111 self.job_id = JobUtil.create_job(
112 "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
113 self.package_id = "7"
114 self.client = Client()
118 nspackage_id=self.package_id,
122 NSInstModel.objects.filter().delete()
124 @mock.patch.object(NSManualScaleService, 'run')
125 def test_ns_manual_scale(self, mock_run):
127 "scaleType": "SCALE_NS",
129 "scaleNsByStepsData": [{
132 "scalingDirection": "0"
136 response = self.client.post(
137 "/api/nslcm/v1/ns/%s/scale" %
138 self.ns_inst_id, data=data)
139 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
141 def test_ns_manual_scale_error_scaletype(self):
143 "scaleType": "SCALE_ERR",
145 "scaleNsByStepsData": [{
146 "aspectId": "sss_zte",
148 "scalingDirection": "0"
152 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
153 jobs = JobModel.objects.filter(jobid=self.job_id)
154 self.assertEqual(255, jobs[0].progress)
156 def test_ns_manual_scale_error_nsd_id(self):
158 "scaleType": "SCALE_ERR",
160 "scaleNsByStepsData": [{
161 "aspectId": "sss_zte",
163 "scalingDirection": "0"
167 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
168 jobs = JobModel.objects.filter(jobid=self.job_id)
169 self.assertEqual(255, jobs[0].progress)
171 @mock.patch.object(restcall, 'call_req')
172 def test_ns_manual_scale_thread(self, mock_call):
174 "scaleType": "SCALE_NS",
176 "scaleNsByStepsData": [{
179 "scalingDirection": "0"
183 NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
185 NSInstModel.objects.get(
186 id=self.ns_inst_id).status,
187 NS_INST_STATUS.ACTIVE)
189 def test_swagger_ok(self):
190 resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
191 self.assertEqual(resp.status_code, status.HTTP_200_OK)
193 @mock.patch.object(NSManualScaleService, 'start')
194 def test_ns_manual_scale_empty_data(self, mock_start):
195 mock_start.side_effect = NSLCMException("NS scale failed.")
196 response = self.client.post(
197 "/api/nslcm/v1/ns/%s/scale" %
198 self.ns_inst_id, data={})
200 response.status_code,
201 status.HTTP_500_INTERNAL_SERVER_ERROR)
202 self.assertIn("error", response.data)
204 @mock.patch.object(NSManualScaleService, 'start')
205 def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
206 mock_start.side_effect = NSLCMException("NS scale failed.")
208 "scaleType": "SCALE_NS",
210 "scaleNsByStepsData": [{
213 "scalingDirection": "0"
217 response = self.client.post("/api/nslcm/v1/ns/11/scale", data=data)
219 response.status_code,
220 status.HTTP_500_INTERNAL_SERVER_ERROR)
221 self.assertIn("error", response.data)