Dealing with test related stuffs.
[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             "vnfProvider": None,
31             "instantiationState": "INSTANTIATED",
32             "instantiatedVnfInfo": {
33                 "vnfState": "STARTED",
34                 "extCpInfo": [],
35                 "virtualStorageResourceInfo": [
36                     {
37                         "id": "s02",
38                         "storageResource": {
39                             "resourceId": "resource01",
40                             "vimConnectionId": "vim01"
41                         }
42                     }
43                 ],
44                 "extVirtualLinkInfo": [],
45                 "vnfcResourceInfo": [],
46                 "monitoringParameters": {},
47                 "flavourId": None,
48                 "vnfVirtualLinkResourceInfo": [],
49                 "scaleStatus": []
50             },
51             "vnfdVersion": None,
52             "vnfPkgId": None
53         }
54         self.test_data_multi_vnf = [
55             {
56                 "id": "1",
57                 "vnfInstanceName": "VNF1",
58                 "vnfProvider": None,
59                 "instantiationState": "INSTANTIATED",
60                 "instantiatedVnfInfo": {
61                     "vnfState": "STARTED",
62                     "extCpInfo": [],
63                     "virtualStorageResourceInfo": [
64                         {
65                             "id": "s01",
66                             "storageResource": {
67                                 "resourceId": "resource01",
68                                 "vimConnectionId": "vim01"
69                             }
70                         }
71                     ],
72                     "extVirtualLinkInfo": [],
73                     "vnfcResourceInfo": [],
74                     "monitoringParameters": {},
75                     "flavourId": None,
76                     "vnfVirtualLinkResourceInfo": [],
77                     "scaleStatus": []
78                 },
79                 "vnfdVersion": None,
80                 "vnfPkgId": None
81             },
82             {
83                 "id": "2",
84                 "vnfInstanceName": "VNF2",
85                 "vnfProvider": None,
86                 "instantiationState": "INSTANTIATED",
87                 "instantiatedVnfInfo": {
88                     "vnfState": "STARTED",
89                     "extCpInfo": [],
90                     "virtualStorageResourceInfo": [
91                         {
92                             "id": "s02",
93                             "storageResource": {
94                                 "resourceId": "resource02",
95                                 "vimConnectionId": "vim02"
96                             }
97                         }
98                     ],
99                     "extVirtualLinkInfo": [],
100                     "vnfcResourceInfo": [],
101                     "monitoringParameters": {},
102                     "flavourId": None,
103                     "vnfVirtualLinkResourceInfo": [],
104                     "scaleStatus": []
105                 },
106                 "vnfdVersion": None,
107                 "vnfPkgId": None
108             }
109         ]
110
111     def tearDown(self):
112         pass
113
114     def test_get_vnf(self):
115         vnf_inst_id = "1"
116         NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
117         StorageInstModel(storageid='s02',
118                          vimid='vim01',
119                          resouceid='resource01',
120                          insttype=1,
121                          instid=vnf_inst_id).save()
122         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
123         self.assertEqual(response.status_code, status.HTTP_200_OK)
124         self.assertEqual(self.test_data_single_vnf, response.data)
125
126     def test_get_vnf_not_exist(self):
127         response = self.client.get("/api/vnflcm/v1/vnf_instances/x", format='json')
128         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
129         self.assertEqual({'error': 'VnfInst(x) does not exist.'}, response.data)
130
131     def test_get_vnf_net_not_exist(self):
132         vnf_inst_id = "1"
133         NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
134         VLInstModel(ownerid=vnf_inst_id, relatednetworkid='x', ownertype='0').save()
135         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
136         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
137         self.assertEqual({'error': 'NetworkInst(x) does not exist.'}, response.data)
138
139     def test_get_vnf_vm_not_exist(self):
140         vnf_inst_id = "1"
141         NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
142         VNFCInstModel(instid=vnf_inst_id, vmid='x').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_500_INTERNAL_SERVER_ERROR)
145         self.assertEqual({'error': 'VmInst(x) does not exist.'}, response.data)
146
147     def test_get_vnf_storage_not_exist(self):
148         vnf_inst_id = "1"
149         NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
150         VNFCInstModel(instid=vnf_inst_id,
151                       vmid='x',).save()
152         VmInstModel(vmid='x', insttype='0').save()
153         response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
154         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
155         self.assertEqual({'error': 'StorageInst(x) does not exist.'}, response.data)
156
157     def test_get_vnfs(self):
158         for i in range(1, 3):
159             NfInstModel(nfinstid='%s' % i,
160                         nf_name='VNF%s' % i,
161                         status='INSTANTIATED').save()
162             StorageInstModel(storageid='s0%s' % i,
163                              vimid='vim0%s' % i,
164                              resouceid='resource0%s' % i,
165                              insttype=1,
166                              instid='%s' % i).save()
167         response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
168         self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
169         self.assertEqual(self.test_data_multi_vnf, response.data)
170
171     def test_get_vnfs_not_exist(self):
172         response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
173         self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
174         self.assertEqual({'error': 'VnfInsts does not exist.'}, response.data)