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
27 from lcm.jobs.enum import JOB_TYPE, JOB_ACTION
28 from lcm.ns.tests import VNFD_MODEL_DICT, HEAL_NS_DICT, HEAL_VNF_DICT
31 class TestHealNsApi(TestCase):
33 self.url = "/api/nslcm/v1/ns_instances/%s/heal"
34 self.ns_inst_id = str(uuid.uuid4())
36 self.nf_uuid = '1-1-1'
37 self.job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.HEAL, self.ns_inst_id)
38 self.client = Client()
39 model = json.dumps(VNFD_MODEL_DICT)
40 NSInstModel.objects.filter().delete()
41 NfInstModel.objects.filter().delete()
42 NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
43 NfInstModel.objects.create(
44 nfinstid=self.nf_inst_id,
48 ns_inst_id=self.ns_inst_id,
55 mnfinstid=self.nf_uuid,
62 @mock.patch.object(NSHealService, 'run')
63 def test_heal_vnf_url(self, mock_run):
64 data = HEAL_VNF_DICT.copy()
65 data["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
66 response = self.client.post(self.url % self.ns_inst_id, data=data)
67 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
68 self.assertIsNotNone(response.data)
69 self.assertIsNotNone(response['Location'])
70 response = self.client.get(response['Location'], format='json')
71 self.assertEqual(response.status_code, status.HTTP_200_OK)
75 @mock.patch.object(NSHealService, 'run')
76 def test_heal_ns_url(self, mock_run):
77 data = HEAL_NS_DICT.copy()
78 data["healNsData"]["vnfInstanceId"] = self.nf_inst_id
79 response = self.client.post(self.url % self.ns_inst_id, data=data)
80 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
81 self.assertIsNotNone(response['Location'])
82 response = self.client.get(response['Location'], format='json')
83 self.assertEqual(response.status_code, status.HTTP_200_OK)
85 @mock.patch.object(NSHealService, "start")
86 def test_heal_vnf_non_existing_ns(self, mock_start):
87 mock_start.side_effect = NSLCMException("NS Not Found")
89 data = HEAL_VNF_DICT.copy()
90 data["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
91 response = self.client.post(self.url % ns_inst_id, data=data)
92 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
95 @mock.patch.object(NSHealService, "start")
96 def test_heal_ns_heal_non_existing_ns(self, mock_start):
97 mock_start.side_effect = NSLCMException("NS Not Found")
99 data = HEAL_NS_DICT.copy()
100 data["healNsData"]["vnfInstanceId"] = self.nf_inst_id
101 response = self.client.post(self.url % ns_inst_id, data=data)
102 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
104 @mock.patch.object(NSHealService, "start")
105 def test_heal_vnf_empty_post(self, mock_start):
106 mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
107 response = self.client.post(self.url % self.ns_inst_id, data={})
108 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)