BIND 10 trac615, updated. d686e3ada2141094413e756d601d2e727fb6f760 [trac615] Pass options to the manager

BIND 10 source code commits bind10-changes at lists.isc.org
Fri Mar 4 10:51:53 UTC 2011


The branch, trac615 has been updated
       via  d686e3ada2141094413e756d601d2e727fb6f760 (commit)
       via  d0dcd91d39a438d5a18e0726251e1a93212143ca (commit)
       via  b32ea06f28de94a8ec779385d5e86374e81c023d (commit)
       via  1faf02bda80053ddb2f815466d0372a2eeb6c08c (commit)
       via  9e782794969ab034eb92dca6a8d5ee8f518ccc95 (commit)
      from  1fd5547c170550880268ba0eb83a374231be348b (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit d686e3ada2141094413e756d601d2e727fb6f760
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Fri Mar 4 11:09:12 2011 +0100

    [trac615] Pass options to the manager

commit d0dcd91d39a438d5a18e0726251e1a93212143ca
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Fri Mar 4 10:59:14 2011 +0100

    [trac615] Parser of args

commit b32ea06f28de94a8ec779385d5e86374e81c023d
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Thu Mar 3 19:19:26 2011 +0100

    [trac615] Tests for parsing args

commit 1faf02bda80053ddb2f815466d0372a2eeb6c08c
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Thu Mar 3 18:57:00 2011 +0100

    [trac615] Don't hurt absolute names

commit 9e782794969ab034eb92dca6a8d5ee8f518ccc95
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date:   Thu Mar 3 18:47:56 2011 +0100

    [trac615] Test absolute filename for cfgmgr

-----------------------------------------------------------------------

Summary of changes:
 src/bin/cfgmgr/b10-cfgmgr.py.in                |   17 ++++++-
 src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in     |   68 +++++++++++++++++++++++-
 src/lib/python/isc/config/cfgmgr.py            |   33 ++++++++----
 src/lib/python/isc/config/tests/cfgmgr_test.py |   10 ++++
 4 files changed, 115 insertions(+), 13 deletions(-)

-----------------------------------------------------------------------
diff --git a/src/bin/cfgmgr/b10-cfgmgr.py.in b/src/bin/cfgmgr/b10-cfgmgr.py.in
index 659426d..98f515b 100755
--- a/src/bin/cfgmgr/b10-cfgmgr.py.in
+++ b/src/bin/cfgmgr/b10-cfgmgr.py.in
@@ -22,6 +22,7 @@ from isc.cc import SessionError
 import isc.util.process
 import signal
 import os
+from optparse import OptionParser
 
 isc.util.process.rename()
 
@@ -36,15 +37,29 @@ else:
 
 cm = None
 
+def parse_options(args=sys.argv[1:], Parser=OptionParser):
+    parser = Parser()
+    parser.add_option("-p", "--data-path", dest="data_path",
+                      help="Directory to search for configuration files " +
+                      "(default="+DATA_PATH+")", default=DATA_PATH)
+    parser.add_option("-f", "--database-filename", dest="file_name",
+                      help="Configuration database filename " +
+                      "(default=b10-config.db)", default="b10-config.db")
+    (options, args) = parser.parse_args(args)
+    if args:
+        parser.error("No non-option arguments allowed")
+    return options
+
 def signal_handler(signal, frame):
     global cm
     if cm:
         cm.running = False
 
 def main():
+    options = parse_options()
     global cm
     try:
-        cm = ConfigManager(DATA_PATH)
+        cm = ConfigManager(options.data_path, options.file_name)
         signal.signal(signal.SIGINT, signal_handler)
         signal.signal(signal.SIGTERM, signal_handler)
         cm.read_config()
diff --git a/src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in b/src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in
index 8847b18..01b8ae5 100644
--- a/src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in
+++ b/src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in
@@ -20,9 +20,10 @@
 import unittest
 import os
 import sys
+from optparse import OptionParser
 
 class MyConfigManager:
-    def __init__(self, path):
+    def __init__(self, path, filename):
         self._path = path
         self.read_config_called = False
         self.notify_boss_called = False
@@ -88,6 +89,71 @@ class TestConfigManagerStartup(unittest.TestCase):
 
         sys.modules.pop("b10-cfgmgr")
 
+class OptsError(Exception):
+    """To know when OptionParser would exit"""
+    pass
+
+class TestOptParser(OptionParser):
+    """
+    We define our own option parser to push into the parsing routine.
+    This one does not exit the whole application on error, it just raises
+    exception. It doesn't change anything else. The application uses the
+    stock one.
+    """
+    def error(self, message):
+        raise OptsError(message)
+
+class TestParseArgs(unittest.TestCase):
+    """
+    Test for the parsing of command line arguments. We provide a different
+    array to parse instead.
+    """
+
+    def test_defaults(self):
+        """
+        Test the default values when no options are provided.
+        """
+        # Pass it empty array, not our arguments
+        b = __import__("b10-cfgmgr")
+        parsed = b.parse_options([], TestOptParser)
+        self.assertEqual(b.DATA_PATH, parsed.data_path)
+        self.assertEqual("b10-config.db", parsed.file_name)
+
+    def test_wrong_args(self):
+        """
+        Test it fails when we pass invalid option.
+        """
+        b = __import__("b10-cfgmgr")
+        self.assertRaises(OptsError, b.parse_options, (['--wrong-option']),
+                          TestOptParser)
+
+    def test_not_arg(self):
+        """
+        Test it fails when there's an argument that's not option
+        (eg. without -- at the beginning).
+        """
+        b = __import__("b10-cfgmgr")
+        self.assertRaises(OptsError, b.parse_options, (['not-option']),
+                          TestOptParser)
+
+    def test_datapath(self):
+        """
+        Test overwriting the data path.
+        """
+        b = __import__("b10-cfgmgr")
+        parsed = b.parse_options(['--data-path=/path'], TestOptParser)
+        self.assertEqual('/path', parsed.data_path)
+        self.assertEqual("b10-config.db", parsed.file_name)
+
+    def test_db_filename(self):
+        """
+        Test setting the configuration database file.
+        """
+        b = __import__("b10-cfgmgr")
+        parsed = b.parse_options(['--database-filename=filename'],
+                                 TestOptParser)
+        self.assertEqual(b.DATA_PATH, parsed.data_path)
+        self.assertEqual("filename", parsed.file_name)
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/src/lib/python/isc/config/cfgmgr.py b/src/lib/python/isc/config/cfgmgr.py
index e347f6a..1c36fe1 100644
--- a/src/lib/python/isc/config/cfgmgr.py
+++ b/src/lib/python/isc/config/cfgmgr.py
@@ -48,21 +48,32 @@ class ConfigManagerData:
         """Initialize the data for the configuration manager, and
            set the version and path for the data store. Initializing
            this does not yet read the database, a call to
-           read_from_file is needed for that."""
+           read_from_file is needed for that.
+
+           In case the file_name is absolute, data_path is ignored
+           and the directory where the file_name lives is used instead.
+           """
         self.data = {}
         self.data['version'] = config_data.BIND10_CONFIG_DATA_VERSION
-        self.data_path = data_path
-        self.db_filename = data_path + os.sep + file_name
+        if os.path.isabs(file_name):
+            self.db_filename = file_name
+            self.data_path = os.path.dirname(file_name)
+        else:
+            self.db_filename = data_path + os.sep + file_name
+            self.data_path = data_path
 
     def read_from_file(data_path, file_name = "b10-config.db"):
-        """Read the current configuration found in the file at
-           data_path. If the file does not exist, a
-           ConfigManagerDataEmpty exception is raised. If there is a
-           parse error, or if the data in the file has the wrong
-           version, a ConfigManagerDataReadError is raised. In the first
-           case, it is probably safe to log and ignore. In the case of
-           the second exception, the best way is probably to report the
-           error and stop loading the system."""
+        """Read the current configuration found in the file file_name.
+           If file_name is absolute, data_path is ignored. Otherwise
+           we look for the file_name in data_path directory.
+
+           If the file does not exist, a ConfigManagerDataEmpty exception is
+           raised. If there is a parse error, or if the data in the file has
+           the wrong version, a ConfigManagerDataReadError is raised. In the
+           first case, it is probably safe to log and ignore. In the case of
+           the second exception, the best way is probably to report the error
+           and stop loading the system.
+           """
         config = ConfigManagerData(data_path, file_name)
         file = None
         try:
diff --git a/src/lib/python/isc/config/tests/cfgmgr_test.py b/src/lib/python/isc/config/tests/cfgmgr_test.py
index c992d0d..f9ddfd5 100644
--- a/src/lib/python/isc/config/tests/cfgmgr_test.py
+++ b/src/lib/python/isc/config/tests/cfgmgr_test.py
@@ -30,6 +30,16 @@ class TestConfigManagerData(unittest.TestCase):
         self.config_manager_data = ConfigManagerData(self.writable_data_path)
         self.assert_(self.config_manager_data)
 
+    def test_abs_file(self):
+        """
+        Test what happens if we give the config manager an absolute path.
+        It shouldn't append the data path to it.
+        """
+        abs_path = self.data_path + os.sep + "b10-config-imaginary.db"
+        data = ConfigManagerData(os.getcwd(), abs_path)
+        self.assertEqual(abs_path, data.db_filename)
+        self.assertEqual(self.data_path, data.data_path)
+
     def test_init(self):
         self.assertEqual(self.config_manager_data.data['version'],
                          config_data.BIND10_CONFIG_DATA_VERSION)




More information about the bind10-changes mailing list