From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id DC0391FF15E for ; Tue, 28 Jan 2025 13:26:25 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id ED7C4CDBB; Tue, 28 Jan 2025 13:26:23 +0100 (CET) From: Lukas Wagner To: pdm-devel@lists.proxmox.com Date: Tue, 28 Jan 2025 13:25:08 +0100 Message-Id: <20250128122520.167796-4-l.wagner@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250128122520.167796-1-l.wagner@proxmox.com> References: <20250128122520.167796-1-l.wagner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.015 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pdm-devel] [PATCH proxmox-datacenter-manager 03/15] task cache: add basic test for TaskCache X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" This one helps us to verify that we did not break the core functionality in the upcoming changes. Signed-off-by: Lukas Wagner --- server/src/task_cache.rs | 100 +++++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 21 deletions(-) diff --git a/server/src/task_cache.rs b/server/src/task_cache.rs index 7ded540..211beb4 100644 --- a/server/src/task_cache.rs +++ b/server/src/task_cache.rs @@ -25,8 +25,13 @@ pub async fn get_tasks(max_age: i64, filters: TaskFilters) -> Result Result { + fn new(cachefile_path: PathBuf, cachefile_options: CreateOptions) -> Result { Ok(Self { - content: Self::load_content()?, + content: Self::load_content(&cachefile_path)?, new_or_updated: Default::default(), dirty: false, cachefile_path, + cachefile_options, }) } /// Load the task cache contents from disk. - fn load_content() -> Result { - let taskcache_path = Path::new(pdm_buildcfg::PDM_CACHE_DIR).join("taskcache.json"); - let content = proxmox_sys::fs::file_read_optional_string(taskcache_path)?; + fn load_content(path: &Path) -> Result { + let content = proxmox_sys::fs::file_read_optional_string(path)?; let content = if let Some(content) = content { - serde_json::from_str(&content)? + serde_json::from_str(&content).unwrap_or_default() } else { Default::default() }; @@ -293,7 +301,7 @@ impl TaskCache { let _guard = self.lock(Duration::from_secs(5))?; // Read content again, in case somebody has changed it in the meanwhile - let mut content = Self::load_content()?; + let mut content = Self::load_content(&self.cachefile_path)?; for (remote_name, entry) in self.new_or_updated.remote_tasks.drain() { if let Some(existing_entry) = content.remote_tasks.get_mut(&remote_name) { @@ -308,12 +316,12 @@ impl TaskCache { let bytes = serde_json::to_vec_pretty(&content)?; - let api_uid = pdm_config::api_user()?.uid; - let api_gid = pdm_config::api_group()?.gid; - - let file_options = CreateOptions::new().owner(api_uid).group(api_gid); - - proxmox_sys::fs::replace_file(&self.cachefile_path, &bytes, file_options, true)?; + proxmox_sys::fs::replace_file( + &self.cachefile_path, + &bytes, + self.cachefile_options.clone(), + true, + )?; self.dirty = false; @@ -358,22 +366,23 @@ impl TaskCache { // without a lock, since the cache file is replaced atomically // when updating. fn lock(&self, duration: Duration) -> Result { - let api_uid = pdm_config::api_user()?.uid; - let api_gid = pdm_config::api_group()?.gid; - - let file_options = CreateOptions::new().owner(api_uid).group(api_gid); - proxmox_sys::fs::open_file_locked(self.lockfile_path(), duration, true, file_options) + proxmox_sys::fs::open_file_locked( + self.lockfile_path(), + duration, + true, + self.cachefile_options.clone(), + ) } } -#[derive(Serialize, Deserialize)] +#[derive(Debug, Serialize, Deserialize)] /// Per-remote entry in the task cache. struct TaskCacheEntry { timestamp: i64, tasks: Vec, } -#[derive(Default, Serialize, Deserialize)] +#[derive(Debug, Default, Serialize, Deserialize)] /// Content of the task cache file. struct TaskCacheContent { remote_tasks: HashMap, @@ -522,3 +531,52 @@ pub fn tasktype(status: &str) -> TaskStateType { TaskStateType::Error } } + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_support::temp::NamedTempFile; + + #[test] + fn basic_task_cache() -> Result<(), Error> { + let options = CreateOptions::new() + .owner(nix::unistd::Uid::effective()) + .group(nix::unistd::Gid::effective()) + .perm(nix::sys::stat::Mode::from_bits_truncate(0o600)); + + let temp_file = NamedTempFile::new(options.clone())?; + + let mut cache = TaskCache::new(temp_file.path().into(), options.clone())?; + + let mut tasks = Vec::new(); + + let now = proxmox_time::epoch_i64(); + for i in (0..20).rev() { + let upid: PveUpid = + "UPID:pve-node:0000C530:001C9BEC:677E934A:stopall::root@pam:".parse()?; + + tasks.push(TaskListItem { + upid: upid.to_string(), + node: upid.node, + pid: upid.pid as i64, + pstart: upid.pstart, + starttime: now - 10 * i, + worker_type: upid.worker_type, + worker_id: upid.worker_id, + user: upid.auth_id, + endtime: None, + status: None, + }); + } + + cache.set_tasks("some-remote", tasks.clone(), now); + cache.save()?; + + let cache = TaskCache::new(temp_file.path().into(), options)?; + + let res = cache.get_tasks("some-remote", now, 10000).unwrap(); + assert_eq!(tasks, res); + + Ok(()) + } +} -- 2.39.5 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel