1 # Copyright 2017 Intel Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from django.test import TestCase
18 from rest_framework.test import APIClient
19 from rest_framework import status
21 from lcm.jobs.enum import JOB_TYPE, JOB_ACTION, JOB_PROGRESS
22 from lcm.ns.biz.ns_heal import NSHealService
23 from lcm.ns.enum import NS_INST_STATUS
24 from lcm.ns_vnfs.biz.heal_vnfs import NFHealService
25 from lcm.ns.tests import HEAL_NS_DICT, HEAL_VNF_DICT, VNFD_MODEL_DICT
26 from lcm.pub.database.models import NSInstModel, NfInstModel, JobModel, VNFCInstModel, VmInstModel, NSLcmOpOccModel
27 from lcm.pub.exceptions import NSLCMException
28 from lcm.pub.utils.jobutil import JobUtil
31 class TestHealNsViews(TestCase):
35 self.nf_uuid = '1-1-1'
36 self.job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.HEAL, self.ns_inst_id)
37 self.client = APIClient()
38 NSInstModel.objects.filter().delete()
39 NfInstModel.objects.filter().delete()
40 NSInstModel(id=self.ns_inst_id, name='ns_name', status='null').save()
41 NfInstModel.objects.create(nfinstid=self.nf_inst_id,
45 ns_inst_id=self.ns_inst_id,
52 mnfinstid=self.nf_uuid,
54 vnfd_model=VNFD_MODEL_DICT)
55 VNFCInstModel(nfinstid=self.nf_inst_id, vmid='vmid_01', vduid='vduid_01').save()
56 VmInstModel(vmid='vmid_01', vmname='vmname_01').save()
59 NSInstModel.objects.all().delete()
60 NfInstModel.objects.all().delete()
61 VNFCInstModel.objects.all().delete()
62 VmInstModel.objects.all().delete()
64 @mock.patch.object(NSHealService, 'run')
65 def test_heal_vnf_url(self, mock_run):
66 heal_vnf_json = HEAL_VNF_DICT.copy()
67 heal_vnf_json['healVnfData']['vnfInstanceId'] = self.nf_inst_id
68 response = self.client.post('/api/nslcm/v1/ns/%s/heal' % self.ns_inst_id, data=heal_vnf_json, format='json')
69 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
70 self.assertIsNotNone(response.data)
71 self.assertIn('jobId', response.data)
72 self.assertNotIn('error', response.data)
73 response = self.client.delete('/api/nslcm/v1/ns/%s' % self.ns_inst_id)
74 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
77 @mock.patch.object(NSHealService, 'run')
78 def test_heal_ns_url(self, mock_run):
79 heal_ns_json = HEAL_NS_DICT.copy()
80 heal_ns_json['healNsData']['vnfInstanceId'] = self.nf_inst_id
81 response = self.client.post('/api/nslcm/v1/ns/%s/heal' % self.ns_inst_id, data=heal_ns_json, format='json')
82 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
83 self.assertIsNotNone(response.data)
84 self.assertIn('jobId', response.data)
85 self.assertNotIn('error', response.data)
86 response = self.client.delete('/api/nslcm/v1/ns/%s' % self.ns_inst_id)
87 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
89 @mock.patch.object(NSHealService, 'start')
90 def test_heal_vnf_non_existing_ns(self, mock_start):
91 mock_start.side_effect = NSLCMException('NS Not Found')
93 heal_vnf_json = HEAL_VNF_DICT.copy()
94 heal_vnf_json['healVnfData']['vnfInstanceId'] = self.nf_inst_id
95 response = self.client.post('/api/nslcm/v1/ns/%s/heal' % ns_inst_id, data=heal_vnf_json, format='json')
96 self.assertEqual(response.data['error'], 'NS Not Found')
97 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
98 self.assertIn('error', response.data)
101 @mock.patch.object(NSHealService, 'start')
102 def test_heal_ns_heal_non_existing_ns(self, mock_start):
103 mock_start.side_effect = NSLCMException('NS Not Found')
105 heal_ns_json = HEAL_NS_DICT.copy()
106 heal_ns_json['healNsData']['vnfInstanceId'] = self.nf_inst_id
107 response = self.client.post('/api/nslcm/v1/ns/%s/heal' % ns_inst_id, data=heal_ns_json, format='json')
108 self.assertEqual(response.data['error'], 'NS Not Found')
109 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
110 self.assertIn('error', response.data)
112 @mock.patch.object(NSHealService, 'start')
113 def test_heal_vnf_empty_post(self, mock_start):
114 mock_start.side_effect = NSLCMException('healVnfData parameter does not exist or value is incorrect.')
115 response = self.client.post('/api/nslcm/v1/ns/%s/heal' % self.ns_inst_id, data={})
116 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
117 self.assertIn('error', response.data)
119 @mock.patch.object(NFHealService, 'run')
120 @mock.patch.object(time, 'sleep')
121 @mock.patch.object(JobModel.objects, 'get')
122 def test_heal_vnf_thread(self, mock_get, mock_sleep, mock_run):
123 mock_run.return_value = None
124 mock_sleep.return_value = None
125 mock_get.return_value = JobModel(progress=JOB_PROGRESS.FINISHED)
126 heal_vnf_json = HEAL_VNF_DICT.copy()
127 heal_vnf_json['healVnfData']['vnfInstanceId'] = self.nf_inst_id
128 NSHealService(self.ns_inst_id, heal_vnf_json, self.job_id).run()
129 self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.ACTIVE)
132 @mock.patch.object(NFHealService, 'run')
133 @mock.patch.object(time, 'sleep')
134 @mock.patch.object(JobModel.objects, 'get')
135 def test_heal_ns_thread(self, mock_get, mock_sleep, mock_run):
136 mock_run.return_value = None
137 mock_sleep.return_value = None
138 mock_get.return_value = JobModel(progress=JOB_PROGRESS.FINISHED)
139 heal_ns_json = HEAL_NS_DICT.copy()
140 heal_ns_json['healNsData']['vnfInstanceId'] = self.nf_inst_id
141 NSHealService(self.ns_inst_id, heal_ns_json, self.job_id).run()
142 self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.ACTIVE)
144 def test_heal_when_ns_does_not_exist(self):
146 ns_heal_service = NSHealService(ns_inst_id, {}, self.job_id)
147 ns_heal_service.run()
148 self.assertEqual(JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR)
149 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
151 def test_heal_when_healnsdata_and_healvnfdata_parameters_does_not_exist(self):
152 ns_heal_service = NSHealService(self.nf_inst_id, {}, self.job_id)
153 ns_heal_service.run()
154 self.assertEqual(JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR)
155 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
157 def test_heal_when_healnsdata_and_healvnfdata_parameters_exist_together(self):
159 'healNsData': {'degreeHealing': 'HEAL_RESTORE'},
160 'healVnfData': {'vnfInstanceId': 'default'}
162 ns_heal_service = NSHealService(self.nf_inst_id, data, self.job_id)
163 ns_heal_service.run()
164 self.assertEqual(JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR)
165 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
167 @mock.patch.object(NFHealService, 'run')
168 @mock.patch.object(time, 'sleep')
169 @mock.patch.object(JobModel.objects, 'get')
170 def test_heal_vnf_thread_when_nf_heal_failed(self, mock_get, mock_sleep, mock_run):
171 mock_run.return_value = None
172 mock_sleep.return_value = None
173 mock_get.return_value = JobModel(progress=JOB_PROGRESS.ERROR)
174 heal_vnf_json = HEAL_VNF_DICT.copy()
175 heal_vnf_json['healVnfData']['vnfInstanceId'] = self.nf_inst_id
176 ns_heal_service = NSHealService(self.ns_inst_id, heal_vnf_json, self.job_id)
177 ns_heal_service.run()
178 self.assertEqual(JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR)
179 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
181 @mock.patch.object(NFHealService, 'run')
182 @mock.patch.object(time, 'sleep')
183 @mock.patch.object(JobModel.objects, 'get')
184 def test_heal_ns_thread_when_nf_heal_failed(self, mock_get, mock_sleep, mock_run):
185 mock_run.return_value = None
186 mock_sleep.return_value = None
187 mock_get.return_value = JobModel(progress=JOB_PROGRESS.ERROR)
188 heal_ns_json = HEAL_NS_DICT.copy()
189 heal_ns_json['healNsData']['vnfInstanceId'] = self.nf_inst_id
190 ns_heal_service = NSHealService(self.ns_inst_id, heal_ns_json, self.job_id)
191 ns_heal_service.run()
192 self.assertEqual(JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR)
193 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
195 def test_heal_vnf_thread_when_vnfinstanceid_does_not_exist(self):
196 heal_vnf_json = {'healVnfData': {'vnfInstanceId': ''}}
197 ns_heal_service = NSHealService(self.ns_inst_id, heal_vnf_json, self.job_id)
198 ns_heal_service.run()
199 self.assertEqual(JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR)
200 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
202 def test_heal_ns_thread_when_degreeHealing_does_not_exist(self):
203 heal_ns_json = {'healNsData': {'degreeHealing': ''}}
204 ns_heal_service = NSHealService(self.ns_inst_id, heal_ns_json, self.job_id)
205 ns_heal_service.run()
206 self.assertEqual(JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR)
207 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
209 def test_heal_ns_thread_when_the_degree_of_healing_does_not_exist(self):
210 heal_ns_json = {'healNsData': {'degreeHealing': 'xxx'}}
211 ns_heal_service = NSHealService(self.ns_inst_id, heal_ns_json, self.job_id)
212 ns_heal_service.run()
213 self.assertEqual(JobModel.objects.filter(jobid=self.job_id).first().progress, JOB_PROGRESS.ERROR)
214 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
216 def test_heal_ns_thread_when_nsinsts_does_not_exist(self):
217 NSInstModel(id='text_nsinsts_does_not_exist', name='ns_name', status='null').save()
218 heal_ns_json = HEAL_NS_DICT.copy()
219 heal_ns_json['healNsData']['vnfInstanceId'] = 'text_nsinsts_does_not_exist'
220 job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.HEAL, 'text_nsinsts_does_not_exist')
221 ns_heal_service = NSHealService('text_nsinsts_does_not_exist', heal_ns_json, job_id)
222 ns_heal_service.run()
223 self.assertEqual(JobModel.objects.filter(jobid=job_id).first().progress, JOB_PROGRESS.ERROR)
224 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
226 def test_heal_ns_thread_when_vnfcinsts_does_not_exist(self):
227 NSInstModel(id='text_vnfcinsts_does_not_exist', name='ns_name', status='null').save()
228 NfInstModel.objects.create(nfinstid='text_vnfcinsts_does_not_exist_nf_id',
229 ns_inst_id='text_vnfcinsts_does_not_exist')
230 heal_ns_json = HEAL_NS_DICT.copy()
231 heal_ns_json['healNsData']['vnfInstanceId'] = 'text_vnfcinsts_does_not_exist_nf_id'
232 job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.HEAL, 'text_vnfcinsts_does_not_exist')
233 ns_heal_service = NSHealService('text_vnfcinsts_does_not_exist', heal_ns_json, job_id)
234 ns_heal_service.run()
235 self.assertEqual(JobModel.objects.filter(jobid=job_id).first().progress, JOB_PROGRESS.ERROR)
236 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')
238 def test_heal_ns_thread_when_vminstinfo_does_not_exist(self):
239 ns_inst_id = 'text_vminstinfo_does_not_exist'
240 NSInstModel(id=ns_inst_id, name='ns_name', status='null').save()
241 NfInstModel.objects.create(nfinstid='text_vminstinfo_does_not_exist_nf_id',
242 ns_inst_id=ns_inst_id)
243 VNFCInstModel(nfinstid='text_vminstinfo_does_not_exist_nf_id', vmid='text_vminstinfo_does_not_exist_vm_id',
244 vduid='text_vminstinfo_does_not_exist_vdu_id').save()
245 heal_ns_json = HEAL_NS_DICT.copy()
246 heal_ns_json['healNsData']['vnfInstanceId'] = 'text_vminstinfo_does_not_exist_nf_id'
247 job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.HEAL, ns_inst_id)
248 ns_heal_service = NSHealService(ns_inst_id, heal_ns_json, job_id)
249 ns_heal_service.run()
250 self.assertEqual(JobModel.objects.filter(jobid=job_id).first().progress, JOB_PROGRESS.ERROR)
251 self.assertEqual(NSLcmOpOccModel.objects.filter(id=ns_heal_service.occ_id).first().operation_state, 'FAILED')