From: Tim Beale Date: Thu, 10 May 2018 04:22:06 +0000 (+1200) Subject: tests: Add tests for samba-tool passwordsettings commands X-Git-Tag: ldb-1.4.0~354 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=569937b8008ea8b64b4e3ead61ebc97c6c41f6b6;p=thirdparty%2Fsamba.git tests: Add tests for samba-tool passwordsettings commands I've added a test case for 'samba-tool domain passwordsettings set/show' to prove I haven't broken it. It's behaviour shouldn't have changed, but there was no test for it previously. We'll extend these tests in the very near future, when we add samba-tool support for managing PSOs. The base samba_tool test's runsubcmd() only handled commands with exactly one sub-command, i.e. it would handle the command 'samba-tool domain passwordsettings' OK, but not 'samba-tool domain passwordsettings set' (The command still seemed to run OK, but you wouldn't get the output/err back correctly). A new runsublevelcmd() function now handles a varying number of sub-commands. Reviewed-by: Andrew Bartlett Reviewed-by: Garming Sam Signed-off-by: Tim Beale Autobuild-User(master): Garming Sam Autobuild-Date(master): Fri May 11 09:06:10 CEST 2018 on sn-devel-144 --- diff --git a/python/samba/tests/samba_tool/base.py b/python/samba/tests/samba_tool/base.py index 89a09225e64..06e19c19087 100644 --- a/python/samba/tests/samba_tool/base.py +++ b/python/samba/tests/samba_tool/base.py @@ -88,6 +88,23 @@ class SambaToolCmdTest(samba.tests.BlackboxTestCase): result = cmd._run("samba-tool %s %s" % (name, sub), *args) return (result, cmd.outf.getvalue(), cmd.errf.getvalue()) + def runsublevelcmd(self, name, sublevels, *args): + """run a command with any number of sub command levels""" + # Same as runsubcmd, except this handles a varying number of sub-command + # levels, e.g. 'samba-tool domain passwordsettings pso set', whereas + # runsubcmd() only handles exactly one level of sub-commands. + # First, traverse the levels of sub-commands to get the actual cmd + # object we'll run, and construct the cmd string along the way + cmd = cmd_sambatool.subcommands[name] + cmd_str = "samba-tool %s" % name + for sub in sublevels: + cmd = cmd.subcommands[sub] + cmd_str += " %s" % sub + cmd.outf = StringIO() + cmd.errf = StringIO() + result = cmd._run(cmd_str, *args) + return (result, cmd.outf.getvalue(), cmd.errf.getvalue()) + def assertCmdSuccess(self, exit, out, err, msg=""): self.assertIsNone(exit, msg="exit[%s] stdout[%s] stderr[%s]: %s" % ( exit, out, err, msg)) diff --git a/python/samba/tests/samba_tool/passwordsettings.py b/python/samba/tests/samba_tool/passwordsettings.py new file mode 100644 index 00000000000..5766352772d --- /dev/null +++ b/python/samba/tests/samba_tool/passwordsettings.py @@ -0,0 +1,70 @@ +# Test 'samba-tool domain passwordsettings' sub-commands +# +# Copyright (C) Andrew Bartlett 2018 +# +# 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; either version 3 of the License, or +# (at your option) any later version. +# +# 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, see . +# + +import os +import ldb +from samba.tests.samba_tool.base import SambaToolCmdTest + +class PwdSettingsCmdTestCase(SambaToolCmdTest): + """Tests for 'samba-tool domain passwordsettings' subcommands""" + + def setUp(self): + super(PwdSettingsCmdTestCase, self).setUp() + self.server = "ldap://%s" % os.environ["DC_SERVER"] + self.user_auth = "-U%s%%%s" % (os.environ["DC_USERNAME"], + os.environ["DC_PASSWORD"]) + self.ldb = self.getSamDB("-H", self.server, self.user_auth) + + def tearDown(self): + super(PwdSettingsCmdTestCase, self).tearDown() + + def test_domain_passwordsettings(self): + """Checks the 'set/show' commands for the domain settings (non-PSO)""" + + # check the 'show' cmd for the domain settings + (result, out, err) = self.runsublevelcmd("domain", ("passwordsettings", + "show"), "-H", self.server, + self.user_auth) + self.assertCmdSuccess(result, out, err) + self.assertEquals(err,"","Shouldn't be any error messages") + + # check an arbitrary setting is displayed correctly + min_pwd_len = self.ldb.get_minPwdLength() + self.assertIn("Minimum password length: %s" % min_pwd_len, out) + + # check we can change the domain setting + self.addCleanup(self.ldb.set_minPwdLength, min_pwd_len) + new_len = int(min_pwd_len) + 3 + (result, out, err) = self.runsublevelcmd("domain", ("passwordsettings", + "set"), + "--min-pwd-length=%u" % new_len, + "-H", self.server, + self.user_auth) + self.assertCmdSuccess(result, out, err) + self.assertEquals(err,"","Shouldn't be any error messages") + self.assertIn("successful", out) + self.assertEquals(str(new_len), self.ldb.get_minPwdLength()) + + # check the updated value is now displayed + (result, out, err) = self.runsublevelcmd("domain", ("passwordsettings", + "show"), "-H", self.server, + self.user_auth) + self.assertCmdSuccess(result, out, err) + self.assertEquals(err,"","Shouldn't be any error messages") + self.assertIn("Minimum password length: %u" % new_len, out) + diff --git a/source4/selftest/tests.py b/source4/selftest/tests.py index 405f2b5b7ea..88af6078894 100755 --- a/source4/selftest/tests.py +++ b/source4/selftest/tests.py @@ -615,6 +615,7 @@ planpythontestsuite("ad_dc_ntvfs:local", "samba.tests.samba_tool.computer") planpythontestsuite("ad_dc:local", "samba.tests.samba_tool.ntacl") planpythontestsuite("none", "samba.tests.samba_tool.provision_password_check") planpythontestsuite("none", "samba.tests.samba_tool.help") +planpythontestsuite("ad_dc_ntvfs:local", "samba.tests.samba_tool.passwordsettings") # Run these against chgdcpass to share the runtime load planpythontestsuite("chgdcpass:local", "samba.tests.samba_tool.sites")