1 # Copyright 2019 ZTE 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.
19 from django.test import Client
20 from django.test import TestCase
21 from rest_framework import status
23 from lcm.ns.biz.ns_heal import NSHealService
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
29 class TestHealNsApi(TestCase):
31 self.url = "/api/nslcm/v1/ns_instances/%s/heal"
32 self.ns_inst_id = str(uuid.uuid4())
34 self.nf_uuid = '1-1-1'
36 self.job_id = JobUtil.create_job("NS", JOB_TYPE.HEAL_VNF, self.ns_inst_id)
38 self.client = Client()
45 "vnfdVersion": "V00001",
46 "vnfVersion": "V5.10.20",
49 "description": "PGW VNFD description",
51 "vnfExtendType": "driver"
54 NSInstModel.objects.filter().delete()
55 NfInstModel.objects.filter().delete()
56 NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
57 NfInstModel.objects.create(nfinstid=self.nf_inst_id,
61 ns_inst_id=self.ns_inst_id,
68 mnfinstid=self.nf_uuid,
75 @mock.patch.object(NSHealService, 'run')
76 def test_heal_vnf_url(self, mock_run):
80 "vnfInstanceId": self.nf_inst_id,
81 "cause": "vm is down",
83 "action": "restartvm",
93 response = self.client.post(self.url % self.ns_inst_id, data=data)
94 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
95 self.assertIsNotNone(response.data)
96 self.assertIsNotNone(response['Location'])
97 response = self.client.get(response['Location'], format='json')
98 self.assertEqual(response.status_code, status.HTTP_200_OK)
102 @mock.patch.object(NSHealService, 'run')
103 def test_heal_ns_url(self, mock_run):
107 "vnfInstanceId": self.nf_inst_id,
109 "additionalParams": {
114 "vmname": "xgw-smp11"
120 response = self.client.post(self.url % self.ns_inst_id, data=data)
121 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
122 self.assertIsNotNone(response['Location'])
123 response = self.client.get(response['Location'], format='json')
124 self.assertEqual(response.status_code, status.HTTP_200_OK)
126 @mock.patch.object(NSHealService, "start")
127 def test_heal_vnf_non_existing_ns(self, mock_start):
128 mock_start.side_effect = NSLCMException("NS Not Found")
134 "vnfInstanceId": self.nf_inst_id,
135 "cause": "vm is down",
136 "additionalParams": {
137 "action": "restartvm",
140 "vmname": "xgw-smp11"
146 response = self.client.post(self.url % ns_inst_id, data=data)
147 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
150 @mock.patch.object(NSHealService, "start")
151 def test_heal_ns_heal_non_existing_ns(self, mock_start):
152 mock_start.side_effect = NSLCMException("NS Not Found")
158 "vnfInstanceId": self.nf_inst_id,
160 "additionalParams": {
165 "vmname": "xgw-smp11"
171 response = self.client.post(self.url % ns_inst_id, data=data)
172 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
174 @mock.patch.object(NSHealService, "start")
175 def test_heal_vnf_empty_post(self, mock_start):
176 mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
177 response = self.client.post(self.url % self.ns_inst_id, data={})
178 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)