testing/mozbase/mozprocess/tests/proctest.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/mozprocess/tests/proctest.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +import mozinfo
     1.5 +import os
     1.6 +import subprocess
     1.7 +import sys
     1.8 +import unittest
     1.9 +
    1.10 +here = os.path.dirname(os.path.abspath(__file__))
    1.11 +
    1.12 +def check_for_process(processName):
    1.13 +    """
    1.14 +        Use to determine if process of the given name is still running.
    1.15 +
    1.16 +        Returns:
    1.17 +        detected -- True if process is detected to exist, False otherwise
    1.18 +        output -- if process exists, stdout of the process, '' otherwise
    1.19 +    """
    1.20 +    # TODO: replace with
    1.21 +    # https://github.com/mozilla/mozbase/blob/master/mozprocess/mozprocess/pid.py
    1.22 +    # which should be augmented from talos
    1.23 +    # see https://bugzilla.mozilla.org/show_bug.cgi?id=705864
    1.24 +    output = ''
    1.25 +    if mozinfo.isWin:
    1.26 +        # On windows we use tasklist
    1.27 +        p1 = subprocess.Popen(["tasklist"], stdout=subprocess.PIPE)
    1.28 +        output = p1.communicate()[0]
    1.29 +        detected = False
    1.30 +        for line in output.splitlines():
    1.31 +            if processName in line:
    1.32 +                detected = True
    1.33 +                break
    1.34 +    else:
    1.35 +        p1 = subprocess.Popen(["ps", "-ef"], stdout=subprocess.PIPE)
    1.36 +        p2 = subprocess.Popen(["grep", processName], stdin=p1.stdout, stdout=subprocess.PIPE)
    1.37 +        p1.stdout.close()
    1.38 +        output = p2.communicate()[0]
    1.39 +        detected = False
    1.40 +        for line in output.splitlines():
    1.41 +            if "grep %s" % processName in line:
    1.42 +                continue
    1.43 +            elif processName in line and not 'defunct' in line:
    1.44 +                detected = True
    1.45 +                break
    1.46 +
    1.47 +    return detected, output
    1.48 +
    1.49 +class ProcTest(unittest.TestCase):
    1.50 +
    1.51 +    @classmethod
    1.52 +    def setUpClass(cls):
    1.53 +        cls.proclaunch = os.path.join(here, "proclaunch.py")
    1.54 +        cls.python = sys.executable
    1.55 +
    1.56 +    def determine_status(self,
    1.57 +                         detected=False,
    1.58 +                         output='',
    1.59 +                         returncode=0,
    1.60 +                         didtimeout=False,
    1.61 +                         isalive=False,
    1.62 +                         expectedfail=()):
    1.63 +        """
    1.64 +        Use to determine if the situation has failed.
    1.65 +        Parameters:
    1.66 +            detected -- value from check_for_process to determine if the process is detected
    1.67 +            output -- string of data from detected process, can be ''
    1.68 +            returncode -- return code from process, defaults to 0
    1.69 +            didtimeout -- True if process timed out, defaults to False
    1.70 +            isalive -- Use True to indicate we pass if the process exists; however, by default
    1.71 +                       the test will pass if the process does not exist (isalive == False)
    1.72 +            expectedfail -- Defaults to [], used to indicate a list of fields that are expected to fail
    1.73 +        """
    1.74 +        if 'returncode' in expectedfail:
    1.75 +            self.assertTrue(returncode, "Detected an unexpected return code of: %s" % returncode)
    1.76 +        elif isalive:
    1.77 +            self.assertEqual(returncode, None, "Detected not None return code of: %s" % returncode)
    1.78 +        else:
    1.79 +            self.assertNotEqual(returncode, None, "Detected unexpected None return code of")
    1.80 +
    1.81 +        if 'didtimeout' in expectedfail:
    1.82 +            self.assertTrue(didtimeout, "Detected that process didn't time out")
    1.83 +        else:
    1.84 +            self.assertTrue(not didtimeout, "Detected that process timed out")
    1.85 +
    1.86 +        if isalive:
    1.87 +            self.assertTrue(detected, "Detected process is not running, process output: %s" % output)
    1.88 +        else:
    1.89 +            self.assertTrue(not detected, "Detected process is still running, process output: %s" % output)

mercurial