3c197aa7c8055555101e7312c7ce5a410d858175
[vfc/gvnfm/vnfmgr.git] / mgr / mgr / vnfreg / tests.py
1 # Copyright 2017 ZTE Corporation.
2 #
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
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
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
15 import json
16 import unittest
17
18 import mock
19 from rest_framework import status
20 from rest_framework.test import APIClient
21
22 from mgr.pub.database.models import VnfRegModel
23 from mgr.pub.utils import restcall
24
25
26 class VnfRegTest(unittest.TestCase):
27     def setUp(self):
28         self.client = APIClient()
29         VnfRegModel.objects.filter().delete()
30         self.vnfInst1 = {
31             "vnfInstId": "1",
32             "ip": "192.168.0.1",
33             "port": "2324",
34             "username": "admin",
35             "password": "admin123"
36         }
37         self.vnfconfig = {
38             "vnfInstanceId": "1",
39             "vnfConfigurationData": {
40                 "cp": [
41                     {
42                         "cpId": "cp-1",
43                         "cpdId": "cpd-a",
44                     }
45                 ],
46                 "vnfSpecificData": {
47                     "autoScalable": "FALSE",
48                     "autoHealable": "FALSE"
49                 }
50             }
51         }
52
53     def tearDown(self):
54         pass
55
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))
61         vnfInstActual = {
62             "vnfInstId": vnfs[0].id,
63             "ip": vnfs[0].ip,
64             "port": vnfs[0].port,
65             "username": vnfs[0].username,
66             "password": vnfs[0].password
67         }
68         self.assertEqual(self.vnfInst1, vnfInstActual)
69
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))
75
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))
83         vnfInstActual = {
84             "vnfInstId": vnfs[0].id,
85             "ip": vnfs[0].ip,
86             "port": vnfs[0].port,
87             "username": vnfs[0].username,
88             "password": vnfs[0].password
89         }
90         self.assertEqual(self.vnfInst1, vnfInstActual)
91
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))
97
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))
103
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))
108
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)
113
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))
118
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)
125
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))