Fix query vnf bugs
[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({'error': 'VnfInst(x) does not exist.'}, response.data)
151
152     def test_get_vnf_net_not_exist(self):
153         vnf_inst_id = "1"
154         NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
155         VLInstModel(ownerid=vnf_inst_id, relatednetworkid='x', ownertype='0').save()
156         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
157         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
158         self.assertEqual({'error': 'NetworkInst(x) does not exist.'}, response.data)
159
160     def test_get_vnf_vm_not_exist(self):
161         vnf_inst_id = "1"
162         NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
163         VNFCInstModel(instid=vnf_inst_id, vmid='x').save()
164         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
165         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
166         self.assertEqual({'error': 'VmInst(x) does not exist.'}, response.data)
167
168     def test_get_vnf_storage_not_exist(self):
169         vnf_inst_id = "1"
170         NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
171         VNFCInstModel(instid=vnf_inst_id,
172                       vmid='x',).save()
173         VmInstModel(vmid='x', insttype='0').save()
174         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
175         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
176
177     def test_get_vnfs(self):
178         for i in range(1, 3):
179             NfInstModel(nfinstid='%s' % i,
180                         nf_name='VNF%s' % i,
181                         nf_desc="VNF DESC",
182                         vnfdid="1",
183                         netype="XGW",
184                         vendor="ZTE",
185                         vnfSoftwareVersion="V1",
186                         version="V1",
187                         package_id="2",
188                         status='INSTANTIATED').save()
189             StorageInstModel(storageid='s0%s' % i,
190                              vimid='vim0%s' % i,
191                              resourceid='resource0%s' % i,
192                              insttype=1,
193                              instid='%s' % i).save()
194         response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
195         self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
196         self.assertEqual(self.test_data_multi_vnf, response.data)
197
198     def test_get_vnfs_not_exist(self):
199         response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
200         self.assertEqual(response.status_code, status.HTTP_200_OK)