diff options
author | Ivo De Decker <ivo.dedecker@ugent.be> | 2013-05-10 13:33:02 +0200 |
---|---|---|
committer | Ivo De Decker <ivo.dedecker@ugent.be> | 2013-05-10 13:33:02 +0200 |
commit | 31202ad025bcdeb2585d18dc3f4641b5cf9c0ec4 (patch) | |
tree | 32c20d66684ac97b86e55495146e9a676bfae85a /selftest/tests | |
parent | 2865eba17fddda6c49f1209ca92d539111e7ff93 (diff) | |
download | samba-31202ad025bcdeb2585d18dc3f4641b5cf9c0ec4.tar.gz |
Imported Upstream version 4.0.0+dfsg1upstream/4.0.0+dfsg1
Diffstat (limited to 'selftest/tests')
-rw-r--r-- | selftest/tests/__init__.py | 32 | ||||
-rw-r--r-- | selftest/tests/test_run.py | 187 | ||||
-rw-r--r-- | selftest/tests/test_samba.py | 116 | ||||
-rw-r--r-- | selftest/tests/test_socket_wrapper.py | 36 | ||||
-rw-r--r-- | selftest/tests/test_target.py | 129 | ||||
-rw-r--r-- | selftest/tests/test_testlist.py | 148 |
6 files changed, 648 insertions, 0 deletions
diff --git a/selftest/tests/__init__.py b/selftest/tests/__init__.py new file mode 100644 index 0000000000..85d0316b46 --- /dev/null +++ b/selftest/tests/__init__.py @@ -0,0 +1,32 @@ +# __init__.py -- The tests for selftest +# Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 3 +# of the License or (at your option) any later version of +# the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +"""Tests for selftest.""" + +from testtools import TestCase + +import unittest + +def test_suite(): + result = unittest.TestSuite() + names = ['socket_wrapper', 'target', 'testlist', 'run', 'samba'] + module_names = ['selftest.tests.test_' + name for name in names] + loader = unittest.TestLoader() + result.addTests(loader.loadTestsFromNames(module_names)) + return result diff --git a/selftest/tests/test_run.py b/selftest/tests/test_run.py new file mode 100644 index 0000000000..de5f4b121b --- /dev/null +++ b/selftest/tests/test_run.py @@ -0,0 +1,187 @@ +# test_run.py -- Tests for selftest.run +# Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 3 +# of the License or (at your option) any later version of +# the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +"""Tests for selftest.run.""" + +import datetime +import os +import subunit +import tempfile + +from selftest.run import ( + expand_command_list, + expand_environment_strings, + expand_command_run, + exported_envvars_str, + now, + run_testsuite_command, + ) + +from selftest.tests import TestCase + + +class ExpandEnvironmentStringsTests(TestCase): + + def test_no_vars(self): + self.assertEquals("foo bar", expand_environment_strings("foo bar", {})) + + def test_simple(self): + self.assertEquals("foo bar", + expand_environment_strings("foo $BLA", {"BLA": "bar"})) + + def test_unknown(self): + self.assertEquals("foo $BLA", + expand_environment_strings("foo $BLA", {})) + + +class ExpandCommandListTests(TestCase): + + def test_no_list(self): + self.assertIs(None, expand_command_list("test bla")) + + def test_list(self): + self.assertEquals("test --list", expand_command_list("test $LISTOPT")) + + +class ExpandCommandRunTests(TestCase): + + def test_idlist(self): + self.assertEquals(("test foo bar", None), + expand_command_run("test", False, True, subtests=["foo", "bar"])) + + def test_idlist_all(self): + self.assertEquals(("test", None), + expand_command_run("test", False, True)) + + def test_loadlist(self): + (cmd, tmpf) = expand_command_run("test $LOADLIST", True, False, + subtests=["foo", "bar"]) + self.addCleanup(os.remove, tmpf) + f = open(tmpf, 'r') + try: + self.assertEquals(f.read(), "foo\nbar\n") + finally: + f.close() + self.assertEquals("test --load-list=%s" % tmpf, cmd) + + def test_loadlist_all(self): + self.assertEquals(("test ", None), + expand_command_run("test $LOADLIST", True, False)) + + +class ExportedEnvvarsStrTests(TestCase): + + def test_no_vars(self): + self.assertEquals("", exported_envvars_str({}, ["foo", "bar"])) + + def test_vars(self): + self.assertEquals("foo=1\n", + exported_envvars_str({"foo": "1"}, ["foo", "bar"])) + + def test_vars_unknown(self): + self.assertEquals("foo=1\n", + exported_envvars_str({"foo": "1", "bla": "2"}, ["foo", "bar"])) + + + +class NowTests(TestCase): + + def test_basic(self): + self.assertIsInstance(now(), datetime.datetime) + self.assertIsNot(now().tzinfo, None) + + +class MockSubunitOps(object): + + def __init__(self): + self.calls = [] + + def start_testsuite(self, name): + self.calls.append(("start-testsuite", name)) + + def progress(self, count, whence): + self.calls.append(("progress", count, whence)) + + def time(self, t): + self.calls.append(("time", )) + + def end_testsuite(self, name, result, details=None): + self.calls.append(("end-testsuite", name, result, details)) + + +class RunTestsuiteCommandTests(TestCase): + + def test_success_no_env(self): + outf = tempfile.TemporaryFile() + subunit_ops = MockSubunitOps() + exit_code = run_testsuite_command("thetestsuitename", "echo doing something", subunit_ops, outf=outf) + self.assertEquals([ + ("start-testsuite", "thetestsuitename"), + ("progress", None, subunit.PROGRESS_PUSH), + ("time", ), + ("time", ), + ("progress", None, subunit.PROGRESS_POP), + ("end-testsuite", "thetestsuitename", "success", None), + ], subunit_ops.calls) + self.assertEquals(0, exit_code) + outf.seek(0) + self.assertEquals("""\ +doing something +command: echo doing something +expanded command: echo doing something +""", outf.read()) + + def test_failure(self): + outf = tempfile.TemporaryFile() + subunit_ops = MockSubunitOps() + exit_code = run_testsuite_command("thetestsuitename", "exit 3", subunit_ops, outf=outf) + self.assertEquals([ + ("start-testsuite", "thetestsuitename"), + ("progress", None, subunit.PROGRESS_PUSH), + ("time", ), + ("time", ), + ("progress", None, subunit.PROGRESS_POP), + ("end-testsuite", "thetestsuitename", "failure", "Exit code was 3"), + ], subunit_ops.calls) + self.assertEquals(3, exit_code) + outf.seek(0) + self.assertEquals("""\ +command: exit 3 +expanded command: exit 3 +""", outf.read()) + + def test_error(self): + outf = tempfile.TemporaryFile() + subunit_ops = MockSubunitOps() + exit_code = run_testsuite_command("thetestsuitename", + "thisisacommandthatdoesnotexist 2>/dev/null", subunit_ops, outf=outf) + self.assertEquals([ + ("start-testsuite", "thetestsuitename"), + ("progress", None, subunit.PROGRESS_PUSH), + ("time", ), + ("time", ), + ("progress", None, subunit.PROGRESS_POP), + ("end-testsuite", "thetestsuitename", "failure", "Exit code was 127"), + ], subunit_ops.calls) + self.assertEquals(127, exit_code) + outf.seek(0) + self.assertEquals("""\ +command: thisisacommandthatdoesnotexist 2>/dev/null +expanded command: thisisacommandthatdoesnotexist 2>/dev/null +""", outf.read()) diff --git a/selftest/tests/test_samba.py b/selftest/tests/test_samba.py new file mode 100644 index 0000000000..b49463eabb --- /dev/null +++ b/selftest/tests/test_samba.py @@ -0,0 +1,116 @@ +# test_run.py -- Tests for selftest.target.samba +# Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 3 +# of the License or (at your option) any later version of +# the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +"""Tests for selftest.target.samba.""" + +from cStringIO import StringIO + +from selftest.tests import TestCase + +from selftest.target.samba import ( + bindir_path, + get_interface, + mk_realms_stanza, + write_krb5_conf, + ) + + +class BinDirPathTests(TestCase): + + def test_mapping(self): + self.assertEquals("exe4", + bindir_path({"exe": "exe4"}, "/some/path", "exe")) + self.assertEquals("/bin/ls", + bindir_path({"exe": "ls"}, "/bin", "exe")) + + def test_no_mapping(self): + self.assertEqual("exe", bindir_path({}, "/some/path", "exe")) + self.assertEqual("/bin/ls", + bindir_path({}, "/bin", "ls")) + + +class MkRealmsStanzaTests(TestCase): + + def test_basic(self): + self.assertEqual( + mk_realms_stanza("rijk", "dnsnaam", "domein", "ipv4_kdc"), + '''\ + rijk = { + kdc = ipv4_kdc:88 + admin_server = ipv4_kdc:88 + default_domain = dnsnaam + } + dnsnaam = { + kdc = ipv4_kdc:88 + admin_server = ipv4_kdc:88 + default_domain = dnsnaam + } + domein = { + kdc = ipv4_kdc:88 + admin_server = ipv4_kdc:88 + default_domain = dnsnaam + } + +''') + + +class WriteKrb5ConfTests(TestCase): + + def test_simple(self): + f = StringIO() + write_krb5_conf(f, "rijk", "dnsnaam", "domein", "kdc_ipv4") + self.assertEquals('''\ +#Generated krb5.conf for rijk + +[libdefaults] +\tdefault_realm = rijk +\tdns_lookup_realm = false +\tdns_lookup_kdc = false +\tticket_lifetime = 24h +\tforwardable = yes +\tallow_weak_crypto = yes + +[realms] + rijk = { + kdc = kdc_ipv4:88 + admin_server = kdc_ipv4:88 + default_domain = dnsnaam + } + dnsnaam = { + kdc = kdc_ipv4:88 + admin_server = kdc_ipv4:88 + default_domain = dnsnaam + } + domein = { + kdc = kdc_ipv4:88 + admin_server = kdc_ipv4:88 + default_domain = dnsnaam + } + +''', f.getvalue()) + + +class GetInterfaceTests(TestCase): + + def test_get_interface(self): + self.assertEquals(21, get_interface("localdc")) + self.assertEquals(4, get_interface("localshare4")) + + def test_unknown(self): + self.assertRaises(KeyError, get_interface, "unknown") diff --git a/selftest/tests/test_socket_wrapper.py b/selftest/tests/test_socket_wrapper.py new file mode 100644 index 0000000000..81fe5b603c --- /dev/null +++ b/selftest/tests/test_socket_wrapper.py @@ -0,0 +1,36 @@ +# test_socket_wraper.py -- The tests for selftest socket wrapper routines +# Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 3 +# of the License or (at your option) any later version of +# the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +"""Tests for selftest/socket_wrapper.""" + +from selftest.tests import TestCase + +from selftest import socket_wrapper + +import os + +class SocketWrapperTests(TestCase): + + def test_setup_pcap(self): + socket_wrapper.setup_pcap("somefile") + self.assertEquals("somefile", os.environ["SOCKET_WRAPPER_PCAP_FILE"]) + + def test_set_default_iface(self): + socket_wrapper.set_default_iface(4) + self.assertEquals("4", os.environ["SOCKET_WRAPPER_DEFAULT_IFACE"]) diff --git a/selftest/tests/test_target.py b/selftest/tests/test_target.py new file mode 100644 index 0000000000..00b7126a07 --- /dev/null +++ b/selftest/tests/test_target.py @@ -0,0 +1,129 @@ +# test_target.py -- The tests for selftest target code +# Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 3 +# of the License or (at your option) any later version of +# the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +"""Tests for selftest.target.""" + +from selftest.target import ( + Environment, + EnvironmentDown, + EnvironmentManager, + NoneEnvironment, + NoneTarget, + Target, + UnsupportedEnvironment, + ) + +from selftest.tests import TestCase + + +class DummyEnvironment(Environment): + + def __init__(self, name, prefix): + self.name = name + self.prefix = prefix + self.check_ret = True + self.log_ret = "" + + def check(self): + return self.check_ret + + def get_log(self): + return self.log_ret + + +class DummyTarget(Target): + + def setup_env(self, name, prefix): + return DummyEnvironment(name, prefix) + + +class NoneEnvironmentTests(TestCase): + + def setUp(self): + super(NoneEnvironmentTests, self).setUp() + self.env = NoneEnvironment() + + def test_get_vars(self): + self.assertEquals({}, self.env.get_vars()) + + def test_check(self): + self.assertEquals(True, self.env.check()) + + def test_get_log(self): + self.assertEquals("", self.env.get_log()) + + +class NoneTargetTests(TestCase): + + def setUp(self): + super(NoneTargetTests, self).setUp() + self.target = NoneTarget() + + def test_setup_env(self): + self.assertRaises(UnsupportedEnvironment, self.target.setup_env, + "something", "prefx") + + +class EnvironmentManagerTests(TestCase): + + def setUp(self): + super(EnvironmentManagerTests, self).setUp() + self.mgr = EnvironmentManager(DummyTarget()) + + def test_none(self): + self.assertIs( + NoneEnvironment, type(self.mgr.setup_env("none", "prefix"))) + + def test_setup(self): + env = self.mgr.setup_env("something", "prefix") + self.assertEquals(env.name, "something") + self.assertEquals(env.prefix, "prefix") + + def test_setup_reuses(self): + env1 = self.mgr.setup_env("something", "prefix") + env2 = self.mgr.setup_env("something", "prefix") + self.assertIs(env1, env2) + + def test_setup_down(self): + env1 = self.mgr.setup_env("something", "prefix") + env1.check_ret = False + self.assertRaises(EnvironmentDown, self.mgr.setup_env, "something", "") + + def test_check(self): + env = self.mgr.setup_env("something", "prefix") + self.assertTrue(env.check()) + self.assertTrue(self.mgr.check_env("something")) + env.check_ret = False + self.assertFalse(env.check()) + self.assertFalse(self.mgr.check_env("something")) + + def test_get_log(self): + env = self.mgr.setup_env("something", "prefix") + self.assertEquals("", env.get_log()) + self.assertEquals("", self.mgr.getlog_env("something")) + env.log_ret = 'bla' + self.assertEquals('bla', env.get_log()) + self.assertEquals('bla', self.mgr.getlog_env("something")) + + def test_get_running_env(self): + env = self.mgr.setup_env("something", "prefix") + self.assertIs(env, self.mgr.get_running_env("something")) + + def test_get_running_env_nonexistent(self): + self.assertIs(None, self.mgr.get_running_env("something")) diff --git a/selftest/tests/test_testlist.py b/selftest/tests/test_testlist.py new file mode 100644 index 0000000000..4474d0aa07 --- /dev/null +++ b/selftest/tests/test_testlist.py @@ -0,0 +1,148 @@ +# test_testlist.py -- The tests for selftest testlist code +# Copyright (C) 2012 Jelmer Vernooij <jelmer@samba.org> +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 3 +# of the License or (at your option) any later version of +# the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. + +"""Tests for selftest.testlist.""" + +import os +import tempfile + +from selftest.tests import TestCase + +from selftest.testlist import ( + RestrictedTestManager, + find_in_list, + open_file_or_pipe, + read_test_regexes, + read_testlist, + read_testlist_file, + ) + +from cStringIO import StringIO + + +class FindInListTests(TestCase): + + def test_empty(self): + self.assertIs(None, find_in_list([], "foo.test")) + + def test_no_reason(self): + self.assertEquals("because", + find_in_list([("foo.*bar", "because")], "foo.bla.bar")) + + +class ReadTestRegexesTests(TestCase): + + def test_comment(self): + f = StringIO("# I am a comment\n # I am also a comment\n") + self.assertEquals([], list(read_test_regexes(f))) + + def test_no_reason(self): + f = StringIO(" foo\n") + self.assertEquals([("foo", None)], list(read_test_regexes(f))) + + def test_reason(self): + f = StringIO(" foo # because\nbar\n") + self.assertEquals([("foo", "because"), ("bar", None)], + list(read_test_regexes(f))) + + +class ReadTestlistTests(TestCase): + + def test_read_list(self): + inf = StringIO("-- TEST --\nfoo\nbar\nbla\n") + outf = StringIO() + self.assertEquals([('foo', 'bar', 'bla', False, False)], + list(read_testlist(inf, outf))) + self.assertEquals("", outf.getvalue()) + + def test_read_list_passes_through(self): + inf = StringIO("MORENOISE\n-- TEST --\nfoo\nbar\nbla\nNOISE\n") + outf = StringIO() + self.assertEquals([('foo', 'bar', 'bla', False, False)], + list(read_testlist(inf, outf))) + self.assertEquals("MORENOISE\nNOISE\n", outf.getvalue()) + + + +class RestrictedTestManagerTests(TestCase): + + def test_unused(self): + mgr = RestrictedTestManager(["foo.bar"]) + self.assertEquals(["foo.bar"], list(mgr.iter_unused())) + + def test_run_testsuite(self): + mgr = RestrictedTestManager(["foo.bar"]) + self.assertEquals(None, mgr.should_run_testsuite("foo.bar")) + + def test_run_subtest(self): + mgr = RestrictedTestManager(["foo.bar.bla"]) + self.assertEquals(["bla"], mgr.should_run_testsuite("foo.bar")) + + def test_run_subtest_after_testsuite(self): + mgr = RestrictedTestManager(["foo.bar", "foo.bar.bla"]) + self.assertEquals(None, mgr.should_run_testsuite("foo.bar")) + + def test_run_multiple_subtests(self): + mgr = RestrictedTestManager(["foo.bar.blie", "foo.bar.bla"]) + self.assertEquals(["blie", "bla"], mgr.should_run_testsuite("foo.bar")) + + def test_run_nomatch(self): + mgr = RestrictedTestManager(["foo.bar"]) + self.assertEquals([], mgr.should_run_testsuite("foo.blie.bla")) + + +class OpenFileOrPipeTests(TestCase): + + def test_regular_file(self): + (fd, p) = tempfile.mkstemp() + self.addCleanup(os.remove, p) + f = os.fdopen(fd, 'w') + try: + f.write('data\nbla\n') + finally: + f.close() + f = open_file_or_pipe(p, 'r') + try: + self.assertEquals("data\nbla\n", f.read()) + finally: + f.close() + + def test_pipe(self): + f = open_file_or_pipe('echo foo|', 'r') + try: + self.assertEquals("foo\n", f.read()) + finally: + f.close() + + +class ReadTestListFileTests(TestCase): + + def test_regular_file(self): + (fd, p) = tempfile.mkstemp() + self.addCleanup(os.remove, p) + f = os.fdopen(fd, 'w') + try: + f.write('noise\n-- TEST --\ndata\nenv\ncmd\n') + finally: + f.close() + outf = StringIO() + self.assertEquals( + [('data', 'env', 'cmd', False, False)], + list(read_testlist_file(p, outf))) + self.assertEquals("noise\n", outf.getvalue()) |