update link to upper-constraints.txt
[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
27 from lcm.jobs.enum import JOB_TYPE, JOB_ACTION
28 from lcm.ns.tests import VNFD_MODEL_DICT, HEAL_NS_DICT, HEAL_VNF_DICT
29
30
31 class TestHealNsApi(TestCase):
32     def setUp(self):
33         self.url = "/api/nslcm/v1/ns_instances/%s/heal"
34         self.ns_inst_id = str(uuid.uuid4())
35         self.nf_inst_id = '1'
36         self.nf_uuid = '1-1-1'
37         self.job_id = JobUtil.create_job(JOB_TYPE.NS, JOB_ACTION.HEAL, self.ns_inst_id)
38         self.client = Client()
39         model = json.dumps(VNFD_MODEL_DICT)
40         NSInstModel.objects.filter().delete()
41         NfInstModel.objects.filter().delete()
42         NSInstModel(id=self.ns_inst_id, name="ns_name", status='null').save()
43         NfInstModel.objects.create(
44             nfinstid=self.nf_inst_id,
45             nf_name='name_1',
46             vnf_id='1',
47             vnfm_inst_id='1',
48             ns_inst_id=self.ns_inst_id,
49             max_cpu='14',
50             max_ram='12296',
51             max_hd='101',
52             max_shd="20",
53             max_net=10,
54             status='null',
55             mnfinstid=self.nf_uuid,
56             package_id='pkg1',
57             vnfd_model=model)
58
59     def tearDown(self):
60         pass
61
62     @mock.patch.object(NSHealService, 'run')
63     def test_heal_vnf_url(self, mock_run):
64         data = HEAL_VNF_DICT.copy()
65         data["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
66         response = self.client.post(self.url % self.ns_inst_id, data=data)
67         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
68         self.assertIsNotNone(response.data)
69         self.assertIsNotNone(response['Location'])
70         response = self.client.get(response['Location'], format='json')
71         self.assertEqual(response.status_code, status.HTTP_200_OK)
72
73     # add healNsData
74
75     @mock.patch.object(NSHealService, 'run')
76     def test_heal_ns_url(self, mock_run):
77         data = HEAL_NS_DICT.copy()
78         data["healNsData"]["vnfInstanceId"] = self.nf_inst_id
79         response = self.client.post(self.url % self.ns_inst_id, data=data)
80         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.data)
81         self.assertIsNotNone(response['Location'])
82         response = self.client.get(response['Location'], format='json')
83         self.assertEqual(response.status_code, status.HTTP_200_OK)
84
85     @mock.patch.object(NSHealService, "start")
86     def test_heal_vnf_non_existing_ns(self, mock_start):
87         mock_start.side_effect = NSLCMException("NS Not Found")
88         ns_inst_id = "2"
89         data = HEAL_VNF_DICT.copy()
90         data["healVnfData"]["vnfInstanceId"] = self.nf_inst_id
91         response = self.client.post(self.url % ns_inst_id, data=data)
92         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
93
94     # add healNsData
95     @mock.patch.object(NSHealService, "start")
96     def test_heal_ns_heal_non_existing_ns(self, mock_start):
97         mock_start.side_effect = NSLCMException("NS Not Found")
98         ns_inst_id = "2"
99         data = HEAL_NS_DICT.copy()
100         data["healNsData"]["vnfInstanceId"] = self.nf_inst_id
101         response = self.client.post(self.url % ns_inst_id, data=data)
102         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
103
104     @mock.patch.object(NSHealService, "start")
105     def test_heal_vnf_empty_post(self, mock_start):
106         mock_start.side_effect = NSLCMException("healVnfData parameter does not exist or value is incorrect.")
107         response = self.client.post(self.url % self.ns_inst_id, data={})
108         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)