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.
19 from django.test import TestCase, Client
20 from rest_framework import status
22 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
23 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
24 from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel, VmInstModel, NetworkInstModel, \
26 from lcm.pub.utils import restcall
27 from lcm.pub.utils.jobutil import JobUtil
30 "flavourId": "flavour_1",
31 "instantiationLevelId": "instantiationLevel_1",
41 "protocolType": "http"
44 "tenant": "tenant_vCPE",
46 "password": "vCPE_321"
48 "interfaceEndpoint": "http://10.43.21.105:80/"
57 "l2AddressData": "00:f3:43:20:a2:a3"
62 "iPAddressType": "IPv4",
63 "iPAddress": "192.168.104.2"
67 "numDynamicAddresses": 0
72 "localizationLanguage": "en_US",
73 "additionalParams": {}
77 class TestNsInstantiate(TestCase):
79 self.client = Client()
80 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
82 VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2", vmname="test_02",
84 NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
85 tenant='admin', insttype=0, instid='1')
86 SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
87 name='sub_pnet',tenant='admin', insttype=0, instid='1')
92 def assert_job_result(self, job_id, job_progress, job_detail):
93 jobs = JobStatusModel.objects.filter(
95 progress=job_progress,
97 self.assertEqual(1, len(jobs))
99 def test_swagger_ok(self):
100 response = self.client.get("/gvnfmapi/lcm/v1/swagger.json", format='json')
101 self.assertEqual(response.status_code, status.HTTP_200_OK)
103 @mock.patch.object(restcall, 'call_req')
104 def test_create_vnf_identifier(self, mock_call_req):
105 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
106 mock_call_req.side_effect = [r1]
109 "vnfInstanceName": "vFW_01",
110 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
111 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
112 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
113 context = json.loads(response.content)
114 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
116 @mock.patch.object(InstVnf, 'run')
117 def test_instantiate_vnf(self, mock_run):
118 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
119 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
121 def test_instantiate_vnf_when_inst_id_not_exist(self):
122 self.nf_inst_id = str(uuid.uuid4())
123 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
124 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
126 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
127 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
129 @mock.patch.object(restcall, 'call_req')
130 def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
131 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
132 r2 = [0, json.JSONEncoder().encode(''), '200']
133 mock_call_req.side_effect = [r1, r2]
136 "vnfInstanceName": "vFW_01",
137 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
138 self.nf_inst_id = CreateVnf(create_data).do_biz()
139 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
140 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
142 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
143 self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
145 @mock.patch.object(restcall, 'call_req')
146 def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
147 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
148 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
149 mock_call_req.side_effect = [r1, r2]
152 "vnfInstanceName": "vFW_01",
153 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
154 self.nf_inst_id = CreateVnf(create_data).do_biz()
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")
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, "Nfvo was not registered")
161 @mock.patch.object(restcall, 'call_req')
162 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
163 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
164 nfvouser='root', nfvopassword='root123')
165 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
166 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
167 r3 = [1, json.JSONEncoder().encode(''), '200']
168 mock_call_req.side_effect = [r1, r2, r3]
171 "vnfInstanceName": "vFW_01",
172 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
173 self.nf_inst_id = CreateVnf(create_data).do_biz()
174 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
175 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
177 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
178 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
180 @mock.patch.object(restcall, 'call_req')
181 def test_instantiate_vnf_success(self, mock_call_req):
182 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
183 nfvouser='root', nfvopassword='root123')
184 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
185 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
186 r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
187 r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
188 mock_call_req.side_effect = [r1, r2, r3, r4]
191 "vnfInstanceName": "vFW_01",
192 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
193 self.nf_inst_id = CreateVnf(create_data).do_biz()
194 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
195 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
197 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
198 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
202 'local_storages': [],
205 'volumn_storages': [],
209 'local_storages': [],
210 'vdu_id': u'vdu_omm.001',
211 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
217 'support_scaling': False,
220 'storage_policy': '',
223 'availability_zone': '',
228 'inject_data_list': [],
232 'local_affinity_antiaffinity_rule': {},
233 'template_id': u'omm.001',
234 'manual_scale_select_vim': False},
235 'description': u'singleommvm'},
237 'volumn_storages': [],
241 'local_storages': [],
243 'image_file': u'sss',
249 'support_scaling': False,
252 'storage_policy': '',
255 'availability_zone': '',
260 'inject_data_list': [],
264 'local_affinity_antiaffinity_rule': {},
266 'manual_scale_select_vim': False},
267 'description': u'ompvm'},
269 'volumn_storages': [],
273 'local_storages': [],
275 'image_file': u'sss',
281 'support_scaling': False,
284 'storage_policy': '',
287 'availability_zone': '',
292 'inject_data_list': [],
296 'local_affinity_antiaffinity_rule': {},
298 'manual_scale_select_vim': False},
299 'description': u'ompvm'},
301 'volumn_storages': [],
305 'local_storages': [],
307 'image_file': u'sss',
313 'support_scaling': False,
316 'storage_policy': '',
319 'availability_zone': '',
324 'inject_data_list': [],
328 'local_affinity_antiaffinity_rule': {},
330 'manual_scale_select_vim': False},
331 'description': u'ompvm'},
333 'volumn_storages': [],
337 'local_storages': [],
339 'image_file': u'sss',
345 'support_scaling': False,
348 'storage_policy': '',
351 'availability_zone': '',
356 'inject_data_list': [],
360 'local_affinity_antiaffinity_rule': {},
361 'template_id': u'10',
362 'manual_scale_select_vim': False},
363 'description': u'ppvm'},
365 'volumn_storages': [],
369 'local_storages': [],
371 'image_file': u'sss',
377 'support_scaling': False,
380 'storage_policy': '',
383 'availability_zone': '',
388 'inject_data_list': [],
392 'local_affinity_antiaffinity_rule': {},
393 'template_id': u'11',
394 'manual_scale_select_vim': False},
395 'description': u'ppvm'},
397 'volumn_storages': [],
401 'local_storages': [],
403 'image_file': u'sss',
409 'support_scaling': False,
412 'storage_policy': '',
415 'availability_zone': '',
420 'inject_data_list': [],
424 'local_affinity_antiaffinity_rule': {},
425 'template_id': u'12',
426 'manual_scale_select_vim': False},
427 'description': u'ppvm'}],
428 'volumn_storages': [],
432 'policy_id': u'policy_scale_sss-vnf-template',
434 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
440 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
442 'disk_format': u'VMDK',
443 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
444 'container_type': 'vm',
446 'hypervisor_type': 'kvm'},
447 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
453 'disk_format': u'VMDK',
454 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
455 'container_type': 'vm',
457 'hypervisor_type': 'kvm'},
458 'image_file_id': u'sss'}],
465 'domain_type': u'CN',
466 'version': u'v4.14.10',
467 'vmnumber_overquota_alarm': False,
470 'vnfd_version': u'V00000001',
471 'id': u'sss-vnf-template',
472 'name': u'sss-vnf-template'},
473 "flavourId": "flavour_1",
474 "instantiationLevelId": "instantiationLevel_1",
484 "protocolType": "http"
487 "tenant": "tenant_vCPE",
489 "password": "vCPE_321"
491 "interfaceEndpoint": "http://10.43.21.105:80/"
493 "resourceId": "1246",
499 "addressType": "MAC",
500 "l2AddressData": "00:f3:43:20:a2:a3"
505 "iPAddressType": "IPv4",
506 "iPAddress": "192.168.104.2"
510 "numDynamicAddresses": 0
515 "localizationLanguage": "en_US",
516 "additionalParams": {}