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,
75 "security_groups": 'sec0',
76 "flavor_id": 'flavor0',
77 "availability_zone": 'ava0',
78 "server_group": 'server0',
79 "volume_array": 'volume0',
98 self.networks_data = {
109 self.subnets_data = {
111 "subnetworkid": "sub0",
123 "cpinstanceid": "cp0",
125 "cpinstancename": "cpinstname0",
126 "vlinstanceid": "vlinst0",
133 self.volumes_data = {
140 "storagetype": "stype0",
148 def test_get_vnf(self):
150 NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1').save()
151 StorageInstModel(storageid='s02', vimid='vim01', resouceid='resource01', insttype=1,\
152 instid=vnf_inst_id, storagetype='desc01', size='ten').save()
153 response = self.client.get("/openoapi/vnfres/v1/vnfs/%s" % vnf_inst_id)
154 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
155 self.assertEqual(self.test_data, response.data)
158 def test_get_vnfs(self):
160 NfInstModel(nfinstid='%s' % i, nf_name='VNF%s' % i).save()
161 StorageInstModel(storageid='s0%s' % i, vimid='vim0%s' % i, resouceid='resource0%s' % i, insttype=1, instid='%s' % i, storagetype='desc%s' % i, size='ten').save()
162 response = self.client.get("/openoapi/vnfres/v1/vnfs")
163 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
164 # self.assertEqual(self.test_data, response.data)
166 def test_get_vms(self):
167 NfInstModel(nfinstid='%s' % '01', nf_name='VFS%s' % '01').save()
168 nfInst = NfInstModel.objects.get(pk='01')
170 VmInstModel(vmid='vm%s' % i, vimid='vim%s' % i, resouceid='res%s' % i, instid='%s' % nfInst.nfinstid, insttype=0, vmname='v1', nic_array='nic%s' % i, metadata='meta%s' % i, volume_array='volume%s' % i, server_group='server%s' % i, availability_zone='ava%s' %i, flavor_id='flavor%s' % i, security_groups='sec%s' % i).save()
171 response = self.client.get("/openoapi/vnfres/v1/%s/vms" % nfInst.nfinstid)
172 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
173 self.assertEqual(self.vms_data, response.data)
175 def test_get_flavors(self):
176 NfInstModel(nfinstid='%s' % '01', nf_name='VFS%s' % '01').save()
177 nfInst = NfInstModel.objects.get(pk='01')
179 FlavourInstModel(flavourid='fla%s' % i, name='fname%s' % i, vcpu='%d' % i, instid='%s' % nfInst.nfinstid, memory='%d' % i, extraspecs='ext%s' % i, vimid='vim%s' % i, resouceid='res%s' % i).save()
180 response = self.client.get("/openoapi/vnfres/v1/%s/flavors" % nfInst.nfinstid)
181 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
182 self.assertEqual(self.flavors_data, response.data)
184 def test_get_networks(self):
186 NetworkInstModel(networkid='net%s' % i, name='net_name%s' % i, vimid='vim%s' % i, instid='%s' % self.nf_inst_id,
187 resouceid='res%s' % i, insttype='%d' % i).save()
188 response = self.client.get("/openoapi/vnfres/v1/%s/networks" % self.nf_inst_id)
189 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
190 self.assertEqual(self.networks_data, response.data)
192 def test_get_subnets(self):
194 SubNetworkInstModel(subnetworkid="sub%s" % i, vimid='vim%s' % i, resouceid='res%s' % i, networkid='net%s' % i, insttype='%d' % i, instid='%s' % self.nf_inst_id, 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, vlinstanceid='vlinst%s' % i, ownertype='%d' % i, ownerid='%s' % self.nf_inst_id, relatedtype='%d' % i).save()
202 response = self.client.get("/openoapi/vnfres/v1/%s/cps" % self.nf_inst_id)
203 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
204 self.assertEqual(self.cps_data, response.data)
206 def test_get_volumes(self):
208 StorageInstModel(storageid="st%s" % i, vimid='vim%s' % i, resouceid='res%s' % i, insttype='%d' % i, instid='%s' % self.nf_inst_id, storagetype='stype%s' % i, size='%s' % i).save()
209 response = self.client.get("/openoapi/vnfres/v1/%s/volumes" % self.nf_inst_id)
210 self.assertEqual(self.volumes_data, response.data)
211 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
213 def test_swagger_ok(self):
214 resp = self.client.get("/openoapi/vnfres/v1/swagger.json", format='json')
215 self.assertEqual(resp.status_code, status.HTTP_200_OK, resp.content)