From: Douglas Bagnall Date: Wed, 7 Sep 2022 04:57:46 +0000 (+1200) Subject: pytest: samba-tool: coalesce run*cmd functions X-Git-Tag: talloc-2.4.0~1118 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=273797d8cf937eaa9262ad5b9f860d3f9a0fb0c4;p=thirdparty%2Fsamba.git pytest: samba-tool: coalesce run*cmd functions 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 Reviewed-by: Andrew Bartlett --- diff --git a/python/samba/tests/samba_tool/base.py b/python/samba/tests/samba_tool/base.py index 728ed1139d0..3a6038cb7e9 100644 --- a/python/samba/tests/samba_tool/base.py +++ b/python/samba/tests/samba_tool/base.py @@ -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