From 8233e7b35e9e1d6c2ff80ee47e7289a9f331f72c Mon Sep 17 00:00:00 2001 From: Edwin Eefting Date: Sat, 29 Jan 2022 10:10:18 +0100 Subject: [PATCH] script mode testing and fixes --- tests/test_executenode.py | 30 +++++++++++++++++++----------- zfs_autobackup/ExecuteNode.py | 2 +- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/tests/test_executenode.py b/tests/test_executenode.py index a35b19d..7f6e17e 100644 --- a/tests/test_executenode.py +++ b/tests/test_executenode.py @@ -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() diff --git a/zfs_autobackup/ExecuteNode.py b/zfs_autobackup/ExecuteNode.py index 91f5557..2513f4e 100644 --- a/zfs_autobackup/ExecuteNode.py +++ b/zfs_autobackup/ExecuteNode.py @@ -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]