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 rest_framework import status
19 from django.test import TestCase
20 from django.test import Client
21 from lcm.pub.database.models import NSInstModel, NfInstModel
22 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
23 from lcm.ns.const import NS_INST_STATUS
24 from lcm.pub.exceptions import NSLCMException
25 from lcm.ns.ns_heal import NSHealService
28 class TestHealNsViews(TestCase):
33 self.nf_uuid = '1-1-1'
35 self.job_id = JobUtil.create_job("NS", JOB_TYPE.HEAL_VNF, self.ns_inst_id)
37 self.client = Client()
44 "vnfdVersion": "V00001",
45 "vnfVersion": "V5.10.20",
48 "description": "PGW VNFD description",
50 "vnfExtendType": "driver"
53 NSInstModel.objects.filter().delete()
54 NfInstModel.objects.filter().delete()
55 NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
56 NfInstModel.objects.create(nfinstid=self.nf_inst_id,
60 ns_inst_id=self.ns_inst_id,
67 mnfinstid=self.nf_uuid,
74 @mock.patch.object(NSHealService, 'run')
75 def test_heal_vnf_url(self, mock_run):
78 "vnfInstanceId": self.nf_inst_id,
79 "cause": "vm is down",
81 "action": "restartvm",
90 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data)
91 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
92 self.assertIsNotNone(response.data)
93 self.assertIn("jobId", response.data)
94 self.assertNotIn("error", response.data)
96 response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
97 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
99 @mock.patch.object(NSHealService, 'start')
100 @mock.patch.object(NSHealService, 'wait_job_finish')
101 @mock.patch.object(NSHealService, 'update_job')
102 def test_ns_manual_scale_thread(self, mock_start, mock_wait, mock_update):
106 "vnfInstanceId": self.nf_inst_id,
107 "cause": "vm is down",
108 "additionalParams": {
109 "action": "restartvm",
112 "vmname": "xgw-smp11"
118 NSHealService(self.ns_inst_id, data, self.job_id).run()
119 self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
121 @mock.patch.object(NSHealService, "start")
122 def test_ns_heal_non_existing_ns(self, mock_start):
123 mock_start.side_effect = NSLCMException("NS Not Found")
129 "vnfInstanceId": self.nf_inst_id,
130 "cause": "vm is down",
131 "additionalParams": {
132 "action": "restartvm",
135 "vmname": "xgw-smp11"
141 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=data)
142 self.assertEqual(response.data["error"], "NS Not Found")
143 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
144 self.assertIn("error", response.data)
146 @mock.patch.object(NSHealService, "start")
147 def test_ns_heal_empty_post(self, mock_start):
148 mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
152 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data)
153 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
154 self.assertIn("error", response.data)