Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

* Fix broken ``st2 action-alias execute`` command and make sure it works
correctly. (bug fix) #5138

Expand Down
8 changes: 7 additions & 1 deletion st2client/st2client/commands/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -550,7 +556,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
Expand Down
39 changes: 39 additions & 0 deletions st2client/tests/unit/test_command_actionrun.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import copy

import unittest2
import six
import mock

from st2client.commands.action import ActionRunCommand
Expand Down Expand Up @@ -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

Expand Down