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.
18 from django.test import Client
19 from django.test import TestCase
20 from rest_framework import status
22 from lcm.ns.biz.ns_heal import NSHealService
23 from lcm.ns.const import NS_INST_STATUS
24 from lcm.pub.database.models import NSInstModel, NfInstModel
25 from lcm.pub.exceptions import NSLCMException
26 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
27 from lcm.ns_vnfs.biz.heal_vnfs import NFHealService
30 class TestHealNsViews(TestCase):
35 self.nf_uuid = '1-1-1'
37 self.job_id = JobUtil.create_job("NS", JOB_TYPE.HEAL_VNF, self.ns_inst_id)
39 self.client = Client()
46 "vnfdVersion": "V00001",
47 "vnfVersion": "V5.10.20",
50 "description": "PGW VNFD description",
52 "vnfExtendType": "driver"
55 NSInstModel.objects.filter().delete()
56 NfInstModel.objects.filter().delete()
57 NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
58 NfInstModel.objects.create(nfinstid=self.nf_inst_id,
62 ns_inst_id=self.ns_inst_id,
69 mnfinstid=self.nf_uuid,
76 @mock.patch.object(NSHealService, 'run')
77 def test_heal_vnf_url(self, mock_run):
81 "vnfInstanceId": self.nf_inst_id,
82 "cause": "vm is down",
84 "action": "restartvm",
94 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data)
95 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
96 self.assertIsNotNone(response.data)
97 self.assertIn("jobId", response.data)
98 self.assertNotIn("error", response.data)
100 response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
101 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
104 @mock.patch.object(NSHealService, 'run')
105 def test_heal_ns_url(self, mock_run):
109 "vnfInstanceId": self.nf_inst_id,
111 "additionalParams": {
116 "vmname": "xgw-smp11"
122 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data)
123 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
124 self.assertIsNotNone(response.data)
125 self.assertIn("jobId", response.data)
126 self.assertNotIn("error", response.data)
128 response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
129 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
131 @mock.patch.object(NFHealService, 'start')
132 @mock.patch.object(NSHealService, 'wait_job_finish')
133 @mock.patch.object(NSHealService, 'update_job')
134 def test_heal_vnf_thread(self, mock_start, mock_wait, mock_update):
138 "vnfInstanceId": self.nf_inst_id,
139 "cause": "vm is down",
140 "additionalParams": {
141 "action": "restartvm",
145 "vmname": "xgw-smp11"
151 NSHealService(self.ns_inst_id, data, self.job_id).run()
152 self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
155 @mock.patch.object(NFHealService, 'start')
156 @mock.patch.object(NSHealService, 'wait_job_finish')
157 @mock.patch.object(NSHealService, 'update_job')
158 def test_heal_ns_thread(self, mock_start, mock_wait, mock_update):
162 "vnfInstanceId": self.nf_inst_id,
164 "additionalParams": {
169 "vmname": "xgw-smp11"
175 NSHealService(self.ns_inst_id, data, self.job_id).run()
176 self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
178 @mock.patch.object(NSHealService, "start")
179 def test_heal_vnf_non_existing_ns(self, mock_start):
180 mock_start.side_effect = NSLCMException("NS Not Found")
186 "vnfInstanceId": self.nf_inst_id,
187 "cause": "vm is down",
188 "additionalParams": {
189 "action": "restartvm",
192 "vmname": "xgw-smp11"
198 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=data)
199 self.assertEqual(response.data["error"], "NS Not Found")
200 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
201 self.assertIn("error", response.data)
204 @mock.patch.object(NSHealService, "start")
205 def test_heal_ns_heal_non_existing_ns(self, mock_start):
206 mock_start.side_effect = NSLCMException("NS Not Found")
212 "vnfInstanceId": self.nf_inst_id,
214 "additionalParams": {
219 "vmname": "xgw-smp11"
225 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=data)
226 self.assertEqual(response.data["error"], "NS Not Found")
227 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
228 self.assertIn("error", response.data)
230 @mock.patch.object(NSHealService, "start")
231 def test_heal_vnf_empty_post(self, mock_start):
232 mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
236 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data)
237 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
238 self.assertIn("error", response.data)