Fix vfc-vnflcm pep8 issues
[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     def test_create_vnf_identifier_when_vnf_is_exist(self):
55         NfInstModel.objects.create(nfinstid='1111',
56                                    nf_name='vFW_01',
57                                    package_id='222',
58                                    version='',
59                                    vendor='',
60                                    netype='',
61                                    vnfd_model='',
62                                    status='NOT_INSTANTIATED',
63                                    nf_desc='vFW in Nanjing TIC Edge',
64                                    vnfdid='111',
65                                    create_time=now_time())
66         data = {
67             "vnfdId": "111",
68             "vnfInstanceName": "vFW_01",
69             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
70         }
71         response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
72         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
73         context = json.loads(response.content)
74         self.assertEqual({'error': 'VNF is already exist.'}, context)
75
76     @mock.patch.object(restcall, 'call_req')
77     def test_create_vnf_identifier(self, mock_call_req):
78         r2_get_vnfpackage_from_catalog = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
79         mock_call_req.side_effect = [r2_get_vnfpackage_from_catalog]
80         data = {
81             "vnfdId": "111",
82             "vnfInstanceName": "vFW_01",
83             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"
84         }
85         response = self.client.post("/api/vnflcm/v1/vnf_instances", data=data, format='json')
86         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
87         context = json.loads(response.content)
88         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
89
90     @mock.patch.object(InstVnf, 'run')
91     def test_instantiate_vnf(self, mock_run):
92         mock_run.re.return_value = None
93         response = self.client.post("/api/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
94         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
95
96     def test_instantiate_vnf_when_inst_id_not_exist(self):
97         self.nf_inst_id = str(uuid.uuid4())
98         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
99         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
100         jobs = JobStatusModel.objects.filter(jobid=self.job_id,
101                                              progress=0,
102                                              descp="INST_VNF_READY")
103         self.assertEqual(1, len(jobs))
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, "VNF nf_inst_id is not exist.")
107
108     @mock.patch.object(restcall, 'call_req')
109     def test_instantiate_vnf_when_get_packageinfo_by_csarid_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 = [1, json.JSONEncoder().encode(vnfpackage_info), '200']
122         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid]
123         self.nf_inst_id = '1111'
124         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
125         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
126         data = inst_req_data
127         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
128         self.assert_job_result(self.job_id, 255, "Failed to query vnf CSAR(111) from catalog.")
129
130     @mock.patch.object(restcall, 'call_req')
131     def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
132         NfInstModel.objects.create(nfinstid='1111',
133                                    nf_name='vFW_01',
134                                    package_id='222',
135                                    version='',
136                                    vendor='',
137                                    netype='',
138                                    vnfd_model='',
139                                    status='NOT_INSTANTIATED',
140                                    nf_desc='vFW in Nanjing TIC Edge',
141                                    vnfdid='111',
142                                    create_time=now_time())
143         r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
144         r2_apply_grant_result = [1, json.JSONEncoder().encode(self.grant_result), '200']
145         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
146         self.nf_inst_id = '1111'
147         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
148         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
149         data = inst_req_data
150         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
151         self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
152
153     @mock.patch.object(restcall, 'call_req')
154     @mock.patch.object(api, 'call')
155     def test_instantiate_vnf_when_unexpected_exception(self, mock_call, mock_call_req):
156         NfInstModel.objects.create(nfinstid='1111',
157                                    nf_name='vFW_01',
158                                    package_id='222',
159                                    version='',
160                                    vendor='',
161                                    netype='',
162                                    vnfd_model='',
163                                    status='NOT_INSTANTIATED',
164                                    nf_desc='vFW in Nanjing TIC Edge',
165                                    vnfdid='111',
166                                    create_time=now_time())
167         r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
168         r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
169         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result]
170         mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume]
171         self.nf_inst_id = '1111'
172         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
173         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
174         data = inst_req_data
175         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
176         self.assert_job_result(self.job_id, 255, "unexpected exception")
177
178     @mock.patch.object(restcall, 'call_req')
179     @mock.patch.object(api, 'call')
180     def test_instantiate_vnf_success(self, mock_call, mock_call_req):
181         NfInstModel.objects.create(nfinstid='1111',
182                                    nf_name='vFW_01',
183                                    package_id='222',
184                                    version='',
185                                    vendor='',
186                                    netype='',
187                                    vnfd_model='',
188                                    status='NOT_INSTANTIATED',
189                                    nf_desc='vFW in Nanjing TIC Edge',
190                                    vnfdid='111',
191                                    create_time=now_time())
192         r1_get_vnfpackage_by_vnfdid = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
193         r2_apply_grant_result = [0, json.JSONEncoder().encode(self.grant_result), '200']
194         r3_lcm_notify_result = [0, json.JSONEncoder().encode(''), '200']
195         mock_call_req.side_effect = [r1_get_vnfpackage_by_vnfdid, r2_apply_grant_result, r3_lcm_notify_result]
196         mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume,
197                                  c4_data_create_network, c5_data_create_subnet, c6_data_create_port,
198                                  c7_data_create_flavor, c8_data_list_image, c9_data_create_vm, c10_data_get_vm]
199         self.nf_inst_id = '1111'
200         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
201         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
202         data = inst_req_data
203         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
204         self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")