2019-03-03 21:44:18 +00:00
|
|
|
import os
|
|
|
|
import os.path
|
|
|
|
import subprocess
|
- Replaced pipenv with poetry
- Updated min version check in setup.py to Python 3.6
- Updated isort config file and calls to align with v5.x
- Added init-hook for gi imports in .pylintrc to avoid invalid no-member issues
- Makefile: added lock, show-tree, bandit, black, mypy; updated test, cover, dev, isort, autopep8, check, isort-check, flake8, pylint; removed req, requirements;
- Updated .travis.yml to use poetry and make
- Added my email in AUTHORS
- Updated CONTRIBUTING.md
- Updated lint_python.yml to use poetry and make, reorganized instructions to have all install related steps first
- sorted imports: lutris, lutris-wrapper, cleanup_prefix.py and multiple files in tests dir
2021-10-24 18:42:06 +00:00
|
|
|
import sys
|
|
|
|
import unittest
|
2019-03-03 21:44:18 +00:00
|
|
|
|
2024-02-25 05:02:06 +00:00
|
|
|
if os.path.isfile("share/lutris/bin/lutris-wrapper"):
|
|
|
|
lutris_wrapper_bin = "share/lutris/bin/lutris-wrapper"
|
2019-03-03 21:44:18 +00:00
|
|
|
else:
|
2024-02-25 05:02:06 +00:00
|
|
|
lutris_wrapper_bin = "lutris-wrapper"
|
2019-03-03 21:44:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LutrisWrapperTestCase(unittest.TestCase):
|
2020-01-01 20:49:18 +00:00
|
|
|
def test_excluded_initial_process(self):
|
|
|
|
"Test that an excluded process that starts a monitored process works"
|
|
|
|
env = os.environ.copy()
|
2024-02-25 05:02:06 +00:00
|
|
|
env["PYTHONPATH"] = ":".join(sys.path)
|
2020-01-01 20:49:18 +00:00
|
|
|
# run the lutris-wrapper with a bash subshell. bash is "excluded"
|
|
|
|
wrapper_proc = subprocess.Popen(
|
|
|
|
[
|
|
|
|
sys.executable,
|
|
|
|
lutris_wrapper_bin,
|
2024-02-25 05:02:06 +00:00
|
|
|
"title",
|
|
|
|
"0",
|
|
|
|
"1",
|
|
|
|
"bash",
|
|
|
|
"bash",
|
|
|
|
"-c",
|
|
|
|
"echo Hello World; exec 1>&-; while sleep infinity; do true; done",
|
2020-01-01 20:49:18 +00:00
|
|
|
],
|
|
|
|
stdin=subprocess.DEVNULL,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
env=env,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
# Wait for the "Hello World" message that indicates that the process
|
|
|
|
# tree has started. This message arrives on stdout.
|
2021-02-16 03:36:13 +00:00
|
|
|
for line in wrapper_proc.stdout:
|
2024-02-25 05:02:06 +00:00
|
|
|
if line.strip() == b"Hello World":
|
2021-02-16 03:36:13 +00:00
|
|
|
# We found the output we're looking for.
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.fail("stdout EOF unexpectedly")
|
|
|
|
|
2020-01-01 20:49:18 +00:00
|
|
|
# Wait a short while to see if lutris-wrapper will exit (it shouldn't)
|
|
|
|
try:
|
|
|
|
wrapper_proc.wait(0.5)
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
# as expected, the process is still alive.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# the test failed because the process exited for some reason.
|
|
|
|
self.fail("Process exited unexpectedly")
|
|
|
|
finally:
|
|
|
|
if wrapper_proc.returncode is None:
|
2021-02-16 03:36:13 +00:00
|
|
|
wrapper_proc.terminate()
|
|
|
|
wrapper_proc.wait(30)
|
2020-01-01 20:49:18 +00:00
|
|
|
wrapper_proc.stdout.close()
|
|
|
|
|
2019-03-03 21:44:18 +00:00
|
|
|
def test_cleanup_children(self):
|
|
|
|
"Test that nonresponsive child processes can be killed with 2x sigterm"
|
|
|
|
env = os.environ.copy()
|
2024-02-25 05:02:06 +00:00
|
|
|
env["PYTHONPATH"] = ":".join(sys.path)
|
2019-03-03 21:44:18 +00:00
|
|
|
# First, we run the lutris-wrapper with a bash subshell which ignores
|
|
|
|
# SIGTERMs, emits a message to indicate readiness, and then closes
|
|
|
|
# stdout.
|
|
|
|
wrapper_proc = subprocess.Popen(
|
|
|
|
[
|
|
|
|
sys.executable,
|
|
|
|
lutris_wrapper_bin,
|
2024-02-25 05:02:06 +00:00
|
|
|
"title",
|
|
|
|
"0",
|
|
|
|
"0",
|
|
|
|
"bash",
|
|
|
|
"-c",
|
|
|
|
"trap '' SIGTERM; echo Hello World; exec 1>&-; while sleep infinity; do true; done",
|
2019-03-03 21:44:18 +00:00
|
|
|
],
|
|
|
|
stdin=subprocess.DEVNULL,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
env=env,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
# Wait for the "Hello World" message that indicates that the process
|
|
|
|
# tree has started. This message arrives on stdout.
|
|
|
|
for line in wrapper_proc.stdout:
|
2024-02-25 05:02:06 +00:00
|
|
|
if line.strip() == b"Hello World":
|
2019-03-03 21:44:18 +00:00
|
|
|
# We found the output we're looking for.
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.fail("stdout EOF unexpectedly")
|
|
|
|
|
|
|
|
# Send first SIGTERM
|
|
|
|
wrapper_proc.terminate()
|
|
|
|
|
|
|
|
# Wait for confirmation that lutris-wrapper got our signal.
|
|
|
|
for line in wrapper_proc.stdout:
|
2024-02-25 05:02:06 +00:00
|
|
|
if line.strip() == b"--terminated processes--":
|
2019-03-03 21:44:18 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.fail("stdout EOF unexpectedly")
|
|
|
|
|
|
|
|
wrapper_proc.stdout.close() # don't need this anymore.
|
|
|
|
|
|
|
|
# Wait a short while to see if lutris-wrapper will exit (it shouldn't)
|
|
|
|
try:
|
|
|
|
wrapper_proc.wait(0.5)
|
|
|
|
except subprocess.TimeoutExpired:
|
|
|
|
# as expected, the process is still alive.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# the test failed because the process exited for some reason.
|
|
|
|
self.fail("Process exited unexpectedly")
|
|
|
|
|
|
|
|
# Send second SIGTERM
|
|
|
|
wrapper_proc.terminate()
|
|
|
|
|
|
|
|
# verify that lutris-wrapper closes.
|
|
|
|
wrapper_proc.wait(30)
|
|
|
|
finally:
|
|
|
|
if wrapper_proc.returncode is None:
|
|
|
|
wrapper_proc.kill()
|
|
|
|
wrapper_proc.wait(30)
|
|
|
|
wrapper_proc.stdout.close()
|