-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathunittest_grouper.py
275 lines (230 loc) · 7.92 KB
/
unittest_grouper.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
import argparse
import hashlib
import json
import os
import platform
import stat
import subprocess
import sys
import time
import urllib.request
from typing import Dict, List
from retry import retry
from buildscripts.simple_report import make_report, put_report, try_combine_reports
sys.path.append(".")
from buildscripts.install_bazel import install_bazel
RELEASE_URL = "https://github.com/bazelbuild/buildtools/releases/download/v7.3.1/"
groups_sort_keys = {
"first": 1,
"second": 2,
"third": 3,
"fourth": 4,
"fifth": 5,
"sixth": 6,
"seventh": 7,
"eighth": 8,
}
@retry(tries=3, delay=5)
def _download_with_retry(*args, **kwargs):
return urllib.request.urlretrieve(*args, **kwargs)
def determine_platform():
syst = platform.system()
pltf = None
if syst == "Darwin":
pltf = "darwin"
elif syst == "Windows":
pltf = "windows"
elif syst == "Linux":
pltf = "linux"
else:
raise RuntimeError("Platform cannot be inferred.")
return pltf
def determine_architecture():
arch = None
machine = platform.machine()
if machine in ("AMD64", "x86_64"):
arch = "amd64"
elif machine in ("arm", "arm64", "aarch64"):
arch = "arm64"
else:
raise RuntimeError(f"Detected architecture is not supported: {machine}")
return arch
def download_buildozer(download_location: str = "./"):
operating_system = determine_platform()
architechture = determine_architecture()
if operating_system == "windows" and architechture == "arm64":
raise RuntimeError("There are no published arm windows releases for buildifier.")
extension = ".exe" if operating_system == "windows" else ""
binary_name = f"buildozer-{operating_system}-{architechture}{extension}"
url = f"{RELEASE_URL}{binary_name}"
file_location = os.path.join(download_location, f"buildozer{extension}")
_download_with_retry(url, file_location)
os.chmod(file_location, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
return file_location
def find_group(unittest_paths):
groups = {
# group1
"0": "first",
"1": "first",
# group2
"2": "second",
"3": "second",
# group3
"4": "third",
"5": "third",
# group4
"6": "fourth",
"7": "fourth",
# group5
"8": "fifth",
"9": "fifth",
# group6
"a": "sixth",
"b": "sixth",
# group7
"c": "seventh",
"d": "seventh",
# group8
"e": "eighth",
"f": "eighth",
}
group_to_path: Dict[str, List[str]] = {}
for path in unittest_paths:
norm_path = path.replace(":", "/").replace("\\", "/")
if norm_path.startswith("//"):
norm_path = norm_path[2:]
if not norm_path.startswith("src/"):
print(f"ERROR: {path} not relative to mongo repo root")
sys.exit(1)
basename = os.path.basename(norm_path)
if basename.startswith("lib"):
basename = basename[3:]
ext = basename.find(".")
if ext != -1:
basename = basename[:ext]
dirname = os.path.dirname(norm_path)
hash_path = os.path.join(dirname, basename).replace("\\", "/")
first_char = hashlib.sha256(hash_path.encode()).hexdigest()[0]
group = groups[first_char]
if group not in group_to_path:
group_to_path[group] = []
group_to_path[group].append(path)
return json.dumps(group_to_path, indent=4)
def find_multiple_groups(test, groups):
tagged_groups = []
for group in groups:
if test in groups[group]:
tagged_groups.append(group)
return tagged_groups
def validate_bazel_groups(generate_report, fix):
buildozer = download_buildozer()
bazel_bin = install_bazel(".")
query_opts = [
"--implicit_deps=False",
"--tool_deps=False",
"--include_aspects=False",
"--remote_executor=",
"--remote_cache=",
"--bes_backend=",
"--bes_results_url=",
]
try:
start = time.time()
sys.stdout.write("Query all unittest binaries... ")
sys.stdout.flush()
query_proc = subprocess.run(
[
bazel_bin,
"query",
'kind(extract_debug, attr(tags, "[\[ ]mongo_unittest[,\]]", //src/...))',
]
+ query_opts,
capture_output=True,
text=True,
check=True,
)
bazel_unittests = query_proc.stdout.splitlines()
sys.stdout.write("{:0.2f}s\n".format(time.time() - start))
except subprocess.CalledProcessError as exc:
print("BAZEL ERROR:")
print(exc.stdout)
print(exc.stderr)
sys.exit(exc.returncode)
buildozer_update_cmds = []
groups = json.loads(find_group(bazel_unittests))
failures = []
for group in sorted(groups, key=lambda x: groups_sort_keys[x]):
try:
start = time.time()
sys.stdout.write(f"Query all mongo_unittest_{group}_group unittests... ")
sys.stdout.flush()
query_proc = subprocess.run(
[
bazel_bin,
"query",
f'kind(extract_debug, attr(tags, "[\[ ]mongo_unittest_{group}_group[,\]]", //src/...))',
]
+ query_opts,
capture_output=True,
text=True,
check=True,
)
sys.stdout.write("{:0.2f}s\n".format(time.time() - start))
group_tests = query_proc.stdout.splitlines()
except subprocess.CalledProcessError as exc:
print("BAZEL ERROR:")
print(exc.stdout)
print(exc.stderr)
sys.exit(exc.returncode)
if groups[group] != group_tests:
for test in group_tests:
if test not in bazel_unittests:
failures.append(
[
test + " tag",
f"{test} not a 'mongo_unittest' but has 'mongo_unittest_{group}_group' tag.",
]
)
print(failures[-1][1])
if fix:
buildozer_update_cmds += [
[f"remove tags mongo_unittest_{group}_group", test]
]
for test in groups[group]:
if test not in group_tests:
failures.append(
[test + " tag", f"{test} missing 'mongo_unittest_{group}_group'"]
)
print(failures[-1][1])
if fix:
buildozer_update_cmds += [[f"add tags mongo_unittest_{group}_group", test]]
for test in group_tests:
if test not in groups[group]:
failures.append(
[
test + " tag",
f"{test} is tagged in the wrong group.",
]
)
print(failures[-1][1])
if fix:
buildozer_update_cmds += [
[f"remove tags mongo_unittest_{group}_group", test]
]
if fix:
for cmd in buildozer_update_cmds:
subprocess.run([buildozer] + cmd)
if failures:
for failure in failures:
if generate_report:
report = make_report(failure[0], failure[1], 1)
try_combine_reports(report)
put_report(report)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--generate-report", default=False, action="store_true")
parser.add_argument("--fix", default=False, action="store_true")
args = parser.parse_args()
validate_bazel_groups(args.generate_report, args.fix)
if __name__ == "__main__":
main()