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 rest_framework import status
23 from lcm.ns.biz.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.jobutil import JobUtil, JOB_TYPE
27 from lcm.pub.utils import fileutil
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.cur_path = os.path.dirname(os.path.abspath(__file__))
111 self.ns_inst_id = str(uuid.uuid4())
112 self.job_id = JobUtil.create_job(
113 "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
114 self.package_id = "7"
115 self.client = Client()
119 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 self.scaling_map_json = fileutil.read_json_file(self.cur_path + '/data/scalemapping.json')
130 def insert_new_ns(self):
131 ns_inst_id = str(uuid.uuid4())
132 job_id = JobUtil.create_job(
133 "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
138 nspackage_id=package_id,
139 nsd_id=package_id).save()
140 return ns_inst_id, job_id
142 def insert_new_nf(self):
143 # Create a third vnf instance
144 self.nf_name = "name_1"
146 self.vnfm_inst_id = "1"
148 package_id = "nf_zte_hss"
149 nf_uuid = "ab34-3g5j-de13-ab85-ij93"
153 "vnfdName": "PGW001",
154 "vnfProvider": "zte",
155 "vnfdVersion": "V00001",
156 "vnfVersion": "V5.10.20",
159 "description": "PGW VNFD description",
161 "vnfExtendType": "driver"
164 NfInstModel.objects.create(
166 nf_name=self.nf_name,
168 vnfm_inst_id=self.vnfm_inst_id,
169 ns_inst_id=self.ns_inst_id,
177 package_id=package_id,
178 vnfd_model=str(vnf_model)
181 @mock.patch.object(NSManualScaleService, 'run')
182 def test_ns_scale(self, mock_run):
184 "scaleType": "SCALE_NS",
186 "scaleNsByStepsData": {
189 "scalingDirection": "0"
193 response = self.client.post(self.url % self.ns_inst_id, data=data)
194 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
195 self.assertIsNotNone(response['Location'])
196 response = self.client.get(response['Location'], format='json')
197 self.assertEqual(response.status_code, status.HTTP_200_OK)
199 @mock.patch.object(NSManualScaleService, 'start')
200 def test_ns_manual_scale_empty_data(self, mock_start):
201 mock_start.side_effect = NSLCMException("NS scale failed.")
202 response = self.client.post(
204 self.ns_inst_id, data={})
206 response.status_code,
207 status.HTTP_400_BAD_REQUEST)
209 @mock.patch.object(NSManualScaleService, 'start')
210 def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
211 mock_start.side_effect = NSLCMException("NS scale failed.")
213 "scaleType": "SCALE_NS",
215 "scaleNsByStepsData": {
218 "scalingDirection": "0"
222 response = self.client.post(self.url % '11', data=data)
224 response.status_code,
225 status.HTTP_500_INTERNAL_SERVER_ERROR)
227 def test_method_not_allowed(self):
228 response = self.client.put(self.url % '1', data={}, format='json')
229 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
230 response = self.client.patch(self.url % '1', data={}, format='json')
231 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
232 response = self.client.delete(self.url % '1', data={}, format='json')
233 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)
234 response = self.client.get(self.url % '1', data={}, format='json')
235 self.failUnlessEqual(status.HTTP_405_METHOD_NOT_ALLOWED, response.status_code)