agent: enhance tests of execute_hook

Use which to find the full path of exe before run execute_hook
to avoid error: 'No such file or directory'

Fixes: #2172

Signed-off-by: Tim Zhang <tim@hyper.sh>
This commit is contained in:
Tim Zhang 2021-07-02 11:39:58 +08:00
parent 7d37fbfdfb
commit 55c5c871d2

View File

@ -1554,6 +1554,7 @@ mod tests {
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
use tempfile::tempdir; use tempfile::tempdir;
use tokio::process::Command;
macro_rules! sl { macro_rules! sl {
() => { () => {
@ -1561,12 +1562,27 @@ mod tests {
}; };
} }
async fn which(cmd: &str) -> String {
let output: std::process::Output = Command::new("which")
.arg(cmd)
.output()
.await
.expect("which command failed to run");
match String::from_utf8(output.stdout) {
Ok(v) => v.trim_end_matches('\n').to_string(),
Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
}
}
#[tokio::test] #[tokio::test]
async fn test_execute_hook() { async fn test_execute_hook() {
let xargs = which("xargs").await;
execute_hook( execute_hook(
&slog_scope::logger(), &slog_scope::logger(),
&Hook { &Hook {
path: "/usr/bin/xargs".to_string(), path: xargs,
args: vec![], args: vec![],
env: vec![], env: vec![],
timeout: None, timeout: None,
@ -1586,10 +1602,12 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_execute_hook_with_timeout() { async fn test_execute_hook_with_timeout() {
let sleep = which("sleep").await;
let res = execute_hook( let res = execute_hook(
&slog_scope::logger(), &slog_scope::logger(),
&Hook { &Hook {
path: "/usr/bin/sleep".to_string(), path: sleep,
args: vec!["2".to_string()], args: vec!["2".to_string()],
env: vec![], env: vec![],
timeout: Some(1), timeout: Some(1),