Refactor code and test case of gvnfmlcm
[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 import json
15 import uuid
16
17 import mock
18 from django.test import TestCase, Client
19 from rest_framework import status
20
21 from lcm.nf.vnfs.const import vnfd_rawdata, c1_data_get_tenant_id, c4_data_create_network, c2_data_create_volume, \
22     c5_data_create_subnet, c3_data_get_volume, c6_data_create_port, c7_data_create_flavor, c8_data_list_image, c9_data_create_vm, \
23     c10_data_get_vm, inst_req_data
24 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
25 from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, \
26     SubNetworkInstModel, PortInstModel
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         VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
37                                    operationalstate=1)
38         VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2",
39                                    vmname="test_02", operationalstate=1)
40         NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
41                                         tenant='admin', insttype=0, instid='1')
42         SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
43                                            name='sub_pnet', tenant='admin', insttype=0, instid='1')
44         PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
45                                      name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
46
47     def tearDown(self):
48         pass
49         VmInstModel.objects.all().delete()
50         NetworkInstModel.objects.all().delete()
51         SubNetworkInstModel.objects.all().delete()
52         PortInstModel.objects.all().delete()
53
54     def assert_job_result(self, job_id, job_progress, job_detail):
55         jobs = JobStatusModel.objects.filter(
56             jobid=job_id,
57             progress=job_progress,
58             descp=job_detail)
59         self.assertEqual(1, len(jobs))
60
61     def test_swagger_ok(self):
62         response = self.client.get("/openoapi/vnflcm/v1/swagger.json", format='json')
63         self.assertEqual(response.status_code, status.HTTP_200_OK)
64
65     @mock.patch.object(restcall, 'call_req')
66     def test_create_vnf_identifier(self, mock_call_req):
67         r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200']  # get csar_id from nslcm by vnfd_id
68         r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
69         mock_call_req.side_effect = [r1, r2]
70         data = {
71             "vnfdId": "111",
72             "vnfInstanceName": "vFW_01",
73             "vnfInstanceDescription": "vFW in Nanjing TIC Edge"}
74         response = self.client.post("/openoapi/vnflcm/v1/vnf_instances", data=data, format='json')
75         self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
76         context = json.loads(response.content)
77         self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
78
79     @mock.patch.object(InstVnf, 'run')
80     def test_instantiate_vnf(self, mock_run):
81         mock_run.re.return_value = None
82         response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
83         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
84
85     def test_instantiate_vnf_when_inst_id_not_exist(self):
86         self.nf_inst_id = str(uuid.uuid4())
87         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
88         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
89         jobs = JobStatusModel.objects.filter(
90             jobid=self.job_id,
91             progress=0,
92             descp="INST_VNF_READY")
93         self.assertEqual(1, len(jobs))
94         data = inst_req_data
95         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
96         self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
97
98     @mock.patch.object(restcall, 'call_req')
99     def test_instantiate_vnf_when_get_package_info_by_vnfdid_failed(self, mock_call_req):
100         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
101                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
102                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
103         r1 = [1, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200']  # get csar_id from nslcm by vnfd_id
104         mock_call_req.side_effect = [r1]
105         self.nf_inst_id = '1111'
106         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
107         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
108         data = inst_req_data
109         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
110         self.assert_job_result(self.job_id, 255, "Failed to query package_info of vnfdid(111) from nslcm.")
111
112     @mock.patch.object(restcall, 'call_req')
113     def test_instantiate_vnf_when_get_rawdata_by_csarid_failed(self, mock_call_req):
114         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
115                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
116                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
117         r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200']  # get csar_id from nslcm by vnfd_id
118         r2 = [1, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
119         mock_call_req.side_effect = [r1, r2]
120         self.nf_inst_id = '1111'
121         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
122         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
123         data = inst_req_data
124         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
125         self.assert_job_result(self.job_id, 255, "Failed to query rawdata of CSAR(2222) from catalog.")
126
127     @mock.patch.object(restcall, 'call_req')
128     def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
129         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
130                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
131                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
132         r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}), '200']  # get csar_id from nslcm by vnfd_id
133         r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
134         r3 = [1, json.JSONEncoder().encode({"vim":{"vimid": '1', "accessinfo": {"tenant": '2'}}}), '200']  # apply_grant_to_nfvo
135         mock_call_req.side_effect = [r1, r2, r3]
136         self.nf_inst_id = '1111'
137         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
138         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
139         data = inst_req_data
140         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
141         self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
142
143     @mock.patch.object(restcall, 'call_req')
144     @mock.patch.object(api, 'call')
145     def test_instantiate_vnf_when_(self, mock_call, mock_call_req):
146         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
147                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
148                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
149         r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}), '200']  # get csar_id from nslcm by vnfd_id
150         r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
151         r3 = [0, json.JSONEncoder().encode({"vim":{"vimid": '1', "accessinfo": {"tenant": '2'}}}), '200']  # apply_grant_to_nfvo
152         mock_call_req.side_effect = [r1, r2, r3]
153         mock_call.side_effect = [c1_data_get_tenant_id, c2_data_create_volume, c3_data_get_volume]
154         self.nf_inst_id = '1111'
155         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
156         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
157         data = inst_req_data
158         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
159         self.assert_job_result(self.job_id, 255, "unexpected exception")
160
161     @mock.patch.object(restcall, 'call_req')
162     @mock.patch.object(api, 'call')
163     def test_instantiate_vnf_when_111(self, mock_call, mock_call_req):
164         NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
165                                    version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
166                                    nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
167         r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}),
168               '200']  # get csar_id from nslcm by vnfd_id
169         r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200']  # get rawdata from catalog by csar_id
170         r3 = [0, json.JSONEncoder().encode({"vim": {"vimid": 'vimid_1', "accessinfo": {"tenant": 'tenantname_1'}}}),
171               '200']  # apply_grant_to_nfvo
172         r4 = [0, None, '200']
173         mock_call_req.side_effect = [r1, r2, r3, r4]
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.")