Skip to content

Commit

Permalink
poorly formatted .conf file will syslog a message but will
Browse files Browse the repository at this point in the history
not break the execution.
fixes #38
  • Loading branch information
grybak-arista committed Jan 21, 2016
1 parent ce506dc commit d7cc20c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
12 changes: 9 additions & 3 deletions pyeapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@
# Try Python 3.x import first
# Note: SafeConfigParser is deprecated and replaced by ConfigParser
from configparser import ConfigParser as SafeConfigParser
from configparser import MissingSectionHeaderError
except ImportError:
# Use Python 2.7 import as a fallback
from ConfigParser import SafeConfigParser
from ConfigParser import SafeConfigParser, MissingSectionHeaderError

from pyeapi.utils import load_module, make_iterable
from pyeapi.utils import load_module, make_iterable, debug

from pyeapi.eapilib import HttpEapiConnection, HttpsEapiConnection
from pyeapi.eapilib import SocketEapiConnection, HttpLocalEapiConnection
Expand Down Expand Up @@ -191,7 +192,12 @@ def read(self, filename):
Args:
filename (str): The full path to the file to load
"""
SafeConfigParser.read(self, filename)
try:
SafeConfigParser.read(self, filename)
except MissingSectionHeaderError:
# Ignore file and log message on MissingSectionHeaderError
debug("File contains no section headers: in eapi conf file: %s" %
filename)

self._add_default_connection()

Expand Down
3 changes: 1 addition & 2 deletions pyeapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def islocalconnection():
return os.path.exists('/etc/Eos-release')

def debug(text):
"""Prints test to syslog when on a local connection
"""Prints text to syslog when on a local connection
Args:
text (str): The string object to print to syslog
Expand Down Expand Up @@ -231,4 +231,3 @@ def collapse_range(arg, value_delimiter=',', range_delimiter='-'):
else:
values.extend([v1])
return [str(x) for x in values]

6 changes: 6 additions & 0 deletions test/fixtures/eapi.conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
:username: admin
:password: admin
:use_ssl: true
:port: 199
:hostname: bogus
Empty file added test/fixtures/empty.conf
Empty file.
5 changes: 5 additions & 0 deletions test/fixtures/env_path.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[connection:env_path]
host: 172.16.131.40
username: admin
password: admin
transport: https
17 changes: 17 additions & 0 deletions test/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,23 @@ def test_load_config(self):
name = 'connection:%s' % name
self.assertIn(name, pyeapi.client.config.sections())

def test_load_config_empty_conf(self):
conf = get_fixture('empty.conf')
pyeapi.client.load_config(filename=conf)
conns = pyeapi.client.config.connections
self.assertEqual(conns, ['localhost'])

def test_load_config_yaml(self):
conf = get_fixture('eapi.conf.yaml')
pyeapi.client.load_config(filename=conf)
conns = pyeapi.client.config.connections
self.assertEqual(conns, ['localhost'])

def test_load_config_env_path(self):
os.environ['EAPI_CONF'] = get_fixture('env_path.conf')
pyeapi.client.config.autoload()
self.assertIn('connection:env_path', pyeapi.client.config.sections())

def test_config_always_has_default_connection(self):
conf = '/invalid.conf'
pyeapi.client.load_config(conf)
Expand Down

0 comments on commit d7cc20c

Please sign in to comment.