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.
14 from django.test import TestCase, Client
15 from rest_framework import status
17 from res.pub.database.models import NfInstModel, StorageInstModel, VmInstModel, FlavourInstModel, NetworkInstModel, \
18 SubNetworkInstModel, CPInstModel
21 class ResourceTest(TestCase):
23 self.client = Client()
24 self.nf_inst_id = "01"
25 NfInstModel.objects.all().delete()
27 "vnfInstanceId": u'1',
28 "vnfInstanceName": 'VNF1',
29 "vnfInstanceDescription": None,
30 "onboardedVnfPkgInfoId": None,
33 "vnfSoftwareVersion": None,
35 "vnfProductName": None,
36 "vnfConfigurableProperties": {None},
37 "instantiationState": None,
38 "instantiatedVnfInfo": {
44 "monitoringParameters": {},
45 "localizationLanguage": None,
47 "vnfcResourceInfo": [],
48 "virtualLinkResourceInfo": [],
49 "virtualStorageResourceInfo": [
51 "virtualStorageInstanceId": "s02",
52 "virtualStorageDescId": "desc01",
55 "resourceId": "resource01"
72 "operationalstate": None,
93 self.networks_data = {
104 self.subnets_data = {
106 "subnetworkid": "sub0",
118 "cpinstanceid": "cp0",
120 "cpinstancename": "cpinstname0",
121 "vlinstanceid": "vlinst0",
127 self.volumes_data = {
134 "storagetype": "stype0",
143 def test_get_vnf(self):
145 NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1').save()
146 StorageInstModel(storageid='s02', vimid='vim01', resouceid='resource01', insttype=1,
147 instid=vnf_inst_id, storageDesc='desc01').save()
148 response = self.client.get("/openoapi/vnfres/v1/vnfs/%s" % vnf_inst_id)
149 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
150 self.assertEqual(self.test_data, response.data)
152 def test_get_vnfs(self):
153 for i in range(1, 5):
154 NfInstModel(nfinstid='%s' % i, nf_name='VNF%s' % i).save()
155 StorageInstModel(storageid='s0%s' % i, vimid='vim0%s' % i, resouceid='resource0%s' % i,
156 insttype=1, instid='%s' % i, storageDesc='desc%s' % i).save()
157 response = self.client.get("/openoapi/vnfres/v1/vnfs")
158 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
159 # self.assertEqual(self.test_data, response.data)
161 def test_get_vms(self):
162 NfInstModel(nfinstid='%s' % '01', nf_name='VFS%s' % '01').save()
163 nfinst = NfInstModel.objects.get(pk='01')
165 VmInstModel(vmid='vm%s' % i, vimid='vim%s' % i, resouceid='res%s' % i,
166 instid='%s' % nfinst.nfinstid, insttype=0, vmname='v1').save()
167 response = self.client.get("/openoapi/vnfres/v1/%s/vms" % nfinst.nfinstid)
168 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
169 self.assertEqual(self.vms_data, response.data)
171 def test_get_flavors(self):
172 NfInstModel(nfinstid='%s' % '01', nf_name='VFS%s' % '01').save()
173 nfinst = NfInstModel.objects.get(pk='01')
175 FlavourInstModel(flavourid='fla%s' % i, name='fname%s' % i, vcpu='cpu%s' % i,
176 instid='%s' % nfinst.nfinstid, memory='mem%s' % i,
177 extraspecs='ext%s' % i, vmid='vm%s' % i).save()
178 response = self.client.get("/openoapi/vnfres/v1/%s/flavors" % nfinst.nfinstid)
179 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
180 self.assertEqual(self.flavors_data, response.data)
182 def test_get_networks(self):
184 NetworkInstModel(networkid='net%s' % i, name='net_name%s' % i, vimid='vim%s' % i,
185 instid='%s' % self.nf_inst_id, resouceid='res%s' % i, insttype='%d' % i).save()
186 response = self.client.get("/openoapi/vnfres/v1/%s/networks" % self.nf_inst_id)
187 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
188 self.assertEqual(self.networks_data, response.data)
190 def test_get_subnets(self):
192 SubNetworkInstModel(subnetworkid="sub%s" % i, vimid='vim%s' % i, resouceid='res%s' % i,
193 networkid='net%s' % i, insttype='%d' % i, instid='%s' % self.nf_inst_id,
194 name='sub_name%s' % i, cidr="cidr%s" % i).save()
195 response = self.client.get("/openoapi/vnfres/v1/%s/subnets" % self.nf_inst_id)
196 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
197 self.assertEqual(self.subnets_data, response.data)
199 def test_get_cps(self):
201 CPInstModel(cpinstanceid="cp%s" % i, cpdid='cpd%s' % i, cpinstancename='cpinstname%s' % i,
202 vlinstanceid='vlinst%s' % i, ownertype='%d' % i, ownerid='%s' % self.nf_inst_id,
203 relatedtype='%d' % i).save()
204 response = self.client.get("/openoapi/vnfres/v1/%s/cps" % self.nf_inst_id)
205 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
206 self.assertEqual(self.cps_data, response.data)
208 def test_get_volumes(self):
210 StorageInstModel(storageid="st%s" % i, vimid='vim%s' % i, resouceid='res%s' % i, insttype='%d' % i,
211 instid='%s' % self.nf_inst_id, storagetype='stype%s' % i, size='%s' % i,
212 disktype='disk%s' % i).save()
213 response = self.client.get("/openoapi/vnfres/v1/%s/volumes" % self.nf_inst_id)
214 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
215 self.assertEqual(self.volumes_data, response.data)
217 def test_swagger_ok(self):
218 resp = self.client.get("/openoapi/vnfres/v1/swagger.json", format='json')
219 self.assertEqual(resp.status_code, status.HTTP_200_OK)