9f1ae9ced0f17a1f8760bc17b0f57d3002e35649
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / tests / test_scale_vnf.py
1 # Copyright (C) 2019 ZTE. All Rights Reserved.
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 import json
15 import mock
16 from . import const
17 from django.test import TestCase
18 from rest_framework.test import APIClient
19 from lcm.pub.database.models import NfInstModel, JobStatusModel, StorageInstModel, NetworkInstModel, \
20     SubNetworkInstModel, PortInstModel, FlavourInstModel, VmInstModel
21 from lcm.pub.utils import restcall
22 from lcm.pub.vimapi import api
23 from lcm.pub.utils.jobutil import JobUtil
24 from lcm.nf.biz.scale_vnf import ScaleVnf
25
26
27 class TestNfScale(TestCase):
28     def setUp(self):
29         self.client = APIClient()
30         NfInstModel(nfinstid='12345',
31                     nf_name='VNF1',
32                     nf_desc="VNF DESC",
33                     vnfdid="1",
34                     netype="XGW",
35                     vendor="ZTE",
36                     vnfSoftwareVersion="V1",
37                     version="V1",
38                     package_id="2",
39                     status='NOT_INSTANTIATED').save()
40         self.req_data = {
41             "type": "SCALE_IN",
42             "aspectId": "sunshine_aspect"
43         }
44
45     def tearDown(self):
46         NfInstModel.objects.filter(nfinstid='12345').delete()
47
48     def assert_job_result(self, job_id, job_progress, job_detail):
49         jobs = JobStatusModel.objects.filter(
50             jobid=job_id,
51             progress=job_progress,
52             descp=job_detail
53         )
54         self.assertEqual(1, len(jobs))
55
56     # def test_scale_vnf_not_found(self):
57     #     url = "/api/vnflcm/v1/vnf_instances/12/scale"
58     #     response = self.client.post(url,
59     #                                 data=self.req_data,
60     #                                 format='json')
61     #     self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code)
62     #
63     # def test_scale_vnf_conflict(self):
64     #     url = "/api/vnflcm/v1/vnf_instances/12345/scale"
65     #     response = self.client.post(url,
66     #                                 data=self.req_data,
67     #                                 format='json')
68     #     self.assertEqual(status.HTTP_409_CONFLICT, response.status_code)
69     #
70     # def test_scale_vnf_badreq(self):
71     #     NfInstModel(nfinstid='678',
72     #                 nf_name='VNF1',
73     #                 nf_desc="VNF DESC",
74     #                 vnfdid="1",
75     #                 netype="XGW",
76     #                 vendor="ZTE",
77     #                 vnfSoftwareVersion="V1",
78     #                 version="V1",
79     #                 package_id="2",
80     #                 status='INSTANTIATED').save()
81     #     url = "/api/vnflcm/v1/vnf_instances/678/scale"
82     #     response = self.client.post(url,
83     #                                 data={},
84     #                                 format='json')
85     #     NfInstModel.objects.filter(nfinstid='678').delete()
86     #     self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
87
88     @mock.patch.object(restcall, 'call_req')
89     @mock.patch.object(api, 'call')
90     def test_scale_out_vnf_success(self, mock_call, mock_call_req):
91         self.nf_inst_id = '6789'
92         res_cache = {"volume": {}, "flavor": {}, "port": {}}
93         res_cache["volume"]["test"] = "test"
94         # res_cache["flavor"]["vdu1Id"] = "flavor1"
95         # res_cache["port"]["ext_cp"] = "port1"
96         NfInstModel(nfinstid=self.nf_inst_id,
97                     nf_name='VNF1',
98                     nf_desc="VNF DESC",
99                     vnfdid="1",
100                     netype="XGW",
101                     vendor="ZTE",
102                     vnfSoftwareVersion="V1",
103                     version="V1",
104                     package_id="2",
105                     status='INSTANTIATED',
106                     vnfd_model=json.dumps(const.vnfd_for_scale),
107                     vimInfo=json.dumps({}),
108                     resInfo=json.dumps(res_cache)).save()
109         StorageInstModel.objects.create(
110             storageid="1",
111             vimid="1",
112             resourceid="11",
113             insttype=0,
114             instid=self.nf_inst_id,
115             is_predefined=1
116         )
117         NetworkInstModel.objects.create(
118             networkid='1',
119             vimid='1',
120             resourceid='1',
121             name='pnet_network',
122             is_predefined=1,
123             tenant='admin',
124             insttype=0,
125             instid=self.nf_inst_id
126         )
127         SubNetworkInstModel.objects.create(
128             subnetworkid='1',
129             vimid='1',
130             resourceid='1',
131             networkid='1',
132             is_predefined=1,
133             name='sub_pnet',
134             tenant='admin',
135             insttype=0,
136             instid=self.nf_inst_id
137         )
138         PortInstModel.objects.create(
139             portid='1',
140             networkid='1',
141             subnetworkid='1',
142             vimid='1',
143             resourceid='1',
144             is_predefined=1,
145             name='ext_cp',
146             tenant='admin',
147             insttype=0,
148             instid=self.nf_inst_id
149         )
150         FlavourInstModel.objects.create(
151             flavourid="1",
152             vimid="1",
153             resourceid="11",
154             instid=self.nf_inst_id,
155             is_predefined=1,
156             name="Flavor_sunshine"
157         )
158         VmInstModel.objects.create(
159             vmid="1",
160             vimid="1",
161             resourceid="11",
162             insttype=0,
163             instid=self.nf_inst_id,
164             vmname="test_01",
165             is_predefined=1,
166             operationalstate=1
167         )
168         r1_apply_grant_result = [
169             0,
170             json.JSONEncoder().encode(const.instantiate_grant_result),
171             '200'
172         ]
173         mock_call_req.side_effect = [
174             r1_apply_grant_result,
175         ]
176         mock_call.side_effect = [
177             const.c1_data_get_tenant_id,
178             const.c6_data_create_port,
179             const.c7_data_create_flavor,
180             const.c8_data_list_image,
181             const.c9_data_create_vm,
182             const.c10_data_get_vm
183         ]
184
185         self.job_id = JobUtil.create_job('NF', 'SCALE', self.nf_inst_id)
186         JobUtil.add_job_status(self.job_id, 0, "VNF_SCALE_READY", )
187
188         ScaleVnf(
189             {"type": "SCALE_OUT",
190              "aspectId": "sunshine_aspect"},
191             nf_inst_id=self.nf_inst_id,
192             job_id=self.job_id
193         ).run()
194
195         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).delete()
196         # print([{job.progress: job.descp} for job in  JobStatusModel.objects.filter( jobid=self.job_id)])
197         self.assert_job_result(
198             self.job_id,
199             100,
200             'Scale Vnf success.'
201         )
202
203     # @mock.patch.object(restcall, 'call_req')
204     # @mock.patch.object(api, 'call')
205     # def test_scale_in_vnf_success(self, mock_call, mock_call_req):
206     #     self.nf_inst_id = '6789'
207     #     res_cache = {"volume": {}, "flavor": {}, "port": {}}
208     #     # res_cache["volume"]["volume_storage1"] = "vol1"
209     #     # res_cache["flavor"]["vdu1Id"] = "flavor1"
210     #     res_cache["port"]["ext_cp"] = "port1"
211     #     NfInstModel(nfinstid=self.nf_inst_id,
212     #                 nf_name='VNF1',
213     #                 nf_desc="VNF DESC",
214     #                 vnfdid="1",
215     #                 netype="XGW",
216     #                 vendor="ZTE",
217     #                 vnfSoftwareVersion="V1",
218     #                 version="V1",
219     #                 package_id="2",
220     #                 status='INSTANTIATED',
221     #                 vnfd_model=json.dumps(const.vnfd_for_scale),
222     #                 vimInfo=json.dumps({}),
223     #                 resInfo=json.dumps(res_cache)).save()
224     #     StorageInstModel.objects.create(
225     #         storageid="1",
226     #         vimid="1",
227     #         resourceid="11",
228     #         insttype=0,
229     #         instid=self.nf_inst_id,
230     #         is_predefined=1
231     #     )
232     #     NetworkInstModel.objects.create(
233     #         networkid='1',
234     #         vimid='1',
235     #         resourceid='1',
236     #         name='pnet_network',
237     #         is_predefined=1,
238     #         tenant='admin',
239     #         insttype=0,
240     #         instid=self.nf_inst_id
241     #     )
242     #     SubNetworkInstModel.objects.create(
243     #         subnetworkid='1',
244     #         vimid='1',
245     #         resourceid='1',
246     #         networkid='1',
247     #         is_predefined=1,
248     #         name='sub_pnet',
249     #         tenant='admin',
250     #         insttype=0,
251     #         instid=self.nf_inst_id
252     #     )
253     #     PortInstModel.objects.create(
254     #         portid='1',
255     #         networkid='1',
256     #         subnetworkid='1',
257     #         vimid='1',
258     #         resourceid='1',
259     #         is_predefined=1,
260     #         name='ext_cp',
261     #         tenant='admin',
262     #         insttype=0,
263     #         instid=self.nf_inst_id
264     #     )
265     #     FlavourInstModel.objects.create(
266     #         flavourid="1",
267     #         vimid="1",
268     #         resourceid="11",
269     #         instid=self.nf_inst_id,
270     #         is_predefined=1,
271     #         name="Flavor_sunshine"
272     #     )
273     #     VmInstModel.objects.create(
274     #         vmid="1",
275     #         vimid="1",
276     #         resourceid="11",
277     #         insttype=0,
278     #         instid=self.nf_inst_id,
279     #         vmname="test_01",
280     #         is_predefined=1,
281     #         operationalstate=1
282     #     )
283     #     VmInstModel.objects.create(
284     #         vmid="2",
285     #         vimid="1",
286     #         resourceid="22",
287     #         insttype=0,
288     #         instid=self.nf_inst_id,
289     #         vmname="test_02",
290     #         is_predefined=1,
291     #         operationalstate=1
292     #     )
293     #     VNFCInstModel.objects.create(
294     #         vnfcinstanceid="1",
295     #         instid=self.nf_inst_id,
296     #         vmid="1"
297     #     )
298     #     VNFCInstModel.objects.create(
299     #         vnfcinstanceid="2",
300     #         instid=self.nf_inst_id,
301     #         vmid="2"
302     #     )
303     #     r1_apply_grant_result = [
304     #         0,
305     #         json.JSONEncoder().encode(const.instantiate_grant_result),
306     #         '200'
307     #     ]
308     #     mock_call_req.side_effect = [
309     #         r1_apply_grant_result,
310     #     ]
311     #     mock_call.side_effect = [
312     #         const.c1_data_get_tenant_id,
313     #         const.c7_data_create_flavor,
314     #         const.c8_data_list_image,
315     #         const.c9_data_create_vm,
316     #         const.c10_data_get_vm
317     #     ]
318     #
319     #     self.job_id = JobUtil.create_job('NF', 'SCALE', self.nf_inst_id)
320     #     JobUtil.add_job_status(self.job_id, 0, "VNF_SCALE_READY", )
321     #
322     #     ScaleVnf(
323     #         {"type": "SCALE_IN",
324     #          "aspectId": "sunshine_aspect"},
325     #         nf_inst_id=self.nf_inst_id,
326     #         job_id=self.job_id
327     #     ).run()
328     #
329     #     NfInstModel.objects.filter(nfinstid=self.nf_inst_id).delete()
330     #     self.assert_job_result(
331     #         self.job_id,
332     #         100,
333     #         'Scale Vnf success.'
334     #     )