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.
18 from django.test import TestCase, Client
19 from rest_framework import status
21 from lcm.nf.vnfs.vnf_create.create_vnf_identifier import CreateVnf
22 from lcm.nf.vnfs.vnf_create.inst_vnf import InstVnf
23 from lcm.pub.database.models import NfInstModel, JobStatusModel, NfvoRegInfoModel
24 from lcm.pub.utils import restcall
25 from lcm.pub.utils.jobutil import JobUtil
28 class TestNsInstantiate(TestCase):
30 self.client = Client()
31 # VmInstModel.objects.create(vmid="1", vimid="1", resouceid="11", insttype=0, instid="1", vmname="test_01",
33 # VmInstModel.objects.create(vmid="2", vimid="2", resouceid="22", insttype=0, instid="2", vmname="test_02",
35 # NetworkInstModel.objects.create(networkid='1', vimid='1', resouceid='1', name='pnet_network',
36 # tenant='admin', insttype=0, instid='1')
37 # SubNetworkInstModel.objects.create(subnetworkid='1', vimid='1', resouceid='1', networkid='1',
38 # name='sub_pnet',tenant='admin', insttype=0, instid='1')
39 # PortInstModel.objects.create(portid='1', networkid='1', subnetworkid='1', vimid='1', resouceid='1',
40 # name='aaa_pnet_cp', tenant='admin', insttype=0, instid='1')
44 # VmInstModel.objects.all().delete()
45 # NetworkInstModel.objects.all().delete()
46 # SubNetworkInstModel.objects.all().delete()
47 # PortInstModel.objects.all().delete()
49 def assert_job_result(self, job_id, job_progress, job_detail):
50 jobs = JobStatusModel.objects.filter(
52 progress=job_progress,
54 self.assertEqual(1, len(jobs))
56 def test_swagger_ok(self):
57 response = self.client.get("/gvnfmapi/lcm/v1/swagger.json", format='json')
58 self.assertEqual(response.status_code, status.HTTP_200_OK)
60 @mock.patch.object(restcall, 'call_req')
61 def test_create_vnf_identifier(self, mock_call_req):
62 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
63 mock_call_req.side_effect = [r1]
66 "vnfInstanceName": "vFW_01",
67 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
68 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances", data=data, format='json')
69 self.failUnlessEqual(status.HTTP_201_CREATED, response.status_code)
70 context = json.loads(response.content)
71 self.assertTrue(NfInstModel.objects.filter(nfinstid=context['vnfInstanceId']).exists())
73 @mock.patch.object(InstVnf, 'run')
74 def test_instantiate_vnf(self, mock_run):
75 mock_run.re.return_value = None
76 response = self.client.post("/gvnfmapi/lcm/v1/vnf_instances/12/instantiate", data={}, format='json')
77 self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
79 def test_instantiate_vnf_when_inst_id_not_exist(self):
80 self.nf_inst_id = str(uuid.uuid4())
81 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
82 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
84 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
85 self.assert_job_result(self.job_id, 255, "VNF nf_inst_id is not exist.")
87 @mock.patch.object(restcall, 'call_req')
88 def test_instantiate_vnf_when_input_para_not_define_in_vnfd(self, mock_call_req):
89 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
90 r2 = [0, json.JSONEncoder().encode(''), '200']
91 mock_call_req.side_effect = [r1, r2]
94 "vnfInstanceName": "vFW_01",
95 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
96 self.nf_inst_id = CreateVnf(create_data).do_biz()
97 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
98 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
100 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
101 self.assert_job_result(self.job_id, 255, "Input parameter is not defined in vnfd_info.")
103 @mock.patch.object(restcall, 'call_req')
104 def test_instantiate_vnf_when_get_nfvo_config_failed(self, mock_call_req):
105 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
106 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
107 mock_call_req.side_effect = [r1, r2]
110 "vnfInstanceName": "vFW_01",
111 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
112 self.nf_inst_id = CreateVnf(create_data).do_biz()
113 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
114 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
116 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
117 self.assert_job_result(self.job_id, 255, "Nfvo was not registered")
119 @mock.patch.object(restcall, 'call_req')
120 def test_instantiate_vnf_when_applay_grant_failed(self, mock_call_req):
121 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
122 nfvouser='root', nfvopassword='root123')
123 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
124 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
125 r3 = [1, json.JSONEncoder().encode(''), '200']
126 mock_call_req.side_effect = [r1, r2, r3]
129 "vnfInstanceName": "vFW_01",
130 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
131 self.nf_inst_id = CreateVnf(create_data).do_biz()
132 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
133 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
135 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
136 self.assert_job_result(self.job_id, 255, "Nf instancing apply grant exception")
138 @mock.patch.object(restcall, 'call_req')
139 # @mock.patch.object(adaptor, 'create_vim_res')
140 def test_instantiate_vnf_when_create_res_failed(self, mock_call_req):
141 NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
142 nfvouser='root', nfvopassword='root123')
143 r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
144 r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
145 r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
146 # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
147 mock_call_req.side_effect = [r1, r2, r3]
148 # mock_create_vim_res.re.return_value = None
151 "vnfInstanceName": "vFW_01",
152 "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
153 self.nf_inst_id = CreateVnf(create_data).do_biz()
154 self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
155 JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
157 InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
158 self.assert_job_result(self.job_id, 255, "Create resource failed")
160 # @mock.patch.object(restcall, 'call_req')
161 # # @mock.patch.object(adaptor, 'create_vim_res')
162 # def test_instantiate_vnf_success(self, mock_call_req):
163 # NfvoRegInfoModel.objects.create(nfvoid='nfvo111', vnfminstid='vnfm111', apiurl='http://10.74.44.11',
164 # nfvouser='root', nfvopassword='root123')
165 # r1 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
166 # r2 = [0, json.JSONEncoder().encode(vnfd_model_dict), '200']
167 # r3 = [0, json.JSONEncoder().encode('Nf instancing apply grant'), '200']
168 # # r4 = [0, json.JSONEncoder().encode('Nf instancing apply resource'), '200']
169 # mock_call_req.side_effect = [r1, r2, r3]
170 # # mock_create_vim_res.re.return_value = None
173 # "vnfInstanceName": "vFW_01",
174 # "vnfInstanceDescription": " vFW in Nanjing TIC Edge"}
175 # self.nf_inst_id = CreateVnf(create_data).do_biz()
176 # self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
177 # JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
178 # data = inst_req_data
179 # InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
180 # self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
183 "flavourId": "flavour_1",
184 "instantiationLevelId": "instantiationLevel_1",
194 "protocolType": "http"
197 "tenant": "tenant_vCPE",
199 "password": "vCPE_321"
201 "interfaceEndpoint": "http://10.43.21.105:80/"
203 "resourceId": "1246",
209 "addressType": "MAC",
210 "l2AddressData": "00:f3:43:20:a2:a3"
215 "iPAddressType": "IPv4",
216 "iPAddress": "192.168.104.2"
220 "numDynamicAddresses": 0
225 "localizationLanguage": "en_US",
226 "additionalParams": {}
230 'local_storages': [],
233 'volumn_storages': [],
237 'local_storages': [],
238 'vdu_id': u'vdu_omm.001',
239 'image_file': u'opencos_sss_omm_img_release_20150723-1-disk1',
245 'support_scaling': False,
248 'storage_policy': '',
251 'availability_zone': '',
256 'inject_data_list': [],
260 'local_affinity_antiaffinity_rule': {},
261 'template_id': u'omm.001',
262 'manual_scale_select_vim': False},
263 'description': u'singleommvm'},
265 'volumn_storages': [],
269 'local_storages': [],
271 'image_file': u'sss',
277 'support_scaling': False,
280 'storage_policy': '',
283 'availability_zone': '',
288 'inject_data_list': [],
292 'local_affinity_antiaffinity_rule': {},
294 'manual_scale_select_vim': False},
295 'description': u'ompvm'},
297 'volumn_storages': [],
301 'local_storages': [],
303 'image_file': u'sss',
309 'support_scaling': False,
312 'storage_policy': '',
315 'availability_zone': '',
320 'inject_data_list': [],
324 'local_affinity_antiaffinity_rule': {},
326 'manual_scale_select_vim': False},
327 'description': u'ompvm'},
329 'volumn_storages': [],
333 'local_storages': [],
335 'image_file': u'sss',
341 'support_scaling': False,
344 'storage_policy': '',
347 'availability_zone': '',
352 'inject_data_list': [],
356 'local_affinity_antiaffinity_rule': {},
358 'manual_scale_select_vim': False},
359 'description': u'ompvm'},
361 'volumn_storages': [],
365 'local_storages': [],
367 'image_file': u'sss',
373 'support_scaling': False,
376 'storage_policy': '',
379 'availability_zone': '',
384 'inject_data_list': [],
388 'local_affinity_antiaffinity_rule': {},
389 'template_id': u'10',
390 'manual_scale_select_vim': False},
391 'description': u'ppvm'},
393 'volumn_storages': [],
397 'local_storages': [],
399 'image_file': u'sss',
405 'support_scaling': False,
408 'storage_policy': '',
411 'availability_zone': '',
416 'inject_data_list': [],
420 'local_affinity_antiaffinity_rule': {},
421 'template_id': u'11',
422 'manual_scale_select_vim': False},
423 'description': u'ppvm'},
425 'volumn_storages': [],
429 'local_storages': [],
431 'image_file': u'sss',
437 'support_scaling': False,
440 'storage_policy': '',
443 'availability_zone': '',
448 'inject_data_list': [],
452 'local_affinity_antiaffinity_rule': {},
453 'template_id': u'12',
454 'manual_scale_select_vim': False},
455 'description': u'ppvm'}],
456 'volumn_storages': [],
460 'policy_id': u'policy_scale_sss-vnf-template',
462 'policy_file': '*-vnfd.zip/*-vnf-policy.xml'},
468 'name': u'opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
470 'disk_format': u'VMDK',
471 'file_url': u'./zte-cn-sss-main-image/OMM/opencos_sss_omm_img_release_20150723-1-disk1.vmdk',
472 'container_type': 'vm',
474 'hypervisor_type': 'kvm'},
475 'image_file_id': u'opencos_sss_omm_img_release_20150723-1-disk1'},
481 'disk_format': u'VMDK',
482 'file_url': u'./zte-cn-sss-main-image/NE/sss.vmdk',
483 'container_type': 'vm',
485 'hypervisor_type': 'kvm'},
486 'image_file_id': u'sss'}],
493 'domain_type': u'CN',
494 'version': u'v4.14.10',
495 'vmnumber_overquota_alarm': False,
498 'vnfd_version': u'V00000001',
499 'id': u'sss-vnf-template',
500 'name': u'sss-vnf-template'},
501 "flavourId": "flavour_1",
502 "instantiationLevelId": "instantiationLevel_1",
512 "protocolType": "http"
515 "tenant": "tenant_vCPE",
517 "password": "vCPE_321"
519 "interfaceEndpoint": "http://10.43.21.105:80/"
521 "resourceId": "1246",
527 "addressType": "MAC",
528 "l2AddressData": "00:f3:43:20:a2:a3"
533 "iPAddressType": "IPv4",
534 "iPAddress": "192.168.104.2"
538 "numDynamicAddresses": 0
543 "localizationLanguage": "en_US",
544 "additionalParams": {}