Commit afba96a7 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Implement all network calls + unittests

Refs: #4546

Implemented: list_networks, create_network, create_networks,
    get_network_details, update_network, delete_network
parent c95ff061
......@@ -42,8 +42,61 @@ class NetworkingClient(NetworkingRestClient):
r = self.networks_get(success=200)
return r.json['networks']
def create_network(self, name=None, admin_state_up=None, shared=None):
def create_network(self, name, admin_state_up=None, shared=None):
req = dict(network=dict(
name=name or '', admin_state_up=bool(admin_state_up)))
r = self.networks_post(json_data=req, shared=shared, success=201)
return r.json['network']
def create_networks(self, networks, shared=None):
"""
:param networks: (list or tuple) [
{name: ..., admin_state_up: ...},
{name:..., admin_state_up: ...}]
name is mandatory, the rest is optional
e.g. create_networks(({name: 'net1', shared: True}, {name: net2}))
:returns: list of dicts of created networks
:raises ValueError: if networks is misformated
:raises ClientError: if the request failed or didn't return 201
"""
try:
msg = 'The networks parameter must be list or tuple'
assert (
isinstance(networks, list) or isinstance(networks, tuple)), msg
for network in networks:
msg = 'Network specification %s is not a dict' % network
assert isinstance(network, dict), msg
err = set(network).difference(('name', 'admin_state_up'))
if err:
raise ValueError(
'Invalid key(s): %s in network specification %s' % (
err, network))
msg = 'Name is missing in network specification: %s' % network
assert network.get('name', None), msg
network.setdefault('admin_state_up', False)
except AssertionError as ae:
raise ValueError('%s' % ae)
req = dict(networks=list(networks))
r = self.networks_post(json_data=req, shared=shared, success=201)
return r.json['networks']
def get_network_details(self, network_id):
r = self.networks_get(network_id, success=200)
return r.json['network']
def update_network(
self, network_id, name=None, admin_state_up=None, shared=None):
network = dict()
if name:
network['name'] = name
if admin_state_up not in (None, ):
network['admin_state_up'] = admin_state_up
network = dict(network=network)
r = self.networks_put(
network_id, json_data=network, shared=shared, success=200)
return r.json['network']
def delete_network(self, network_id):
r = self.networks_delete(network_id, success=204)
return r.headers
......@@ -263,6 +263,7 @@ class NetworkingRestClient(TestCase):
class FakeObject(object):
json = None
headers = None
class NetworkingClient(TestCase):
......@@ -288,25 +289,94 @@ class NetworkingClient(TestCase):
'kamaki.clients.networking.NetworkingClient.networks_post',
return_value=FakeObject())
def test_create_network(self, networks_post):
FakeObject.json = dict(network='ret val')
req = dict()
for kwargs in (dict(shared=None), dict(shared=True)):
for body_params in product(
(('name', None, ''), ('name', 'some name', 'some name')),
(
('admin_state_up', None, False),
('admin_state_up', True, True))):
FakeObject.json = dict(network='ret val')
req = dict()
for k, v, exp in (
('admin_state_up', None, False),
('admin_state_up', True, True)):
fullargs = dict(kwargs)
for k, v, exp in body_params:
fullargs[k] = v
req[k] = exp
fullargs[k], name = v, 'net name'
self.assertEqual(
self.client.create_network(**fullargs), 'ret val')
self.client.create_network(name, **fullargs), 'ret val')
req[k] = exp
req['name'] = name
expargs = dict(kwargs)
expargs = dict(json_data=dict(network=req), success=201)
expargs.update(kwargs)
self.assertEqual(networks_post.mock_calls[-1], call(**expargs))
@patch(
'kamaki.clients.networking.NetworkingClient.networks_post',
return_value=FakeObject())
def test_create_networks(self, networks_post):
for networks, shared in product(
(
None, dict(name='name'), 'nets', [1, 2, 3], [{'k': 'v'}, ],
[dict(name='n1', invalid='mistake'), ],
[dict(name='valid', admin_state_up=True), {'err': 'nop'}]),
(None, True)
):
self.assertRaises(
ValueError, self.client.create_networks, networks, shared)
FakeObject.json = dict(networks='ret val')
for networks, kwargs in product(
(
[
dict(name='net1'),
dict(name='net 2', admin_state_up=False)],
[
dict(name='net1', admin_state_up=True),
dict(name='net 2', admin_state_up=False),
dict(name='net-3')],
(dict(name='n.e.t'), dict(name='net 2'))),
(dict(shared=None), dict(shared=True))):
self.assertEqual(
self.client.create_networks(networks, **kwargs), 'ret val')
networks = list(networks)
expargs = dict(json_data=dict(networks=networks), success=201)
expargs.update(kwargs)
self.assertEqual(networks_post.mock_calls[-1], call(**expargs))
@patch(
'kamaki.clients.networking.NetworkingClient.networks_get',
return_value=FakeObject())
def test_get_network_details(self, networks_get):
netid, FakeObject.json = 'netid', dict(network='ret val')
self.assertEqual(self.client.get_network_details(netid), 'ret val')
networks_get.assert_called_once_with(netid, success=200)
@patch(
'kamaki.clients.networking.NetworkingClient.networks_put',
return_value=FakeObject())
def test_update_network(self, networks_put):
netid, FakeObject.json = 'netid', dict(network='ret val')
for name, admin_state_up, shared in product(
('net name', None), (True, None), (True, None)):
kwargs = dict(
name=name, admin_state_up=admin_state_up, shared=shared)
self.assertEqual(
self.client.update_network(netid, **kwargs), 'ret val')
req = dict()
if name not in (None, ):
req['name'] = name
if admin_state_up not in (None, ):
req['admin_state_up'] = admin_state_up
kwargs = dict(
json_data=dict(network=req), shared=shared, success=200)
self.assertEqual(
networks_put.mock_calls[-1], call(netid, **kwargs))
@patch(
'kamaki.clients.networking.NetworkingClient.networks_delete',
return_value=FakeObject())
def test_delete_network(self, networks_delete):
netid, FakeObject.headers = 'netid', 'ret headers'
self.assertEqual(self.client.delete_network(netid), 'ret headers')
networks_delete.assert_called_once_with(netid, success=204)
if __name__ == '__main__':
from sys import argv
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment