Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / network / test.py @ 963bd664

History | View | Annotate | Download (11.1 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 963bd664 Stavros Sachtouris
from kamaki.clients import ClientError, network
40 963bd664 Stavros Sachtouris
41 963bd664 Stavros Sachtouris
42 963bd664 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 963bd664 Stavros Sachtouris
        self.url = 'http://network.example.com'
47 963bd664 Stavros Sachtouris
        self.token = 'n2tw0rk70k3n'
48 963bd664 Stavros Sachtouris
        self.client = network.NetworkRestClient(self.url, self.token)
49 963bd664 Stavros Sachtouris
50 963bd664 Stavros Sachtouris
    def tearDown(self):
51 963bd664 Stavros Sachtouris
        pass
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 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
64 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.networks_get(**kwargs), 'ret val')
65 963bd664 Stavros Sachtouris
            self._assert(get, '/networks', **kwargs)
66 963bd664 Stavros Sachtouris
67 963bd664 Stavros Sachtouris
            netid = 'netid'
68 963bd664 Stavros Sachtouris
            self.assertEqual(
69 963bd664 Stavros Sachtouris
                self.client.networks_get(network_id=netid, **kwargs),
70 963bd664 Stavros Sachtouris
                'ret val')
71 963bd664 Stavros Sachtouris
            self._assert(get, '/networks/%s' % netid, **kwargs)
72 963bd664 Stavros Sachtouris
73 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.set_param')
74 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.post', return_value='ret val')
75 963bd664 Stavros Sachtouris
    def test_networks_post(self, post, set_param):
76 963bd664 Stavros Sachtouris
        for params, kwargs in product(
77 963bd664 Stavros Sachtouris
                (
78 963bd664 Stavros Sachtouris
                    (('shared', False, None), ),
79 963bd664 Stavros Sachtouris
                    (('shared', True, True), )),
80 963bd664 Stavros Sachtouris
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
81 963bd664 Stavros Sachtouris
82 963bd664 Stavros Sachtouris
            callargs = dict()
83 963bd664 Stavros Sachtouris
            for p in params:
84 963bd664 Stavros Sachtouris
                callargs[p[0]] = p[2]
85 963bd664 Stavros Sachtouris
            callargs.update(kwargs)
86 963bd664 Stavros Sachtouris
87 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.networks_post(**callargs), 'ret val')
88 963bd664 Stavros Sachtouris
            self._assert(
89 963bd664 Stavros Sachtouris
                post, '/networks', set_param,
90 963bd664 Stavros Sachtouris
                params=params, data=None, **kwargs)
91 963bd664 Stavros Sachtouris
92 963bd664 Stavros Sachtouris
            json_data = dict(id='some id', other_param='other val')
93 963bd664 Stavros Sachtouris
            callargs['json_data'] = json_data
94 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.networks_post(**callargs), 'ret val')
95 963bd664 Stavros Sachtouris
            self._assert(
96 963bd664 Stavros Sachtouris
                post, '/networks', set_param, params,
97 963bd664 Stavros Sachtouris
                data=dumps(json_data), **kwargs)
98 963bd664 Stavros Sachtouris
99 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.set_param')
100 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.put', return_value='ret val')
101 963bd664 Stavros Sachtouris
    def test_networks_put(self, put, set_param):
102 963bd664 Stavros Sachtouris
        netid = 'netid'
103 963bd664 Stavros Sachtouris
        for params, kwargs in product(
104 963bd664 Stavros Sachtouris
                [p for p in product(
105 963bd664 Stavros Sachtouris
                    (
106 963bd664 Stavros Sachtouris
                        ('admin_state_up', False, None),
107 963bd664 Stavros Sachtouris
                        ('admin_state_up', True, True)),
108 963bd664 Stavros Sachtouris
                    (('shared', False, None), ('shared', True, True)),
109 963bd664 Stavros Sachtouris
                )],
110 963bd664 Stavros Sachtouris
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
111 963bd664 Stavros Sachtouris
112 963bd664 Stavros Sachtouris
            callargs = dict()
113 963bd664 Stavros Sachtouris
            for p in params:
114 963bd664 Stavros Sachtouris
                callargs[p[0]] = p[2]
115 963bd664 Stavros Sachtouris
            callargs.update(kwargs)
116 963bd664 Stavros Sachtouris
117 963bd664 Stavros Sachtouris
            self.assertEqual(
118 963bd664 Stavros Sachtouris
                self.client.networks_put(netid, **callargs), 'ret val')
119 963bd664 Stavros Sachtouris
            self._assert(
120 963bd664 Stavros Sachtouris
                put, '/networks/%s' % netid, set_param, params,
121 963bd664 Stavros Sachtouris
                data=None, **kwargs)
122 963bd664 Stavros Sachtouris
123 963bd664 Stavros Sachtouris
            json_data = dict(id='some id', other_param='other val')
124 963bd664 Stavros Sachtouris
            callargs['json_data'] = json_data
125 963bd664 Stavros Sachtouris
            self.assertEqual(
126 963bd664 Stavros Sachtouris
                self.client.networks_put(netid, **callargs), 'ret val')
127 963bd664 Stavros Sachtouris
            self._assert(
128 963bd664 Stavros Sachtouris
                put, '/networks/%s' % netid, set_param, params,
129 963bd664 Stavros Sachtouris
                data=dumps(json_data), **kwargs)
130 963bd664 Stavros Sachtouris
131 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.delete', return_value='ret val')
132 963bd664 Stavros Sachtouris
    def test_networks_delete(self, delete):
133 963bd664 Stavros Sachtouris
        netid = 'netid'
134 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
135 963bd664 Stavros Sachtouris
            self.assertEqual(
136 963bd664 Stavros Sachtouris
                self.client.networks_delete(netid, **kwargs), 'ret val')
137 963bd664 Stavros Sachtouris
            self._assert(delete, '/networks/%s' % netid, **kwargs)
138 963bd664 Stavros Sachtouris
139 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.get', return_value='ret val')
140 963bd664 Stavros Sachtouris
    def test_subnets_get(self, get):
141 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
142 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.subnets_get(**kwargs), 'ret val')
143 963bd664 Stavros Sachtouris
            self._assert(get, '/subnets', **kwargs)
144 963bd664 Stavros Sachtouris
145 963bd664 Stavros Sachtouris
            subnet_id = 'subnet id'
146 963bd664 Stavros Sachtouris
            self.assertEqual(
147 963bd664 Stavros Sachtouris
                self.client.subnets_get(subnet_id=subnet_id, **kwargs),
148 963bd664 Stavros Sachtouris
                'ret val')
149 963bd664 Stavros Sachtouris
            self._assert(get, '/subnets/%s' % subnet_id, **kwargs)
150 963bd664 Stavros Sachtouris
151 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.post', return_value='ret val')
152 963bd664 Stavros Sachtouris
    def test_subnets_post(self, post):
153 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
154 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.subnets_post(**kwargs), 'ret val')
155 963bd664 Stavros Sachtouris
            self._assert(post, '/subnets', **kwargs)
156 963bd664 Stavros Sachtouris
157 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.put', return_value='ret val')
158 963bd664 Stavros Sachtouris
    def test_subnets_put(self, put):
159 963bd664 Stavros Sachtouris
        subnet_id = 'subid'
160 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
161 963bd664 Stavros Sachtouris
            self.assertEqual(
162 963bd664 Stavros Sachtouris
                self.client.subnets_put(subnet_id, **kwargs), 'ret val')
163 963bd664 Stavros Sachtouris
            self._assert(put, '/subnets/%s' % subnet_id, **kwargs)
164 963bd664 Stavros Sachtouris
165 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.delete', return_value='ret val')
166 963bd664 Stavros Sachtouris
    def test_subnets_delete(self, delete):
167 963bd664 Stavros Sachtouris
        netid = 'netid'
168 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
169 963bd664 Stavros Sachtouris
            self.assertEqual(
170 963bd664 Stavros Sachtouris
                self.client.subnets_delete(netid, **kwargs), 'ret val')
171 963bd664 Stavros Sachtouris
            self._assert(delete, '/subnets/%s' % netid, **kwargs)
172 963bd664 Stavros Sachtouris
173 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.get', return_value='ret val')
174 963bd664 Stavros Sachtouris
    def test_ports_get(self, get):
175 963bd664 Stavros Sachtouris
        for kwargs in (dict(), dict(k1='v1'), dict(k2='v2', k3='v3')):
176 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.ports_get(**kwargs), 'ret val')
177 963bd664 Stavros Sachtouris
            self._assert(get, '/ports', **kwargs)
178 963bd664 Stavros Sachtouris
179 963bd664 Stavros Sachtouris
            port_id = 'port id'
180 963bd664 Stavros Sachtouris
            self.assertEqual(
181 963bd664 Stavros Sachtouris
                self.client.ports_get(port_id=port_id, **kwargs),
182 963bd664 Stavros Sachtouris
                'ret val')
183 963bd664 Stavros Sachtouris
            self._assert(get, '/ports/%s' % port_id, **kwargs)
184 963bd664 Stavros Sachtouris
185 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.set_param')
186 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.post', return_value='ret val')
187 963bd664 Stavros Sachtouris
    def test_ports_post(self, post, set_param):
188 963bd664 Stavros Sachtouris
        for params, kwargs in product(
189 963bd664 Stavros Sachtouris
                [p for p in product(
190 963bd664 Stavros Sachtouris
                    (
191 963bd664 Stavros Sachtouris
                        ('name', 'port name', 'port name'),
192 963bd664 Stavros Sachtouris
                        ('name', None, None)),
193 963bd664 Stavros Sachtouris
                    (
194 963bd664 Stavros Sachtouris
                        ('mac_address', 'max address', 'max address'),
195 963bd664 Stavros Sachtouris
                        ('mac_address', None, None)),
196 963bd664 Stavros Sachtouris
                    (
197 963bd664 Stavros Sachtouris
                        ('fixed_ips', 'fixed ip', 'fixed ip'),
198 963bd664 Stavros Sachtouris
                        ('fixed_ips', None, None)),
199 963bd664 Stavros Sachtouris
                    (
200 963bd664 Stavros Sachtouris
                        ('security_groups', 'sec groups', 'sec groups'),
201 963bd664 Stavros Sachtouris
                        ('security_groups', None, None))
202 963bd664 Stavros Sachtouris
                )],
203 963bd664 Stavros Sachtouris
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
204 963bd664 Stavros Sachtouris
205 963bd664 Stavros Sachtouris
            callargs = dict()
206 963bd664 Stavros Sachtouris
            for p in params:
207 963bd664 Stavros Sachtouris
                callargs[p[0]] = p[2]
208 963bd664 Stavros Sachtouris
            callargs.update(kwargs)
209 963bd664 Stavros Sachtouris
210 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.ports_post(**callargs), 'ret val')
211 963bd664 Stavros Sachtouris
            self._assert(
212 963bd664 Stavros Sachtouris
                post, '/ports', set_param,
213 963bd664 Stavros Sachtouris
                params=params, data=None, **kwargs)
214 963bd664 Stavros Sachtouris
215 963bd664 Stavros Sachtouris
            json_data = dict(id='some id', other_param='other val')
216 963bd664 Stavros Sachtouris
            callargs['json_data'] = json_data
217 963bd664 Stavros Sachtouris
            self.assertEqual(self.client.ports_post(**callargs), 'ret val')
218 963bd664 Stavros Sachtouris
            self._assert(
219 963bd664 Stavros Sachtouris
                post, '/ports', set_param, params,
220 963bd664 Stavros Sachtouris
                data=dumps(json_data), **kwargs)
221 963bd664 Stavros Sachtouris
222 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.set_param')
223 963bd664 Stavros Sachtouris
    @patch('kamaki.clients.Client.put', return_value='ret val')
224 963bd664 Stavros Sachtouris
    def test_ports_put(self, put, set_param):
225 963bd664 Stavros Sachtouris
        port_id = 'portid'
226 963bd664 Stavros Sachtouris
        for params, kwargs in product(
227 963bd664 Stavros Sachtouris
                [p for p in product(
228 963bd664 Stavros Sachtouris
                    (
229 963bd664 Stavros Sachtouris
                        ('name', 'port name', 'port name'),
230 963bd664 Stavros Sachtouris
                        ('name', None, None)),
231 963bd664 Stavros Sachtouris
                    (
232 963bd664 Stavros Sachtouris
                        ('mac_address', 'max address', 'max address'),
233 963bd664 Stavros Sachtouris
                        ('mac_address', None, None)),
234 963bd664 Stavros Sachtouris
                    (
235 963bd664 Stavros Sachtouris
                        ('fixed_ips', 'fixed ip', 'fixed ip'),
236 963bd664 Stavros Sachtouris
                        ('fixed_ips', None, None)),
237 963bd664 Stavros Sachtouris
                    (
238 963bd664 Stavros Sachtouris
                        ('security_groups', 'sec groups', 'sec groups'),
239 963bd664 Stavros Sachtouris
                        ('security_groups', None, None))
240 963bd664 Stavros Sachtouris
                )],
241 963bd664 Stavros Sachtouris
                (dict(), dict(k1='v1'), dict(k2='v2', k3='v3'))):
242 963bd664 Stavros Sachtouris
243 963bd664 Stavros Sachtouris
            callargs = dict()
244 963bd664 Stavros Sachtouris
            for p in params:
245 963bd664 Stavros Sachtouris
                callargs[p[0]] = p[2]
246 963bd664 Stavros Sachtouris
            callargs.update(kwargs)
247 963bd664 Stavros Sachtouris
248 963bd664 Stavros Sachtouris
            self.assertEqual(
249 963bd664 Stavros Sachtouris
                self.client.ports_put(port_id, **callargs), 'ret val')
250 963bd664 Stavros Sachtouris
            self._assert(
251 963bd664 Stavros Sachtouris
                put, '/ports/%s' % port_id, set_param,
252 963bd664 Stavros Sachtouris
                params=params, data=None, **kwargs)
253 963bd664 Stavros Sachtouris
254 963bd664 Stavros Sachtouris
            json_data = dict(id='some id', other_param='other val')
255 963bd664 Stavros Sachtouris
            callargs['json_data'] = json_data
256 963bd664 Stavros Sachtouris
            self.assertEqual(
257 963bd664 Stavros Sachtouris
                self.client.ports_put(port_id, **callargs), 'ret val')
258 963bd664 Stavros Sachtouris
            self._assert(
259 963bd664 Stavros Sachtouris
                put, '/ports/%s' % port_id, set_param, params,
260 963bd664 Stavros Sachtouris
                data=dumps(json_data), **kwargs)
261 963bd664 Stavros Sachtouris
262 963bd664 Stavros Sachtouris
263 963bd664 Stavros Sachtouris
if __name__ == '__main__':
264 963bd664 Stavros Sachtouris
    from sys import argv
265 963bd664 Stavros Sachtouris
    from kamaki.clients.test import runTestCase
266 963bd664 Stavros Sachtouris
    not_found = True
267 963bd664 Stavros Sachtouris
    #if not argv[1:] or argv[1] == 'NetworkClient':
268 963bd664 Stavros Sachtouris
    #    not_found = False
269 963bd664 Stavros Sachtouris
    #    runTestCase(NetworkClient, 'Network Client', argv[2:])
270 963bd664 Stavros Sachtouris
    if not argv[1:] or argv[1] == 'NetworkRest':
271 963bd664 Stavros Sachtouris
        not_found = False
272 963bd664 Stavros Sachtouris
        runTestCase(NetworkRestClient, 'NetworkRest Client', argv[2:])
273 963bd664 Stavros Sachtouris
    if not_found:
274 963bd664 Stavros Sachtouris
        print('TestCase %s not found' % argv[1])