testing/mozbase/mozprocess/tests/proctest.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial