]> git.proxmox.com Git - proxmox.git/commitdiff
notify: add separate context for unit-tests
authorLukas Wagner <l.wagner@proxmox.com>
Wed, 10 Jan 2024 09:31:22 +0000 (10:31 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 10 Jan 2024 11:29:26 +0000 (12:29 +0100)
... as using PVEContext for tests is brittle and annoying for some
tests.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
proxmox-notify/src/context/mod.rs
proxmox-notify/src/context/test.rs [new file with mode: 0644]

index b419641b2e7a7adf74a6616067d0b9b58215b469..cc6860305e82cce97b1b396782629dfc265f396a 100644 (file)
@@ -7,6 +7,8 @@ pub mod common;
 pub mod pbs;
 #[cfg(feature = "pve-context")]
 pub mod pve;
+#[cfg(test)]
+mod test;
 
 /// Product-specific context
 pub trait Context: Send + Sync + Debug {
@@ -22,12 +24,10 @@ pub trait Context: Send + Sync + Debug {
     fn default_config(&self) -> &'static str;
 }
 
-#[cfg(not(feature = "pve-context"))]
+#[cfg(not(test))]
 static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(None);
-// The test unfortunately require context...
-// TODO: Check if we can make this nicer...
-#[cfg(feature = "pve-context")]
-static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(Some(&pve::PVE_CONTEXT));
+#[cfg(test)]
+static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(Some(&test::TestContext));
 
 /// Set the product-specific context
 pub fn set_context(context: &'static dyn Context) {
diff --git a/proxmox-notify/src/context/test.rs b/proxmox-notify/src/context/test.rs
new file mode 100644 (file)
index 0000000..61f794a
--- /dev/null
@@ -0,0 +1,26 @@
+use crate::context::Context;
+
+#[derive(Debug)]
+pub struct TestContext;
+
+impl Context for TestContext {
+    fn lookup_email_for_user(&self, _user: &str) -> Option<String> {
+        Some("test@example.com".into())
+    }
+
+    fn default_sendmail_author(&self) -> String {
+        "Proxmox VE".into()
+    }
+
+    fn default_sendmail_from(&self) -> String {
+        "root".into()
+    }
+
+    fn http_proxy_config(&self) -> Option<String> {
+        None
+    }
+
+    fn default_config(&self) -> &'static str {
+        ""
+    }
+}