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, StorageInstModel
21 class ResourceTest(TestCase):
23 self.client = Client()
24 self.nf_inst_id = "01"
25 NfInstModel.objects.all().delete()
26 self.test_data_single_vnf = {
28 "vnfInstanceName": "VNF1",
30 "instantiationState": "INSTANTIATED",
31 "instantiatedVnfInfo": {
32 "vnfState": "STARTED",
34 "virtualStorageResourceInfo": [
38 "resourceId": "resource01",
39 "vimConnectionId": "vim01"
43 "extVirtualLinkInfo": [],
44 "vnfcResourceInfo": [],
45 "monitoringParameters": {},
47 "vnfVirtualLinkResourceInfo": [],
53 self.test_data_multi_vnf = [
56 "vnfInstanceName": "VNF1",
58 "instantiationState": "INSTANTIATED",
59 "instantiatedVnfInfo": {
60 "vnfState": "STARTED",
62 "virtualStorageResourceInfo": [
66 "resourceId": "resource01",
67 "vimConnectionId": "vim01"
71 "extVirtualLinkInfo": [],
72 "vnfcResourceInfo": [],
73 "monitoringParameters": {},
75 "vnfVirtualLinkResourceInfo": [],
83 "vnfInstanceName": "VNF2",
85 "instantiationState": "INSTANTIATED",
86 "instantiatedVnfInfo": {
87 "vnfState": "STARTED",
89 "virtualStorageResourceInfo": [
93 "resourceId": "resource02",
94 "vimConnectionId": "vim02"
98 "extVirtualLinkInfo": [],
99 "vnfcResourceInfo": [],
100 "monitoringParameters": {},
102 "vnfVirtualLinkResourceInfo": [],
113 def test_get_vnf(self):
115 NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1', status='INSTANTIATED').save()
116 StorageInstModel(storageid='s02',
118 resouceid='resource01',
120 instid=vnf_inst_id).save()
121 response = self.client.get("/api/vnflcm/v1/vnf_instances/%s" % vnf_inst_id, format='json')
122 self.assertEqual(response.status_code, status.HTTP_200_OK)
123 self.assertEqual(self.test_data_single_vnf, response.data)
125 def test_get_vnf_not_exist(self):
126 response = self.client.get("/api/vnflcm/v1/vnf_instances/x", format='json')
127 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
128 self.assertEqual({'error': 'VnfInst(x) does not exist'}, response.data)
130 def test_get_vnfs(self):
131 for i in range(1, 3):
132 NfInstModel(nfinstid='%s' % i,
134 status='INSTANTIATED').save()
135 StorageInstModel(storageid='s0%s' % i,
137 resouceid='resource0%s' % i,
139 instid='%s' % i).save()
140 response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
141 self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
142 self.assertEqual(self.test_data_multi_vnf, response.data)
144 def test_get_vnfs_not_exist(self):
145 response = self.client.get("/api/vnflcm/v1/vnf_instances", format='json')
146 self.assertEqual(response.status_code, status.HTTP_500_INTERNAL_SERVER_ERROR)
147 self.assertEqual({'error': 'VnfInsts does not exist'}, response.data)