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_instid_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_success(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 mock_call_req.side_effect = [r1, r2]
157 "vnfInstanceName": "vFW_01",
158 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
159 self.nf_inst_id = CreateVnf(create_data).do_biz()
160 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
161 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
163 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
164 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
168 'local_storages': [],
171 'volumn_storages': [],
175 'local_storages': [],
176 'vdu_id': u'vdu_omm.001',
177 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
183 'support_scaling': False,
186 'storage_policy': '',
189 'availability_zone': '',
194 'inject_data_list': [],
198 'local_affinity_antiaffinity_rule': {},
199 'template_id': u'omm.001',
200 'manual_scale_select_vim': False},
201 'description': u'singleommvm'},
203 'volumn_storages': [],
207 'local_storages': [],
209 'image_file': u'sss',
215 'support_scaling': False,
218 'storage_policy': '',
221 'availability_zone': '',
226 'inject_data_list': [],
230 'local_affinity_antiaffinity_rule': {},
232 'manual_scale_select_vim': False},
233 'description': u'ompvm'},
235 'volumn_storages': [],
239 'local_storages': [],
241 'image_file': u'sss',
247 'support_scaling': False,
250 'storage_policy': '',
253 'availability_zone': '',
258 'inject_data_list': [],
262 'local_affinity_antiaffinity_rule': {},
264 'manual_scale_select_vim': False},
265 'description': u'ompvm'},
267 'volumn_storages': [],
271 'local_storages': [],
273 'image_file': u'sss',
279 'support_scaling': False,
282 'storage_policy': '',
285 'availability_zone': '',
290 'inject_data_list': [],
294 'local_affinity_antiaffinity_rule': {},
296 'manual_scale_select_vim': False},
297 'description': u'ompvm'},
299 'volumn_storages': [],
303 'local_storages': [],
305 'image_file': u'sss',
311 'support_scaling': False,
314 'storage_policy': '',
317 'availability_zone': '',
322 'inject_data_list': [],
326 'local_affinity_antiaffinity_rule': {},
327 'template_id': u'10',
328 'manual_scale_select_vim': False},
329 'description': u'ppvm'},
331 'volumn_storages': [],
335 'local_storages': [],
337 'image_file': u'sss',
343 'support_scaling': False,
346 'storage_policy': '',
349 'availability_zone': '',
354 'inject_data_list': [],
358 'local_affinity_antiaffinity_rule': {},
359 'template_id': u'11',
360 'manual_scale_select_vim': False},
361 'description': u'ppvm'},
363 'volumn_storages': [],
367 'local_storages': [],
369 'image_file': u'sss',
375 'support_scaling': False,
378 'storage_policy': '',
381 'availability_zone': '',
386 'inject_data_list': [],
390 'local_affinity_antiaffinity_rule': {},
391 'template_id': u'12',
392 'manual_scale_select_vim': False},
393 'description': u'ppvm'}],
394 'volumn_storages': [],
398 'policy_id': u'policy_scale_sss-vnf-template',
400 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
406 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
408 'disk_format': u'VMDK',
409 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
410 'container_type': 'vm',
412 'hypervisor_type': 'kvm'},
413 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
419 'disk_format': u'VMDK',
420 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
421 'container_type': 'vm',
423 'hypervisor_type': 'kvm'},
424 'image_file_id': u'sss'}],
431 'domain_type': u'CN',
432 'version': u'v4.14.10',
433 'vmnumber_overquota_alarm': False,
436 'vnfd_version': u'V00000001',
437 'id': u'sss-vnf-template',
438 'name': u'sss-vnf-template'},
439 "flavourId": "flavour_1",
440 "instantiationLevelId": "instantiationLevel_1",
450 "protocolType": "http"
453 "tenant": "tenant_vCPE",
455 "password": "vCPE_321"
457 "interfaceEndpoint": "http://10.43.21.105:80/"
459 "resourceId": "1246",
465 "addressType": "MAC",
466 "l2AddressData": "00:f3:43:20:a2:a3"
471 "iPAddressType": "IPv4",
472 "iPAddress": "192.168.104.2"
476 "numDynamicAddresses": 0
481 "localizationLanguage": "en_US",
482 "additionalParams": {}