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 = '1'
33 self.ns_inst_id = str(uuid.uuid4())
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(self.url % 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.assertIsNotNone(response['Location'])
98 response = self.client.get(response['Location'], format='json')
99 self.assertEqual(response.status_code, status.HTTP_200_OK)
103 @mock.patch.object(NSHealService, 'run')
104 def test_heal_ns_url(self, mock_run):
108 "vnfInstanceId": self.nf_inst_id,
110 "additionalParams": {
115 "vmname": "xgw-smp11"
121 response = self.client.post(self.url % self.ns_inst_id, data=data)
122 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
123 self.assertIsNotNone(response['Location'])
124 response = self.client.get(response['Location'], format='json')
125 self.assertEqual(response.status_code, status.HTTP_200_OK)
127 @mock.patch.object(NSHealService, "start")
128 def test_heal_vnf_non_existing_ns(self, mock_start):
129 mock_start.side_effect = NSLCMException("NS Not Found")
135 "vnfInstanceId": self.nf_inst_id,
136 "cause": "vm is down",
137 "additionalParams": {
138 "action": "restartvm",
141 "vmname": "xgw-smp11"
147 response = self.client.post(self.url % ns_inst_id, data=data)
148 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
151 @mock.patch.object(NSHealService, "start")
152 def test_heal_ns_heal_non_existing_ns(self, mock_start):
153 mock_start.side_effect = NSLCMException("NS Not Found")
159 "vnfInstanceId": self.nf_inst_id,
161 "additionalParams": {
166 "vmname": "xgw-smp11"
172 response = self.client.post(self.url % ns_inst_id, data=data)
173 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
175 @mock.patch.object(NSHealService, "start")
176 def test_heal_vnf_empty_post(self, mock_start):
177 mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
178 response = self.client.post(self.url % self.ns_inst_id, data={})
179 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)