From abdadd075fca6478ae26bc19c18b4d5332ed9ed4 Mon Sep 17 00:00:00 2001 From: Jiri Podivin Date: Wed, 5 May 2021 11:18:27 +0200 Subject: [PATCH] New optional param for log path As we now have a mechanism for falling back to a default directory for logs and artifacts, we can allow the customers to specify their own. This patch add the argument to the CLI along with minimum necessary information about the usage. Signed-off-by: Jiri Podivin Change-Id: Id31a7ce2dfc4d9a132fea7d600e8beb8adc0739b --- validations_libs/cli/run.py | 12 +++++++- validations_libs/tests/cli/test_run.py | 42 ++++++++++++++++++-------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/validations_libs/cli/run.py b/validations_libs/cli/run.py index 6df03af7..d94cd704 100644 --- a/validations_libs/cli/run.py +++ b/validations_libs/cli/run.py @@ -79,6 +79,15 @@ class Run(Command): "will override the other(s)") ) + parser.add_argument( + '--validation-log-dir', + dest='log_path', + default=constants.VALIDATIONS_LOG_BASEDIR, + help=( + "Path where the log files and artifacts will be located. "), + metavar='' + ) + extra_vars_group = parser.add_mutually_exclusive_group(required=False) extra_vars_group.add_argument( '--extra-vars', @@ -158,7 +167,8 @@ class Run(Command): validation_name=parsed_args.validation_name, extra_env_vars=parsed_args.extra_env_vars, quiet=True, - ssh_user=parsed_args.ssh_user) + ssh_user=parsed_args.ssh_user, + log_path=parsed_args.log_path) except RuntimeError as e: raise RuntimeError(e) diff --git a/validations_libs/tests/cli/test_run.py b/validations_libs/tests/cli/test_run.py index 7a34828a..ebde0bcb 100644 --- a/validations_libs/tests/cli/test_run.py +++ b/validations_libs/tests/cli/test_run.py @@ -56,13 +56,15 @@ class TestRun(BaseCommand): self.assertRaises(Exception, self.check_parser, self.cmd, arglist, verifylist) + @mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR') @mock.patch('validations_libs.cli.common.print_dict') @mock.patch('getpass.getuser', return_value='doe') @mock.patch('validations_libs.validation_actions.ValidationActions.' 'run_validations', return_value=fakes.FAKE_SUCCESS_RUN) - def test_run_command_extra_vars(self, mock_run, mock_user, mock_print): + def test_run_command_extra_vars(self, mock_run, mock_user, mock_print, + mock_log_dir): run_called_args = { 'inventory': 'localhost', 'limit_hosts': None, @@ -73,7 +75,8 @@ class TestRun(BaseCommand): 'validation_name': ['foo'], 'extra_env_vars': None, 'quiet': True, - 'ssh_user': 'doe'} + 'ssh_user': 'doe', + 'log_path': mock_log_dir} arglist = ['--validation', 'foo', '--extra-vars', 'key=value'] @@ -84,6 +87,7 @@ class TestRun(BaseCommand): self.cmd.take_action(parsed_args) mock_run.assert_called_with(**run_called_args) + @mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR') @mock.patch('validations_libs.cli.common.print_dict') @mock.patch('getpass.getuser', return_value='doe') @@ -91,7 +95,7 @@ class TestRun(BaseCommand): 'run_validations', return_value=fakes.FAKE_SUCCESS_RUN) def test_run_command_extra_vars_twice(self, mock_run, mock_user, - mock_print): + mock_print, mock_log_dir): run_called_args = { 'inventory': 'localhost', 'limit_hosts': None, @@ -102,7 +106,8 @@ class TestRun(BaseCommand): 'validation_name': ['foo'], 'extra_env_vars': None, 'quiet': True, - 'ssh_user': 'doe'} + 'ssh_user': 'doe', + 'log_path': mock_log_dir} arglist = ['--validation', 'foo', '--extra-vars', 'key=value1', @@ -124,6 +129,7 @@ class TestRun(BaseCommand): self.assertRaises(Exception, self.check_parser, self.cmd, arglist, verifylist) + @mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR') @mock.patch('yaml.safe_load', return_value={'key': 'value'}) @mock.patch('six.moves.builtins.open') @mock.patch('getpass.getuser', @@ -132,7 +138,8 @@ class TestRun(BaseCommand): 'run_validations', return_value=fakes.FAKE_SUCCESS_RUN) def test_run_command_extra_vars_file(self, mock_run, mock_user, mock_open, - mock_yaml): + mock_yaml, mock_log_dir): + run_called_args = { 'inventory': 'localhost', 'limit_hosts': None, @@ -143,7 +150,8 @@ class TestRun(BaseCommand): 'validation_name': ['foo'], 'extra_env_vars': None, 'quiet': True, - 'ssh_user': 'doe'} + 'ssh_user': 'doe', + 'log_path': mock_log_dir} arglist = ['--validation', 'foo', '--extra-vars-file', '/foo/vars.yaml'] @@ -154,12 +162,13 @@ class TestRun(BaseCommand): self.cmd.take_action(parsed_args) mock_run.assert_called_with(**run_called_args) + @mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR') @mock.patch('getpass.getuser', return_value='doe') @mock.patch('validations_libs.validation_actions.ValidationActions.' 'run_validations', return_value=fakes.FAKE_SUCCESS_RUN) - def test_run_command_extra_env_vars(self, mock_run, mock_user): + def test_run_command_extra_env_vars(self, mock_run, mock_user, mock_log_dir): run_called_args = { 'inventory': 'localhost', 'limit_hosts': None, @@ -170,7 +179,8 @@ class TestRun(BaseCommand): 'validation_name': ['foo'], 'extra_env_vars': {'key': 'value'}, 'quiet': True, - 'ssh_user': 'doe'} + 'ssh_user': 'doe', + 'log_path': mock_log_dir} arglist = ['--validation', 'foo', '--extra-env-vars', 'key=value'] @@ -181,12 +191,13 @@ class TestRun(BaseCommand): self.cmd.take_action(parsed_args) mock_run.assert_called_with(**run_called_args) + @mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR') @mock.patch('getpass.getuser', return_value='doe') @mock.patch('validations_libs.validation_actions.ValidationActions.' 'run_validations', return_value=fakes.FAKE_SUCCESS_RUN) - def test_run_command_extra_env_vars_twice(self, mock_run, mock_user): + def test_run_command_extra_env_vars_twice(self, mock_run, mock_user, mock_log_dir): run_called_args = { 'inventory': 'localhost', 'limit_hosts': None, @@ -197,7 +208,8 @@ class TestRun(BaseCommand): 'validation_name': ['foo'], 'extra_env_vars': {'key': 'value2'}, 'quiet': True, - 'ssh_user': 'doe'} + 'ssh_user': 'doe', + 'log_path': mock_log_dir} arglist = ['--validation', 'foo', '--extra-env-vars', 'key=value1', @@ -209,13 +221,16 @@ class TestRun(BaseCommand): self.cmd.take_action(parsed_args) mock_run.assert_called_with(**run_called_args) + @mock.patch('validations_libs.constants.VALIDATIONS_LOG_BASEDIR') @mock.patch('getpass.getuser', return_value='doe') @mock.patch('validations_libs.validation_actions.ValidationActions.' 'run_validations', return_value=fakes.FAKE_SUCCESS_RUN) - def test_run_command_extra_env_vars_and_extra_vars(self, mock_run, - mock_user): + def test_run_command_extra_env_vars_and_extra_vars(self, + mock_run, + mock_user, + mock_log_dir): run_called_args = { 'inventory': 'localhost', 'limit_hosts': None, @@ -226,7 +241,8 @@ class TestRun(BaseCommand): 'validation_name': ['foo'], 'extra_env_vars': {'key2': 'value2'}, 'quiet': True, - 'ssh_user': 'doe'} + 'ssh_user': 'doe', + 'log_path': mock_log_dir} arglist = ['--validation', 'foo', '--extra-vars', 'key=value',