notify: add separate context for unit-tests

... as using PVEContext for tests is brittle and annoying for some
tests.

Signed-off-by: Lukas Wagner <l.wagner@proxmox.com>
This commit is contained in:
Lukas Wagner 2024-01-10 10:31:22 +01:00 committed by Wolfgang Bumiller
parent 50fa98e241
commit f0bf95f53b
2 changed files with 31 additions and 5 deletions

View File

@ -7,6 +7,8 @@ pub mod common;
pub mod pbs; pub mod pbs;
#[cfg(feature = "pve-context")] #[cfg(feature = "pve-context")]
pub mod pve; pub mod pve;
#[cfg(test)]
mod test;
/// Product-specific context /// Product-specific context
pub trait Context: Send + Sync + Debug { pub trait Context: Send + Sync + Debug {
@ -22,12 +24,10 @@ pub trait Context: Send + Sync + Debug {
fn default_config(&self) -> &'static str; fn default_config(&self) -> &'static str;
} }
#[cfg(not(feature = "pve-context"))] #[cfg(not(test))]
static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(None); static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(None);
// The test unfortunately require context... #[cfg(test)]
// TODO: Check if we can make this nicer... static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(Some(&test::TestContext));
#[cfg(feature = "pve-context")]
static CONTEXT: Mutex<Option<&'static dyn Context>> = Mutex::new(Some(&pve::PVE_CONTEXT));
/// Set the product-specific context /// Set the product-specific context
pub fn set_context(context: &'static dyn Context) { pub fn set_context(context: &'static dyn Context) {

View File

@ -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 {
""
}
}