Skip to content

Commit

Permalink
xtask: cargo xtask {sudo,unshare} {run,test}
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Jan 2, 2025
1 parent 9df3545 commit 3dafa72
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Just use your general Cargo workflow:
```console
$ cargo build
$ cargo run
$ cargo xtask sudo run -- run # short for:
$ cargo --config "target.'cfg(target_os = \"<your OS>\")'.runner = 'sudo -E'" run -- run
```

To generate manpages and shell autocompletions into target/assets directory, run:
Expand All @@ -96,15 +98,15 @@ $ cargo xtask gen
Intergration tests involve exchanging information with BIRD and modifying kernel network interface. For example, on Linux, install BIRD (version 2.x or above) and use [`unshare(1)`](https://www.man7.org/linux/man-pages/man1/unshare.1.html) to run the full sets of tests:

```console
$ cargo xtask unshare-test # shortcut for:
$ cargo xtask unshare test # shortcut for:
$ cargo --config "target.'cfg(target_os = \"linux\")'.runner = 'unshare -rn'" test
```

If `unshare` or similar unprivileged isolation methods are unavailable, be careful when running tests with root, since modifications to host network may not be completely reverted in test code:

```console
$ cargo xtask sudo-test # shortcut for:
$ cargo --config "target.'cfg(target_os = \"linux\")'.runner = 'sudo -E'" test
$ cargo xtask sudo test # shortcut for:
$ cargo --config "target.'cfg(target_os = \"<your OS>\")'.runner = 'sudo -E'" test
```

Or, skip integration tests and run only unit tests:
Expand Down
32 changes: 15 additions & 17 deletions xtask/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,47 @@ use std::process::Command;
enum Cli {
/// Generate manpages and shell autocompletions into target/assets.
Gen,
/// Run tests with `unshare -rn`
UnshareTest {
/// Run command with `unshare -rn`
Unshare {
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
_args: Vec<String>,
},
/// Run tests with `sudo -E`
SudoTest {
/// Run command with `sudo -E`
Sudo {
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
_args: Vec<String>,
},
}

fn main() {
let args = Cli::parse();
match args {
match Cli::parse() {
Cli::Gen => {
Command::new(env!("CARGO")).args(["run", "--features=__gen"]).status().unwrap();
}

Cli::UnshareTest { _args } => {
#[cfg(not(target_os = "linux"))]
eprintln!("Unshare not supported, running tests as current user");

#[cfg(target_os = "linux")]
Cli::Unshare { _args } => {
Command::new(env!("CARGO"))
.args([
"--config",
"target.'cfg(target_os = \"linux\")'.runner = 'unshare -rn'",
"test",
])
.args(["--config", "target.'cfg(target_os = \"linux\")'.runner = 'unshare -rn'"])
.args(std::env::args().skip(2))
.status()
.unwrap();
}

Cli::SudoTest { _args } => {
#[cfg(not(target_os = "linux"))]
Cli::Unshare { _args } => {
eprintln!("Unshare not supported, running tests as current user");
Command::new(env!("CARGO")).args(std::env::args().skip(2)).status().unwrap();
}

Cli::Sudo { _args } => {
Command::new(env!("CARGO"))
.args([
"--config",
&format!(
"target.'cfg(target_os = \"{}\")'.runner = 'sudo -E'",
std::env::consts::OS
),
"test",
])
.args(std::env::args().skip(2))
.status()
Expand Down

0 comments on commit 3dafa72

Please sign in to comment.