Skip to content

Commit

Permalink
Add test cases for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kami committed Feb 8, 2021
1 parent 7483c30 commit 32d3c8d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 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 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

0 comments on commit 32d3c8d

Please sign in to comment.