Skip to content

Commit

Permalink
sudo-test: add API to set user shell
Browse files Browse the repository at this point in the history
  • Loading branch information
japaric committed Apr 3, 2023
1 parent 0aa8ced commit 7ab384a
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion test-framework/sudo-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ pub struct User {
groups: HashSet<Groupname>,
id: Option<u32>,
password: Option<String>,
shell: Option<String>,
}

/// creates a new user with the specified `name` and the following defaults:
Expand Down Expand Up @@ -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<str>) -> 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());
}
Expand All @@ -363,10 +373,11 @@ impl From<String> for User {

Self {
create_home_directory: false,
name,
groups: HashSet::new(),
id: None,
name,
password: None,
shell: None,
}
}
}
Expand Down Expand Up @@ -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(())
}
}

0 comments on commit 7ab384a

Please sign in to comment.