|
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
|
2 |
#
|
|
3 |
# Redistribution and use in source and binary forms, with or
|
|
4 |
# without modification, are permitted provided that the following
|
|
5 |
# conditions are met:
|
|
6 |
#
|
|
7 |
# 1. Redistributions of source code must retain the above
|
|
8 |
# copyright notice, this list of conditions and the following
|
|
9 |
# disclaimer.
|
|
10 |
#
|
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
|
12 |
# copyright notice, this list of conditions and the following
|
|
13 |
# disclaimer in the documentation and/or other materials
|
|
14 |
# provided with the distribution.
|
|
15 |
#
|
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
|
28 |
#
|
|
29 |
# The views and conclusions contained in the software and
|
|
30 |
# documentation are those of the authors and should not be
|
|
31 |
# interpreted as representing official policies, either expressed
|
|
32 |
# or implied, of GRNET S.A.
|
|
33 |
|
|
34 |
from mock import patch, call
|
|
35 |
from unittest import TestCase
|
|
36 |
from itertools import product
|
|
37 |
from json import dumps
|
|
38 |
|
|
39 |
from kamaki.clients import ClientError, network
|
|
40 |
|
|
41 |
|
|
42 |
class NetworkRestClient(TestCase):
|
|
43 |
|
|
44 |
"""Set up a ComputesRest thorough test"""
|
|
45 |
def setUp(self):
|
|
46 |
self.url = 'http://network.example.com'
|
|
47 |
self.token = 'n2tw0rk70k3n'
|
|
48 |
self.client = network.NetworkRestClient(self.url, self.token)
|
|
49 |
|
|
50 |
def tearDown(self):
|
|
51 |
pass
|
|
52 |
|
|
53 |
def _assert(self, method_call, path, set_param=None, params=(), **kwargs):
|
|
54 |
"""Assert the REST method call is called as expected"""
|
|
55 |
x0 = - len(params)
|
|
56 |
for i, (k, v, c) in enumerate(params):
|
|
57 |
self.assertEqual(set_param.mock_calls[x0 + i], call(k, v, iff=c))
|
|
58 |
|
|
59 |
self.assertEqual(method_call.mock_calls[-1], call(path, **kwargs))
|
|
60 |
|
|
61 |
@patch('kamaki.clients.Client.get', return_value='ret val')
|
|
62 |
def test_networks_get(self, get):
|
|
63 |
for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
|
|
64 |
self.assertEqual(self.client.networks_get(**kwargs), 'ret val')
|
|
65 |
self._assert(get, '/networks', **kwargs)
|
|
66 |
|
|
67 |
netid = 'netid'
|
|
68 |
self.assertEqual(
|
|
69 |
self.client.networks_get(network_id=netid, **kwargs),
|
|
70 |
'ret val')
|
|
71 |
self._assert(get, '/networks/%s' % netid, **kwargs)
|
|
72 |
|
|
73 |
@patch('kamaki.clients.Client.set_param')
|
|
74 |
@patch('kamaki.clients.Client.post', return_value='ret val')
|
|
75 |
def test_networks_post(self, post, set_param):
|
|
76 |
for params, kwargs in product(
|
|
77 |
(
|
|
78 |
(('shared', False, None), ),
|
|
79 |
(('shared', True, True), )),
|
|
80 |
(dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
|
|
81 |
|
|
82 |
callargs = dict()
|
|
83 |
for p in params:
|
|
84 |
callargs[p[0]] = p[2]
|
|
85 |
callargs.update(kwargs)
|
|
86 |
|
|
87 |
self.assertEqual(self.client.networks_post(**callargs), 'ret val')
|
|
88 |
self._assert(
|
|
89 |
post, '/networks', set_param,
|
|
90 |
params=params, data=None, **kwargs)
|
|
91 |
|
|
92 |
json_data = dict(id='some id', other_param='other val')
|
|
93 |
callargs['json_data'] = json_data
|
|
94 |
self.assertEqual(self.client.networks_post(**callargs), 'ret val')
|
|
95 |
self._assert(
|
|
96 |
post, '/networks', set_param, params,
|
|
97 |
data=dumps(json_data), **kwargs)
|
|
98 |
|
|
99 |
@patch('kamaki.clients.Client.set_param')
|
|
100 |
@patch('kamaki.clients.Client.put', return_value='ret val')
|
|
101 |
def test_networks_put(self, put, set_param):
|
|
102 |
netid = 'netid'
|
|
103 |
for params, kwargs in product(
|
|
104 |
[p for p in product(
|
|
105 |
(
|
|
106 |
('admin_state_up', False, None),
|
|
107 |
('admin_state_up', True, True)),
|
|
108 |
(('shared', False, None), ('shared', True, True)),
|
|
109 |
)],
|
|
110 |
(dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
|
|
111 |
|
|
112 |
callargs = dict()
|
|
113 |
for p in params:
|
|
114 |
callargs[p[0]] = p[2]
|
|
115 |
callargs.update(kwargs)
|
|
116 |
|
|
117 |
self.assertEqual(
|
|
118 |
self.client.networks_put(netid, **callargs), 'ret val')
|
|
119 |
self._assert(
|
|
120 |
put, '/networks/%s' % netid, set_param, params,
|
|
121 |
data=None, **kwargs)
|
|
122 |
|
|
123 |
json_data = dict(id='some id', other_param='other val')
|
|
124 |
callargs['json_data'] = json_data
|
|
125 |
self.assertEqual(
|
|
126 |
self.client.networks_put(netid, **callargs), 'ret val')
|
|
127 |
self._assert(
|
|
128 |
put, '/networks/%s' % netid, set_param, params,
|
|
129 |
data=dumps(json_data), **kwargs)
|
|
130 |
|
|
131 |
@patch('kamaki.clients.Client.delete', return_value='ret val')
|
|
132 |
def test_networks_delete(self, delete):
|
|
133 |
netid = 'netid'
|
|
134 |
for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
|
|
135 |
self.assertEqual(
|
|
136 |
self.client.networks_delete(netid, **kwargs), 'ret val')
|
|
137 |
self._assert(delete, '/networks/%s' % netid, **kwargs)
|
|
138 |
|
|
139 |
@patch('kamaki.clients.Client.get', return_value='ret val')
|
|
140 |
def test_subnets_get(self, get):
|
|
141 |
for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
|
|
142 |
self.assertEqual(self.client.subnets_get(**kwargs), 'ret val')
|
|
143 |
self._assert(get, '/subnets', **kwargs)
|
|
144 |
|
|
145 |
subnet_id = 'subnet id'
|
|
146 |
self.assertEqual(
|
|
147 |
self.client.subnets_get(subnet_id=subnet_id, **kwargs),
|
|
148 |
'ret val')
|
|
149 |
self._assert(get, '/subnets/%s' % subnet_id, **kwargs)
|
|
150 |
|
|
151 |
@patch('kamaki.clients.Client.post', return_value='ret val')
|
|
152 |
def test_subnets_post(self, post):
|
|
153 |
for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
|
|
154 |
self.assertEqual(self.client.subnets_post(**kwargs), 'ret val')
|
|
155 |
self._assert(post, '/subnets', **kwargs)
|
|
156 |
|
|
157 |
@patch('kamaki.clients.Client.put', return_value='ret val')
|
|
158 |
def test_subnets_put(self, put):
|
|
159 |
subnet_id = 'subid'
|
|
160 |
for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
|
|
161 |
self.assertEqual(
|
|
162 |
self.client.subnets_put(subnet_id, **kwargs), 'ret val')
|
|
163 |
self._assert(put, '/subnets/%s' % subnet_id, **kwargs)
|
|
164 |
|
|
165 |
@patch('kamaki.clients.Client.delete', return_value='ret val')
|
|
166 |
def test_subnets_delete(self, delete):
|
|
167 |
netid = 'netid'
|
|
168 |
for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
|
|
169 |
self.assertEqual(
|
|
170 |
self.client.subnets_delete(netid, **kwargs), 'ret val')
|
|
171 |
self._assert(delete, '/subnets/%s' % netid, **kwargs)
|
|
172 |
|
|
173 |
@patch('kamaki.clients.Client.get', return_value='ret val')
|
|
174 |
def test_ports_get(self, get):
|
|
175 |
for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
|
|
176 |
self.assertEqual(self.client.ports_get(**kwargs), 'ret val')
|
|
177 |
self._assert(get, '/ports', **kwargs)
|
|
178 |
|
|
179 |
port_id = 'port id'
|
|
180 |
self.assertEqual(
|
|
181 |
self.client.ports_get(port_id=port_id, **kwargs),
|
|
182 |
'ret val')
|
|
183 |
self._assert(get, '/ports/%s' % port_id, **kwargs)
|
|
184 |
|
|
185 |
@patch('kamaki.clients.Client.set_param')
|
|
186 |
@patch('kamaki.clients.Client.post', return_value='ret val')
|
|
187 |
def test_ports_post(self, post, set_param):
|
|
188 |
for params, kwargs in product(
|
|
189 |
[p for p in product(
|
|
190 |
(
|
|
191 |
('name', 'port name', 'port name'),
|
|
192 |
('name', None, None)),
|
|
193 |
(
|
|
194 |
('mac_address', 'max address', 'max address'),
|
|
195 |
('mac_address', None, None)),
|
|
196 |
(
|
|
197 |
('fixed_ips', 'fixed ip', 'fixed ip'),
|
|
198 |
('fixed_ips', None, None)),
|
|
199 |
(
|
|
200 |
('security_groups', 'sec groups', 'sec groups'),
|
|
201 |
('security_groups', None, None))
|
|
202 |
)],
|
|
203 |
(dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
|
|
204 |
|
|
205 |
callargs = dict()
|
|
206 |
for p in params:
|
|
207 |
callargs[p[0]] = p[2]
|
|
208 |
callargs.update(kwargs)
|
|
209 |
|
|
210 |
self.assertEqual(self.client.ports_post(**callargs), 'ret val')
|
|
211 |
self._assert(
|
|
212 |
post, '/ports', set_param,
|
|
213 |
params=params, data=None, **kwargs)
|
|
214 |
|
|
215 |
json_data = dict(id='some id', other_param='other val')
|
|
216 |
callargs['json_data'] = json_data
|
|
217 |
self.assertEqual(self.client.ports_post(**callargs), 'ret val')
|
|
218 |
self._assert(
|
|
219 |
post, '/ports', set_param, params,
|
|
220 |
data=dumps(json_data), **kwargs)
|
|
221 |
|
|
222 |
@patch('kamaki.clients.Client.set_param')
|
|
223 |
@patch('kamaki.clients.Client.put', return_value='ret val')
|
|
224 |
def test_ports_put(self, put, set_param):
|
|
225 |
port_id = 'portid'
|
|
226 |
for params, kwargs in product(
|
|
227 |
[p for p in product(
|
|
228 |
(
|
|
229 |
('name', 'port name', 'port name'),
|
|
230 |
('name', None, None)),
|
|
231 |
(
|
|
232 |
('mac_address', 'max address', 'max address'),
|
|
233 |
('mac_address', None, None)),
|
|
234 |
(
|
|
235 |
('fixed_ips', 'fixed ip', 'fixed ip'),
|
|
236 |
('fixed_ips', None, None)),
|
|
237 |
(
|
|
238 |
('security_groups', 'sec groups', 'sec groups'),
|
|
239 |
('security_groups', None, None))
|
|
240 |
)],
|
|
241 |
(dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
|
|
242 |
|
|
243 |
callargs = dict()
|
|
244 |
for p in params:
|
|
245 |
callargs[p[0]] = p[2]
|
|
246 |
callargs.update(kwargs)
|
|
247 |
|
|
248 |
self.assertEqual(
|
|
249 |
self.client.ports_put(port_id, **callargs), 'ret val')
|
|
250 |
self._assert(
|
|
251 |
put, '/ports/%s' % port_id, set_param,
|
|
252 |
params=params, data=None, **kwargs)
|
|
253 |
|
|
254 |
json_data = dict(id='some id', other_param='other val')
|
|
255 |
callargs['json_data'] = json_data
|
|
256 |
self.assertEqual(
|
|
257 |
self.client.ports_put(port_id, **callargs), 'ret val')
|
|
258 |
self._assert(
|
|
259 |
put, '/ports/%s' % port_id, set_param, params,
|
|
260 |
data=dumps(json_data), **kwargs)
|
|
261 |
|
|
262 |
|
|
263 |
if __name__ == '__main__':
|
|
264 |
from sys import argv
|
|
265 |
from kamaki.clients.test import runTestCase
|
|
266 |
not_found = True
|
|
267 |
#if not argv[1:] or argv[1] == 'NetworkClient':
|
|
268 |
# not_found = False
|
|
269 |
# runTestCase(NetworkClient, 'Network Client', argv[2:])
|
|
270 |
if not argv[1:] or argv[1] == 'NetworkRest':
|
|
271 |
not_found = False
|
|
272 |
runTestCase(NetworkRestClient, 'NetworkRest Client', argv[2:])
|
|
273 |
if not_found:
|
|
274 |
print('TestCase %s not found' % argv[1])
|