aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_steps_master.py
blob: 5b5ce6b8bbc611929eee180b3a78ae964f70b244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import os
import sys
from twisted.python import failure, runtime
from twisted.internet import error, reactor
from twisted.trial import unittest
from buildbot.test.util import steps
from buildbot.status.results import SUCCESS, FAILURE, EXCEPTION
from buildbot.steps import master
from buildbot.process.properties import WithProperties
from buildbot.process.properties import Interpolate
import pprint

class TestMasterShellCommand(steps.BuildStepMixin, unittest.TestCase):

    def setUp(self):
        if runtime.platformType == 'win32':
            self.comspec = os.environ.get('COMPSPEC')
            os.environ['COMSPEC'] = r'C:\WINDOWS\system32\cmd.exe'
        return self.setUpBuildStep()

    def tearDown(self):
        if runtime.platformType == 'win32':
            if self.comspec:
                os.environ['COMSPEC'] = self.comspec
            else:
                del os.environ['COMSPEC']
        return self.tearDownBuildStep()

    def patchSpawnProcess(self, exp_cmd, exp_argv, exp_path, exp_usePTY,
                          exp_env, outputs):
        def spawnProcess(pp, cmd, argv, path, usePTY, env):
            self.assertEqual([cmd, argv, path, usePTY, env],
                        [exp_cmd, exp_argv, exp_path, exp_usePTY, exp_env])
            for output in outputs:
                if output[0] == 'out':
                    pp.outReceived(output[1])
                elif output[0] == 'err':
                    pp.errReceived(output[1])
                elif output[0] == 'rc':
                    if output[1] != 0:
                        so = error.ProcessTerminated(exitCode=output[1])
                    else:
                        so = error.ProcessDone(None)
                    pp.processEnded(failure.Failure(so))
        self.patch(reactor, 'spawnProcess', spawnProcess)

    def test_real_cmd(self):
        cmd = [ sys.executable, '-c', 'print "hello"' ]
        self.setupStep(
                master.MasterShellCommand(command=cmd))
        if runtime.platformType == 'win32':
            self.expectLogfile('stdio', "hello\r\n")
        else:
            self.expectLogfile('stdio', "hello\n")
        self.expectOutcome(result=SUCCESS, status_text=["Ran"])
        return self.runStep()

    def test_real_cmd_interrupted(self):
        cmd = [ sys.executable, '-c', 'while True: pass' ]
        self.setupStep(
                master.MasterShellCommand(command=cmd))
        self.expectLogfile('stdio', "")
        if runtime.platformType == 'win32':
            # windows doesn't have signals, so we don't get 'killed'
            self.expectOutcome(result=EXCEPTION,
                    status_text=["failed (1)", "interrupted"])
        else:
            self.expectOutcome(result=EXCEPTION,
                    status_text=["killed (9)", "interrupted"])
        d = self.runStep()
        self.step.interrupt("KILL")
        return d

    def test_real_cmd_fails(self):
        cmd = [ sys.executable, '-c', 'import sys; sys.exit(1)' ]
        self.setupStep(
                master.MasterShellCommand(command=cmd))
        self.expectLogfile('stdio', "")
        self.expectOutcome(result=FAILURE, status_text=["failed (1)"])
        return self.runStep()

    def test_constr_args(self):
        self.setupStep(
                master.MasterShellCommand(description='x', descriptionDone='y',
                                env={'a':'b'}, path=['/usr/bin'], usePTY=True,
                                command='true'))

        self.assertEqual(self.step.describe(), ['x'])

        if runtime.platformType == 'win32':
            exp_argv = [ r'C:\WINDOWS\system32\cmd.exe', '/c', 'true' ]
        else:
            exp_argv = [ '/bin/sh', '-c', 'true' ]
        self.patchSpawnProcess(
                exp_cmd=exp_argv[0], exp_argv=exp_argv,
                exp_path=['/usr/bin'], exp_usePTY=True, exp_env={'a':'b'},
                outputs=[
                    ('out', 'hello!\n'),
                    ('err', 'world\n'),
                    ('rc', 0),
                ])
        self.expectOutcome(result=SUCCESS, status_text=['y'])
        return self.runStep()

    def test_env_subst(self):
        cmd = [ sys.executable, '-c', 'import os; print os.environ["HELLO"]' ]
        os.environ['WORLD'] = 'hello'
        self.setupStep(
                master.MasterShellCommand(command=cmd,
                                env={'HELLO': '${WORLD}'}))
        if runtime.platformType == 'win32':
            self.expectLogfile('stdio', "hello\r\n")
        else:
            self.expectLogfile('stdio', "hello\n")
        self.expectOutcome(result=SUCCESS, status_text=["Ran"])
        def _restore_env(res):
            del os.environ['WORLD']
            return res
        d = self.runStep()
        d.addBoth(_restore_env)
        return d

    def test_env_list_subst(self):
        cmd = [ sys.executable, '-c', 'import os; print os.environ["HELLO"]' ]
        os.environ['WORLD'] = 'hello'
        os.environ['LIST'] = 'world'
        self.setupStep(
                master.MasterShellCommand(command=cmd,
                                env={'HELLO': ['${WORLD}', '${LIST}']}))
        if runtime.platformType == 'win32':
            self.expectLogfile('stdio', "hello;world\r\n")
        else:
            self.expectLogfile('stdio', "hello:world\n")
        self.expectOutcome(result=SUCCESS, status_text=["Ran"])
        def _restore_env(res):
            del os.environ['WORLD']
            del os.environ['LIST']
            return res
        d = self.runStep()
        d.addBoth(_restore_env)
        return d

    def test_prop_rendering(self):
        cmd = [ sys.executable, '-c', WithProperties(
                    'import os; print "%s"; print os.environ[\"BUILD\"]',
                    'project') ]
        self.setupStep(
                master.MasterShellCommand(command=cmd,
                        env={'BUILD': WithProperties('%s', "project")}))
        self.properties.setProperty("project", "BUILDBOT-TEST", "TEST")
        if runtime.platformType == 'win32':
            self.expectLogfile('stdio', "BUILDBOT-TEST\r\nBUILDBOT-TEST\r\n")
        else:
            self.expectLogfile('stdio', "BUILDBOT-TEST\nBUILDBOT-TEST\n")
        self.expectOutcome(result=SUCCESS, status_text=["Ran"])
        return self.runStep()

    def test_constr_args_descriptionSuffix(self):
        self.setupStep(
                master.MasterShellCommand(description='x', descriptionDone='y',
                                          descriptionSuffix='z',
                                env={'a':'b'}, path=['/usr/bin'], usePTY=True,
                                command='true'))

        # call twice to make sure the suffix doesn't get double added
        self.assertEqual(self.step.describe(), ['x', 'z'])
        self.assertEqual(self.step.describe(), ['x', 'z'])

        if runtime.platformType == 'win32':
            exp_argv = [ r'C:\WINDOWS\system32\cmd.exe', '/c', 'true' ]
        else:
            exp_argv = [ '/bin/sh', '-c', 'true' ]
        self.patchSpawnProcess(
                exp_cmd=exp_argv[0], exp_argv=exp_argv,
                exp_path=['/usr/bin'], exp_usePTY=True, exp_env={'a':'b'},
                outputs=[
                    ('out', 'hello!\n'),
                    ('err', 'world\n'),
                    ('rc', 0),
                ])
        self.expectOutcome(result=SUCCESS, status_text=['y', 'z'])
        return self.runStep()

class TestSetProperty(steps.BuildStepMixin, unittest.TestCase):

    def setUp(self):
        return self.setUpBuildStep()

    def tearDown(self):
        return self.tearDownBuildStep()

    def test_simple(self):
        self.setupStep(master.SetProperty(property="testProperty", value=Interpolate("sch=%(prop:scheduler)s, slave=%(prop:slavename)s")))
        self.properties.setProperty('scheduler', 'force', source='SetProperty', runtime=True)
        self.properties.setProperty('slavename', 'testSlave', source='SetPropery', runtime=True)
        self.expectOutcome(result=SUCCESS, status_text=["SetProperty"])
        self.expectProperty('testProperty', 'sch=force, slave=testSlave', source='SetProperty')
        return self.runStep()

class TestLogRenderable(steps.BuildStepMixin, unittest.TestCase):

    def setUp(self):
        return self.setUpBuildStep()

    def tearDown(self):
        return self.tearDownBuildStep()

    def test_simple(self):
        self.setupStep(master.LogRenderable(content=Interpolate('sch=%(prop:scheduler)s, slave=%(prop:slavename)s')))
        self.properties.setProperty('scheduler', 'force', source='TestSetProperty', runtime=True)
        self.properties.setProperty('slavename', 'testSlave', source='TestSetProperty', runtime=True)
        self.expectOutcome(result=SUCCESS, status_text=['LogRenderable'])
        self.expectLogfile('Output', pprint.pformat('sch=force, slave=testSlave'))
        return self.runStep()