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 committed by Snir Sheriber
parent 0fd747c752
commit aa4a3053a0

View File

@ -1557,6 +1557,7 @@ mod tests {
use std::os::unix::fs::MetadataExt;
use std::os::unix::io::AsRawFd;
use tempfile::tempdir;
use tokio::process::Command;
macro_rules! sl {
() => {
@ -1564,12 +1565,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]
async fn test_execute_hook() {
let xargs = which("xargs").await;
execute_hook(
&slog_scope::logger(),
&Hook {
path: "/usr/bin/xargs".to_string(),
path: xargs,
args: vec![],
env: vec![],
timeout: None,
@ -1589,10 +1605,12 @@ mod tests {
#[tokio::test]
async fn test_execute_hook_with_timeout() {
let sleep = which("sleep").await;
let res = execute_hook(
&slog_scope::logger(),
&Hook {
path: "/usr/bin/sleep".to_string(),
path: sleep,
args: vec!["2".to_string()],
env: vec![],
timeout: Some(1),