[pve-devel] [PATCH proxmox 2/3] notify: smtp: add test for building forwarded messages
Lukas Wagner
l.wagner at proxmox.com
Fri May 23 14:26:14 CEST 2025
Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
---
proxmox-notify/src/endpoints/smtp.rs | 32 ++++++
proxmox-notify/testdata/test1.msg | 104 ++++++++++++++++++
.../test_forward_message_from_raw.ref | 95 ++++++++++++++++
3 files changed, 231 insertions(+)
create mode 100644 proxmox-notify/testdata/test1.msg
create mode 100644 proxmox-notify/testdata/test_forward_message_from_raw.ref
diff --git a/proxmox-notify/src/endpoints/smtp.rs b/proxmox-notify/src/endpoints/smtp.rs
index 2f4bad3c..7ad85de8 100644
--- a/proxmox-notify/src/endpoints/smtp.rs
+++ b/proxmox-notify/src/endpoints/smtp.rs
@@ -376,3 +376,35 @@ fn build_forwarded_message(
Ok(message)
}
+
+#[cfg(all(test, feature = "mail-forwarder"))]
+mod tests {
+ use lettre::message::header::Date;
+
+ use super::*;
+
+ #[test]
+ fn test_forward_message_from_raw() {
+ let input = "testdata/test1.msg";
+ let reference = "testdata/test_forward_message_from_raw.ref";
+
+ let mut email_builder =
+ Message::builder().from("Sender <sender at example.com>".parse().unwrap());
+
+ email_builder = email_builder.to("Recipient <recipient at example.com>".parse().unwrap());
+
+ let raw_mail = std::fs::read(input).unwrap();
+ let mut message = build_forwarded_message(email_builder, "test", &raw_mail).unwrap();
+
+ // The Date header contains the current time, let's just remove it
+ // for this test.
+ message.headers_mut().remove::<Date>();
+
+ let formatted = message.formatted();
+
+ // Uncomment to update the reference file.
+ // std::fs::write(reference, &formatted).unwrap();
+
+ assert_eq!(formatted, std::fs::read(reference).unwrap());
+ }
+}
diff --git a/proxmox-notify/testdata/test1.msg b/proxmox-notify/testdata/test1.msg
new file mode 100644
index 00000000..634e7522
--- /dev/null
+++ b/proxmox-notify/testdata/test1.msg
@@ -0,0 +1,104 @@
+From root at host.example.com Fri May 23 11:59:32 2025
+Return-Path: <root at host.example.com>
+X-Original-To: root
+Delivered-To: root at host.example.com
+Received: by host.example.com (Postfix, from userid 0)
+ id 25CE920081A; Fri, 23 May 2025 11:59:32 +0200 (CEST)
+Content-Type: multipart/alternative;
+ boundary="----_=_NextPart_002_1747994372"
+MIME-Version: 1.0
+Subject: vzdump backup status (host.example.com): backup successful
+From: Proxmox VE <root at host.example.com>
+To: root at host.example.com
+Date: Fri, 23 May 2025 11:59:32 +0200
+Auto-Submitted: auto-generated;
+Message-Id: <20250523095932.25CE920081A at host.example.com>
+
+This is a multi-part message in MIME format.
+
+------_=_NextPart_002_1747994372
+Content-Type: text/plain;
+ charset="UTF-8"
+Content-Transfer-Encoding: 8bit
+
+
+Details
+=======
+VMID Name Status Time Size Filename
+187 smtp-sink ok 11s 430.758 MiB /var/lib/vz/dump/vzdump-lxc-187-2025_05_23-11_59_21.tar.zst
+
+Total running time: 11s
+Total size: 430.758 MiB
+
+Logs
+====
+vzdump 187 --compress zstd --notes-template '{{guestname}}' --remove 0 --storage local --mode snapshot --notification-mode notification-system --node host
+
+
+187: 2025-05-23 11:59:21 INFO: Starting Backup of VM 187 (lxc)
+187: 2025-05-23 11:59:21 INFO: status = stopped
+187: 2025-05-23 11:59:21 INFO: backup mode: stop
+187: 2025-05-23 11:59:21 INFO: ionice priority: 7
+187: 2025-05-23 11:59:21 INFO: CT Name: smtp-sink
+187: 2025-05-23 11:59:21 INFO: including mount point rootfs ('/') in backup
+187: 2025-05-23 11:59:21 INFO: creating vzdump archive '/var/lib/vz/dump/vzdump-lxc-187-2025_05_23-11_59_21.tar.zst'
+187: 2025-05-23 11:59:32 INFO: Total bytes written: 1519319040 (1.5GiB, 135MiB/s)
+187: 2025-05-23 11:59:32 INFO: archive file size: 430MB
+187: 2025-05-23 11:59:32 INFO: adding notes to backup
+187: 2025-05-23 11:59:32 INFO: Finished Backup of VM 187 (00:00:11)
+
+
+
+------_=_NextPart_002_1747994372
+Content-Type: text/html;
+ charset="UTF-8"
+Content-Transfer-Encoding: 8bit
+
+<html>
+ <body>
+
+ <h1 style="font-size: 1.2em">Details</h1>
+ <table style="border: 1px solid;border-collapse=collapse;">
+ <tr>
+ <th style="border: 1px solid;border-collapse=collapse;">VMID</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Name</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Status</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Time</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Size</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Filename</th>
+ </tr>
+
+ <tr>
+ <td style="border: 1px solid;border-collapse=collapse;">187</th>
+ <td style="border: 1px solid;border-collapse=collapse;">smtp-sink</th>
+ <td style="border: 1px solid;border-collapse=collapse;">ok</th>
+ <td style="border: 1px solid;border-collapse=collapse;">11s</th>
+ <td style="border: 1px solid;border-collapse=collapse;">430.758 MiB</th>
+ <td style="border: 1px solid;border-collapse=collapse;">/var/lib/vz/dump/vzdump-lxc-187-2025_05_23-11_59_21.tar.zst</th>
+ </tr>
+
+ </table>
+ <br/>
+ Total running time: 11s<br/>
+ Total size: 430.758 MiB<br/>
+ <h1 style="font-size: 1.2em">Logs</h1>
+ <pre>vzdump 187 --compress zstd --notes-template '{{guestname}}' --remove 0 --storage local --mode snapshot --notification-mode notification-system --node host
+
+
+187: 2025-05-23 11:59:21 INFO: Starting Backup of VM 187 (lxc)
+187: 2025-05-23 11:59:21 INFO: status = stopped
+187: 2025-05-23 11:59:21 INFO: backup mode: stop
+187: 2025-05-23 11:59:21 INFO: ionice priority: 7
+187: 2025-05-23 11:59:21 INFO: CT Name: smtp-sink
+187: 2025-05-23 11:59:21 INFO: including mount point rootfs ('/') in backup
+187: 2025-05-23 11:59:21 INFO: creating vzdump archive '/var/lib/vz/dump/vzdump-lxc-187-2025_05_23-11_59_21.tar.zst'
+187: 2025-05-23 11:59:32 INFO: Total bytes written: 1519319040 (1.5GiB, 135MiB/s)
+187: 2025-05-23 11:59:32 INFO: archive file size: 430MB
+187: 2025-05-23 11:59:32 INFO: adding notes to backup
+187: 2025-05-23 11:59:32 INFO: Finished Backup of VM 187 (00:00:11)
+
+</pre>
+ </body>
+</html>
+
+------_=_NextPart_002_1747994372--
diff --git a/proxmox-notify/testdata/test_forward_message_from_raw.ref b/proxmox-notify/testdata/test_forward_message_from_raw.ref
new file mode 100644
index 00000000..79fede27
--- /dev/null
+++ b/proxmox-notify/testdata/test_forward_message_from_raw.ref
@@ -0,0 +1,95 @@
+From: Sender <sender at example.com>
+To: Recipient <recipient at example.com>
+Subject: vzdump backup status (host.example.com): backup successful
+Content-Type: multipart/alternative;
+ boundary="----_=_NextPart_002_1747994372"
+MIME-Version: 1.0
+
+This is a multi-part message in MIME format.
+
+------_=_NextPart_002_1747994372
+Content-Type: text/plain;
+ charset="UTF-8"
+Content-Transfer-Encoding: 8bit
+
+
+Details
+=======
+VMID Name Status Time Size Filename
+187 smtp-sink ok 11s 430.758 MiB /var/lib/vz/dump/vzdump-lxc-187-2025_05_23-11_59_21.tar.zst
+
+Total running time: 11s
+Total size: 430.758 MiB
+
+Logs
+====
+vzdump 187 --compress zstd --notes-template '{{guestname}}' --remove 0 --storage local --mode snapshot --notification-mode notification-system --node host
+
+
+187: 2025-05-23 11:59:21 INFO: Starting Backup of VM 187 (lxc)
+187: 2025-05-23 11:59:21 INFO: status = stopped
+187: 2025-05-23 11:59:21 INFO: backup mode: stop
+187: 2025-05-23 11:59:21 INFO: ionice priority: 7
+187: 2025-05-23 11:59:21 INFO: CT Name: smtp-sink
+187: 2025-05-23 11:59:21 INFO: including mount point rootfs ('/') in backup
+187: 2025-05-23 11:59:21 INFO: creating vzdump archive '/var/lib/vz/dump/vzdump-lxc-187-2025_05_23-11_59_21.tar.zst'
+187: 2025-05-23 11:59:32 INFO: Total bytes written: 1519319040 (1.5GiB, 135MiB/s)
+187: 2025-05-23 11:59:32 INFO: archive file size: 430MB
+187: 2025-05-23 11:59:32 INFO: adding notes to backup
+187: 2025-05-23 11:59:32 INFO: Finished Backup of VM 187 (00:00:11)
+
+
+
+------_=_NextPart_002_1747994372
+Content-Type: text/html;
+ charset="UTF-8"
+Content-Transfer-Encoding: 8bit
+
+<html>
+ <body>
+
+ <h1 style="font-size: 1.2em">Details</h1>
+ <table style="border: 1px solid;border-collapse=collapse;">
+ <tr>
+ <th style="border: 1px solid;border-collapse=collapse;">VMID</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Name</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Status</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Time</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Size</th>
+ <th style="border: 1px solid;border-collapse=collapse;">Filename</th>
+ </tr>
+
+ <tr>
+ <td style="border: 1px solid;border-collapse=collapse;">187</th>
+ <td style="border: 1px solid;border-collapse=collapse;">smtp-sink</th>
+ <td style="border: 1px solid;border-collapse=collapse;">ok</th>
+ <td style="border: 1px solid;border-collapse=collapse;">11s</th>
+ <td style="border: 1px solid;border-collapse=collapse;">430.758 MiB</th>
+ <td style="border: 1px solid;border-collapse=collapse;">/var/lib/vz/dump/vzdump-lxc-187-2025_05_23-11_59_21.tar.zst</th>
+ </tr>
+
+ </table>
+ <br/>
+ Total running time: 11s<br/>
+ Total size: 430.758 MiB<br/>
+ <h1 style="font-size: 1.2em">Logs</h1>
+ <pre>vzdump 187 --compress zstd --notes-template '{{guestname}}' --remove 0 --storage local --mode snapshot --notification-mode notification-system --node host
+
+
+187: 2025-05-23 11:59:21 INFO: Starting Backup of VM 187 (lxc)
+187: 2025-05-23 11:59:21 INFO: status = stopped
+187: 2025-05-23 11:59:21 INFO: backup mode: stop
+187: 2025-05-23 11:59:21 INFO: ionice priority: 7
+187: 2025-05-23 11:59:21 INFO: CT Name: smtp-sink
+187: 2025-05-23 11:59:21 INFO: including mount point rootfs ('/') in backup
+187: 2025-05-23 11:59:21 INFO: creating vzdump archive '/var/lib/vz/dump/vzdump-lxc-187-2025_05_23-11_59_21.tar.zst'
+187: 2025-05-23 11:59:32 INFO: Total bytes written: 1519319040 (1.5GiB, 135MiB/s)
+187: 2025-05-23 11:59:32 INFO: archive file size: 430MB
+187: 2025-05-23 11:59:32 INFO: adding notes to backup
+187: 2025-05-23 11:59:32 INFO: Finished Backup of VM 187 (00:00:11)
+
+</pre>
+ </body>
+</html>
+
+------_=_NextPart_002_1747994372--
--
2.39.5
More information about the pve-devel
mailing list