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 def test_instantiate_vnf(self):
105 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
106 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
108 def test_instantiate_vnf_when_inst_id_not_exist(self):
109 self.nf_inst_id = str(uuid.uuid4())
110 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
111 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
113 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
114 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
116 @mock.patch.object(restcall, 'call_req')
117 def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
118 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
119 r2 = [0, json.JSONEncoder().encode(''), '200']
120 mock_call_req.side_effect = [r1, r2]
123 "vnfInstanceName": "vFW_01",
124 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
125 self.nf_inst_id = CreateVnf(create_data).do_biz()
126 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
127 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
129 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
130 self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
132 @mock.patch.object(restcall, 'call_req')
133 def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
134 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
135 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
136 mock_call_req.side_effect = [r1, r2]
139 "vnfInstanceName": "vFW_01",
140 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
141 self.nf_inst_id = CreateVnf(create_data).do_biz()
142 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
143 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
145 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
146 self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
148 @mock.patch.object(restcall, 'call_req')
149 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
150 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
151 nfvouser='root', nfvopassword='root123')
152 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
153 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
154 r3 = [1, json.JSONEncoder().encode(''), '200']
155 mock_call_req.side_effect = [r1, r2, r3]
158 "vnfInstanceName": "vFW_01",
159 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
160 self.nf_inst_id = CreateVnf(create_data).do_biz()
161 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
162 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
164 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
165 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
167 @mock.patch.object(restcall, 'call_req')
168 def test_instantiate_vnf_success(self, mock_call_req):
169 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
170 nfvouser='root', nfvopassword='root123')
171 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
172 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
173 r3 = [0, json.JSONEncoder().encode(''), '200']
174 mock_call_req.side_effect = [r1, r2, r3]
177 "vnfInstanceName": "vFW_01",
178 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
179 self.nf_inst_id = CreateVnf(create_data).do_biz()
180 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
181 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
183 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
184 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
188 'local_storages': [],
191 'volumn_storages': [],
195 'local_storages': [],
196 'vdu_id': u'vdu_omm.001',
197 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
203 'support_scaling': False,
206 'storage_policy': '',
209 'availability_zone': '',
214 'inject_data_list': [],
218 'local_affinity_antiaffinity_rule': {},
219 'template_id': u'omm.001',
220 'manual_scale_select_vim': False},
221 'description': u'singleommvm'},
223 'volumn_storages': [],
227 'local_storages': [],
229 'image_file': u'sss',
235 'support_scaling': False,
238 'storage_policy': '',
241 'availability_zone': '',
246 'inject_data_list': [],
250 'local_affinity_antiaffinity_rule': {},
252 'manual_scale_select_vim': False},
253 'description': u'ompvm'},
255 'volumn_storages': [],
259 'local_storages': [],
261 'image_file': u'sss',
267 'support_scaling': False,
270 'storage_policy': '',
273 'availability_zone': '',
278 'inject_data_list': [],
282 'local_affinity_antiaffinity_rule': {},
284 'manual_scale_select_vim': False},
285 'description': u'ompvm'},
287 'volumn_storages': [],
291 'local_storages': [],
293 'image_file': u'sss',
299 'support_scaling': False,
302 'storage_policy': '',
305 'availability_zone': '',
310 'inject_data_list': [],
314 'local_affinity_antiaffinity_rule': {},
316 'manual_scale_select_vim': False},
317 'description': u'ompvm'},
319 'volumn_storages': [],
323 'local_storages': [],
325 'image_file': u'sss',
331 'support_scaling': False,
334 'storage_policy': '',
337 'availability_zone': '',
342 'inject_data_list': [],
346 'local_affinity_antiaffinity_rule': {},
347 'template_id': u'10',
348 'manual_scale_select_vim': False},
349 'description': u'ppvm'},
351 'volumn_storages': [],
355 'local_storages': [],
357 'image_file': u'sss',
363 'support_scaling': False,
366 'storage_policy': '',
369 'availability_zone': '',
374 'inject_data_list': [],
378 'local_affinity_antiaffinity_rule': {},
379 'template_id': u'11',
380 'manual_scale_select_vim': False},
381 'description': u'ppvm'},
383 'volumn_storages': [],
387 'local_storages': [],
389 'image_file': u'sss',
395 'support_scaling': False,
398 'storage_policy': '',
401 'availability_zone': '',
406 'inject_data_list': [],
410 'local_affinity_antiaffinity_rule': {},
411 'template_id': u'12',
412 'manual_scale_select_vim': False},
413 'description': u'ppvm'}],
414 'volumn_storages': [],
418 'policy_id': u'policy_scale_sss-vnf-template',
420 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
426 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
428 'disk_format': u'VMDK',
429 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
430 'container_type': 'vm',
432 'hypervisor_type': 'kvm'},
433 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
439 'disk_format': u'VMDK',
440 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
441 'container_type': 'vm',
443 'hypervisor_type': 'kvm'},
444 'image_file_id': u'sss'}],
451 'domain_type': u'CN',
452 'version': u'v4.14.10',
453 'vmnumber_overquota_alarm': False,
456 'vnfd_version': u'V00000001',
457 'id': u'sss-vnf-template',
458 'name': u'sss-vnf-template'},
459 "flavourId": "flavour_1",
460 "instantiationLevelId": "instantiationLevel_1",
470 "protocolType": "http"
473 "tenant": "tenant_vCPE",
475 "password": "vCPE_321"
477 "interfaceEndpoint": "http://10.43.21.105:80/"
479 "resourceId": "1246",
485 "addressType": "MAC",
486 "l2AddressData": "00:f3:43:20:a2:a3"
491 "iPAddressType": "IPv4",
492 "iPAddress": "192.168.104.2"
496 "numDynamicAddresses": 0
501 "localizationLanguage": "en_US",
502 "additionalParams": {}