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