This repository has been archived by the owner on May 24, 2018. It is now read-only.
forked from buildbot/buildbot
-
Notifications
You must be signed in to change notification settings - Fork 31
/
test_process_log.py
289 lines (227 loc) · 9.11 KB
/
test_process_log.py
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
# 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 mock
from buildbot.process import log
from buildbot.test.fake import fakemaster
from buildbot.test.fake import logfile as fakelogfile
from buildbot.test.util import interfaces
from twisted.internet import defer
from twisted.trial import unittest
class Tests(unittest.TestCase):
def setUp(self):
self.master = fakemaster.make_master(testcase=self,
wantData=True)
@defer.inlineCallbacks
def makeLog(self, type, logEncoding='utf-8'):
logid = yield self.master.data.updates.addLog(
stepid=27, name=u'testlog', type=unicode(type))
defer.returnValue(log.Log.new(self.master, u'testlog', type,
logid, logEncoding))
@defer.inlineCallbacks
def test_creation(self):
for type in 'ths':
yield self.makeLog(type)
def test_logDecodeFunctionFromConfig(self):
otilde = u'\u00f5'
otilde_utf8 = otilde.encode('utf-8')
otilde_latin1 = otilde.encode('latin1')
invalid_utf8 = '\xff'
replacement = u'\ufffd'
f = log.Log._decoderFromString('latin-1')
self.assertEqual(f(otilde_latin1), otilde)
f = log.Log._decoderFromString('utf-8')
self.assertEqual(f(otilde_utf8), otilde)
self.assertEqual(f(invalid_utf8), replacement)
f = log.Log._decoderFromString(lambda s: unicode(s[::-1]))
self.assertEqual(f('abc'), u'cba')
@defer.inlineCallbacks
def test_updates_plain(self):
l = yield self.makeLog('t')
l.addContent(u'hello\n')
l.addContent(u'hello ')
l.addContent(u'cruel ')
l.addContent(u'world\nthis is a second line') # unfinished
l.finish()
self.assertEqual(self.master.data.updates.logs[l.logid], {
'content': [u'hello\n', u'hello cruel world\n',
u'this is a second line\n'],
'finished': True,
'type': u't',
'name': u'testlog',
})
@defer.inlineCallbacks
def test_updates_different_encoding(self):
l = yield self.makeLog('t', logEncoding='latin-1')
l.addContent('$ and \xa2\n') # 0xa2 is latin-1 encoding for CENT SIGN
l.finish()
self.assertEqual(self.master.data.updates.logs[l.logid]['content'],
[u'$ and \N{CENT SIGN}\n'])
@defer.inlineCallbacks
def test_updates_unicode_input(self):
l = yield self.makeLog('t', logEncoding='something-invalid')
l.addContent(u'\N{SNOWMAN}\n')
l.finish()
self.assertEqual(self.master.data.updates.logs[l.logid]['content'],
[u'\N{SNOWMAN}\n'])
@defer.inlineCallbacks
def test_subscription_plain(self):
l = yield self.makeLog('t')
calls = []
l.subscribe(lambda stream, content: calls.append((stream, content)))
self.assertEqual(calls, [])
yield l.addContent(u'hello\n')
self.assertEqual(calls, [(None, u'hello\n')])
calls = []
yield l.addContent(u'hello ')
self.assertEqual(calls, [])
yield l.addContent(u'cruel ')
self.assertEqual(calls, [])
yield l.addContent(u'world\nthis is a second line\n')
self.assertEqual(calls, [
(None, u'hello cruel world\nthis is a second line\n')])
calls = []
yield l.finish()
self.assertEqual(calls, [(None, None)])
@defer.inlineCallbacks
def test_subscription_unsubscribe(self):
l = yield self.makeLog('t')
sub_fn = mock.Mock()
sub = l.subscribe(sub_fn)
sub.unsubscribe()
yield l.finish()
sub_fn.assert_not_called()
@defer.inlineCallbacks
def test_subscription_stream(self):
l = yield self.makeLog('s')
calls = []
l.subscribe(lambda stream, content: calls.append((stream, content)))
self.assertEqual(calls, [])
yield l.addStdout(u'hello\n')
self.assertEqual(calls, [('o', u'hello\n')])
calls = []
yield l.addStdout(u'hello ')
self.assertEqual(calls, [])
yield l.addStdout(u'cruel ')
self.assertEqual(calls, [])
yield l.addStderr(u'!!\n')
self.assertEqual(calls, [('e', '!!\n')])
calls = []
yield l.addHeader(u'**\n')
self.assertEqual(calls, [('h', '**\n')])
calls = []
yield l.addStdout(u'world\nthis is a second line') # unfinished
self.assertEqual(calls, [
('o', u'hello cruel world\n')])
calls = []
yield l.finish()
self.assertEqual(calls, [
('o', u'this is a second line\n'),
(None, None)])
@defer.inlineCallbacks
def test_updates_stream(self):
l = yield self.makeLog('s')
l.addStdout(u'hello\n')
l.addStdout(u'hello ')
l.addStderr(u'oh noes!\n')
l.addStdout(u'cruel world\n')
l.addStderr(u'bad things!') # unfinished
l.finish()
self.assertEqual(self.master.data.updates.logs[l.logid], {
'content': [u'ohello\n', u'eoh noes!\n', u'ohello cruel world\n',
u'ebad things!\n'],
'finished': True,
'name': u'testlog',
'type': u's',
})
@defer.inlineCallbacks
def test_isFinished(self):
l = yield self.makeLog('s')
self.assertFalse(l.isFinished())
yield l.finish()
self.assertTrue(l.isFinished())
@defer.inlineCallbacks
def test_waitUntilFinished(self):
l = yield self.makeLog('s')
d = l.waitUntilFinished()
self.assertFalse(d.called)
yield l.finish()
self.assertTrue(d.called)
class InterfaceTests(interfaces.InterfaceTests):
# for compatibility between old-style and new-style steps, both
# buildbot.status.logfile.LogFile and buildbot.process.log.StreamLog must
# meet this interace, at least until support for old-style steps is
# removed.
# ILogFile
def test_signature_addStdout(self):
@self.assertArgSpecMatches(self.log.addStdout)
def addStdout(self, text):
pass
def test_signature_addStderr(self):
@self.assertArgSpecMatches(self.log.addStderr)
def addStderr(self, text):
pass
def test_signature_addHeader(self):
@self.assertArgSpecMatches(self.log.addHeader)
def addHeader(self, text):
pass
def test_signature_finish(self):
@self.assertArgSpecMatches(self.log.finish)
def finish(self):
pass
# IStatusLog
def test_signature_getName(self):
@self.assertArgSpecMatches(self.log.getName)
def getName(self):
pass
def test_getName(self):
self.assertEqual(self.log.getName(), 'stdio')
def test_signature_isFinished(self):
@self.assertArgSpecMatches(self.log.isFinished)
def isFinished(self):
pass
def test_signature_waitUntilFinished(self):
@self.assertArgSpecMatches(self.log.waitUntilFinished)
def waitUntilFinished(self):
pass
def test_signature_subscribe(self):
@self.assertArgSpecMatches(self.log.subscribe)
def subscribe(self, callback):
pass
def test_signature_unsubscribe(self):
# method has been removed
self.failIf(hasattr(self.log, 'unsubscribe'))
def test_signature_getStep_removed(self):
self.failIf(hasattr(self.log, 'getStep'))
def test_signature_subscribeConsumer_removed(self):
self.failIf(hasattr(self.log, 'subscribeConsumer'))
def test_signature_hasContents_removed(self):
self.failIf(hasattr(self.log, 'hasContents'))
def test_signature_getText_removed(self):
self.failIf(hasattr(self.log, 'getText'))
def test_signature_readlines_removed(self):
self.failIf(hasattr(self.log, 'readlines'))
def test_signature_getTextWithHeaders_removed(self):
self.failIf(hasattr(self.log, 'getTextWithHeaders'))
def test_signature_getChunks_removed(self):
self.failIf(hasattr(self.log, 'getChunks'))
class TestProcessItfc(unittest.TestCase, InterfaceTests):
def setUp(self):
self.log = log.StreamLog(mock.Mock(name='master'), 'stdio', 's',
101, unicode)
class TestFakeLogFile(unittest.TestCase, InterfaceTests):
def setUp(self):
step = mock.Mock(name='fake step')
step.logobservers = []
self.log = fakelogfile.FakeLogFile('stdio', step)