Skip to content

Commit

Permalink
Adds --json,--pprint flags to cmd
Browse files Browse the repository at this point in the history
* Add flags to output cmd results in json(--json)
  and pretty print(--pprint) format

Change-Id: Id4331761e4f9c4bb6735745ed8461eaa671d0945
Closes-Bug: #1341501
  • Loading branch information
Rohan Kanade committed Aug 8, 2014
1 parent 3609d51 commit 61842b8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 14 deletions.
24 changes: 21 additions & 3 deletions rally/cmd/commands/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import json
import os
import pprint
import sys

import yaml
Expand Down Expand Up @@ -136,14 +137,31 @@ def list(self, deployment_list=None):

@cliutils.args('--uuid', dest='deploy_id', type=str, required=False,
help='UUID of a deployment.')
@cliutils.args('--json', dest='output_json', action='store_true',
help='Output in json format(default)')
@cliutils.args('--pprint', dest='output_pprint', action='store_true',
help='Output in pretty print format')
@envutils.with_default_deploy_id
def config(self, deploy_id=None):
"""Print on stdout a config of the deployment in JSON format.
def config(self, deploy_id=None, output_json=None, output_pprint=None):
"""Print on stdout a config of the deployment.
Output can JSON or Pretty print format.
:param deploy_id: a UUID of the deployment
:param output_json: Output in json format (Default)
:param output_pprint: Output in pretty print format
"""
deploy = db.deployment_get(deploy_id)
print(json.dumps(deploy['config']))
result = deploy['config']
if all([output_json, output_pprint]):
print(_('Please select only one output format'))
return 1
elif output_pprint:
print()
pprint.pprint(result)
print()
else:
print(json.dumps(result))

@cliutils.args('--uuid', dest='deploy_id', type=str, required=False,
help='UUID of a deployment.')
Expand Down
21 changes: 12 additions & 9 deletions rally/cmd/commands/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,28 +285,31 @@ def _get_atomic_action_durations(raw):
print("\trally task results %s\n" % task["uuid"])

@cliutils.args('--uuid', type=str, dest='task_id', help='uuid of task')
@cliutils.args('--pretty', type=str, help=('pretty print (pprint) '
'or json print (json)'))
@cliutils.args('--pprint', action='store_true', dest='output_pprint',
help=('Output in pretty print format'))
@cliutils.args('--json', action='store_true', dest='output_json',
help=('Output in json format(default)'))
@envutils.with_default_task_id
def results(self, task_id=None, pretty=False):
def results(self, task_id=None, output_pprint=None, output_json=None):
"""Print raw results of task.
:param task_id: Task uuid
:param pretty: Pretty print (pprint) or not (json)
:param output_pprint: Output in pretty print format
:param output_json: Output in json format (Default)
"""
results = map(lambda x: {"key": x["key"], 'result': x['data']['raw']},
db.task_result_get_all_by_uuid(task_id))

if results:
if not pretty or pretty == 'json':
print(json.dumps(results))
elif pretty == 'pprint':
if all([output_pprint, output_json]):
print(_('Please select only one output format'))
return 1
elif output_pprint:
print()
pprint.pprint(results)
print()
else:
print(_("Wrong value for --pretty=%s") % pretty)
return(1)
print(json.dumps(results))
else:
print(_("The task %s can not be found") % task_id)
return(1)
Expand Down
24 changes: 23 additions & 1 deletion tests/cmd/commands/test_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,33 @@ def test_list_current_deploy_id(self, mock_deployments,
'created_at'))

@mock.patch('rally.cmd.commands.deployment.db.deployment_get')
def test_config(self, mock_deployment):
@mock.patch('json.dumps')
def test_config_default(self, mock_json_dumps, mock_deployment):
deploy_id = 'fa4a423e-f15d-4d83-971a-89574f892999'
value = {'config': 'config'}
mock_deployment.return_value = value
self.deployment.config(deploy_id)
mock_json_dumps.assert_called_once_with(value['config'])
mock_deployment.assert_called_once_with(deploy_id)

@mock.patch('rally.cmd.commands.deployment.db.deployment_get')
@mock.patch('json.dumps')
def test_config_json(self, mock_json_dumps, mock_deployment):
deploy_id = '25c5f6d3-56ce-4273-834c-1ae5e1c2599c'
value = {'config': 'config'}
mock_deployment.return_value = value
self.deployment.config(deploy_id, output_json=True)
mock_json_dumps.assert_called_once_with(value['config'])
mock_deployment.assert_called_once_with(deploy_id)

@mock.patch('rally.cmd.commands.deployment.db.deployment_get')
@mock.patch('pprint.pprint')
def test_config_pprint(self, mock_pprint, mock_deployment):
deploy_id = '840a0144-4634-46fd-8cf8-b84caa0dba67'
value = {'config': 'config'}
mock_deployment.return_value = value
self.deployment.config(deploy_id, output_pprint=True)
mock_pprint.assert_called_once_with(value['config'])
mock_deployment.assert_called_once_with(deploy_id)

@mock.patch('rally.cmd.commands.deployment.envutils.get_global')
Expand Down
35 changes: 34 additions & 1 deletion tests/cmd/commands/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,46 @@ def test_detailed_wrong_id(self, mock_db):
mock_db.task_get_detailed.assert_called_once_with(test_uuid)

@mock.patch('rally.cmd.commands.task.db')
def test_results(self, mock_db):
@mock.patch('json.dumps')
def test_results_default(self, mock_json, mock_db):
test_uuid = 'aa808c14-69cc-4faf-a906-97e05f5aebbd'
value = [
{'key': 'key', 'data': {'raw': 'raw'}}
]
result = map(lambda x: {"key": x["key"],
'result': x['data']['raw']}, value)
mock_db.task_result_get_all_by_uuid.return_value = value
self.task.results(test_uuid)
mock_json.assert_called_once_with(result)
mock_db.task_result_get_all_by_uuid.assert_called_once_with(test_uuid)

@mock.patch('rally.cmd.commands.task.db')
@mock.patch('json.dumps')
def test_results_json(self, mock_json, mock_db):
test_uuid = 'e87dd629-cd3d-4a1e-b377-7b93c19226fb'
value = [
{'key': 'key', 'data': {'raw': 'raw'}}
]
result = map(lambda x: {"key": x["key"],
'result': x['data']['raw']}, value)

mock_db.task_result_get_all_by_uuid.return_value = value
self.task.results(test_uuid, output_json=True)
mock_json.assert_called_once_with(result)
mock_db.task_result_get_all_by_uuid.assert_called_once_with(test_uuid)

@mock.patch('rally.cmd.commands.task.db')
@mock.patch('pprint.pprint')
def test_results_pprint(self, mock_pprint, mock_db):
test_uuid = 'c1e4bc59-a8fd-458c-9abb-c922d8df4285'
value = [
{'key': 'key', 'data': {'raw': 'raw'}}
]
result = map(lambda x: {"key": x["key"],
'result': x['data']['raw']}, value)
mock_db.task_result_get_all_by_uuid.return_value = value
self.task.results(test_uuid, output_pprint=True)
mock_pprint.assert_called_once_with(result)
mock_db.task_result_get_all_by_uuid.assert_called_once_with(test_uuid)

@mock.patch('rally.cmd.commands.task.db')
Expand Down

0 comments on commit 61842b8

Please sign in to comment.