From b9cd36cbf75fda432ce82cf3058c89ef25dff2f6 Mon Sep 17 00:00:00 2001 From: Jelte Jansen Date: Wed, 16 Jun 2010 16:10:49 +0000 Subject: [PATCH] unit tests for b10-cfgmgr moved a little bit of code into a main() function (instead of calling it directly), for easier testing module magic happening in the test. git-svn-id: svn://bind10.isc.org/svn/bind10/branches/trac238@2127 e5f2f494-b856-4b98-b285-d166d9295462 --- configure.ac | 2 + src/bin/cfgmgr/Makefile.am | 2 + src/bin/cfgmgr/b10-cfgmgr.py.in | 6 +- src/bin/cfgmgr/tests/Makefile.am | 13 +++ src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in | 95 ++++++++++++++++++++++ 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 src/bin/cfgmgr/tests/Makefile.am create mode 100644 src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in diff --git a/configure.ac b/configure.ac index 519d483ff3..e6c7f38c52 100644 --- a/configure.ac +++ b/configure.ac @@ -393,6 +393,7 @@ AC_CONFIG_FILES([Makefile src/bin/bindctl/Makefile src/bin/bindctl/tests/Makefile src/bin/cfgmgr/Makefile + src/bin/cfgmgr/tests/Makefile src/bin/host/Makefile src/bin/loadzone/Makefile src/bin/msgq/Makefile @@ -423,6 +424,7 @@ AC_CONFIG_FILES([Makefile src/lib/xfr/Makefile ]) AC_OUTPUT([src/bin/cfgmgr/b10-cfgmgr.py + src/bin/cfgmgr/tests/b10-cfgmgr_test.py src/bin/cmdctl/cmdctl.py src/bin/cmdctl/run_b10-cmdctl.sh src/bin/cmdctl/tests/cmdctl_test diff --git a/src/bin/cfgmgr/Makefile.am b/src/bin/cfgmgr/Makefile.am index 695d371ff8..b3dffd07ae 100644 --- a/src/bin/cfgmgr/Makefile.am +++ b/src/bin/cfgmgr/Makefile.am @@ -1,3 +1,5 @@ +SUBDIRS = tests + pkglibexecdir = $(libexecdir)/@PACKAGE@ pkglibexec_SCRIPTS = b10-cfgmgr diff --git a/src/bin/cfgmgr/b10-cfgmgr.py.in b/src/bin/cfgmgr/b10-cfgmgr.py.in index 563bbcdf7e..1c524c3d55 100644 --- a/src/bin/cfgmgr/b10-cfgmgr.py.in +++ b/src/bin/cfgmgr/b10-cfgmgr.py.in @@ -38,7 +38,8 @@ def signal_handler(signal, frame): if cm: cm.running = False -if __name__ == "__main__": +def main(): + global cm try: cm = ConfigManager(DATA_PATH) signal.signal(signal.SIGINT, signal_handler) @@ -53,3 +54,6 @@ if __name__ == "__main__": print("[b10-cfgmgr] Interrupted, exiting") if cm: cm.write_config() + +if __name__ == "__main__": + main() diff --git a/src/bin/cfgmgr/tests/Makefile.am b/src/bin/cfgmgr/tests/Makefile.am new file mode 100644 index 0000000000..fc71023ef0 --- /dev/null +++ b/src/bin/cfgmgr/tests/Makefile.am @@ -0,0 +1,13 @@ +PYTESTS = b10-cfgmgr_test.py + +EXTRA_DIST = $(PYTESTS) + +# later will have configure option to choose this, like: coverage run --branch +PYCOVERAGE = $(PYTHON) +# test using command-line arguments, so use check-local target instead of TESTS +check-local: + for pytest in $(PYTESTS) ; do \ + echo Running test: $$pytest ; \ + env PYTHONPATH=$(abs_top_srcdir)/src/lib/python:$(abs_top_builddir)/src/lib/python:$(abs_top_builddir)/src/bin/cfgmgr \ + $(PYCOVERAGE) $(abs_srcdir)/$$pytest ; \ + done diff --git a/src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in b/src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in new file mode 100644 index 0000000000..81bceee7df --- /dev/null +++ b/src/bin/cfgmgr/tests/b10-cfgmgr_test.py.in @@ -0,0 +1,95 @@ +# Copyright (C) 2010 Internet Systems Consortium. +# +# Permission to use, copy, modify, and distribute this software for any +# purpose with or without fee is hereby granted, provided that the above +# copyright notice and this permission notice appear in all copies. +# +# THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SYSTEMS CONSORTIUM +# DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL +# INTERNET SYSTEMS CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, +# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING +# FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, +# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION +# WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +# $Id: cfgmgr_test.py 2126 2010-06-16 14:40:22Z jelte $ + +# +# Tests for the configuration manager run script +# + +import unittest +import os +import sys + +class MyConfigManager: + def __init__(self, path): + self._path = path + self.read_config_called = False + self.notify_boss_called = False + self.run_called = False + self.write_config_called = False + self.running = True + + def read_config(self): + self.read_config_called = True + + def notify_boss(self): + self.notify_boss_called = True + + def run(self): + self.run_called = True + + def write_config(self): + self.write_config_called = True + +class TestConfigManagerStartup(unittest.TestCase): + def test_cfgmgr(self): + # some creative module use; + # b10-cfgmgr has a hypen, so we use __import__ + # this also gives us the chance to override the imported + # moculde ConfigManager in it. + b = __import__("b10-cfgmgr") + b.ConfigManager = MyConfigManager + + b.main() + + self.assertTrue(b.cm.read_config_called) + self.assertTrue(b.cm.notify_boss_called) + self.assertTrue(b.cm.run_called) + self.assertTrue(b.cm.write_config_called) + + self.assertTrue(b.cm.running) + b.signal_handler(None, None) + self.assertFalse(b.cm.running) + + # TODO: take value from the 'global config module' + # (and rename the .in aay from this file again) + data_path = "@localstatedir@/@PACKAGE@".replace("${prefix}", "@prefix@") + self.assertEqual(data_path, b.DATA_PATH) + + # remove the 'module' again, or later tests may fail + # (if it is already present it won't be loaded again) + sys.modules.pop("b10-cfgmgr") + + def test_cfgmgr_from_source(self): + tmp_env_var = "/just/some/dir" + env_var = None + if "B10_FROM_SOURCE" in os.environ: + env_var = os.environ["B10_FROM_SOURCE"] + + os.environ["B10_FROM_SOURCE"] = tmp_env_var + b = __import__("b10-cfgmgr", globals(), locals()) + b.ConfigManager = MyConfigManager + self.assertEqual(tmp_env_var, b.DATA_PATH) + + if env_var != None: + os.environ["B10_FROM_SOURCE"] = env_var + + sys.modules.pop("b10-cfgmgr") + + +if __name__ == '__main__': + unittest.main() +