Modify testcase of terminate VNF instance
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / tests / test_vnf_cancel.py
1 # Copyright 2017 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 import uuid
15
16 import mock
17 from django.test import TestCase, Client
18 from rest_framework import status
19
20 from lcm.nf.vnfs.vnf_cancel.term_vnf import TermVnf
21 from lcm.pub.database.models import NfInstModel, JobStatusModel
22 from lcm.pub.utils.jobutil import JobUtil
23 from lcm.pub.utils.timeutil import now_time
24
25
26 class TestNFTerminate(TestCase):
27     def setUp(self):
28         self.client = Client()
29
30     def tearDown(self):
31         pass
32
33     def assert_job_result(self, job_id, job_progress, job_detail):
34         jobs = JobStatusModel.objects.filter(
35             jobid=job_id,
36             progress=job_progress,
37             descp=job_detail)
38         self.assertEqual(1, len(jobs))
39
40     def test_delete_vnf_identifier(self):
41         NfInstModel.objects.create(nfinstid='1111', mnfinstid='1111', nf_name='2222',
42                                    package_id='todo', vnfm_inst_id='todo', version='', vendor='',
43                                    producttype='', netype='', vnfd_model='',
44                                    instantiationState='VNF_INSTANTIATED', nf_desc='', vnfdid='',
45                                    vnfSoftwareVersion='', vnfConfigurableProperties='todo',
46                                    localizationLanguage='EN_US', create_time=now_time())
47         response = self.client.delete("/openoapi/vnflcm/v1/vnf_instances/1111")
48         self.failUnlessEqual(status.HTTP_204_NO_CONTENT, response.status_code)
49         self.assertEqual(None, response.data)
50
51     def test_delete_vnf_identifier_when_vnf_not_exist(self):
52         response = self.client.delete("/openoapi/vnflcm/v1/vnf_instances/1111")
53         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
54         self.assertEqual("VnfInst(1111) does not exist", response.data["error"])
55
56     def test_delete_vnf_identifier_when_instantiationState_check_failed(self):
57         NfInstModel.objects.create(nfinstid='1111', mnfinstid='1111', nf_name='2222',
58                                    package_id='todo', vnfm_inst_id='todo', version='', vendor='',
59                                    producttype='', netype='', vnfd_model='',
60                                    instantiationState='NOT_INSTANTIATED', nf_desc='', vnfdid='',
61                                    vnfSoftwareVersion='', vnfConfigurableProperties='todo',
62                                    localizationLanguage='EN_US', create_time=now_time())
63         response = self.client.delete("/openoapi/vnflcm/v1/vnf_instances/1111")
64         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
65         self.assertEqual("No instantiated vnf", response.data["error"])
66
67     @mock.patch.object(TermVnf, 'run')
68     def test_terminate_vnf(self, mock_run):
69         mock_run.re.return_value = None
70         response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/terminate", data={}, format='json')
71         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
72
73     def test_terminate_vnf_when_inst_id_not_exist(self):
74         data = {"terminationType": "GRACEFUL",
75                 "gracefulTerminationTimeout": 120}
76         self.nf_inst_id = str(uuid.uuid4())
77         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
78         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
79         TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
80         self.assert_job_result(self.job_id, 255, "VnfInst(%s) does not exist" % self.nf_inst_id)
81
82     def test_terminate_vnf_success(self):
83         NfInstModel.objects.create(nfinstid='1111', mnfinstid='1111', nf_name='2222',
84                                    package_id='todo', vnfm_inst_id='todo', version='', vendor='',
85                                    producttype='', netype='', vnfd_model='',
86                                    instantiationState='VNF_INSTANTIATED', nf_desc='', vnfdid='',
87                                    vnfSoftwareVersion='', vnfConfigurableProperties='todo',
88                                    localizationLanguage='EN_US', create_time=now_time())
89         data = {"terminationType": "FORCEFUL",
90                 "gracefulTerminationTimeout": 120}
91         self.nf_inst_id = '1111'
92         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
93         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
94         TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
95         self.assert_job_result(self.job_id, 100, "Terminate Vnf success.")