1 # Copyright 2017 ZTE Corporation.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from django.test import TestCase, Client
19 from rest_framework import status
21 from lcm.nf.vnfs.const import vnfd_rawdata
22 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
23 from lcm.pub.database.models import NfInstModel, JobStatusModel, VmInstModel, NetworkInstModel, \
24 SubNetworkInstModel, PortInstModel
25 from lcm.pub.utils import restcall
26 from lcm.pub.utils.jobutil import JobUtil
27 from lcm.pub.utils.timeutil import now_time
30 class TestNFInstantiate(TestCase):
32 self.client = Client()
33 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
35 VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2",
36 vmname="test_02", operationalstate=1)
37 NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
38 tenant='admin', insttype=0, instid='1')
39 SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
40 name='sub_pnet', tenant='admin', insttype=0, instid='1')
41 PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
42 name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
46 VmInstModel.objects.all().delete()
47 NetworkInstModel.objects.all().delete()
48 SubNetworkInstModel.objects.all().delete()
49 PortInstModel.objects.all().delete()
51 def assert_job_result(self, job_id, job_progress, job_detail):
52 jobs = JobStatusModel.objects.filter(
54 progress=job_progress,
56 self.assertEqual(1, len(jobs))
58 def test_swagger_ok(self):
59 response = self.client.get("/openoapi/vnflcm/v1/swagger.json", format='json')
60 self.assertEqual(response.status_code, status.HTTP_200_OK)
62 @mock.patch.object(restcall, 'call_req')
63 def test_create_vnf_identifier(self, mock_call_req):
64 r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
65 r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200'] # get rawdata from catalog by csar_id
66 mock_call_req.side_effect = [r1, r2]
69 "vnfInstanceName": "vFW_01",
70 "vnfInstanceDescription": "vFW in Nanjing TIC Edge"}
71 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances", data=data, format='json')
72 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
73 context = json.loads(response.content)
74 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
76 @mock.patch.object(InstVnf, 'run')
77 def test_instantiate_vnf(self, mock_run):
78 mock_run.re.return_value = None
79 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
80 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
82 def test_instantiate_vnf_when_inst_id_not_exist(self):
83 self.nf_inst_id = str(uuid.uuid4())
84 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
85 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
87 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
88 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
90 @mock.patch.object(restcall, 'call_req')
91 def test_instantiate_vnf_when_get_package_info_by_vnfdid_failed(self, mock_call_req):
92 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
93 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
94 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
95 r1 = [1, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
96 mock_call_req.side_effect = [r1]
97 self.nf_inst_id = '1111'
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")
101 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
102 self.assert_job_result(self.job_id, 255, "Failed to query package_info of vnfdid(111) from nslcm.")
104 @mock.patch.object(restcall, 'call_req')
105 def test_instantiate_vnf_when_get_rawdata_by_csarid_failed(self, mock_call_req):
106 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
107 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
108 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
109 r1 = [0, json.JSONEncoder().encode({'package_id':'222', 'csar_id':'2222'}), '200'] # get csar_id from nslcm by vnfd_id
110 r2 = [1, json.JSONEncoder().encode(vnfd_rawdata), '200'] # get rawdata from catalog by csar_id
111 mock_call_req.side_effect = [r1, r2]
112 self.nf_inst_id = '1111'
113 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
114 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
116 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
117 self.assert_job_result(self.job_id, 255, "Failed to query rawdata of CSAR(2222) from catalog.")
119 @mock.patch.object(restcall, 'call_req')
120 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
121 NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
122 version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
123 nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
124 r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}), '200'] # get csar_id from nslcm by vnfd_id
125 r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200'] # get rawdata from catalog by csar_id
126 r3 = [1, json.JSONEncoder().encode({"vim":
129 "accessinfo": {"tenant": '2'}
131 }), '200'] # apply_grant_to_nfvo
132 mock_call_req.side_effect = [r1, r2, r3]
133 self.nf_inst_id = '1111'
134 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
135 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
137 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
138 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
145 # @mock.patch.object(restcall, 'call_req')
146 # def test_instantiate_vnf_when_(self, mock_call_req):
147 # NfInstModel.objects.create(nfinstid='1111', nf_name='vFW_01', package_id='222',
148 # version='', vendor='', netype='', vnfd_model='', status='NOT_INSTANTIATED',
149 # nf_desc='vFW in Nanjing TIC Edge', vnfdid='111', create_time=now_time())
150 # r1 = [0, json.JSONEncoder().encode({'package_id': '222', 'csar_id': '2222'}), '200'] # get csar_id from nslcm by vnfd_id
151 # r2 = [0, json.JSONEncoder().encode(vnfd_rawdata), '200'] # get rawdata from catalog by csar_id
152 # r3 = [0, json.JSONEncoder().encode({"vim":{"vimid": '1', "accessinfo": {"tenant": '2'}}}), '200'] # apply_grant_to_nfvo
153 # mock_call_req.side_effect = [r1, r2, r3]
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, "Nf instancing apply grant exception")
163 # @mock.patch.object(restcall, 'call_req')
164 # # @mock.patch.object(adaptor, 'create_vim_res')
165 # def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
166 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
167 # nfvouser='root', nfvopassword='root123')
168 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
169 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
170 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
171 # # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
172 # mock_call_req.side_effect = [r1, r2, r3]
173 # # mock_create_vim_res.re.return_value = None
176 # "vnfInstanceName": "vFW_01",
177 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
178 # self.nf_inst_id = CreateVnf(create_data).do_biz()
179 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
180 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
181 # data = inst_req_data
182 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
183 # self.assert_job_result(self.job_id, 255, "Create resource failed")
185 # @mock.patch.object(restcall, 'call_req')
186 # # @mock.patch.object(adaptor, 'create_vim_res')
187 # def test_instantiate_vnf_success(self, mock_call_req):
188 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
189 # nfvouser='root', nfvopassword='root123')
190 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
191 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
192 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
193 # r4 = [0, json.JSONEncoder().encode('None'), '200']
194 # mock_call_req.side_effect = [r1, r2, r3, r4]
195 # # mock_create_vim_res.re.return_value = None
198 # "vnfInstanceName": "vFW_01",
199 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
200 # self.nf_inst_id = CreateVnf(create_data).do_biz()
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.")
208 "flavourId": "flavour_1",
209 "instantiationLevelId": "instantiationLevel_1",
219 "protocolType": "http"
222 "tenant": "tenant_vCPE",
224 "password": "vCPE_321"
226 "interfaceEndpoint": "http://10.43.21.105:80/"
228 "resourceId": "1246",
234 "addressType": "MAC",
235 "l2AddressData": "00:f3:43:20:a2:a3"
240 "iPAddressType": "IPv4",
241 "iPAddress": "192.168.104.2"
245 "numDynamicAddresses": 0
250 "localizationLanguage": "en_US",
251 "additionalParams": {}