Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for expanding environment variables in configuration files. #378

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions prestoadmin/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ def get_conf_from_properties_data(data):
if len(line) > 0 and line[0] not in COMMENT_CHARS:
pair = split_to_pair(line)
props[pair[0]] = pair[1]
return expand_environment_variables(props)


def expand_environment_variables(props):
for key in props:
props[key] = os.path.expandvars(props[key])
return props


Expand Down
1 change: 1 addition & 0 deletions tests/unit/resources/environment_variable.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a=${ENVIRONMENT_VARIABLE}
3 changes: 2 additions & 1 deletion tests/unit/resources/valid.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
prop1
prop2
prop3
prop3
${ENVIRONMENT_VARIABLE}
2 changes: 1 addition & 1 deletion tests/unit/resources/valid.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ f===6
g:= 7
h=:8
i = 9

j=${ENVIRONMENT_VARIABLE}
12 changes: 10 additions & 2 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,22 @@ def test_invalid_json(self):
def test_get_config(self):
config_file = os.path.join(DIR, 'resources', 'valid.config')
conf = config.get_conf_from_config_file(config_file)
self.assertEqual(conf, ['prop1', 'prop2', 'prop3'])
self.assertEqual(conf, ['prop1', 'prop2', 'prop3', '${ENVIRONMENT_VARIABLE}'])

def test_get_properties(self):
config_file = os.path.join(DIR, 'resources', 'valid.properties')
conf = config.get_conf_from_properties_file(config_file)
self.assertEqual(conf, {'a': '1', 'b': '2', 'c': '3',
'd\\=': '4', 'e\\:': '5', 'f': '==6',
'g': '= 7', 'h': ':8', 'i': '9'})
'g': '= 7', 'h': ':8', 'i': '9', 'j': '${ENVIRONMENT_VARIABLE}'})

@patch('prestoadmin.config.os.path.expandvars', return_value='environment_variable_value')
def test_expending_environment_variable(self, environment_variables):
config_file = os.path.join(DIR, 'resources', 'environment_variable.properties')
conf = config.get_conf_from_properties_file(config_file)
self.assertTrue(environment_variables.called)
self.assertEqual({'a': 'environment_variable_value'}, conf)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have an real test that is using actual environment variable, to see the configuration with expanded environment variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply. I will work on this as well as automation failures.



@patch('__builtin__.open')
def test_get_properties_ignores_whitespace(self, open_mock):
Expand Down