move scale NS test json from code to indepandent file
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_sol_ns_scale_api.py
1 # Copyright 2019 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 django.test import TestCase
21 from rest_framework import status
22
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
28
29 SCALING_JSON = {
30     "scale_options": [
31         {
32             "nsd_id": "ns_ims",
33             "ns_scale_aspect": "TIC_CORE_IMS",
34             "ns_scale_info": [
35                 {
36                     "step": "1",
37                     "scale_list": [
38                         {
39                             "vnfd_id": "zte_ims_cscf",
40                             "vnf_scale_aspect": "mpu",
41                             "numberOfSteps": "1"
42                         },
43                         {
44                             "vnfd_id": "zte_ims_hss",
45                             "vnf_scale_aspect": "fpu",
46                             "numberOfSteps": "3"
47                         }
48                     ]
49                 },
50                 {
51                     "step": "2",
52                     "scale_list": [
53                         {
54                             "vnfd_id": "zte_ims_cscf",
55                             "vnf_scale_aspect": "mpu",
56                             "numberOfSteps": "2"
57                         },
58                         {
59                             "vnfd_id": "zte_ims_hss",
60                             "vnf_scale_aspect": "fpu",
61                             "numberOfSteps": "6"
62                         }
63                     ]
64                 }
65             ]
66         },
67         {
68             "nsd_id": "ns_epc",
69             "ns_scale_aspect": "TIC_EDGE_EPC",
70             "ns_scale_info": [
71                 {
72                     "step": "1",
73                     "scale_list": [
74                         {
75                             "vnfd_id": "zte_epc_spgw",
76                             "vnf_scale_aspect": "gpu",
77                             "numberOfSteps": "1"
78                         },
79                         {
80                             "vnfd_id": "zte_epc_tas",
81                             "vnf_scale_aspect": "fpu",
82                             "numberOfSteps": "2"
83                         }
84                     ]
85                 },
86                 {
87                     "step": "2",
88                     "scale_list": [
89                         {
90                             "vnfd_id": "zte_epc_spgw",
91                             "vnf_scale_aspect": "mpu",
92                             "numberOfSteps": "2"
93                         },
94                         {
95                             "vnfd_id": "zte_epc_tas",
96                             "vnf_scale_aspect": "fpu",
97                             "numberOfSteps": "4"
98                         }
99                     ]
100                 }
101             ]
102         }
103     ]
104 }
105
106
107 class TestScaleNsApi(TestCase):
108     def setUp(self):
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()
116         NSInstModel(
117             id=self.ns_inst_id,
118             name="abc",
119             nspackage_id=self.package_id,
120             nsd_id="111").save()
121         self.init_scaling_map_json()
122
123     def tearDown(self):
124         NSInstModel.objects.filter().delete()
125         JobModel.objects.filter().delete()
126
127     def init_scaling_map_json(self):
128         self.scaling_map_json = fileutil.read_json_file(self.cur_path + '/data/scalemapping.json')
129
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)
134         package_id = "23"
135         NSInstModel(
136             id=ns_inst_id,
137             name="abc",
138             nspackage_id=package_id,
139             nsd_id=package_id).save()
140         return ns_inst_id, job_id
141
142     def insert_new_nf(self):
143         # Create a third vnf instance
144         self.nf_name = "name_1"
145         self.vnf_id = "1"
146         self.vnfm_inst_id = "1"
147         nf_inst_id = "233"
148         package_id = "nf_zte_hss"
149         nf_uuid = "ab34-3g5j-de13-ab85-ij93"
150         vnf_model = {
151             "metadata": {
152                 "vnfdId": "1",
153                 "vnfdName": "PGW001",
154                 "vnfProvider": "zte",
155                 "vnfdVersion": "V00001",
156                 "vnfVersion": "V5.10.20",
157                 "productType": "CN",
158                 "vnfType": "PGW",
159                 "description": "PGW VNFD description",
160                 "isShared": True,
161                 "vnfExtendType": "driver"
162             }
163         }
164         NfInstModel.objects.create(
165             nfinstid=nf_inst_id,
166             nf_name=self.nf_name,
167             vnf_id=self.vnf_id,
168             vnfm_inst_id=self.vnfm_inst_id,
169             ns_inst_id=self.ns_inst_id,
170             max_cpu='14',
171             max_ram='12296',
172             max_hd='101',
173             max_shd="20",
174             max_net=10,
175             status='active',
176             mnfinstid=nf_uuid,
177             package_id=package_id,
178             vnfd_model=str(vnf_model)
179         )
180
181     @mock.patch.object(NSManualScaleService, 'run')
182     def test_ns_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(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)
198
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(
203             self.url %
204             self.ns_inst_id, data={})
205         self.assertEqual(
206             response.status_code,
207             status.HTTP_400_BAD_REQUEST)
208
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.")
212         data = {
213             "scaleType": "SCALE_NS",
214             "scaleNsData": {
215                 "scaleNsByStepsData": {
216                     "aspectId": "1",
217                     "numberOfSteps": 1,
218                     "scalingDirection": "0"
219                 }
220             }
221         }
222         response = self.client.post(self.url % '11', data=data)
223         self.assertEqual(
224             response.status_code,
225             status.HTTP_500_INTERNAL_SERVER_ERROR)
226
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)