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
24 class VnfRegTest(unittest.TestCase):
26 self.client = Client()
27 VnfRegModel.objects.filter().delete()
33 "password": "admin123"
40 "password": "admin1234"
44 "vnfConfigurationData": {
54 "l2AddressData": "00:f3:43:20:a2:a3"
59 "iPAddressType": "IPv4",
60 "iPAddress": "192.168.104.2"
64 "useDynamicAddress": "FALSE"
70 "autoScalable": "FALSE",
71 "autoHealable": "FALSE"
74 "vnfcConfigurationData": {
85 "l2AddressData": "00:f3:43:21:a2:a3"
90 "iPAddressType": "IPv4",
91 "iPAddress": "192.168.105.2"
95 "useDynamicAddress": "FALSE"
100 "vnfcSpecificData": {}
107 def test_add_vnf_normal(self):
108 response = self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
109 self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
110 vnfs = VnfRegModel.objects.filter()
111 self.assertEqual(1, len(vnfs))
113 "vnfInstId": vnfs[0].id,
115 "port": vnfs[0].port,
116 "username": vnfs[0].username,
117 "password": vnfs[0].password
119 self.assertEqual(self.vnfInst1, vnfInstActual)
121 def test_add_vnf_when_duplicate(self):
122 self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
123 response = self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
124 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code, response.content)
125 self.assertEqual({'error': "Vnf(1) already exists."}, json.loads(response.content))
127 def test_set_vnf_normal(self):
128 self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
129 response = self.client.put("/openoapi/vnfmgr/v1/vnfs/1",
130 json.dumps(self.vnfInst1_new), content_type='application/json')
131 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
132 vnfs = VnfRegModel.objects.filter()
133 self.assertEqual(1, len(vnfs))
135 "vnfInstId": vnfs[0].id,
137 "port": vnfs[0].port,
138 "username": vnfs[0].username,
139 "password": vnfs[0].password
141 self.assertEqual(self.vnfInst1_new, vnfInstActual)
143 def test_set_vnf_when_not_exist(self):
144 response = self.client.put("/openoapi/vnfmgr/v1/vnfs/1",
145 json.dumps(self.vnfInst1_new), content_type='application/json')
146 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
147 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
149 def test_get_vnf_normal(self):
150 self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
151 response = self.client.get("/openoapi/vnfmgr/v1/vnfs/1")
152 self.assertEqual(status.HTTP_200_OK, response.status_code, response.content)
153 self.assertEqual(self.vnfInst1, json.loads(response.content))
155 def test_get_vnf_when_not_exist(self):
156 response = self.client.get("/openoapi/vnfmgr/v1/vnfs/1")
157 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
158 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
160 def test_del_vnf_normal(self):
161 self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
162 response = self.client.delete("/openoapi/vnfmgr/v1/vnfs/1")
163 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
165 def test_del_vnf_when_not_exist(self):
166 response = self.client.delete("/openoapi/vnfmgr/v1/vnfs/1")
167 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
168 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
170 @mock.patch.object(restcall, 'call_req')
171 def test_vnf_config_normal(self, mock_call_req):
172 mock_call_req.return_value = [0, "", '204']
173 self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
174 response = self.client.post("/openoapi/vnfmgr/v1/configuration", self.vnfconfig, format='json')
175 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
177 def test_vnf_config_when_not_exist(self):
178 response = self.client.post("/openoapi/vnfmgr/v1/configuration", self.vnfconfig, format='json')
179 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code, response.content)
180 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))