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