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
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 VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
81 VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2", vmname="test_02",
87 def assert_job_result(self, job_id, job_progress, job_detail):
88 jobs = JobStatusModel.objects.filter(
90 progress=job_progress,
92 self.assertEqual(1, len(jobs))
94 def test_swagger_ok(self):
95 response = self.client.get("/gvnfmapi/lcm/v1/swagger.json", format='json')
96 self.assertEqual(response.status_code, status.HTTP_200_OK)
98 @mock.patch.object(restcall, 'call_req')
99 def test_create_vnf_identifier(self, mock_call_req):
100 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
101 mock_call_req.side_effect = [r1]
104 "vnfInstanceName": "vFW_01",
105 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
106 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
107 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
108 context = json.loads(response.content)
109 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
111 @mock.patch.object(InstVnf, 'run')
112 def test_instantiate_vnf(self, mock_run):
113 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
114 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
116 def test_instantiate_vnf_when_inst_id_not_exist(self):
117 self.nf_inst_id = str(uuid.uuid4())
118 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
119 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
121 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
122 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
124 @mock.patch.object(restcall, 'call_req')
125 def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
126 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
127 r2 = [0, json.JSONEncoder().encode(''), '200']
128 mock_call_req.side_effect = [r1, r2]
131 "vnfInstanceName": "vFW_01",
132 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
133 self.nf_inst_id = CreateVnf(create_data).do_biz()
134 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
135 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
137 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
138 self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
140 @mock.patch.object(restcall, 'call_req')
141 def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
142 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
143 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
144 mock_call_req.side_effect = [r1, r2]
147 "vnfInstanceName": "vFW_01",
148 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
149 self.nf_inst_id = CreateVnf(create_data).do_biz()
150 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
151 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
153 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
154 self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
156 @mock.patch.object(restcall, 'call_req')
157 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
158 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
159 nfvouser='root', nfvopassword='root123')
160 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
161 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
162 r3 = [1, json.JSONEncoder().encode(''), '200']
163 mock_call_req.side_effect = [r1, r2, r3]
166 "vnfInstanceName": "vFW_01",
167 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
168 self.nf_inst_id = CreateVnf(create_data).do_biz()
169 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
170 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
172 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
173 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
175 @mock.patch.object(restcall, 'call_req')
176 def test_instantiate_vnf_success(self, mock_call_req):
177 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
178 nfvouser='root', nfvopassword='root123')
179 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
180 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
181 r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
182 r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
183 mock_call_req.side_effect = [r1, r2, r3, r4]
186 "vnfInstanceName": "vFW_01",
187 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
188 self.nf_inst_id = CreateVnf(create_data).do_biz()
189 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
190 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
192 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
193 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
197 'local_storages': [],
200 'volumn_storages': [],
204 'local_storages': [],
205 'vdu_id': u'vdu_omm.001',
206 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
212 'support_scaling': False,
215 'storage_policy': '',
218 'availability_zone': '',
223 'inject_data_list': [],
227 'local_affinity_antiaffinity_rule': {},
228 'template_id': u'omm.001',
229 'manual_scale_select_vim': False},
230 'description': u'singleommvm'},
232 'volumn_storages': [],
236 'local_storages': [],
238 'image_file': u'sss',
244 'support_scaling': False,
247 'storage_policy': '',
250 'availability_zone': '',
255 'inject_data_list': [],
259 'local_affinity_antiaffinity_rule': {},
261 'manual_scale_select_vim': False},
262 'description': u'ompvm'},
264 'volumn_storages': [],
268 'local_storages': [],
270 'image_file': u'sss',
276 'support_scaling': False,
279 'storage_policy': '',
282 'availability_zone': '',
287 'inject_data_list': [],
291 'local_affinity_antiaffinity_rule': {},
293 'manual_scale_select_vim': False},
294 'description': u'ompvm'},
296 'volumn_storages': [],
300 'local_storages': [],
302 'image_file': u'sss',
308 'support_scaling': False,
311 'storage_policy': '',
314 'availability_zone': '',
319 'inject_data_list': [],
323 'local_affinity_antiaffinity_rule': {},
325 'manual_scale_select_vim': False},
326 'description': u'ompvm'},
328 'volumn_storages': [],
332 'local_storages': [],
334 'image_file': u'sss',
340 'support_scaling': False,
343 'storage_policy': '',
346 'availability_zone': '',
351 'inject_data_list': [],
355 'local_affinity_antiaffinity_rule': {},
356 'template_id': u'10',
357 'manual_scale_select_vim': False},
358 'description': u'ppvm'},
360 'volumn_storages': [],
364 'local_storages': [],
366 'image_file': u'sss',
372 'support_scaling': False,
375 'storage_policy': '',
378 'availability_zone': '',
383 'inject_data_list': [],
387 'local_affinity_antiaffinity_rule': {},
388 'template_id': u'11',
389 'manual_scale_select_vim': False},
390 'description': u'ppvm'},
392 'volumn_storages': [],
396 'local_storages': [],
398 'image_file': u'sss',
404 'support_scaling': False,
407 'storage_policy': '',
410 'availability_zone': '',
415 'inject_data_list': [],
419 'local_affinity_antiaffinity_rule': {},
420 'template_id': u'12',
421 'manual_scale_select_vim': False},
422 'description': u'ppvm'}],
423 'volumn_storages': [],
427 'policy_id': u'policy_scale_sss-vnf-template',
429 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
435 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
437 'disk_format': u'VMDK',
438 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
439 'container_type': 'vm',
441 'hypervisor_type': 'kvm'},
442 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
448 'disk_format': u'VMDK',
449 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
450 'container_type': 'vm',
452 'hypervisor_type': 'kvm'},
453 'image_file_id': u'sss'}],
460 'domain_type': u'CN',
461 'version': u'v4.14.10',
462 'vmnumber_overquota_alarm': False,
465 'vnfd_version': u'V00000001',
466 'id': u'sss-vnf-template',
467 'name': u'sss-vnf-template'},
468 "flavourId": "flavour_1",
469 "instantiationLevelId": "instantiationLevel_1",
479 "protocolType": "http"
482 "tenant": "tenant_vCPE",
484 "password": "vCPE_321"
486 "interfaceEndpoint": "http://10.43.21.105:80/"
488 "resourceId": "1246",
494 "addressType": "MAC",
495 "l2AddressData": "00:f3:43:20:a2:a3"
500 "iPAddressType": "IPv4",
501 "iPAddress": "192.168.104.2"
505 "numDynamicAddresses": 0
510 "localizationLanguage": "en_US",
511 "additionalParams": {}