From 979d25ee13a3227e0aa817f9e436289b4f5d03ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janne=20He=C3=9F?= Date: Mon, 24 Apr 2023 11:48:46 +0200 Subject: [PATCH 1/2] Revert "Merge pull request #139 from adtya/built_on_date" This reverts commit d751d13b0ae8f9c4afb9d9118822f31542ed5f9c, reversing changes made to 7c55847aaf804398c0cd1c75f20591075eafcdee. --- rust/tool/src/generation.rs | 3 ++- rust/tool/tests/common/mod.rs | 3 +++ rust/tool/tests/os_release.rs | 15 +++++++-------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/rust/tool/src/generation.rs b/rust/tool/src/generation.rs index 41c0525..bffddef 100644 --- a/rust/tool/src/generation.rs +++ b/rust/tool/src/generation.rs @@ -1,5 +1,6 @@ use std::fmt; use std::fs; +use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; use anyhow::{anyhow, Context, Result}; @@ -95,7 +96,7 @@ impl fmt::Display for Generation { } fn read_build_time(path: &Path) -> Result { - let build_time = time::OffsetDateTime::from(fs::metadata(path)?.created()?).date(); + let build_time = time::OffsetDateTime::from_unix_timestamp(fs::metadata(path)?.mtime())?.date(); Ok(build_time) } diff --git a/rust/tool/tests/common/mod.rs b/rust/tool/tests/common/mod.rs index 3de7d88..7980d8e 100644 --- a/rust/tool/tests/common/mod.rs +++ b/rust/tool/tests/common/mod.rs @@ -70,6 +70,9 @@ pub fn setup_generation_link_from_toplevel( let mut file = fs::File::create(bootspec_path)?; file.write_all(&serde_json::to_vec(&bootspec)?)?; + // Explicitly set modification time so that snapshot test of os-release reliably works. + // This has to happen after any modifications to the directory. + filetime::set_file_mtime(&generation_link_path, filetime::FileTime::zero())?; Ok(generation_link_path) } diff --git a/rust/tool/tests/os_release.rs b/rust/tool/tests/os_release.rs index e5c6629..5203723 100644 --- a/rust/tool/tests/os_release.rs +++ b/rust/tool/tests/os_release.rs @@ -1,6 +1,7 @@ use std::fs; use anyhow::{Context, Result}; +use expect_test::expect; use tempfile::tempdir; mod common; @@ -14,9 +15,6 @@ fn generate_expected_os_release() -> Result<()> { let generation_link = common::setup_generation_link(tmpdir.path(), profiles.path(), 1) .expect("Failed to setup generation link"); - // Expect the 'Built on' date in VERSION_ID to be from the birth time of generation_link - let expected_built_on = time::OffsetDateTime::from(fs::metadata(generation_link.as_path())?.created()?).date(); - let output0 = common::lanzaboote_install(0, esp_mountpoint.path(), vec![generation_link])?; assert!(output0.status.success()); @@ -29,12 +27,13 @@ fn generate_expected_os_release() -> Result<()> { .context("Failed to read .osrelease PE section.")? .to_owned(); - let expected_os_release_section = format!(r#"ID=lanza -PRETTY_NAME=LanzaOS -VERSION_ID=Generation 1, Built on {} -"#, expected_built_on.to_string()); + let expected = expect![[r#" + ID=lanza + PRETTY_NAME=LanzaOS + VERSION_ID=Generation 1, Built on 1970-01-01 + "#]]; - assert_eq!(&expected_os_release_section, &String::from_utf8(os_release_section)?); + expected.assert_eq(&String::from_utf8(os_release_section)?); Ok(()) } From c22352ca2066fe6a56d74691b0f44ca10891a4d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Janne=20He=C3=9F?= Date: Mon, 24 Apr 2023 11:57:34 +0200 Subject: [PATCH 2/2] tool: Use mtime of the symlink rather than the target When using the target, this will always result in a timestamp from 1970 because the symlink points to the store. --- rust/tool/src/generation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/tool/src/generation.rs b/rust/tool/src/generation.rs index bffddef..d5c5b5e 100644 --- a/rust/tool/src/generation.rs +++ b/rust/tool/src/generation.rs @@ -96,7 +96,7 @@ impl fmt::Display for Generation { } fn read_build_time(path: &Path) -> Result { - let build_time = time::OffsetDateTime::from_unix_timestamp(fs::metadata(path)?.mtime())?.date(); + let build_time = time::OffsetDateTime::from_unix_timestamp(fs::symlink_metadata(path)?.mtime())?.date(); Ok(build_time) }