From d43aa431862512839efec8eef6d42a28fd007b40 Mon Sep 17 00:00:00 2001 From: Jonas Braathen Date: Mon, 2 Nov 2026 01:31:03 +0100 Subject: [PATCH] fix: name the user with getpass in sync failure alerts, which cron broke --- mdb/management/commands/dhcp_sync.py | 3 ++- mdb/management/commands/zone_sync.py | 3 ++- tests/test_dhcp_sync_command.py | 4 ++-- tests/test_zone_sync_command.py | 6 +++--- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/mdb/management/commands/dhcp_sync.py b/mdb/management/commands/dhcp_sync.py index f8b7fdb..be40d5e 100644 --- a/mdb/management/commands/dhcp_sync.py +++ b/mdb/management/commands/dhcp_sync.py @@ -1,3 +1,4 @@ +import getpass import os from django.core.management.base import BaseCommand @@ -64,7 +65,7 @@ class Command(BaseCommand): self.stdout.write("fail\n") message = ( "Failed to restart isc-dhcp-server, please inspect...\n\n" - f"{os.getlogin()}@{os.uname()[1]}$ {service.restart_command}\n{output}" + f"{getpass.getuser()}@{os.uname()[1]}$ {service.restart_command}\n{output}" ) discord.notify( subject=f"⚠️ FAIL: dhcpd {old_serial} -> {config.active_serial}", diff --git a/mdb/management/commands/zone_sync.py b/mdb/management/commands/zone_sync.py index abe178f..e16d3e4 100644 --- a/mdb/management/commands/zone_sync.py +++ b/mdb/management/commands/zone_sync.py @@ -1,3 +1,4 @@ +import getpass import os from django.core.management.base import BaseCommand @@ -120,7 +121,7 @@ class Command(BaseCommand): self, summary, failed_command, output, zones, errors="", diffs="" ): zone_list = "\n".join("- " + z for z in zones) - message = f"{summary}, please inspect...\n\n{os.getlogin()}@{os.uname()[1]}$ {failed_command}\n{output}\n{zone_list}" + message = f"{summary}, please inspect...\n\n{getpass.getuser()}@{os.uname()[1]}$ {failed_command}\n{output}\n{zone_list}" if errors: message += "\n\n" + errors discord.notify( diff --git a/tests/test_dhcp_sync_command.py b/tests/test_dhcp_sync_command.py index 2e50da1..3caf9a9 100644 --- a/tests/test_dhcp_sync_command.py +++ b/tests/test_dhcp_sync_command.py @@ -115,7 +115,7 @@ def test_restart_and_notify_success_passes_diff_as_preformatted(cmd): def test_restart_and_notify_failure_writes_fail_and_exits(cmd): svc, config = make_service(restart_success=False, restart_output="Job failed") with patch("mdb.management.commands.dhcp_sync.discord"): - with patch("os.getlogin", return_value="testuser"): + with patch("getpass.getuser", return_value="testuser"): with pytest.raises(SystemExit) as exc: cmd.restart_and_notify(svc, config, old_serial=1, diff="") assert exc.value.code == 1 @@ -125,7 +125,7 @@ def test_restart_and_notify_failure_writes_fail_and_exits(cmd): def test_restart_and_notify_failure_discord_includes_command_and_output(cmd): svc, config = make_service(restart_success=False, restart_output="Job failed") with patch("mdb.management.commands.dhcp_sync.discord") as mock_discord: - with patch("os.getlogin", return_value="testuser"): + with patch("getpass.getuser", return_value="testuser"): with pytest.raises(SystemExit): cmd.restart_and_notify(svc, config, old_serial=1, diff="") subject = mock_discord.notify.call_args.kwargs["subject"] diff --git a/tests/test_zone_sync_command.py b/tests/test_zone_sync_command.py index 6e811aa..a8a05ab 100644 --- a/tests/test_zone_sync_command.py +++ b/tests/test_zone_sync_command.py @@ -99,7 +99,7 @@ def test_reload_and_notify_failure_writes_fail_and_exits(cmd): svc = make_service() svc.reload.return_value = (False, "/usr/sbin/rndc reload", "timed out") with patch("mdb.management.commands.zone_sync.discord"): - with patch("os.getlogin", return_value="testuser"): + with patch("getpass.getuser", return_value="testuser"): with pytest.raises(SystemExit) as exc: cmd.reload_and_notify(svc, svc.changes) assert exc.value.code == 1 @@ -110,7 +110,7 @@ def test_reload_and_notify_failure_discord_includes_command_and_output(cmd): svc = make_service() svc.reload.return_value = (False, "/usr/sbin/rndc reload", "timed out") with patch("mdb.management.commands.zone_sync.discord") as mock_discord: - with patch("os.getlogin", return_value="testuser"): + with patch("getpass.getuser", return_value="testuser"): with pytest.raises(SystemExit): cmd.reload_and_notify(svc, svc.changes) subject = mock_discord.notify.call_args.kwargs["subject"] @@ -187,7 +187,7 @@ def test_handle_writes_nothing_when_freeze_fails(cmd, db_domain): ), patch.object(ZoneSyncService, "run_commands") as mock_run, patch("mdb.management.commands.zone_sync.discord") as mock_discord, - patch("os.getlogin", return_value="testuser"), + patch("getpass.getuser", return_value="testuser"), pytest.raises(SystemExit), ): cmd.handle(debug=False, force=False, zone=None)