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.
18 from django.test import Client
19 from rest_framework import status
21 from mgr.pub.database.models import VnfRegModel
22 from mgr.pub.utils import restcall
25 class VnfRegTest(unittest.TestCase):
27 self.client = Client()
28 VnfRegModel.objects.filter().delete()
34 "password": "admin123"
41 "password": "admin1234"
45 "vnfConfigurationData": {
55 "l2AddressData": "00:f3:43:20:a2:a3"
60 "iPAddressType": "IPv4",
61 "iPAddress": "192.168.104.2"
65 "useDynamicAddress": "FALSE"
71 "autoScalable": "FALSE",
72 "autoHealable": "FALSE"
75 "vnfcConfigurationData": {
86 "l2AddressData": "00:f3:43:21:a2:a3"
91 "iPAddressType": "IPv4",
92 "iPAddress": "192.168.105.2"
96 "useDynamicAddress": "FALSE"
101 "vnfcSpecificData": {}
108 def test_add_vnf_normal(self):
109 response = self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
110 self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
111 vnfs = VnfRegModel.objects.filter()
112 self.assertEqual(1, len(vnfs))
114 "vnfInstId": vnfs[0].id,
116 "port": vnfs[0].port,
117 "username": vnfs[0].username,
118 "password": vnfs[0].password
120 self.assertEqual(self.vnfInst1, vnfInstActual)
122 def test_add_vnf_when_duplicate(self):
123 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
124 response = self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
125 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code, response.content)
126 self.assertEqual({'error': "Vnf(1) already exists."}, json.loads(response.content))
128 def test_set_vnf_normal(self):
129 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
130 response = self.client.put("/api/vnfmgr/v1/vnfs/1",
131 json.dumps(self.vnfInst1_new), content_type='application/json')
132 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
133 vnfs = VnfRegModel.objects.filter()
134 self.assertEqual(1, len(vnfs))
136 "vnfInstId": vnfs[0].id,
138 "port": vnfs[0].port,
139 "username": vnfs[0].username,
140 "password": vnfs[0].password
142 self.assertEqual(self.vnfInst1_new, vnfInstActual)
144 def test_set_vnf_when_not_exist(self):
145 response = self.client.put("/api/vnfmgr/v1/vnfs/1",
146 json.dumps(self.vnfInst1_new), content_type='application/json')
147 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
148 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
150 def test_get_vnf_normal(self):
151 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
152 response = self.client.get("/api/vnfmgr/v1/vnfs/1")
153 self.assertEqual(status.HTTP_200_OK, response.status_code, response.content)
154 self.assertEqual(self.vnfInst1, json.loads(response.content))
156 def test_get_vnf_when_not_exist(self):
157 response = self.client.get("/api/vnfmgr/v1/vnfs/1")
158 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
159 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
161 def test_del_vnf_normal(self):
162 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
163 response = self.client.delete("/api/vnfmgr/v1/vnfs/1")
164 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
166 def test_del_vnf_when_not_exist(self):
167 response = self.client.delete("/api/vnfmgr/v1/vnfs/1")
168 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
169 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
171 @mock.patch.object(restcall, 'call_req')
172 def test_vnf_config_normal(self, mock_call_req):
173 mock_call_req.return_value = [0, "", '204']
174 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
175 response = self.client.post("/api/vnfmgr/v1/configuration", self.vnfconfig, format='json')
176 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
178 def test_vnf_config_when_not_exist(self):
179 response = self.client.post("/api/vnfmgr/v1/configuration", self.vnfconfig, format='json')
180 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code, response.content)
181 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))