Commit 058ee9a8 authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Implement clients.network.NetworkClient (part)

Refs: #4546

Implemented in this commit: list_networks, create_network, unittests for both
parent 963bd664
......@@ -37,3 +37,13 @@ from kamaki.clients.network.rest_api import NetworkRestClient
class NetworkClient(NetworkRestClient):
"""OpenStack Network API 2.0 client"""
def list_networks(self):
r = self.networks_get(success=200)
return r.json['networks']
def create_network(self, name=None, 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']
......@@ -48,7 +48,7 @@ class NetworkRestClient(TestCase):
self.client = network.NetworkRestClient(self.url, self.token)
def tearDown(self):
pass
del self.client
def _assert(self, method_call, path, set_param=None, params=(), **kwargs):
"""Assert the REST method call is called as expected"""
......@@ -260,13 +260,61 @@ class NetworkRestClient(TestCase):
data=dumps(json_data), **kwargs)
class FakeObject(object):
json = None
class NetworkClient(TestCase):
"""Set up a ComputesRest thorough test"""
def setUp(self):
self.url = 'http://network.example.com'
self.token = 'n2tw0rk70k3n'
self.client = network.NetworkClient(self.url, self.token)
def tearDown(self):
del self.client
@patch(
'kamaki.clients.network.NetworkClient.networks_get',
return_value=FakeObject())
def test_list_networks(self, networks_get):
FakeObject.json = dict(networks='ret val')
self.assertEqual(self.client.list_networks(), 'ret val')
networks_get.assert_called_once_with(success=200)
@patch(
'kamaki.clients.network.NetworkClient.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))):
fullargs = dict(kwargs)
for k, v, exp in body_params:
fullargs[k] = v
req[k] = exp
self.assertEqual(
self.client.create_network(**fullargs), 'ret val')
expargs = dict(kwargs)
expargs = dict(json_data=dict(network=req), success=201)
expargs.update(kwargs)
self.assertEqual(networks_post.mock_calls[-1], call(**expargs))
if __name__ == '__main__':
from sys import argv
from kamaki.clients.test import runTestCase
not_found = True
#if not argv[1:] or argv[1] == 'NetworkClient':
# not_found = False
# runTestCase(NetworkClient, 'Network Client', argv[2:])
if not argv[1:] or argv[1] == 'NetworkClient':
not_found = False
runTestCase(NetworkClient, 'Network Client', argv[2:])
if not argv[1:] or argv[1] == 'NetworkRest':
not_found = False
runTestCase(NetworkRestClient, 'NetworkRest Client', argv[2:])
......
......@@ -41,7 +41,7 @@ from random import randint
from kamaki.clients.utils.test import Utils
from kamaki.clients.astakos.test import AstakosClient
from kamaki.clients.compute.test import ComputeClient, ComputeRestClient
from kamaki.clients.network.test import NetworkRestClient
from kamaki.clients.network.test import NetworkClient, NetworkRestClient
from kamaki.clients.cyclades.test import CycladesClient
from kamaki.clients.cyclades.test import CycladesRestClient
from kamaki.clients.image.test import ImageClient
......
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