public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup 1/2] pbs-config: add 'create_mocked_lock' helper
@ 2021-09-06 13:07 Dominik Csapak
  2021-09-06 13:07 ` [pbs-devel] [PATCH proxmox-backup 2/2] tape/inventory: fix the tape tests as user, by mocking the lock Dominik Csapak
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2021-09-06 13:07 UTC (permalink / raw)
  To: pbs-devel

by making the field an option and making it None in the mocked case
this function is only intended for testing and hidden from the docs

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 pbs-config/src/lib.rs | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/pbs-config/src/lib.rs b/pbs-config/src/lib.rs
index 29811164..c9ed543f 100644
--- a/pbs-config/src/lib.rs
+++ b/pbs-config/src/lib.rs
@@ -18,7 +18,13 @@ pub fn backup_group() -> Result<nix::unistd::Group, Error> {
     pbs_tools::sys::query_group(BACKUP_GROUP_NAME)?
         .ok_or_else(|| format_err!("Unable to lookup '{}' group.", BACKUP_GROUP_NAME))
 }
-pub struct BackupLockGuard(std::fs::File);
+pub struct BackupLockGuard(Option<std::fs::File>);
+
+#[doc(hidden)]
+/// Note: do not use for production code, this is only intended for tests
+pub fn create_mocked_lock() -> BackupLockGuard {
+    BackupLockGuard(None)
+}
 
 /// Open or create a lock file owned by user "backup" and lock it.
 ///
@@ -41,7 +47,7 @@ pub fn open_backup_lockfile<P: AsRef<std::path::Path>>(
     let timeout = timeout.unwrap_or(std::time::Duration::new(10, 0));
 
     let file = proxmox::tools::fs::open_file_locked(&path, timeout, exclusive, options)?;
-    Ok(BackupLockGuard(file))
+    Ok(BackupLockGuard(Some(file)))
 }
 
 /// Atomically write data to file owned by "root:backup" with permission "0640"
-- 
2.30.2





^ permalink raw reply	[flat|nested] 2+ messages in thread

* [pbs-devel] [PATCH proxmox-backup 2/2] tape/inventory: fix the tape tests as user, by mocking the lock
  2021-09-06 13:07 [pbs-devel] [PATCH proxmox-backup 1/2] pbs-config: add 'create_mocked_lock' helper Dominik Csapak
@ 2021-09-06 13:07 ` Dominik Csapak
  0 siblings, 0 replies; 2+ messages in thread
From: Dominik Csapak @ 2021-09-06 13:07 UTC (permalink / raw)
  To: pbs-devel

locking during the tests as regular user failed because we try to
chown to the backup user (which is not always possible).

Instead, do not lock at all, by implementing 'open_backup_lockfile' with
'create_mocked_lock'

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/tape/inventory.rs | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/tape/inventory.rs b/src/tape/inventory.rs
index d56b2144..2ecd73ac 100644
--- a/src/tape/inventory.rs
+++ b/src/tape/inventory.rs
@@ -40,7 +40,20 @@ use proxmox::tools::{
 };
 
 use pbs_systemd::time::compute_next_event;
-use pbs_config::{open_backup_lockfile, BackupLockGuard};
+use pbs_config::BackupLockGuard;
+
+#[cfg(not(test))]
+use pbs_config::open_backup_lockfile;
+
+#[cfg(test)]
+fn open_backup_lockfile<P: AsRef<std::path::Path>>(
+    _path: P,
+    _timeout: Option<std::time::Duration>,
+    _exclusive: bool,
+) -> Result<pbs_config::BackupLockGuard, anyhow::Error> {
+    Ok(pbs_config::create_mocked_lock())
+}
+
 
 use crate::{
     api2::types::{
-- 
2.30.2





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-06 13:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-06 13:07 [pbs-devel] [PATCH proxmox-backup 1/2] pbs-config: add 'create_mocked_lock' helper Dominik Csapak
2021-09-06 13:07 ` [pbs-devel] [PATCH proxmox-backup 2/2] tape/inventory: fix the tape tests as user, by mocking the lock Dominik Csapak

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal