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.vnf_create.create_vnf_identifier import CreateVnf
22 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
23 from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel, VmInstModel, NetworkInstModel, \
24 SubNetworkInstModel, PortInstModel
25 from lcm.pub.utils import restcall
26 from lcm.pub.utils.jobutil import JobUtil
29 class TestNsInstantiate(TestCase):
31 self.client = Client()
32 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
34 VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2",
35 vmname="test_02", operationalstate=1)
36 NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
37 tenant='admin', insttype=0, instid='1')
38 SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
39 name='sub_pnet', tenant='admin', insttype=0, instid='1')
40 PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
41 name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
45 VmInstModel.objects.all().delete()
46 NetworkInstModel.objects.all().delete()
47 SubNetworkInstModel.objects.all().delete()
48 PortInstModel.objects.all().delete()
50 def assert_job_result(self, job_id, job_progress, job_detail):
51 jobs = JobStatusModel.objects.filter(
53 progress=job_progress,
55 self.assertEqual(1, len(jobs))
57 def test_swagger_ok(self):
58 response = self.client.get("/openoapi/vnflcm/v1/swagger.json", format='json')
59 self.assertEqual(response.status_code, status.HTTP_200_OK)
61 @mock.patch.object(restcall, 'call_req')
62 def test_create_vnf_identifier(self, mock_call_req):
63 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
64 mock_call_req.side_effect = [r1]
67 "vnfInstanceName": "vFW_01",
68 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
69 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances", data=data, format='json')
70 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
71 context = json.loads(response.content)
72 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
74 @mock.patch.object(InstVnf, 'run')
75 def test_instantiate_vnf(self, mock_run):
76 mock_run.re.return_value = None
77 response = self.client.post("/openoapi/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
78 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
80 def test_instantiate_vnf_when_inst_id_not_exist(self):
81 self.nf_inst_id = str(uuid.uuid4())
82 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
83 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
85 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
86 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
88 @mock.patch.object(restcall, 'call_req')
89 def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
90 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
91 r2 = [0, json.JSONEncoder().encode(''), '200']
92 mock_call_req.side_effect = [r1, r2]
95 "vnfInstanceName": "vFW_01",
96 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
97 self.nf_inst_id = CreateVnf(create_data).do_biz()
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, "Input parameter is not defined in vnfd_info.")
104 @mock.patch.object(restcall, 'call_req')
105 def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
106 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
107 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
108 mock_call_req.side_effect = [r1, r2]
111 "vnfInstanceName": "vFW_01",
112 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
113 self.nf_inst_id = CreateVnf(create_data).do_biz()
114 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
115 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
117 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
118 self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
120 @mock.patch.object(restcall, 'call_req')
121 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
122 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
123 nfvouser='root', nfvopassword='root123')
124 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
125 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
126 r3 = [1, json.JSONEncoder().encode(''), '200']
127 mock_call_req.side_effect = [r1, r2, r3]
130 "vnfInstanceName": "vFW_01",
131 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
132 self.nf_inst_id = CreateVnf(create_data).do_biz()
133 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
134 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
136 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
137 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
139 # @mock.patch.object(restcall, 'call_req')
140 # # @mock.patch.object(adaptor, 'create_vim_res')
141 # def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
142 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
143 # nfvouser='root', nfvopassword='root123')
144 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
145 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
146 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
147 # # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
148 # mock_call_req.side_effect = [r1, r2, r3]
149 # # mock_create_vim_res.re.return_value = None
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")
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, "Create resource failed")
161 @mock.patch.object(restcall, 'call_req')
162 # @mock.patch.object(adaptor, 'create_vim_res')
163 def test_instantiate_vnf_success(self, mock_call_req):
164 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
165 nfvouser='root', nfvopassword='root123')
166 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
167 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
168 r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
169 r4 = [0, json.JSONEncoder().encode('None'), '200']
170 mock_call_req.side_effect = [r1, r2, r3, r4]
171 # mock_create_vim_res.re.return_value = None
174 "vnfInstanceName": "vFW_01",
175 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
176 self.nf_inst_id = CreateVnf(create_data).do_biz()
177 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
178 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
180 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
181 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
184 "flavourId": "flavour_1",
185 "instantiationLevelId": "instantiationLevel_1",
195 "protocolType": "http"
198 "tenant": "tenant_vCPE",
200 "password": "vCPE_321"
202 "interfaceEndpoint": "http://10.43.21.105:80/"
204 "resourceId": "1246",
210 "addressType": "MAC",
211 "l2AddressData": "00:f3:43:20:a2:a3"
216 "iPAddressType": "IPv4",
217 "iPAddress": "192.168.104.2"
221 "numDynamicAddresses": 0
226 "localizationLanguage": "en_US",
227 "additionalParams": {}
231 'local_storages': [],
234 'volumn_storages': [],
238 'local_storages': [],
239 'vdu_id': u'vdu_omm.001',
240 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
246 'support_scaling': False,
249 'storage_policy': '',
252 'availability_zone': '',
257 'inject_data_list': [],
261 'local_affinity_antiaffinity_rule': {},
262 'template_id': u'omm.001',
263 'manual_scale_select_vim': False},
264 'description': u'singleommvm'},
266 'volumn_storages': [],
270 'local_storages': [],
272 'image_file': u'sss',
278 'support_scaling': False,
281 'storage_policy': '',
284 'availability_zone': '',
289 'inject_data_list': [],
293 'local_affinity_antiaffinity_rule': {},
295 'manual_scale_select_vim': False},
296 'description': u'ompvm'},
298 'volumn_storages': [],
302 'local_storages': [],
304 'image_file': u'sss',
310 'support_scaling': False,
313 'storage_policy': '',
316 'availability_zone': '',
321 'inject_data_list': [],
325 'local_affinity_antiaffinity_rule': {},
327 'manual_scale_select_vim': False},
328 'description': u'ompvm'},
330 'volumn_storages': [],
334 'local_storages': [],
336 'image_file': u'sss',
342 'support_scaling': False,
345 'storage_policy': '',
348 'availability_zone': '',
353 'inject_data_list': [],
357 'local_affinity_antiaffinity_rule': {},
359 'manual_scale_select_vim': False},
360 'description': u'ompvm'},
362 'volumn_storages': [],
366 'local_storages': [],
368 'image_file': u'sss',
374 'support_scaling': False,
377 'storage_policy': '',
380 'availability_zone': '',
385 'inject_data_list': [],
389 'local_affinity_antiaffinity_rule': {},
390 'template_id': u'10',
391 'manual_scale_select_vim': False},
392 'description': u'ppvm'},
394 'volumn_storages': [],
398 'local_storages': [],
400 'image_file': u'sss',
406 'support_scaling': False,
409 'storage_policy': '',
412 'availability_zone': '',
417 'inject_data_list': [],
421 'local_affinity_antiaffinity_rule': {},
422 'template_id': u'11',
423 'manual_scale_select_vim': False},
424 'description': u'ppvm'},
426 'volumn_storages': [],
430 'local_storages': [],
432 'image_file': u'sss',
438 'support_scaling': False,
441 'storage_policy': '',
444 'availability_zone': '',
449 'inject_data_list': [],
453 'local_affinity_antiaffinity_rule': {},
454 'template_id': u'12',
455 'manual_scale_select_vim': False},
456 'description': u'ppvm'}],
457 'volumn_storages': [],
461 'policy_id': u'policy_scale_sss-vnf-template',
463 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
469 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
471 'disk_format': u'VMDK',
472 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
473 'container_type': 'vm',
475 'hypervisor_type': 'kvm'},
476 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
482 'disk_format': u'VMDK',
483 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
484 'container_type': 'vm',
486 'hypervisor_type': 'kvm'},
487 'image_file_id': u'sss'}],
494 'domain_type': u'CN',
495 'version': u'v4.14.10',
496 'vmnumber_overquota_alarm': False,
499 'vnfd_version': u'V00000001',
500 'id': u'sss-vnf-template',
501 'name': u'sss-vnf-template'},
502 "flavourId": "flavour_1",
503 "instantiationLevelId": "instantiationLevel_1",
513 "protocolType": "http"
516 "tenant": "tenant_vCPE",
518 "password": "vCPE_321"
520 "interfaceEndpoint": "http://10.43.21.105:80/"
522 "resourceId": "1246",
528 "addressType": "MAC",
529 "l2AddressData": "00:f3:43:20:a2:a3"
534 "iPAddressType": "IPv4",
535 "iPAddress": "192.168.104.2"
539 "numDynamicAddresses": 0
544 "localizationLanguage": "en_US",
545 "additionalParams": {}