Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
slapos.core
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Xavier Thompson
slapos.core
Commits
4bdf69f8
Commit
4bdf69f8
authored
Nov 29, 2022
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
test_grid_utils: Add more SlapPopen tests
parent
5591f894
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
233 additions
and
1 deletion
+233
-1
slapos/tests/test_grid_utils.py
slapos/tests/test_grid_utils.py
+233
-1
No files found.
slapos/tests/test_grid_utils.py
View file @
4bdf69f8
...
@@ -27,12 +27,18 @@
...
@@ -27,12 +27,18 @@
from
__future__
import
unicode_literals
from
__future__
import
unicode_literals
import
logging
import
logging
import
os
import
os
import
subprocess
import
psutil
import
sys
import
sys
import
tempfile
import
tempfile
import
textwrap
import
textwrap
import
time
import
unittest
import
unittest
if
sys
.
version_info
>=
(
3
,):
import
subprocess
else
:
import
subprocess32
as
subprocess
import
mock
import
mock
import
slapos.grid.utils
import
slapos.grid.utils
...
@@ -169,6 +175,232 @@ class SlapPopenTestCase(unittest.TestCase):
...
@@ -169,6 +175,232 @@ class SlapPopenTestCase(unittest.TestCase):
for
fd
in
(
child_stdin_r
,
child_stdout_r
,
child_stdout_w
,
stdin_backup
,
stdout_backup
):
for
fd
in
(
child_stdin_r
,
child_stdout_r
,
child_stdout_w
,
stdin_backup
,
stdout_backup
):
os
.
close
(
fd
)
os
.
close
(
fd
)
def
test_stderr
(
self
):
self
.
script
.
write
(
textwrap
.
dedent
(
"""
\
#!/bin/sh
>&2 echo "hello"
exit 123
"""
).
encode
())
self
.
script
.
close
()
logger
=
mock
.
MagicMock
()
program
=
slapos
.
grid
.
utils
.
SlapPopen
(
self
.
script
.
name
,
stdout
=
None
,
stderr
=
subprocess
.
PIPE
,
logger
=
logger
)
# error code, and error output are returned
self
.
assertEqual
(
123
,
program
.
returncode
)
self
.
assertEqual
(
'hello
\
n
'
,
program
.
error
)
self
.
assertEqual
(
''
,
program
.
output
)
# no output, nothing is logged "live"
self
.
assertFalse
(
logger
.
info
.
called
)
def
test_stdout_and_stderr
(
self
):
self
.
script
.
write
(
textwrap
.
dedent
(
"""
\
#!/bin/sh
echo "hello"
>&2 echo "world"
exit 123
"""
).
encode
())
self
.
script
.
close
()
logger
=
mock
.
MagicMock
()
program
=
slapos
.
grid
.
utils
.
SlapPopen
(
self
.
script
.
name
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
logger
=
logger
)
# error code, stderr and stdout are returned
self
.
assertEqual
(
123
,
program
.
returncode
)
self
.
assertEqual
(
'hello
\
n
'
,
program
.
output
)
self
.
assertEqual
(
'world
\
n
'
,
program
.
error
)
# only stdout is logged
logger
.
info
.
assert_called_once_with
(
'hello'
)
def
test_timeout_stdout_multiline
(
self
):
self
.
script
.
write
(
textwrap
.
dedent
(
"""
\
#!/bin/sh
for i in $(seq 100)
do
echo .
sleep 0.1
done
"""
).
encode
())
self
.
script
.
close
()
logger
=
mock
.
MagicMock
()
start
=
time
.
time
()
with
self
.
assertRaises
(
subprocess
.
TimeoutExpired
)
as
cm
:
program
=
slapos
.
grid
.
utils
.
SlapPopen
(
self
.
script
.
name
,
timeout
=
1
,
logger
=
logger
)
# the timeout was respected
elapsed
=
time
.
time
()
-
start
self
.
assertLess
(
elapsed
,
5
)
self
.
assertGreaterEqual
(
elapsed
,
1
)
# the output before timeout is captured
self
.
assertEqual
(
cm
.
exception
.
output
,
'.
\
n
'
*
10
)
# each line before timeout is logged "live" as well
self
.
assertEqual
(
logger
.
info
.
call_args_list
,
[
mock
.
call
(
'.'
)]
*
10
)
def
test_timeout_stdout_oneline
(
self
):
self
.
script
.
write
(
textwrap
.
dedent
(
"""
\
#!/bin/sh
for i in $(seq 100)
do
echo -n .
sleep 0.1
done
"""
).
encode
())
self
.
script
.
close
()
logger
=
mock
.
MagicMock
()
start
=
time
.
time
()
with
self
.
assertRaises
(
subprocess
.
TimeoutExpired
)
as
cm
:
program
=
slapos
.
grid
.
utils
.
SlapPopen
(
self
.
script
.
name
,
timeout
=
1
,
logger
=
logger
)
# the timeout was respected
elapsed
=
time
.
time
()
-
start
self
.
assertLess
(
elapsed
,
5
)
self
.
assertGreaterEqual
(
elapsed
,
1
)
# the output before timeout is captured
self
.
assertEqual
(
cm
.
exception
.
output
,
'.'
*
10
)
# endline is never reached, so nothing is logged "live"
self
.
assertFalse
(
logger
.
info
.
called
)
def
test_timeout_stdout_and_stderr
(
self
):
self
.
script
.
write
(
textwrap
.
dedent
(
"""
\
#!/bin/sh
for i in $(seq 100)
do
>&2 echo -n -
echo -n .
sleep 0.1
done
"""
).
encode
())
self
.
script
.
close
()
logger
=
mock
.
MagicMock
()
start
=
time
.
time
()
with
self
.
assertRaises
(
subprocess
.
TimeoutExpired
)
as
cm
:
program
=
slapos
.
grid
.
utils
.
SlapPopen
(
self
.
script
.
name
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
timeout
=
1
,
logger
=
logger
)
# the timeout was respected
elapsed
=
time
.
time
()
-
start
self
.
assertLess
(
elapsed
,
5
)
self
.
assertGreaterEqual
(
elapsed
,
1
)
# the output before timeout is captured
self
.
assertEqual
(
cm
.
exception
.
output
,
'.'
*
10
)
self
.
assertEqual
(
cm
.
exception
.
stderr
,
'-'
*
10
)
# endline is never reached, so nothing is logged "live"
self
.
assertFalse
(
logger
.
info
.
called
)
def
test_timeout_no_stdout_no_stderr
(
self
):
self
.
script
.
write
(
b'#!/bin/sh
\
n
sleep 20'
)
self
.
script
.
close
()
logger
=
mock
.
MagicMock
()
start
=
time
.
time
()
with
self
.
assertRaises
(
subprocess
.
TimeoutExpired
)
as
cm
:
program
=
slapos
.
grid
.
utils
.
SlapPopen
(
self
.
script
.
name
,
timeout
=
1
,
logger
=
logger
)
# the timeout was respected
elapsed
=
time
.
time
()
-
start
self
.
assertLess
(
elapsed
,
5
)
self
.
assertGreaterEqual
(
elapsed
,
1
)
# no output
self
.
assertEqual
(
cm
.
exception
.
output
,
''
)
self
.
assertEqual
(
cm
.
exception
.
stderr
,
''
)
# nothing is logged "live"
self
.
assertFalse
(
logger
.
info
.
called
)
def
test_timeout_killed
(
self
):
self
.
script
.
write
(
b'#!/bin/sh
\
n
echo -n $$
\
n
sleep 20'
)
self
.
script
.
close
()
logger
=
mock
.
MagicMock
()
start
=
time
.
time
()
with
self
.
assertRaises
(
subprocess
.
TimeoutExpired
)
as
cm
:
program
=
slapos
.
grid
.
utils
.
SlapPopen
(
self
.
script
.
name
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
timeout
=
1
,
logger
=
logger
)
# the timeout was respected
elapsed
=
time
.
time
()
-
start
self
.
assertLess
(
elapsed
,
5
)
self
.
assertGreaterEqual
(
elapsed
,
1
)
# output pid
pid
=
int
(
cm
.
exception
.
output
)
self
.
assertEqual
(
cm
.
exception
.
stderr
,
''
)
# subprocess has been killed
self
.
assertFalse
(
psutil
.
pid_exists
(
pid
))
# endline is never reached, so nothing is logged "live"
self
.
assertFalse
(
logger
.
info
.
called
)
def
test_timeout_killed_grandchild
(
self
):
self
.
script
.
write
(
textwrap
.
dedent
(
"""
\
#!/bin/sh
(echo $(exec /bin/sh -c 'echo "$PPID"'); sleep 20)
"""
).
encode
())
self
.
script
.
close
()
logger
=
mock
.
MagicMock
()
start
=
time
.
time
()
with
self
.
assertRaises
(
subprocess
.
TimeoutExpired
)
as
cm
:
program
=
slapos
.
grid
.
utils
.
SlapPopen
(
self
.
script
.
name
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
timeout
=
1
,
logger
=
logger
)
# the timeout was respected
elapsed
=
time
.
time
()
-
start
self
.
assertLess
(
elapsed
,
5
)
self
.
assertGreaterEqual
(
elapsed
,
1
)
# output pid
pid
=
int
(
cm
.
exception
.
output
)
self
.
assertEqual
(
cm
.
exception
.
stderr
,
''
)
# sub-subprocess has been killed
self
.
assertFalse
(
psutil
.
pid_exists
(
pid
))
# the pid is logged "live"
logger
.
info
.
assert_called_once_with
(
str
(
pid
))
class
DummySystemExit
(
Exception
):
class
DummySystemExit
(
Exception
):
"""Dummy exception raised instead of SystemExit so that if something goes
"""Dummy exception raised instead of SystemExit so that if something goes
...
...
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