From da198dbb197c436e65d3c108ff9b9a53a2031338 Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Sun, 19 Sep 2021 22:24:11 +0200 Subject: [PATCH 1/4] tests/util: add a convenience wrapper to run a ucmd with root permissions --- tests/common/util.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/common/util.rs b/tests/common/util.rs index f3cdec0100d..a7c6e464d89 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -1237,6 +1237,58 @@ pub fn expected_result(ts: &TestScenario, args: &[&str]) -> std::result::Result< )) } +/// This is a convenience wrapper to run a ucmd with root permissions. +/// This runs 'sudo -E --non-interactive target/debug/coreutils util_name args` +/// This is primarily designed to run in the CICD environment where whoami is in $path +/// and where non-interactive sudo is possible. +/// To check if i) non-interactive sudo is possible and ii) if sudo works, this runs: +/// 'sudo -E --non-interactive whoami' first. +/// +/// Example: +/// +/// ```no_run +/// use crate::common::util::*; +/// #[test] +/// fn test_xyz() { +/// let ts = TestScenario::new("whoami"); +/// let expected = "root\n".to_string(); +/// if let Ok(result) = run_ucmd_as_root(&ts, &[]) { +/// result.stdout_is(expected); +/// } else { +/// print!("TEST SKIPPED"); +/// } +/// } +///``` +#[cfg(unix)] +pub fn run_ucmd_as_root( + ts: &TestScenario, + args: &[&str], +) -> std::result::Result { + if ts + .cmd_keepenv("sudo") + .env("LC_ALL", "C") + .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") + .arg("-E") + .arg("--non-interactive") + .arg(&ts.bin_path) + .arg(&ts.util_name) + .args(args) + .run()) + } +} + /// Sanity checks for test utils #[cfg(test)] mod tests { @@ -1523,4 +1575,20 @@ mod tests { std::assert_eq!(host_name_for("gwho"), "gwho"); } } + + #[test] + #[cfg(unix)] + fn test_run_ucmd_as_root() { + // We need non-interactive `sudo. + // CICD environment should allow non-interactive `sudo`. + // Return early if we can't guarantee non-interactive `sudo` + if !is_ci() { + return; + } + let ts = TestScenario::new("whoami"); + std::assert_eq!( + run_ucmd_as_root(&ts, &[]).unwrap().stdout_str().trim(), + "root" + ); + } } From 4ef77232486f0c7e9a6b8e0ad88a966f96b79f6d Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Mon, 20 Sep 2021 00:20:06 +0200 Subject: [PATCH 2/4] Update util.rs add feature for `whoami` --- tests/common/util.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/common/util.rs b/tests/common/util.rs index a7c6e464d89..f056f35103a 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -1578,6 +1578,7 @@ mod tests { #[test] #[cfg(unix)] + #[cfg(feature = "whoami")] fn test_run_ucmd_as_root() { // We need non-interactive `sudo. // CICD environment should allow non-interactive `sudo`. From 1ccf55a3dc611c5de46b9fe8a479dc6d42c67d6d Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Tue, 21 Sep 2021 00:23:23 +0200 Subject: [PATCH 3/4] Update util.rs --- tests/common/util.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/tests/common/util.rs b/tests/common/util.rs index f056f35103a..928bc84f3de 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -1239,7 +1239,7 @@ pub fn expected_result(ts: &TestScenario, args: &[&str]) -> std::result::Result< /// This is a convenience wrapper to run a ucmd with root permissions. /// This runs 'sudo -E --non-interactive target/debug/coreutils util_name args` -/// This is primarily designed to run in the CICD environment where whoami is in $path +/// This is primarily designed to run in an environment where whoami is in $path /// and where non-interactive sudo is possible. /// To check if i) non-interactive sudo is possible and ii) if sudo works, this runs: /// 'sudo -E --non-interactive whoami' first. @@ -1264,6 +1264,7 @@ pub fn run_ucmd_as_root( ts: &TestScenario, args: &[&str], ) -> std::result::Result { + // Apparently CICD environment has no `sudo`? if ts .cmd_keepenv("sudo") .env("LC_ALL", "C") @@ -1580,16 +1581,18 @@ mod tests { #[cfg(unix)] #[cfg(feature = "whoami")] fn test_run_ucmd_as_root() { - // We need non-interactive `sudo. - // CICD environment should allow non-interactive `sudo`. - // Return early if we can't guarantee non-interactive `sudo` - if !is_ci() { - return; + // Skip test if we can't guarantee non-interactive `sudo`. + if let Ok(_status) = Command::new("sudo") + .args(&["-E", "-v", "--non-interactive"]) + .status() + { + let ts = TestScenario::new("whoami"); + std::assert_eq!( + run_ucmd_as_root(&ts, &[]).unwrap().stdout_str().trim(), + "root" + ); + } else { + print!("TEST SKIPPED"); } - let ts = TestScenario::new("whoami"); - std::assert_eq!( - run_ucmd_as_root(&ts, &[]).unwrap().stdout_str().trim(), - "root" - ); } } From 12d7d9846abc825e95e95b00d2ce00e50b22068b Mon Sep 17 00:00:00 2001 From: Jan Scheer Date: Tue, 10 May 2022 13:46:22 +0200 Subject: [PATCH 4/4] test/util: improve `run_ucmd_as_root` for CICD * ensure that `fn run_no_wait` is only invoked if the system running the test has `sudo` in $path Previously, if run inside CICD, calling `fn run_ucmd_as_root` would provoke `fn run_no_wait` to panic because there's no `sudo`. --- tests/common/util.rs | 87 +++++++++++++++++++++++++++----------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/tests/common/util.rs b/tests/common/util.rs index 34bdc72f3bf..d601b90d841 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -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: /// 'sudo -E --non-interactive whoami' first. /// +/// This return an `Err()` if run inside CICD because there's no 'sudo'. +/// /// Example: /// /// ```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, &[]) { /// result.stdout_is(expected); /// } else { -/// print!("TEST SKIPPED"); +/// println!("TEST SKIPPED"); /// } /// } ///``` @@ -1390,29 +1392,37 @@ pub fn run_ucmd_as_root( ts: &TestScenario, args: &[&str], ) -> std::result::Result { - // Apparently CICD environment has no `sudo`? - if ts - .cmd_keepenv("sudo") - .env("LC_ALL", "C") - .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") + if !is_ci() { + // check if we can run 'sudo' + log_info("run", "sudo -E --non-interactive whoami"); + match Command::new("sudo") .env("LC_ALL", "C") - .arg("-E") - .arg("--non-interactive") - .arg(&ts.bin_path) - .arg(&ts.util_name) - .args(args) - .run()) + .args(&["-E", "--non-interactive", "whoami"]) + .output() + { + Ok(output) if String::from_utf8_lossy(&output.stdout).eq("root\n") => { + // we can run sudo and we're root + // 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(feature = "whoami")] fn test_run_ucmd_as_root() { - // Skip test if we can't guarantee non-interactive `sudo`. - if let Ok(_status) = Command::new("sudo") - .args(&["-E", "-v", "--non-interactive"]) - .status() - { - let ts = TestScenario::new("whoami"); - std::assert_eq!( - run_ucmd_as_root(&ts, &[]).unwrap().stdout_str().trim(), - "root" - ); + if !is_ci() { + // Skip test if we can't guarantee non-interactive `sudo`, or if we're not "root" + if let Ok(output) = Command::new("sudo") + .env("LC_ALL", "C") + .args(&["-E", "--non-interactive", "whoami"]) + .output() + { + if output.status.success() && String::from_utf8_lossy(&output.stdout).eq("root\n") { + 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 { - print!("TEST SKIPPED"); + println!("TEST SKIPPED (cannot run inside CI)"); } } }