more tests

This commit is contained in:
Edwin Eefting 2022-02-09 12:24:24 +01:00
parent 53db61de96
commit acb0172ddf
2 changed files with 31 additions and 9 deletions

@ -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"])

@ -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)