vfclcm upgrade from python2 to python3
[vfc/gvnfm/vnflcm.git] / lcm / lcm / nf / tests / test_query_vnf.py
1 # Copyright 2017 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 from django.test import TestCase, Client
16 from rest_framework import status
17
18 from lcm.pub.database.models import (NfInstModel, VLInstModel,
19                                      VNFCInstModel, VmInstModel, StorageInstModel)
20
21
22 class ResourceTest(TestCase):
23     def setUp(self):
24         self.client = Client()
25         self.nf_inst_id = "01"
26         NfInstModel.objects.all().delete()
27         self.test_data_single_vnf = {
28             "id": "1",
29             "vnfInstanceName": "VNF1",
30             "vnfInstanceDescription": "VNF DESC",
31             "vnfdId": "1",
32             "vnfProvider": "ZTE",
33             "vnfProductName": "XGW",
34             "vnfSoftwareVersion": "V1",
35             "vnfdVersion": "V1",
36             "vnfPkgId": "2",
37             "instantiationState": "INSTANTIATED",
38             "instantiatedVnfInfo": {
39                 "vnfState": "STARTED",
40                 "extCpInfo": [],
41                 "virtualStorageResourceInfo": [
42                     {
43                         "id": "s02",
44                         "storageResource": {
45                             "resourceId": "resource01",
46                             "vimConnectionId": "vim01"
47                         }
48                     }
49                 ],
50                 "extVirtualLinkInfo": [],
51                 "vnfcResourceInfo": [],
52                 "monitoringParameters": [],
53                 "flavourId": None,
54                 "vnfVirtualLinkResourceInfo": [],
55                 "scaleStatus": []
56             }
57         }
58         self.test_data_multi_vnf = [
59             {
60                 "id": "1",
61                 "vnfInstanceName": "VNF1",
62                 "vnfInstanceDescription": "VNF DESC",
63                 "vnfdId": "1",
64                 "vnfProvider": "ZTE",
65                 "vnfProductName": "XGW",
66                 "vnfSoftwareVersion": "V1",
67                 "vnfdVersion": "V1",
68                 "vnfPkgId": "2",
69                 "instantiationState": "INSTANTIATED",
70                 "instantiatedVnfInfo": {
71                     "vnfState": "STARTED",
72                     "extCpInfo": [],
73                     "virtualStorageResourceInfo": [
74                         {
75                             "id": "s01",
76                             "storageResource": {
77                                 "resourceId": "resource01",
78                                 "vimConnectionId": "vim01"
79                             }
80                         }
81                     ],
82                     "extVirtualLinkInfo": [],
83                     "vnfcResourceInfo": [],
84                     "monitoringParameters": [],
85                     "flavourId": None,
86                     "vnfVirtualLinkResourceInfo": [],
87                     "scaleStatus": []
88                 }
89             },
90             {
91                 "id": "2",
92                 "vnfInstanceName": "VNF2",
93                 "vnfInstanceDescription": "VNF DESC",
94                 "vnfdId": "1",
95                 "vnfProvider": "ZTE",
96                 "vnfProductName": "XGW",
97                 "vnfSoftwareVersion": "V1",
98                 "vnfdVersion": "V1",
99                 "vnfPkgId": "2",
100                 "instantiationState": "INSTANTIATED",
101                 "instantiatedVnfInfo": {
102                     "vnfState": "STARTED",
103                     "extCpInfo": [],
104                     "virtualStorageResourceInfo": [
105                         {
106                             "id": "s02",
107                             "storageResource": {
108                                 "resourceId": "resource02",
109                                 "vimConnectionId": "vim02"
110                             }
111                         }
112                     ],
113                     "extVirtualLinkInfo": [],
114                     "vnfcResourceInfo": [],
115                     "monitoringParameters": [],
116                     "flavourId": None,
117                     "vnfVirtualLinkResourceInfo": [],
118                     "scaleStatus": []
119                 }
120             }
121         ]
122
123     def tearDown(self):
124         pass
125
126     def test_get_vnf(self):
127         vnf_inst_id = "1"
128         NfInstModel(nfinstid=vnf_inst_id,
129                     nf_name='VNF1',
130                     nf_desc="VNF DESC",
131                     vnfdid="1",
132                     netype="XGW",
133                     vendor="ZTE",
134                     vnfSoftwareVersion="V1",
135                     version="V1",
136                     package_id="2",
137                     status='INSTANTIATED').save()
138         StorageInstModel(storageid='s02',
139                          vimid='vim01',
140                          resourceid='resource01',
141                          insttype=1,
142                          instid=vnf_inst_id).save()
143         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
144         self.assertEqual(response.status_code, status.HTTP_200_OK)
145         self.assertEqual(self.test_data_single_vnf, response.data)
146
147     def test_get_vnf_not_exist(self):
148         response = self.client.get("/api/vnflcm/v1/vnf_instances/x", format='json')
149         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
150         self.assertEqual({
151             'detail': 'VnfInst(x) does not exist.',
152             'status': 500
153         }, response.data)
154
155     def test_get_vnf_net_not_exist(self):
156         vnf_inst_id = "1"
157         NfInstModel(nfinstid=vnf_inst_id,
158                     nf_name='VNF1',
159                     vnfdid="1",
160                     netype="XGW",
161                     vendor="ZTE",
162                     vnfSoftwareVersion="V1",
163                     version="V1",
164                     package_id="2",
165                     status='INSTANTIATED').save()
166         VLInstModel(ownerid=vnf_inst_id,
167                     relatednetworkid='x',
168                     ownertype='0').save()
169         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
170         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
171         self.assertEqual({
172             'detail': 'NetworkInst(x) does not exist.',
173             'status': 500
174         }, response.data)
175
176     def test_get_vnf_vm_not_exist(self):
177         vnf_inst_id = "1"
178         NfInstModel(nfinstid=vnf_inst_id,
179                     nf_name='VNF1',
180                     vnfdid="1",
181                     netype="XGW",
182                     vendor="ZTE",
183                     vnfSoftwareVersion="V1",
184                     version="V1",
185                     package_id="2",
186                     status='INSTANTIATED').save()
187         VNFCInstModel(instid=vnf_inst_id,
188                       vmid='x').save()
189         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
190         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
191         self.assertEqual({
192             'detail': 'VmInst(x) does not exist.',
193             'status': 500
194         }, response.data)
195
196     def test_get_vnf_storage_not_exist(self):
197         vnf_inst_id = "1"
198         NfInstModel(nfinstid=vnf_inst_id,
199                     nf_name='VNF1',
200                     vnfdid="1",
201                     netype="XGW",
202                     vendor="ZTE",
203                     vnfSoftwareVersion="V1",
204                     version="V1",
205                     package_id="2",
206                     status='INSTANTIATED').save()
207         VNFCInstModel(instid=vnf_inst_id,
208                       vmid='x',).save()
209         VmInstModel(vmid='x', insttype='0').save()
210         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
211         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
212
213     def test_get_vnfs(self):
214         for i in range(1, 3):
215             NfInstModel(nfinstid='%s' % i,
216                         nf_name='VNF%s' % i,
217                         nf_desc="VNF DESC",
218                         vnfdid="1",
219                         netype="XGW",
220                         vendor="ZTE",
221                         vnfSoftwareVersion="V1",
222                         version="V1",
223                         package_id="2",
224                         status='INSTANTIATED').save()
225             StorageInstModel(storageid='s0%s' % i,
226                              vimid='vim0%s' % i,
227                              resourceid='resource0%s' % i,
228                              insttype=1,
229                              instid='%s' % i).save()
230         response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
231         self.assertEqual(status.HTTP_200_OK, response.status_code)
232         self.assertEqual(self.test_data_multi_vnf, response.data)
233
234     def test_get_vnfs_not_exist(self):
235         response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
236         self.assertEqual(response.status_code, status.HTTP_200_OK)