Skip to content

Commit

Permalink
sudo-test: add API for home directory creation
Browse files Browse the repository at this point in the history
  • Loading branch information
japaric committed Apr 3, 2023
1 parent c9afb25 commit 0aa8ced
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test-framework/sudo-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ impl EnvBuilder {
pub struct User {
name: Username,

create_home_directory: bool,
groups: HashSet<Groupname>,
id: Option<u32>,
password: Option<String>,
Expand Down Expand Up @@ -322,9 +323,20 @@ impl User {
self
}

/// creates a home directory for the user at `/home/<username>`
///
/// by default, the directory is not created
pub fn create_home_directory(mut self) -> Self {
self.create_home_directory = true;
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(id) = self.id {
useradd.arg("--uid").arg(id.to_string());
}
Expand All @@ -350,6 +362,7 @@ impl From<String> for User {
assert!(!name.is_empty(), "user name cannot be an empty string");

Self {
create_home_directory: false,
name,
groups: HashSet::new(),
id: None,
Expand Down Expand Up @@ -809,4 +822,17 @@ mod tests {

Ok(())
}

#[test]
fn create_home_directory_works() -> Result<()> {
let env = EnvBuilder::default()
.user(User(USERNAME).create_home_directory())
.build()?;

Command::new("sh")
.arg("-c")
.arg(format!("[ -d /home/{USERNAME} ]"))
.exec(&env)?
.assert_success()
}
}

0 comments on commit 0aa8ced

Please sign in to comment.