-
Notifications
You must be signed in to change notification settings - Fork 308
/
Copy pathrun.rs
599 lines (553 loc) · 25.2 KB
/
run.rs
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
use std::{
ffi::OsString,
fmt::Write as _,
fs::{copy, create_dir_all, OpenOptions},
io::{BufRead as _, BufReader, Write as _},
path::{Path, PathBuf},
process::{Child, ChildStdin, Command, Output, Stdio},
sync::{Arc, Mutex},
thread,
};
use anyhow::{anyhow, bail, Context as _, Result};
use base64::engine::Engine as _;
use cargo_metadata::{Artifact, CompilerMessage, Message, Target};
use clap::Parser;
use xtask::{Errors, AYA_BUILD_INTEGRATION_BPF};
#[derive(Parser)]
enum Environment {
/// Runs the integration tests locally.
Local {
/// The command used to wrap your application.
#[clap(short, long, default_value = "sudo -E")]
runner: String,
},
/// Runs the integration tests in a VM.
VM {
/// The cache directory in which to store intermediate artifacts.
#[clap(long)]
cache_dir: PathBuf,
/// The Github API token to use if network requests to Github are made.
///
/// This may be required if Github rate limits are exceeded.
#[clap(long)]
github_api_token: Option<String>,
/// The kernel images to use.
///
/// You can download some images with:
///
/// wget --accept-regex '.*/linux-image-[0-9\.-]+-cloud-.*-unsigned*' \
/// --recursive http://ftp.us.debian.org/debian/pool/main/l/linux/
///
/// You can then extract them with:
///
/// find . -name '*.deb' -print0 \
/// | xargs -0 -I {} sh -c "dpkg --fsys-tarfile {} \
/// | tar --wildcards --extract '*boot*' '**/modules/*' --file -"
///
/// `*boot*` is used to extract the kernel image and config.
/// `**/modules/*` is used to extract the kernel modules.
/// Modules are required since not all parts of the kernel we want to
/// test are built-in. See `grep "=m" boot/config-6.10.9-cloud-amd64`.
#[clap(required = true)]
kernel_image: Vec<PathBuf>,
},
}
#[derive(Parser)]
pub struct Options {
#[clap(subcommand)]
environment: Environment,
/// Arguments to pass to your application.
#[clap(global = true, last = true)]
run_args: Vec<OsString>,
}
pub fn build<F>(target: Option<&str>, f: F) -> Result<Vec<(String, PathBuf)>>
where
F: FnOnce(&mut Command) -> &mut Command,
{
// Always use rust-lld in case we're cross-compiling.
let mut cmd = Command::new("cargo");
cmd.args(["build", "--message-format=json"]);
if let Some(target) = target {
let config = format!("target.{target}.linker = \"rust-lld\"");
cmd.args(["--target", target, "--config", &config]);
}
f(&mut cmd);
let mut child = cmd
.stdout(Stdio::piped())
.spawn()
.with_context(|| format!("failed to spawn {cmd:?}"))?;
let Child { stdout, .. } = &mut child;
let stdout = stdout.take().unwrap();
let stdout = BufReader::new(stdout);
let mut executables = Vec::new();
for message in Message::parse_stream(stdout) {
#[allow(clippy::collapsible_match)]
match message.context("valid JSON")? {
Message::CompilerArtifact(Artifact {
executable,
target: Target { name, .. },
..
}) => {
if let Some(executable) = executable {
executables.push((name, executable.into()));
}
}
Message::CompilerMessage(CompilerMessage { message, .. }) => {
for line in message.rendered.unwrap_or_default().split('\n') {
println!("cargo:warning={line}");
}
}
Message::TextLine(line) => {
println!("{line}");
}
_ => {}
}
}
let status = child
.wait()
.with_context(|| format!("failed to wait for {cmd:?}"))?;
if status.code() != Some(0) {
bail!("{cmd:?} failed: {status:?}")
}
Ok(executables)
}
/// Build and run the project.
pub fn run(opts: Options) -> Result<()> {
let Options {
environment,
run_args,
} = opts;
type Binary = (String, PathBuf);
fn binaries(target: Option<&str>) -> Result<Vec<(&str, Vec<Binary>)>> {
["dev", "release"]
.into_iter()
.map(|profile| {
let binaries = build(target, |cmd| {
cmd.env(AYA_BUILD_INTEGRATION_BPF, "true").args([
"--package",
"integration-test",
"--tests",
"--profile",
profile,
])
})?;
anyhow::Ok((profile, binaries))
})
.collect()
}
// Use --test-threads=1 to prevent tests from interacting with shared
// kernel state due to the lack of inter-test isolation.
let default_args = [OsString::from("--test-threads=1")];
let run_args = default_args.iter().chain(run_args.iter());
match environment {
Environment::Local { runner } => {
let mut args = runner.trim().split_terminator(' ');
let runner = args.next().ok_or(anyhow!("no first argument"))?;
let args = args.collect::<Vec<_>>();
let binaries = binaries(None)?;
let mut failures = String::new();
for (profile, binaries) in binaries {
for (name, binary) in binaries {
let mut cmd = Command::new(runner);
let cmd = cmd.args(args.iter()).arg(binary).args(run_args.clone());
println!("{profile}:{name} running {cmd:?}");
let status = cmd
.status()
.with_context(|| format!("failed to run {cmd:?}"))?;
if status.code() != Some(0) {
writeln!(&mut failures, "{profile}:{name} failed: {status:?}")
.context("String write failed")?
}
}
}
if failures.is_empty() {
Ok(())
} else {
Err(anyhow!("failures:\n{}", failures))
}
}
Environment::VM {
cache_dir,
github_api_token,
kernel_image,
} => {
// The user has asked us to run the tests on a VM. This is involved; strap in.
//
// We need tools to build the initramfs; we use gen_init_cpio from the Linux repository,
// taking care to cache it.
//
// Then we iterate the kernel images, using the `file` program to guess the target
// architecture. We then build the init program and our test binaries for that
// architecture, and use gen_init_cpio to build an initramfs containing the test
// binaries. We're almost ready to run the VM.
//
// We consult our OS, our architecture, and the target architecture to determine if
// hardware acceleration is available, and then start QEMU with the provided kernel
// image and the initramfs we built.
//
// We consume the output of QEMU, looking for the output of our init program. This is
// the only way to distinguish success from failure. We batch up the errors across all
// VM images and report to the user. The end.
create_dir_all(&cache_dir).context("failed to create cache dir")?;
let gen_init_cpio = cache_dir.join("gen_init_cpio");
if !gen_init_cpio
.try_exists()
.context("failed to check existence of gen_init_cpio")?
{
// TODO(https://github.com/oxidecomputer/third-party-api-clients/issues/96): Use ETag-based caching.
let client = octorust::Client::new(
String::from("aya-xtask-integration-test-run"),
github_api_token.map(octorust::auth::Credentials::Token),
)?;
let octorust::Response {
status: _,
headers: _,
body: octorust::types::ContentFile { mut content, .. },
} = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(client.repos().get_content_file(
"torvalds",
"linux",
"usr/gen_init_cpio.c",
"master",
))
.context("failed to download gen_init_cpio.c")?;
// Github very helpfully wraps their base64 at 10 columns /s.
content.retain(|c| !c.is_whitespace());
let content = base64::engine::general_purpose::STANDARD
.decode(content)
.context("failed to decode gen_init_cpio.c")?;
let mut clang = Command::new("clang");
clang
.args(["-g", "-O2", "-x", "c", "-", "-o"])
.arg(&gen_init_cpio);
let mut child = clang
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.with_context(|| format!("failed to spawn {clang:?}"))?;
let Child { stdin, .. } = &mut child;
let mut stdin = stdin.take().unwrap();
stdin
.write_all(&content)
.with_context(|| format!("failed to write to {clang:?} stdin"))?;
std::mem::drop(stdin); // Send EOF.
let output = child
.wait_with_output()
.with_context(|| format!("failed to wait for {clang:?}"))?;
let Output { status, .. } = &output;
if status.code() != Some(0) {
bail!("{clang:?} failed: {output:?}")
}
}
let mut errors = Vec::new();
for kernel_image in kernel_image {
// Guess the guest architecture.
let mut cmd = Command::new("file");
let output = cmd
.arg("--brief")
.arg(&kernel_image)
.output()
.with_context(|| format!("failed to run {cmd:?}"))?;
let Output { status, .. } = &output;
if status.code() != Some(0) {
bail!("{cmd:?} failed: {output:?}")
}
let Output { stdout, .. } = output;
// Now parse the output of the file command, which looks something like
//
// - Linux kernel ARM64 boot executable Image, little-endian, 4K pages
//
// - Linux kernel x86 boot executable bzImage, version 6.1.0-10-cloud-amd64 [..]
let stdout = String::from_utf8(stdout)
.with_context(|| format!("invalid UTF-8 in {cmd:?} stdout"))?;
let (_, stdout) = stdout
.split_once("Linux kernel")
.ok_or_else(|| anyhow!("failed to parse {cmd:?} stdout: {stdout}"))?;
let (guest_arch, _) = stdout
.split_once("boot executable")
.ok_or_else(|| anyhow!("failed to parse {cmd:?} stdout: {stdout}"))?;
let guest_arch = guest_arch.trim();
let (guest_arch, machine, cpu, console) = match guest_arch {
"ARM64" => ("aarch64", Some("virt"), Some("max"), "ttyAMA0"),
"x86" => ("x86_64", None, None, "ttyS0"),
guest_arch => (guest_arch, None, None, "ttyS0"),
};
let target = format!("{guest_arch}-unknown-linux-musl");
let version = kernel_image
.file_name()
.and_then(|name| name.to_str())
.ok_or_else(|| anyhow!("kernel_image file_name failed"))?;
let version = version.trim_start_matches("vmlinuz-");
let modules_dir = kernel_image
.parent()
.expect("unable to get parent directory from kernel_image")
.join("../usr/lib/modules/")
.join(version)
.canonicalize()
.with_context(|| format!("failed to canonicalize {version}"))?;
let binaries = binaries(Some(&target))?;
let tmp_dir = tempfile::tempdir().context("tempdir failed")?;
let initrd_image = tmp_dir.path().join("qemu-initramfs.img");
let initrd_image_file = OpenOptions::new()
.create_new(true)
.write(true)
.open(&initrd_image)
.with_context(|| {
format!("failed to create {} for writing", initrd_image.display())
})?;
let mut gen_init_cpio = Command::new(&gen_init_cpio);
let mut gen_init_cpio_child = gen_init_cpio
.arg("-")
.stdin(Stdio::piped())
.stdout(initrd_image_file)
.spawn()
.with_context(|| format!("failed to spawn {gen_init_cpio:?}"))?;
let Child { stdin, .. } = &mut gen_init_cpio_child;
let mut stdin = stdin.take().unwrap();
// Send input into gen_init_cpio which looks something like
//
// file /init path-to-init 755 0 0
// dir /bin 755 0 0
// file /bin/foo path-to-foo 755 0 0
// file /bin/bar path-to-bar 755 0 0
let mut input = String::from(
"dir /bin 755 0 0\n\
dir /sbin 755 0 0\n\
dir /lib 755 0 0\n\
dir /lib/modules 755 0 0\n",
);
build(Some(&target), |cmd| {
cmd.args(["--package", "test-distro", "--profile", "release"])
})
.context("building test-distro program failed")?
.into_iter()
.for_each(|(name, path)| {
if name == "init" {
input.push_str("file /init ");
input.push_str(&path.to_string_lossy());
input.push_str(" 755 0 0\n");
} else {
let out_path = Path::new("/sbin").join(&name);
input.push_str("file ");
input.push_str(out_path.to_str().unwrap());
input.push(' ');
input.push_str(&path.to_string_lossy());
input.push_str(" 755 0 0\n");
}
});
// At this point we need to make a slight detour!
// Preparing the `modules.alias` file inside the VM as part of
// `/init` is slow. It's faster to prepare it here, and it can
// also be reused later.
if !(modules_dir.join("modules.alias").exists()) {
Command::new("cargo")
.args([
"run",
"--package",
"test-distro",
"--bin",
"depmod",
"--release",
"--",
"-b",
&modules_dir.to_string_lossy(),
])
.status()
.context("failed to run depmod")?;
}
// Now our modules.alias file is built, we can recursively
// walk the modules directory and add all the files to the
// initramfs.
fn parse_dir(acc: &mut String, input_dir: &Path, output_dir: &Path) -> Result<()> {
for entry in std::fs::read_dir(input_dir).context("read_dir failed")? {
let entry = entry.context("read_dir failed")?;
let path = entry.path();
let metadata = entry.metadata().context("metadata failed")?;
let file_type = metadata.file_type();
let file_name = path
.file_name()
.ok_or_else(|| anyhow!("file_name failed"))?;
let file_name =
file_name.to_str().ok_or_else(|| anyhow!("to_str failed"))?;
if file_type.is_dir() {
let out_path = output_dir.join(file_name);
acc.push_str("dir ");
acc.push_str(&out_path.to_string_lossy());
acc.push_str(" 755 0 0\n");
parse_dir(acc, &path, &output_dir.join(file_name))?;
} else if file_type.is_file() {
let out_path = output_dir.join(file_name);
acc.push_str("file ");
acc.push_str(&out_path.to_string_lossy());
acc.push(' ');
acc.push_str(&path.to_string_lossy());
acc.push_str(" 644 0 0\n");
}
}
Ok(())
}
parse_dir(&mut input, &modules_dir, Path::new("/lib/modules"))?;
for (profile, binaries) in binaries {
for (name, binary) in binaries {
let name = format!("{}-{}", profile, name);
let path = tmp_dir.path().join(&name);
copy(&binary, &path).with_context(|| {
format!("copy({}, {}) failed", binary.display(), path.display())
})?;
let out_path = Path::new("/bin").join(&name);
input.push_str("file ");
input.push_str(out_path.to_str().unwrap());
input.push(' ');
input.push_str(&path.to_string_lossy());
input.push_str(" 755 0 0\n");
}
}
stdin.write_all(input.as_bytes()).expect("write");
// Must explicitly close to signal EOF.
drop(stdin);
let output = gen_init_cpio_child
.wait_with_output()
.with_context(|| format!("failed to wait for {gen_init_cpio:?}"))?;
let Output { status, .. } = &output;
if status.code() != Some(0) {
bail!("{gen_init_cpio:?} failed: {output:?}")
}
let mut qemu = Command::new(format!("qemu-system-{guest_arch}"));
if let Some(machine) = machine {
qemu.args(["-machine", machine]);
}
if let Some(cpu) = cpu {
qemu.args(["-cpu", cpu]);
}
for accel in ["kvm", "hvf", "tcg"] {
qemu.args(["-accel", accel]);
}
let console = OsString::from(console);
let mut kernel_args = std::iter::once(("console", &console))
.chain(run_args.clone().map(|run_arg| ("init.arg", run_arg)))
.enumerate()
.fold(OsString::new(), |mut acc, (i, (k, v))| {
if i != 0 {
acc.push(" ");
}
acc.push(k);
acc.push("=");
acc.push(v);
acc
});
// We sometimes see kernel panics containing:
//
// [ 0.064000] Kernel panic - not syncing: IO-APIC + timer doesn't work! Boot with apic=debug and send a report. Then try booting with the 'noapic' option.
//
// Heed the advice and boot with noapic. We don't know why this happens.
kernel_args.push(" noapic");
kernel_args.push(" init=/sbin/init");
qemu.args(["-no-reboot", "-nographic", "-m", "512M", "-smp", "2"])
.arg("-append")
.arg(kernel_args)
.arg("-kernel")
.arg(&kernel_image)
.arg("-initrd")
.arg(&initrd_image);
let mut qemu_child = qemu
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.with_context(|| format!("failed to spawn {qemu:?}"))?;
let Child {
stdin,
stdout,
stderr,
..
} = &mut qemu_child;
let stdin = stdin.take().unwrap();
let stdin = Arc::new(Mutex::new(stdin));
let stdout = stdout.take().unwrap();
let stdout = BufReader::new(stdout);
let stderr = stderr.take().unwrap();
let stderr = BufReader::new(stderr);
const TERMINATE_AFTER_COUNT: &[(&str, usize)] = &[
("end Kernel panic", 0),
("rcu: RCU grace-period kthread stack dump:", 0),
("watchdog: BUG: soft lockup", 1),
];
let mut counts = [0; TERMINATE_AFTER_COUNT.len()];
let mut terminate_if_kernel_hang =
move |line: &str, stdin: &Arc<Mutex<ChildStdin>>| -> anyhow::Result<()> {
if let Some(i) = TERMINATE_AFTER_COUNT
.iter()
.position(|(marker, _)| line.contains(marker))
{
counts[i] += 1;
let (marker, max) = TERMINATE_AFTER_COUNT[i];
if counts[i] > max {
println!("{marker} detected > {max} times; terminating QEMU");
let mut stdin = stdin.lock().unwrap();
stdin
.write_all(&[0x01, b'x'])
.context("failed to write to stdin")?;
println!("waiting for QEMU to terminate");
}
}
Ok(())
};
let stderr = {
let stdin = stdin.clone();
thread::Builder::new()
.spawn(move || {
for line in stderr.lines() {
let line = line.context("failed to read line from stderr")?;
eprintln!("{}", line);
terminate_if_kernel_hang(&line, &stdin)?;
}
anyhow::Ok(())
})
.unwrap()
};
let mut outcome = None;
for line in stdout.lines() {
let line = line.context("failed to read line from stdout")?;
println!("{}", line);
terminate_if_kernel_hang(&line, &stdin)?;
// The init program will print "init: success" or "init: failure" to indicate
// the outcome of running the binaries it found in /bin.
if let Some(line) = line.strip_prefix("init: ") {
let previous = match line {
"success" => outcome.replace(Ok(())),
"failure" => outcome.replace(Err(())),
line => bail!("unexpected init output: {}", line),
};
if let Some(previous) = previous {
bail!("multiple exit status: previous={previous:?}, current={line}");
}
}
}
let output = qemu_child
.wait_with_output()
.with_context(|| format!("failed to wait for {qemu:?}"))?;
let Output { status, .. } = &output;
if status.code() != Some(0) {
bail!("{qemu:?} failed: {output:?}")
}
stderr.join().unwrap()?;
let outcome = outcome.ok_or(anyhow!("init did not exit"))?;
match outcome {
Ok(()) => {}
Err(()) => {
errors.push(anyhow!("VM binaries failed on {}", kernel_image.display()))
}
}
}
if errors.is_empty() {
Ok(())
} else {
Err(Errors::new(errors).into())
}
}
}
}