1 # Copyright 2018 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 rest_framework.test import APIClient
19 from rest_framework import status
20 from lcm.ns_vnfs.tests.const import GRANT_DATA, VNF_LCM_OP_OCC_NOTIFICATION_DATA, \
21 VNF_IDENTIFIER_CREATION_NOTIFICATION_DATA, VNF_IDENTIFIER_DELETION_NOTIFICATION_DATA
22 from lcm.pub.database.models import NfInstModel
23 from lcm.pub.utils import restcall
26 class VnfGrantViewTest(unittest.TestCase):
28 self.client = APIClient()
33 @mock.patch.object(restcall, 'call_req')
34 def test_grant_vnf_normal(self, mock_call_req):
39 mock_call_req.return_value = [0, json.JSONEncoder().encode(vim_connections), '200']
40 response = self.client.post("/api/nslcm/v2/grants", data=GRANT_DATA, format='json')
41 self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
42 resp_data = json.loads(response.content)
44 "id": resp_data.get("id"),
54 self.assertEqual(expect_resp_data, resp_data)
56 def test_grant_vnf_when_vnfinst_not_exist(self):
57 response = self.client.post("/api/nslcm/v2/grants", data=GRANT_DATA, format='json')
58 self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
60 @mock.patch.object(restcall, 'call_req')
61 def test_grant_vnf(self, mock_call_req):
67 "operation": "INSTANTIATE",
68 "isAutomaticInvocation": True,
69 "instantiationLevelId": "5",
75 "resourceTemplateId": "3",
79 "additionalParams": {"vnfmid": "3"},
90 "volume_storages": [],
96 "local_storages": "2",
99 "num_virtual_cpu": "111"
102 "virtual_mem_size": "3 B"
108 "local_storages": [{"local_storage_id": "1"}],
117 "name": "underlayervpn",
118 "csarVersion": "1.0",
120 "csarProvider": "huawei",
123 "id": "ns_underlayervpn_1_0"
129 "csarId": "vOpenNAT",
131 "csarName": "vOpenNAT.csar",
132 "vnfdModel": json.dumps(vnfdModel),
133 "vnfdProvider": "Intel",
134 "vnfdId": "openNAT_1.0",
135 "downloadUrl": "http://10.96.33.39:8806/static/catalog/vOpenNAT/vOpenNAT.csar",
136 "vnfVersion": "v1.0",
137 "vnfdVersion": "v1.0",
138 "vnfPackageId": "vOpenNAT"
145 NfInstModel.objects.create(nfinstid='1',
148 get_vnfpackage = [0, json.JSONEncoder().encode(vnfpackage_info), '200']
149 get_vimConnections = [0, json.JSONEncoder().encode(vimConnections), '200']
150 mock_call_req.side_effect = [get_vnfpackage, get_vimConnections]
151 response = self.client.post("/api/nslcm/v2/grants", data=data, format='json')
152 self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
153 resp_data = json.loads(response.content)
155 "id": resp_data.get("id"),
156 "vnfInstanceId": "1",
157 "vnfLcmOpOccId": "2",
165 self.assertEqual(expect_resp_data, resp_data)
167 def test_get_notify_vnf_normal(self):
168 response = self.client.get("/api/nslcm/v2/ns/1/ns_vnfs/1/Notify")
169 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
171 def test_notify_vnflcmopooc_normal(self):
172 NfInstModel.objects.create(nfinstid='22',
175 response = self.client.post("/api/nslcm/v2/ns/1/ns_vnfs/2/Notify",
176 data=VNF_LCM_OP_OCC_NOTIFICATION_DATA,
178 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
180 def test_notify_vnf_identifier_creation_normal(self):
181 response = self.client.post("/api/nslcm/v2/ns/1/ns_vnfs/2/Notify",
182 data=VNF_IDENTIFIER_CREATION_NOTIFICATION_DATA,
184 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)
186 def test_notify_vnf_identifier_deletion_normal(self):
187 NfInstModel.objects.create(nfinstid='22',
190 response = self.client.post("/api/nslcm/v2/ns/1/ns_vnfs/2/Notify",
191 data=VNF_IDENTIFIER_DELETION_NOTIFICATION_DATA,
193 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code)