Commit d4648f57 by Peter Weidenbach

refactoring

parent 5cfc68cd
...@@ -30,18 +30,18 @@ def execute_shell_command_get_return_code(shell_command, timeout=None): ...@@ -30,18 +30,18 @@ def execute_shell_command_get_return_code(shell_command, timeout=None):
:return: str, int :return: str, int
""" """
output = "" output = ""
rc = 1 return_code = 1
pl = Popen(shell_command, shell=True, stdout=PIPE, stderr=STDOUT) pl = Popen(shell_command, shell=True, stdout=PIPE, stderr=STDOUT)
try: try:
output = pl.communicate(timeout=timeout)[0].decode('utf-8', errors='replace') output = pl.communicate(timeout=timeout)[0].decode('utf-8', errors='replace')
rc = pl.returncode return_code = pl.returncode
except TimeoutExpired: except TimeoutExpired:
logging.warning("Execution timeout!") logging.warning("Execution timeout!")
pl.kill() pl.kill()
output = pl.communicate()[0].decode('utf-8', errors='replace') output = pl.communicate()[0].decode('utf-8', errors='replace')
output += "\n\nERROR: execution timed out!" output += "\n\nERROR: execution timed out!"
rc = 1 return_code = 1
return output, rc return output, return_code
def execute_interactive_shell_command(shell_command, timeout=60, inputs={}): def execute_interactive_shell_command(shell_command, timeout=60, inputs={}):
......
...@@ -7,7 +7,7 @@ from common_helper_process import execute_shell_command, execute_shell_command_g ...@@ -7,7 +7,7 @@ from common_helper_process import execute_shell_command, execute_shell_command_g
from common_helper_process.fail_safe_subprocess import _parse_inputs from common_helper_process.fail_safe_subprocess import _parse_inputs
class TestProcessHelper(unittest.TestCase): class TestProcessHelperPublic(unittest.TestCase):
def test_execute_shell_command(self): def test_execute_shell_command(self):
result = execute_shell_command("echo 'test 123'") result = execute_shell_command("echo 'test 123'")
...@@ -35,11 +35,12 @@ class TestProcessHelper(unittest.TestCase): ...@@ -35,11 +35,12 @@ class TestProcessHelper(unittest.TestCase):
self.assertEqual(rc, 1, 'return code not correct') self.assertEqual(rc, 1, 'return code not correct')
self.assertGreater(5, run_time, "command not aborted") self.assertGreater(5, run_time, "command not aborted")
def test_parse_inputs(self): def test_interactive_shell_command_none_correct_input(self):
test_dict = {'a': 'a_out', 'b': 'b_out'} script_path = os.path.join(get_dir_of_file(__file__), 'data/interactive.sh')
trigger, inputs = _parse_inputs(test_dict) output, ret_code = execute_interactive_shell_command(script_path, timeout=2)
self.assertEqual(trigger, ['a', 'b']) assert 'give me some input' in output
self.assertEqual(inputs, ['a_out', 'b_out']) assert '\n\nError: Execution timed out!' in output
assert ret_code > 0
def test_interactive_shell_command(self): def test_interactive_shell_command(self):
script_path = os.path.join(get_dir_of_file(__file__), 'data/interactive.sh') script_path = os.path.join(get_dir_of_file(__file__), 'data/interactive.sh')
...@@ -49,9 +50,11 @@ class TestProcessHelper(unittest.TestCase): ...@@ -49,9 +50,11 @@ class TestProcessHelper(unittest.TestCase):
assert 'second=test_input_2' in output assert 'second=test_input_2' in output
assert ret_code == 0 assert ret_code == 0
def test_interactive_shell_command_none_correct_input(self):
script_path = os.path.join(get_dir_of_file(__file__), 'data/interactive.sh') class TestProcessHelperInternal(unittest.TestCase):
output, ret_code = execute_interactive_shell_command(script_path, timeout=2)
assert 'give me some input' in output def test_parse_inputs(self):
assert '\n\nError: Execution timed out!' in output test_dict = {'a': 'a_out', 'b': 'b_out'}
assert ret_code > 0 trigger, inputs = _parse_inputs(test_dict)
self.assertEqual(trigger, ['a', 'b'])
self.assertEqual(inputs, ['a_out', 'b_out'])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment