From 01d2ac16e2edab2b81294cd8974938226dd76953 Mon Sep 17 00:00:00 2001 From: Jelte Jansen Date: Wed, 20 Feb 2013 11:35:34 +0100 Subject: [PATCH] [2713] Add test script for b10-cmdctl-usermgr Doesn't test anything useful yet, mostly unit testing framework --- configure.ac | 1 + src/bin/usermgr/Makefile.am | 2 + src/bin/usermgr/tests/Makefile.am | 30 +++++++++ .../usermgr/tests/b10-cmdctl-usermgr_test.py | 62 +++++++++++++++++++ 4 files changed, 95 insertions(+) create mode 100644 src/bin/usermgr/tests/Makefile.am create mode 100644 src/bin/usermgr/tests/b10-cmdctl-usermgr_test.py diff --git a/configure.ac b/configure.ac index 5195c2422a..d6d3278139 100644 --- a/configure.ac +++ b/configure.ac @@ -1202,6 +1202,7 @@ AC_CONFIG_FILES([Makefile src/bin/stats/tests/Makefile src/bin/stats/tests/testdata/Makefile src/bin/usermgr/Makefile + src/bin/usermgr/tests/Makefile src/bin/tests/Makefile src/lib/Makefile src/lib/asiolink/Makefile diff --git a/src/bin/usermgr/Makefile.am b/src/bin/usermgr/Makefile.am index ce7977f94e..08c9f9ab2c 100644 --- a/src/bin/usermgr/Makefile.am +++ b/src/bin/usermgr/Makefile.am @@ -1,3 +1,5 @@ +SUBDIRS = tests + sbin_SCRIPTS = b10-cmdctl-usermgr b10_cmdctl_usermgrdir = $(pkgdatadir) diff --git a/src/bin/usermgr/tests/Makefile.am b/src/bin/usermgr/tests/Makefile.am new file mode 100644 index 0000000000..e177ed8533 --- /dev/null +++ b/src/bin/usermgr/tests/Makefile.am @@ -0,0 +1,30 @@ +PYCOVERAGE_RUN=@PYCOVERAGE_RUN@ +PYTESTS = b10-cmdctl-usermgr_test.py +EXTRA_DIST = $(PYTESTS) + +# If necessary (rare cases), explicitly specify paths to dynamic libraries +# required by loadable python modules. +#LIBRARY_PATH_PLACEHOLDER = +#if SET_ENV_LIBRARY_PATH +#LIBRARY_PATH_PLACEHOLDER += $(ENV_LIBRARY_PATH)=$(abs_top_builddir)/src/lib/cryptolink/.libs:$(abs_top_builddir)/src/lib/dns/.libs:$(abs_top_builddir)/src/lib/dns/python/.libs:$(abs_top_builddir)/src/lib/cc/.libs:$(abs_top_builddir)/src/lib/config/.libs:$(abs_top_builddir)/src/lib/log/.libs:$(abs_top_builddir)/src/lib/util/.libs:$(abs_top_builddir)/src/lib/exceptions/.libs:$(abs_top_builddir)/src/lib/util/io/.libs:$(abs_top_builddir)/src/lib/datasrc/.libs:$$$(ENV_LIBRARY_PATH) +#endif + +#CLEANFILES = test-keyfile.pem test-certfile.pem +CLEANFILES = *.csv + +# test using command-line arguments, so use check-local target instead of TESTS +check-local: +if ENABLE_PYTHON_COVERAGE + touch $(abs_top_srcdir)/.coverage + rm -f .coverage + ${LN_S} $(abs_top_srcdir)/.coverage .coverage +endif + for pytest in $(PYTESTS) ; do \ + echo Running test: $$pytest ; \ + $(LIBRARY_PATH_PLACEHOLDER) \ + PYTHONPATH=$(COMMON_PYTHON_PATH):$(abs_top_builddir)/src/bin/cmdctl \ + CMDCTL_BUILD_PATH=$(abs_top_builddir)/src/bin/cmdctl \ + CMDCTL_SRC_PATH=$(abs_top_srcdir)/src/bin/cmdctl \ + B10_LOCKFILE_DIR_FROM_BUILD=$(abs_top_builddir) \ + $(PYCOVERAGE_RUN) $(abs_srcdir)/$$pytest || exit ; \ + done diff --git a/src/bin/usermgr/tests/b10-cmdctl-usermgr_test.py b/src/bin/usermgr/tests/b10-cmdctl-usermgr_test.py new file mode 100644 index 0000000000..f303fa1e88 --- /dev/null +++ b/src/bin/usermgr/tests/b10-cmdctl-usermgr_test.py @@ -0,0 +1,62 @@ +# Copyright (C) 2013 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. + +import unittest +import subprocess + +def run(command): + """ + Small helper function that returns a tuple of (rcode, stdout, stderr) after + running the given command (an array of command and arguments, as passed on + to subprocess). + """ + subp = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (stdout, stderr) = subp.communicate() + return (subp.returncode, stdout, stderr) + +class TestUserMgr(unittest.TestCase): + TOOL = '../b10-cmdctl-usermgr' + + def run_check(self, expected_returncode, expected_stdout, expected_stderr, command): + """ + Runs the given command, and checks return code, and outputs (if provided). + Arguments: + expected_returncode, return code of the command + expected_stdout, (multiline) string that is checked agains stdout. + May be None, in which case the check is skipped. + expected_stderr, (multiline) string that is checked agains stderr. + May be None, in which case the check is skipped. + """ + (returncode, stdout, stderr) = run(command) + self.assertEqual(expected_returncode, returncode, " ".join(command)) + if expected_stdout is not None: + self.assertEqual(expected_stdout, stdout.decode()) + if expected_stderr is not None: + self.assertEqual(expected_stderr, stderr.decode()) + + def test_bad_options(self): + self.run_check(2, + 'option -a not recognized\n' + 'Usage: usermgr [options]\n' + ' -h, --help Show this help message and exit\n' + ' -f, --file Specify the file to append user name and password\n' + ' -v, --version Get version number\n' + ' \n', + '', [self.TOOL, '-a']) + + +if __name__== '__main__': + unittest.main() +