-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
test_util.py
405 lines (288 loc) · 12.8 KB
/
test_util.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# 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 datetime
import locale
import mock
import os
from twisted.internet import reactor
from twisted.internet import task
from twisted.trial import unittest
from buildbot import util
class formatInterval(unittest.TestCase):
def test_zero(self):
self.assertEqual(util.formatInterval(0), "0 secs")
def test_seconds_singular(self):
self.assertEqual(util.formatInterval(1), "1 secs")
def test_seconds(self):
self.assertEqual(util.formatInterval(7), "7 secs")
def test_minutes_one(self):
self.assertEqual(util.formatInterval(60), "60 secs")
def test_minutes_over_one(self):
self.assertEqual(util.formatInterval(61), "1 mins, 1 secs")
def test_minutes(self):
self.assertEqual(util.formatInterval(300), "5 mins, 0 secs")
def test_hours_one(self):
self.assertEqual(util.formatInterval(3600), "60 mins, 0 secs")
def test_hours_over_one_sec(self):
self.assertEqual(util.formatInterval(3601), "1 hrs, 1 secs")
def test_hours_over_one_min(self):
self.assertEqual(util.formatInterval(3660), "1 hrs, 60 secs")
def test_hours(self):
self.assertEqual(util.formatInterval(7200), "2 hrs, 0 secs")
def test_mixed(self):
self.assertEqual(util.formatInterval(7392), "2 hrs, 3 mins, 12 secs")
class TestHumanReadableDelta(unittest.TestCase):
def test_timeDeltaToHumanReadable(self):
"""
It will return a human readable time difference.
"""
result = util.human_readable_delta(1, 1)
self.assertEqual('super fast', result)
result = util.human_readable_delta(1, 2)
self.assertEqual('1 seconds', result)
result = util.human_readable_delta(1, 61)
self.assertEqual('1 minutes', result)
result = util.human_readable_delta(1, 62)
self.assertEqual('1 minutes, 1 seconds', result)
result = util.human_readable_delta(1, 60 * 60 + 1)
self.assertEqual('1 hours', result)
result = util.human_readable_delta(1, 60 * 60 + 61)
self.assertEqual('1 hours, 1 minutes', result)
result = util.human_readable_delta(1, 60 * 60 + 62)
self.assertEqual('1 hours, 1 minutes, 1 seconds', result)
result = util.human_readable_delta(1, 24 * 60 * 60 + 1)
self.assertEqual('1 days', result)
result = util.human_readable_delta(1, 24 * 60 * 60 + 2)
self.assertEqual('1 days, 1 seconds', result)
class safeTranslate(unittest.TestCase):
def test_str_good(self):
self.assertEqual(util.safeTranslate(str("full")), str("full"))
def test_str_bad(self):
self.assertEqual(util.safeTranslate(str("speed=slow;quality=high")),
str("speed_slow_quality_high"))
def test_str_pathological(self):
# if you needed proof this wasn't for use with sensitive data
self.assertEqual(util.safeTranslate(str("p\ath\x01ogy")),
str("p\ath\x01ogy")) # bad chars still here!
def test_unicode_good(self):
self.assertEqual(util.safeTranslate(u"full"), str("full"))
def test_unicode_bad(self):
self.assertEqual(util.safeTranslate(unicode("speed=slow;quality=high")),
str("speed_slow_quality_high"))
def test_unicode_pathological(self):
self.assertEqual(util.safeTranslate(u"\u0109"),
str("\xc4\x89")) # yuck!
class naturalSort(unittest.TestCase):
def test_alpha(self):
self.assertEqual(
util.naturalSort(['x', 'aa', 'ab']),
['aa', 'ab', 'x'])
def test_numeric(self):
self.assertEqual(
util.naturalSort(['1', '10', '11', '2', '20']),
['1', '2', '10', '11', '20'])
def test_alphanum(self):
l1 = 'aa10ab aa1ab aa10aa f a aa3 aa30 aa3a aa30a'.split()
l2 = 'a aa1ab aa3 aa3a aa10aa aa10ab aa30 aa30a f'.split()
self.assertEqual(util.naturalSort(l1), l2)
class none_or_str(unittest.TestCase):
def test_none(self):
self.assertEqual(util.none_or_str(None), None)
def test_str(self):
self.assertEqual(util.none_or_str("hi"), "hi")
def test_int(self):
self.assertEqual(util.none_or_str(199), "199")
class TimeFunctions(unittest.TestCase):
def test_UTC(self):
self.assertEqual(util.UTC.utcoffset(datetime.datetime.now()),
datetime.timedelta(0))
self.assertEqual(util.UTC.dst(datetime.datetime.now()),
datetime.timedelta(0))
self.assertEqual(util.UTC.tzname(datetime.datetime.utcnow()), "UTC")
def test_epoch2datetime(self):
self.assertEqual(util.epoch2datetime(0),
datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=util.UTC))
self.assertEqual(util.epoch2datetime(1300000000),
datetime.datetime(2011, 3, 13, 7, 6, 40, tzinfo=util.UTC))
def test_datetime2epoch(self):
dt = datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=util.UTC)
self.assertEqual(util.datetime2epoch(dt), 0)
dt = datetime.datetime(2011, 3, 13, 7, 6, 40, tzinfo=util.UTC)
self.assertEqual(util.datetime2epoch(dt), 1300000000)
class DiffSets(unittest.TestCase):
def test_empty(self):
removed, added = util.diffSets(set([]), set([]))
self.assertEqual((removed, added), (set([]), set([])))
def test_no_lists(self):
removed, added = util.diffSets([1, 2], [2, 3])
self.assertEqual((removed, added), (set([1]), set([3])))
def test_no_overlap(self):
removed, added = util.diffSets(set([1, 2]), set([3, 4]))
self.assertEqual((removed, added), (set([1, 2]), set([3, 4])))
def test_no_change(self):
removed, added = util.diffSets(set([1, 2]), set([1, 2]))
self.assertEqual((removed, added), (set([]), set([])))
def test_added(self):
removed, added = util.diffSets(set([1, 2]), set([1, 2, 3]))
self.assertEqual((removed, added), (set([]), set([3])))
def test_removed(self):
removed, added = util.diffSets(set([1, 2]), set([1]))
self.assertEqual((removed, added), (set([2]), set([])))
class MakeList(unittest.TestCase):
def test_empty_string(self):
self.assertEqual(util.makeList(''), [''])
def test_None(self):
self.assertEqual(util.makeList(None), [])
def test_string(self):
self.assertEqual(util.makeList('hello'), ['hello'])
def test_unicode(self):
self.assertEqual(util.makeList(u'\N{SNOWMAN}'), [u'\N{SNOWMAN}'])
def test_list(self):
self.assertEqual(util.makeList(['a', 'b']), ['a', 'b'])
def test_tuple(self):
self.assertEqual(util.makeList(('a', 'b')), ['a', 'b'])
def test_copy(self):
input = ['a', 'b']
output = util.makeList(input)
input.append('c')
self.assertEqual(output, ['a', 'b'])
class Flatten(unittest.TestCase):
def test_simple(self):
self.assertEqual(util.flatten([1, 2, 3]), [1, 2, 3])
def test_deep(self):
self.assertEqual(util.flatten([[1, 2], 3, [[4]]]),
[1, 2, 3, 4])
# def test_deeply_nested(self):
# self.assertEqual(util.flatten([5, [6, (7, 8)]]),
# [5, 6, 7, 8])
# def test_tuples(self):
# self.assertEqual(util.flatten([(1, 2), 3]), [1, 2, 3])
def test_dict(self):
d = {'a': [5, 6, 7], 'b': [7, 8, 9]}
self.assertEqual(util.flatten(d), d)
def test_string(self):
self.assertEqual(util.flatten("abc"), "abc")
class Ascii2Unicode(unittest.TestCase):
def test_unicode(self):
rv = util.ascii2unicode(u'\N{SNOWMAN}')
self.assertEqual((rv, type(rv)), (u'\N{SNOWMAN}', unicode))
def test_ascii(self):
rv = util.ascii2unicode('abcd')
self.assertEqual((rv, type(rv)), (u'abcd', unicode))
def test_nonascii(self):
self.assertRaises(UnicodeDecodeError, lambda:
util.ascii2unicode('a\x85'))
def test_None(self):
self.assertEqual(util.ascii2unicode(None), None)
class StringToBoolean(unittest.TestCase):
def test_it(self):
stringValues = [
('on', True),
('true', True),
('yes', True),
('1', True),
('off', False),
('false', False),
('no', False),
('0', False),
('ON', True),
('TRUE', True),
('YES', True),
('OFF', False),
('FALSE', False),
('NO', False),
]
for s, b in stringValues:
self.assertEqual(util.string2boolean(s), b, repr(s))
def test_ascii(self):
rv = util.ascii2unicode('abcd')
self.assertEqual((rv, type(rv)), (u'abcd', unicode))
def test_nonascii(self):
self.assertRaises(UnicodeDecodeError, lambda:
util.ascii2unicode('a\x85'))
def test_None(self):
self.assertEqual(util.ascii2unicode(None), None)
class AsyncSleep(unittest.TestCase):
def test_sleep(self):
clock = task.Clock()
self.patch(reactor, 'callLater', clock.callLater)
d = util.asyncSleep(2)
self.assertFalse(d.called)
clock.advance(1)
self.assertFalse(d.called)
clock.advance(1)
self.assertTrue(d.called)
class FunctionalEnvironment(unittest.TestCase):
def test_working_locale(self):
environ = {'LANG': 'en_GB.UTF-8'}
self.patch(os, 'environ', environ)
config = mock.Mock()
util.check_functional_environment(config)
self.assertEqual(config.error.called, False)
def test_broken_locale(self):
def err():
raise KeyError
self.patch(locale, 'getdefaultlocale', err)
config = mock.Mock()
util.check_functional_environment(config)
config.error.assert_called_with(mock.ANY)
class StripUrlPassword(unittest.TestCase):
def test_simple_url(self):
self.assertEqual(util.stripUrlPassword('http://foo.com/bar'),
'http://foo.com/bar')
def test_username(self):
self.assertEqual(util.stripUrlPassword('http://d@foo.com/bar'),
'http://d@foo.com/bar')
def test_username_with_at(self):
self.assertEqual(util.stripUrlPassword('http://d@bb.net@foo.com/bar'),
'http://d@bb.net@foo.com/bar')
def test_username_pass(self):
self.assertEqual(util.stripUrlPassword('http://d:secret@foo.com/bar'),
'http://d:xxxx@foo.com/bar')
def test_username_pass_with_at(self):
self.assertEqual(
util.stripUrlPassword('http://d@bb.net:scrt@foo.com/bar'),
'http://d@bb.net:xxxx@foo.com/bar')
class JoinList(unittest.TestCase):
def test_list(self):
self.assertEqual(util.join_list(['aa', 'bb']), u'aa bb')
def test_tuple(self):
self.assertEqual(util.join_list(('aa', 'bb')), u'aa bb')
def test_string(self):
self.assertEqual(util.join_list('abc'), u'abc')
def test_unicode(self):
self.assertEqual(util.join_list(u'abc'), u'abc')
def test_nonascii(self):
self.assertRaises(UnicodeDecodeError, lambda: util.join_list(['\xff']))
class CommandToString(unittest.TestCase):
def test_short_string(self):
self.assertEqual(util.command_to_string("ab cd"), u"'ab cd'")
def test_long_string(self):
self.assertEqual(util.command_to_string("ab cd ef"), u"'ab cd ...'")
def test_list(self):
self.assertEqual(util.command_to_string(['ab', 'cd', 'ef']),
u"'ab cd ...'")
def test_nested_list(self):
self.assertEqual(util.command_to_string(['ab', ['cd', ['ef']]]),
u"'ab cd ...'")
def test_object(self):
# this looks like a renderable
self.assertEqual(util.command_to_string(object()), None)
def test_list_with_objects(self):
# the object looks like a renderable, and is skipped
self.assertEqual(util.command_to_string(['ab', object(), 'cd']),
u"'ab cd'")
def test_invalid_ascii(self):
self.assertEqual(util.command_to_string('a\xffc'), u"'a\ufffdc'")