Skip to content

Commit

Permalink
tmpfiles: Add a unit test
Browse files Browse the repository at this point in the history
This is way faster to test than a full compose build.
  • Loading branch information
cgwalters committed Dec 12, 2023
1 parent 8f44b0e commit a1ce86c
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions rust/src/tmpfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ fn tmpfiles_entry_get_path(line: &str) -> Result<&str> {

#[cfg(test)]
mod tests {
use rustix::fd::AsRawFd;

use super::*;
#[test]
fn test_tmpfiles_entry_get_path() {
Expand All @@ -119,4 +121,55 @@ mod tests {
assert_eq!(path, expected, "Input: {input}");
}
}

fn newroot() -> Result<cap_std_ext::cap_tempfile::TempDir> {
let root = cap_std_ext::cap_tempfile::tempdir(cap_std::ambient_authority())?;
root.create_dir_all(RPMOSTREE_TMPFILESD)?;
root.create_dir_all(TMPFILESD)?;
Ok(root)
}

#[test]
fn test_deduplicate_noop() -> Result<()> {
let root = &newroot()?;
deduplicate_tmpfiles_entries(root.as_raw_fd())?;
Ok(())
}

// The first and 3rd are duplicates
const PKG_FILESYSTEM_CONTENTS: &str = indoc::indoc! { r#"
d /var/cache 0755 root root - -
d /var/lib/games 0755 root root - -
d /var/tmp 1777 root root - -
d /var/spool/mail 0775 root mail - -
"# };
const VAR_CONF: &str = indoc::indoc! { r#"
d /var/cache 0755 - - -
"# };
const TMP_CONF: &str = indoc::indoc! { r#"
q /var/tmp 1777 root root 30d
"# };

#[test]
fn test_deduplicate() -> Result<()> {
let root = &newroot()?;
let rpmostree_tmpfiles_dir = root.open_dir(RPMOSTREE_TMPFILESD)?;
let tmpfiles_dir = root.open_dir(TMPFILESD)?;
rpmostree_tmpfiles_dir
.atomic_write(format!("pkg-filesystem.conf"), PKG_FILESYSTEM_CONTENTS)?;
tmpfiles_dir.atomic_write("var.conf", VAR_CONF)?;
tmpfiles_dir.atomic_write("tmp.conf", TMP_CONF)?;
assert!(!tmpfiles_dir.try_exists(AUTOVAR_PATH)?);
deduplicate_tmpfiles_entries(root.as_raw_fd())?;
let contents = tmpfiles_dir.read_to_string(AUTOVAR_PATH).unwrap();
assert!(contents.contains("# This file was generated by rpm-ostree."));
let entries = contents
.lines()
.filter(|l| !(l.is_empty() || l.starts_with('#')))
.collect::<Vec<_>>();
assert_eq!(entries.len(), 2);
assert_eq!(entries[0], "d /var/lib/games 0755 root root - -");
assert_eq!(entries[1], "d /var/spool/mail 0775 root mail - -");
Ok(())
}
}

0 comments on commit a1ce86c

Please sign in to comment.