File: test_lutris_wrapper.py

package info (click to toggle)
lutris 0.5.19-3
  • links: PTS, VCS
  • area: contrib
  • in suites: forky
  • size: 7,636 kB
  • sloc: python: 42,264; xml: 151; makefile: 77; sh: 30
file content (123 lines) | stat: -rw-r--r-- 4,447 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import os.path
import subprocess
import sys
import unittest

if os.path.isfile("share/lutris/bin/lutris-wrapper"):
    lutris_wrapper_bin = "share/lutris/bin/lutris-wrapper"
else:
    lutris_wrapper_bin = "lutris-wrapper"


class LutrisWrapperTestCase(unittest.TestCase):
    def test_excluded_initial_process(self):
        "Test that an excluded process that starts a monitored process works"
        env = os.environ.copy()
        env["PYTHONPATH"] = ":".join(sys.path)
        # run the lutris-wrapper with a bash subshell. bash is "excluded"
        wrapper_proc = subprocess.Popen(
            [
                sys.executable,
                lutris_wrapper_bin,
                "title",
                "0",
                "1",
                "bash",
                "bash",
                "-c",
                "echo Hello World; exec 1>&-; while sleep infinity; do true; done",
            ],
            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:
                if line.strip() == b"Hello World":
                    # We found the output we're looking for.
                    break
            else:
                self.fail("stdout EOF unexpectedly")

            # 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:
                wrapper_proc.terminate()
                wrapper_proc.wait(30)
            wrapper_proc.stdout.close()

    def test_cleanup_children(self):
        "Test that nonresponsive child processes can be killed with 2x sigterm"
        env = os.environ.copy()
        env["PYTHONPATH"] = ":".join(sys.path)
        # 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,
                "title",
                "0",
                "0",
                "bash",
                "-c",
                "trap '' SIGTERM; echo Hello World; exec 1>&-; while sleep infinity; do true; done",
            ],
            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:
                if line.strip() == b"Hello World":
                    # 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:
                if line.strip() == b"--terminated processes--":
                    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()