c4fbf983bcc3806a53602ad7095dc8fe53e4164e
[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 json
16
17 import mock
18 from django.test import Client
19 from django.test import TestCase
20 from rest_framework import status
21
22 from lcm.ns.biz.ns_heal import NSHealService
23 from lcm.ns.const import NS_INST_STATUS
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
27 from lcm.ns_vnfs.biz.heal_vnfs import NFHealService
28
29
30 class TestHealNsViews(TestCase):
31     def setUp(self):
32
33         self.ns_inst_id = '1'
34         self.nf_inst_id = '1'
35         self.nf_uuid = '1-1-1'
36
37         self.job_id = JobUtil.create_job("NS", JOB_TYPE.HEAL_VNF, self.ns_inst_id)
38
39         self.client = Client()
40
41         model = json.dumps({
42             "metadata": {
43                 "vnfdId": "1",
44                 "vnfdName": "PGW001",
45                 "vnfProvider": "zte",
46                 "vnfdVersion": "V00001",
47                 "vnfVersion": "V5.10.20",
48                 "productType": "CN",
49                 "vnfType": "PGW",
50                 "description": "PGW VNFD description",
51                 "isShared": True,
52                 "vnfExtendType": "driver"
53             }
54         })
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,
59                                    nf_name='name_1',
60                                    vnf_id='1',
61                                    vnfm_inst_id='1',
62                                    ns_inst_id=self.ns_inst_id,
63                                    max_cpu='14',
64                                    max_ram='12296',
65                                    max_hd='101',
66                                    max_shd="20",
67                                    max_net=10,
68                                    status='null',
69                                    mnfinstid=self.nf_uuid,
70                                    package_id='pkg1',
71                                    vnfd_model=model)
72
73     def tearDown(self):
74         pass
75
76     @mock.patch.object(NSHealService, 'run')
77     def test_heal_vnf_url(self, mock_run):
78
79         data = {
80             "healVnfData": [{
81                 "vnfInstanceId": self.nf_inst_id,
82                 "cause": "vm is down",
83                 "additionalParams": {
84                     "action": "restartvm",
85                     "actionvminfo": {
86                         "vmid": "33",
87                         "vduid": "",
88                         "vmname": "xgw-smp11"
89                     }
90                 }
91             }]
92         }
93
94         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % 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.assertIn("jobId", response.data)
98         self.assertNotIn("error", response.data)
99
100         response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
101         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
102
103     # add healNsData
104     @mock.patch.object(NSHealService, 'run')
105     def test_heal_ns_url(self, mock_run):
106
107         data = {
108             "healNsData": {
109                 "vnfInstanceId": self.nf_inst_id,
110                 "cause": "",
111                 "additionalParams": {
112                     "action": "vmreset",
113                     "actionvminfo": {
114                         "vmid": "33",
115                         "vduid": "",
116                         "vmname": "xgw-smp11"
117                     }
118                 }
119             }
120         }
121
122         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data)
123         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
124         self.assertIsNotNone(response.data)
125         self.assertIn("jobId", response.data)
126         self.assertNotIn("error", response.data)
127
128         response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
129         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
130
131     @mock.patch.object(NFHealService, 'start')
132     @mock.patch.object(NSHealService, 'wait_job_finish')
133     @mock.patch.object(NSHealService, 'update_job')
134     def test_heal_vnf_thread(self, mock_start, mock_wait, mock_update):
135
136         data = {
137             "healVnfData": [{
138                 "vnfInstanceId": self.nf_inst_id,
139                 "cause": "vm is down",
140                 "additionalParams": {
141                     "action": "restartvm",
142                     "actionvminfo": {
143                         "vmid": "33",
144                         "vduid": "",
145                         "vmname": "xgw-smp11"
146                     }
147                 }
148             }]
149         }
150
151         NSHealService(self.ns_inst_id, data, self.job_id).run()
152         self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
153
154     # add healNsData
155     @mock.patch.object(NFHealService, 'start')
156     @mock.patch.object(NSHealService, 'wait_job_finish')
157     @mock.patch.object(NSHealService, 'update_job')
158     def test_heal_ns_thread(self, mock_start, mock_wait, mock_update):
159
160         data = {
161             "healNsData": {
162                 "vnfInstanceId": self.nf_inst_id,
163                 "cause": "",
164                 "additionalParams": {
165                     "action": "vmreset",
166                     "actionvminfo": {
167                         "vmid": "33",
168                         "vduid": "",
169                         "vmname": "xgw-smp11"
170                     }
171                 }
172             }
173         }
174
175         NSHealService(self.ns_inst_id, data, self.job_id).run()
176         self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
177
178     @mock.patch.object(NSHealService, "start")
179     def test_heal_vnf_non_existing_ns(self, mock_start):
180         mock_start.side_effect = NSLCMException("NS Not Found")
181
182         ns_inst_id = "2"
183
184         data = {
185             "healVnfData": [{
186                 "vnfInstanceId": self.nf_inst_id,
187                 "cause": "vm is down",
188                 "additionalParams": {
189                     "action": "restartvm",
190                     "actionvminfo": {
191                         "vmid": "33",
192                         "vmname": "xgw-smp11"
193                     }
194                 }
195             }]
196         }
197
198         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=data)
199         self.assertEqual(response.data["error"], "NS Not Found")
200         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
201         self.assertIn("error", response.data)
202
203     # add healNsData
204     @mock.patch.object(NSHealService, "start")
205     def test_heal_ns_heal_non_existing_ns(self, mock_start):
206         mock_start.side_effect = NSLCMException("NS Not Found")
207
208         ns_inst_id = "2"
209
210         data = {
211             "healNsData": {
212                 "vnfInstanceId": self.nf_inst_id,
213                 "cause": "",
214                 "additionalParams": {
215                     "action": "vmreset",
216                     "actionvminfo": {
217                         "vmid": "33",
218                         "vduid": "",
219                         "vmname": "xgw-smp11"
220                     }
221                 }
222             }
223         }
224
225         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=data)
226         self.assertEqual(response.data["error"], "NS Not Found")
227         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
228         self.assertIn("error", response.data)
229
230     @mock.patch.object(NSHealService, "start")
231     def test_heal_vnf_empty_post(self, mock_start):
232         mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
233
234         data = {}
235
236         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=data)
237         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
238         self.assertIn("error", response.data)