diff --git a/rally/cmd/commands/deployment.py b/rally/cmd/commands/deployment.py index 207b7d7416..eefdf2686a 100644 --- a/rally/cmd/commands/deployment.py +++ b/rally/cmd/commands/deployment.py @@ -19,6 +19,7 @@ from __future__ import print_function import json import os +import pprint import sys import yaml @@ -136,14 +137,31 @@ class DeploymentCommands(object): @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.') diff --git a/rally/cmd/commands/task.py b/rally/cmd/commands/task.py index a62b942faa..b4d7965b88 100644 --- a/rally/cmd/commands/task.py +++ b/rally/cmd/commands/task.py @@ -289,28 +289,31 @@ class TaskCommands(object): 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) diff --git a/tests/cmd/commands/test_deployment.py b/tests/cmd/commands/test_deployment.py index 7a9b4e12a2..bf1003347a 100644 --- a/tests/cmd/commands/test_deployment.py +++ b/tests/cmd/commands/test_deployment.py @@ -154,11 +154,33 @@ class DeploymentCommandsTestCase(test.TestCase): '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') diff --git a/tests/cmd/commands/test_task.py b/tests/cmd/commands/test_task.py index 3f6895e9f6..5f626becff 100644 --- a/tests/cmd/commands/test_task.py +++ b/tests/cmd/commands/test_task.py @@ -135,13 +135,46 @@ class TaskCommandsTestCase(test.TestCase): 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')