Fix NS Scale serializers error
[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 os
16 import uuid
17
18 import mock
19 from django.test import Client
20 from rest_framework.test import APIClient
21 from django.test import TestCase
22 from lcm.ns.biz.scaleaspect import get_json_data
23 from rest_framework import status
24
25 from lcm.ns.biz.ns_manual_scale import NSManualScaleService
26 from lcm.ns.const import NS_INST_STATUS
27 from lcm.pub.database.models import NSInstModel, JobModel, NfInstModel
28 from lcm.pub.exceptions import NSLCMException
29 from lcm.pub.msapi import catalog
30 from lcm.pub.utils import restcall
31 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE, JOB_MODEL_STATUS
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         self.apiClient = APIClient()
119         NSInstModel(
120             id=self.ns_inst_id,
121             name="abc",
122             nspackage_id=self.package_id,
123             nsd_id="111").save()
124
125         self.init_scaling_map_json()
126
127     def tearDown(self):
128         NSInstModel.objects.filter().delete()
129         JobModel.objects.filter().delete()
130
131     def init_scaling_map_json(self):
132         curdir_path = os.path.dirname(
133             os.path.dirname(
134                 os.path.dirname(
135                     os.path.abspath(__file__))))
136         filename = curdir_path + "/ns/data/scalemapping.json"
137         self.scaling_map_json = get_json_data(filename)
138
139     def insert_new_ns(self):
140         ns_inst_id = str(uuid.uuid4())
141         job_id = JobUtil.create_job(
142             "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
143         package_id = "23"
144         NSInstModel(
145             id=ns_inst_id,
146             name="abc",
147             nspackage_id=package_id,
148             nsd_id=package_id).save()
149         return ns_inst_id, job_id
150
151     def insert_new_nf(self):
152         # Create a third vnf instance
153         self.nf_name = "name_1"
154         self.vnf_id = "1"
155         self.vnfm_inst_id = "1"
156         nf_inst_id = "233"
157         package_id = "nf_zte_hss"
158         nf_uuid = "ab34-3g5j-de13-ab85-ij93"
159
160         NfInstModel.objects.create(
161             nfinstid=nf_inst_id,
162             nf_name=self.nf_name,
163             vnf_id=self.vnf_id,
164             vnfm_inst_id=self.vnfm_inst_id,
165             ns_inst_id=self.ns_inst_id,
166             max_cpu='14',
167             max_ram='12296',
168             max_hd='101',
169             max_shd="20",
170             max_net=10,
171             status='active',
172             mnfinstid=nf_uuid,
173             package_id=package_id,
174             vnfd_model='{"metadata": {"vnfdId": "1","vnfdName": "PGW001",'
175                        '"vnfProvider": "zte","vnfdVersion": "V00001","vnfVersion": "V5.10.20",'
176                        '"productType": "CN","vnfType": "PGW",'
177                        '"description": "PGW VNFD description",'
178                        '"isShared":true,"vnfExtendType":"driver"}}')
179
180     @mock.patch.object(NSManualScaleService, 'run')
181     def test_ns_manual_scale(self, mock_run):
182         data = {
183             "scaleType": "SCALE_NS",
184             "scaleNsData": [
185                 {
186                     "scaleNsByStepsData": [{
187                         "aspectId": "1",
188                         "numberOfSteps": 1,
189                         "scalingDirection": "0"
190                     }]
191                 }
192             ]
193         }
194         response = self.apiClient.post(
195             "/api/nslcm/v1/ns/%s/scale" %
196             self.ns_inst_id, data=data, format='json')
197         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
198
199     def test_ns_manual_scale_error_scaletype(self):
200         data = {
201             "scaleType": "SCALE_ERR",
202             "scaleNsData": [{
203                 "scaleNsByStepsData": [{
204                     "aspectId": "sss_zte",
205                     "numberOfSteps": 1,
206                     "scalingDirection": "0"
207                 }]
208             }]
209         }
210         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
211         jobs = JobModel.objects.filter(jobid=self.job_id)
212         self.assertEqual(255, jobs[0].progress)
213
214     def test_ns_manual_scale_error_nsd_id(self):
215         data = {
216             "scaleType": "SCALE_NS",
217             "scaleNsData": [{
218                 "scaleNsByStepsData": [{
219                     "aspectId": "sss_zte",
220                     "numberOfSteps": 1,
221                     "scalingDirection": "0"
222                 }]
223             }]
224         }
225         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
226         jobs = JobModel.objects.filter(jobid=self.job_id)
227         self.assertEqual(255, jobs[0].progress)
228
229     def test_ns_manual_scale_error_aspect(self):
230         data = {
231             "scaleType": "SCALE_NS",
232             "scaleNsData": [{
233                 "scaleNsByStepsData": [{
234                     "aspectId": "sss_zte",
235                     "numberOfSteps": 1,
236                     "scalingDirection": "0"
237                 }]
238             }]
239         }
240         ns_inst_id, job_id = self.insert_new_ns()
241         job_id = JobUtil.create_job(
242             "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
243         NSManualScaleService(ns_inst_id, data, job_id).run()
244         jobs = JobModel.objects.filter(jobid=job_id)
245         self.assertEqual(255, jobs[0].progress)
246
247     @mock.patch.object(catalog, 'get_scalingmap_json_package')
248     @mock.patch.object(NSManualScaleService, 'do_vnfs_scale')
249     def test_ns_manual_scale_success(self, mock_do_vnfs_scale, mock_get_scalingmap_json_package):
250         data = {
251             "scaleType": "SCALE_NS",
252             "scaleNsData": [{
253                 "scaleNsByStepsData": [{
254                     "aspectId": "TIC_EDGE_IMS",
255                     "numberOfSteps": "1",
256                     "scalingDirection": "0"
257                 }]
258             }]
259         }
260         mock_get_scalingmap_json_package.return_value = self.scaling_map_json
261         mock_do_vnfs_scale.return_value = JOB_MODEL_STATUS.FINISHED
262         ns_inst_id, job_id = self.insert_new_ns()
263         job_id = JobUtil.create_job(
264             "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
265         self.insert_new_nf()
266         NSManualScaleService(ns_inst_id, data, job_id).run()
267         jobs = JobModel.objects.filter(jobid=job_id)
268         self.assertEqual(255, jobs[0].progress)
269
270     @mock.patch.object(restcall, 'call_req')
271     def test_ns_manual_scale_thread(self, mock_call):
272         data = {
273             "scaleType": "SCALE_NS",
274             "scaleNsData": [{
275                 "scaleNsByStepsData": [{
276                     "aspectId": "1",
277                     "numberOfSteps": 1,
278                     "scalingDirection": "0"
279                 }]
280             }]
281         }
282         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
283         self.assertTrue(
284             NSInstModel.objects.get(
285                 id=self.ns_inst_id).status,
286             NS_INST_STATUS.ACTIVE)
287
288     # def test_swagger_ok(self):
289     # resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
290     # self.assertEqual(resp.status_code, status.HTTP_200_OK)
291
292     @mock.patch.object(NSManualScaleService, 'start')
293     def test_ns_manual_scale_empty_data(self, mock_start):
294         mock_start.side_effect = NSLCMException("NS scale failed.")
295         response = self.client.post(
296             "/api/nslcm/v1/ns/%s/scale" %
297             self.ns_inst_id, data={})
298         self.assertEqual(
299             response.status_code,
300             status.HTTP_500_INTERNAL_SERVER_ERROR)
301         self.assertIn("error", response.data)
302
303     @mock.patch.object(NSManualScaleService, 'start')
304     def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
305         mock_start.side_effect = NSLCMException("NS scale failed.")
306         data = {
307             "scaleType": "SCALE_NS",
308             "scaleNsData": [{
309                 "scaleNsByStepsData": [{
310                     "aspectId": "1",
311                     "numberOfSteps": 1,
312                     "scalingDirection": "0"
313                 }]
314             }]
315         }
316         response = self.client.post("/api/nslcm/v1/ns/11/scale", data=data)
317         self.assertEqual(
318             response.status_code,
319             status.HTTP_500_INTERNAL_SERVER_ERROR)
320         self.assertIn("error", response.data)