From 7483c3065346374ae94e5f02232e64073d6ab901 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Sun, 7 Feb 2021 21:33:45 +0100 Subject: [PATCH 1/4] Fix st2client @key=file_path.json notation. Previously it was broken, because it didn't correctly convert bytes to string which means all data would have b"" prefix which would break JSON and other parsing. This bug was likely introduced when adding support for Python 3. --- st2client/st2client/commands/action.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/st2client/st2client/commands/action.py b/st2client/st2client/commands/action.py index d23fe583d2..62bea27378 100644 --- a/st2client/st2client/commands/action.py +++ b/st2client/st2client/commands/action.py @@ -550,7 +550,7 @@ def read_file(file_path): with open(file_path, 'rb') as fp: content = fp.read() - return content + return content.decode("utf-8") def transform_object(value): # Also support simple key1=val1,key2=val2 syntax From 32d3c8df0cd54ec3b5d18061468198959e8d1001 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Mon, 8 Feb 2021 15:44:25 +0100 Subject: [PATCH 2/4] Add test cases for it. --- st2client/st2client/commands/action.py | 6 +++ .../tests/unit/test_command_actionrun.py | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/st2client/st2client/commands/action.py b/st2client/st2client/commands/action.py index 62bea27378..7a41d9e2eb 100644 --- a/st2client/st2client/commands/action.py +++ b/st2client/st2client/commands/action.py @@ -541,6 +541,12 @@ def _get_action_parameters_from_args(self, action, runner, args): action_ref_or_id = action.ref def read_file(file_path): + """ + Read file content and return content as string / unicode. + + NOTE: It's only mean to be used to read non-binary files since API right now doesn't + support passing binary data. + """ if not os.path.exists(file_path): raise ValueError('File "%s" doesn\'t exist' % (file_path)) diff --git a/st2client/tests/unit/test_command_actionrun.py b/st2client/tests/unit/test_command_actionrun.py index 4163b98ca5..aa62d851e4 100644 --- a/st2client/tests/unit/test_command_actionrun.py +++ b/st2client/tests/unit/test_command_actionrun.py @@ -17,6 +17,7 @@ import copy import unittest2 +import six import mock from st2client.commands.action import ActionRunCommand @@ -161,6 +162,44 @@ def test_get_params_from_args(self): # set auto_dict back to default mockarg.auto_dict = False + def test_get_params_from_args_read_content_from_file(self): + runner = RunnerType() + runner.runner_parameters = {} + + action = Action() + action.ref = 'test.action' + action.parameters = { + 'param_object': {'type': 'object'}, + } + + subparser = mock.Mock() + command = ActionRunCommand(action, self, subparser, name='test') + + # 1. File doesn't exist + mockarg = mock.Mock() + mockarg.inherit_env = False + mockarg.auto_dict = True + mockarg.parameters = [ + '@param_object=doesnt-exist.json' + ] + + self.assertRaisesRegex(ValueError, "doesn't exist", + command._get_action_parameters_from_args, action=action, + runner=runner, args=mockarg) + + # 2. Valid file path (we simply read this file) + mockarg = mock.Mock() + mockarg.inherit_env = False + mockarg.auto_dict = True + mockarg.parameters = [ + '@param_string=%s' % (__file__) + ] + + params = command._get_action_parameters_from_args(action=action, + runner=runner, args=mockarg) + self.assertTrue(isinstance(params["param_string"], six.text_type)) + self.assertTrue(params["param_string"].startswith("# Copyright")) + def test_get_params_from_args_with_multiple_declarations(self): """test_get_params_from_args_with_multiple_declarations From 64a3023a1adfefcb4345a74a60ab39f6c6259c72 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Mon, 8 Feb 2021 15:47:27 +0100 Subject: [PATCH 3/4] Add changelog entry. --- CHANGELOG.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 94db66b6db..949e327cc2 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -53,6 +53,11 @@ Fixed * StackStorm now explicitly decodes pack files as utf-8 instead of implicitly as ascii (bug fix) #5106, #5107 +* Fix ``@parameter_name=/path/to/file/foo.json`` notation in the ``st2 run`` command which didn't + work correctly because it didn't convert read bytes to string / unicode type. (bug fix) #5140 + + Contributed by @kami. + Removed ~~~~~~~~ * Removed --python3 pack install option #5100 From ed9a9845ebf8494a0682639b48f2612caf1f0872 Mon Sep 17 00:00:00 2001 From: Tomaz Muraus Date: Mon, 8 Feb 2021 15:51:16 +0100 Subject: [PATCH 4/4] Fix lint. --- st2client/tests/unit/test_command_actionrun.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/st2client/tests/unit/test_command_actionrun.py b/st2client/tests/unit/test_command_actionrun.py index aa62d851e4..763ac649a6 100644 --- a/st2client/tests/unit/test_command_actionrun.py +++ b/st2client/tests/unit/test_command_actionrun.py @@ -184,8 +184,8 @@ def test_get_params_from_args_read_content_from_file(self): ] self.assertRaisesRegex(ValueError, "doesn't exist", - command._get_action_parameters_from_args, action=action, - runner=runner, args=mockarg) + command._get_action_parameters_from_args, action=action, + runner=runner, args=mockarg) # 2. Valid file path (we simply read this file) mockarg = mock.Mock()