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",
31 "instantiationState": "INSTANTIATED",
32 "instantiatedVnfInfo": {
33 "vnfState": "STARTED",
35 "virtualStorageResourceInfo": [
39 "resourceId": "resource01",
40 "vimConnectionId": "vim01"
44 "extVirtualLinkInfo": [],
45 "vnfcResourceInfo": [],
46 "monitoringParameters": {},
48 "vnfVirtualLinkResourceInfo": [],
54 self.test_data_multi_vnf = [
57 "vnfInstanceName": "VNF1",
59 "instantiationState": "INSTANTIATED",
60 "instantiatedVnfInfo": {
61 "vnfState": "STARTED",
63 "virtualStorageResourceInfo": [
67 "resourceId": "resource01",
68 "vimConnectionId": "vim01"
72 "extVirtualLinkInfo": [],
73 "vnfcResourceInfo": [],
74 "monitoringParameters": {},
76 "vnfVirtualLinkResourceInfo": [],
84 "vnfInstanceName": "VNF2",
86 "instantiationState": "INSTANTIATED",
87 "instantiatedVnfInfo": {
88 "vnfState": "STARTED",
90 "virtualStorageResourceInfo": [
94 "resourceId": "resource02",
95 "vimConnectionId": "vim02"
99 "extVirtualLinkInfo": [],
100 "vnfcResourceInfo": [],
101 "monitoringParameters": {},
103 "vnfVirtualLinkResourceInfo": [],
114 def test_get_vnf(self):
116 NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
117 StorageInstModel(storageid='s02',
119 resourceid='resource01',
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)
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)
131 def test_get_vnf_net_not_exist(self):
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)
139 def test_get_vnf_vm_not_exist(self):
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)
147 def test_get_vnf_storage_not_exist(self):
149 NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
150 VNFCInstModel(instid=vnf_inst_id,
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)
156 def test_get_vnfs(self):
157 for i in range(1, 3):
158 NfInstModel(nfinstid='%s' % i,
160 status='INSTANTIATED').save()
161 StorageInstModel(storageid='s0%s' % i,
163 resourceid='resource0%s' % i,
165 instid='%s' % i).save()
166 response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
167 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
168 self.assertEqual(self.test_data_multi_vnf, response.data)
170 def test_get_vnfs_not_exist(self):
171 response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
172 self.assertEqual(response.status_code, status.HTTP_200_OK)