Add a new test case with incorrect scale aspect
[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
17 import mock
18 from django.test import Client
19 from django.test import TestCase
20 from rest_framework import status
21
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
28
29
30 SCALING_JSON = {
31     "scale_options": [
32         {
33             "nsd_id": "ns_ims",
34             "ns_scale_aspect": "TIC_CORE_IMS",
35             "ns_scale_info": [
36                 {
37                     "step": "1",
38                     "scale_list": [
39                         {
40                             "vnfd_id": "zte_ims_cscf",
41                             "vnf_scale_aspect": "mpu",
42                             "numberOfSteps": "1"
43                         },
44                         {
45                             "vnfd_id": "zte_ims_hss",
46                             "vnf_scale_aspect": "fpu",
47                             "numberOfSteps": "3"
48                         }
49                     ]
50                 },
51                 {
52                     "step": "2",
53                     "scale_list": [
54                         {
55                             "vnfd_id": "zte_ims_cscf",
56                             "vnf_scale_aspect": "mpu",
57                             "numberOfSteps": "2"
58                         },
59                         {
60                             "vnfd_id": "zte_ims_hss",
61                             "vnf_scale_aspect": "fpu",
62                             "numberOfSteps": "6"
63                         }
64                     ]
65                 }
66             ]
67         },
68         {
69             "nsd_id": "ns_epc",
70             "ns_scale_aspect": "TIC_EDGE_EPC",
71             "ns_scale_info": [
72                 {
73                     "step": "1",
74                     "scale_list": [
75                         {
76                             "vnfd_id": "zte_epc_spgw",
77                             "vnf_scale_aspect": "gpu",
78                             "numberOfSteps": "1"
79                         },
80                         {
81                             "vnfd_id": "zte_epc_tas",
82                             "vnf_scale_aspect": "fpu",
83                             "numberOfSteps": "2"
84                         }
85                     ]
86                 },
87                 {
88                     "step": "2",
89                     "scale_list": [
90                         {
91                             "vnfd_id": "zte_epc_spgw",
92                             "vnf_scale_aspect": "mpu",
93                             "numberOfSteps": "2"
94                         },
95                         {
96                             "vnfd_id": "zte_epc_tas",
97                             "vnf_scale_aspect": "fpu",
98                             "numberOfSteps": "4"
99                         }
100                     ]
101                 }
102             ]
103         }
104     ]
105 }
106
107
108 class TestNsManualScale(TestCase):
109     def setUp(self):
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()
115         NSInstModel(
116             id=self.ns_inst_id,
117             name="abc",
118             nspackage_id=self.package_id,
119             nsd_id="111").save()
120
121     def tearDown(self):
122         NSInstModel.objects.filter().delete()
123
124     def insert_new_ns(self):
125         ns_inst_id = str(uuid.uuid4())
126         job_id = JobUtil.create_job(
127             "NS", JOB_TYPE.MANUAL_SCALE_VNF, self.ns_inst_id)
128         package_id = "23"
129         NSInstModel(
130             id=ns_inst_id,
131             name="abc",
132             nspackage_id=package_id,
133             nsd_id=package_id).save()
134         return ns_inst_id, job_id
135
136     @mock.patch.object(NSManualScaleService, 'run')
137     def test_ns_manual_scale(self, mock_run):
138         data = {
139             "scaleType": "SCALE_NS",
140             "scaleNsData": [{
141                 "scaleNsByStepsData": [{
142                     "aspectId": "1",
143                     "numberOfSteps": 1,
144                     "scalingDirection": "0"
145                 }]
146             }]
147         }
148         response = self.client.post(
149             "/api/nslcm/v1/ns/%s/scale" %
150             self.ns_inst_id, data=data)
151         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
152
153     def test_ns_manual_scale_error_scaletype(self):
154         data = {
155             "scaleType": "SCALE_ERR",
156             "scaleNsData": [{
157                 "scaleNsByStepsData": [{
158                     "aspectId": "sss_zte",
159                     "numberOfSteps": 1,
160                     "scalingDirection": "0"
161                 }]
162             }]
163         }
164         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
165         jobs = JobModel.objects.filter(jobid=self.job_id)
166         self.assertEqual(255, jobs[0].progress)
167
168     def test_ns_manual_scale_error_nsd_id(self):
169         data = {
170             "scaleType": "SCALE_NS",
171             "scaleNsData": [{
172                 "scaleNsByStepsData": [{
173                     "aspectId": "sss_zte",
174                     "numberOfSteps": 1,
175                     "scalingDirection": "0"
176                 }]
177             }]
178         }
179         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
180         jobs = JobModel.objects.filter(jobid=self.job_id)
181         self.assertEqual(255, jobs[0].progress)
182
183     def test_ns_manual_scale_error_aspect(self):
184         data = {
185             "scaleType": "SCALE_NS",
186             "scaleNsData": [{
187                 "scaleNsByStepsData": [{
188                     "aspectId": "sss_zte",
189                     "numberOfSteps": 1,
190                     "scalingDirection": "0"
191                 }]
192             }]
193         }
194         ns_inst_id, job_id = self.insert_new_ns()
195         job_id = JobUtil.create_job(
196             "NS", JOB_TYPE.MANUAL_SCALE_VNF, ns_inst_id)
197         NSManualScaleService(ns_inst_id, data, job_id).run()
198         jobs = JobModel.objects.filter(jobid=job_id)
199         self.assertEqual(255, jobs[0].progress)
200
201     @mock.patch.object(restcall, 'call_req')
202     def test_ns_manual_scale_thread(self, mock_call):
203         data = {
204             "scaleType": "SCALE_NS",
205             "scaleNsData": [{
206                 "scaleNsByStepsData": [{
207                     "aspectId": "1",
208                     "numberOfSteps": 1,
209                     "scalingDirection": "0"
210                 }]
211             }]
212         }
213         NSManualScaleService(self.ns_inst_id, data, self.job_id).run()
214         self.assertTrue(
215             NSInstModel.objects.get(
216                 id=self.ns_inst_id).status,
217             NS_INST_STATUS.ACTIVE)
218
219     def test_swagger_ok(self):
220         resp = self.client.get("/api/nslcm/v1/swagger.json", format='json')
221         self.assertEqual(resp.status_code, status.HTTP_200_OK)
222
223     @mock.patch.object(NSManualScaleService, 'start')
224     def test_ns_manual_scale_empty_data(self, mock_start):
225         mock_start.side_effect = NSLCMException("NS scale failed.")
226         response = self.client.post(
227             "/api/nslcm/v1/ns/%s/scale" %
228             self.ns_inst_id, data={})
229         self.assertEqual(
230             response.status_code,
231             status.HTTP_500_INTERNAL_SERVER_ERROR)
232         self.assertIn("error", response.data)
233
234     @mock.patch.object(NSManualScaleService, 'start')
235     def test_ns_manual_scale_when_ns_not_exist(self, mock_start):
236         mock_start.side_effect = NSLCMException("NS scale failed.")
237         data = {
238             "scaleType": "SCALE_NS",
239             "scaleNsData": [{
240                 "scaleNsByStepsData": [{
241                     "aspectId": "1",
242                     "numberOfSteps": 1,
243                     "scalingDirection": "0"
244                 }]
245             }]
246         }
247         response = self.client.post("/api/nslcm/v1/ns/11/scale", data=data)
248         self.assertEqual(
249             response.status_code,
250             status.HTTP_500_INTERNAL_SERVER_ERROR)
251         self.assertIn("error", response.data)