From acb0172ddf6fec45874e0ddae13fcb0ff368bc80 Mon Sep 17 00:00:00 2001 From: Edwin Eefting Date: Wed, 9 Feb 2022 12:24:24 +0100 Subject: [PATCH] more tests --- tests/test_executenode.py | 34 +++++++++++++++++++++++++++------- zfs_autobackup/ExecuteNode.py | 6 ++++-- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/tests/test_executenode.py b/tests/test_executenode.py index 7f6e17e..84200db 100644 --- a/tests/test_executenode.py +++ b/tests/test_executenode.py @@ -158,8 +158,6 @@ class TestExecuteNode(unittest2.TestCase): def test_script_handlers(self): - - def test(node): results = [] cmd_pipe=node.script(lines=["echo line1", "echo line2 1>&2", "exit 123"], @@ -177,15 +175,37 @@ class TestExecuteNode(unittest2.TestCase): with self.subTest("local"): test(ExecuteNode(debug_output=True)) - def test_script_defaults(self): - def handler(line): - pass - + result=[] nodea=ExecuteNode(debug_output=True) - cmd_pipe=nodea.script(lines=["echo test"], stdout_handler=handler) + cmd_pipe=nodea.script(lines=["echo test"], stdout_handler=lambda line: result.append(line)) cmd_pipe.execute() + self.assertEqual(result, ["test"]) + + def test_script_pipe(self): + + result=[] + nodea=ExecuteNode() + cmd_pipe=nodea.script(lines=["echo test"]) + nodea.script(lines=["tr e E"], inp=cmd_pipe,stdout_handler=lambda line: result.append(line)) + cmd_pipe.execute() + self.assertEqual(result, ["tEst"]) + + + def test_mixed(self): + + #should be able to mix run() and script() + node=ExecuteNode() + + result=[] + pipe=node.run(["echo", "test"], pipe=True) + pipe=node.script(["tr e E"], inp=pipe, stdout_handler=lambda line: result.append(line)) + pipe.execute() + self.assertEqual(result, ["tEst"]) + + + diff --git a/zfs_autobackup/ExecuteNode.py b/zfs_autobackup/ExecuteNode.py index 2513f4e..6736010 100644 --- a/zfs_autobackup/ExecuteNode.py +++ b/zfs_autobackup/ExecuteNode.py @@ -183,13 +183,12 @@ class ExecuteNode(LogStub): else: return output_lines - def script(self, lines, inp=None, stdout_handler=None, stderr_handler=None, exit_handler=None, valid_exitcodes=None, readonly=False, hide_errors=False): + def script(self, lines, inp=None, stdout_handler=None, stderr_handler=None, exit_handler=None, valid_exitcodes=None, readonly=False, hide_errors=False, pipe=False): """Run a multiline script on the node. This is much more low level than run() and allows for finer grained control. Either uses a local shell (sh -c) or remote shell (ssh) to execute the command. - It will always return a CmdPipe that you should call execute on, or pipe to another script. (via inp=...) You need to do your own escaping/quoting. It will do logging of stderr and exit codes, but you should specify your stdout handler when calling CmdPipe.execute. @@ -202,6 +201,7 @@ class ExecuteNode(LogStub): :param readonly: make this True if the command doesn't make any changes and is safe to execute in testmode :param valid_exitcodes: list of valid exit codes for this command. Use [] to accept all exit codes. Default [0] :param hide_errors: don't show stderr output as error, instead show it as debugging output (use to hide expected errors) + :param pipe: return CmdPipe instead of executing it. (pipe this into another run() command via inp=...) """ @@ -218,6 +218,8 @@ class ExecuteNode(LogStub): def internal_stdout_handler(line): self.debug("STDOUT > " + line.rstrip()) stdout_handler(line) + else: + internal_stdout_handler=stdout_handler def internal_stderr_handler(line): self._parse_stderr(line, hide_errors)