~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to testing/mozbase/mozprocess/tests/mozprocess2.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
# This Source Code Form is subject to the terms of the Mozilla Public
4
 
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
5
 
# You can obtain one at http://mozilla.org/MPL/2.0/.
6
 
 
7
 
import os
8
 
import subprocess
9
 
import sys
10
 
import unittest
11
 
from time import sleep
12
 
 
13
 
from mozprocess import processhandler
14
 
 
15
 
here = os.path.dirname(os.path.abspath(__file__))
16
 
 
17
 
# This tests specifically the case reported in bug 671316
18
 
# TODO: Because of the way mutt works we can't just load a utils.py in here.
19
 
#       so, for all process handler tests, copy these two
20
 
#       utility functions to to the top of your source.
21
 
 
22
 
def make_proclaunch(aDir):
23
 
    """
24
 
        Makes the proclaunch executable.
25
 
        Params:
26
 
            aDir - the directory in which to issue the make commands
27
 
        Returns:
28
 
            the path to the proclaunch executable that is generated
29
 
    """
30
 
    # Ideally make should take care of this, but since it doesn't - on windows,
31
 
    # anyway, let's just call out both targets explicitly.
32
 
    p = subprocess.call(["make", "-C", "iniparser"], cwd=aDir)
33
 
    p = subprocess.call(["make"], cwd=aDir)
34
 
    if sys.platform == "win32":
35
 
        exepath = os.path.join(aDir, "proclaunch.exe")
36
 
    else:
37
 
        exepath = os.path.join(aDir, "proclaunch")
38
 
    return exepath
39
 
 
40
 
def check_for_process(processName):
41
 
    """
42
 
        Use to determine if process is still running.
43
 
 
44
 
        Returns:
45
 
        detected -- True if process is detected to exist, False otherwise
46
 
        output -- if process exists, stdout of the process, '' otherwise
47
 
    """
48
 
    output = ''
49
 
    if sys.platform == "win32":
50
 
        # On windows we use tasklist
51
 
        p1 = subprocess.Popen(["tasklist"], stdout=subprocess.PIPE)
52
 
        output = p1.communicate()[0]
53
 
        detected = False
54
 
        for line in output.splitlines():
55
 
            if processName in line:
56
 
                detected = True
57
 
                break
58
 
    else:
59
 
        p1 = subprocess.Popen(["ps", "-ef"], stdout=subprocess.PIPE)
60
 
        p2 = subprocess.Popen(["grep", processName], stdin=p1.stdout, stdout=subprocess.PIPE)
61
 
        p1.stdout.close()
62
 
        output = p2.communicate()[0]
63
 
        detected = False
64
 
        for line in output.splitlines():
65
 
            if "grep %s" % processName in line:
66
 
                continue
67
 
            elif processName in line and not 'defunct' in line:
68
 
                detected = True
69
 
                break
70
 
 
71
 
    return detected, output
72
 
 
73
 
class ProcTest2(unittest.TestCase):
74
 
 
75
 
    def __init__(self, *args, **kwargs):
76
 
 
77
 
        # Ideally, I'd use setUpClass but that only exists in 2.7.
78
 
        # So, we'll do this make step now.
79
 
        self.proclaunch = make_proclaunch(here)
80
 
        unittest.TestCase.__init__(self, *args, **kwargs)
81
 
 
82
 
    def test_process_waitnotimeout(self):
83
 
        """ Process is started, runs to completion before our wait times out
84
 
        """
85
 
        p = processhandler.ProcessHandler([self.proclaunch,
86
 
                                          "process_waittimeout_10s.ini"],
87
 
                                          cwd=here)
88
 
        p.run()
89
 
        p.processOutput(timeout=30)
90
 
        p.waitForFinish()
91
 
 
92
 
        detected, output = check_for_process(self.proclaunch)
93
 
        self.determine_status(detected,
94
 
                              output,
95
 
                              p.proc.returncode,
96
 
                              p.didTimeout)
97
 
 
98
 
    def test_process_wait(self):
99
 
        """ Process is started runs to completion while we wait indefinitely
100
 
        """
101
 
 
102
 
        p = processhandler.ProcessHandler([self.proclaunch,
103
 
                                          "process_waittimeout_10s.ini"],
104
 
                                          cwd=here)
105
 
        p.run()
106
 
        p.waitForFinish()
107
 
 
108
 
        detected, output = check_for_process(self.proclaunch)
109
 
        self.determine_status(detected,
110
 
                              output,
111
 
                              p.proc.returncode,
112
 
                              p.didTimeout)
113
 
 
114
 
    def test_process_waittimeout(self):
115
 
        """
116
 
        Process is started, then waitForFinish is called and times out.
117
 
        Process is still running and didn't timeout
118
 
        """
119
 
        p = processhandler.ProcessHandler([self.proclaunch,
120
 
                                          "process_waittimeout_10s.ini"],
121
 
                                          cwd=here)
122
 
 
123
 
        p.run()
124
 
        p.processOutput()
125
 
        p.waitForFinish(timeout=5)
126
 
 
127
 
        detected, output = check_for_process(self.proclaunch)
128
 
        self.determine_status(detected,
129
 
                              output,
130
 
                              p.proc.returncode,
131
 
                              p.didTimeout,
132
 
                              True,
133
 
                              [])
134
 
 
135
 
    def determine_status(self,
136
 
                         detected=False,
137
 
                         output = '',
138
 
                         returncode = 0,
139
 
                         didtimeout = False,
140
 
                         isalive=False,
141
 
                         expectedfail=[]):
142
 
        """
143
 
        Use to determine if the situation has failed.
144
 
        Parameters:
145
 
            detected -- value from check_for_process to determine if the process is detected
146
 
            output -- string of data from detected process, can be ''
147
 
            returncode -- return code from process, defaults to 0
148
 
            didtimeout -- True if process timed out, defaults to False
149
 
            isalive -- Use True to indicate we pass if the process exists; however, by default
150
 
                       the test will pass if the process does not exist (isalive == False)
151
 
            expectedfail -- Defaults to [], used to indicate a list of fields that are expected to fail
152
 
        """
153
 
        if 'returncode' in expectedfail:
154
 
            self.assertTrue(returncode, "Detected an unexpected return code of: %s" % returncode)
155
 
        elif not isalive:
156
 
            self.assertTrue(returncode == 0, "Detected non-zero return code of: %d" % returncode)
157
 
 
158
 
        if 'didtimeout' in expectedfail:
159
 
            self.assertTrue(didtimeout, "Detected that process didn't time out")
160
 
        else:
161
 
            self.assertTrue(not didtimeout, "Detected that process timed out")
162
 
 
163
 
        if isalive:
164
 
            self.assertTrue(detected, "Detected process is not running, process output: %s" % output)
165
 
        else:
166
 
            self.assertTrue(not detected, "Detected process is still running, process output: %s" % output)
167
 
 
168
 
if __name__ == '__main__':
169
 
    unittest.main()