Remove unused endpoints of nspackages
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_ns_manual_scale.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import uuid
16 import os
17 import mock
18 import random
19 from django.test import Client
20 from django.test import TestCase
21 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, NfInstModel
25 from lcm.pub.exceptions import NSLCMException
26 from lcm.pub.utils import restcall
27 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS
28 from lcm.pub.msapi import catalog
29 from lcm.pub.utils.scaleaspect import get_json_data
30 from lcm.pub.utils import idutil
31
32
33 SCALING_JSON = {
34     "scale_options": [
35         {
36             "nsd_id": "ns_ims",
37             "ns_scale_aspect": "TIC_CORE_IMS",
38             "ns_scale_info": [
39                 {
40                     "step": "1",
41                     "scale_list": [
42                         {
43                             "vnfd_id": "zte_ims_cscf",
44                             "vnf_scale_aspect": "mpu",
45                             "numberOfSteps": "1"
46                         },
47                         {
48                             "vnfd_id": "zte_ims_hss",
49                             "vnf_scale_aspect": "fpu",
50                             "numberOfSteps": "3"
51                         }
52                     ]
53                 },
54                 {
55                     "step": "2",
56                     "scale_list": [
57                         {
58                             "vnfd_id": "zte_ims_cscf",
59                             "vnf_scale_aspect": "mpu",
60                             "numberOfSteps": "2"
61                         },
62                         {
63                             "vnfd_id": "zte_ims_hss",
64                             "vnf_scale_aspect": "fpu",
65                             "numberOfSteps": "6"
66                         }
67                     ]
68                 }
69             ]
70         },
71         {
72             "nsd_id": "ns_epc",
73             "ns_scale_aspect": "TIC_EDGE_EPC",
74             "ns_scale_info": [
75                 {
76                     "step": "1",
77                     "scale_list": [
78                         {
79                             "vnfd_id": "zte_epc_spgw",
80                             "vnf_scale_aspect": "gpu",
81                             "numberOfSteps": "1"
82                         },
83                         {
84                             "vnfd_id": "zte_epc_tas",
85                             "vnf_scale_aspect": "fpu",
86                             "numberOfSteps": "2"
87                         }
88                     ]
89                 },
90                 {
91                     "step": "2",
92                     "scale_list": [
93                         {
94                             "vnfd_id": "zte_epc_spgw",
95                             "vnf_scale_aspect": "mpu",
96                             "numberOfSteps": "2"
97                         },
98                         {
99                             "vnfd_id": "zte_epc_tas",
100                             "vnf_scale_aspect": "fpu",
101                             "numberOfSteps": "4"
102                         }
103                     ]
104                 }
105             ]
106         }
107     ]
108 }
109
110
111 class TestNsManualScale(TestCase):
112     def setUp(self):
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         idutil.get_auto_id = mock.Mock()
119         idutil.get_auto_id.return_value = random.random()
120         NSInstModel(
121             id=self.ns_inst_id,
122             name="abc",
123             nspackage_id=self.package_id,
124             nsd_id="111").save()
125
126         self.init_scaling_map_json()
127
128     def tearDown(self):
129         NSInstModel.objects.filter().delete()
130         JobModel.objects.filter().delete()
131
132     def init_scaling_map_json(self):
133         curdir_path = os.path.dirname(
134             os.path.dirname(
135                 os.path.dirname(
136                     os.path.abspath(__file__))))
137         filename = curdir_path + "/ns/data/scalemapping.json"
138         self.scaling_map_json = get_json_data(filename)
139
140     def insert_new_ns(self):
141         ns_inst_id = str(uuid.uuid4())
142         job_id = JobUtil.create_job(
143             "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
144         package_id = "23"
145         NSInstModel(
146             id=ns_inst_id,
147             name="abc",
148             nspackage_id=package_id,
149             nsd_id=package_id).save()
150         return ns_inst_id, job_id
151
152     def insert_new_nf(self):
153         # Create a third vnf instance
154         self.nf_name = "name_1"
155         self.vnf_id = "1"
156         self.vnfm_inst_id = "1"
157         nf_inst_id = "233"
158         package_id = "nf_zte_hss"
159         nf_uuid = "ab34-3g5j-de13-ab85-ij93"
160
161         NfInstModel.objects.create(
162             nfinstid=nf_inst_id,
163             nf_name=self.nf_name,
164             vnf_id=self.vnf_id,
165             vnfm_inst_id=self.vnfm_inst_id,
166             ns_inst_id=self.ns_inst_id,
167             max_cpu='14',
168             max_ram='12296',
169             max_hd='101',
170             max_shd="20",
171             max_net=10,
172             status='active',
173             mnfinstid=nf_uuid,
174             package_id=package_id,
175             vnfd_model='{"metadata": {"vnfdId": "1","vnfdName": "PGW001",'
176                        '"vnfProvider": "zte","vnfdVersion": "V00001","vnfVersion": "V5.10.20",'
177                        '"productType": "CN","vnfType": "PGW",'
178                        '"description": "PGW VNFD description",'
179                        '"isShared":true,"vnfExtendType":"driver"}}')
180
181     @mock.patch.object(NSManualScaleService, 'run')
182     def test_ns_manual_scale(self, mock_run):
183         data = {
184             "scaleType": "SCALE_NS",
185             "scaleNsData": [{
186                 "scaleNsByStepsData": [{
187                     "aspectId": "1",
188                     "numberOfSteps": 1,
189                     "scalingDirection": "0"
190                 }]
191             }]
192         }
193         response = self.client.post(
194             "/api/nslcm/v1/ns/%s/scale" %
195             self.ns_inst_id, data=data)
196         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
197
198     def test_ns_manual_scale_error_scaletype(self):
199         data = {
200             "scaleType": "SCALE_ERR",
201             "scaleNsData": [{
202                 "scaleNsByStepsData": [{
203                     "aspectId": "sss_zte",
204                     "numberOfSteps": 1,
205                     "scalingDirection": "0"
206                 }]
207             }]
208         }
209         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
210         jobs = JobModel.objects.filter(jobid=self.job_id)
211         self.assertEqual(255, jobs[0].progress)
212
213     def test_ns_manual_scale_error_nsd_id(self):
214         data = {
215             "scaleType": "SCALE_NS",
216             "scaleNsData": [{
217                 "scaleNsByStepsData": [{
218                     "aspectId": "sss_zte",
219                     "numberOfSteps": 1,
220                     "scalingDirection": "0"
221                 }]
222             }]
223         }
224         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
225         jobs = JobModel.objects.filter(jobid=self.job_id)
226         self.assertEqual(255, jobs[0].progress)
227
228     def test_ns_manual_scale_error_aspect(self):
229         data = {
230             "scaleType": "SCALE_NS",
231             "scaleNsData": [{
232                 "scaleNsByStepsData": [{
233                     "aspectId": "sss_zte",
234                     "numberOfSteps": 1,
235                     "scalingDirection": "0"
236                 }]
237             }]
238         }
239         ns_inst_id, job_id = self.insert_new_ns()
240         job_id = JobUtil.create_job(
241             "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
242         NSManualScaleService(ns_inst_id, data, job_id).run()
243         jobs = JobModel.objects.filter(jobid=job_id)
244         self.assertEqual(255, jobs[0].progress)
245
246     @mock.patch.object(catalog, 'get_scalingmap_json_package')
247     @mock.patch.object(NSManualScaleService, 'do_vnfs_scale')
248     def test_ns_manual_scale_success(self, mock_do_vnfs_scale, mock_get_scalingmap_json_package):
249         data = {
250             "scaleType": "SCALE_NS",
251             "scaleNsData": [{
252                 "scaleNsByStepsData": [{
253                     "aspectId": "TIC_EDGE_IMS",
254                     "numberOfSteps": "1",
255                     "scalingDirection": "0"
256                 }]
257             }]
258         }
259         mock_get_scalingmap_json_package.return_value = self.scaling_map_json
260         mock_do_vnfs_scale.return_value = JOB_MODEL_STATUS.FINISHED
261         ns_inst_id, job_id = self.insert_new_ns()
262         job_id = JobUtil.create_job(
263             "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
264         self.insert_new_nf()
265         NSManualScaleService(ns_inst_id, data, job_id).run()
266         jobs = JobModel.objects.filter(jobid=job_id)
267         self.assertEqual(255, jobs[0].progress)
268
269     @mock.patch.object(restcall, 'call_req')
270     def test_ns_manual_scale_thread(self, mock_call):
271         data = {
272             "scaleType": "SCALE_NS",
273             "scaleNsData": [{
274                 "scaleNsByStepsData": [{
275                     "aspectId": "1",
276                     "numberOfSteps": 1,
277                     "scalingDirection": "0"
278                 }]
279             }]
280         }
281         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
282         self.assertTrue(
283             NSInstModel.objects.get(
284                 id=self.ns_inst_id).status,
285             NS_INST_STATUS.ACTIVE)
286
287     # def test_swagger_ok(self):
288     # resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
289     # self.assertEqual(resp.status_code, status.HTTP_200_OK)
290
291     @mock.patch.object(NSManualScaleService, 'start')
292     def test_ns_manual_scale_empty_data(self, mock_start):
293         mock_start.side_effect = NSLCMException("NS scale failed.")
294         response = self.client.post(
295             "/api/nslcm/v1/ns/%s/scale" %
296             self.ns_inst_id, data={})
297         self.assertEqual(
298             response.status_code,
299             status.HTTP_500_INTERNAL_SERVER_ERROR)
300         self.assertIn("error", response.data)
301
302     @mock.patch.object(NSManualScaleService, 'start')
303     def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
304         mock_start.side_effect = NSLCMException("NS scale failed.")
305         data = {
306             "scaleType": "SCALE_NS",
307             "scaleNsData": [{
308                 "scaleNsByStepsData": [{
309                     "aspectId": "1",
310                     "numberOfSteps": 1,
311                     "scalingDirection": "0"
312                 }]
313             }]
314         }
315         response = self.client.post("/api/nslcm/v1/ns/11/scale", data=data)
316         self.assertEqual(
317             response.status_code,
318             status.HTTP_500_INTERNAL_SERVER_ERROR)
319         self.assertIn("error", response.data)