Merge "Fix NS update serializers error"
[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 = '1'
33         self.ns_inst_id = str(uuid.uuid4())
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(self.url % 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.assertIsNotNone(response['Location'])
98         response = self.client.get(response['Location'], format='json')
99         self.assertEqual(response.status_code, status.HTTP_200_OK)
100
101     # add healNsData
102
103     @mock.patch.object(NSHealService, 'run')
104     def test_heal_ns_url(self, mock_run):
105
106         data = {
107             "healNsData": {
108                 "vnfInstanceId": self.nf_inst_id,
109                 "cause": "",
110                 "additionalParams": {
111                     "action": "vmreset",
112                     "actionvminfo": {
113                         "vmid": "33",
114                         "vduid": "",
115                         "vmname": "xgw-smp11"
116                     }
117                 }
118             }
119         }
120
121         response = self.client.post(self.url % self.ns_inst_id, data=data)
122         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
123         self.assertIsNotNone(response['Location'])
124         response = self.client.get(response['Location'], format='json')
125         self.assertEqual(response.status_code, status.HTTP_200_OK)
126
127     @mock.patch.object(NSHealService, "start")
128     def test_heal_vnf_non_existing_ns(self, mock_start):
129         mock_start.side_effect = NSLCMException("NS Not Found")
130
131         ns_inst_id = "2"
132
133         data = {
134             "healVnfData": [{
135                 "vnfInstanceId": self.nf_inst_id,
136                 "cause": "vm is down",
137                 "additionalParams": {
138                     "action": "restartvm",
139                     "actionvminfo": {
140                         "vmid": "33",
141                         "vmname": "xgw-smp11"
142                     }
143                 }
144             }]
145         }
146
147         response = self.client.post(self.url % ns_inst_id, data=data)
148         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
149
150     # add healNsData
151     @mock.patch.object(NSHealService, "start")
152     def test_heal_ns_heal_non_existing_ns(self, mock_start):
153         mock_start.side_effect = NSLCMException("NS Not Found")
154
155         ns_inst_id = "2"
156
157         data = {
158             "healNsData": {
159                 "vnfInstanceId": self.nf_inst_id,
160                 "cause": "",
161                 "additionalParams": {
162                     "action": "vmreset",
163                     "actionvminfo": {
164                         "vmid": "33",
165                         "vduid": "",
166                         "vmname": "xgw-smp11"
167                     }
168                 }
169             }
170         }
171
172         response = self.client.post(self.url % ns_inst_id, data=data)
173         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
174
175     @mock.patch.object(NSHealService, "start")
176     def test_heal_vnf_empty_post(self, mock_start):
177         mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
178         response = self.client.post(self.url % self.ns_inst_id, data={})
179         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)