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.
15 from django.test import TestCase
17 from rest_framework.test import APIClient
18 from rest_framework import status
19 from lcm.ns.biz.ns_heal import NSHealService
20 from lcm.ns.enum import NS_INST_STATUS
21 from lcm.pub.database.models import NSInstModel, NfInstModel
22 from lcm.pub.exceptions import NSLCMException
23 from lcm.pub.utils.jobutil import JobUtil
24 from lcm.pub.enum import JOB_TYPE
25 from lcm.ns_vnfs.biz.heal_vnfs import NFHealService
26 from lcm.ns.tests import HEAL_NS_DICT, HEAL_VNF_DICT, VNFD_MODEL_DICT
29 class TestHealNsViews(TestCase):
33 self.nf_uuid = '1-1-1'
34 self.job_id = JobUtil.create_job("NS", JOB_TYPE.HEAL_VNF, self.ns_inst_id)
35 self.client = APIClient()
36 NSInstModel.objects.filter().delete()
37 NfInstModel.objects.filter().delete()
38 NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
39 NfInstModel.objects.create(nfinstid=self.nf_inst_id,
43 ns_inst_id=self.ns_inst_id,
50 mnfinstid=self.nf_uuid,
52 vnfd_model=VNFD_MODEL_DICT)
57 @mock.patch.object(NSHealService, 'run')
58 def test_heal_vnf_url(self, mock_run):
59 heal_vnf_json = HEAL_VNF_DICT.copy()
60 heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
61 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=heal_vnf_json, format='json')
62 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
63 self.assertIsNotNone(response.data)
64 self.assertIn("jobId", response.data)
65 self.assertNotIn("error", response.data)
66 response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
67 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
70 @mock.patch.object(NSHealService, 'run')
71 def test_heal_ns_url(self, mock_run):
72 heal_ns_json = HEAL_NS_DICT.copy()
73 heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id
74 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=heal_ns_json, format='json')
75 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
76 self.assertIsNotNone(response.data)
77 self.assertIn("jobId", response.data)
78 self.assertNotIn("error", response.data)
79 response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
80 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
82 @mock.patch.object(NFHealService, 'start')
83 @mock.patch.object(NSHealService, 'wait_job_finish')
84 @mock.patch.object(NSHealService, 'update_job')
85 def test_heal_vnf_thread(self, mock_start, mock_wait, mock_update):
86 heal_vnf_json = HEAL_VNF_DICT.copy()
87 heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
88 NSHealService(self.ns_inst_id, heal_vnf_json, self.job_id).run()
89 self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
92 @mock.patch.object(NFHealService, 'start')
93 @mock.patch.object(NSHealService, 'wait_job_finish')
94 @mock.patch.object(NSHealService, 'update_job')
95 def test_heal_ns_thread(self, mock_start, mock_wait, mock_update):
96 heal_ns_json = HEAL_NS_DICT.copy()
97 heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id
98 NSHealService(self.ns_inst_id, heal_ns_json, self.job_id).run()
99 self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
101 @mock.patch.object(NSHealService, "start")
102 def test_heal_vnf_non_existing_ns(self, mock_start):
103 mock_start.side_effect = NSLCMException("NS Not Found")
105 heal_vnf_json = HEAL_VNF_DICT.copy()
106 heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
107 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=heal_vnf_json, format='json')
108 self.assertEqual(response.data["error"], "NS Not Found")
109 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
110 self.assertIn("error", response.data)
113 @mock.patch.object(NSHealService, "start")
114 def test_heal_ns_heal_non_existing_ns(self, mock_start):
115 mock_start.side_effect = NSLCMException("NS Not Found")
117 heal_ns_json = HEAL_NS_DICT.copy()
118 heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id
119 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=heal_ns_json, format='json')
120 self.assertEqual(response.data["error"], "NS Not Found")
121 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
122 self.assertIn("error", response.data)
124 @mock.patch.object(NSHealService, "start")
125 def test_heal_vnf_empty_post(self, mock_start):
126 mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
127 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data={})
128 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
129 self.assertIn("error", response.data)