Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / network / test.py @ e8ba3e9f

History | View | Annotate | Download (23.6 kB)

1 963bd664 Stavros Sachtouris
# Copyright 2013 GRNET S.A. All rights reserved.
2 963bd664 Stavros Sachtouris
#
3 963bd664 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 963bd664 Stavros Sachtouris
# without modification, are permitted provided that the following
5 963bd664 Stavros Sachtouris
# conditions are met:
6 963bd664 Stavros Sachtouris
#
7 963bd664 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 963bd664 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 963bd664 Stavros Sachtouris
#      disclaimer.
10 963bd664 Stavros Sachtouris
#
11 963bd664 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 963bd664 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 963bd664 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 963bd664 Stavros Sachtouris
#      provided with the distribution.
15 963bd664 Stavros Sachtouris
#
16 963bd664 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 963bd664 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 963bd664 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 963bd664 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 963bd664 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 963bd664 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 963bd664 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 963bd664 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 963bd664 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 963bd664 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 963bd664 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 963bd664 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 963bd664 Stavros Sachtouris
#
29 963bd664 Stavros Sachtouris
# The views and conclusions contained in the software and
30 963bd664 Stavros Sachtouris
# documentation are those of the authors and should not be
31 963bd664 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 963bd664 Stavros Sachtouris
# or implied, of GRNET S.A.
33 963bd664 Stavros Sachtouris
34 963bd664 Stavros Sachtouris
from mock import patch, call
35 963bd664 Stavros Sachtouris
from unittest import TestCase
36 963bd664 Stavros Sachtouris
from itertools import product
37 963bd664 Stavros Sachtouris
from json import dumps
38 963bd664 Stavros Sachtouris
39 5c433331 Stavros Sachtouris
from kamaki.clients import network
40 963bd664 Stavros Sachtouris
41 963bd664 Stavros Sachtouris
42 5c433331 Stavros Sachtouris
class NetworkRestClient(TestCase):
43 963bd664 Stavros Sachtouris
44 963bd664 Stavros Sachtouris
    """Set up a ComputesRest thorough test"""
45 963bd664 Stavros Sachtouris
    def setUp(self):
46 5c433331 Stavros Sachtouris
        self.url = 'http://network.example.com'
47 963bd664 Stavros Sachtouris
        self.token = 'n2tw0rk70k3n'
48 5c433331 Stavros Sachtouris
        self.client = network.NetworkRestClient(self.url, self.token)
49 963bd664 Stavros Sachtouris
50 963bd664 Stavros Sachtouris
    def tearDown(self):
51 058ee9a8 Stavros Sachtouris
        del self.client
52 963bd664 Stavros Sachtouris
53 963bd664 Stavros Sachtouris
    def _assert(self, method_call, path, set_param=None, params=(), **kwargs):
54 963bd664 Stavros Sachtouris
        """Assert the REST method call is called as expected"""
55 963bd664 Stavros Sachtouris
        x0 = - len(params)
56 963bd664 Stavros Sachtouris
        for i, (k, v, c) in enumerate(params):
57 963bd664 Stavros Sachtouris
            self.assertEqual(set_param.mock_calls[x0 + i], call(k, v, iff=c))
58 963bd664 Stavros Sachtouris
59 963bd664 Stavros Sachtouris
        self.assertEqual(method_call.mock_calls[-1], call(path, **kwargs))
60 963bd664 Stavros Sachtouris
61 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.get', return_value='ret val')
62 963bd664 Stavros Sachtouris
    def test_networks_get(self, get):
63 ba1ab65d Stavros Sachtouris
        netid = 'netid'
64 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
65 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.networks_get(**kwargs), 'ret val')
66 963bd664 Stavros Sachtouris
            self._assert(get, '/networks', **kwargs)
67 963bd664 Stavros Sachtouris
            self.assertEqual(
68 963bd664 Stavros Sachtouris
                self.client.networks_get(network_id=netid, **kwargs),
69 963bd664 Stavros Sachtouris
                'ret val')
70 963bd664 Stavros Sachtouris
            self._assert(get, '/networks/%s' % netid, **kwargs)
71 963bd664 Stavros Sachtouris
72 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.post', return_value='ret val')
73 ba1ab65d Stavros Sachtouris
    def test_networks_post(self, post):
74 ba1ab65d Stavros Sachtouris
        for kwargs in (
75 ba1ab65d Stavros Sachtouris
                dict(json_data=dict(k1='v1')),
76 ba1ab65d Stavros Sachtouris
                dict(json_data=dict(k2='v2'), k3='v3')):
77 ba1ab65d Stavros Sachtouris
            self.assertEqual(self.client.networks_post(**kwargs), 'ret val')
78 ba1ab65d Stavros Sachtouris
            json_data = kwargs.pop('json_data')
79 56f5c6fc Stavros Sachtouris
            self._assert(post, '/networks', json=json_data, **kwargs)
80 963bd664 Stavros Sachtouris
81 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.put', return_value='ret val')
82 ba1ab65d Stavros Sachtouris
    def test_networks_put(self, put):
83 963bd664 Stavros Sachtouris
        netid = 'netid'
84 ba1ab65d Stavros Sachtouris
        for kwargs in (
85 ba1ab65d Stavros Sachtouris
                dict(json_data=dict(k1='v1')),
86 ba1ab65d Stavros Sachtouris
                dict(json_data=dict(k2='v2'), k3='v3')):
87 963bd664 Stavros Sachtouris
            self.assertEqual(
88 ba1ab65d Stavros Sachtouris
                self.client.networks_put(netid, **kwargs), 'ret val')
89 ba1ab65d Stavros Sachtouris
            json_data = kwargs.pop('json_data')
90 963bd664 Stavros Sachtouris
            self._assert(
91 56f5c6fc Stavros Sachtouris
                put, '/networks/%s' % netid, json=json_data, **kwargs)
92 963bd664 Stavros Sachtouris
93 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.delete', return_value='ret val')
94 963bd664 Stavros Sachtouris
    def test_networks_delete(self, delete):
95 963bd664 Stavros Sachtouris
        netid = 'netid'
96 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
97 963bd664 Stavros Sachtouris
            self.assertEqual(
98 963bd664 Stavros Sachtouris
                self.client.networks_delete(netid, **kwargs), 'ret val')
99 963bd664 Stavros Sachtouris
            self._assert(delete, '/networks/%s' % netid, **kwargs)
100 963bd664 Stavros Sachtouris
101 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.get', return_value='ret val')
102 963bd664 Stavros Sachtouris
    def test_subnets_get(self, get):
103 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
104 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.subnets_get(**kwargs), 'ret val')
105 963bd664 Stavros Sachtouris
            self._assert(get, '/subnets', **kwargs)
106 963bd664 Stavros Sachtouris
107 963bd664 Stavros Sachtouris
            subnet_id = 'subnet id'
108 963bd664 Stavros Sachtouris
            self.assertEqual(
109 963bd664 Stavros Sachtouris
                self.client.subnets_get(subnet_id=subnet_id, **kwargs),
110 963bd664 Stavros Sachtouris
                'ret val')
111 963bd664 Stavros Sachtouris
            self._assert(get, '/subnets/%s' % subnet_id, **kwargs)
112 963bd664 Stavros Sachtouris
113 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.post', return_value='ret val')
114 963bd664 Stavros Sachtouris
    def test_subnets_post(self, post):
115 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
116 ba1ab65d Stavros Sachtouris
            json_data = dict(subnets='some data')
117 ba1ab65d Stavros Sachtouris
            self.assertEqual(self.client.subnets_post(
118 ba1ab65d Stavros Sachtouris
                json_data=json_data, **kwargs), 'ret val')
119 56f5c6fc Stavros Sachtouris
            self._assert(post, '/subnets', json=json_data, **kwargs)
120 963bd664 Stavros Sachtouris
121 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.put', return_value='ret val')
122 963bd664 Stavros Sachtouris
    def test_subnets_put(self, put):
123 963bd664 Stavros Sachtouris
        subnet_id = 'subid'
124 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
125 963bd664 Stavros Sachtouris
            self.assertEqual(
126 963bd664 Stavros Sachtouris
                self.client.subnets_put(subnet_id, **kwargs), 'ret val')
127 963bd664 Stavros Sachtouris
            self._assert(put, '/subnets/%s' % subnet_id, **kwargs)
128 963bd664 Stavros Sachtouris
129 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.delete', return_value='ret val')
130 963bd664 Stavros Sachtouris
    def test_subnets_delete(self, delete):
131 963bd664 Stavros Sachtouris
        netid = 'netid'
132 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
133 963bd664 Stavros Sachtouris
            self.assertEqual(
134 963bd664 Stavros Sachtouris
                self.client.subnets_delete(netid, **kwargs), 'ret val')
135 963bd664 Stavros Sachtouris
            self._assert(delete, '/subnets/%s' % netid, **kwargs)
136 963bd664 Stavros Sachtouris
137 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.get', return_value='ret val')
138 963bd664 Stavros Sachtouris
    def test_ports_get(self, get):
139 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
140 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.ports_get(**kwargs), 'ret val')
141 963bd664 Stavros Sachtouris
            self._assert(get, '/ports', **kwargs)
142 963bd664 Stavros Sachtouris
143 963bd664 Stavros Sachtouris
            port_id = 'port id'
144 963bd664 Stavros Sachtouris
            self.assertEqual(
145 963bd664 Stavros Sachtouris
                self.client.ports_get(port_id=port_id, **kwargs),
146 963bd664 Stavros Sachtouris
                'ret val')
147 963bd664 Stavros Sachtouris
            self._assert(get, '/ports/%s' % port_id, **kwargs)
148 963bd664 Stavros Sachtouris
149 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.post', return_value='ret val')
150 447365fe Stavros Sachtouris
    def test_ports_post(self, post):
151 447365fe Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
152 447365fe Stavros Sachtouris
            self.assertEqual(self.client.ports_post(**kwargs), 'ret val')
153 447365fe Stavros Sachtouris
            self._assert(post, '/ports', json=None, **kwargs)
154 963bd664 Stavros Sachtouris
155 963bd664 Stavros Sachtouris
            json_data = dict(id='some id', other_param='other val')
156 447365fe Stavros Sachtouris
            self.assertEqual(
157 447365fe Stavros Sachtouris
                self.client.ports_post(json_data=json_data, **kwargs),
158 447365fe Stavros Sachtouris
                'ret val')
159 447365fe Stavros Sachtouris
            self._assert(post, '/ports', json=json_data, **kwargs)
160 963bd664 Stavros Sachtouris
161 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.put', return_value='ret val')
162 447365fe Stavros Sachtouris
    def test_ports_put(self, put):
163 963bd664 Stavros Sachtouris
        port_id = 'portid'
164 447365fe Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
165 963bd664 Stavros Sachtouris
            self.assertEqual(
166 447365fe Stavros Sachtouris
                self.client.ports_put(port_id, **kwargs), 'ret val')
167 447365fe Stavros Sachtouris
            self._assert(put, '/ports/%s' % port_id, json=None, **kwargs)
168 963bd664 Stavros Sachtouris
169 963bd664 Stavros Sachtouris
            json_data = dict(id='some id', other_param='other val')
170 963bd664 Stavros Sachtouris
            self.assertEqual(
171 447365fe Stavros Sachtouris
                self.client.ports_put(port_id, json_data=json_data, **kwargs),
172 447365fe Stavros Sachtouris
                'ret val')
173 447365fe Stavros Sachtouris
            self._assert(put, '/ports/%s' % port_id, json=json_data, **kwargs)
174 963bd664 Stavros Sachtouris
175 1ad949a7 Stavros Sachtouris
    @patch('kamaki.clients.Client.get', return_value='ret val')
176 1ad949a7 Stavros Sachtouris
    def test_floatingips_get(self, get):
177 1ad949a7 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
178 1ad949a7 Stavros Sachtouris
            self.assertEqual(self.client.floatingips_get(**kwargs), 'ret val')
179 1ad949a7 Stavros Sachtouris
            self._assert(get, '/floatingips', **kwargs)
180 1ad949a7 Stavros Sachtouris
181 1ad949a7 Stavros Sachtouris
            floatingip_id = 'port id'
182 1ad949a7 Stavros Sachtouris
            self.assertEqual(
183 1ad949a7 Stavros Sachtouris
                self.client.floatingips_get(
184 1ad949a7 Stavros Sachtouris
                    floatingip_id=floatingip_id, **kwargs),
185 1ad949a7 Stavros Sachtouris
                'ret val')
186 1ad949a7 Stavros Sachtouris
            self._assert(get, '/floatingips/%s' % floatingip_id, **kwargs)
187 1ad949a7 Stavros Sachtouris
188 1ad949a7 Stavros Sachtouris
    @patch('kamaki.clients.Client.post', return_value='ret val')
189 1ad949a7 Stavros Sachtouris
    def test_floatingips_post(self, post):
190 1ad949a7 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
191 1ad949a7 Stavros Sachtouris
            json_data = dict(id='some id', other_param='other val')
192 1ad949a7 Stavros Sachtouris
            self.assertEqual(
193 1ad949a7 Stavros Sachtouris
                self.client.floatingips_post(json_data=json_data, **kwargs),
194 1ad949a7 Stavros Sachtouris
                'ret val')
195 1ad949a7 Stavros Sachtouris
            self._assert(post, '/floatingips', json=json_data, **kwargs)
196 1ad949a7 Stavros Sachtouris
197 1ad949a7 Stavros Sachtouris
    @patch('kamaki.clients.Client.put', return_value='ret val')
198 1ad949a7 Stavros Sachtouris
    def test_floatingips_put(self, put):
199 1ad949a7 Stavros Sachtouris
        floatingip_id = 'portid'
200 1ad949a7 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
201 1ad949a7 Stavros Sachtouris
            json_data = dict(id='some id', other_param='other val')
202 1ad949a7 Stavros Sachtouris
            self.assertEqual(
203 1ad949a7 Stavros Sachtouris
                self.client.floatingips_put(
204 1ad949a7 Stavros Sachtouris
                    floatingip_id, json_data=json_data, **kwargs),
205 1ad949a7 Stavros Sachtouris
                'ret val')
206 1ad949a7 Stavros Sachtouris
            self._assert(
207 1ad949a7 Stavros Sachtouris
                put, '/floatingips/%s' % floatingip_id,
208 1ad949a7 Stavros Sachtouris
                json=json_data, **kwargs)
209 1ad949a7 Stavros Sachtouris
210 963bd664 Stavros Sachtouris
211 058ee9a8 Stavros Sachtouris
class FakeObject(object):
212 058ee9a8 Stavros Sachtouris
213 058ee9a8 Stavros Sachtouris
    json = None
214 afba96a7 Stavros Sachtouris
    headers = None
215 058ee9a8 Stavros Sachtouris
216 058ee9a8 Stavros Sachtouris
217 5c433331 Stavros Sachtouris
class NetworkClient(TestCase):
218 058ee9a8 Stavros Sachtouris
219 058ee9a8 Stavros Sachtouris
    """Set up a ComputesRest thorough test"""
220 058ee9a8 Stavros Sachtouris
    def setUp(self):
221 058ee9a8 Stavros Sachtouris
        self.url = 'http://network.example.com'
222 058ee9a8 Stavros Sachtouris
        self.token = 'n2tw0rk70k3n'
223 5c433331 Stavros Sachtouris
        self.client = network.NetworkClient(self.url, self.token)
224 058ee9a8 Stavros Sachtouris
225 058ee9a8 Stavros Sachtouris
    def tearDown(self):
226 ba1ab65d Stavros Sachtouris
        FakeObject.json, FakeObject.headers = None, None
227 058ee9a8 Stavros Sachtouris
        del self.client
228 058ee9a8 Stavros Sachtouris
229 058ee9a8 Stavros Sachtouris
    @patch(
230 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.networks_get',
231 058ee9a8 Stavros Sachtouris
        return_value=FakeObject())
232 058ee9a8 Stavros Sachtouris
    def test_list_networks(self, networks_get):
233 058ee9a8 Stavros Sachtouris
        FakeObject.json = dict(networks='ret val')
234 058ee9a8 Stavros Sachtouris
        self.assertEqual(self.client.list_networks(), 'ret val')
235 058ee9a8 Stavros Sachtouris
        networks_get.assert_called_once_with(success=200)
236 058ee9a8 Stavros Sachtouris
237 058ee9a8 Stavros Sachtouris
    @patch(
238 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.networks_post',
239 058ee9a8 Stavros Sachtouris
        return_value=FakeObject())
240 058ee9a8 Stavros Sachtouris
    def test_create_network(self, networks_post):
241 ba1ab65d Stavros Sachtouris
        for admin_state_up, shared in product((None, True), (None, True)):
242 afba96a7 Stavros Sachtouris
            FakeObject.json = dict(network='ret val')
243 ba1ab65d Stavros Sachtouris
            name = 'net name'
244 ba1ab65d Stavros Sachtouris
            self.assertEqual(
245 ba1ab65d Stavros Sachtouris
                self.client.create_network(
246 ba1ab65d Stavros Sachtouris
                    name, admin_state_up=admin_state_up, shared=shared),
247 ba1ab65d Stavros Sachtouris
                'ret val')
248 ba1ab65d Stavros Sachtouris
            req = dict(name=name, admin_state_up=bool(admin_state_up))
249 ba1ab65d Stavros Sachtouris
            if shared:
250 ba1ab65d Stavros Sachtouris
                req['shared'] = shared
251 ba1ab65d Stavros Sachtouris
            expargs = dict(json_data=dict(network=req), success=201)
252 ba1ab65d Stavros Sachtouris
            self.assertEqual(networks_post.mock_calls[-1], call(**expargs))
253 058ee9a8 Stavros Sachtouris
254 afba96a7 Stavros Sachtouris
    @patch(
255 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.networks_post',
256 afba96a7 Stavros Sachtouris
        return_value=FakeObject())
257 afba96a7 Stavros Sachtouris
    def test_create_networks(self, networks_post):
258 ba1ab65d Stavros Sachtouris
        for networks in (
259 ba1ab65d Stavros Sachtouris
                None, dict(name='name'), 'nets', [1, 2, 3], [{'k': 'v'}, ],
260 ba1ab65d Stavros Sachtouris
                [dict(admin_state_up=True, shared=True)],
261 ba1ab65d Stavros Sachtouris
                [dict(name='n1', invalid='mistake'), ],
262 ba1ab65d Stavros Sachtouris
                [dict(name='valid', shared=True), {'err': 'nop'}]):
263 afba96a7 Stavros Sachtouris
            self.assertRaises(
264 ba1ab65d Stavros Sachtouris
                ValueError, self.client.create_networks, networks)
265 afba96a7 Stavros Sachtouris
266 afba96a7 Stavros Sachtouris
        FakeObject.json = dict(networks='ret val')
267 ba1ab65d Stavros Sachtouris
        for networks in (
268 ba1ab65d Stavros Sachtouris
                [
269 ba1ab65d Stavros Sachtouris
                    dict(name='net1'),
270 ba1ab65d Stavros Sachtouris
                    dict(name='net 2', admin_state_up=False, shared=True)],
271 ba1ab65d Stavros Sachtouris
                [
272 ba1ab65d Stavros Sachtouris
                    dict(name='net1', admin_state_up=True),
273 ba1ab65d Stavros Sachtouris
                    dict(name='net 2', shared=False),
274 ba1ab65d Stavros Sachtouris
                    dict(name='net-3')],
275 ba1ab65d Stavros Sachtouris
                (dict(name='n.e.t'), dict(name='net 2'))):
276 ba1ab65d Stavros Sachtouris
            self.assertEqual(self.client.create_networks(networks), 'ret val')
277 afba96a7 Stavros Sachtouris
278 afba96a7 Stavros Sachtouris
            networks = list(networks)
279 afba96a7 Stavros Sachtouris
            expargs = dict(json_data=dict(networks=networks), success=201)
280 afba96a7 Stavros Sachtouris
            self.assertEqual(networks_post.mock_calls[-1], call(**expargs))
281 afba96a7 Stavros Sachtouris
282 afba96a7 Stavros Sachtouris
    @patch(
283 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.networks_get',
284 afba96a7 Stavros Sachtouris
        return_value=FakeObject())
285 afba96a7 Stavros Sachtouris
    def test_get_network_details(self, networks_get):
286 afba96a7 Stavros Sachtouris
        netid, FakeObject.json = 'netid', dict(network='ret val')
287 afba96a7 Stavros Sachtouris
        self.assertEqual(self.client.get_network_details(netid), 'ret val')
288 afba96a7 Stavros Sachtouris
        networks_get.assert_called_once_with(netid, success=200)
289 afba96a7 Stavros Sachtouris
290 afba96a7 Stavros Sachtouris
    @patch(
291 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.networks_put',
292 afba96a7 Stavros Sachtouris
        return_value=FakeObject())
293 afba96a7 Stavros Sachtouris
    def test_update_network(self, networks_put):
294 afba96a7 Stavros Sachtouris
        netid, FakeObject.json = 'netid', dict(network='ret val')
295 afba96a7 Stavros Sachtouris
        for name, admin_state_up, shared in product(
296 afba96a7 Stavros Sachtouris
                ('net name', None), (True, None), (True, None)):
297 afba96a7 Stavros Sachtouris
            kwargs = dict(
298 afba96a7 Stavros Sachtouris
                name=name, admin_state_up=admin_state_up, shared=shared)
299 afba96a7 Stavros Sachtouris
            self.assertEqual(
300 afba96a7 Stavros Sachtouris
                self.client.update_network(netid, **kwargs), 'ret val')
301 ba1ab65d Stavros Sachtouris
            if name in (None, ):
302 ba1ab65d Stavros Sachtouris
                kwargs.pop('name')
303 ba1ab65d Stavros Sachtouris
            if admin_state_up in (None, ):
304 ba1ab65d Stavros Sachtouris
                kwargs.pop('admin_state_up')
305 ba1ab65d Stavros Sachtouris
            if shared in (None, ):
306 ba1ab65d Stavros Sachtouris
                kwargs.pop('shared')
307 ba1ab65d Stavros Sachtouris
            kwargs = dict(json_data=dict(network=kwargs), success=200)
308 afba96a7 Stavros Sachtouris
            self.assertEqual(
309 afba96a7 Stavros Sachtouris
                networks_put.mock_calls[-1], call(netid, **kwargs))
310 afba96a7 Stavros Sachtouris
311 afba96a7 Stavros Sachtouris
    @patch(
312 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.networks_delete',
313 afba96a7 Stavros Sachtouris
        return_value=FakeObject())
314 afba96a7 Stavros Sachtouris
    def test_delete_network(self, networks_delete):
315 afba96a7 Stavros Sachtouris
        netid, FakeObject.headers = 'netid', 'ret headers'
316 afba96a7 Stavros Sachtouris
        self.assertEqual(self.client.delete_network(netid), 'ret headers')
317 afba96a7 Stavros Sachtouris
        networks_delete.assert_called_once_with(netid, success=204)
318 afba96a7 Stavros Sachtouris
319 ba1ab65d Stavros Sachtouris
    @patch(
320 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.subnets_get',
321 ba1ab65d Stavros Sachtouris
        return_value=FakeObject())
322 ba1ab65d Stavros Sachtouris
    def test_list_subnets(self, subnets_get):
323 ba1ab65d Stavros Sachtouris
        FakeObject.json = dict(subnets='ret val')
324 ba1ab65d Stavros Sachtouris
        self.assertEqual(self.client.list_subnets(), 'ret val')
325 ba1ab65d Stavros Sachtouris
        subnets_get.assert_called_once_with(success=200)
326 ba1ab65d Stavros Sachtouris
327 25d2ff8e Stavros Sachtouris
    @patch(
328 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.subnets_post',
329 25d2ff8e Stavros Sachtouris
        return_value=FakeObject())
330 25d2ff8e Stavros Sachtouris
    def test_create_subnet(self, subnets_post):
331 25d2ff8e Stavros Sachtouris
        for (
332 25d2ff8e Stavros Sachtouris
                name, allocation_pools, gateway_ip,
333 25d2ff8e Stavros Sachtouris
                subnet_id, ipv6, enable_dhcp) in product(
334 25d2ff8e Stavros Sachtouris
                    ('name', None), ('all pools', None), ('gip', None),
335 25d2ff8e Stavros Sachtouris
                    ('sid', None), (True, None), (True, None)):
336 25d2ff8e Stavros Sachtouris
            kwargs = dict(
337 25d2ff8e Stavros Sachtouris
                name=name, allocation_pools=allocation_pools,
338 25d2ff8e Stavros Sachtouris
                gateway_ip=gateway_ip, subnet_id=subnet_id,
339 25d2ff8e Stavros Sachtouris
                ipv6=ipv6, enable_dhcp=enable_dhcp)
340 25d2ff8e Stavros Sachtouris
            FakeObject.json, network_id, cidr = dict(subnet='rv'), 'name', 'cd'
341 25d2ff8e Stavros Sachtouris
            self.assertEqual(
342 25d2ff8e Stavros Sachtouris
                self.client.create_subnet(network_id, cidr, **kwargs), 'rv')
343 25d2ff8e Stavros Sachtouris
            req = dict(
344 25d2ff8e Stavros Sachtouris
                network_id=network_id, cidr=cidr,
345 25d2ff8e Stavros Sachtouris
                ip_version=6 if kwargs.pop('ipv6', None) else 4)
346 25d2ff8e Stavros Sachtouris
            for k, v in kwargs.items():
347 25d2ff8e Stavros Sachtouris
                if v:
348 25d2ff8e Stavros Sachtouris
                    req['id' if k == 'subnet_id' else k] = v
349 25d2ff8e Stavros Sachtouris
            expargs = dict(json_data=dict(subnet=req), success=201)
350 25d2ff8e Stavros Sachtouris
            self.assertEqual(subnets_post.mock_calls[-1], call(**expargs))
351 25d2ff8e Stavros Sachtouris
352 25d2ff8e Stavros Sachtouris
    @patch(
353 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.subnets_post',
354 25d2ff8e Stavros Sachtouris
        return_value=FakeObject())
355 25d2ff8e Stavros Sachtouris
    def test_create_subnets(self, subnets_post):
356 25d2ff8e Stavros Sachtouris
        for subnets in (
357 25d2ff8e Stavros Sachtouris
                None, dict(network_id='name'), 'nets', [1, 2, 3], [{'k': 'v'}],
358 25d2ff8e Stavros Sachtouris
                [dict(ipv6=True, enable_dhcp=True)],
359 25d2ff8e Stavros Sachtouris
                [dict(network_id='n1', cidr='dr', invalid='mistake'), ],
360 25d2ff8e Stavros Sachtouris
                [dict(network_id='valid', cidr='valid'), {'err': 'nop'}]):
361 25d2ff8e Stavros Sachtouris
            self.assertRaises(
362 25d2ff8e Stavros Sachtouris
                ValueError, self.client.create_subnets, subnets)
363 25d2ff8e Stavros Sachtouris
364 25d2ff8e Stavros Sachtouris
        FakeObject.json = dict(subnets='ret val')
365 25d2ff8e Stavros Sachtouris
        for subnets in (
366 25d2ff8e Stavros Sachtouris
                [
367 25d2ff8e Stavros Sachtouris
                    dict(network_id='n1', cidr='c1'),
368 25d2ff8e Stavros Sachtouris
                    dict(network_id='n 2', cidr='c 2', name='name')],
369 25d2ff8e Stavros Sachtouris
                [
370 25d2ff8e Stavros Sachtouris
                    dict(network_id='n1', cidr='c 6', allocation_pools='a p'),
371 25d2ff8e Stavros Sachtouris
                    dict(network_id='n 2', cidr='c_4', gateway_ip='g ip'),
372 25d2ff8e Stavros Sachtouris
                    dict(network_id='n 2', cidr='c_4', subnet_id='s id'),
373 25d2ff8e Stavros Sachtouris
                    dict(network_id='n-4', cidr='c3', ipv6=True, name='w. 6'),
374 25d2ff8e Stavros Sachtouris
                    dict(network_id='n_5', cidr='c2', enable_dhcp=True)],
375 25d2ff8e Stavros Sachtouris
                (
376 25d2ff8e Stavros Sachtouris
                    dict(network_id='n.e.t', cidr='c-5'),
377 25d2ff8e Stavros Sachtouris
                    dict(network_id='net 2', cidr='c 2'))):
378 25d2ff8e Stavros Sachtouris
            self.assertEqual(self.client.create_subnets(subnets), 'ret val')
379 25d2ff8e Stavros Sachtouris
380 25d2ff8e Stavros Sachtouris
            for subnet in subnets:
381 25d2ff8e Stavros Sachtouris
                subnet['ip_version'] = 6 if subnet.pop('ipv6', None) else 4
382 25d2ff8e Stavros Sachtouris
                if 'subnet_id' in subnet:
383 25d2ff8e Stavros Sachtouris
                    subnet['id'] = subnet.pop('subnet_id')
384 25d2ff8e Stavros Sachtouris
            subnets = list(subnets)
385 25d2ff8e Stavros Sachtouris
            expargs = dict(json_data=dict(subnets=subnets), success=201)
386 25d2ff8e Stavros Sachtouris
            self.assertEqual(subnets_post.mock_calls[-1], call(**expargs))
387 25d2ff8e Stavros Sachtouris
388 25d2ff8e Stavros Sachtouris
    @patch(
389 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.subnets_get',
390 25d2ff8e Stavros Sachtouris
        return_value=FakeObject())
391 25d2ff8e Stavros Sachtouris
    def test_get_subnet_details(self, subnets_get):
392 25d2ff8e Stavros Sachtouris
        subid, FakeObject.json = 'subid', 'ret val'
393 25d2ff8e Stavros Sachtouris
        self.assertEqual(self.client.get_subnet_details(subid), 'ret val')
394 25d2ff8e Stavros Sachtouris
        subnets_get.assert_called_once_with(subid, success=201)
395 25d2ff8e Stavros Sachtouris
396 25d2ff8e Stavros Sachtouris
    @patch(
397 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.subnets_put',
398 25d2ff8e Stavros Sachtouris
        return_value=FakeObject())
399 25d2ff8e Stavros Sachtouris
    def test_update_subnet(self, subnets_put):
400 25d2ff8e Stavros Sachtouris
        for (
401 25d2ff8e Stavros Sachtouris
                name, allocation_pools, gateway_ip,
402 25d2ff8e Stavros Sachtouris
                subnet_id, ipv6, enable_dhcp) in product(
403 25d2ff8e Stavros Sachtouris
                    ('name', None), ('all pools', None), ('gip', None),
404 25d2ff8e Stavros Sachtouris
                    ('sid', None), (True, False, None), (True, False, None)):
405 25d2ff8e Stavros Sachtouris
            kwargs = dict(
406 25d2ff8e Stavros Sachtouris
                name=name, allocation_pools=allocation_pools,
407 25d2ff8e Stavros Sachtouris
                gateway_ip=gateway_ip, subnet_id=subnet_id,
408 25d2ff8e Stavros Sachtouris
                ipv6=ipv6, enable_dhcp=enable_dhcp)
409 25d2ff8e Stavros Sachtouris
            FakeObject.json, network_id, cidr = dict(subnet='rv'), 'name', 'cd'
410 25d2ff8e Stavros Sachtouris
            self.assertEqual(
411 25d2ff8e Stavros Sachtouris
                self.client.update_subnet(network_id, cidr, **kwargs), 'rv')
412 25d2ff8e Stavros Sachtouris
            req = dict(network_id=network_id, cidr=cidr)
413 25d2ff8e Stavros Sachtouris
            if kwargs.get('ipv6', None) not in (None, ):
414 25d2ff8e Stavros Sachtouris
                req['ip_version'] = 6 if kwargs.pop('ipv6') else 4
415 25d2ff8e Stavros Sachtouris
            for k, v in kwargs.items():
416 25d2ff8e Stavros Sachtouris
                if v not in (None, ):
417 25d2ff8e Stavros Sachtouris
                    req['id' if k == 'subnet_id' else k] = v
418 25d2ff8e Stavros Sachtouris
            expargs = dict(json_data=dict(subnet=req), success=201)
419 25d2ff8e Stavros Sachtouris
            self.assertEqual(subnets_put.mock_calls[-1], call(**expargs))
420 25d2ff8e Stavros Sachtouris
421 25d2ff8e Stavros Sachtouris
    @patch(
422 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.subnets_delete',
423 25d2ff8e Stavros Sachtouris
        return_value=FakeObject())
424 25d2ff8e Stavros Sachtouris
    def test_delete_subnet(self, subnets_delete):
425 25d2ff8e Stavros Sachtouris
        netid, FakeObject.headers = 'netid', 'ret headers'
426 25d2ff8e Stavros Sachtouris
        self.assertEqual(self.client.delete_subnet(netid), 'ret headers')
427 25d2ff8e Stavros Sachtouris
        subnets_delete.assert_called_once_with(netid, success=204)
428 25d2ff8e Stavros Sachtouris
429 bac9c8ac Stavros Sachtouris
    @patch(
430 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.ports_get',
431 bac9c8ac Stavros Sachtouris
        return_value=FakeObject())
432 bac9c8ac Stavros Sachtouris
    def test_list_ports(self, ports_get):
433 bac9c8ac Stavros Sachtouris
        FakeObject.json = dict(ports='ret val')
434 bac9c8ac Stavros Sachtouris
        self.assertEqual(self.client.list_ports(), 'ret val')
435 bac9c8ac Stavros Sachtouris
        ports_get.assert_called_once_with(success=200)
436 bac9c8ac Stavros Sachtouris
437 bac9c8ac Stavros Sachtouris
    @patch(
438 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.ports_post',
439 bac9c8ac Stavros Sachtouris
        return_value=FakeObject())
440 bac9c8ac Stavros Sachtouris
    def test_create_port(self, ports_post):
441 bac9c8ac Stavros Sachtouris
        for (
442 bac9c8ac Stavros Sachtouris
                name, status, admin_state_up,
443 bac9c8ac Stavros Sachtouris
                mac_address, fixed_ips, security_groups
444 bac9c8ac Stavros Sachtouris
                ) in product(
445 bac9c8ac Stavros Sachtouris
                    ('name', None), ('status', None), (True, False, None),
446 bac9c8ac Stavros Sachtouris
                    ('maddr', None), ('some ips', None), ([1, 2, 3], None)):
447 bac9c8ac Stavros Sachtouris
            kwargs = dict(
448 bac9c8ac Stavros Sachtouris
                name=name, status=status, admin_state_up=admin_state_up,
449 bac9c8ac Stavros Sachtouris
                mac_address=mac_address, fixed_ips=fixed_ips,
450 bac9c8ac Stavros Sachtouris
                security_groups=security_groups)
451 bac9c8ac Stavros Sachtouris
            FakeObject.json, network_id = dict(port='ret val'), 'name'
452 bac9c8ac Stavros Sachtouris
            self.assertEqual(
453 bac9c8ac Stavros Sachtouris
                self.client.create_port(network_id, **kwargs), 'ret val')
454 bac9c8ac Stavros Sachtouris
            req = dict(network_id=network_id)
455 bac9c8ac Stavros Sachtouris
            for k, v in kwargs.items():
456 bac9c8ac Stavros Sachtouris
                if v not in (None, ):
457 bac9c8ac Stavros Sachtouris
                    req[k] = v
458 bac9c8ac Stavros Sachtouris
            expargs = dict(json_data=dict(port=req), success=201)
459 bac9c8ac Stavros Sachtouris
            self.assertEqual(ports_post.mock_calls[-1], call(**expargs))
460 bac9c8ac Stavros Sachtouris
461 bac9c8ac Stavros Sachtouris
    @patch(
462 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.ports_post',
463 bac9c8ac Stavros Sachtouris
        return_value=FakeObject())
464 bac9c8ac Stavros Sachtouris
    def test_create_ports(self, ports_post):
465 bac9c8ac Stavros Sachtouris
        for ports in (
466 bac9c8ac Stavros Sachtouris
                None, dict(network_id='name'), 'nets', [1, 2, 3], [{'k': 'v'}],
467 bac9c8ac Stavros Sachtouris
                [dict(name=True, mac_address='mac')],
468 bac9c8ac Stavros Sachtouris
                [dict(network_id='n1', invalid='mistake'), ],
469 bac9c8ac Stavros Sachtouris
                [dict(network_id='valid', name='valid'), {'err': 'nop'}]):
470 bac9c8ac Stavros Sachtouris
            self.assertRaises(
471 bac9c8ac Stavros Sachtouris
                ValueError, self.client.create_ports, ports)
472 bac9c8ac Stavros Sachtouris
473 bac9c8ac Stavros Sachtouris
        FakeObject.json = dict(ports='ret val')
474 bac9c8ac Stavros Sachtouris
        for ports in (
475 bac9c8ac Stavros Sachtouris
                [dict(network_id='n1'), dict(network_id='n 2', name='name')],
476 bac9c8ac Stavros Sachtouris
                [
477 bac9c8ac Stavros Sachtouris
                    dict(network_id='n1', name='n 6', status='status'),
478 bac9c8ac Stavros Sachtouris
                    dict(network_id='n 2', admin_state_up=True, fixed_ips='f'),
479 bac9c8ac Stavros Sachtouris
                    dict(network_id='n 2', mac_address='mc', name='n.a.m.e.'),
480 bac9c8ac Stavros Sachtouris
                    dict(network_id='n-4', security_groups='s~G', name='w. 6'),
481 bac9c8ac Stavros Sachtouris
                    dict(network_id='n_5', admin_state_up=False, name='f a')],
482 bac9c8ac Stavros Sachtouris
                (
483 bac9c8ac Stavros Sachtouris
                    dict(network_id='n.e.t', name='c-5'),
484 bac9c8ac Stavros Sachtouris
                    dict(network_id='net 2', status='YEAH'))):
485 bac9c8ac Stavros Sachtouris
            self.assertEqual(self.client.create_ports(ports), 'ret val')
486 bac9c8ac Stavros Sachtouris
            expargs = dict(json_data=dict(ports=list(ports)), success=201)
487 bac9c8ac Stavros Sachtouris
            self.assertEqual(ports_post.mock_calls[-1], call(**expargs))
488 bac9c8ac Stavros Sachtouris
489 bac9c8ac Stavros Sachtouris
    @patch(
490 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.ports_get',
491 bac9c8ac Stavros Sachtouris
        return_value=FakeObject())
492 bac9c8ac Stavros Sachtouris
    def test_get_port_details(self, ports_get):
493 bac9c8ac Stavros Sachtouris
        portid, FakeObject.json = 'portid', dict(ports='ret val')
494 bac9c8ac Stavros Sachtouris
        self.assertEqual(self.client.get_port_details(portid), 'ret val')
495 bac9c8ac Stavros Sachtouris
        ports_get.assert_called_once_with(portid, success=201)
496 bac9c8ac Stavros Sachtouris
497 bac9c8ac Stavros Sachtouris
    @patch(
498 5c433331 Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.ports_delete',
499 bac9c8ac Stavros Sachtouris
        return_value=FakeObject())
500 bac9c8ac Stavros Sachtouris
    def test_delete_port(self, ports_delete):
501 bac9c8ac Stavros Sachtouris
        portid, FakeObject.headers = 'portid', 'ret headers'
502 bac9c8ac Stavros Sachtouris
        self.assertEqual(self.client.delete_port(portid), 'ret headers')
503 bac9c8ac Stavros Sachtouris
        ports_delete.assert_called_once_with(portid, success=204)
504 bac9c8ac Stavros Sachtouris
505 447365fe Stavros Sachtouris
    @patch(
506 447365fe Stavros Sachtouris
        'kamaki.clients.network.NetworkClient.ports_put',
507 447365fe Stavros Sachtouris
        return_value=FakeObject())
508 447365fe Stavros Sachtouris
    def test_update_port(self, ports_put):
509 447365fe Stavros Sachtouris
        for (
510 447365fe Stavros Sachtouris
                name, status, admin_state_up, mac_address, fixed_ips,
511 447365fe Stavros Sachtouris
                security_groups) in product(
512 447365fe Stavros Sachtouris
                    ('name', None), ('st', None), (True, None), ('mc', None),
513 447365fe Stavros Sachtouris
                    ('fps', None), ('sg', None)):
514 447365fe Stavros Sachtouris
            FakeObject.json = dict(port='rv')
515 447365fe Stavros Sachtouris
            port_id, network_id = 'pid', 'nid'
516 447365fe Stavros Sachtouris
            kwargs = dict(
517 447365fe Stavros Sachtouris
                network_id=network_id, name=name, status=status,
518 447365fe Stavros Sachtouris
                admin_state_up=admin_state_up, mac_address=mac_address,
519 447365fe Stavros Sachtouris
                fixed_ips=fixed_ips, security_groups=security_groups)
520 447365fe Stavros Sachtouris
            self.assertEqual(
521 447365fe Stavros Sachtouris
                self.client.update_port(port_id, **kwargs), 'rv')
522 447365fe Stavros Sachtouris
            req = dict()
523 447365fe Stavros Sachtouris
            for k, v in kwargs.items():
524 447365fe Stavros Sachtouris
                if v:
525 447365fe Stavros Sachtouris
                    req[k] = v
526 447365fe Stavros Sachtouris
            expargs = dict(json_data=dict(port=req), success=201)
527 447365fe Stavros Sachtouris
            self.assertEqual(
528 447365fe Stavros Sachtouris
                ports_put.mock_calls[-1], call(port_id, **expargs))
529 447365fe Stavros Sachtouris
530 058ee9a8 Stavros Sachtouris
531 963bd664 Stavros Sachtouris
if __name__ == '__main__':
532 963bd664 Stavros Sachtouris
    from sys import argv
533 963bd664 Stavros Sachtouris
    from kamaki.clients.test import runTestCase
534 963bd664 Stavros Sachtouris
    not_found = True
535 5c433331 Stavros Sachtouris
    if not argv[1:] or argv[1] == 'NetworkClient':
536 058ee9a8 Stavros Sachtouris
        not_found = False
537 5c433331 Stavros Sachtouris
        runTestCase(NetworkClient, 'Network Client', argv[2:])
538 5c433331 Stavros Sachtouris
    if not argv[1:] or argv[1] == 'NetworkRestClient':
539 963bd664 Stavros Sachtouris
        not_found = False
540 5c433331 Stavros Sachtouris
        runTestCase(NetworkRestClient, 'NetworkRest Client', argv[2:])
541 963bd664 Stavros Sachtouris
    if not_found:
542 963bd664 Stavros Sachtouris
        print('TestCase %s not found' % argv[1])