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.
reviewable/pr638/r3
Andrew Werner 2 years ago
parent 5c86b7ee95
commit eda65025b7

@ -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, BuildEbpfOptions};
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
pub struct Options { pub struct Options {
@ -18,14 +18,20 @@ pub struct Options {
pub runner: String, pub runner: String,
/// libbpf directory /// libbpf directory
#[clap(long, action)] #[clap(long, action)]
pub libbpf_dir: String, pub libbpf_dir: PathBuf,
/// Arguments to pass to your application /// Arguments to pass to your application
#[clap(name = "args", last = true)] #[clap(name = "args", last = true)]
pub run_args: Vec<String>, pub run_args: Vec<String>,
} }
/// Build the project /// Configures building the integration test binary.
fn build(opts: &Options) -> Result<(), anyhow::Error> { pub struct BuildOptions {
pub release: bool,
}
/// Build the project. Returns the path to the binary that was built.
pub fn build(opts: BuildOptions) -> Result<std::path::PathBuf, anyhow::Error> {
let BuildOptions { release } = opts;
let mut args = vec!["build"]; let mut args = vec!["build"];
if opts.release { if opts.release {
args.push("--release") args.push("--release")
@ -37,28 +43,35 @@ fn build(opts: &Options) -> Result<(), anyhow::Error> {
.status() .status()
.expect("failed to build userspace"); .expect("failed to build userspace");
assert!(status.success()); assert!(status.success());
Ok(()) let profile = if release { "release" } else { "debug" };
let bin_path = format!("target/{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> {
let Options {
bpf_target,
release,
runner,
libbpf_dir,
run_args,
} = opts;
// build our ebpf program followed by our application // build our ebpf program followed by our application
build_ebpf(BuildOptions { build_ebpf(BuildEbpfOptions {
target: opts.bpf_target, target: bpf_target,
libbpf_dir: PathBuf::from(&opts.libbpf_dir), libbpf_dir,
}) })
.context("Error while building eBPF program")?; .context("Error while building eBPF program")?;
build(&opts).context("Error while building userspace application")?; let bin_path =
// profile we are building (release or debug) build(BuildOptions { release }).context("Error while building userspace application")?;
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<_> = 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<_> = 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