From b439b4d12745db7bdf3d70a8404209565d3c396d Mon Sep 17 00:00:00 2001 From: Tzanetos Balitsaris Date: Tue, 28 Oct 2014 15:41:20 +0200 Subject: [PATCH] Modify rally commands deployment, task, and verify Small modifications to the rally commands 'deployment', 'task', and 'verify'. In deployment, rename the subcommand endpoint to show, and remove the pprint and json output options in subcommand config in favor of pretty-printed json output only. In task, perform the same in subcommand results as in config. In verify, perform the same in subcommand results as in config. Change-Id: I9686abf5088782e68111638da10be72c0e1135fa --- rally/cmd/commands/deployment.py | 26 ++++------------ rally/cmd/commands/task.py | 20 ++----------- rally/cmd/commands/verify.py | 26 +++++----------- tests/functional/test_cli_deployment.py | 4 +-- tests/unit/cmd/commands/test_deployment.py | 31 ++++--------------- tests/unit/cmd/commands/test_task.py | 35 ++-------------------- tests/unit/cmd/commands/test_verify.py | 17 +---------- tools/rally.bash_completion | 8 ++--- 8 files changed, 32 insertions(+), 135 deletions(-) diff --git a/rally/cmd/commands/deployment.py b/rally/cmd/commands/deployment.py index 660a3b9d04..b9de7c1f9f 100644 --- a/rally/cmd/commands/deployment.py +++ b/rally/cmd/commands/deployment.py @@ -19,7 +19,6 @@ from __future__ import print_function import json import os -import pprint import sys import jsonschema @@ -173,37 +172,24 @@ 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, output_json=None, output_pprint=None): + def config(self, deploy_id=None): """Display configuration of the deployment. - Output can JSON or Pretty print format. + Output is the configuration of the deployment in a + pretty-printed JSON 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) 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)) + print(json.dumps(result, sort_keys=True, indent=4)) @cliutils.args('--uuid', dest='deploy_id', type=str, required=False, help='UUID of a deployment.') @envutils.with_default_deploy_id - def endpoint(self, deploy_id=None): - """Display all endpoints of the deployment. + def show(self, deploy_id=None): + """Show the endpoints of the deployment. :param deploy_id: a UUID of the deployment """ diff --git a/rally/cmd/commands/task.py b/rally/cmd/commands/task.py index 6286eb04eb..6906aa8196 100644 --- a/rally/cmd/commands/task.py +++ b/rally/cmd/commands/task.py @@ -304,19 +304,13 @@ 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('--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, output_pprint=None, output_json=None): - """Diplay raw task results. + def results(self, task_id=None): + """Display raw task results. This will produce a lot of output data about every iteration. :param task_id: Task uuid - :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'], @@ -324,15 +318,7 @@ class TaskCommands(object): db.task_result_get_all_by_uuid(task_id)) if results: - 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(json.dumps(results)) + print(json.dumps(results, sort_keys=True, indent=4)) else: print(_("The task %s can not be found") % task_id) return(1) diff --git a/rally/cmd/commands/verify.py b/rally/cmd/commands/verify.py index 6040ba04a2..38b8e0e9a7 100644 --- a/rally/cmd/commands/verify.py +++ b/rally/cmd/commands/verify.py @@ -17,7 +17,6 @@ import json import os -import pprint import six @@ -90,26 +89,21 @@ class VerifyCommands(object): @cliutils.args('--uuid', type=str, dest='verification_uuid', help='UUID of the verification') @cliutils.args('--html', action='store_true', dest='output_html', - help=('Save results in html format to specified file')) + help=('Results will be in html format')) @cliutils.args('--json', action='store_true', dest='output_json', - help=('Save results in json format to specified file')) - @cliutils.args('--pprint', action='store_true', dest='output_pprint', - help=('Save results in pprint format to specified file')) + help=('Results will be in json format')) @cliutils.args('--output-file', type=str, required=False, dest='output_file', help='If specified, output will be saved to given file') @envutils.with_default_verification_id def results(self, verification_uuid=None, output_file=None, - output_html=None, output_json=None, output_pprint=None): + output_html=None, output_json=None): """Get raw results of the verification. :param verification_uuid: Verification UUID :param output_file: If specified, output will be saved to given file - :param output_html: Save results in html format to the specified file - :param output_json: Save results in json format to the specified file - (Default) - :param output_pprint: Save results in pprint format to the - specified file + :param output_html: The output will be in HTML format + :param output_json: The output will be in JSON format (Default) """ try: @@ -119,16 +113,12 @@ class VerifyCommands(object): return 1 result = '' - if len(filter(lambda x: bool(x), [output_json, output_pprint, - output_html])) > 1: - print("Please specify only on output format") - return 1 - elif output_pprint: - result = pprint.pformat(results) + if len(filter(lambda x: bool(x), [output_json, output_html])) > 1: + print("Please specify only one output format.") elif output_html: result = json2html.main(results) else: - result = json.dumps(results) + result = json.dumps(results, sort_keys=True, indent=4) if output_file: output_file = os.path.expanduser(output_file) diff --git a/tests/functional/test_cli_deployment.py b/tests/functional/test_cli_deployment.py index 71f45a98d2..6c536b53e0 100644 --- a/tests/functional/test_cli_deployment.py +++ b/tests/functional/test_cli_deployment.py @@ -27,12 +27,12 @@ class DeploymentTestCase(unittest.TestCase): super(DeploymentTestCase, self).setUp() self.rally = utils.Rally() - def test_create_fromenv_list_endpoint(self): + def test_create_fromenv_list_show(self): with mock.patch.dict("os.environ", utils.TEST_ENV): self.rally("deployment create --name t_create_env --fromenv") self.assertIn("t_create_env", self.rally("deployment list")) self.assertIn(utils.TEST_ENV["OS_AUTH_URL"], - self.rally("deployment endpoint")) + self.rally("deployment show")) def test_create_fromfile(self): with mock.patch.dict("os.environ", utils.TEST_ENV): diff --git a/tests/unit/cmd/commands/test_deployment.py b/tests/unit/cmd/commands/test_deployment.py index 029dc30657..bed516d2bd 100644 --- a/tests/unit/cmd/commands/test_deployment.py +++ b/tests/unit/cmd/commands/test_deployment.py @@ -156,32 +156,13 @@ class DeploymentCommandsTestCase(test.TestCase): @mock.patch('rally.cmd.commands.deployment.db.deployment_get') @mock.patch('json.dumps') - def test_config_default(self, mock_json_dumps, mock_deployment): + def test_config(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_json_dumps.assert_called_once_with(value['config'], + sort_keys=True, indent=4) mock_deployment.assert_called_once_with(deploy_id) @mock.patch('rally.cmd.commands.deployment.envutils.get_global') @@ -193,7 +174,7 @@ class DeploymentCommandsTestCase(test.TestCase): @mock.patch('rally.cmd.commands.deployment.common_cliutils.print_list') @mock.patch('rally.cmd.commands.deployment.utils.Struct') @mock.patch('rally.cmd.commands.deployment.db.deployment_get') - def test_endpoint(self, mock_deployment, mock_struct, mock_print_list): + def test_show(self, mock_deployment, mock_struct, mock_print_list): deploy_id = "b1a6153e-a314-4cb3-b63b-cf08c1a416c3" value = { "admin": { @@ -208,7 +189,7 @@ class DeploymentCommandsTestCase(test.TestCase): "users": [] } mock_deployment.return_value = value - self.deployment.endpoint(deploy_id) + self.deployment.show(deploy_id) mock_deployment.assert_called_once_with(deploy_id) headers = ["auth_url", "username", "password", "tenant_name", @@ -222,4 +203,4 @@ class DeploymentCommandsTestCase(test.TestCase): def test_deploy_no_deploy_id(self, mock_default): mock_default.side_effect = exceptions.InvalidArgumentsException self.assertRaises(exceptions.InvalidArgumentsException, - self.deployment.endpoint, None) + self.deployment.show, None) diff --git a/tests/unit/cmd/commands/test_task.py b/tests/unit/cmd/commands/test_task.py index 7b6b744655..14a2678523 100644 --- a/tests/unit/cmd/commands/test_task.py +++ b/tests/unit/cmd/commands/test_task.py @@ -136,7 +136,7 @@ class TaskCommandsTestCase(test.TestCase): @mock.patch('rally.cmd.commands.task.db') @mock.patch('json.dumps') - def test_results_default(self, mock_json, mock_db): + def test_results(self, mock_json, mock_db): test_uuid = 'aa808c14-69cc-4faf-a906-97e05f5aebbd' value = [ {'key': 'key', 'data': {'raw': 'raw', 'sla': []}} @@ -146,38 +146,7 @@ class TaskCommandsTestCase(test.TestCase): "sla": x["data"]["sla"]}, 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', 'sla': []}} - ] - result = map(lambda x: {"key": x["key"], - "result": x["data"]["raw"], - "sla": x["data"]["sla"]}, 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', 'sla': []}} - ] - result = map(lambda x: {"key": x["key"], - "result": x["data"]["raw"], - "sla": x["data"]["sla"]}, 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_json.assert_called_once_with(result, sort_keys=True, indent=4) mock_db.task_result_get_all_by_uuid.assert_called_once_with(test_uuid) @mock.patch('rally.cmd.commands.task.db') diff --git a/tests/unit/cmd/commands/test_verify.py b/tests/unit/cmd/commands/test_verify.py index 5496262a12..502e479a83 100644 --- a/tests/unit/cmd/commands/test_verify.py +++ b/tests/unit/cmd/commands/test_verify.py @@ -146,7 +146,7 @@ class VerifyCommandsTestCase(test.TestCase): self.verify.results(verification_uuid, output_json=True) mock_db_result_get.assert_called_once_with(verification_uuid) - mock_json_dumps.assert_called_once_with({}) + mock_json_dumps.assert_called_once_with({}, sort_keys=True, indent=4) @mock.patch('rally.db.verification_result_get') def test_results_verification_not_found(self, mock_db_result_get): @@ -171,21 +171,6 @@ class VerifyCommandsTestCase(test.TestCase): fake_file = mock_open.return_value.__enter__.return_value fake_file.write.assert_called_once_with('{}') - @mock.patch('rally.cmd.commands.verify.open', create=True) - @mock.patch('rally.db.verification_result_get', return_value={'data': {}}) - def test_results_with_output_pprint_and_output_file(self, - mock_db_result_get, - mock_open): - mock_open.return_value = mock.MagicMock() - verification_uuid = 'fa882ccc-153e-4a6e-9001-91fecda6a75c' - self.verify.results(verification_uuid, output_pprint=True, - output_file='results') - - mock_db_result_get.assert_called_once_with(verification_uuid) - mock_open.assert_called_once_with('results', 'wb') - fake_file = mock_open.return_value.__enter__.return_value - fake_file.write.assert_called_once_with('{}') - @mock.patch('rally.cmd.commands.verify.open', create=True) @mock.patch('rally.db.verification_result_get') @mock.patch('rally.verification.verifiers.tempest.json2html.main', diff --git a/tools/rally.bash_completion b/tools/rally.bash_completion index 947943b00e..af84cae65e 100644 --- a/tools/rally.bash_completion +++ b/tools/rally.bash_completion @@ -17,7 +17,7 @@ _rally() OPTS["task_list"]="" OPTS["task_plot2html"]="--uuid --out --open" OPTS["task_report"]="--uuid --out --open" - OPTS["task_results"]="--uuid --pprint --json" + OPTS["task_results"]="--uuid" OPTS["task_sla_check"]="--uuid --json" OPTS["task_start"]="--deploy-id --task --tag --no-use" OPTS["task_status"]="--uuid" @@ -29,16 +29,16 @@ _rally() OPTS["show_secgroups"]="--deploy-id" OPTS["verify_detailed"]="--uuid --sort-by" OPTS["verify_list"]="" - OPTS["verify_results"]="--uuid --html --json --pprint --output-file" + OPTS["verify_results"]="--uuid --html --json --output-file" OPTS["verify_show"]="--uuid --sort-by --detailed" OPTS["verify_start"]="--deploy-id --set --regex --tempest-config --no-use" OPTS["deployment_check"]="--uuid" - OPTS["deployment_config"]="--uuid --json --pprint" + OPTS["deployment_config"]="--uuid" OPTS["deployment_create"]="--name --fromenv --filename --no-use" OPTS["deployment_destroy"]="--uuid" - OPTS["deployment_endpoint"]="--uuid" OPTS["deployment_list"]="" OPTS["deployment_recreate"]="--uuid" + OPTS["deployment_show"]="--uuid" for OPT in ${!OPTS[*]} ; do