Fix vfc-gvnfm-vnfmgr pep8 W293 issue.
[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 unittest
16 import json
17 import mock
18 from django.test import Client
19 from rest_framework import status
20
21 from mgr.pub.database.models import VnfRegModel
22 from mgr.pub.utils import restcall
23
24
25 class VnfRegTest(unittest.TestCase):
26     def setUp(self):
27         self.client = Client()
28         VnfRegModel.objects.filter().delete()
29         self.vnfInst1 = {
30             "vnfInstId": "1",
31             "ip": "192.168.0.1",
32             "port": "2324",
33             "username": "admin",
34             "password": "admin123"
35         }
36         self.vnfInst1_new = {
37             "vnfInstId": "1",
38             "ip": "192.168.0.2",
39             "port": "2325",
40             "username": "admin1",
41             "password": "admin1234"
42         }
43         self.vnfconfig = {
44             "vnfInstanceId": "1",
45             "vnfConfigurationData": {
46                 "cp": [
47                     {
48                         "cpId": "cp-1",
49                         "cpdId": "cpd-a",
50                         "cpAddress": [
51                             {
52                                 "addresses": [
53                                     {
54                                         "addressType": "MAC",
55                                         "l2AddressData": "00:f3:43:20:a2:a3"
56                                     },
57                                     {
58                                         "addressType": "IP",
59                                         "l3AddressData": {
60                                             "iPAddressType": "IPv4",
61                                             "iPAddress": "192.168.104.2"
62                                         }
63                                     }
64                                 ],
65                                 "useDynamicAddress": "FALSE"
66                             }
67                         ]
68                     }
69                 ],
70                 "vnfSpecificData": {
71                     "autoScalable": "FALSE",
72                     "autoHealable": "FALSE"
73                 }
74             },
75             "vnfcConfigurationData": {
76                 "vnfcId": "vnfc-1",
77                 "cp": [
78                     {
79                         "cpId": "cp-11",
80                         "cpdId": "cpd-1a",
81                         "cpAddress": [
82                             {
83                                 "addresses": [
84                                     {
85                                         "addressType": "MAC",
86                                         "l2AddressData": "00:f3:43:21:a2:a3"
87                                     },
88                                     {
89                                         "addressType": "IP",
90                                         "l3AddressData": {
91                                             "iPAddressType": "IPv4",
92                                             "iPAddress": "192.168.105.2"
93                                         }
94                                     }
95                                 ],
96                                 "useDynamicAddress": "FALSE"
97                             }
98                         ]
99                     }
100                 ],
101                 "vnfcSpecificData": {}
102             }
103         }
104
105     def tearDown(self):
106         pass
107
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))
113         vnfInstActual = {
114             "vnfInstId": vnfs[0].id,
115             "ip": vnfs[0].ip,
116             "port": vnfs[0].port,
117             "username": vnfs[0].username,
118             "password": vnfs[0].password
119         }
120         self.assertEqual(self.vnfInst1, vnfInstActual)
121
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))
127
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))
135         vnfInstActual = {
136             "vnfInstId": vnfs[0].id,
137             "ip": vnfs[0].ip,
138             "port": vnfs[0].port,
139             "username": vnfs[0].username,
140             "password": vnfs[0].password
141         }
142         self.assertEqual(self.vnfInst1_new, vnfInstActual)
143
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))
149
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))
155
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))
160
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)
165
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))
170
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)
177
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))