From e24308c0172304a5f56ecf8e35282c76009d2f8d Mon Sep 17 00:00:00 2001 From: martino <32328813+f3rs3n@users.noreply.github.com> Date: Tue, 29 Sep 2026 23:52:07 +0200 Subject: [PATCH] Correct backup outcome diagnostics and email presentation chains --- .github/scripts/tests/notification_fixture.py | 51 ++++ .../tests/test_notification_corrections.py | 251 ++++++++++++++++++ .../test_notification_outcome_wording.py | 8 +- AppImage/scripts/notification_channels.py | 25 +- AppImage/scripts/notification_events.py | 21 +- AppImage/scripts/notification_manager.py | 5 + AppImage/scripts/notification_templates.py | 80 +++++- .../tests/test_notification_runtime_i18n.py | 13 +- .../scripts/tests/test_vzdump_ai_integrity.py | 8 +- 9 files changed, 431 insertions(+), 31 deletions(-) create mode 100644 .github/scripts/tests/notification_fixture.py create mode 100644 .github/scripts/tests/test_notification_corrections.py diff --git a/.github/scripts/tests/notification_fixture.py b/.github/scripts/tests/notification_fixture.py new file mode 100644 index 00000000..90ba64e4 --- /dev/null +++ b/.github/scripts/tests/notification_fixture.py @@ -0,0 +1,51 @@ +"""Assertion-free inert actual consumers; no operational host imports.""" +import ast +import re +import sys +from pathlib import Path +ROOT = Path(__file__).resolve().parents[3] +SCRIPTS = ROOT / 'AppImage/scripts' +if str(SCRIPTS) not in sys.path: + sys.path.insert(0, str(SCRIPTS)) +import notification_templates as templates +# Display-name resolution is an infrastructure boundary, never load manager. +templates._get_hostname = lambda: 'node-a' +from notification_channels import EmailChannel +LANGUAGES = ('en', 'de', 'es', 'fr', 'it', 'pt', 'sk', 'sv') + +def extract(path, name, owner, ns): + tree = ast.parse(path.read_text()) + nodes = tree.body if owner is None else next(n.body for n in tree.body if isinstance(n, ast.ClassDef) and n.name == owner) + node = next(n for n in nodes if isinstance(n, ast.FunctionDef) and n.name == name) + node.decorator_list = [] + exec(compile(ast.Module(body=[node], type_ignores=[]), str(path), 'exec'), ns) + return ns[name] + +def receive(message, severity='info', title='Backup', kind='vzdump'): + ns = {'re': re, 'capture_journal_context': lambda **kw: ''} + class Event: + def __init__(self, **kw): self.__dict__.update(kw); self.event_id = 'inert' + ns['NotificationEvent'] = Event + methods = {name: extract(SCRIPTS / 'notification_events.py', name, 'ProxmoxHookWatcher', ns) + for name in ('_classify_pve', '_map_severity', '_backup_outcome', 'process_webhook')} + class Queue: + def __init__(self): self.items = [] + def put(self, event): self.items.append(event) + class Receiver: + _hostname = 'node-a' + _classify_pve = methods['_classify_pve'] + _map_severity = staticmethod(methods['_map_severity']) + _backup_outcome = staticmethod(methods['_backup_outcome']) + process_webhook = methods['process_webhook'] + def __init__(self): self._queue = Queue() + target = Receiver() + result = target.process_webhook({'fields': {'type': kind}, 'severity': severity, 'title': title, 'message': message}) + assert result['accepted'] and len(target._queue.items) == 1 + return target._queue.items[0] + +def email(event_type, data, severity='INFO', language='en'): + result = templates.render_template(event_type, data, language) + channel = object.__new__(EmailChannel) + channel.subject_prefix = '[ProxMenux]' + context = {**data, 'severity': severity, '_event_type': event_type, '_group': result['group'], '_notification_language': language} + return result, channel._format_html(result['title'], result['body'], severity, context) diff --git a/.github/scripts/tests/test_notification_corrections.py b/.github/scripts/tests/test_notification_corrections.py new file mode 100644 index 00000000..94ae1469 --- /dev/null +++ b/.github/scripts/tests/test_notification_corrections.py @@ -0,0 +1,251 @@ +"""Whole-PR outcome corrections, inert producer/actual email consumers.""" +import html +import unittest +from notification_fixture import templates, receive, email, LANGUAGES + +REPORT = """Details +======= +VMID Name Status Time Size Filename +100 web ok 1m 1s 1 GiB vm/100/2026-09-29T17:00:00Z + +Total running time: 1m 1s +Total size: 1 GiB +""" + +class CorrectionTests(unittest.TestCase): + def test_reversed_finish_is_not_completion_evidence(self): + event = receive('INFO: Finished Backup of VM 100 (00:01:01)\nINFO: Starting Backup of VM 100 (qemu)') + self.assertEqual(event.data['backup_outcome'], 'unconfirmed') + + + def test_interleaved_complete_logs_keep_both_finished_guests(self): + message = ('INFO: Starting Backup of VM 100 (qemu)\nINFO: VM Name: web\n' + 'INFO: Starting Backup of VM 101 (lxc)\nINFO: CT Name: db\n' + 'INFO: Finished Backup of VM 100 (00:01:01)\nINFO: Finished Backup of VM 101 (00:01:02)') + event = receive(message) + self.assertEqual(event.data['backup_outcome'], 'confirmed') + result, markup = email(event.event_type, event.data, event.severity) + self.assertIn('✅ VM web (100)', result['body']) + self.assertIn('✅ CT db (101)', result['body']) + self.assertNotIn('❔', result['body']) + self.assertIn('00:01:01', result['body']) + + + def test_backup_identity_has_event_scoped_mail_compatible_wrapping(self): + result, markup = email('backup_complete', {'hostname': 'n' * 64, + 'backup_outcome': 'confirmed', 'pve_message': 'INFO: Starting Backup of VM 100 (qemu)\nINFO: VM Name: customerproductionpostgresqlreplicaeuropewestdatacenter01\nINFO: Finished Backup of VM 100 (00:01:01)'}) + self.assertIn('table-layout:fixed;', markup) + title_tag = markup.split('
| + | {html_mod.escape(host_label)}: {html_mod.escape(data.get('hostname', ''))} | diff --git a/AppImage/scripts/notification_events.py b/AppImage/scripts/notification_events.py index 6d9ac520..4aa91067 100644 --- a/AppImage/scripts/notification_events.py +++ b/AppImage/scripts/notification_events.py @@ -4294,7 +4294,7 @@ class ProxmoxHookWatcher: """Distinguish explicit failure, complete guest logs and unknown results.""" text = str(message or '') if severity in ('error', 'err', 'critical') or re.search( - r'(?im)^\s*(?:ERROR:|TASK ERROR:|.*\bStatus\s+ERROR\b)', text): + r'(?im)^\s*(?:\d+:\s*)?(?:\d{4}-\d{2}-\d{2}\s+\S+\s+)?(?:ERROR:|TASK ERROR:|.*\bStatus\s+ERROR\b)', text): return 'failed' starts = re.findall(r'(?im)\bStarting Backup of VM (\d+)\s*\(', text) finished = re.findall(r'(?im)\bFinished Backup of VM (\d+)\s*\(', text) @@ -4303,7 +4303,7 @@ class ProxmoxHookWatcher: if table is not None and any(guest['status'].lower() == 'error' for guest in table['vms']): return 'failed' if severity not in ('info', 'ok', 'success') or re.search( - r'(?im)(?:^\s*WARNING:|\bWARNINGS\s*:\s*\d+)', text): + r'(?im)(?:^\s*(?:\d+:\s*)?(?:\d{4}-\d{2}-\d{2}\s+\S+\s+)?WARN(?:ING)?:|\bWARNINGS\s*:\s*\d+)', text): return 'unconfirmed' # A present table is authoritative: do not certify an incomplete table # from a finished guest log, or reject a complete OK table merely @@ -4313,7 +4313,16 @@ class ProxmoxHookWatcher: all(guest['status'].lower() == 'ok' for guest in table['vms']) else 'unconfirmed') if starts: - return 'confirmed' if sorted(starts) == sorted(finished) else 'unconfirmed' + pending = {} + for match in re.finditer(r'(?im)\b(Starting|Finished) Backup of VM (\d+)\s*\(', text): + action, vmid = match.groups() + if action.lower() == 'starting': + pending[vmid] = pending.get(vmid, 0) + 1 + elif not pending.get(vmid): + return 'unconfirmed' # A finish before its start is not evidence. + else: + pending[vmid] -= 1 + return 'confirmed' if not any(pending.values()) else 'unconfirmed' if re.search( r'(?im)^\s*(?:INFO:\s*)?TASK OK\s*$', text): return 'confirmed' @@ -4379,10 +4388,10 @@ class ProxmoxHookWatcher: } if event_type in ('backup_complete', 'backup_fail'): # This is presentation metadata, not a new event/toggle/delivery path. + outcome = self._backup_outcome(severity_raw, message) data['backup_outcome'] = ( - 'failed' if event_type == 'backup_fail' else - self._backup_outcome(severity_raw, message) if pve_type == 'vzdump' - else 'unconfirmed' + 'failed' if event_type == 'backup_fail' or outcome == 'failed' else + outcome if pve_type == 'vzdump' else 'unconfirmed' ) if pve_type == 'replication': diff --git a/AppImage/scripts/notification_manager.py b/AppImage/scripts/notification_manager.py index 2b68d298..332072a9 100644 --- a/AppImage/scripts/notification_manager.py +++ b/AppImage/scripts/notification_manager.py @@ -2467,6 +2467,11 @@ class NotificationManager: runtime_data.get('hostname'), self._config, ) runtime_data.setdefault('_notification_language', self._notification_language()) + # Match queued dispatch's presentation context for these outcome + # notices; this does not alter event/severity or direct-send policy. + if event_type in ('backup_complete', 'backup_fail', 'error_resolved', 'system_restore_completed'): + runtime_data['_event_type'] = event_type + runtime_data['_group'] = TEMPLATES[event_type].get('group', 'other') # Render template if available if event_type in TEMPLATES and not message: diff --git a/AppImage/scripts/notification_templates.py b/AppImage/scripts/notification_templates.py index d0a20dd5..69d6bdbd 100644 --- a/AppImage/scripts/notification_templates.py +++ b/AppImage/scripts/notification_templates.py @@ -238,7 +238,7 @@ def _parse_vzdump_table(message: str) -> Optional[Dict[str, Any]]: valid = False break valid = bool(valid and all(values) - and re.fullmatch(r'(?:\d+:\d{2}:\d{2}|(?:\d+[dhms]\s*)+)', duration) + and re.fullmatch(r'(?:\d+:\d{2}:\d{2}|(?:\d+[yMwdhms]\s*)+)', duration) and re.fullmatch(r'\d+(?:\.\d+)?\s*(?:[KMGTPE]i?B?|B)', size, re.IGNORECASE)) if status.lower() in ('err', 'error'): status = 'error' @@ -309,6 +309,18 @@ def _parse_vzdump_message(message: str) -> Optional[Dict[str, Any]]: } continue + # A finish can belong to a guest already stored when another + # start arrived. Preserve that guest's actual completion too. + prior_finish = re.match(r'Finished Backup of VM (\d+)\s+\(([^)]+)\)', clean) + if prior_finish: + prior = next((vm for vm in reversed(vms) + if vm['vmid'] == prior_finish.group(1)), None) + if prior is not None: + prior['time'] = prior_finish.group(2) + if prior['status'] != 'error': + prior['status'] = 'ok' + continue + if current_vm: # Guest name m_name = re.match(r'(?:CT|VM) Name:\s*(.+)', clean) @@ -357,6 +369,21 @@ def _parse_vzdump_message(message: str) -> Optional[Dict[str, Any]]: if current_vm: vms.append(current_vm) + # Explicit guest-linked failures outrank a contradictory summary OK row. + # Job-level/prune errors do not invalidate unrelated successfully saved guests. + for line in lines: + error = re.match(r'^\s*(?:\d+:\s*)?(?:\d{4}-\d{2}-\d{2}\s+\S+\s+)?(?:ERROR:|TASK ERROR:)\s*(.*)', + line, re.IGNORECASE) + if not error: + continue + failed_guest = re.search(r'\bBackup of (?:VM|CT) (\d+) failed\b|\bbackup failed for (?:VM|CT) (\d+)\b', + error.group(1), re.IGNORECASE) + if failed_guest: + vmid = failed_guest.group(1) or failed_guest.group(2) + for vm in vms: + if vm['vmid'] == vmid: + vm['status'] = 'error' + # ── Extract totals ── for line in lines: m_time = re.search(r'Total running time:\s*(.+)', line) @@ -372,7 +399,7 @@ def _parse_vzdump_message(message: str) -> Optional[Dict[str, Any]]: sizes_gib = 0.0 for vm in vms: s = vm.get('size', '') - m = re.match(r'([\d.]+)\s+(.*)', s) + m = re.fullmatch(r'(\d+(?:\.\d+)?)\s+([KMGTPE]i?B|B)', s, re.IGNORECASE) if m: val = float(m.group(1)) unit = m.group(2).strip().upper() @@ -1869,11 +1896,19 @@ def render_template(event_type: str, data: Dict[str, Any], template['title'] = runtime_message('backup.errorTitle', language, hostname=data.get('hostname') or _get_hostname()) template['body'] = runtime_message('backup.errorBody', language) - if event_type == 'backup_complete' and data.get('backup_outcome') in ('confirmed', 'failed'): + if event_type == 'backup_fail': + template['title'] = runtime_message('backup.errorTitle', language, + hostname=data.get('hostname') or _get_hostname()) + if event_type == 'backup_fail' or (event_type == 'backup_complete' and data.get('backup_outcome') in ('confirmed', 'failed')): parsed_backup = _parse_vzdump_message(str(data.get('pve_message') or '')) storage = str((parsed_backup or {}).get('storage_name') or data.get('storage') or '').strip() guests = (parsed_backup or {}).get('vms') or [] - if data.get('backup_outcome') == 'failed': + # Explicit confirmed manual metadata is useful context, not evidence + # about an unparsed batch. Only use it when there is no raw report. + if not data.get('pve_message') and data.get('backup_outcome') == 'confirmed' and data.get('vmid'): + guests = [{'vmid': str(data['vmid']), 'name': str(data.get('vmname') or ''), + 'type': str(data.get('vm_type') or ''), 'status': 'ok'}] + if event_type == 'backup_fail' or data.get('backup_outcome') == 'failed': guests = [guest for guest in guests if guest.get('status', '').lower() == 'error'] target = [] if storage: @@ -1915,6 +1950,11 @@ def render_template(event_type: str, data: Dict[str, Any], 'log_file': '', } variables.update(data) + if event_type == 'backup_fail' or (event_type == 'backup_complete' and data.get('backup_outcome') in ('confirmed', 'failed')): + # The provider has already substituted raw Display Names. Insert the + # resolved title as a value, never reinterpret its literal braces. + variables['_backup_title'] = template['title'] + template['title'] = '{_backup_title}' # Old persisted errors and manual events may lack a complete reading. # Accept plain numeric strings, but never interpret booleans or objects as @@ -2047,11 +2087,11 @@ def render_template(event_type: str, data: Dict[str, Any], if parsed: is_success = (event_type == 'backup_complete') body_text = _format_vzdump_body(parsed, is_success, language=language) - if event_type == 'backup_complete' and data.get('backup_outcome') == 'failed': - error_lines = [line.strip() for line in pve_message.splitlines() - if re.match(r'^\s*(?:ERROR:|TASK ERROR)', line, re.IGNORECASE)] - if error_lines: - body_text += '\n' + '\n'.join(error_lines) + diagnostic_lines = [line.strip() for line in pve_message.splitlines() + if re.match(r'^\s*(?:\d+:\s*)?(?:\d{4}-\d{2}-\d{2}\s+\S+\s+)?(?:WARN(?:ING)?:|ERROR:|TASK ERROR)', + line, re.IGNORECASE)] + if diagnostic_lines: + body_text += '\n' + '\n'.join(dict.fromkeys(diagnostic_lines)) else: # Couldn't parse -- use PVE raw message as body body_text = pve_message.strip() @@ -2068,6 +2108,28 @@ def render_template(event_type: str, data: Dict[str, Any], except (ValueError, IndexError): body_text = template['body'] + if event_type == 'backup_complete' and data.get('backup_outcome') == 'confirmed' and not pve_message: + context = [] + if data.get('vmid'): + name = str(data.get('vmname') or '') + context.append(f"{name} ({data['vmid']})" if name else str(data['vmid'])) + if data.get('storage'): + context.append(str(data['storage'])) + if data.get('size'): + context.append(runtime_message('vzdump.size', language, value=data['size'])) + if data.get('duration'): + context.append(runtime_message('vzdump.duration', language, value=data['duration'])) + if context: + body_text += '\n' + '\n'.join(context) + + # PVE can move a one-line setup/abort reason exclusively into its subject. + # Preserve that raw failure context, without using it as a localized title. + if event_type in ('backup_complete', 'backup_fail') and ( + event_type == 'backup_fail' or data.get('backup_outcome') == 'failed'): + source_subject = str(data.get('pve_title') or '').strip() + if source_subject and source_subject not in body_text: + body_text += '\n' + source_subject + # Clean up: collapse runs of 3+ blank lines into 1, remove trailing whitespace import re as _re body_text = _re.sub(r'\n{3,}', '\n\n', body_text.strip()) diff --git a/AppImage/scripts/tests/test_notification_runtime_i18n.py b/AppImage/scripts/tests/test_notification_runtime_i18n.py index abd1b127..6242ffae 100644 --- a/AppImage/scripts/tests/test_notification_runtime_i18n.py +++ b/AppImage/scripts/tests/test_notification_runtime_i18n.py @@ -77,9 +77,16 @@ class RuntimeCatalogTests(unittest.TestCase): "channels.email.severity.observation", "channels.email.status.unconfirmed"} for language, catalog in self.catalogs.items(): translated = flatten(catalog) - expected = set(en) - pending_slovak if language == 'sk' else set(en) - self.assertEqual(set(translated), expected, language) - for key in expected: + if language == 'sk': + # Missing maintainer-owned leaves may be generated later. + # Accept only this bounded gap, and validate every present leaf. + self.assertTrue(set(en) - pending_slovak <= set(translated), language) + self.assertTrue(set(translated) <= set(en), language) + else: + self.assertEqual(set(translated), set(en), language) + for key in translated: + self.assertIsInstance(translated[key], str, f"{language}:{key}") + self.assertTrue(translated[key].strip(), f"{language}:{key}") if language == 'sk' and key in ('templates.backup_complete.title', 'templates.backup_complete.body'): continue # exact upstream SK, superseded only at render time diff --git a/AppImage/scripts/tests/test_vzdump_ai_integrity.py b/AppImage/scripts/tests/test_vzdump_ai_integrity.py index 458606bf..2286842c 100644 --- a/AppImage/scripts/tests/test_vzdump_ai_integrity.py +++ b/AppImage/scripts/tests/test_vzdump_ai_integrity.py @@ -275,8 +275,14 @@ class VzdumpAIIntegrityTests(unittest.TestCase): rendered["body"], "CRITICAL", data, ) + # The failure title now identifies the unique failed guest; inventory + # remains exactly once in the detail table, not suppressed from body. + inventory = html.split('', 1)[1].split(' |