Add utils to work with OpenStack python clients

These utils simplify and unify creation of OpenStack python clients.

Becuase it will be usead at least in test_scenarios and OpenStack Vm Provider
it make sense to make it as separated module

implement bp os-python-clients

Change-Id: I47645a7b85865e737669f11e4cd57a6dc897d62a
This commit is contained in:
Boris Pavlovic
2013-09-15 23:43:22 +04:00
parent 7b36d564e1
commit c55888ce10
2 changed files with 144 additions and 0 deletions

59
rally/osclients.py Normal file
View File

@@ -0,0 +1,59 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013: Mirantis Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from cinderclient import client as cinder
import glanceclient as glance
from keystoneclient.v2_0 import client as keystone
from novaclient import client as nova
class Clients(object):
"""This class simplify and unify work with openstack python clients."""
def __init__(self, username, password, tenant_name, auth_url):
self.kw = {'username': username, 'password': password,
'tenant_name': tenant_name, 'auth_url': auth_url}
def get_keystone_client(self):
"""Return keystone client."""
return keystone.Client(**self.kw)
def get_nova_client(self, version='2'):
"""Returns nova client."""
return nova.Client(version,
self.kw['username'],
self.kw['password'],
self.kw['tenant_name'],
auth_url=self.kw['auth_url'],
service_type='compute')
def get_glance_client(self, version='1'):
"""Returns glance client."""
kc = self.get_keystone_client()
endpoint = kc.service_catalog.get_endpoints()['image'][0]
return glance.Client(version,
endpoint=endpoint['publicURL'],
token=kc.auth_token)
def get_cinder_client(self, version='1'):
"""Returns cinder client."""
return cinder.Client(version,
self.kw['username'],
self.kw['password'],
self.kw['tenant_name'],
auth_url=self.kw['auth_url'],
service_type='volume')

85
tests/test_osclients.py Normal file
View File

@@ -0,0 +1,85 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013: Mirantis Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import mock
from rally import osclients
from rally import test
class FakeServiceCatalog(object):
def get_endpoints(self):
return {'image': [{'publicURL': 'http://fake.to'}]}
class FakeKeystone(object):
def __init__(self):
self.auth_token = 'fake'
self.service_catalog = FakeServiceCatalog()
class OSClientsTestCase(test.NoDBTestCase):
def _get_auth_params(self):
args = ['user', 'pass', 'tenant', 'auth_url']
keys = ['username', 'password', 'tenant_name', 'auth_url']
return (args, dict(zip(keys, args)))
def setUp(self):
super(OSClientsTestCase, self).setUp()
self.args, self.kwargs = self._get_auth_params()
self.clients = osclients.Clients(*self.args)
def test_init(self):
self.assertEqual(self.kwargs, self.clients.kw)
def test_get_keystone_client(self):
with mock.patch('rally.osclients.keystone') as mock_keystone:
mock_keystone.Client = mock.MagicMock(return_value={})
client = self.clients.get_keystone_client()
self.assertEqual(client, {})
mock_keystone.Client.assert_called_once_with(**self.kwargs)
def test_get_nova_client(self):
with mock.patch('rally.osclients.nova') as mock_nova:
mock_nova.Client = mock.MagicMock(return_value={})
client = self.clients.get_nova_client()
self.assertEqual(client, {})
mock_nova.Client.assert_called_once_with('2', *self.args[:3],
auth_url=self.args[-1],
service_type='compute')
def test_get_glance_client(self):
with mock.patch('rally.osclients.glance') as mock_glance:
mock_glance.Client = mock.MagicMock(return_value={})
kc = FakeKeystone()
self.clients.get_keystone_client = mock.MagicMock(return_value=kc)
client = self.clients.get_glance_client()
self.assertEqual(client, {})
endpoint = kc.service_catalog.get_endpoints()['image'][0]
kw = {'endpoint': endpoint['publicURL'], 'token': kc.auth_token}
mock_glance.Client.assert_called_once_with('1', **kw)
def test_get_cinder_client(self):
with mock.patch('rally.osclients.cinder') as mock_cinder:
mock_cinder.Client = mock.MagicMock(return_value={})
client = self.clients.get_cinder_client()
self.assertEqual(client, {})
mock_cinder.Client.assert_called_once_with('1', *self.args[:3],
auth_url=self.args[-1],
service_type='volume')