pytest: samba-tool: coalesce run*cmd functions
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Wed, 7 Sep 2022 04:57:46 +0000 (16:57 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 8 Sep 2022 22:34:36 +0000 (22:34 +0000)
We have had three different functions for resolving samba-tool commands,
depending on whether they are nested 1, 2, or n deep (where n could also
be 1 or 2). This API evolved around a separation of sub-command names and
options, so that the Command that was eventually found could be given the
right outf and errf.

Now we can just use the same outf and errf for all levels, and we can not
care about this distinction.

All these functions are now synonyms, and we keep them all for now for
backward-compatibility.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
python/samba/tests/samba_tool/base.py

index 728ed1139d0db9ccbe083760270021b09fcd8348..3a6038cb7e999847bcc711329fe1503b34ed8702 100644 (file)
@@ -72,24 +72,24 @@ class SambaToolCmdTest(samba.tests.BlackboxTestCase):
                       credentials=creds, lp=lp)
         return samdb
 
+    def _run(self, *argv):
+        """run an arbitrary level command"""
+        cmd, args = cmd_sambatool()._resolve('samba-tool', *argv,
+                                             outf=self.stringIO(),
+                                             errf=self.stringIO())
+        result = cmd._run(*args)
+        return (result, cmd.outf.getvalue(), cmd.errf.getvalue())
+
     def runcmd(self, name, *args):
         """run a single level command"""
-        cmd = cmd_sambatool.subcommands[name]
-        cmd.outf = self.stringIO()
-        cmd.errf = self.stringIO()
-        result = cmd._run("samba-tool %s" % name, *args)
-        return (result, cmd.outf.getvalue(), cmd.errf.getvalue())
+        return self._run(name, *args)
 
     def runsubcmd(self, name, sub, *args):
         """run a command with sub commands"""
         # The reason we need this function separate from runcmd is
         # that the .outf StringIO assignment is overridden if we use
         # runcmd, so we can't capture stdout and stderr
-        cmd = cmd_sambatool.subcommands[name].subcommands[sub]
-        cmd.outf = self.stringIO()
-        cmd.errf = self.stringIO()
-        result = cmd._run("samba-tool %s %s" % (name, sub), *args)
-        return (result, cmd.outf.getvalue(), cmd.errf.getvalue())
+        return self._run(name, sub, *args)
 
     def runsublevelcmd(self, name, sublevels, *args):
         """run a command with any number of sub command levels"""
@@ -98,15 +98,7 @@ class SambaToolCmdTest(samba.tests.BlackboxTestCase):
         # 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 = self.stringIO()
-        cmd.errf = self.stringIO()
-        result = cmd._run(cmd_str, *args)
-        return (result, cmd.outf.getvalue(), cmd.errf.getvalue())
+        return self._run(name, *sublevels, *args)
 
     def assertCmdSuccess(self, exit, out, err, msg=""):
         # Make sure we allow '\n]\n' in stdout and stderr