-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathci.py
More file actions
executable file
·285 lines (232 loc) · 7.81 KB
/
ci.py
File metadata and controls
executable file
·285 lines (232 loc) · 7.81 KB
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
#!/usr/bin/env -S python3 -u
"""
This script is the entrypoint for the CI.
To make CI errors easier to reproduce locally, please limit
this script to using only the standard library on a recent-ish
python 3 release.
Please also be conservative with what tools you expect on the host
system when subprocessing out. Safe things to expect are `git` and
`nix`. Note that when running subprocessing _inside_ the nix env
you are free to use whatever the flake provides.
"""
from collections import namedtuple
from enum import Enum
from functools import lru_cache
from io import StringIO
import multiprocessing
import os
from pathlib import Path
import shutil
import subprocess
import sys
from typing import Callable, Dict, List, Optional, Union
BUILD_DIR = "build-ci"
#
# Knobs CI might use. We choose to use env vars b/c it's less
# messy than propagating flags everywhere.
#
# Default nix target is empty string which by convention is the
# latest LLVM release we support
NIX_TARGET = os.environ.get("NIX_TARGET", "")
CMAKE_BUILD_TYPE = os.environ.get("CMAKE_BUILD_TYPE", "Release")
RUN_TESTS = os.environ.get("RUN_TESTS", "1")
RUN_MEMLEAK_TEST = os.environ.get("RUN_MEMLEAK_TEST", "0")
CC = os.environ.get("CC", "cc")
CXX = os.environ.get("CXX", "c++")
RUNTIME_TEST_DISABLE = os.environ.get("RUNTIME_TEST_DISABLE", "")
TOOLS_TEST_OLDVERSION = os.environ.get("TOOLS_TEST_OLDVERSION", "")
TOOLS_TEST_DISABLE = os.environ.get("TOOLS_TEST_DISABLE", "")
class TestStatus(Enum):
PASSED = "passed"
FAILED = "failed"
SKIPPED = "skipped"
TestResult = namedtuple("TestResult", ["test_name", "status"])
def truthy(value: str) -> bool:
v = value.strip().lower()
return v == "true" or v == "1"
@lru_cache(maxsize=1)
def root() -> Path:
"""Return the absolute path root of git repo"""
output = subprocess.check_output(["git", "rev-parse", "--show-toplevel"])
return Path(output.decode("utf-8").strip())
def _which(cmd: str) -> Path:
p = shutil.which(cmd)
if not p:
raise RuntimeError(f"Failed to find binary: {cmd}")
return Path(p)
@lru_cache(maxsize=1)
def nix() -> Path:
"""Return the absolute path of nix binary in host env"""
return _which("nix")
@lru_cache(maxsize=1)
def sudo() -> Path:
"""Return the absolute path of sudo binary in host env"""
return _which("sudo")
def shell(
cmd: List[str],
as_root: bool = False,
cwd: Optional[Path] = None,
env: Optional[Dict[str, str]] = None,
):
"""
Runs the specified command in the proper nix development
environment.
Note that output is sent to our inherited stderr/stdout and
that any errors immediately raise an exception.
"""
c: List[Union[str, Path]] = [
nix(),
"develop",
]
if NIX_TARGET:
c.append(NIX_TARGET)
c.append("--command")
if as_root:
to_preserve = ",".join([n for n in env]) if env else []
c += [
sudo(),
# We need to preserve path so that default root PATH is not
# generated. If that occurs, then commands run in nix env
# can escape and use host system binaries. This creates some
# very hard to debug errors in CI.
#
# And yes, I realize that we should probably be using nix's
# sandboxing via checkPhase, but unfortunately that does not
# play nice with root or writing temporary files. So that
# requires further investigation.
"--preserve-env=PATH",
# Also preserve any caller specified env vars
f"--preserve-env={to_preserve}",
]
c += cmd
# Ugly workaround for mypy not being able to infer empty dict
empty: Dict[str, str] = {}
subprocess.run(
c,
cwd=cwd if cwd else root(),
check=True,
# Explicitly clear the environment so that any commands run
# inside the nix environment cannot accidentally depend on
# host environment. There are known very-hard-to-debug issues
# that occur in CI when the envirionment escapes.
env=env if env else empty,
)
def configure():
"""Run cmake configure step"""
# fmt: off
c = [
"cmake",
"-B",
BUILD_DIR,
# Dynamic configs
f"-DCMAKE_C_COMPILER={CC}",
f"-DCMAKE_CXX_COMPILER={CXX}",
f"-DCMAKE_BUILD_TYPE={CMAKE_BUILD_TYPE}",
f"-DBUILD_ASAN={RUN_MEMLEAK_TEST}",
# Static configs
f"-DUSE_SYSTEM_BPF_BCC=1",
f"-DCMAKE_VERBOSE_MAKEFILE=1",
f"-DBUILD_TESTING=1",
f"-DENABLE_SKB_OUTPUT=1",
f"-DALLOW_UNSAFE_PROBE=0",
]
# fmt: on
shell(c)
def build():
"""Build everything"""
cpus = multiprocessing.cpu_count()
shell(["make", "-C", BUILD_DIR, "-j", str(cpus)])
def test_one(name: str, cond: Callable[[], bool], fn: Callable[[], None]) -> TestResult:
"""Runs a single test suite and returns the result"""
status = TestStatus.PASSED
if cond():
print(f"\n======= {name} ======")
try:
fn()
except subprocess.CalledProcessError as e:
status = TestStatus.FAILED
else:
status = TestStatus.SKIPPED
return TestResult(test_name=name, status=status)
def tests_finish(results: List[TestResult]):
"""Process test results and output status"""
skipped = sum(1 for r in results if r.status == TestStatus.SKIPPED)
passed = sum(1 for r in results if r.status == TestStatus.PASSED)
failed = sum(1 for r in results if r.status == TestStatus.FAILED)
failed_names = [r.test_name for r in results if r.status == TestStatus.FAILED]
total_run = passed + failed
output = StringIO()
print("\n======= Results =======", file=output)
if skipped:
print(f"{skipped} suite(s) skipped", file=output)
if failed:
print(f"{failed}/{total_run} suites(s) failed: {failed_names}", file=output)
else:
print(f"{passed}/{total_run} suites(s) passed", file=output)
print("=======================", file=output)
if failed:
raise RuntimeError(output.getvalue())
else:
print(output.getvalue())
def test():
"""
Run all requested tests
Note we're not using `ctest` b/c it's kinda a pain to work with.
We don't use any of it's advanced features but still suffer from
it's limitations, like not being able to flexibly configure test
runners (we need `sudo` for some suites). It also buffers output
rather oddly.
"""
results = []
results.append(
test_one(
"bpftrace_test",
lambda: truthy(RUN_TESTS),
lambda: shell(["./tests/bpftrace_test"], cwd=Path(BUILD_DIR)),
)
)
results.append(
test_one(
"runtime-tests.sh",
lambda: truthy(RUN_TESTS),
lambda: shell(
["./tests/runtime-tests.sh"],
as_root=True,
cwd=Path(BUILD_DIR),
env={"RUNTIME_TEST_DISABLE": RUNTIME_TEST_DISABLE},
),
)
)
results.append(
test_one(
"tools-parsing-test.sh",
lambda: truthy(RUN_TESTS),
lambda: shell(
[
"./tests/tools-parsing-test.sh",
],
as_root=True,
cwd=Path(BUILD_DIR),
env={
"TOOLS_TEST_OLDVERSION": TOOLS_TEST_OLDVERSION,
"TOOLS_TEST_DISABLE": TOOLS_TEST_DISABLE,
},
),
)
)
results.append(
test_one(
"memleak-tests.sh.sh",
lambda: truthy(RUN_MEMLEAK_TEST),
lambda: shell(
["./tests/memleak-tests.sh"], as_root=True, cwd=Path(BUILD_DIR)
),
)
)
tests_finish(results)
def main():
configure()
build()
test()
if __name__ == "__main__":
main()