public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Lukas Wagner <l.wagner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox 2/2] notify: add separate context for unit-tests
Date: Thu, 14 Dec 2023 13:42:08 +0100	[thread overview]
Message-ID: <20231214124208.219312-2-l.wagner@proxmox.com> (raw)
In-Reply-To: <20231214124208.219312-1-l.wagner@proxmox.com>

... 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  | 10 +++++-----
 proxmox-notify/src/context/test.rs | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 5 deletions(-)
 create mode 100644 proxmox-notify/src/context/test.rs

diff --git a/proxmox-notify/src/context/mod.rs b/proxmox-notify/src/context/mod.rs
index b419641..cc68603 100644
--- a/proxmox-notify/src/context/mod.rs
+++ b/proxmox-notify/src/context/mod.rs
@@ -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
index 0000000..61f794a
--- /dev/null
+++ b/proxmox-notify/src/context/test.rs
@@ -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 {
+        ""
+    }
+}
-- 
2.39.2




  reply	other threads:[~2023-12-14 12:42 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 12:42 [pve-devel] [PATCH proxmox 1/2] notify: api: allow resetting built-in targets when they are referenced Lukas Wagner
2023-12-14 12:42 ` Lukas Wagner [this message]
2024-01-08 10:44 ` Lukas Wagner
2024-01-09 13:18 ` Wolfgang Bumiller
2024-01-10  8:29   ` Lukas Wagner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231214124208.219312-2-l.wagner@proxmox.com \
    --to=l.wagner@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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