move heal test json
[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 from django.test import TestCase
16 import mock
17 from rest_framework.test import APIClient
18 from rest_framework import status
19 from lcm.ns.biz.ns_heal import NSHealService
20 from lcm.ns.enum import NS_INST_STATUS
21 from lcm.pub.database.models import NSInstModel, NfInstModel
22 from lcm.pub.exceptions import NSLCMException
23 from lcm.pub.utils.jobutil import JobUtil, JOB_TYPE
24 from lcm.ns_vnfs.biz.heal_vnfs import NFHealService
25 from lcm.ns.tests import HEAL_NS_DICT, HEAL_VNF_DICT, VNFD_MODEL_DICT
26
27
28 class TestHealNsViews(TestCase):
29     def setUp(self):
30         self.ns_inst_id = '1'
31         self.nf_inst_id = '1'
32         self.nf_uuid = '1-1-1'
33         self.job_id = JobUtil.create_job("NS", JOB_TYPE.HEAL_VNF, self.ns_inst_id)
34         self.client = APIClient()
35         NSInstModel.objects.filter().delete()
36         NfInstModel.objects.filter().delete()
37         NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
38         NfInstModel.objects.create(nfinstid=self.nf_inst_id,
39                                    nf_name='name_1',
40                                    vnf_id='1',
41                                    vnfm_inst_id='1',
42                                    ns_inst_id=self.ns_inst_id,
43                                    max_cpu='14',
44                                    max_ram='12296',
45                                    max_hd='101',
46                                    max_shd="20",
47                                    max_net=10,
48                                    status='null',
49                                    mnfinstid=self.nf_uuid,
50                                    package_id='pkg1',
51                                    vnfd_model=VNFD_MODEL_DICT)
52
53     def tearDown(self):
54         pass
55
56     @mock.patch.object(NSHealService, 'run')
57     def test_heal_vnf_url(self, mock_run):
58         heal_vnf_json = HEAL_VNF_DICT.copy()
59         heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
60         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=heal_vnf_json, format='json')
61         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
62         self.assertIsNotNone(response.data)
63         self.assertIn("jobId", response.data)
64         self.assertNotIn("error", response.data)
65         response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
66         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
67
68     # add healNsData
69     @mock.patch.object(NSHealService, 'run')
70     def test_heal_ns_url(self, mock_run):
71         heal_ns_json = HEAL_NS_DICT.copy()
72         heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id
73         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data=heal_ns_json, format='json')
74         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
75         self.assertIsNotNone(response.data)
76         self.assertIn("jobId", response.data)
77         self.assertNotIn("error", response.data)
78         response = self.client.delete("/api/nslcm/v1/ns/%s" % self.ns_inst_id)
79         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
80
81     @mock.patch.object(NFHealService, 'start')
82     @mock.patch.object(NSHealService, 'wait_job_finish')
83     @mock.patch.object(NSHealService, 'update_job')
84     def test_heal_vnf_thread(self, mock_start, mock_wait, mock_update):
85         heal_vnf_json = HEAL_VNF_DICT.copy()
86         heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
87         NSHealService(self.ns_inst_id, heal_vnf_json, self.job_id).run()
88         self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
89
90     # add healNsData
91     @mock.patch.object(NFHealService, 'start')
92     @mock.patch.object(NSHealService, 'wait_job_finish')
93     @mock.patch.object(NSHealService, 'update_job')
94     def test_heal_ns_thread(self, mock_start, mock_wait, mock_update):
95         heal_ns_json = HEAL_NS_DICT.copy()
96         heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id
97         NSHealService(self.ns_inst_id, heal_ns_json, self.job_id).run()
98         self.assertEqual(NSInstModel.objects.get(id=self.ns_inst_id).status, NS_INST_STATUS.HEALING)
99
100     @mock.patch.object(NSHealService, "start")
101     def test_heal_vnf_non_existing_ns(self, mock_start):
102         mock_start.side_effect = NSLCMException("NS Not Found")
103         ns_inst_id = "2"
104         heal_vnf_json = HEAL_VNF_DICT.copy()
105         heal_vnf_json["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
106         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=heal_vnf_json, format='json')
107         self.assertEqual(response.data["error"], "NS Not Found")
108         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
109         self.assertIn("error", response.data)
110
111     # add healNsData
112     @mock.patch.object(NSHealService, "start")
113     def test_heal_ns_heal_non_existing_ns(self, mock_start):
114         mock_start.side_effect = NSLCMException("NS Not Found")
115         ns_inst_id = "2"
116         heal_ns_json = HEAL_NS_DICT.copy()
117         heal_ns_json["healNsData"]["vnfInstanceId"] = self.nf_inst_id
118         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % ns_inst_id, data=heal_ns_json, format='json')
119         self.assertEqual(response.data["error"], "NS Not Found")
120         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
121         self.assertIn("error", response.data)
122
123     @mock.patch.object(NSHealService, "start")
124     def test_heal_vnf_empty_post(self, mock_start):
125         mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
126         response = self.client.post("/api/nslcm/v1/ns/%s/heal" % self.ns_inst_id, data={})
127         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
128         self.assertIn("error", response.data)