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.
19 from rest_framework import status
20 from rest_framework.test import APIClient
22 from mgr.pub.database.models import VnfRegModel
23 from mgr.pub.utils import restcall
26 class VnfRegTest(unittest.TestCase):
28 self.client = APIClient()
29 VnfRegModel.objects.filter().delete()
35 "password": "admin123"
39 "vnfConfigurationData": {
47 "autoScalable": "FALSE",
48 "autoHealable": "FALSE"
56 def test_add_vnf_normal(self):
57 response = self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
58 self.assertEqual(status.HTTP_201_CREATED, response.status_code, response.content)
59 vnfs = VnfRegModel.objects.filter()
60 self.assertEqual(1, len(vnfs))
62 "vnfInstId": vnfs[0].id,
65 "username": vnfs[0].username,
66 "password": vnfs[0].password
68 self.assertEqual(self.vnfInst1, vnfInstActual)
70 def test_add_vnf_when_duplicate(self):
71 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
72 response = self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
73 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code, response.content)
74 self.assertEqual({'error': "Vnf(1) already exists."}, json.loads(response.content))
76 def test_set_vnf_normal(self):
77 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
78 response = self.client.put("/api/vnfmgr/v1/vnfs/1",
79 json.dumps(self.vnfInst1), content_type='application/json')
80 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
81 vnfs = VnfRegModel.objects.filter()
82 self.assertEqual(1, len(vnfs))
84 "vnfInstId": vnfs[0].id,
87 "username": vnfs[0].username,
88 "password": vnfs[0].password
90 self.assertEqual(self.vnfInst1, vnfInstActual)
92 def test_set_vnf_when_not_exist(self):
93 response = self.client.put("/api/vnfmgr/v1/vnfs/1",
94 json.dumps(self.vnfInst1), content_type='application/json')
95 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
96 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
98 def test_get_vnf_normal(self):
99 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
100 response = self.client.get("/api/vnfmgr/v1/vnfs/1")
101 self.assertEqual(status.HTTP_200_OK, response.status_code, response.content)
102 self.assertEqual(self.vnfInst1, json.loads(response.content))
104 def test_get_vnf_when_not_exist(self):
105 response = self.client.get("/api/vnfmgr/v1/vnfs/1")
106 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
107 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
109 def test_del_vnf_normal(self):
110 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
111 response = self.client.delete("/api/vnfmgr/v1/vnfs/1")
112 self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
114 def test_del_vnf_when_not_exist(self):
115 response = self.client.delete("/api/vnfmgr/v1/vnfs/1")
116 self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
117 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
119 @mock.patch.object(restcall, 'call_req')
120 def test_vnf_config_normal(self, mock_call_req):
121 mock_call_req.return_value = [0, "", '204']
122 self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
123 response = self.client.post("/api/vnfmgr/v1/configuration", self.vnfconfig, format='json')
124 self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
126 def test_vnf_config_when_not_exist(self):
127 response = self.client.post("/api/vnfmgr/v1/configuration", self.vnfconfig, format='json')
128 self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code, response.content)
129 self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
132 class HealthCheckViewTest(unittest.TestCase):
134 self.client = APIClient()
139 def test_health_check(self):
140 response = self.client.get("/api/vnfmgr/v1/health_check")
141 self.assertEqual(status.HTTP_200_OK, response.status_code, response.content)
142 resp_data = json.loads(response.content)
143 self.assertEqual({"status": "active"}, resp_data)