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
25 from lcm.pub.utils import restcall
26 from lcm.pub.utils.jobutil import JobUtil
29 "flavourId": "flavour_1",
30 "instantiationLevelId": "instantiationLevel_1",
40 "protocolType": "http"
43 "tenant": "tenant_vCPE",
45 "password": "vCPE_321"
47 "interfaceEndpoint": "http://10.43.21.105:80/"
56 "l2AddressData": "00:f3:43:20:a2:a3"
61 "iPAddressType": "IPv4",
62 "iPAddress": "192.168.104.2"
66 "numDynamicAddresses": 0
71 "localizationLanguage": "en_US",
72 "additionalParams": {}
76 class TestNsInstantiate(TestCase):
78 self.client = Client()
79 self.nsd_id = str(uuid.uuid4())
84 def assert_job_result(self, job_id, job_progress, job_detail):
85 jobs = JobStatusModel.objects.filter(
87 progress=job_progress,
89 self.assertEqual(1, len(jobs))
91 @mock.patch.object(restcall, 'call_req')
92 def test_create_vnf_identifier(self, mock_call_req):
93 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
94 mock_call_req.side_effect = [r1]
97 "vnfInstanceName": "vFW_01",
98 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
99 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
100 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
101 context = json.loads(response.content)
102 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
104 @mock.patch.object(InstVnf, 'run')
105 def test_instantiate_vnf(self, mock_run):
106 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
107 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
109 def test_instantiate_vnf_when_inst_id_not_exist(self):
110 self.nf_inst_id = str(uuid.uuid4())
111 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
112 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
114 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
115 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
117 @mock.patch.object(restcall, 'call_req')
118 def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
119 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
120 r2 = [0, json.JSONEncoder().encode(''), '200']
121 mock_call_req.side_effect = [r1, r2]
124 "vnfInstanceName": "vFW_01",
125 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
126 self.nf_inst_id = CreateVnf(create_data).do_biz()
127 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
128 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
130 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
131 self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
133 @mock.patch.object(restcall, 'call_req')
134 def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
135 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
136 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
137 mock_call_req.side_effect = [r1, r2]
140 "vnfInstanceName": "vFW_01",
141 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
142 self.nf_inst_id = CreateVnf(create_data).do_biz()
143 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
144 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
146 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
147 self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
149 @mock.patch.object(restcall, 'call_req')
150 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
151 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
152 nfvouser='root', nfvopassword='root123')
153 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
154 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
155 r3 = [1, json.JSONEncoder().encode(''), '200']
156 mock_call_req.side_effect = [r1, r2, r3]
159 "vnfInstanceName": "vFW_01",
160 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
161 self.nf_inst_id = CreateVnf(create_data).do_biz()
162 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
163 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
165 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
166 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
168 @mock.patch.object(restcall, 'call_req')
169 def test_instantiate_vnf_success(self, mock_call_req):
170 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
171 nfvouser='root', nfvopassword='root123')
172 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
173 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
174 r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
175 r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
176 mock_call_req.side_effect = [r1, r2, r3, r4]
179 "vnfInstanceName": "vFW_01",
180 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
181 self.nf_inst_id = CreateVnf(create_data).do_biz()
182 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
183 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
185 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
186 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
190 'local_storages': [],
193 'volumn_storages': [],
197 'local_storages': [],
198 'vdu_id': u'vdu_omm.001',
199 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
205 'support_scaling': False,
208 'storage_policy': '',
211 'availability_zone': '',
216 'inject_data_list': [],
220 'local_affinity_antiaffinity_rule': {},
221 'template_id': u'omm.001',
222 'manual_scale_select_vim': False},
223 'description': u'singleommvm'},
225 'volumn_storages': [],
229 'local_storages': [],
231 'image_file': u'sss',
237 'support_scaling': False,
240 'storage_policy': '',
243 'availability_zone': '',
248 'inject_data_list': [],
252 'local_affinity_antiaffinity_rule': {},
254 'manual_scale_select_vim': False},
255 'description': u'ompvm'},
257 'volumn_storages': [],
261 'local_storages': [],
263 'image_file': u'sss',
269 'support_scaling': False,
272 'storage_policy': '',
275 'availability_zone': '',
280 'inject_data_list': [],
284 'local_affinity_antiaffinity_rule': {},
286 'manual_scale_select_vim': False},
287 'description': u'ompvm'},
289 'volumn_storages': [],
293 'local_storages': [],
295 'image_file': u'sss',
301 'support_scaling': False,
304 'storage_policy': '',
307 'availability_zone': '',
312 'inject_data_list': [],
316 'local_affinity_antiaffinity_rule': {},
318 'manual_scale_select_vim': False},
319 'description': u'ompvm'},
321 'volumn_storages': [],
325 'local_storages': [],
327 'image_file': u'sss',
333 'support_scaling': False,
336 'storage_policy': '',
339 'availability_zone': '',
344 'inject_data_list': [],
348 'local_affinity_antiaffinity_rule': {},
349 'template_id': u'10',
350 'manual_scale_select_vim': False},
351 'description': u'ppvm'},
353 'volumn_storages': [],
357 'local_storages': [],
359 'image_file': u'sss',
365 'support_scaling': False,
368 'storage_policy': '',
371 'availability_zone': '',
376 'inject_data_list': [],
380 'local_affinity_antiaffinity_rule': {},
381 'template_id': u'11',
382 'manual_scale_select_vim': False},
383 'description': u'ppvm'},
385 'volumn_storages': [],
389 'local_storages': [],
391 'image_file': u'sss',
397 'support_scaling': False,
400 'storage_policy': '',
403 'availability_zone': '',
408 'inject_data_list': [],
412 'local_affinity_antiaffinity_rule': {},
413 'template_id': u'12',
414 'manual_scale_select_vim': False},
415 'description': u'ppvm'}],
416 'volumn_storages': [],
420 'policy_id': u'policy_scale_sss-vnf-template',
422 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
428 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
430 'disk_format': u'VMDK',
431 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
432 'container_type': 'vm',
434 'hypervisor_type': 'kvm'},
435 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
441 'disk_format': u'VMDK',
442 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
443 'container_type': 'vm',
445 'hypervisor_type': 'kvm'},
446 'image_file_id': u'sss'}],
453 'domain_type': u'CN',
454 'version': u'v4.14.10',
455 'vmnumber_overquota_alarm': False,
458 'vnfd_version': u'V00000001',
459 'id': u'sss-vnf-template',
460 'name': u'sss-vnf-template'},
461 "flavourId": "flavour_1",
462 "instantiationLevelId": "instantiationLevel_1",
472 "protocolType": "http"
475 "tenant": "tenant_vCPE",
477 "password": "vCPE_321"
479 "interfaceEndpoint": "http://10.43.21.105:80/"
481 "resourceId": "1246",
487 "addressType": "MAC",
488 "l2AddressData": "00:f3:43:20:a2:a3"
493 "iPAddressType": "IPv4",
494 "iPAddress": "192.168.104.2"
498 "numDynamicAddresses": 0
503 "localizationLanguage": "en_US",
504 "additionalParams": {}