From 7ab384a7ab7f01ce55f3084dc120f6af7591d6b9 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 3 Apr 2023 18:07:54 +0200 Subject: [PATCH] sudo-test: add API to set user shell --- test-framework/sudo-test/src/lib.rs | 35 ++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/test-framework/sudo-test/src/lib.rs b/test-framework/sudo-test/src/lib.rs index 5ac4785d6..5c5b3230e 100644 --- a/test-framework/sudo-test/src/lib.rs +++ b/test-framework/sudo-test/src/lib.rs @@ -266,6 +266,7 @@ pub struct User { groups: HashSet, id: Option, password: Option, + shell: Option, } /// creates a new user with the specified `name` and the following defaults: @@ -331,12 +332,21 @@ impl User { self } + /// sets the user's shell to the one at the specified `path` + pub fn shell(mut self, path: impl AsRef) -> Self { + self.shell = Some(path.as_ref().to_string()); + self + } + fn create(&self, container: &Container) -> Result<()> { let mut useradd = Command::new("useradd"); useradd.arg("--no-user-group"); if self.create_home_directory { useradd.arg("--create-home"); } + if let Some(path) = &self.shell { + useradd.arg("--shell").arg(path); + } if let Some(id) = self.id { useradd.arg("--uid").arg(id.to_string()); } @@ -363,10 +373,11 @@ impl From for User { Self { create_home_directory: false, - name, groups: HashSet::new(), id: None, + name, password: None, + shell: None, } } } @@ -835,4 +846,26 @@ mod tests { .exec(&env)? .assert_success() } + + #[test] + fn setting_shell_works() -> Result<()> { + let expected = "/path/to/shell"; + let env = EnvBuilder::default() + .user(User(USERNAME).shell(expected)) + .build()?; + + let passwd = Command::new("getent").arg("passwd").exec(&env)?.stdout()?; + + let mut found = false; + for line in passwd.lines() { + if line.starts_with(&format!("{USERNAME}:")) { + found = true; + assert!(line.ends_with(&format!(":{expected}"))); + } + } + + assert!(found); + + Ok(()) + } }