1from __future__ import absolute_import
2
3import os
4import sys
5import unittest
6
7from mozprocess import ProcessHandler
8
9
10here = os.path.dirname(os.path.abspath(__file__))
11
12
13class ProcTest(unittest.TestCase):
14    @classmethod
15    def setUpClass(cls):
16        cls.proclaunch = os.path.join(here, "proclaunch.py")
17        cls.python = sys.executable
18
19    def determine_status(self, proc, isalive=False, expectedfail=()):
20        """
21        Use to determine if the situation has failed.
22        Parameters:
23            proc -- the processhandler instance
24            isalive -- Use True to indicate we pass if the process exists; however, by default
25                       the test will pass if the process does not exist (isalive == False)
26            expectedfail -- Defaults to [], used to indicate a list of fields
27                            that are expected to fail
28        """
29        returncode = proc.proc.returncode
30        didtimeout = proc.didTimeout
31        detected = ProcessHandler.pid_exists(proc.pid)
32        output = ""
33        # ProcessHandler has output when store_output is set to True in the constructor
34        # (this is the default)
35        if getattr(proc, "output"):
36            output = proc.output
37
38        if "returncode" in expectedfail:
39            self.assertTrue(
40                returncode, "Detected an unexpected return code of: %s" % returncode
41            )
42        elif isalive:
43            self.assertEqual(
44                returncode, None, "Detected not None return code of: %s" % returncode
45            )
46        else:
47            self.assertNotEqual(
48                returncode, None, "Detected unexpected None return code of"
49            )
50
51        if "didtimeout" in expectedfail:
52            self.assertTrue(didtimeout, "Detected that process didn't time out")
53        else:
54            self.assertTrue(not didtimeout, "Detected that process timed out")
55
56        if isalive:
57            self.assertTrue(
58                detected,
59                "Detected process is not running, " "process output: %s" % output,
60            )
61        else:
62            self.assertTrue(
63                not detected,
64                "Detected process is still running, " "process output: %s" % output,
65            )
66