|
1 import mozinfo |
|
2 import os |
|
3 import subprocess |
|
4 import sys |
|
5 import unittest |
|
6 |
|
7 here = os.path.dirname(os.path.abspath(__file__)) |
|
8 |
|
9 def check_for_process(processName): |
|
10 """ |
|
11 Use to determine if process of the given name is still running. |
|
12 |
|
13 Returns: |
|
14 detected -- True if process is detected to exist, False otherwise |
|
15 output -- if process exists, stdout of the process, '' otherwise |
|
16 """ |
|
17 # TODO: replace with |
|
18 # https://github.com/mozilla/mozbase/blob/master/mozprocess/mozprocess/pid.py |
|
19 # which should be augmented from talos |
|
20 # see https://bugzilla.mozilla.org/show_bug.cgi?id=705864 |
|
21 output = '' |
|
22 if mozinfo.isWin: |
|
23 # On windows we use tasklist |
|
24 p1 = subprocess.Popen(["tasklist"], stdout=subprocess.PIPE) |
|
25 output = p1.communicate()[0] |
|
26 detected = False |
|
27 for line in output.splitlines(): |
|
28 if processName in line: |
|
29 detected = True |
|
30 break |
|
31 else: |
|
32 p1 = subprocess.Popen(["ps", "-ef"], stdout=subprocess.PIPE) |
|
33 p2 = subprocess.Popen(["grep", processName], stdin=p1.stdout, stdout=subprocess.PIPE) |
|
34 p1.stdout.close() |
|
35 output = p2.communicate()[0] |
|
36 detected = False |
|
37 for line in output.splitlines(): |
|
38 if "grep %s" % processName in line: |
|
39 continue |
|
40 elif processName in line and not 'defunct' in line: |
|
41 detected = True |
|
42 break |
|
43 |
|
44 return detected, output |
|
45 |
|
46 class ProcTest(unittest.TestCase): |
|
47 |
|
48 @classmethod |
|
49 def setUpClass(cls): |
|
50 cls.proclaunch = os.path.join(here, "proclaunch.py") |
|
51 cls.python = sys.executable |
|
52 |
|
53 def determine_status(self, |
|
54 detected=False, |
|
55 output='', |
|
56 returncode=0, |
|
57 didtimeout=False, |
|
58 isalive=False, |
|
59 expectedfail=()): |
|
60 """ |
|
61 Use to determine if the situation has failed. |
|
62 Parameters: |
|
63 detected -- value from check_for_process to determine if the process is detected |
|
64 output -- string of data from detected process, can be '' |
|
65 returncode -- return code from process, defaults to 0 |
|
66 didtimeout -- True if process timed out, defaults to False |
|
67 isalive -- Use True to indicate we pass if the process exists; however, by default |
|
68 the test will pass if the process does not exist (isalive == False) |
|
69 expectedfail -- Defaults to [], used to indicate a list of fields that are expected to fail |
|
70 """ |
|
71 if 'returncode' in expectedfail: |
|
72 self.assertTrue(returncode, "Detected an unexpected return code of: %s" % returncode) |
|
73 elif isalive: |
|
74 self.assertEqual(returncode, None, "Detected not None return code of: %s" % returncode) |
|
75 else: |
|
76 self.assertNotEqual(returncode, None, "Detected unexpected None return code of") |
|
77 |
|
78 if 'didtimeout' in expectedfail: |
|
79 self.assertTrue(didtimeout, "Detected that process didn't time out") |
|
80 else: |
|
81 self.assertTrue(not didtimeout, "Detected that process timed out") |
|
82 |
|
83 if isalive: |
|
84 self.assertTrue(detected, "Detected process is not running, process output: %s" % output) |
|
85 else: |
|
86 self.assertTrue(not detected, "Detected process is still running, process output: %s" % output) |