From 0e360dc63b383f0455542468a1fa50d6be902432 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 11 Dec 2023 20:36:19 -0500 Subject: [PATCH] tmpfiles: Add a unit test This is way faster to test than a full compose build. --- rust/src/tmpfiles.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/rust/src/tmpfiles.rs b/rust/src/tmpfiles.rs index 27f8d6e658..88ca0f0910 100644 --- a/rust/src/tmpfiles.rs +++ b/rust/src/tmpfiles.rs @@ -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() { @@ -119,4 +121,55 @@ mod tests { assert_eq!(path, expected, "Input: {input}"); } } + + fn newroot() -> Result { + 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::>(); + 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(()) + } }