Merge "Add a test in Grand"
[vfc/nfvo/lcm.git] / lcm / v2 / tests.py
1 # Copyright 2018 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import unittest
16 import json
17 import mock
18 from rest_framework.test import APIClient
19 from rest_framework import status
20 from lcm.pub.database.models import NfInstModel
21 from lcm.pub.utils import restcall
22
23
24 class VnfGrantViewTest(unittest.TestCase):
25     def setUp(self):
26         self.client = APIClient()
27
28     def tearDown(self):
29         pass
30
31     @mock.patch.object(restcall, 'call_req')
32     def test_grant_vnf_normal(self, mock_call_req):
33         data = {
34             "vnfInstanceId": "1",
35             "vnfLcmOpOccId": "2",
36             "vnfdId": "3",
37             "flavourId": "4",
38             "operation": "INSTANTIATE",
39             "isAutomaticInvocation": True,
40             "instantiationLevelId": "5",
41             "addResources": [
42                 {
43                     "id": "1",
44                     "type": "COMPUTE",
45                     "vduId": "2",
46                     "resourceTemplateId": "3",
47                     "resourceTemplate": {
48                         "vimConnectionId": "4",
49                         "resourceProviderId": "5",
50                         "resourceId": "6",
51                         "vimLevelResourceType": "7"
52                     }
53                 }
54             ],
55             "placementConstraints": [
56                 {
57                     "affinityOrAntiAffinity": "AFFINITY",
58                     "scope": "NFVI_POP",
59                     "resource": [
60                         {
61                             "idType": "RES_MGMT",
62                             "resourceId": "1",
63                             "vimConnectionId": "2",
64                             "resourceProviderId": "3"
65                         }
66                     ]
67                 }
68             ],
69             "vimConstraints": [
70                 {
71                     "sameResourceGroup": True,
72                     "resource": [
73                         {
74                             "idType": "RES_MGMT",
75                             "resourceId": "1",
76                             "vimConnectionId": "2",
77                             "resourceProviderId": "3"
78                         }
79                     ]
80                 }
81             ],
82             "additionalParams": {},
83             "_links": {
84                 "vnfLcmOpOcc": {
85                     "href": "1"
86                 },
87                 "vnfInstance": {
88                     "href": "2"
89                 }
90             }
91         }
92         vimConnections = {
93             "id": "1",
94             "vimId": "1",
95         }
96         mock_call_req.return_value = [0, json.JSONEncoder().encode(vimConnections), '200']
97         response = self.client.post("/api/nslcm/v2/grants", data=data, format='json')
98         self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
99         resp_data = json.loads(response.content)
100         expect_resp_data = {
101             "id": resp_data.get("id"),
102             "vnfInstanceId": "1",
103             "vnfLcmOpOccId": "2",
104             "vimConnections": [
105                 {
106                     "id": "1",
107                     "vimId": "1"
108                 }
109             ]
110         }
111         self.assertEqual(expect_resp_data, resp_data)
112
113     def test_grant_vnf_when_vnfinst_not_exist(self):
114         data = {
115             "vnfInstanceId": "1",
116             "vnfLcmOpOccId": "2",
117             "vnfdId": "3",
118             "flavourId": "4",
119             "operation": "INSTANTIATE",
120             "isAutomaticInvocation": True,
121             "instantiationLevelId": "5",
122             "addResources": [
123                 {
124                     "id": "1",
125                     "type": "COMPUTE",
126                     "vduId": "2",
127                     "resourceTemplateId": "3",
128                 }
129             ],
130             "placementConstraints": [
131                 {
132                     "affinityOrAntiAffinity": "AFFINITY",
133                     "scope": "NFVI_POP",
134                     "resource": [
135                         {
136                             "idType": "RES_MGMT",
137                             "resourceId": "1",
138                             "vimConnectionId": "2",
139                             "resourceProviderId": "3"
140                         }
141                     ]
142                 }
143             ],
144             "vimConstraints": [
145                 {
146                     "sameResourceGroup": True,
147                     "resource": [
148                         {
149                             "idType": "RES_MGMT",
150                             "resourceId": "1",
151                             "vimConnectionId": "2",
152                             "resourceProviderId": "3"
153                         }
154                     ]
155                 }
156             ],
157             "additionalParams": {},
158             "_links": {
159                 "vnfLcmOpOcc": {
160                     "href": "1"
161                 },
162                 "vnfInstance": {
163                     "href": "2"
164                 }
165             }
166         }
167         response = self.client.post("/api/nslcm/v2/grants", data=data, format='json')
168         self.failUnlessEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code)
169
170     def test_get_notify_vnf_normal(self):
171         response = self.client.get("/api/nslcm/v2/ns/1/vnfs/1/Notify")
172         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
173
174     def test_notify_vnf_normal(self):
175         data = {
176             "id": "string",
177             "notificationType": "string",
178             "subscriptionId": "string",
179             "timeStamp": "string",
180             "notificationStatus": "START",
181             "operationState": "STARTING",
182             "vnfInstanceId": "string",
183             "operation": "INSTANTIATE",
184             "isAutomaticInvocation": True,
185             "vnfLcmOpOccId": "string",
186             "affectedVnfcs": [{
187                 "id": "string",
188                 "vduId": "string",
189                 "changeType": "ADDED",
190                 "computeResource": {
191                     "vimConnectionId": "string",
192                     "resourceProviderId": "string",
193                     "resourceId": "string",
194                     "vimLevelResourceType": "string"
195                 },
196                 "metadata": {},
197                 "affectedVnfcCpIds": [],
198                 "addedStorageResourceIds": [],
199                 "removedStorageResourceIds": [],
200             }],
201             "affectedVirtualLinks": [{
202                 "id": "string",
203                 "virtualLinkDescId": "string",
204                 "changeType": "ADDED",
205                 "networkResource": {
206                     "vimConnectionId": "string",
207                     "resourceProviderId": "string",
208                     "resourceId": "string",
209                     "vimLevelResourceType": "network",
210                 }
211             }],
212             "affectedVirtualStorages": [{
213                 "id": "string",
214                 "virtualStorageDescId": "string",
215                 "changeType": "ADDED",
216                 "storageResource": {
217                     "vimConnectionId": "string",
218                     "resourceProviderId": "string",
219                     "resourceId": "string",
220                     "vimLevelResourceType": "network",
221                 },
222                 "metadata": {}
223             }],
224             "changedInfo": {
225                 "vnfInstanceName": "string",
226                 "vnfInstanceDescription": "string",
227                 "vnfConfigurableProperties": {
228                     "additionalProp1": "string",
229                     "additionalProp2": "string",
230                     "additionalProp3": "string"
231                 },
232                 "metadata": {
233                     "additionalProp1": "string",
234                     "additionalProp2": "string",
235                     "additionalProp3": "string"
236                 },
237                 "extensions": {
238                     "additionalProp1": "string",
239                     "additionalProp2": "string",
240                     "additionalProp3": "string"
241                 },
242                 "vimConnectionInfo": [{
243                     "id": "string",
244                     "vimId": "string",
245                     "vimType": "string",
246                     "interfaceInfo": {
247                         "additionalProp1": "string",
248                         "additionalProp2": "string",
249                         "additionalProp3": "string"
250                     },
251                     "accessInfo": {
252                         "additionalProp1": "string",
253                         "additionalProp2": "string",
254                         "additionalProp3": "string"
255                     },
256                     "extra": {
257                         "additionalProp1": "string",
258                         "additionalProp2": "string",
259                         "additionalProp3": "string"
260                     }
261                 }],
262                 "vnfPkgId": "string",
263                 "vnfdId": "string",
264                 "vnfProvider": "string",
265                 "vnfProductName": "string",
266                 "vnfSoftwareVersion": "string",
267                 "vnfdVersion": "string"
268             },
269             "changedExtConnectivity": [{
270                 "id": "string",
271                 "resourceHandle": {
272                     "vimConnectionId": "string",
273                     "resourceProviderId": "string",
274                     "resourceId": "string",
275                     "vimLevelResourceType": "string"
276                 },
277                 "extLinkPorts": [{
278                     "id": "string",
279                     "resourceHandle": {
280                         "vimConnectionId": "string",
281                         "resourceProviderId": "string",
282                         "resourceId": "string",
283                         "vimLevelResourceType": "string"
284                     },
285                     "cpInstanceId": "string"
286                 }]
287             }],
288             "error": {
289                 "type": "string",
290                 "title": "string",
291                 "status": 0,
292                 "detail": "string",
293                 "instance": "string"
294             },
295             "_links": {
296                 "vnfInstance": {
297                     "href": "string"
298                 },
299                 "subscription": {
300                     "href": "string"
301                 },
302                 "vnfLcmOpOcc": {
303                     "href": "string"
304                 }
305             }
306         }
307         NfInstModel.objects.create(nfinstid='22',
308                                    mnfinstid='2',
309                                    vnfm_inst_id='1')
310         response = self.client.post("/api/nslcm/v2/ns/1/vnfs/2/Notify", data=data, format='json')
311         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)