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 def test_swagger_ok(self):
92 response = self.client.get("/gvnfmapi/lcm/v1/swagger.json", format='json')
93 self.assertEqual(response.status_code, status.HTTP_200_OK)
95 @mock.patch.object(restcall, 'call_req')
96 def test_create_vnf_identifier(self, mock_call_req):
97 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
98 mock_call_req.side_effect = [r1]
101 "vnfInstanceName": "vFW_01",
102 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
103 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
104 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
105 context = json.loads(response.content)
106 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
108 @mock.patch.object(InstVnf, 'run')
109 def test_instantiate_vnf(self, mock_run):
110 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
111 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
113 def test_instantiate_vnf_when_inst_id_not_exist(self):
114 self.nf_inst_id = str(uuid.uuid4())
115 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
116 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
118 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
119 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
121 @mock.patch.object(restcall, 'call_req')
122 def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
123 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
124 r2 = [0, json.JSONEncoder().encode(''), '200']
125 mock_call_req.side_effect = [r1, r2]
128 "vnfInstanceName": "vFW_01",
129 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
130 self.nf_inst_id = CreateVnf(create_data).do_biz()
131 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
132 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
134 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
135 self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
137 @mock.patch.object(restcall, 'call_req')
138 def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
139 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
140 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
141 mock_call_req.side_effect = [r1, r2]
144 "vnfInstanceName": "vFW_01",
145 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
146 self.nf_inst_id = CreateVnf(create_data).do_biz()
147 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
148 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
150 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
151 self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
153 @mock.patch.object(restcall, 'call_req')
154 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
155 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
156 nfvouser='root', nfvopassword='root123')
157 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
158 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
159 r3 = [1, json.JSONEncoder().encode(''), '200']
160 mock_call_req.side_effect = [r1, r2, r3]
163 "vnfInstanceName": "vFW_01",
164 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
165 self.nf_inst_id = CreateVnf(create_data).do_biz()
166 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
167 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
169 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
170 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
172 @mock.patch.object(restcall, 'call_req')
173 def test_instantiate_vnf_success(self, mock_call_req):
174 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
175 nfvouser='root', nfvopassword='root123')
176 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
177 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
178 r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
179 r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
180 mock_call_req.side_effect = [r1, r2, r3, r4]
183 "vnfInstanceName": "vFW_01",
184 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
185 self.nf_inst_id = CreateVnf(create_data).do_biz()
186 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
187 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
189 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
190 self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
194 'local_storages': [],
197 'volumn_storages': [],
201 'local_storages': [],
202 'vdu_id': u'vdu_omm.001',
203 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
209 'support_scaling': False,
212 'storage_policy': '',
215 'availability_zone': '',
220 'inject_data_list': [],
224 'local_affinity_antiaffinity_rule': {},
225 'template_id': u'omm.001',
226 'manual_scale_select_vim': False},
227 'description': u'singleommvm'},
229 'volumn_storages': [],
233 'local_storages': [],
235 'image_file': u'sss',
241 'support_scaling': False,
244 'storage_policy': '',
247 'availability_zone': '',
252 'inject_data_list': [],
256 'local_affinity_antiaffinity_rule': {},
258 'manual_scale_select_vim': False},
259 'description': u'ompvm'},
261 'volumn_storages': [],
265 'local_storages': [],
267 'image_file': u'sss',
273 'support_scaling': False,
276 'storage_policy': '',
279 'availability_zone': '',
284 'inject_data_list': [],
288 'local_affinity_antiaffinity_rule': {},
290 'manual_scale_select_vim': False},
291 'description': u'ompvm'},
293 'volumn_storages': [],
297 'local_storages': [],
299 'image_file': u'sss',
305 'support_scaling': False,
308 'storage_policy': '',
311 'availability_zone': '',
316 'inject_data_list': [],
320 'local_affinity_antiaffinity_rule': {},
322 'manual_scale_select_vim': False},
323 'description': u'ompvm'},
325 'volumn_storages': [],
329 'local_storages': [],
331 'image_file': u'sss',
337 'support_scaling': False,
340 'storage_policy': '',
343 'availability_zone': '',
348 'inject_data_list': [],
352 'local_affinity_antiaffinity_rule': {},
353 'template_id': u'10',
354 'manual_scale_select_vim': False},
355 'description': u'ppvm'},
357 'volumn_storages': [],
361 'local_storages': [],
363 'image_file': u'sss',
369 'support_scaling': False,
372 'storage_policy': '',
375 'availability_zone': '',
380 'inject_data_list': [],
384 'local_affinity_antiaffinity_rule': {},
385 'template_id': u'11',
386 'manual_scale_select_vim': False},
387 'description': u'ppvm'},
389 'volumn_storages': [],
393 'local_storages': [],
395 'image_file': u'sss',
401 'support_scaling': False,
404 'storage_policy': '',
407 'availability_zone': '',
412 'inject_data_list': [],
416 'local_affinity_antiaffinity_rule': {},
417 'template_id': u'12',
418 'manual_scale_select_vim': False},
419 'description': u'ppvm'}],
420 'volumn_storages': [],
424 'policy_id': u'policy_scale_sss-vnf-template',
426 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
432 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
434 'disk_format': u'VMDK',
435 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
436 'container_type': 'vm',
438 'hypervisor_type': 'kvm'},
439 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
445 'disk_format': u'VMDK',
446 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
447 'container_type': 'vm',
449 'hypervisor_type': 'kvm'},
450 'image_file_id': u'sss'}],
457 'domain_type': u'CN',
458 'version': u'v4.14.10',
459 'vmnumber_overquota_alarm': False,
462 'vnfd_version': u'V00000001',
463 'id': u'sss-vnf-template',
464 'name': u'sss-vnf-template'},
465 "flavourId": "flavour_1",
466 "instantiationLevelId": "instantiationLevel_1",
476 "protocolType": "http"
479 "tenant": "tenant_vCPE",
481 "password": "vCPE_321"
483 "interfaceEndpoint": "http://10.43.21.105:80/"
485 "resourceId": "1246",
491 "addressType": "MAC",
492 "l2AddressData": "00:f3:43:20:a2:a3"
497 "iPAddressType": "IPv4",
498 "iPAddress": "192.168.104.2"
502 "numDynamicAddresses": 0
507 "localizationLanguage": "en_US",
508 "additionalParams": {}