all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [PATCH datacenter-manager] tests: use tempfile instead using our own, in-tree reimplementation
@ 2026-08-04 12:34 Lukas Wagner
  0 siblings, 0 replies; only message in thread
From: Lukas Wagner @ 2026-08-04 12:34 UTC (permalink / raw)
  To: pdm-devel

PBS started using it a while ago for tests, and also the
`namespaced_cache` in PDM already used it for its own test suite.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
---
 .../remote_collection_task.rs                 |  6 +-
 server/src/metric_collection/rrd_task.rs      | 10 ++--
 server/src/metric_collection/state.rs         |  5 +-
 server/src/remote_tasks/task_cache.rs         |  6 +-
 server/src/test_support/mod.rs                |  3 -
 server/src/test_support/temp.rs               | 60 -------------------
 6 files changed, 11 insertions(+), 79 deletions(-)
 delete mode 100644 server/src/test_support/temp.rs

diff --git a/server/src/metric_collection/remote_collection_task.rs b/server/src/metric_collection/remote_collection_task.rs
index d243dcf1..e630a99d 100644
--- a/server/src/metric_collection/remote_collection_task.rs
+++ b/server/src/metric_collection/remote_collection_task.rs
@@ -399,7 +399,6 @@ pub(super) mod tests {
         connection::{ClientFactory, PveClient},
         metric_collection::rrd_task::RrdStoreResult,
         pbs_client::PbsClient,
-        test_support::temp::NamedTempFile,
     };
 
     use super::*;
@@ -574,7 +573,7 @@ pub(super) mod tests {
 
         let config = make_remote_config();
 
-        let state_file = NamedTempFile::new(get_create_options()).unwrap();
+        let state_file = tempfile::NamedTempFile::new().unwrap();
         let state = MetricCollectionState::new(state_file.path().into(), get_create_options());
 
         let (_control_tx, control_rx) = tokio::sync::mpsc::channel(10);
@@ -627,7 +626,8 @@ pub(super) mod tests {
 
         let config = make_remote_config();
 
-        let state_file = NamedTempFile::new(get_create_options()).unwrap();
+        let state_file = tempfile::NamedTempFile::new().unwrap();
+
         let mut state = MetricCollectionState::new(state_file.path().into(), get_create_options());
 
         let now = proxmox_time::epoch_i64();
diff --git a/server/src/metric_collection/rrd_task.rs b/server/src/metric_collection/rrd_task.rs
index b5d6c008..99474140 100644
--- a/server/src/metric_collection/rrd_task.rs
+++ b/server/src/metric_collection/rrd_task.rs
@@ -428,17 +428,14 @@ mod tests {
     use proxmox_rrd_api_types::{RrdMode, RrdTimeframe};
     use pve_api_types::{ClusterMetrics, ClusterMetricsData};
 
-    use crate::{
-        metric_collection::remote_collection_task::tests::get_create_options,
-        test_support::temp::NamedTempDir,
-    };
+    use crate::metric_collection::remote_collection_task::tests::get_create_options;
 
     use super::*;
 
     #[tokio::test]
     async fn test_rrd_task_persists_data() -> Result<(), Error> {
         // Arrange
-        let dir = NamedTempDir::new()?;
+        let dir = tempfile::tempdir()?;
         let options = get_create_options().perm(nix::sys::stat::Mode::from_bits_truncate(0o700));
         let cache = Arc::new(RrdCache::new(dir.path(), options, options)?);
 
@@ -526,7 +523,8 @@ mod tests {
 
     #[test]
     fn store_datapoints_drops_future_dated() -> Result<(), Error> {
-        let dir = NamedTempDir::new()?;
+        let dir = tempfile::tempdir()?;
+
         let options = get_create_options().perm(nix::sys::stat::Mode::from_bits_truncate(0o700));
         let cache = RrdCache::new(dir.path(), options, options)?;
 
diff --git a/server/src/metric_collection/state.rs b/server/src/metric_collection/state.rs
index 8d98613f..889a8443 100644
--- a/server/src/metric_collection/state.rs
+++ b/server/src/metric_collection/state.rs
@@ -93,13 +93,12 @@ impl MetricCollectionState {
 #[cfg(test)]
 mod tests {
     use crate::metric_collection::remote_collection_task::tests::get_create_options;
-    use crate::test_support::temp::NamedTempFile;
 
     use super::*;
 
     #[test]
     fn save_and_load() -> Result<(), Error> {
-        let file = NamedTempFile::new(get_create_options())?;
+        let file = tempfile::NamedTempFile::new()?;
         let options = get_create_options();
         let mut state = MetricCollectionState::new(file.path().into(), options);
 
@@ -123,7 +122,7 @@ mod tests {
 
     #[test]
     fn test_retain() -> Result<(), Error> {
-        let file = NamedTempFile::new(get_create_options())?;
+        let file = tempfile::NamedTempFile::new()?;
         let options = get_create_options();
         let mut state = MetricCollectionState::new(file.path().into(), options);
 
diff --git a/server/src/remote_tasks/task_cache.rs b/server/src/remote_tasks/task_cache.rs
index b0b4a837..c13d9550 100644
--- a/server/src/remote_tasks/task_cache.rs
+++ b/server/src/remote_tasks/task_cache.rs
@@ -1255,8 +1255,6 @@ impl Iterator for JournalIterator {
 mod tests {
     use std::io::Cursor;
 
-    use crate::test_support::temp::NamedTempDir;
-
     use super::*;
 
     #[test]
@@ -1338,8 +1336,8 @@ mod tests {
 
     const DEFAULT_MAX_SIZE: u64 = 10000;
 
-    fn make_cache() -> Result<(NamedTempDir, TaskCache), Error> {
-        let tmp_dir = NamedTempDir::new()?;
+    fn make_cache() -> Result<(tempfile::TempDir, TaskCache), Error> {
+        let tmp_dir = tempfile::tempdir()?;
         let cache = TaskCache::new(
             tmp_dir.path(),
             CreateOptions::new(),
diff --git a/server/src/test_support/mod.rs b/server/src/test_support/mod.rs
index f026011c..5d264815 100644
--- a/server/src/test_support/mod.rs
+++ b/server/src/test_support/mod.rs
@@ -1,5 +1,2 @@
 #[cfg(remote_config = "faked")]
 pub mod fake_remote;
-
-#[cfg(test)]
-pub mod temp;
diff --git a/server/src/test_support/temp.rs b/server/src/test_support/temp.rs
deleted file mode 100644
index a93c914d..00000000
--- a/server/src/test_support/temp.rs
+++ /dev/null
@@ -1,60 +0,0 @@
-use std::path::{Path, PathBuf};
-
-use anyhow::Error;
-
-use proxmox_sys::fs::CreateOptions;
-
-/// Temporary file that be cleaned up when dropped.
-pub struct NamedTempFile {
-    path: PathBuf,
-}
-
-impl NamedTempFile {
-    /// Create a new temporary file.
-    ///
-    /// The file will be created with the passed [`CreateOptions`].
-    pub fn new(options: CreateOptions) -> Result<Self, Error> {
-        let base = std::env::temp_dir().join("test");
-        let (_, path) = proxmox_sys::fs::make_tmp_file(base, options)?;
-
-        Ok(Self { path })
-    }
-
-    /// Return the [`Path`] to the temporary file.
-    pub fn path(&self) -> &Path {
-        &self.path
-    }
-}
-
-impl Drop for NamedTempFile {
-    fn drop(&mut self) {
-        let _ = std::fs::remove_file(&self.path);
-    }
-}
-
-/// Temporary directory that is cleaned up when dropped.
-pub struct NamedTempDir {
-    path: PathBuf,
-}
-
-impl NamedTempDir {
-    /// Create a new temporary directory.
-    ///
-    /// The directory will be created with `0o700` permissions.
-    pub fn new() -> Result<Self, Error> {
-        let path = proxmox_sys::fs::make_tmp_dir("/tmp", None)?;
-
-        Ok(Self { path })
-    }
-
-    /// Return the [`Path`] to the temporary directory.
-    pub fn path(&self) -> &Path {
-        &self.path
-    }
-}
-
-impl Drop for NamedTempDir {
-    fn drop(&mut self) {
-        let _ = std::fs::remove_dir_all(&self.path);
-    }
-}
-- 
2.47.3





^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-08-04 12:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-04 12:34 [PATCH datacenter-manager] tests: use tempfile instead using our own, in-tree reimplementation Lukas Wagner

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal