Skip to content

Commit

Permalink
Merge pull request #1355 from anarkiwi/master
Browse files Browse the repository at this point in the history
Handle permission denied reading config.
  • Loading branch information
anarkiwi committed Dec 4, 2017
2 parents 08c033c + 827d711 commit 314052f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
2 changes: 1 addition & 1 deletion faucet/config_parser_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def read_config(config_file, logname):
try:
with open(config_file, 'r') as stream:
conf = yaml.safe_load(stream)
except (yaml.YAMLError, UnicodeDecodeError) as err:
except (yaml.YAMLError, UnicodeDecodeError, PermissionError) as err:
logger.error('Error in file %s (%s)', config_file, str(err))
return None
return conf
Expand Down
25 changes: 19 additions & 6 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,39 @@ def tearDown(self):
logging.disable(logging.NOTSET)
shutil.rmtree(self.tmpdir)

def conf_file_name(self):
return os.path.join(self.tmpdir, 'faucet.yaml')

def create_config_file(self, config):
"""Returns file path to file containing the config parameter."""
conf_file_name = os.path.join(self.tmpdir, 'faucet.yaml')
conf_file_name = self.conf_file_name()
with open(conf_file_name, 'wb') as conf_file:
if isinstance(config, bytes):
conf_file.write(config)
else:
conf_file.write(config.encode('utf-8'))
return conf_file_name

def run_function_with_config(self, config, function):
def run_function_with_config(self, config, function, before_function=None):
"""Return False if provided function raises InvalidConfigError."""
conf_file = self.create_config_file(config)
if before_function:
before_function()
try:
function(conf_file, LOGNAME)
except cp.InvalidConfigError:
return False
return True

def check_config_failure(self, config, function):
def check_config_failure(self, config, function, before_function=None):
"""Ensure config parsing reported as failed."""
self.assertEqual(
self.run_function_with_config(config, function), False)
self.run_function_with_config(config, function, before_function), False)

def check_config_success(self, config, function):
def check_config_success(self, config, function, before_function=None):
"""Ensure config parsing reported succeeded."""
self.assertEqual(
self.run_function_with_config(config, function), True)
self.run_function_with_config(config, function, before_function), True)

def test_config_contains_only_int(self):
"""Test that config is invalid when only an int"""
Expand Down Expand Up @@ -705,6 +710,14 @@ def test_invalid_char(self):
config = b'\x63\xe1'
self.check_config_failure(config, cp.dp_parser)

def test_perm_denied(self):

def unreadable():
os.chmod(self.conf_file_name(), 0)

config = ''
self.check_config_failure(config, cp.dp_parser, before_function=unreadable)


if __name__ == "__main__":
unittest.main()

0 comments on commit 314052f

Please sign in to comment.