script mode testing and fixes

This commit is contained in:
Edwin Eefting 2022-01-29 10:10:18 +01:00
parent e1fb7a37be
commit 8233e7b35e
2 changed files with 20 additions and 12 deletions

View File

@ -158,24 +158,32 @@ class TestExecuteNode(unittest2.TestCase):
def test_script_handlers(self):
results=[]
nodea=ExecuteNode(debug_output=True)
cmd_pipe=nodea.script(lines=["echo line1", "echo line2 1>&2", "exit 123"],
stdout_handler=lambda line: results.append(line),
stderr_handler=lambda line: results.append(line),
exit_handler=lambda exit_code: results.append(exit_code),
valid_exitcodes=[123]
)
cmd_pipe.execute()
self.assertEqual(results, ["line1", "line2", 123 ])
def test(node):
results = []
cmd_pipe=node.script(lines=["echo line1", "echo line2 1>&2", "exit 123"],
stdout_handler=lambda line: results.append(line),
stderr_handler=lambda line: results.append(line),
exit_handler=lambda exit_code: results.append(exit_code),
valid_exitcodes=[123]
)
cmd_pipe.execute()
self.assertEqual(results, ["line1", "line2", 123 ])
with self.subTest("remote"):
test(ExecuteNode(ssh_to="localhost", debug_output=True))
#
with self.subTest("local"):
test(ExecuteNode(debug_output=True))
def test_script_defaults(self):
def handler(line):
pass
nodea=ExecuteNode(debug_output=True, ssh_to="localhost")
nodea=ExecuteNode(debug_output=True)
cmd_pipe=nodea.script(lines=["echo test"], stdout_handler=handler)
cmd_pipe.execute()

View File

@ -100,7 +100,7 @@ class ExecuteNode(LogStub):
:param cmd: the actual command, should be a list, where the first item is the command
and the rest are parameters. use ExecuteNode.PIPE to add an unescaped |
(if you want to use system piping instead of python piping)
:param pipe: return CmdPipe instead of executing it. (pipe this into anoter run() command via inp=...)
:param pipe: return CmdPipe instead of executing it. (pipe this into another run() command via inp=...)
:param inp: Can be None, a string or a CmdPipe that was previously returned.
:param tab_split: split tabbed files in output into a list
:param valid_exitcodes: list of valid exit codes for this command. Use [] to accept all exit codes. Default [0]