tests: Add tests for samba-tool passwordsettings commands
authorTim Beale <timbeale@catalyst.net.nz>
Thu, 10 May 2018 04:22:06 +0000 (16:22 +1200)
committerGarming Sam <garming@samba.org>
Fri, 11 May 2018 07:06:10 +0000 (09:06 +0200)
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 <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
Signed-off-by: Tim Beale <timbeale@catalyst.net.nz>
Autobuild-User(master): Garming Sam <garming@samba.org>
Autobuild-Date(master): Fri May 11 09:06:10 CEST 2018 on sn-devel-144

python/samba/tests/samba_tool/base.py
python/samba/tests/samba_tool/passwordsettings.py [new file with mode: 0644]
source4/selftest/tests.py

index 89a09225e643513642f59518e9161a510592a845..06e19c190874c4e4385972aa34f0298f2fcc7b69 100644 (file)
@@ -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 (file)
index 0000000..5766352
--- /dev/null
@@ -0,0 +1,70 @@
+# Test 'samba-tool domain passwordsettings' sub-commands
+#
+# Copyright (C) Andrew Bartlett <abartlet@samba.org> 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 <http://www.gnu.org/licenses/>.
+#
+
+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)
+
index 405f2b5b7eae306473134587315b3310cc8df0b9..88af6078894c3edf1276696f8e7f3eaf14ba3008 100755 (executable)
@@ -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")