Skip to content

Commit

Permalink
fix blatant obvious bugs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
infothrill committed Feb 16, 2015
1 parent fbad420 commit 5408d48
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dyndnsc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def collect_config(cfg):
# options passed "as is" to the dyndnsc client
collected_config[k] = client_cfg_dict[k]

collected_config[_detector_str] = detector_name, detector_options
collected_config[_detector_str] = [(detector_name, detector_options)]
collected_config[_updater_str] = [(updater_name, updater_options)]

collected_configs[client_name] = collected_config
Expand Down
15 changes: 9 additions & 6 deletions dyndnsc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from .plugins.manager import NullPluginManager
from .updater.manager import get_updater_class
from .detector.manager import get_detector_class


# Set default logging handler to avoid "No handler found" warnings.
Expand Down Expand Up @@ -164,8 +165,7 @@ def check(self):


def getDynDnsClientForConfig(config, plugins=None):
"""Factory detector_name to instantiate and initialize a complete and working
dyndns client
"""Instantiate and return a complete and working dyndns client.
:param config: a dictionary with configuration keys
:param plugins: an object that implements PluginManager
Expand All @@ -178,19 +178,22 @@ def getDynDnsClientForConfig(config, plugins=None):
if plugins is not None:
log.debug("Attaching plugins to dyndnsc")
dyndnsclient.plugins = plugins

if 'updater' not in config:
raise ValueError("No updater specified")
# require at least 1 updater:
if len(config['updater']) < 1:
raise ValueError("At least 1 dyndns updater must be specified")
else:
for updater_name, updater_options in config['updater']:
dyndnsclient.add_updater(get_updater_class(updater_name)(**updater_options))

from .detector import manager

# find class and instantiate the detector:
if 'detector' not in config:
raise ValueError("No detector specified")
detector_name, detector_opts = config['detector'][-1]
try:
klass = manager.get_detector_class(detector_name)
klass = get_detector_class(detector_name)
except KeyError as exc:
log.warning("Invalid change detector configuration: '%s'",
detector_name, exc_info=exc)
Expand All @@ -202,7 +205,7 @@ def getDynDnsClientForConfig(config, plugins=None):

# add the DNS detector with the same address family option as the user
# configured detector:
klass = manager.get_detector_class("dns")
klass = get_detector_class("dns")
dyndnsclient.set_dns_detector(klass(hostname=config['updater'][0][1]['hostname'], family=thedetector.af()))

return dyndnsclient
3 changes: 2 additions & 1 deletion dyndnsc/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def tearDown(self):
def test_dyndnsc_factory(self):
# the type of these exceptions is not formally required,
# but we want to have some basic form of argument validity checking
# Note: we prefer ValueError for semantically wrong options
self.assertRaises(TypeError, dyndnsc.getDynDnsClientForConfig, None)
self.assertRaises(KeyError, dyndnsc.getDynDnsClientForConfig, {})
self.assertRaises(ValueError, dyndnsc.getDynDnsClientForConfig, {})
self.assertRaises(ValueError, dyndnsc.getDynDnsClientForConfig,
{'updater': ()})

Expand Down
3 changes: 3 additions & 0 deletions dyndnsc/tests/test_conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def test_collect_configuration(self):
self.assertEqual(dict, type(config))
self.assertTrue('testconfig' in config)
self.assertTrue('detector' in config['testconfig'])
self.assertTrue(isinstance(config['testconfig']['detector'], list))
self.assertEqual(1, len(config['testconfig']['detector']))
self.assertTrue('updater' in config['testconfig'])
self.assertTrue(isinstance(config['testconfig']['updater'], list))
self.assertEqual(1, len(config['testconfig']['updater']))
updater = config['testconfig']['updater'][0]
self.assertEqual("fubarUpdater", updater[0])
Expand Down

0 comments on commit 5408d48

Please sign in to comment.