e47f5f4584eb7924585f1190f1d0a29c0209d762
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_ns_heal.py
1 # Copyright 2017 Intel Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import mock
16
17 from rest_framework import status
18 from django.test import TestCase
19 from django.test import Client
20 from lcm.pub.database.models import NSInstModel, NfInstModel
21 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
22 from lcm.ns.const import NS_INST_STATUS
23 from lcm.pub.utils import restcall
24 from lcm.pub.exceptions import NSLCMException
25 from lcm.ns.ns_heal import NSHealService
26
27
28 class TestHealNsViews(TestCase):
29     def setUp(self):
30
31         self.ns_inst_id = '1'
32         self.nf_inst_id = '1'
33         self.nf_uuid = '1-1-1'
34
35         self.job_id = JobUtil.create_job("NS", JOB_TYPE.HEAL_VNF, self.ns_inst_id)
36
37         self.client = Client()
38
39         model = '{"metadata": {"vnfdId": "1","vnfdName": "PGW001","vnfProvider": "zte","vnfdVersion": "V00001",' \
40                 '"vnfVersion": "V5.10.20","productType": "CN","vnfType": "PGW",' \
41                 '"description": "PGW VNFD description","isShared":true,"vnfExtendType":"driver"}}'
42         NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
43         NfInstModel.objects.create(nfinstid=self.nf_inst_id, nf_name='name_1', vnf_id='1',
44                                    vnfm_inst_id='1', ns_inst_id=self.ns_inst_id,
45                                    max_cpu='14', max_ram='12296', max_hd='101', max_shd="20", max_net=10,
46                                    status='null', mnfinstid=self.nf_uuid, package_id='pkg1',
47                                    vnfd_model=model)
48
49     def tearDown(self):
50         NSInstModel.objects.filter().delete()
51         NfInstModel.objects.filter().delete()
52
53     @mock.patch.object(NSHealService, 'run')
54     def test_heal_vnf_url(self, mock_run):
55         data = {
56             "healVnfData": {
57                 "vnfInstanceId": self.nf_inst_id,
58                 "cause": "vm is down",
59                 "additionalParams": {
60                     "action": "restartvm",
61                     "actionvminfo": {
62                         "vmid": "33",
63                         "vmname": "xgw-smp11"
64                     }
65                 }
66             }
67         }
68
69         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data)
70         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code)
71         self.assertIsNotNone(response.data)
72         self.assertIn("jobId", response.data)
73         self.assertNotIn("error", response.data)
74
75         response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
76         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
77
78     @mock.patch.object(NSHealService, 'start')
79     @mock.patch.object(NSHealService, 'wait_job_finish')
80     @mock.patch.object(NSHealService, 'update_job')
81     def test_ns_manual_scale_thread(self, mock_start, mock_wait, mock_update):
82
83         data = {
84             "healVnfData": {
85                 "vnfInstanceId": self.nf_inst_id,
86                 "cause": "vm is down",
87                 "additionalParams": {
88                     "action": "restartvm",
89                     "actionvminfo": {
90                         "vmid": "33",
91                         "vmname": "xgw-smp11"
92                     }
93                 }
94             }
95         }
96
97         NSHealService(self.ns_inst_id, data, self.job_id).run()
98         self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
99
100     @mock.patch.object(NSHealService, "start")
101     def test_ns_heal_non_existing_ns(self, mock_start):
102         mock_start.side_effect = NSLCMException("NS Not Found")
103
104         ns_inst_id = "2"
105
106         data = {
107             "healVnfData": {
108                 "vnfInstanceId": self.nf_inst_id,
109                 "cause": "vm is down",
110                 "additionalParams": {
111                     "action": "restartvm",
112                     "actionvminfo": {
113                         "vmid": "33",
114                         "vmname": "xgw-smp11"
115                     }
116                 }
117             }
118         }
119
120         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=data)
121         self.assertEqual(response.data["error"], "NS Not Found")
122         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
123         self.assertIn("error", response.data)
124
125     @mock.patch.object(NSHealService, "start")
126     def test_ns_heal_empty_post(self, mock_start):
127         mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
128
129         data = {}
130
131         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data)
132         self.assertEqual(response.data["error"], "healVnfData parameter does not exist or value is incorrect.")
133         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
134         self.assertIn("error", response.data)