michael@0: import mozinfo michael@0: import os michael@0: import subprocess michael@0: import sys michael@0: import unittest michael@0: michael@0: here = os.path.dirname(os.path.abspath(__file__)) michael@0: michael@0: def check_for_process(processName): michael@0: """ michael@0: Use to determine if process of the given name is still running. michael@0: michael@0: Returns: michael@0: detected -- True if process is detected to exist, False otherwise michael@0: output -- if process exists, stdout of the process, '' otherwise michael@0: """ michael@0: # TODO: replace with michael@0: # https://github.com/mozilla/mozbase/blob/master/mozprocess/mozprocess/pid.py michael@0: # which should be augmented from talos michael@0: # see https://bugzilla.mozilla.org/show_bug.cgi?id=705864 michael@0: output = '' michael@0: if mozinfo.isWin: michael@0: # On windows we use tasklist michael@0: p1 = subprocess.Popen(["tasklist"], stdout=subprocess.PIPE) michael@0: output = p1.communicate()[0] michael@0: detected = False michael@0: for line in output.splitlines(): michael@0: if processName in line: michael@0: detected = True michael@0: break michael@0: else: michael@0: p1 = subprocess.Popen(["ps", "-ef"], stdout=subprocess.PIPE) michael@0: p2 = subprocess.Popen(["grep", processName], stdin=p1.stdout, stdout=subprocess.PIPE) michael@0: p1.stdout.close() michael@0: output = p2.communicate()[0] michael@0: detected = False michael@0: for line in output.splitlines(): michael@0: if "grep %s" % processName in line: michael@0: continue michael@0: elif processName in line and not 'defunct' in line: michael@0: detected = True michael@0: break michael@0: michael@0: return detected, output michael@0: michael@0: class ProcTest(unittest.TestCase): michael@0: michael@0: @classmethod michael@0: def setUpClass(cls): michael@0: cls.proclaunch = os.path.join(here, "proclaunch.py") michael@0: cls.python = sys.executable michael@0: michael@0: def determine_status(self, michael@0: detected=False, michael@0: output='', michael@0: returncode=0, michael@0: didtimeout=False, michael@0: isalive=False, michael@0: expectedfail=()): michael@0: """ michael@0: Use to determine if the situation has failed. michael@0: Parameters: michael@0: detected -- value from check_for_process to determine if the process is detected michael@0: output -- string of data from detected process, can be '' michael@0: returncode -- return code from process, defaults to 0 michael@0: didtimeout -- True if process timed out, defaults to False michael@0: isalive -- Use True to indicate we pass if the process exists; however, by default michael@0: the test will pass if the process does not exist (isalive == False) michael@0: expectedfail -- Defaults to [], used to indicate a list of fields that are expected to fail michael@0: """ michael@0: if 'returncode' in expectedfail: michael@0: self.assertTrue(returncode, "Detected an unexpected return code of: %s" % returncode) michael@0: elif isalive: michael@0: self.assertEqual(returncode, None, "Detected not None return code of: %s" % returncode) michael@0: else: michael@0: self.assertNotEqual(returncode, None, "Detected unexpected None return code of") michael@0: michael@0: if 'didtimeout' in expectedfail: michael@0: self.assertTrue(didtimeout, "Detected that process didn't time out") michael@0: else: michael@0: self.assertTrue(not didtimeout, "Detected that process timed out") michael@0: michael@0: if isalive: michael@0: self.assertTrue(detected, "Detected process is not running, process output: %s" % output) michael@0: else: michael@0: self.assertTrue(not detected, "Detected process is still running, process output: %s" % output)