aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/fake/remotecommand.py
blob: 9c583c9278f0ec5279017492921628fbd7f5c0f2 (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
# 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

from twisted.internet import defer
from twisted.python import failure
from buildbot.status.logfile import STDOUT, STDERR, HEADER
from cStringIO import StringIO
from buildbot.status.results import SUCCESS, FAILURE


class FakeRemoteCommand(object):

    # callers should set this to the running TestCase instance
    testcase = None

    active = False

    def __init__(self, remote_command, args,
            ignore_updates=False, collectStdout=False, collectStderr=False, decodeRC={0:SUCCESS}):
        # copy the args and set a few defaults
        self.remote_command = remote_command
        self.args = args.copy()
        self.logs = {}
        self.delayedLogs = {}
        self.rc = -999
        self.collectStdout = collectStdout
        self.collectStderr = collectStderr
        self.updates = {}
        self.decodeRC = decodeRC
        if collectStdout:
            self.stdout = ''
        if collectStderr:
            self.stderr = ''

    def run(self, step, remote):
        # delegate back to the test case
        return self.testcase._remotecommand_run(self, step, remote)

    def useLog(self, log, closeWhenFinished=False, logfileName=None):
        if not logfileName:
            logfileName = log.getName()
        self.logs[logfileName] = log

    def useLogDelayed(self, logfileName, activateCallBack, closeWhenFinished=False):
        self.delayedLogs[logfileName] = (activateCallBack, closeWhenFinished)

    def interrupt(self, why):
        raise NotImplementedError

    def results(self):
        if self.rc in self.decodeRC:
            return self.decodeRC[self.rc]
        return FAILURE

    def didFail(self):
        return self.results() == FAILURE

    def fakeLogData(self, step, log, header='', stdout='', stderr=''):
        # note that this should not be used in the same test as useLog(Delayed)
        self.logs[log] = l = FakeLogFile(log, step)
        l.fakeData(header=header, stdout=stdout, stderr=stderr)

    def __repr__(self):
        return "FakeRemoteCommand("+repr(self.remote_command)+","+repr(self.args)+")"
class FakeRemoteShellCommand(FakeRemoteCommand):

    def __init__(self, workdir, command, env=None,
                 want_stdout=1, want_stderr=1,
                 timeout=20*60, maxTime=None, logfiles={},
                 usePTY="slave-config", logEnviron=True, collectStdout=False,
                 collectStderr=False,
                 interruptSignal=None, initialStdin=None, decodeRC={0:SUCCESS}):
        args = dict(workdir=workdir, command=command, env=env or {},
                want_stdout=want_stdout, want_stderr=want_stderr,
                initial_stdin=initialStdin,
                timeout=timeout, maxTime=maxTime, logfiles=logfiles,
                usePTY=usePTY, logEnviron=logEnviron)
        FakeRemoteCommand.__init__(self, "shell", args,
                                   collectStdout=collectStdout,
                                   collectStderr=collectStderr,
                                   decodeRC=decodeRC)


class FakeLogFile(object):

    def __init__(self, name, step):
        self.name = name
        self.header = ''
        self.stdout = ''
        self.stderr = ''
        self.chunks = []
        self.step = step

    def getName(self):
        return self.name

    def addHeader(self, text):
        self.header += text
        self.chunks.append((HEADER, text))

    def addStdout(self, text):
        self.stdout += text
        self.chunks.append((STDOUT, text))
        if self.name in self.step.logobservers:
            for obs in self.step.logobservers[self.name]:
                obs.outReceived(text)

    def addStderr(self, text):
        self.stderr += text
        self.chunks.append((STDERR, text))
        if self.name in self.step.logobservers:
            for obs in self.step.logobservers[self.name]:
                obs.errReceived(text)

    def readlines(self):
        io = StringIO(self.stdout)
        return io.readlines()

    def getText(self):
        return ''.join([ c for str,c in self.chunks
                           if str in (STDOUT, STDERR)])
    def getTextWithHeaders(self):
        return ''.join([ c for str,c in self.chunks])

    def getChunks(self, channels=[], onlyText=False):
        if onlyText:
            return [ data
                        for (ch, data) in self.chunks
                        if not channels or ch in channels ]
        else:
            return [ (ch, data)
                        for (ch, data) in self.chunks
                        if not channels or ch in channels ]

    def finish(self):
        pass

    def fakeData(self, header='', stdout='', stderr=''):
        if header:
            self.header += header
            self.chunks.append((HEADER, header))
        if stdout:
            self.stdout += stdout
            self.chunks.append((STDOUT, stdout))
        if stderr:
            self.stderr += stderr
            self.chunks.append((STDERR, stderr))

class ExpectRemoteRef(object):
    """
    Define an expected RemoteReference in the args to an L{Expect} class
    """

    def __init__(self, rrclass):
        self.rrclass = rrclass

    def __eq__(self, other):
        return isinstance(other, self.rrclass)

class Expect(object):
    """
    Define an expected L{RemoteCommand}, with the same arguments

    Extra behaviors of the remote command can be added to the instance, using
    class methods.  Use L{Expect.log} to add a logfile, L{Expect.update} to add
    an arbitrary update, or add an integer to specify the return code (rc), or
    add a Failure instance to raise an exception. Additionally, use
    L{Expect.behavior}, passing a callable that will be invoked with the real
    command and can do what it likes:

        def custom_behavior(command):
            ...
        Expect('somecommand', { args='foo' })
            + Expect.behavior(custom_behavior),
        ...

        Expect('somecommand', { args='foo' })
            + Expect.log('stdio', stdout='foo!')
            + Expect.log('config.log', stdout='some info')
            + Expect.update('status', 'running')
            + 0,      # (specifies the rc)
        ...

    """

    def __init__(self, remote_command, args, incomparable_args=[]):
        """

        Expect a command named C{remote_command}, with args C{args}.  Any args
        in C{incomparable_args} are not cmopared, but must exist.

        """
        self.remote_command = remote_command
        self.incomparable_args = incomparable_args
        self.args = args
        self.result = None
        self.behaviors = []

    @classmethod
    def behavior(cls, callable):
        """
        Add an arbitrary behavior that is expected of this command.
        C{callable} will be invoked with the real command as an argument, and
        can do what it wishes.  It will be invoked with maybeDeferred, in case
        the operation is asynchronous.
        """
        return ('callable', callable)

    @classmethod
    def log(self, name, **streams):
        return ('log', name, streams)

    @classmethod
    def update(self, name, value):
        return ('update', name, value)

    def __add__(self, other):
        # special-case adding an integer (return code) or failure (error)
        if isinstance(other, int):
            self.behaviors.append(('rc', other))
        elif isinstance(other, failure.Failure):
            self.behaviors.append(('err', other))
        else:
            self.behaviors.append(other)
        return self

    def runBehavior(self, behavior, args, command):
        """
        Implement the given behavior.  Returns a Deferred.
        """
        if behavior == 'rc':
            command.rc = args[0]
        elif behavior == 'err':
            return defer.fail(args[0])
        elif behavior == 'update':
            command.updates.setdefault(args[0], []).append(args[1])
        elif behavior == 'log':
            name, streams = args
            if 'header' in streams:
                command.logs[name].addHeader(streams['header'])
            if 'stdout' in streams:
                command.logs[name].addStdout(streams['stdout'])
                if command.collectStdout:
                    command.stdout += streams['stdout']
            if 'stderr' in streams:
                command.logs[name].addStderr(streams['stderr'])
                if command.collectStderr:
                    command.stderr += streams['stderr']
        elif behavior == 'callable':
            return defer.maybeDeferred(lambda : args[0](command))
        else:
            return defer.fail(failure.Failure(
                        AssertionError('invalid behavior %s' % behavior)))
        return defer.succeed(None)

    @defer.inlineCallbacks
    def runBehaviors(self, command):
        """
        Run all expected behaviors for this command
        """
        for behavior in self.behaviors:
            yield self.runBehavior(behavior[0], behavior[1:], command)
    def __repr__(self):
        return "Expect("+repr(self.remote_command)+")"

class ExpectShell(Expect):
    """
    Define an expected L{RemoteShellCommand}, with the same arguments Any
    non-default arguments must be specified explicitly (e.g., usePTY).
    """
    def __init__(self, workdir, command, env={},
                 want_stdout=1, want_stderr=1, initialStdin=None,
                 timeout=20*60, maxTime=None, logfiles={},
                 usePTY="slave-config", logEnviron=True):
        args = dict(workdir=workdir, command=command, env=env,
                want_stdout=want_stdout, want_stderr=want_stderr,
                initial_stdin=initialStdin,
                timeout=timeout, maxTime=maxTime, logfiles=logfiles,
                usePTY=usePTY, logEnviron=logEnviron)
        Expect.__init__(self, "shell", args)
    def __repr__(self):
        return "ExpectShell("+repr(self.remote_command)+repr(self.args['command'])+")"