Permalink
Browse files

make sure the config file is applied - bug #13

  • Loading branch information...
1 parent 5555ccf commit 072b65bd47715b1b0eec3a844b255fb43cd2a4ef @tarekziade tarekziade committed Dec 14, 2012
Showing with 67 additions and 14 deletions.
  1. +14 −14 vaurien/run.py
  2. +53 −0 vaurien/tests/test_config.py
View
@@ -81,6 +81,10 @@ def build_args(parser, items, prefix):
def main():
+ # get the values from the default config
+ defaults = DEFAULT_SETTINGS.items()
+ defaults.sort()
+
parser = argparse.ArgumentParser(description='Runs a Chaos TCP proxy.')
# other arguments
@@ -93,14 +97,9 @@ def main():
help='Host of the http server, if any')
parser.add_argument('--http-port', default=8080, type=int,
help='Port of the http server, if any')
-
parser.add_argument('--protocol', default='tcp', choices=get_protocols(),
help='Protocol used')
- # get the values from the default config
- defaults = DEFAULT_SETTINGS.items()
- defaults.sort()
-
for key, default in defaults:
if key.startswith('vaurien'):
key = key[len('vaurien.'):]
@@ -136,17 +135,10 @@ def main():
# configure the logger
configure_logger(logger, args.loglevel, args.logoutput)
+ # load the defaults
settings = DEFAULT_SETTINGS.copy()
- if args.config is not None:
- # read the config if provided
- try:
- load_into_settings(args.config, settings)
- except ValueError, e:
- print(e)
- sys.exit(1)
-
- # overwrite with the commandline arguments
+ # overwrite with the command line arguments
for key in settings.keys():
prefix = ''
if key.startswith('vaurien'):
@@ -161,6 +153,14 @@ def main():
if value is not None:
settings[prefix + key] = value
+ # overwrite with the config file if any
+ if args.config is not None:
+ try:
+ load_into_settings(args.config, settings)
+ except ValueError, e:
+ print(e)
+ sys.exit(1)
+
# pass the args in the settings
settings['args'] = args
statsd = get_statsd_from_settings(settings.getsection('statsd'))
@@ -0,0 +1,53 @@
+from tempfile import mkstemp
+from copy import copy
+from unittest import TestCase
+import sys
+import os
+
+from vaurien import proxy, run
+from vaurien.run import main
+
+
+_CONF = """\
+[vaurien]
+backend = 0.0.0.0:33
+
+"""
+
+
+class FakeProxy(object):
+
+ args = kwargs = None
+
+ def __init__(self, *args, **kwargs):
+ FakeProxy.args = args
+ FakeProxy.kwargs = kwargs
+
+ def serve_forever(self):
+ pass
+
+
+class TestConfig(TestCase):
+ def setUp(self):
+ self.old = proxy.RandomProxy
+ run.RandomProxy = proxy.RandomProxy = FakeProxy
+ fd, self.config = mkstemp()
+ os.close(fd)
+ with open(self.config, 'w') as f:
+ f.write(_CONF)
+
+ def tearDown(self):
+ run.RandomProxy = proxy.RandomProxy = self.old
+ os.remove(self.config)
+
+ def test_config(self):
+
+ # make sure the config is taken into account
+ old = copy(sys.argv)
+ try:
+ sys.argv = ['vaurien', '--config', self.config]
+ main()
+ finally:
+ sys.argv[:] = old
+
+ self.assertEqual(FakeProxy.kwargs['backend'], '0.0.0.0:33')

0 comments on commit 072b65b

Please sign in to comment.