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, JOB_TYPE
24 from lcm.ns_vnfs.biz.heal_vnfs import NFHealService
25 from lcm.ns.tests import HEAL_NS_DICT, HEAL_VNF_DICT, VNFD_MODEL_DICT
28 class TestHealNsViews(TestCase):
32 self.nf_uuid = '1-1-1'
33 self.job_id = JobUtil.create_job("NS", JOB_TYPE.HEAL_VNF, self.ns_inst_id)
34 self.client = APIClient()
35 NSInstModel.objects.filter().delete()
36 NfInstModel.objects.filter().delete()
37 NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
38 NfInstModel.objects.create(nfinstid=self.nf_inst_id,
42 ns_inst_id=self.ns_inst_id,
49 mnfinstid=self.nf_uuid,
51 vnfd_model=VNFD_MODEL_DICT)
56 @mock.patch.object(NSHealService, 'run')
57 def test_heal_vnf_url(self, mock_run):
58 heal_vnf_json = HEAL_VNF_DICT.copy()
59 heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
60 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=heal_vnf_json, format='json')
61 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
62 self.assertIsNotNone(response.data)
63 self.assertIn("jobId", response.data)
64 self.assertNotIn("error", response.data)
65 response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
66 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
69 @mock.patch.object(NSHealService, 'run')
70 def test_heal_ns_url(self, mock_run):
71 heal_ns_json = HEAL_NS_DICT.copy()
72 heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id
73 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=heal_ns_json, format='json')
74 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
75 self.assertIsNotNone(response.data)
76 self.assertIn("jobId", response.data)
77 self.assertNotIn("error", response.data)
78 response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
79 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
81 @mock.patch.object(NFHealService, 'start')
82 @mock.patch.object(NSHealService, 'wait_job_finish')
83 @mock.patch.object(NSHealService, 'update_job')
84 def test_heal_vnf_thread(self, mock_start, mock_wait, mock_update):
85 heal_vnf_json = HEAL_VNF_DICT.copy()
86 heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
87 NSHealService(self.ns_inst_id, heal_vnf_json, self.job_id).run()
88 self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
91 @mock.patch.object(NFHealService, 'start')
92 @mock.patch.object(NSHealService, 'wait_job_finish')
93 @mock.patch.object(NSHealService, 'update_job')
94 def test_heal_ns_thread(self, mock_start, mock_wait, mock_update):
95 heal_ns_json = HEAL_NS_DICT.copy()
96 heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id
97 NSHealService(self.ns_inst_id, heal_ns_json, self.job_id).run()
98 self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
100 @mock.patch.object(NSHealService, "start")
101 def test_heal_vnf_non_existing_ns(self, mock_start):
102 mock_start.side_effect = NSLCMException("NS Not Found")
104 heal_vnf_json = HEAL_VNF_DICT.copy()
105 heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
106 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=heal_vnf_json, format='json')
107 self.assertEqual(response.data["error"], "NS Not Found")
108 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
109 self.assertIn("error", response.data)
112 @mock.patch.object(NSHealService, "start")
113 def test_heal_ns_heal_non_existing_ns(self, mock_start):
114 mock_start.side_effect = NSLCMException("NS Not Found")
116 heal_ns_json = HEAL_NS_DICT.copy()
117 heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id
118 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=heal_ns_json, format='json')
119 self.assertEqual(response.data["error"], "NS Not Found")
120 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
121 self.assertIn("error", response.data)
123 @mock.patch.object(NSHealService, "start")
124 def test_heal_vnf_empty_post(self, mock_start):
125 mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
126 response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data={})
127 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
128 self.assertIn("error", response.data)