Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
common_helper_process
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fact-gitdep
common_helper_process
Commits
d4648f57
Commit
d4648f57
authored
Aug 29, 2017
by
Peter Weidenbach
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring
parent
5cfc68cd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
16 deletions
+19
-16
fail_safe_subprocess.py
common_helper_process/fail_safe_subprocess.py
+4
-4
test_fail_safe_subprocess.py
test/test_fail_safe_subprocess.py
+15
-12
No files found.
common_helper_process/fail_safe_subprocess.py
View file @
d4648f57
...
...
@@ -30,18 +30,18 @@ def execute_shell_command_get_return_code(shell_command, timeout=None):
:return: str, int
"""
output
=
""
r
c
=
1
r
eturn_code
=
1
pl
=
Popen
(
shell_command
,
shell
=
True
,
stdout
=
PIPE
,
stderr
=
STDOUT
)
try
:
output
=
pl
.
communicate
(
timeout
=
timeout
)[
0
]
.
decode
(
'utf-8'
,
errors
=
'replace'
)
r
c
=
pl
.
returncode
r
eturn_code
=
pl
.
returncode
except
TimeoutExpired
:
logging
.
warning
(
"Execution timeout!"
)
pl
.
kill
()
output
=
pl
.
communicate
()[
0
]
.
decode
(
'utf-8'
,
errors
=
'replace'
)
output
+=
"
\n\n
ERROR: execution timed out!"
r
c
=
1
return
output
,
r
c
r
eturn_code
=
1
return
output
,
r
eturn_code
def
execute_interactive_shell_command
(
shell_command
,
timeout
=
60
,
inputs
=
{}):
...
...
test/test_fail_safe_subprocess.py
View file @
d4648f57
...
...
@@ -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
class
TestProcessHelper
(
unittest
.
TestCase
):
class
TestProcessHelper
Public
(
unittest
.
TestCase
):
def
test_execute_shell_command
(
self
):
result
=
execute_shell_command
(
"echo 'test 123'"
)
...
...
@@ -35,11 +35,12 @@ class TestProcessHelper(unittest.TestCase):
self
.
assertEqual
(
rc
,
1
,
'return code not correct'
)
self
.
assertGreater
(
5
,
run_time
,
"command not aborted"
)
def
test_parse_inputs
(
self
):
test_dict
=
{
'a'
:
'a_out'
,
'b'
:
'b_out'
}
trigger
,
inputs
=
_parse_inputs
(
test_dict
)
self
.
assertEqual
(
trigger
,
[
'a'
,
'b'
])
self
.
assertEqual
(
inputs
,
[
'a_out'
,
'b_out'
])
def
test_interactive_shell_command_none_correct_input
(
self
):
script_path
=
os
.
path
.
join
(
get_dir_of_file
(
__file__
),
'data/interactive.sh'
)
output
,
ret_code
=
execute_interactive_shell_command
(
script_path
,
timeout
=
2
)
assert
'give me some input'
in
output
assert
'
\n\n
Error: Execution timed out!'
in
output
assert
ret_code
>
0
def
test_interactive_shell_command
(
self
):
script_path
=
os
.
path
.
join
(
get_dir_of_file
(
__file__
),
'data/interactive.sh'
)
...
...
@@ -49,9 +50,11 @@ class TestProcessHelper(unittest.TestCase):
assert
'second=test_input_2'
in
output
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'
)
output
,
ret_code
=
execute_interactive_shell_command
(
script_path
,
timeout
=
2
)
assert
'give me some input'
in
output
assert
'
\n\n
Error: Execution timed out!'
in
output
assert
ret_code
>
0
class
TestProcessHelperInternal
(
unittest
.
TestCase
):
def
test_parse_inputs
(
self
):
test_dict
=
{
'a'
:
'a_out'
,
'b'
:
'b_out'
}
trigger
,
inputs
=
_parse_inputs
(
test_dict
)
self
.
assertEqual
(
trigger
,
[
'a'
,
'b'
])
self
.
assertEqual
(
inputs
,
[
'a_out'
,
'b_out'
])
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment