forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shard_util_test.py
executable file
·347 lines (311 loc) · 15.3 KB
/
shard_util_test.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
#!/usr/bin/env vpython3
# Copyright 2020 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import collections
from mock import patch
import os
import unittest
import shard_util
DEBUG_APP_OTOOL_OUTPUT = '\n'.join([
'Meta Class', 'name 0x1064b8438 CacheTestCase',
'baseMethods 0x1068586d8 (struct method_list_t *)',
'imp 0x1075e6887 -[CacheTestCase testA]', 'types 0x1064cc3e1',
'imp 0x1075e6887 -[CacheTestCase testB]',
'imp 0x1075e6887 -[CacheTestCase testc]', 'name 0x1064b8438 TabUITestCase',
'baseMethods 0x1068586d8 (struct method_list_t *)',
'imp 0x1075e6887 -[TabUITestCase testD]', 'types 0x1064cc3e1 v16@0:8',
'imp 0x1075e6887 -[TabUITestCase testE]',
'name 0x1064b8438 KeyboardTestCase',
'imp 0x1075e6887 -[KeyboardTestCase testF]',
'name 0x1064b8438 PasswordManagerTestCase',
'imp 0x1075e6887 -[PasswordManagerTestCase testG]',
'name 0x1064b8438 ToolBarTestCase',
'imp 0x1075e6887 -[ToolBarTestCase testH]',
'imp 0x1075e6887 -[ToolBarTestCase DISABLED_testI]',
'imp 0x1075e6887 -[ToolBarTestCase FLAKY_testJ]', 'version 0',
'name 0x1075d8539 PasswordManagerPasswordCheckupDisabledTestCase',
'imp 0x1075d8539 -[PasswordManagerPasswordCheckupDisabledTestCase testK]'
]).encode('utf-8')
# Debug app otool output format in Xcode 11.4 toolchain.
DEBUG_APP_OTOOL_OUTPUT_114 = '\n'.join([
'Meta Class', 'name 0x1064b8438 CacheTestCase',
'baseMethods 0x1068586d8 (struct method_list_t *)',
' imp 0x1075e6887 -[CacheTestCase testA]', ' types 0x1064cc3e1',
' imp 0x1075e6887 -[CacheTestCase testB]',
' imp 0x1075e6887 -[CacheTestCase testc]',
' name 0x1064b8438 TabUITestCase',
'baseMethods 0x1068586d8 (struct method_list_t *)',
' imp 0x1075e6887 -[TabUITestCase testD]',
' types 0x1064cc3e1 v16@0:8',
' imp 0x1075e6887 -[TabUITestCase testE]',
' name 0x1064b8438 KeyboardTestCase',
' imp 0x1075e6887 -[KeyboardTestCase testF]',
' name 0x1064b8438 PasswordsTestCase',
' imp 0x1075e6887 -[PasswordsTestCase testG]',
' name 0x1064b8438 ToolBarTestCase',
' imp 0x1075e6887 -[ToolBarTestCase testH]',
' imp 0x1075e6887 -[ToolBarTestCase DISABLED_testI]',
' imp 0x1075e6887 -[ToolBarTestCase FLAKY_testJ]', 'version 0'
]).encode('utf-8')
RELEASE_APP_OTOOL_OUTPUT = '\n'.join([
'Meta Class', 'name 0x1064b8438 CacheTestCase',
'baseMethods 0x1068586d8 (struct method_list_t *)',
'name 0x1075e6887 testA', 'types 0x1064cc3e1', 'name 0x1075e6887 testB',
'name 0x1075e6887 testc', 'baseProtocols 0x0',
'name 0x1064b8438 CacheTestCase', 'unrelated line', 'Meta Class',
'name 0x1064b8438 TabUITestCase', 'no test methods in this case',
'name 0x1064b8438 TabUITestCase', 'unrelated line',
'baseMethods 0x1068586d8 (struct method_list_t *)',
'name 0x1064b8438 KeyboardTest', 'name 0x1075e6887 testD',
'types 0x1064cc3e1 v16@0:8', 'name 0x1075e6887 testE',
'name 0x1075e6887 testF', 'baseProtocols 0x0',
'name 0x1064b8438 KeyboardTest', 'name 0x1075e6887 testUnrelatedG',
'unrelated line', 'name 0x1064b8438 ChromeTestCase',
'name 0x1064b8438 setUp', 'name 0x1064b8438 testPort',
'name 0x5345ac561 testSomeUnrelatedUtil', 'baseProtocols 0x0',
'name 0x1064b8438 ChromeTestCase', 'unrelated line',
'name 0x1064b8438 invalidTestCase', 'name 0x1075e6887 testG',
'baseProtocols 0x0', 'name 0x1064b8438 ToolBarTestCase',
'name 0x1075e6887 testG', 'name 0x1075e6887 testH',
'name 0x1075e6887 DISABLED_testI', 'name 0x1075e6887 FLAKY_testJ',
'name 0x1064b8438 ToolBarTestCase', 'baseProtocols 0x0', 'version 0'
]).encode('utf-8')
RELEASE_APP_OTOOL_OUTPUT_CLASS_NOT_IN_PAIRS = '\n'.join([
'Meta Class', 'name 0x1064b8438 CacheTestCase',
'baseMethods 0x1068586d8 (struct method_list_t *)',
'name 0x1075e6887 testA', 'types 0x1064cc3e1', 'name 0x1075e6887 testB',
'name 0x1075e6887 testc', 'baseProtocols 0x0',
'name 0x1064b8438 CacheTestCase', 'unrelated line', 'Meta Class',
'name 0x1064b8438 TabUITestCase', 'no test methods in this case',
'name 0x1064b8438 TabUITestCase', 'unrelated line',
'baseMethods 0x1068586d8 (struct method_list_t *)',
'name 0x1064b8438 KeyboardTest', 'name 0x1075e6887 testD',
'types 0x1064cc3e1 v16@0:8', 'name 0x1075e6887 testE',
'name 0x1075e6887 testF', 'baseProtocols 0x0',
'name 0x1075e6887 testUnrelatedG', 'unrelated line',
'name 0x1064b8438 ChromeTestCase', 'name 0x1064b8438 setUp',
'name 0x1064b8438 testPort', 'name 0x5345ac561 testSomeUnrelatedUtil',
'baseProtocols 0x0', 'name 0x1064b8438 ChromeTestCase', 'unrelated line',
'name 0x1064b8438 invalidTestCase', 'name 0x1075e6887 testG',
'baseProtocols 0x0', 'name 0x1064b8438 ToolBarTestCase',
'name 0x1075e6887 testG', 'name 0x1075e6887 testH',
'name 0x1075e6887 DISABLED_testI', 'name 0x1075e6887 FLAKY_testJ',
'name 0x1064b8438 ToolBarTestCase', 'baseProtocols 0x0', 'version 0'
]).encode('utf-8')
# Release app otool output format in Xcode 11.4 toolchain.
RELEASE_APP_OTOOL_OUTPUT_114 = '\n'.join([
'Meta Class', ' name 0x1064b8438 CacheTestCase',
'baseMethods 0x1068586d8 (struct method_list_t *)',
' name 0x1075e6887 testA', ' types 0x1064cc3e1',
' name 0x1075e6887 testB', ' name 0x1075e6887 testc',
'baseProtocols 0x0', ' name 0x1064b8438 CacheTestCase',
'unrelated line', 'Meta Class', ' name 0x1064b8438 TabUITestCase',
'no test methods in this case', ' name 0x1064b8438 TabUITestCase',
'unrelated line', 'baseMethods 0x1068586d8 (struct method_list_t *)',
' name 0x1064b8438 KeyboardTest', ' name 0x1075e6887 testD',
' types 0x1064cc3e1 v16@0:8', ' name 0x1075e6887 testE',
' name 0x1075e6887 testF', 'baseProtocols 0x0',
' name 0x1064b8438 KeyboardTest',
' name 0x1075e6887 testUnrelatedG', 'unrelated line',
' name 0x1064b8438 ChromeTestCase', ' name 0x1064b8438 setUp',
' name 0x1064b8438 testPort',
' name 0x5345ac561 testSomeUnrelatedUtil', 'baseProtocols 0x0',
' name 0x1064b8438 ChromeTestCase', 'unrelated line',
' name 0x1064b8438 invalidTestCase', ' name 0x1075e6887 testG',
'baseProtocols 0x0', ' name 0x1064b8438 ToolBarTestCase',
' name 0x1075e6887 testG', ' name 0x1075e6887 testH',
' name 0x1075e6887 DISABLED_testI',
' name 0x1075e6887 FLAKY_testJ',
' name 0x1064b8438 ToolBarTestCase', 'baseProtocols 0x0', 'version 0'
]).encode('utf-8')
class TestShardUtil(unittest.TestCase):
"""Test cases for shard_util.py"""
@patch('shard_util.os.path.abspath')
def test_determine_path_non_eg2(self, mock_abspath):
mock_abspath.return_value = '/b/s/w/ir/ios/build/bots/scripts/share_util.py'
app = 'some_ios_test.app'
actual_path = shard_util.determine_app_path(app)
expected_path = os.path.join('/b/s/w/ir', 'out/Debug', app, 'some_ios_test')
self.assertEqual(actual_path, expected_path)
@patch('shard_util.os.path.abspath')
def test_determine_path_eg2(self, mock_abspath):
mock_abspath.return_value = '/b/s/w/ir/ios/build/bots/scripts/share_util.py'
app = 'some_ios_test-Runner.app'
host = 'some_host.app'
actual_path = shard_util.determine_app_path(app, host)
expected_path = os.path.join('/b/s/w/ir', 'out/Debug', app, 'PlugIns',
'some_ios_test.xctest', 'some_ios_test')
self.assertEqual(actual_path, expected_path)
@patch('shard_util.os.path.abspath')
def test_determine_path_eg2_release(self, mock_abspath):
mock_abspath.return_value = '/b/s/w/ir/ios/build/bots/scripts/share_util.py'
app = 'some_ios_test-Runner.app'
host = 'some_host.app'
actual_path = shard_util.determine_app_path(app, host, True)
expected_path = os.path.join('/b/s/w/ir', 'out/Release', app, 'PlugIns',
'some_ios_test.xctest', 'some_ios_test')
self.assertEqual(actual_path, expected_path)
def test_fetch_test_names_debug(self):
"""Ensures that the debug output is formatted correctly"""
resp = shard_util.fetch_test_names_for_debug(DEBUG_APP_OTOOL_OUTPUT)
self.assertEqual(len(resp), 11)
expected_test_names = [
('CacheTestCase', 'testA'),
('CacheTestCase', 'testB'),
('CacheTestCase', 'testc'),
('TabUITestCase', 'testD'),
('TabUITestCase', 'testE'),
('KeyboardTestCase', 'testF'),
('PasswordManagerTestCase', 'testG'),
('ToolBarTestCase', 'testH'),
('ToolBarTestCase', 'DISABLED_testI'),
('ToolBarTestCase', 'FLAKY_testJ'),
('PasswordManagerPasswordCheckupDisabledTestCase', 'testK'),
]
for test_name in expected_test_names:
self.assertTrue(test_name in resp)
test_cases = [test_case for (test_case, _) in resp]
# ({'CacheTestCase': 3, 'TabUITestCase': 2, 'PasswordManagerTestCase': 1,
# 'KeyboardTestCase': 1, 'ToolBarTestCase': 3,
# 'PasswordManagerPasswordCheckupDisabledTestCase': 1})
counts = collections.Counter(test_cases).most_common()
name, _ = counts[0]
# CacheTestCase and ToolBarTestCase each have 3 entries.
# In case of ties, most_common() returns the first encountered at index 0.
self.assertEqual(name, 'CacheTestCase')
def test_fetch_test_counts_release(self):
"""Ensures that the release output is formatted correctly"""
resp = shard_util.fetch_test_names_for_release(RELEASE_APP_OTOOL_OUTPUT)
self.assertEqual(len(resp), 10)
expected_test_names = [
('CacheTestCase', 'testA'),
('CacheTestCase', 'testB'),
('CacheTestCase', 'testc'),
('KeyboardTest', 'testD'),
('KeyboardTest', 'testE'),
('KeyboardTest', 'testF'),
('ToolBarTestCase', 'testG'),
('ToolBarTestCase', 'testH'),
('ToolBarTestCase', 'DISABLED_testI'),
('ToolBarTestCase', 'FLAKY_testJ'),
]
for test_name in expected_test_names:
self.assertTrue(test_name in resp)
test_cases = [test_case for (test_case, _) in resp]
# ({'KeyboardTest': 3, 'CacheTestCase': 3,
# 'ToolBarTestCase': 4})
counts = collections.Counter(test_cases).most_common()
name, _ = counts[0]
self.assertEqual(name, 'ToolBarTestCase')
def test_fetch_test_error_release(self):
"""Ensures that unexpected release output raises error."""
with self.assertRaises(shard_util.ShardingError) as context:
shard_util.fetch_test_names_for_release(
RELEASE_APP_OTOOL_OUTPUT_CLASS_NOT_IN_PAIRS)
expected_message = (
'Incorrect otool output in which a test class name doesn\'t appear in '
'group of 2. Test class: KeyboardTest')
self.assertTrue(expected_message in str(context.exception))
def test_fetch_test_names_debug_114(self):
"""Test the debug output from otool in Xcode 11.4"""
resp = shard_util.fetch_test_names_for_debug(DEBUG_APP_OTOOL_OUTPUT_114)
self.assertEqual(len(resp), 10)
expected_test_names = [
('CacheTestCase', 'testA'),
('CacheTestCase', 'testB'),
('CacheTestCase', 'testc'),
('TabUITestCase', 'testD'),
('TabUITestCase', 'testE'),
('KeyboardTestCase', 'testF'),
('PasswordsTestCase', 'testG'),
('ToolBarTestCase', 'testH'),
('ToolBarTestCase', 'DISABLED_testI'),
('ToolBarTestCase', 'FLAKY_testJ'),
]
for test_name in expected_test_names:
self.assertTrue(test_name in resp)
test_cases = [test_case for (test_case, _) in resp]
# ({'CacheTestCase': 3, 'TabUITestCase': 2, 'PasswordsTestCase': 1,
# 'KeyboardTestCase': 1, 'ToolBarTestCase': 3})
counts = collections.Counter(test_cases).most_common()
name, _ = counts[0]
# CacheTestCase and ToolBarTestCase each have 3 entries.
# In case of ties, most_common() returns the first encountered at index 0.
self.assertEqual(name, 'CacheTestCase')
def test_fetch_test_counts_release_114(self):
"""Test the release output from otool in Xcode 11.4"""
resp = shard_util.fetch_test_names_for_release(RELEASE_APP_OTOOL_OUTPUT_114)
self.assertEqual(len(resp), 10)
expected_test_names = [
('CacheTestCase', 'testA'),
('CacheTestCase', 'testB'),
('CacheTestCase', 'testc'),
('KeyboardTest', 'testD'),
('KeyboardTest', 'testE'),
('KeyboardTest', 'testF'),
('ToolBarTestCase', 'testG'),
('ToolBarTestCase', 'testH'),
('ToolBarTestCase', 'DISABLED_testI'),
('ToolBarTestCase', 'FLAKY_testJ'),
]
for test_name in expected_test_names:
self.assertTrue(test_name in resp)
test_cases = [test_case for (test_case, _) in resp]
# ({'KeyboardTest': 3, 'CacheTestCase': 3,
# 'ToolBarTestCase': 4})
counts = collections.Counter(test_cases).most_common()
name, _ = counts[0]
self.assertEqual(name, 'ToolBarTestCase')
def test_balance_into_sublists_debug(self):
"""Ensure the balancing algorithm works"""
resp = shard_util.fetch_test_names_for_debug(DEBUG_APP_OTOOL_OUTPUT)
test_cases = [test_case for (test_case, _) in resp]
test_counts = collections.Counter(test_cases)
sublists_1 = shard_util.balance_into_sublists(test_counts, 1)
self.assertEqual(len(sublists_1), 1)
self.assertEqual(len(sublists_1[0]), 6)
sublists_3 = shard_util.balance_into_sublists(test_counts, 3)
self.assertEqual(len(sublists_3), 3)
# CacheTestCase has 3,
# TabUITestCase has 2, ToolBarTestCase has 4
# PasswordManagerTestCase has 1, KeyboardTestCase has 1
# PasswordManagerPasswordCheckupDisabledTestCase has 2 (due to inheritance)
# They will be balanced into:
# [[ToolBarTestCase, KeyboardTestCase],
# [CacheTestCase, PasswordManagerTestCase],
# [PasswordManagerPasswordCheckupDisabledTestCase, TabUITestCase]]
self.assertEqual(
sorted([len(sublists_3[0]),
len(sublists_3[1]),
len(sublists_3[2])]), [2, 2, 2])
def test_balance_into_sublists_release(self):
"""Ensure the balancing algorithm works"""
resp = shard_util.fetch_test_names_for_release(RELEASE_APP_OTOOL_OUTPUT)
test_cases = [test_case for (test_case, _) in resp]
test_counts = collections.Counter(test_cases)
sublists_3 = shard_util.balance_into_sublists(test_counts, 3)
self.assertEqual(len(sublists_3), 3)
# KeyboardTest has 3
# CacheTestCase has 3
# ToolbarTest Case has 4
# They will be balanced as one in each shard.
self.assertEqual(len(sublists_3[0]), 1)
self.assertEqual(len(sublists_3[1]), 1)
self.assertEqual(len(sublists_3[2]), 1)
def test_balance_into_sublists_release(self):
"""Ensure the balancing algorithm works"""
resp = shard_util.fetch_test_names_for_release(RELEASE_APP_OTOOL_OUTPUT)
test_cases = [test_case for (test_case, _) in resp]
test_counts = collections.Counter(test_cases)
sublists_3 = shard_util.balance_into_sublists(test_counts, 3)
self.assertEqual(len(sublists_3), 3)
# KeyboardTest has 3
# CacheTestCase has 3
# ToolbarTest Case has 4
# They will be balanced as one in each shard.
self.assertEqual(len(sublists_3[0]), 1)
self.assertEqual(len(sublists_3[1]), 1)
self.assertEqual(len(sublists_3[2]), 1)
if __name__ == '__main__':
unittest.main()