xtask: refactor integration test

Rename the module to `integration_test`, make a `BuildOptions` struct
and export the `build` function. Add control over the target, and have
`build` return the path to the compiled binary. Construct the various
`BuildOptions` structs from the `Options` struct.
reviewable/pr638/r1
Andrew Werner 2 years ago
parent 5c86b7ee95
commit 5188b6d07f

@ -3,7 +3,7 @@ use std::{os::unix::process::CommandExt, path::PathBuf, process::Command};
use anyhow::Context as _; use anyhow::Context as _;
use clap::Parser; use clap::Parser;
use crate::build_ebpf::{build_ebpf, Architecture, BuildEbpfOptions as BuildOptions}; use crate::build_ebpf::{build_ebpf, Architecture};
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct Options { pub struct Options {
@ -24,41 +24,73 @@ pub struct Options {
pub run_args: Vec<String>, pub run_args: Vec<String>,
} }
/// Build the project impl Options {
fn build(opts: &Options) -> Result<(), anyhow::Error> { fn build_options(&self) -> BuildOptions {
let Self { release, .. } = self;
BuildOptions {
release: *release,
target: None,
}
}
fn build_ebpf_options(&self) -> crate::build_ebpf::BuildEbpfOptions {
let Self {
bpf_target,
libbpf_dir,
..
} = self;
crate::build_ebpf::BuildEbpfOptions {
target: *bpf_target,
libbpf_dir: PathBuf::from(libbpf_dir),
}
}
}
/// Configures building the integration test binary.
pub struct BuildOptions {
pub release: bool,
pub target: Option<String>,
}
/// Build the project. Returns the path to the binary that was built.
pub fn build(opts: BuildOptions) -> Result<std::path::PathBuf, anyhow::Error> {
let mut args = vec!["build"]; let mut args = vec!["build"];
if opts.release { if opts.release {
args.push("--release") args.push("--release")
} }
args.push("-p"); args.push("-p");
args.push("integration-test"); args.push("integration-test");
let target_path = if let Some(target) = &opts.target {
args.push("--target");
args.push(target);
format!("{target}/")
} else {
String::new()
};
let status = Command::new("cargo") let status = Command::new("cargo")
.args(&args) .args(&args)
.status() .status()
.expect("failed to build userspace"); .expect("failed to build userspace");
assert!(status.success()); assert!(status.success());
Ok(()) let profile = if opts.release { "release" } else { "debug" };
let bin_path = format!("target/{target_path}{profile}/integration-test");
Ok(PathBuf::from(bin_path))
} }
/// Build and run the project /// Build and run the project
pub fn run(opts: Options) -> Result<(), anyhow::Error> { pub fn run(opts: Options) -> Result<(), anyhow::Error> {
// build our ebpf program followed by our application // build our ebpf program followed by our application
build_ebpf(BuildOptions { build_ebpf(opts.build_ebpf_options()).context("Error while building eBPF program")?;
target: opts.bpf_target, let bin_path =
libbpf_dir: PathBuf::from(&opts.libbpf_dir), build(opts.build_options()).context("Error while building userspace application")?;
})
.context("Error while building eBPF program")?;
build(&opts).context("Error while building userspace application")?;
// profile we are building (release or debug)
let profile = if opts.release { "release" } else { "debug" };
let bin_path = format!("target/{profile}/integration-test");
// arguments to pass to the application // arguments to pass to the application
let mut run_args: Vec<_> = opts.run_args.iter().map(String::as_str).collect(); let mut run_args: Vec<_> = opts.run_args.iter().map(String::as_str).collect();
// configure args // configure args
let mut args: Vec<_> = opts.runner.trim().split_terminator(' ').collect(); let mut args: Vec<_> = opts.runner.trim().split_terminator(' ').collect();
args.push(bin_path.as_str()); args.push(bin_path.to_str().expect("Invalid binary path"));
args.append(&mut run_args); args.append(&mut run_args);
// spawn the command // spawn the command

@ -2,7 +2,8 @@ mod build_ebpf;
mod build_test; mod build_test;
mod codegen; mod codegen;
mod docs; mod docs;
mod run; mod integration_test;
pub(crate) mod utils; pub(crate) mod utils;
use std::process::exit; use std::process::exit;
@ -20,7 +21,7 @@ enum Command {
Docs, Docs,
BuildIntegrationTest(build_test::Options), BuildIntegrationTest(build_test::Options),
BuildIntegrationTestEbpf(build_ebpf::BuildEbpfOptions), BuildIntegrationTestEbpf(build_ebpf::BuildEbpfOptions),
IntegrationTest(run::Options), IntegrationTest(integration_test::Options),
} }
fn main() { fn main() {
@ -32,7 +33,7 @@ fn main() {
Docs => docs::docs(), Docs => docs::docs(),
BuildIntegrationTest(opts) => build_test::build_test(opts), BuildIntegrationTest(opts) => build_test::build_test(opts),
BuildIntegrationTestEbpf(opts) => build_ebpf::build_ebpf(opts), BuildIntegrationTestEbpf(opts) => build_ebpf::build_ebpf(opts),
IntegrationTest(opts) => run::run(opts), IntegrationTest(opts) => integration_test::run(opts),
}; };
if let Err(e) = ret { if let Err(e) = ret {

Loading…
Cancel
Save