Fix vfc-vnflcm unittests
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / vnfs / tests / test_vnf_create.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
15 import json
16 import uuid
17
18 import mock
19 from django.test import TestCase, Client
20 from rest_framework import status
21
22 from lcm.nf.vnfs.const import c1_data_get_tenant_id, c4_data_create_network, c2_data_create_volume, \
23     c5_data_create_subnet, c3_data_get_volume, c6_data_create_port, c7_data_create_flavor, c8_data_list_image, \
24     c9_data_create_vm, c10_data_get_vm, inst_req_data, vnfpackage_info
25 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
26 from lcm.pub.database.models import NfInstModel, JobStatusModel
27 from lcm.pub.utils import restcall
28 from lcm.pub.utils.jobutil import JobUtil
29 from lcm.pub.utils.timeutil import now_time
30 from lcm.pub.vimapi import api
31
32
33 class TestNFInstantiate(TestCase):
34     def setUp(self):
35         self.client = Client()
36         self.grant_result = {
37             "vim": {
38                 "vimid": 'vimid_1',
39                 "accessinfo": {
40                     "tenant": 'tenantname_1'
41                 }
42             }
43         }
44
45     def tearDown(self):
46         pass
47
48     def assert_job_result(self, job_id, job_progress, job_detail):
49         jobs = JobStatusModel.objects.filter(jobid=job_id,
50                                              progress=job_progress,
51                                              descp=job_detail)
52         self.assertEqual(1, len(jobs))
53
54     @mock.patch.object(restcall, 'call_req')
55     def test_create_vnf_identifier(self, mock_call_req):
56         r2_get_vnfpackage_from_catalog = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
57         mock_call_req.side_effect = [r2_get_vnfpackage_from_catalog]
58         data = {
59             "vnfdId": "111",
60             "vnfInstanceName": "vFW_01",
61             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
62         }
63         response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
64         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
65         context = json.loads(response.content)
66         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
67
68     @mock.patch.object(InstVnf, 'run')
69     def test_instantiate_vnf(self, mock_run):
70         mock_run.re.return_value = None
71         response = self.client.post("/api/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
72         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
73
74     def test_instantiate_vnf_when_inst_id_not_exist(self):
75         self.nf_inst_id = str(uuid.uuid4())
76         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
77         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
78         jobs = JobStatusModel.objects.filter(jobid=self.job_id,
79                                              progress=0,
80                                              descp="INST_VNF_READY")
81         self.assertEqual(1, len(jobs))
82         data = inst_req_data
83         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
84         self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
85
86     @mock.patch.object(restcall, 'call_req')
87     def test_instantiate_vnf_when_get_rawdata_by_csarid_failed(self, mock_call_req):
88         NfInstModel.objects.create(nfinstid='1111',
89                                    nf_name='vFW_01',
90                                    package_id='222',
91                                    version='',
92                                    vendor='',
93                                    netype='',
94                                    vnfd_model='',
95                                    status='NOT_INSTANTIATED',
96                                    nf_desc='vFW in Nanjing TIC Edge',
97                                    vnfdid='111',
98                                    create_time=now_time())
99         r1_get_vnfpackage_by_vnfdid = [1, json.JSONEncoder().encode(vnfpackage_info), '200']
100         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid]
101         self.nf_inst_id = '1111'
102         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
103         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
104         data = inst_req_data
105         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
106         self.assert_job_result(self.job_id, 255, "Failed to query vnf CSAR(111) from catalog.")
107
108     @mock.patch.object(restcall, 'call_req')
109     def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
110         NfInstModel.objects.create(nfinstid='1111',
111                                    nf_name='vFW_01',
112                                    package_id='222',
113                                    version='',
114                                    vendor='',
115                                    netype='',
116                                    vnfd_model='',
117                                    status='NOT_INSTANTIATED',
118                                    nf_desc='vFW in Nanjing TIC Edge',
119                                    vnfdid='111',
120                                    create_time=now_time())
121         r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
122         r2_apply_grant_result = [1, json.JSONEncoder().encode(self.grant_result), '200']
123         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
124         self.nf_inst_id = '1111'
125         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
126         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
127         data = inst_req_data
128         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
129         self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
130
131     @mock.patch.object(restcall, 'call_req')
132     @mock.patch.object(api, 'call')
133     def test_instantiate_vnf_when_unexpected_exception(self, mock_call, mock_call_req):
134         NfInstModel.objects.create(nfinstid='1111',
135                                    nf_name='vFW_01',
136                                    package_id='222',
137                                    version='',
138                                    vendor='',
139                                    netype='',
140                                    vnfd_model='',
141                                    status='NOT_INSTANTIATED',
142                                    nf_desc='vFW in Nanjing TIC Edge',
143                                    vnfdid='111',
144                                    create_time=now_time())
145         r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
146         r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
147         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
148         mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume]
149         self.nf_inst_id = '1111'
150         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
151         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
152         data = inst_req_data
153         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
154         self.assert_job_result(self.job_id, 255, "unexpected exception")
155
156     @mock.patch.object(restcall, 'call_req')
157     @mock.patch.object(api, 'call')
158     def test_instantiate_vnf_success(self, mock_call, mock_call_req):
159         NfInstModel.objects.create(nfinstid='1111',
160                                    nf_name='vFW_01',
161                                    package_id='222',
162                                    version='',
163                                    vendor='',
164                                    netype='',
165                                    vnfd_model='',
166                                    status='NOT_INSTANTIATED',
167                                    nf_desc='vFW in Nanjing TIC Edge',
168                                    vnfdid='111',
169                                    create_time=now_time())
170         r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
171         r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
172         r3_lcm_notify_result = [0, json.JSONEncoder().encode(''), '200']
173         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result, r3_lcm_notify_result]
174         mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
175                                  c4_data_create_network, c5_data_create_subnet, c6_data_create_port,
176                                  c7_data_create_flavor, c8_data_list_image, c9_data_create_vm, c10_data_get_vm]
177         self.nf_inst_id = '1111'
178         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
179         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
180         data = inst_req_data
181         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
182         self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")