diff --git a/src/coulobre/hosting/test_service_health.py b/src/coulobre/hosting/test_service_health.py index 96ff76b..bdfb6a8 100644 --- a/src/coulobre/hosting/test_service_health.py +++ b/src/coulobre/hosting/test_service_health.py @@ -1,6 +1,7 @@ import json import subprocess from datetime import timedelta +from io import StringIO from unittest.mock import patch from django.contrib.auth import get_user_model @@ -16,6 +17,7 @@ from coulobre.service_health import ( check_remote, check_unit, load_health_targets, + main, ) from .models import Customer, Reseller, ServiceHealth @@ -99,6 +101,37 @@ class ServiceHealthProbeTests(SimpleTestCase): self.assertIn("StrictHostKeyChecking=yes", command) self.assertEqual(command[-2:], ("--", "mail-02")) + def test_spam_filter_units_can_be_checked_locally_and_remotely(self): + filters = ("spamd", "spamass-milter") + self.assertEqual( + load_health_targets( + '{"panel":{"local":["spamd","spamass-milter"]},' + '"mail":{"ssh":["spamd","spamass-milter"]}}' + ), + ( + HealthTarget("panel", "local", filters), + HealthTarget("mail", "ssh", filters), + ), + ) + output = StringIO() + with ( + patch("sys.stdout", output), + patch( + "coulobre.service_health.subprocess.run", + return_value=subprocess.CompletedProcess( + (), 0, b"LoadState=loaded\nActiveState=active\n" + ), + ) as run, + ): + self.assertEqual(main(), 0) + units = {call.args[0][-1] for call in run.call_args_list} + self.assertTrue({"spamd.service", "spamass-milter.service"} <= units) + report = json.loads(output.getvalue()) + self.assertEqual( + {name: report["services"][name] for name in filters}, + dict.fromkeys(filters, "active"), + ) + @override_settings(SERVICE_HEALTH_TARGETS=TARGETS) class ServiceHealthPanelTests(TestCase): diff --git a/src/coulobre/service_health.py b/src/coulobre/service_health.py index 6c9f08c..4033dc7 100644 --- a/src/coulobre/service_health.py +++ b/src/coulobre/service_health.py @@ -15,8 +15,10 @@ SERVICES = { "bind9": ("BIND9", "named.service"), "postfix": ("Postfix", "postfix.service"), "dovecot": ("Dovecot", "dovecot.service"), + "spamd": ("SpamAssassin", "spamd.service"), + "spamass-milter": ("SpamAssassin milter", "spamass-milter.service"), } -REMOTE_SERVICES = ("bind9", "postfix", "dovecot") +REMOTE_SERVICES = ("bind9", "postfix", "dovecot", "spamd", "spamass-milter") REMOTE_VERSION = 1 SSH_CONFIG = "/etc/coulobre-health/ssh_config" NODE_NAME = re.compile(r"[a-z][a-z0-9-]{0,62}") @@ -172,7 +174,8 @@ def check_remote(target): stdin=subprocess.DEVNULL, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, - timeout=15, + # Allow for SSH setup and sequential unit checks. + timeout=22, check=True, ) except (OSError, subprocess.SubprocessError):