Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product sync from testing #6

Open
wants to merge 4 commits into
base: product
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions ceilometerclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,4 @@

__all__ = ['__version__']

import pbr.version

version_info = pbr.version.VersionInfo('python-ceilometerclient')
try:
__version__ = version_info.version_string()
except AttributeError:
__version__ = None
__version__ = "REDHATCEILOMETERCLIENTVERSION"
15 changes: 15 additions & 0 deletions ceilometerclient/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@
from ceilometerclient.openstack.common import cliutils


def _positive_non_zero_int(argument_value):
if argument_value is None:
return None
try:
value = int(argument_value)
except ValueError:
msg = "%s must be an integer" % argument_value
raise argparse.ArgumentTypeError(msg)
if value <= 0:
msg = "%s must be greater than 0" % argument_value
raise argparse.ArgumentTypeError(msg)
return value


class CeilometerShell(object):

def get_base_parser(self):
Expand Down Expand Up @@ -64,6 +78,7 @@ def get_base_parser(self):

parser.add_argument('--timeout',
default=600,
type=_positive_non_zero_int,
help='Number of seconds to wait for a response.')

parser.add_argument('--ceilometer-url',
Expand Down
35 changes: 35 additions & 0 deletions ceilometerclient/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,38 @@ def test_client_without_auth_plugin(self):
def test_client_with_auth_plugin(self):
c = self.create_client(FAKE_ENV, api_version=2)
self.assertIsInstance(c.auth_plugin, str)

def test_v2_client_timeout_invalid_value(self):
env = FAKE_ENV.copy()
env['timeout'] = 'abc'
self.assertRaises(ValueError, self.create_client, env)
env['timeout'] = '1.5'
self.assertRaises(ValueError, self.create_client, env)

def _test_v2_client_timeout_integer(self, timeout, expected_value):
env = FAKE_ENV.copy()
env['timeout'] = timeout
expected = {
'auth_plugin': 'fake_auth',
'timeout': expected_value,
'original_ip': None,
'http': None,
'region_name': None,
'verify': None,
'timings': None,
'keyring_saver': None,
'cert': None,
'endpoint_type': None,
'user_agent': None,
'debug': None,
}
cls = 'ceilometerclient.openstack.common.apiclient.client.HTTPClient'
with mock.patch(cls) as mocked:
self.create_client(env)
mocked.assert_called_with(**expected)

def test_v2_client_timeout_zero(self):
self._test_v2_client_timeout_integer(0, None)

def test_v2_client_timeout_valid_value(self):
self._test_v2_client_timeout_integer(30, 30)
29 changes: 29 additions & 0 deletions ceilometerclient/tests/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,32 @@ def test_no_debug_switch_no_raises_errors(self, mock_ksclient, __):
self.make_env(FAKE_V3_ENV)
args = ['event-list']
self.assertRaises(SystemExit, ceilometer_shell.main, args)


class ShellTimeoutTest(ShellTestBase):

@mock.patch('sys.stderr', new=six.StringIO())
def _test_timeout(self, timeout, expected_msg):
args = ['--timeout', timeout, 'alarm-list']
self.assertRaises(SystemExit, ceilometer_shell.main, args)
self.assertEqual(expected_msg, sys.stderr.getvalue().splitlines()[-1])

def test_timeout_invalid_value(self):
expected_msg = ('ceilometer: error: argument --timeout: '
'abc must be an integer')
self._test_timeout('abc', expected_msg)

def test_timeout_negative_value(self):
expected_msg = ('ceilometer: error: argument --timeout: '
'-1 must be greater than 0')
self._test_timeout('-1', expected_msg)

def test_timeout_float_value(self):
expected_msg = ('ceilometer: error: argument --timeout: '
'1.5 must be an integer')
self._test_timeout('1.5', expected_msg)

def test_timeout_zero(self):
expected_msg = ('ceilometer: error: argument --timeout: '
'0 must be greater than 0')
self._test_timeout('0', expected_msg)
148 changes: 148 additions & 0 deletions ceilometerclient/tests/v2/test_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from ceilometerclient import exc
from ceilometerclient import shell as base_shell
from ceilometerclient.tests import test_shell
from ceilometerclient.tests import utils
from ceilometerclient.v2 import alarms
from ceilometerclient.v2 import samples
Expand Down Expand Up @@ -901,3 +902,150 @@ def test_obsolete_alarm_id(self):
'alarm-combination-update', 'alarm-delete',
'alarm-state-get', 'alarm-history']:
self._test_entity_obsoleted('alarm_id', 'abcde', False, method)


class ShellEventListCommandTest(utils.BaseTestCase):

EVENTS = [
{
"traits": [],
"generated": "2015-01-12T04:03:25.741471",
"message_id": "fb2bef58-88af-4380-8698-e0f18fcf452d",
"event_type": "compute.instance.create.start",
"traits": [{
"name": "state",
"type": "string",
"value": "building",
}],
},
{
"traits": [],
"generated": "2015-01-12T04:03:28.452495",
"message_id": "9b20509a-576b-4995-acfa-1a24ee5cf49f",
"event_type": "compute.instance.create.end",
"traits": [{
"name": "state",
"type": "string",
"value": "active",
}],
},
]

def setUp(self):
super(ShellEventListCommandTest, self).setUp()
self.cc = mock.Mock()
self.args = mock.Mock()
self.args.query = None
self.args.no_traits = None

@mock.patch('sys.stdout', new=six.StringIO())
def test_event_list(self):
ret_events = [events.Event(mock.Mock(), event)
for event in self.EVENTS]
self.cc.events.list.return_value = ret_events
ceilometer_shell.do_event_list(self.cc, self.args)
self.assertEqual('''\
+--------------------------------------+-------------------------------+\
----------------------------+-------------------------------+
| Message ID | Event Type |\
Generated | Traits |
+--------------------------------------+-------------------------------+\
----------------------------+-------------------------------+
| fb2bef58-88af-4380-8698-e0f18fcf452d | compute.instance.create.start |\
2015-01-12T04:03:25.741471 | +-------+--------+----------+ |
| | |\
| | name | type | value | |
| | |\
| +-------+--------+----------+ |
| | |\
| | state | string | building | |
| | |\
| +-------+--------+----------+ |
| 9b20509a-576b-4995-acfa-1a24ee5cf49f | compute.instance.create.end |\
2015-01-12T04:03:28.452495 | +-------+--------+--------+ |
| | |\
| | name | type | value | |
| | |\
| +-------+--------+--------+ |
| | |\
| | state | string | active | |
| | |\
| +-------+--------+--------+ |
+--------------------------------------+-------------------------------+\
----------------------------+-------------------------------+
''', sys.stdout.getvalue())

@mock.patch('sys.stdout', new=six.StringIO())
def test_event_list_no_traits(self):
self.args.no_traits = True
ret_events = [events.Event(mock.Mock(), event)
for event in self.EVENTS]
self.cc.events.list.return_value = ret_events
ceilometer_shell.do_event_list(self.cc, self.args)
self.assertEqual('''\
+--------------------------------------+-------------------------------\
+----------------------------+
| Message ID | Event Type \
| Generated |
+--------------------------------------+-------------------------------\
+----------------------------+
| fb2bef58-88af-4380-8698-e0f18fcf452d | compute.instance.create.start \
| 2015-01-12T04:03:25.741471 |
| 9b20509a-576b-4995-acfa-1a24ee5cf49f | compute.instance.create.end \
| 2015-01-12T04:03:28.452495 |
+--------------------------------------+-------------------------------\
+----------------------------+
''', sys.stdout.getvalue())


class ShellShadowedArgsTest(test_shell.ShellTestBase):

def _test_project_id_alarm(self, command, args, method):
self.make_env(test_shell.FAKE_V2_ENV)
cli_args = [
'--os-project-id', '0ba30185ddf44834914a0b859d244c56',
'--debug', command,
'--project-id', 'the-project-id-i-want-to-set',
'--name', 'project-id-test'] + args
with mock.patch.object(alarms.AlarmManager, method) as mocked:
base_shell.main(cli_args)
args, kwargs = mocked.call_args
self.assertEqual('the-project-id-i-want-to-set',
kwargs.get('project_id'))

def test_project_id_threshold_alarm(self):
cli_args = ['--meter-name', 'cpu', '--threshold', '90']
self._test_project_id_alarm('alarm-create', cli_args, 'create')
self._test_project_id_alarm('alarm-threshold-create',
cli_args, 'create')
cli_args += ['--alarm_id', '437b7ed0-3733-4054-a877-e9a297b8be85']
self._test_project_id_alarm('alarm-update', cli_args, 'update')
self._test_project_id_alarm('alarm-threshold-update',
cli_args, 'update')

def test_project_id_combination_alarm(self):
cli_args = ['--alarm_ids', 'fb16a05a-669d-414e-8bbe-93aa381df6a8',
'--alarm_ids', 'b189bcca-0a7b-49a9-a244-a927ac291881']
self._test_project_id_alarm('alarm-combination-create',
cli_args, 'create')
cli_args += ['--alarm_id', '437b7ed0-3733-4054-a877-e9a297b8be85']
self._test_project_id_alarm('alarm-combination-update',
cli_args, 'update')

@mock.patch.object(samples.OldSampleManager, 'create')
def test_project_id_sample_create(self, mocked):
self.make_env(test_shell.FAKE_V2_ENV)
cli_args = [
'--os-project-id', '0ba30185ddf44834914a0b859d244c56',
'--debug', 'sample-create',
'--project-id', 'the-project-id-i-want-to-set',
'--resource-id', 'b666633d-9bb6-4e05-89c0-ee5a8752fb0b',
'--meter-name', 'cpu',
'--meter-type', 'cumulative',
'--meter-unit', 'ns',
'--sample-volume', '10086',
]
base_shell.main(cli_args)
args, kwargs = mocked.call_args
self.assertEqual('the-project-id-i-want-to-set',
kwargs.get('project_id'))
20 changes: 15 additions & 5 deletions ceilometerclient/v2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,36 @@
class Client(object):
"""Client for the Ceilometer v2 API.

:param string endpoint: A user-supplied endpoint URL for the ceilometer
:param endpoint: A user-supplied endpoint URL for the ceilometer
service.
:param function token: Provides token for authentication.
:param integer timeout: Allows customization of the timeout for client
http requests. (optional)
:type endpoint: string
:param token: Provides token for authentication.
:type token: function
:param timeout: Allows customization of the timeout for client
http requests. (optional)
:type timeout: integer
"""

def __init__(self, *args, **kwargs):

"""Initialize a new client for the Ceilometer v2 API."""
self.auth_plugin = kwargs.get('auth_plugin') \
or ceiloclient.get_auth_plugin(*args, **kwargs)

timeout = kwargs.get('timeout')
if timeout is not None:
timeout = int(timeout)
if timeout <= 0:
timeout = None

self.client = client.HTTPClient(
auth_plugin=self.auth_plugin,
region_name=kwargs.get('region_name'),
endpoint_type=kwargs.get('endpoint_type'),
original_ip=kwargs.get('original_ip'),
verify=kwargs.get('verify'),
cert=kwargs.get('cacert'),
timeout=kwargs.get('timeout'),
timeout=timeout,
timings=kwargs.get('timings'),
keyring_saver=kwargs.get('keyring_saver'),
debug=kwargs.get('debug'),
Expand Down
Loading