Skip to content

Commit

Permalink
Disable config parser interpolation
Browse files Browse the repository at this point in the history
  • Loading branch information
Darko Poljak committed Jun 13, 2018
1 parent b9a48c9 commit 5aa8dac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cdist/configuration.py
Expand Up @@ -382,7 +382,7 @@ def get_args(self, section='GLOBAL'):
return args

def _read_config_file(self, files):
config_parser = configparser.ConfigParser()
config_parser = configparser.ConfigParser(interpolation=None)
config_parser.read(files)
d = dict()
for section in config_parser.sections():
Expand Down
66 changes: 41 additions & 25 deletions cdist/test/configuration/__init__.py
Expand Up @@ -31,6 +31,11 @@

my_dir = op.abspath(op.dirname(__file__))
fixtures = op.join(my_dir, 'fixtures')
interpolation_config_file = op.join(fixtures, "interpolation-test.cfg")


def newConfigParser():
return configparser.ConfigParser(interpolation=None)


class ConfigurationOptionsTestCase(test.CdistTestCase):
Expand Down Expand Up @@ -141,7 +146,7 @@ class ConfigurationTestCase(test.CdistTestCase):

def setUp(self):
# Create test config file.
config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'off',
'local_shell': '/bin/sh',
Expand All @@ -159,13 +164,13 @@ def setUp(self):
'verbosity': 'INFO',
'archiving': 'none',
}
config_custom = configparser.ConfigParser()
config_custom = newConfigParser()
config_custom['GLOBAL'] = {
'parallel': '4',
'archiving': 'txz',
}

config_custom2 = configparser.ConfigParser()
config_custom2 = newConfigParser()
config_custom2['GLOBAL'] = {
'parallel': '16',
'archiving': 'tbz2',
Expand Down Expand Up @@ -405,7 +410,7 @@ def test_configuration2(self):
}
args = argparse.Namespace()

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'off',
'local_shell': '/bin/sh',
Expand Down Expand Up @@ -463,7 +468,7 @@ def test_configuration3(self):
}
args = argparse.Namespace()

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'off',
'local_shell': '/bin/sh',
Expand All @@ -486,7 +491,7 @@ def test_configuration3(self):
with open(global_config_file, 'w') as f:
config.write(f)

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'on',
'local_shell': '/usr/bin/sh',
Expand Down Expand Up @@ -544,7 +549,7 @@ def test_configuration4(self):
}
args = argparse.Namespace()

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'off',
'local_shell': '/bin/sh',
Expand Down Expand Up @@ -611,7 +616,7 @@ def test_configuration5(self):
}
args = argparse.Namespace()

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'off',
'local_shell': '/bin/sh',
Expand All @@ -634,7 +639,7 @@ def test_configuration5(self):
with open(global_config_file, 'w') as f:
config.write(f)

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'on',
'local_shell': '/usr/bin/sh',
Expand Down Expand Up @@ -709,7 +714,7 @@ def test_configuration6(self):
}
args = argparse.Namespace()

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'off',
'local_shell': '/bin/sh',
Expand All @@ -732,7 +737,7 @@ def test_configuration6(self):
with open(global_config_file, 'w') as f:
config.write(f)

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'on',
'local_shell': '/usr/bin/sh',
Expand Down Expand Up @@ -800,7 +805,7 @@ def test_configuration7(self):
}
args = argparse.Namespace()

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'off',
'local_shell': '/bin/sh',
Expand All @@ -823,7 +828,7 @@ def test_configuration7(self):
with open(global_config_file, 'w') as f:
config.write(f)

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'on',
'local_shell': '/usr/bin/sh',
Expand All @@ -840,7 +845,7 @@ def test_configuration7(self):
with open(local_config_file, 'w') as f:
config.write(f)

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'conf_dir': '/opt/conf/cdist',
'remote_copy': 'scpcustom',
Expand Down Expand Up @@ -899,7 +904,7 @@ def test_configuration8(self):
}
args = argparse.Namespace()

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'off',
'local_shell': '/bin/sh',
Expand All @@ -922,7 +927,7 @@ def test_configuration8(self):
with open(global_config_file, 'w') as f:
config.write(f)

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'on',
'local_shell': '/usr/bin/sh',
Expand All @@ -939,7 +944,7 @@ def test_configuration8(self):
with open(local_config_file, 'w') as f:
config.write(f)

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'conf_dir': '/opt/conf/cdist',
'remote_copy': 'scpcustom',
Expand Down Expand Up @@ -998,7 +1003,7 @@ def test_configuration_get_args(self):
}
args = argparse.Namespace()

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'off',
'local_shell': '/bin/sh',
Expand All @@ -1021,7 +1026,7 @@ def test_configuration_get_args(self):
with open(global_config_file, 'w') as f:
config.write(f)

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'beta': 'on',
'local_shell': '/usr/bin/sh',
Expand All @@ -1038,7 +1043,7 @@ def test_configuration_get_args(self):
with open(local_config_file, 'w') as f:
config.write(f)

config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'conf_dir': '/opt/conf/cdist',
'remote_copy': 'scpcustom',
Expand Down Expand Up @@ -1107,7 +1112,7 @@ def test_configuration_get_args(self):
self.assertEqual(dargs, expected_args)

def test_configuration_empty_value_in_file(self):
config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'inventory_dir': '',
'conf_dir': '',
Expand Down Expand Up @@ -1169,7 +1174,7 @@ def test_configuration_cdist_log_level_env_var(self):
config_files=())

def test_configuration_disable_saving_output_streams1(self):
config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'save_output_streams': 'True',
}
Expand Down Expand Up @@ -1197,7 +1202,7 @@ def test_configuration_disable_saving_output_streams1(self):
self.assertEqual(configuration.config, expected_config_dict)

def test_configuration_disable_saving_output_streams2(self):
config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'save_output_streams': 'False',
}
Expand Down Expand Up @@ -1225,7 +1230,7 @@ def test_configuration_disable_saving_output_streams2(self):
self.assertEqual(configuration.config, expected_config_dict)

def test_configuration_disable_saving_output_streams3(self):
config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'save_output_streams': 'False',
}
Expand Down Expand Up @@ -1253,7 +1258,7 @@ def test_configuration_disable_saving_output_streams3(self):
self.assertEqual(configuration.config, expected_config_dict)

def test_configuration_disable_saving_output_streams4(self):
config = configparser.ConfigParser()
config = newConfigParser()
config['GLOBAL'] = {
'save_output_streams': 'True',
}
Expand All @@ -1280,6 +1285,17 @@ def test_configuration_disable_saving_output_streams4(self):
config_files=config_files)
self.assertEqual(configuration.config, expected_config_dict)

def test_read_config_file_with_interpolation(self):
try:
config = cc.Configuration(None, env={}, config_files=())
d = config._read_config_file(interpolation_config_file)
val = d['GLOBAL']['cache_path_pattern']
self.assertIsNotNone(val)
self.assertEqual(val, '%N')
except configparser.InterpolationSyntaxError as e:
self.fail("Exception should not have been raised: {}".format(
e))


if __name__ == "__main__":
import unittest
Expand Down
2 changes: 2 additions & 0 deletions cdist/test/configuration/fixtures/interpolation-test.cfg
@@ -0,0 +1,2 @@
[GLOBAL]
cache_path_pattern = %N
1 change: 1 addition & 0 deletions docs/changelog
Expand Up @@ -3,6 +3,7 @@ Changelog

next:
* New type: __acl (Ander Punnar)
* Core: Disable config parser interpolation (Darko Poljak)

4.9.1: 2018-05-30
* New type: __install_coreos (Ľubomír Kučera)
Expand Down

0 comments on commit 5aa8dac

Please sign in to comment.