mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #3518 from jhscheer/run_ucmd_as_root
test/util: improve `run_ucmd_as_root` for CICD
This commit is contained in:
commit
0ebd9c9391
1 changed files with 53 additions and 34 deletions
|
@ -1370,6 +1370,8 @@ pub fn expected_result(ts: &TestScenario, args: &[&str]) -> std::result::Result<
|
||||||
/// To check if i) non-interactive sudo is possible and ii) if sudo works, this runs:
|
/// To check if i) non-interactive sudo is possible and ii) if sudo works, this runs:
|
||||||
/// 'sudo -E --non-interactive whoami' first.
|
/// 'sudo -E --non-interactive whoami' first.
|
||||||
///
|
///
|
||||||
|
/// This return an `Err()` if run inside CICD because there's no 'sudo'.
|
||||||
|
///
|
||||||
/// Example:
|
/// Example:
|
||||||
///
|
///
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
|
@ -1381,7 +1383,7 @@ pub fn expected_result(ts: &TestScenario, args: &[&str]) -> std::result::Result<
|
||||||
/// if let Ok(result) = run_ucmd_as_root(&ts, &[]) {
|
/// if let Ok(result) = run_ucmd_as_root(&ts, &[]) {
|
||||||
/// result.stdout_is(expected);
|
/// result.stdout_is(expected);
|
||||||
/// } else {
|
/// } else {
|
||||||
/// print!("TEST SKIPPED");
|
/// println!("TEST SKIPPED");
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///```
|
///```
|
||||||
|
@ -1390,29 +1392,37 @@ pub fn run_ucmd_as_root(
|
||||||
ts: &TestScenario,
|
ts: &TestScenario,
|
||||||
args: &[&str],
|
args: &[&str],
|
||||||
) -> std::result::Result<CmdResult, String> {
|
) -> std::result::Result<CmdResult, String> {
|
||||||
// Apparently CICD environment has no `sudo`?
|
if !is_ci() {
|
||||||
if ts
|
// check if we can run 'sudo'
|
||||||
.cmd_keepenv("sudo")
|
log_info("run", "sudo -E --non-interactive whoami");
|
||||||
.env("LC_ALL", "C")
|
match Command::new("sudo")
|
||||||
.arg("-E")
|
|
||||||
.arg("--non-interactive")
|
|
||||||
.arg("whoami")
|
|
||||||
.run()
|
|
||||||
.stdout_str()
|
|
||||||
.trim()
|
|
||||||
!= "root"
|
|
||||||
{
|
|
||||||
Err("\"sudo whoami\" didn't return \"root\"".to_string())
|
|
||||||
} else {
|
|
||||||
Ok(ts
|
|
||||||
.cmd_keepenv("sudo")
|
|
||||||
.env("LC_ALL", "C")
|
.env("LC_ALL", "C")
|
||||||
.arg("-E")
|
.args(&["-E", "--non-interactive", "whoami"])
|
||||||
.arg("--non-interactive")
|
.output()
|
||||||
.arg(&ts.bin_path)
|
{
|
||||||
.arg(&ts.util_name)
|
Ok(output) if String::from_utf8_lossy(&output.stdout).eq("root\n") => {
|
||||||
.args(args)
|
// we can run sudo and we're root
|
||||||
.run())
|
// run ucmd as root:
|
||||||
|
Ok(ts
|
||||||
|
.cmd_keepenv("sudo")
|
||||||
|
.env("LC_ALL", "C")
|
||||||
|
.arg("-E")
|
||||||
|
.arg("--non-interactive")
|
||||||
|
.arg(&ts.bin_path)
|
||||||
|
.arg(&ts.util_name)
|
||||||
|
.args(args)
|
||||||
|
.run())
|
||||||
|
}
|
||||||
|
Ok(output)
|
||||||
|
if String::from_utf8_lossy(&output.stderr).eq("sudo: a password is required\n") =>
|
||||||
|
{
|
||||||
|
Err("Cannot run non-interactive sudo".to_string())
|
||||||
|
}
|
||||||
|
Ok(_output) => Err("\"sudo whoami\" didn't return \"root\"".to_string()),
|
||||||
|
Err(e) => Err(format!("{}: {}", UUTILS_WARNING, e)),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Err(format!("{}: {}", UUTILS_INFO, "cannot run inside CI"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1771,18 +1781,27 @@ mod tests {
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
#[cfg(feature = "whoami")]
|
#[cfg(feature = "whoami")]
|
||||||
fn test_run_ucmd_as_root() {
|
fn test_run_ucmd_as_root() {
|
||||||
// Skip test if we can't guarantee non-interactive `sudo`.
|
if !is_ci() {
|
||||||
if let Ok(_status) = Command::new("sudo")
|
// Skip test if we can't guarantee non-interactive `sudo`, or if we're not "root"
|
||||||
.args(&["-E", "-v", "--non-interactive"])
|
if let Ok(output) = Command::new("sudo")
|
||||||
.status()
|
.env("LC_ALL", "C")
|
||||||
{
|
.args(&["-E", "--non-interactive", "whoami"])
|
||||||
let ts = TestScenario::new("whoami");
|
.output()
|
||||||
std::assert_eq!(
|
{
|
||||||
run_ucmd_as_root(&ts, &[]).unwrap().stdout_str().trim(),
|
if output.status.success() && String::from_utf8_lossy(&output.stdout).eq("root\n") {
|
||||||
"root"
|
let ts = TestScenario::new("whoami");
|
||||||
);
|
std::assert_eq!(
|
||||||
|
run_ucmd_as_root(&ts, &[]).unwrap().stdout_str().trim(),
|
||||||
|
"root"
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
println!("TEST SKIPPED (we're not root)");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("TEST SKIPPED (cannot run sudo)");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
print!("TEST SKIPPED");
|
println!("TEST SKIPPED (cannot run inside CI)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue