Remove vfc-gvnfm-vnfmgr openo labels
[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 class VnfRegTest(unittest.TestCase):
25     def setUp(self):
26         self.client = Client()
27         VnfRegModel.objects.filter().delete()
28         self.vnfInst1 = {
29             "vnfInstId": "1",
30             "ip": "192.168.0.1",
31             "port": "2324",
32             "username": "admin",
33             "password": "admin123"
34         }
35         self.vnfInst1_new = {
36             "vnfInstId": "1",
37             "ip": "192.168.0.2",
38             "port": "2325",
39             "username": "admin1",
40             "password": "admin1234"
41         }
42         self.vnfconfig = {
43             "vnfInstanceId": "1",
44             "vnfConfigurationData": {
45                 "cp": [
46                     {
47                         "cpId": "cp-1",
48                         "cpdId": "cpd-a",
49                         "cpAddress": [
50                             {
51                                 "addresses": [
52                                     {
53                                         "addressType": "MAC",
54                                         "l2AddressData": "00:f3:43:20:a2:a3"
55                                     },
56                                     {
57                                         "addressType": "IP",
58                                         "l3AddressData": {
59                                             "iPAddressType": "IPv4",
60                                             "iPAddress": "192.168.104.2"
61                                         }
62                                     }
63                                 ],
64                                 "useDynamicAddress": "FALSE"
65                             }
66                         ]
67                     }
68                 ],
69                 "vnfSpecificData": {
70                     "autoScalable": "FALSE",
71                     "autoHealable": "FALSE"
72                 }
73             },
74             "vnfcConfigurationData": {
75                 "vnfcId": "vnfc-1",
76                 "cp": [
77                     {
78                         "cpId": "cp-11",
79                         "cpdId": "cpd-1a",
80                         "cpAddress": [
81                             {
82                                 "addresses": [
83                                     {
84                                         "addressType": "MAC",
85                                         "l2AddressData": "00:f3:43:21:a2:a3"
86                                     },
87                                     {
88                                         "addressType": "IP",
89                                         "l3AddressData": {
90                                             "iPAddressType": "IPv4",
91                                             "iPAddress": "192.168.105.2"
92                                         }
93                                     }
94                                 ],
95                                 "useDynamicAddress": "FALSE"
96                             }
97                         ]
98                     }
99                 ],
100                 "vnfcSpecificData": {}
101             }
102         }
103
104     def tearDown(self):
105         pass
106
107     def test_add_vnf_normal(self):
108         response = self.client.post("/api/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))
112         vnfInstActual = {
113             "vnfInstId": vnfs[0].id,
114             "ip": vnfs[0].ip,
115             "port": vnfs[0].port,
116             "username": vnfs[0].username,
117             "password": vnfs[0].password
118         }
119         self.assertEqual(self.vnfInst1, vnfInstActual)
120         
121     def test_add_vnf_when_duplicate(self):
122         self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
123         response = self.client.post("/api/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))
126         
127     def test_set_vnf_normal(self):
128         self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
129         response = self.client.put("/api/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))
134         vnfInstActual = {
135             "vnfInstId": vnfs[0].id,
136             "ip": vnfs[0].ip,
137             "port": vnfs[0].port,
138             "username": vnfs[0].username,
139             "password": vnfs[0].password
140         }
141         self.assertEqual(self.vnfInst1_new, vnfInstActual)
142         
143     def test_set_vnf_when_not_exist(self):
144         response = self.client.put("/api/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))
148         
149     def test_get_vnf_normal(self):
150         self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
151         response = self.client.get("/api/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))
154         
155     def test_get_vnf_when_not_exist(self):
156         response = self.client.get("/api/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))
159         
160     def test_del_vnf_normal(self):
161         self.client.post("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
162         response = self.client.delete("/api/vnfmgr/v1/vnfs/1")
163         self.assertEqual(status.HTTP_204_NO_CONTENT, response.status_code, response.content)
164         
165     def test_del_vnf_when_not_exist(self):
166         response = self.client.delete("/api/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))
169         
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("/api/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
174         response = self.client.post("/api/vnfmgr/v1/configuration", self.vnfconfig, format='json')
175         self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
176         
177     def test_vnf_config_when_not_exist(self):
178         response = self.client.post("/api/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))