Skip to content

Commit

Permalink
#141 added possibility to use scripts for parameter default values
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Feb 14, 2019
1 parent 760d929 commit 9a8e527
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 27 deletions.
26 changes: 18 additions & 8 deletions src/model/parameter_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from model.model_helper import resolve_env_vars, replace_auth_vars, is_empty, SECURE_MASK, \
normalize_extension, read_bool_from_config, InvalidValueException
from react.properties import ObservableDict, observable_fields
from utils import file_utils, string_utils
from utils import file_utils, string_utils, process_utils
from utils.string_utils import strip

LOGGER = logging.getLogger('script_server.parameter_config')
Expand Down Expand Up @@ -68,7 +68,7 @@ def _reload(self):
self.secure = read_bool_from_config('secure', config, default=False)
self.separator = config.get('separator', ',')
self.multiple_arguments = read_bool_from_config('multiple_arguments', config, default=False)
self.default = _resolve_default(config.get('default'), self._username, self._audit_name)
self.default = _resolve_default(config.get('default'), self._username, self._audit_name, self._working_dir)
self.file_dir = _resolve_file_dir(config, 'file_dir')
self._list_files_dir = _resolve_list_files_dir(self.file_dir, self._working_dir)
self.file_extensions = _resolve_file_extensions(config, 'file_extensions')
Expand Down Expand Up @@ -372,18 +372,28 @@ def _build_list_file_path(self, child_path):
return os.path.normpath(os.path.join(self._list_files_dir, *child_path))


def _resolve_default(default, username, audit_name):
def _resolve_default(default, username, audit_name, working_dir):
if not default:
return default

if not isinstance(default, str):
script = False
if isinstance(default, dict) and 'script' in default:
string_value = default['script']
script = True
elif isinstance(default, str):
string_value = default
else:
return default

resolved_env_default = resolve_env_vars(default, full_match=True)
if resolved_env_default != default:
return resolved_env_default
resolved_string_value = resolve_env_vars(string_value, full_match=True)
if resolved_string_value == string_value:
resolved_string_value = replace_auth_vars(string_value, username, audit_name)

return replace_auth_vars(default, username, audit_name)
if not script:
return resolved_string_value

output = process_utils.invoke(resolved_string_value, working_dir)
return output.strip()


def _resolve_file_dir(config, key):
Expand Down
68 changes: 49 additions & 19 deletions src/tests/parameter_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,79 +165,109 @@ def test_map_to_script_args_multiselect_single_arg(self):


class TestDefaultValue(unittest.TestCase):
env_key = 'test_val'

def test_no_value(self):
default = parameter_config._resolve_default(None, None, None)
default = self.resolve_default(None)

self.assertEqual(default, None)

def test_empty_value(self):
default = parameter_config._resolve_default('', None, None)
default = self.resolve_default('')

self.assertEqual(default, '')

def test_text_value(self):
default = parameter_config._resolve_default('text', None, None)
default = self.resolve_default('text')

self.assertEqual(default, 'text')

def test_unicode_value(self):
default = parameter_config._resolve_default(u'text', None, None)
default = self.resolve_default(u'text')

self.assertEqual(default, u'text')

def test_int_value(self):
default = parameter_config._resolve_default(5, None, None)
default = self.resolve_default(5)

self.assertEqual(default, 5)

def test_bool_value(self):
default = parameter_config._resolve_default(True, None, None)
default = self.resolve_default(True)

self.assertEqual(default, True)

def test_env_variable(self):
os.environ[self.env_key] = 'text'
test_utils.set_env_value('test_val', 'text')

default = parameter_config._resolve_default('$$test_val', None, None)
default = self.resolve_default('$$test_val')

self.assertEqual(default, 'text')

def test_missing_env_variable(self):
self.assertRaises(Exception, parameter_config._resolve_default, '$$test_val', None, None)
self.assertRaises(Exception, self.resolve_default, '$$test_val')

def test_auth_username(self):
default = parameter_config._resolve_default('${auth.username}', 'buggy', None)
default = self.resolve_default('${auth.username}', username='buggy')
self.assertEqual('buggy', default)

def test_auth_username_when_none(self):
default = parameter_config._resolve_default('${auth.username}', None, None)
default = self.resolve_default('${auth.username}')
self.assertEqual('', default)

def test_auth_username_when_inside_text(self):
default = parameter_config._resolve_default('__${auth.username}__', 'usx', None)
default = self.resolve_default('__${auth.username}__', username='usx')
self.assertEqual('__usx__', default)

def test_auth_audit_name(self):
default = parameter_config._resolve_default('${auth.audit_name}', None, '127.0.0.1')
default = self.resolve_default('${auth.audit_name}', audit_name='127.0.0.1')
self.assertEqual('127.0.0.1', default)

def test_auth_audit_name_when_none(self):
default = parameter_config._resolve_default('${auth.audit_name}', None, None)
default = self.resolve_default('${auth.audit_name}')
self.assertEqual('', default)

def test_auth_audit_name_when_inside_text(self):
default = parameter_config._resolve_default('__${auth.audit_name}__', None, 'usx')
default = self.resolve_default('__${auth.audit_name}__', audit_name='usx')
self.assertEqual('__usx__', default)

def test_auth_username_and_audit_name(self):
default = parameter_config._resolve_default('${auth.username}:${auth.audit_name}', 'buggy', 'localhost')
default = self.resolve_default('${auth.username}:${auth.audit_name}', username='buggy', audit_name='localhost')
self.assertEqual('buggy:localhost', default)

def test_script_value(self):
default = self.resolve_default({'script': 'echo 123'})
self.assertEqual('123', default)

def test_script_value_with_working_dir(self):
default = self.resolve_default({'script': 'pwd'}, working_dir=test_utils.temp_folder)
abs_temp_path = os.path.abspath(test_utils.temp_folder)
self.assertEqual(abs_temp_path, default)

def test_script_value_when_env_var(self):
test_utils.set_env_value('my_command', 'echo "Hello world"')

default = self.resolve_default({'script': '$$my_command'}, working_dir=test_utils.temp_folder)
self.assertEqual('Hello world', default)

def test_script_value_when_username(self):
default = self.resolve_default({'script': 'echo "x${auth.username}x"'},
working_dir=test_utils.temp_folder,
username='TONY')
self.assertEqual('xTONYx', default)

def test_script_value_with_shell_operators(self):
default = self.resolve_default({'script': 'echo 12345 | grep "1"'})
self.assertEqual('12345 | grep 1', default)

@staticmethod
def resolve_default(value, *, username=None, audit_name=None, working_dir=None):
return parameter_config._resolve_default(value, username, audit_name, working_dir)

def setUp(self):
test_utils.setup()

def tearDown(self):
if self.env_key in os.environ:
del os.environ[self.env_key]
test_utils.cleanup()


class TestSingleParameterValidation(unittest.TestCase):
Expand Down

0 comments on commit 9a8e527

Please sign in to comment.