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