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.
17 from django.test import Client
18 from django.test import TestCase
19 from rest_framework import status
20 from driver.pub.utils import restcall
23 class InterfacesTest(TestCase):
25 self.client = Client()
30 @mock.patch.object(restcall, 'call_req')
31 def test_instantiate_vnf(self, mock_call_req):
35 'name': 'ZTE_VNFM_237_62',
36 'vimId': '516cee95-e8ca-4d26-9268-38e343c2e31e',
37 'url': 'http: //192.168.237.165: 2324',
40 'vnfmId': 'b0797c9b-3da9-459c-b25c-3813e9d8fd70',
42 'type': 'gvnfmdriver',
43 'createTime': '2016-10-3111: 08: 39',
48 # "jobId": "NF-CREATE-8-b384535c-9f45-11e6-8749-fa163e91c2f9"
51 "jobId": "NF-INST-8-6ffa8083-6705-49b3-ae54-cbd6265fbe7a"
53 r1 = [0, json.JSONEncoder().encode(vnfm_info), "200"]
54 ret = [0, json.JSONEncoder().encode(create_vnf_resp), '200']
55 ret2 = [0, json.JSONEncoder().encode(job_info), '200']
56 mock_call_req.side_effect = [r1, ret, r1, ret2]
58 'vnfInstanceName': 'VFW_f88c0cb7-512a-44c4-bd09-891663f19367',
59 'vnfPackageId': 'd852e1be-0aac-48f1-b1a4-cd825f6cdf9a',
60 'vnfDescriptorId': 'vcpe_vfw_zte_1_0',
62 'sdncontroller': 'e4d637f1-a4ec-4c59-8b20-4e8ab34daba9',
63 'NatIpRange': '192.167.0.10-192.168.0.20',
64 'm6000_mng_ip': '192.168.11.11',
65 'externalPluginManageNetworkName': 'plugin_net_2014',
66 'location': '516cee95-e8ca-4d26-9268-38e343c2e31e',
67 'externalManageNetworkName': 'mng_net_2017',
68 'sfc_data_network': 'sfc_data_net_2016',
69 'externalDataNetworkName': 'Flow_out_net',
73 response = self.client.post("/api/gvnfmdriver/v1/1/vnfs",
74 data=json.dumps(req_data), content_type="application/json")
75 self.assertEqual(status.HTTP_201_CREATED, response.status_code)
78 "jobId": "NF-INST-8-6ffa8083-6705-49b3-ae54-cbd6265fbe7a"
80 self.assertEqual(expect_data, response.data)
82 @mock.patch.object(restcall, 'call_req')
83 def test_terminate_vnf(self, mock_call_req):
85 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
87 "type": "gvnfmdriver",
91 "description": "vnfm",
93 "url": "http://10.74.44.11",
96 "createTime": "2016-07-06 15:33:18"
98 job_info = {"vnfInstanceId": "1", "vnfLcOpId": "1"}
101 "responseDescriptor": {
104 "statusDescription": "",
107 "responseHistoryList": [
111 "statusDescription": "",
118 r1 = [0, json.JSONEncoder().encode(vnfm_info), "200"]
119 r2 = [0, json.JSONEncoder().encode(job_info), "200"]
120 job_ret = [0, json.JSONEncoder().encode(job_status_info), "200"]
121 r3 = [0, json.JSONEncoder().encode(None), "200"]
122 mock_call_req.side_effect = [r1, r2, r1, job_ret, r1, r3]
123 response = self.client.post("/api/gvnfmdriver/v1/ztevnfmid/vnfs/2/terminate")
124 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
125 self.assertEqual(None, response.data)
127 @mock.patch.object(restcall, 'call_req')
128 def test_query_vnf(self, mock_call_req):
130 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
132 "type": "gvnfmdriver",
136 "description": "vnfm",
137 "certificateUrl": "",
138 "url": "http://10.74.44.11",
141 "createTime": "2016-07-06 15:33:18"
146 "instantiationState": "INSTANTIATED",
147 "vnfSoftwareVersion": "v1.2.3"
150 r1 = [0, json.JSONEncoder().encode(vnfm_info), "200"]
151 r2 = [0, json.JSONEncoder().encode(job_info), "200"]
152 mock_call_req.side_effect = [r1, r2]
153 response = self.client.get("/api/gvnfmdriver/v1/19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee/vnfs/88")
154 self.assertEqual(status.HTTP_200_OK, response.status_code)
157 "vnfInstanceId": "88",
158 "vnfStatus": "ACTIVE",
162 self.assertEqual(expect_resp_data, response.data)
164 @mock.patch.object(restcall, 'call_req')
165 def test_operation_status(self, mock_call_req):
169 'name': 'ZTE_VNFM_237_62',
170 'vimId': '516cee95-e8ca-4d26-9268-38e343c2e31e',
171 'url': 'http: //192.168.237.165: 2324',
172 'certificateUrl': '',
174 'vnfmId': 'b0797c9b-3da9-459c-b25c-3813e9d8fd70',
176 'type': 'gvnfmdriver',
177 'createTime': '2016-10-3111: 08: 39',
181 "jobId": "NF-CREATE-11-ec6c2f2a-9f48-11e6-9405-fa163e91c2f9",
182 "responseDescriptor": {
185 "status": "PROCESSING",
186 "statusDescription": "OMC VMs are decommissioned in VIM",
188 "responseHistoryList": [
194 "statusdescription": "'JsonParser' object has no attribute 'parser_info'"
201 "vnfLcOpId": "NF-CREATE-11-ec6c2f2a-9f48-11e6-9405-fa163e91c2f9",
202 "responseDescriptor": {
205 "lcmOperationStatus": "PROCESSING",
206 "statusDescription": "OMC VMs are decommissioned in VIM",
208 "responseHistoryList": [
214 "statusdescription": "'JsonParser' object has no attribute 'parser_info'"
220 r1 = [0, json.JSONEncoder().encode(vnfm_info), '200']
221 r2 = [0, json.JSONEncoder().encode(resp_body), '200']
222 mock_call_req.side_effect = [r1, r2]
223 response = self.client.get("/api/gvnfmdriver/v1/%s/jobs/%s?responseId=0"
224 % (vnfm_info["vnfmId"], expected_body["jobId"]))
225 self.assertEqual(status.HTTP_200_OK, response.status_code)
226 self.assertDictEqual(expected_body, response.data)
228 @mock.patch.object(restcall, 'call_req')
229 def test_grantvnf(self, mock_call_req):
231 "vnfInstanceId": "1",
232 "vnfLcmOpOccId": "2",
235 "operation": "INSTANTIATE",
236 "isAutomaticInvocation": True,
237 "instantiationLevelId": "5",
243 "resourceTemplateId": "3",
244 "resourceTemplate": {
245 "vimConnectionId": "4",
246 "resourceProviderId": "5",
248 "vimLevelResourceType": "7"
252 "placementConstraints": [
254 "affinityOrAntiAffinity": "AFFINITY",
258 "idType": "RES_MGMT",
260 "vimConnectionId": "2",
261 "resourceProviderId": "3"
268 "sameResourceGroup": True,
271 "idType": "RES_MGMT",
273 "vimConnectionId": "2",
274 "resourceProviderId": "3"
279 "additionalParams": {},
291 "vnfInstanceId": "1",
292 "vnfLcmOpOccId": "2",
300 mock_call_req.return_value = [0, json.JSONEncoder().encode(grant_resp_data), '201']
301 response = self.client.put("/api/gvnfmdriver/v1/resource/grant",
302 data=json.dumps(data), content_type='application/json')
303 self.assertEqual(status.HTTP_201_CREATED, response.status_code)
306 "vnfInstanceId": "1",
307 "vnfLcmOpOccId": "2",
315 self.assertDictEqual(expect_resp_data, response.data)
317 @mock.patch.object(restcall, 'call_req')
318 def test_grantvnf_failed(self, mock_call_req):
320 "vnfInstanceId": "1",
321 "vnfLcmOpOccId": "2",
324 "operation": "INSTANTIATE",
325 "isAutomaticInvocation": True,
326 "instantiationLevelId": "5",
332 "resourceTemplateId": "3",
333 "resourceTemplate": {
334 "vimConnectionId": "4",
335 "resourceProviderId": "5",
337 "vimLevelResourceType": "7"
341 "placementConstraints": [
343 "affinityOrAntiAffinity": "AFFINITY",
347 "idType": "RES_MGMT",
349 "vimConnectionId": "2",
350 "resourceProviderId": "3"
357 "sameResourceGroup": True,
360 "idType": "RES_MGMT",
362 "vimConnectionId": "2",
363 "resourceProviderId": "3"
368 "additionalParams": {},
378 mock_call_req.return_value = [1, json.JSONEncoder().encode(""), '201']
379 response = self.client.put("/api/gvnfmdriver/v1/resource/grant",
380 data=json.dumps(data), content_type='application/json')
381 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
383 @mock.patch.object(restcall, 'call_req')
384 def test_notify(self, mock_call_req):
387 "vimInfoId": "111111",
392 "protocolType": "None"
397 "password": "password"
399 "interfaceEndpoint": "http://127.0.0.1/api/v1"
403 "resourceDefinitionId": "xxxxx",
407 "removeResource": "",
409 "computeResourceFlavour": {
420 "additionalParam": ""
422 r2 = [0, json.JSONEncoder().encode(vim_info), "200"]
423 mock_call_req.side_effect = [r2]
425 "vnfmInstId": "876543211",
426 "notificationType": "string",
427 "subscriptionId": "string",
428 "timeStamp": "1234567890",
429 "notificationStatus": "START",
430 "operationState": "STARTING",
431 "vnfInstanceId": "string",
432 "operation": "INSTANTIATE",
433 "isAutomaticInvocation": True,
434 "vnfLcmOpOccId": "string",
438 "changeType": "ADDED",
440 "vimConnectionId": "string",
441 "resourceProviderId": "string",
442 "resourceId": "string",
443 "vimLevelResourceType": "string"
446 "affectedVnfcCpIds": [],
447 "addedStorageResourceIds": [],
448 "removedStorageResourceIds": [],
450 "affectedVirtualLinks": [{
452 "virtualLinkDescId": "string",
453 "changeType": "ADDED",
455 "vimConnectionId": "string",
456 "resourceProviderId": "string",
457 "resourceId": "string",
458 "vimLevelResourceType": "network",
461 "affectedVirtualStorages": [{
463 "virtualStorageDescId": "string",
464 "changeType": "ADDED",
466 "vimConnectionId": "string",
467 "resourceProviderId": "string",
468 "resourceId": "string",
469 "vimLevelResourceType": "network",
474 "vnfInstanceName": "string",
475 "vnfInstanceDescription": "string",
476 "vnfConfigurableProperties": {},
479 "vimConnectionInfo": [{
487 "vnfPkgId": "string",
489 "vnfProvider": "string",
490 "vnfProductName": "string",
491 "vnfSoftwareVersion": "string",
492 "vnfdVersion": "string"
494 "changedExtConnectivity": [{
497 "vimConnectionId": "string",
498 "resourceProviderId": "string",
499 "resourceId": "string",
500 "vimLevelResourceType": "string"
505 "vimConnectionId": "string",
506 "resourceProviderId": "string",
507 "resourceId": "string",
508 "vimLevelResourceType": "string"
510 "cpInstanceId": "string"
514 response = self.client.post("/api/gvnfmdriver/v1/vnfs/lifecyclechangesnotification",
515 data=json.dumps(req_data),
516 content_type='application/json')
517 self.assertEqual(status.HTTP_200_OK, response.status_code)
518 expect_resp_data = None
519 self.assertEqual(expect_resp_data, response.data)
521 @mock.patch.object(restcall, 'call_req')
522 def test_get_vnfpkgs(self, mock_call_req):
529 mock_call_req.return_value = [0, json.JSONEncoder().encode(vnfpkgs_info), '200']
530 resp = self.client.get("/api/gvnfmdriver/v1/vnfpackages")
531 self.assertEqual(status.HTTP_200_OK, resp.status_code)
532 self.assertEqual(1, len(resp.data["csars"]))
533 self.assertEqual("1", resp.data["csars"][0]["csarId"])
534 self.assertEqual("2", resp.data["csars"][0]["vnfdId"])
536 @mock.patch.object(restcall, 'call_req')
537 def test_get_vnfpkgs_failed(self, mock_call_req):
538 mock_call_req.return_value = [1, json.JSONEncoder().encode(""), '200']
539 resp = self.client.get("/api/gvnfmdriver/v1/vnfpackages")
540 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, resp.status_code)
542 @mock.patch.object(restcall, 'call_req')
543 def test_get_vnflcmopocc_with_id(self, mock_call_req):
544 vnfLcmOpOccId = "99442b18-a5c7-11e8-998c-bf1755941f16"
546 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
548 "type": "gvnfmdriver",
552 "description": "vnfm",
553 "certificateUrl": "",
554 "url": "http://10.74.44.11",
557 "createTime": "2016-07-06 15:33:18"
559 dummy_single_vnf_lcm_op = {
561 "operationState": "STARTING",
562 "stateEnteredTime": "2018-07-09",
563 "startTime": "2018-07-09",
564 "vnfInstanceId": "cd552c9c-ab6f-11e8-b354-236c32aa91a1",
566 "operation": "SCALE",
567 "isAutomaticInvocation": False,
568 "operationParams": {},
569 "isCancelPending": False,
572 "resourceChanges": None,
574 "changedExtConnectivity": None,
579 "vnfInstance": "demo"
582 mock_call_req.return_value = [0, json.JSONEncoder().encode(dummy_single_vnf_lcm_op), status.HTTP_200_OK]
583 resp = self.client.get("/api/gvnfmdriver/v1/%s/vnf_lcm_op_occs/%s" % (vnfm_info['vnfmId'], vnfLcmOpOccId))
584 self.assertEqual(dummy_single_vnf_lcm_op, resp.data)
585 self.assertEqual(status.HTTP_200_OK, resp.status_code)
587 @mock.patch.object(restcall, 'call_req')
588 def test_get_vnflcmopocc_failed(self, mock_call_req):
589 vnfLcmOpOccId = "99442b18-a5c7-11e8-998c-bf1755941f16"
591 "vnfmId": "19ecbb3a-3242-4fa3-9926-8dfb7ddc29ee",
593 "type": "gvnfmdriver",
597 "description": "vnfm",
598 "certificateUrl": "",
599 "url": "http://10.74.44.11",
602 "createTime": "2016-07-06 15:33:18"
604 mock_call_req.return_value = [1, json.JSONEncoder().encode({}), status.HTTP_500_INTERNAL_SERVER_ERROR]
605 resp = self.client.get("/api/gvnfmdriver/v1/%s/vnf_lcm_op_occs/%s" % (vnfm_info['vnfmId'], vnfLcmOpOccId))
606 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, resp.status_code)