1 # Copyright 2019 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.pub.database.models import NSInstModel, JobModel, NfInstModel
26 from lcm.pub.exceptions import NSLCMException
27 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
33 "ns_scale_aspect": "TIC_CORE_IMS",
39 "vnfd_id": "zte_ims_cscf",
40 "vnf_scale_aspect": "mpu",
44 "vnfd_id": "zte_ims_hss",
45 "vnf_scale_aspect": "fpu",
54 "vnfd_id": "zte_ims_cscf",
55 "vnf_scale_aspect": "mpu",
59 "vnfd_id": "zte_ims_hss",
60 "vnf_scale_aspect": "fpu",
69 "ns_scale_aspect": "TIC_EDGE_EPC",
75 "vnfd_id": "zte_epc_spgw",
76 "vnf_scale_aspect": "gpu",
80 "vnfd_id": "zte_epc_tas",
81 "vnf_scale_aspect": "fpu",
90 "vnfd_id": "zte_epc_spgw",
91 "vnf_scale_aspect": "mpu",
95 "vnfd_id": "zte_epc_tas",
96 "vnf_scale_aspect": "fpu",
107 class TestScaleNsApi(TestCase):
109 self.url = "/api/nslcm/v1/ns_instances/%s/scale"
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,
121 self.init_scaling_map_json()
124 NSInstModel.objects.filter().delete()
125 JobModel.objects.filter().delete()
127 def init_scaling_map_json(self):
128 curdir_path = os.path.dirname(
131 os.path.abspath(__file__))))
132 filename = curdir_path + "/ns/data/scalemapping.json"
133 self.scaling_map_json = get_json_data(filename)
135 def insert_new_ns(self):
136 ns_inst_id = str(uuid.uuid4())
137 job_id = JobUtil.create_job(
138 "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
143 nspackage_id=package_id,
144 nsd_id=package_id).save()
145 return ns_inst_id, job_id
147 def insert_new_nf(self):
148 # Create a third vnf instance
149 self.nf_name = "name_1"
151 self.vnfm_inst_id = "1"
153 package_id = "nf_zte_hss"
154 nf_uuid = "ab34-3g5j-de13-ab85-ij93"
158 "vnfdName": "PGW001",
159 "vnfProvider": "zte",
160 "vnfdVersion": "V00001",
161 "vnfVersion": "V5.10.20",
164 "description": "PGW VNFD description",
166 "vnfExtendType": "driver"
169 NfInstModel.objects.create(
171 nf_name=self.nf_name,
173 vnfm_inst_id=self.vnfm_inst_id,
174 ns_inst_id=self.ns_inst_id,
182 package_id=package_id,
183 vnfd_model=str(vnf_model)
186 @mock.patch.object(NSManualScaleService, 'run')
187 def test_ns_scale(self, mock_run):
189 "scaleType": "SCALE_NS",
191 "scaleNsByStepsData": [{
194 "scalingDirection": "0"
198 response = self.client.post(self.url % self.ns_inst_id, data=data)
199 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
200 self.assertIsNotNone(response['Location'])
201 response = self.client.get(response['Location'], format='json')
202 self.assertEqual(response.status_code, status.HTTP_200_OK)
204 @mock.patch.object(NSManualScaleService, 'start')
205 def test_ns_manual_scale_empty_data(self, mock_start):
206 mock_start.side_effect = NSLCMException("NS scale failed.")
207 response = self.client.post(
209 self.ns_inst_id, data={})
211 response.status_code,
212 status.HTTP_500_INTERNAL_SERVER_ERROR)
214 @mock.patch.object(NSManualScaleService, 'start')
215 def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
216 mock_start.side_effect = NSLCMException("NS scale failed.")
218 "scaleType": "SCALE_NS",
220 "scaleNsByStepsData": [{
223 "scalingDirection": "0"
227 response = self.client.post(self.url % '11', data=data)
229 response.status_code,
230 status.HTTP_500_INTERNAL_SERVER_ERROR)
232 def test_method_not_allowed(self):
233 response = self.client.put(self.url % '1', data={}, format='json')
234 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
235 response = self.client.patch(self.url % '1', data={}, format='json')
236 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
237 response = self.client.delete(self.url % '1', data={}, format='json')
238 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
239 response = self.client.get(self.url % '1', data={}, format='json')
240 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)