aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_steps_slave.py
blob: 8052dca779adbb14fdcb6956e58c1e5fe7ec54b4 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# 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 stat
from twisted.trial import unittest
from twisted.internet import defer
from buildbot.steps import slave
from buildbot.status.results import SUCCESS, FAILURE, EXCEPTION
from buildbot.process import properties, buildstep
from buildbot.test.fake.remotecommand import Expect
from buildbot.test.util import steps, compat
from buildbot.interfaces import BuildSlaveTooOldError

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

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

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

    def test_simple(self):
        self.setupStep(slave.SetPropertiesFromEnv(
                variables=["one", "two", "three", "five", "six"],
                source="me"))
        self.buildslave.slave_environ = { "one": "1", "two": None, "six": "6", "FIVE" : "555" }
        self.properties.setProperty("four", 4, "them")
        self.properties.setProperty("five", 5, "them")
        self.properties.setProperty("six", 99, "them")
        self.expectOutcome(result=SUCCESS,
                status_text=["Set"])
        self.expectProperty('one', "1", source='me')
        self.expectNoProperty('two')
        self.expectNoProperty('three')
        self.expectProperty('four', 4, source='them')
        self.expectProperty('five', 5, source='them')
        self.expectProperty('six', '6', source='me')
        self.expectLogfile("properties",
                "one = '1'\nsix = '6'")
        return self.runStep()

    def test_case_folding(self):
        self.setupStep(slave.SetPropertiesFromEnv(
                variables=["eNv"], source="me"))
        self.buildslave.slave_environ = { "ENV": 'EE' }
        self.buildslave.slave_system = 'win32'
        self.expectOutcome(result=SUCCESS,
                status_text=["Set"])
        self.expectProperty('eNv', 'EE', source='me')
        self.expectLogfile("properties",
                "eNv = 'EE'")
        return self.runStep()


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

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

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

    def test_found(self):
        self.setupStep(slave.FileExists(file="x"))
        self.expectCommands(
            Expect('stat', { 'file' : 'x' })
            + Expect.update('stat', [stat.S_IFREG, 99, 99])
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["File found."])
        return self.runStep()

    def test_not_found(self):
        self.setupStep(slave.FileExists(file="x"))
        self.expectCommands(
            Expect('stat', { 'file' : 'x' })
            + Expect.update('stat', [0, 99, 99])
            + 0
        )
        self.expectOutcome(result=FAILURE,
                status_text=["Not a file."])
        return self.runStep()

    def test_failure(self):
        self.setupStep(slave.FileExists(file="x"))
        self.expectCommands(
            Expect('stat', { 'file' : 'x' })
            + 1
        )
        self.expectOutcome(result=FAILURE,
                status_text=["File not found."])
        return self.runStep()

    def test_render(self):
        self.setupStep(slave.FileExists(file=properties.Property("x")))
        self.properties.setProperty('x', 'XXX', 'here')
        self.expectCommands(
            Expect('stat', { 'file' : 'XXX' })
            + 1
        )
        self.expectOutcome(result=FAILURE,
                status_text=["File not found."])
        return self.runStep()

    @compat.usesFlushLoggedErrors
    def test_old_version(self):
        self.setupStep(slave.FileExists(file="x"),
                slave_version=dict())
        self.expectOutcome(result=EXCEPTION,
                status_text=["FileExists", "exception"])
        d = self.runStep()
        def check(_):
            self.assertEqual(
                    len(self.flushLoggedErrors(BuildSlaveTooOldError)), 1)
        d.addCallback(check)
        return d


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

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

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

    def test_success(self):
        self.setupStep(slave.CopyDirectory(src="s", dest="d"))
        self.expectCommands(
            Expect('cpdir', { 'fromdir' : 's', 'todir' : 'd' })
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["Copied", "s", "to", "d"])
        return self.runStep()

    def test_timeout(self):
        self.setupStep(slave.CopyDirectory(src="s", dest="d", timeout=300))
        self.expectCommands(
            Expect('cpdir', { 'fromdir' : 's', 'todir' : 'd', 'timeout': 300 })
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["Copied", "s", "to", "d"])
        return self.runStep()

    def test_maxTime(self):
        self.setupStep(slave.CopyDirectory(src="s", dest="d", maxTime=10))
        self.expectCommands(
            Expect('cpdir', { 'fromdir' : 's', 'todir' : 'd', 'maxTime': 10 })
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["Copied", "s", "to", "d"])
        return self.runStep()

    def test_failure(self):
        self.setupStep(slave.CopyDirectory(src="s", dest="d"))
        self.expectCommands(
            Expect('cpdir', { 'fromdir' : 's', 'todir' : 'd' })
            + 1
        )
        self.expectOutcome(result=FAILURE,
                status_text=["Copying", "s", "to", "d", "failed."])
        return self.runStep()

    def test_render(self):
        self.setupStep(slave.CopyDirectory(src=properties.Property("x"), dest=properties.Property("y")))
        self.properties.setProperty('x', 'XXX', 'here')
        self.properties.setProperty('y', 'YYY', 'here')
        self.expectCommands(
            Expect('cpdir', { 'fromdir' : 'XXX', 'todir' : 'YYY' })
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["Copied", "XXX", "to", "YYY"])
        return self.runStep()

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

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

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

    def test_success(self):
        self.setupStep(slave.RemoveDirectory(dir="d"))
        self.expectCommands(
            Expect('rmdir', { 'dir' : 'd' })
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["Deleted"])
        return self.runStep()

    def test_failure(self):
        self.setupStep(slave.RemoveDirectory(dir="d"))
        self.expectCommands(
            Expect('rmdir', { 'dir' : 'd' })
            + 1
        )
        self.expectOutcome(result=FAILURE,
                status_text=["Delete failed."])
        return self.runStep()

    def test_render(self):
        self.setupStep(slave.RemoveDirectory(dir=properties.Property("x")))
        self.properties.setProperty('x', 'XXX', 'here')
        self.expectCommands(
            Expect('rmdir', { 'dir' : 'XXX' })
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["Deleted"])
        return self.runStep()

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

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

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

    def test_success(self):
        self.setupStep(slave.MakeDirectory(dir="d"))
        self.expectCommands(
            Expect('mkdir', { 'dir' : 'd' })
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["Created"])
        return self.runStep()

    def test_failure(self):
        self.setupStep(slave.MakeDirectory(dir="d"))
        self.expectCommands(
            Expect('mkdir', { 'dir' : 'd' })
            + 1
        )
        self.expectOutcome(result=FAILURE,
                status_text=["Create failed."])
        return self.runStep()

    def test_render(self):
        self.setupStep(slave.MakeDirectory(dir=properties.Property("x")))
        self.properties.setProperty('x', 'XXX', 'here')
        self.expectCommands(
            Expect('mkdir', { 'dir' : 'XXX' })
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["Created"])
        return self.runStep()

class CompositeUser(buildstep.LoggingBuildStep, slave.CompositeStepMixin):
    def __init__(self, payload):
        self.payload = payload
        self.logEnviron=False
        buildstep.LoggingBuildStep.__init__(self)
    def start(self):
        self.addLogForRemoteCommands('stdio')
        d = self.payload(self)
        d.addCallback(self.commandComplete)
        d.addErrback(self.failed)
    def commandComplete(self,res):
        self.finished(FAILURE if res else SUCCESS)

class TestCompositeStepMixin(steps.BuildStepMixin, unittest.TestCase):
    def setUp(self):
        return self.setUpBuildStep()

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

    def test_runRemoteCommand(self):
        cmd_args = ('foo', {'bar': False})
        def testFunc(x):
            x.runRemoteCommand(*cmd_args)
        self.setupStep(CompositeUser(testFunc))
        self.expectCommands(Expect(*cmd_args)+0)
        self.expectOutcome(result=SUCCESS,
                           status_text=["generic"])

    def test_runRemoteCommandFail(self):
        cmd_args = ('foo', {'bar': False})
        @defer.inlineCallbacks
        def testFunc(x):
            yield x.runRemoteCommand(*cmd_args)
        self.setupStep(CompositeUser(testFunc))
        self.expectCommands(Expect(*cmd_args)+1)
        self.expectOutcome(result=FAILURE,
                           status_text=["generic"])
        return self.runStep()

    def test_runRemoteCommandFailNoAbandon(self):
        cmd_args = ('foo', {'bar': False})
        @defer.inlineCallbacks
        def testFunc(x):
            res = yield x.runRemoteCommand(*cmd_args,
                                **dict(abandonOnFailure=False))
            x.step_status.setText([str(res)])
        self.setupStep(CompositeUser(testFunc))
        self.expectCommands(Expect(*cmd_args)+1)
        self.expectOutcome(result=SUCCESS,
                           status_text=["True"])
        return self.runStep()

    def test_mkdir(self):
        self.setupStep(CompositeUser(lambda x:x.runMkdir("d")))
        self.expectCommands(
            Expect('mkdir', { 'dir' : 'd' , 'logEnviron': False})
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["generic"])
        return self.runStep()

    def test_rmdir(self):
        self.setupStep(CompositeUser(lambda x:x.runRmdir("d")))
        self.expectCommands(
            Expect('rmdir', { 'dir' : 'd' , 'logEnviron': False})
            + 0
        )
        self.expectOutcome(result=SUCCESS,
                status_text=["generic"])
        return self.runStep()

    def test_mkdir_fail(self):
        self.setupStep(CompositeUser(lambda x:x.runMkdir("d")))
        self.expectCommands(
            Expect('mkdir', { 'dir' : 'd' , 'logEnviron': False})
            + 1
        )
        self.expectOutcome(result=FAILURE,
                status_text=["generic"])
        return self.runStep()

    def test_abandonOnFailure(self):
        @defer.inlineCallbacks
        def testFunc(x):
            yield x.runMkdir("d")
            yield x.runMkdir("d")
        self.setupStep(CompositeUser(testFunc))
        self.expectCommands(
            Expect('mkdir', { 'dir' : 'd' , 'logEnviron': False})
            + 1
            )
        self.expectOutcome(result=FAILURE,
                           status_text=["generic"])
        return self.runStep()

    def test_notAbandonOnFailure(self):
        @defer.inlineCallbacks
        def testFunc(x):
            yield x.runMkdir("d", abandonOnFailure=False)
            yield x.runMkdir("d", abandonOnFailure=False)
        self.setupStep(CompositeUser(testFunc))
        self.expectCommands(
            Expect('mkdir', { 'dir' : 'd' , 'logEnviron': False})
            + 1,
            Expect('mkdir', { 'dir' : 'd' , 'logEnviron': False})
            + 1
            )
        self.expectOutcome(result=SUCCESS,
                           status_text=["generic"])
        return self.runStep()