fix nslcm middleware
[vfc/nfvo/lcm.git] / lcm / ns / tests / test_sol_ns_heal_api.py
1 # Copyright 2019 ZTE 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.pub.database.models import NSInstModel, NfInstModel
24 from lcm.pub.exceptions import NSLCMException
25 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
26
27
28 class TestHealNsApi(TestCase):
29     def setUp(self):
30         self.url = "/api/nslcm/v1/ns_instances/%s/heal"
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 = json.dumps({
40             "metadata": {
41                 "vnfdId": "1",
42                 "vnfdName": "PGW001",
43                 "vnfProvider": "zte",
44                 "vnfdVersion": "V00001",
45                 "vnfVersion": "V5.10.20",
46                 "productType": "CN",
47                 "vnfType": "PGW",
48                 "description": "PGW VNFD description",
49                 "isShared": True,
50                 "vnfExtendType": "driver"
51             }
52         })
53         NSInstModel.objects.filter().delete()
54         NfInstModel.objects.filter().delete()
55         NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
56         NfInstModel.objects.create(nfinstid=self.nf_inst_id,
57                                    nf_name='name_1',
58                                    vnf_id='1',
59                                    vnfm_inst_id='1',
60                                    ns_inst_id=self.ns_inst_id,
61                                    max_cpu='14',
62                                    max_ram='12296',
63                                    max_hd='101',
64                                    max_shd="20",
65                                    max_net=10,
66                                    status='null',
67                                    mnfinstid=self.nf_uuid,
68                                    package_id='pkg1',
69                                    vnfd_model=model)
70
71     def tearDown(self):
72         pass
73
74     @mock.patch.object(NSHealService, 'run')
75     def test_heal_vnf_url(self, mock_run):
76
77         data = {
78             "healVnfData": [{
79                 "vnfInstanceId": self.nf_inst_id,
80                 "cause": "vm is down",
81                 "additionalParams": {
82                     "action": "restartvm",
83                     "actionvminfo": {
84                         "vmid": "33",
85                         "vduid": "",
86                         "vmname": "xgw-smp11"
87                     }
88                 }
89             }]
90         }
91
92         response = self.client.post(self.url % self.ns_inst_id, data=data)
93         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
94         self.assertIsNotNone(response.data)
95         self.assertIsNotNone(response['Location'])
96
97     # add healNsData
98
99     @mock.patch.object(NSHealService, 'run')
100     def test_heal_ns_url(self, mock_run):
101
102         data = {
103             "healNsData": {
104                 "vnfInstanceId": self.nf_inst_id,
105                 "cause": "",
106                 "additionalParams": {
107                     "action": "vmreset",
108                     "actionvminfo": {
109                         "vmid": "33",
110                         "vduid": "",
111                         "vmname": "xgw-smp11"
112                     }
113                 }
114             }
115         }
116
117         response = self.client.post(self.url % self.ns_inst_id, data=data)
118         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
119         self.assertIsNotNone(response['Location'])
120
121     @mock.patch.object(NSHealService, "start")
122     def test_heal_vnf_non_existing_ns(self, mock_start):
123         mock_start.side_effect = NSLCMException("NS Not Found")
124
125         ns_inst_id = "2"
126
127         data = {
128             "healVnfData": [{
129                 "vnfInstanceId": self.nf_inst_id,
130                 "cause": "vm is down",
131                 "additionalParams": {
132                     "action": "restartvm",
133                     "actionvminfo": {
134                         "vmid": "33",
135                         "vmname": "xgw-smp11"
136                     }
137                 }
138             }]
139         }
140
141         response = self.client.post(self.url % ns_inst_id, data=data)
142         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
143
144     # add healNsData
145     @mock.patch.object(NSHealService, "start")
146     def test_heal_ns_heal_non_existing_ns(self, mock_start):
147         mock_start.side_effect = NSLCMException("NS Not Found")
148
149         ns_inst_id = "2"
150
151         data = {
152             "healNsData": {
153                 "vnfInstanceId": self.nf_inst_id,
154                 "cause": "",
155                 "additionalParams": {
156                     "action": "vmreset",
157                     "actionvminfo": {
158                         "vmid": "33",
159                         "vduid": "",
160                         "vmname": "xgw-smp11"
161                     }
162                 }
163             }
164         }
165
166         response = self.client.post(self.url % ns_inst_id, data=data)
167         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
168
169     @mock.patch.object(NSHealService, "start")
170     def test_heal_vnf_empty_post(self, mock_start):
171         mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
172         response = self.client.post(self.url % self.ns_inst_id, data={})
173         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)