1 # Copyright 2017 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.
15 from django.test import TestCase, Client
16 from rest_framework import status
18 from lcm.pub.database.models import (NfInstModel, VLInstModel,
19 VNFCInstModel, VmInstModel, StorageInstModel)
22 class ResourceTest(TestCase):
24 self.client = Client()
25 self.nf_inst_id = "01"
26 NfInstModel.objects.all().delete()
27 self.test_data_single_vnf = {
29 "vnfInstanceName": "VNF1",
30 "vnfInstanceDescription": "VNF DESC",
33 "vnfProductName": "XGW",
34 "vnfSoftwareVersion": "V1",
37 "instantiationState": "INSTANTIATED",
38 "instantiatedVnfInfo": {
39 "vnfState": "STARTED",
41 "virtualStorageResourceInfo": [
45 "resourceId": "resource01",
46 "vimConnectionId": "vim01"
50 "extVirtualLinkInfo": [],
51 "vnfcResourceInfo": [],
52 "monitoringParameters": [],
54 "vnfVirtualLinkResourceInfo": [],
58 self.test_data_multi_vnf = [
61 "vnfInstanceName": "VNF1",
62 "vnfInstanceDescription": "VNF DESC",
65 "vnfProductName": "XGW",
66 "vnfSoftwareVersion": "V1",
69 "instantiationState": "INSTANTIATED",
70 "instantiatedVnfInfo": {
71 "vnfState": "STARTED",
73 "virtualStorageResourceInfo": [
77 "resourceId": "resource01",
78 "vimConnectionId": "vim01"
82 "extVirtualLinkInfo": [],
83 "vnfcResourceInfo": [],
84 "monitoringParameters": [],
86 "vnfVirtualLinkResourceInfo": [],
92 "vnfInstanceName": "VNF2",
93 "vnfInstanceDescription": "VNF DESC",
96 "vnfProductName": "XGW",
97 "vnfSoftwareVersion": "V1",
100 "instantiationState": "INSTANTIATED",
101 "instantiatedVnfInfo": {
102 "vnfState": "STARTED",
104 "virtualStorageResourceInfo": [
108 "resourceId": "resource02",
109 "vimConnectionId": "vim02"
113 "extVirtualLinkInfo": [],
114 "vnfcResourceInfo": [],
115 "monitoringParameters": [],
117 "vnfVirtualLinkResourceInfo": [],
126 def test_get_vnf(self):
128 NfInstModel(nfinstid=vnf_inst_id,
134 vnfSoftwareVersion="V1",
137 status='INSTANTIATED').save()
138 StorageInstModel(storageid='s02',
140 resourceid='resource01',
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)
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)
152 def test_get_vnf_net_not_exist(self):
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)
160 def test_get_vnf_vm_not_exist(self):
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)
168 def test_get_vnf_storage_not_exist(self):
170 NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
171 VNFCInstModel(instid=vnf_inst_id,
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)
177 def test_get_vnfs(self):
178 for i in range(1, 3):
179 NfInstModel(nfinstid='%s' % i,
185 vnfSoftwareVersion="V1",
188 status='INSTANTIATED').save()
189 StorageInstModel(storageid='s0%s' % i,
191 resourceid='resource0%s' % i,
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)
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)