diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-10-28 13:48:44 +0100 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-10-28 13:48:44 +0100 |
| commit | b849839d66c353c53548615d8e55c2397027aacb (patch) | |
| tree | 1db8562e3d27d3164daa6bc8d133e295ac9dcf8e /tests/by-name/sn | |
| parent | tests/common/email/dkim: Remove old dkim keys (diff) | |
| download | nixos-server-b849839d66c353c53548615d8e55c2397027aacb.zip | |
tests/simple-nixos-mailserver: Init
These two tests kinda cover our email infrastructure.
Importantly, all new mail gets classified as spam, which is why the
tests do not really cover `rspamd`. As such things like dkim signing,
correct DNS records and so on are not actually covered.
Diffstat (limited to 'tests/by-name/sn')
| -rw-r--r-- | tests/by-name/sn/snm-email-dual-server/test.nix | 134 | ||||
| -rw-r--r-- | tests/by-name/sn/snm-email-single-server/test.nix | 83 |
2 files changed, 217 insertions, 0 deletions
diff --git a/tests/by-name/sn/snm-email-dual-server/test.nix b/tests/by-name/sn/snm-email-dual-server/test.nix new file mode 100644 index 0000000..6f7bebb --- /dev/null +++ b/tests/by-name/sn/snm-email-dual-server/test.nix @@ -0,0 +1,134 @@ +{ + lib, + vhack, + pkgs, + vhackPackages, + ... +}: let + server1Domain = "mail.server1"; + server2Domain = "mail.server2"; + + common = import ../../../common/email/functions.nix {inherit pkgs vhackPackages;}; + + # That's a horrible hack. We should instead wait, until the delivery of the email is + # confirmed. + sleepTime = "40"; +in + vhack.runTest { + name = "email"; + + serverDomains = [ + {server1 = server1Domain;} + {server2 = server2Domain;} + ]; + + nodes = { + server1 = common.mkMailServer server1Domain "alice"; + server2 = common.mkMailServer server2Domain "bob"; + + alice = common.mkUser "alice" server1Domain; + bob = common.mkUser "bob" server2Domain; + }; + + dns = { + server1 = common.mkDnsZone server1Domain; + server2 = common.mkDnsZone server2Domain; + }; + + services = [ + {server1 = "postfix.service";} + {server1 = "dovecot.service";} + {server2 = "postfix.service";} + {server2 = "dovecot.service";} + + {alice = "multi-user.target";} + {bob = "multi-user.target";} + ]; + + ports = [ + {server1 = 993;} # imap + {server1 = 465;} # smtp + + {server2 = 993;} # imap + {server2 = 465;} # smtp + ]; + + testScript = {...}: + # Python + '' + from time import sleep + + with subtest("Dkim keys work"): + server1.succeed("sudo -u rspamd ${pkgs.writeShellScript "dkim-keys-check" '' + set -xe + + stat /run/agenix/mailDkim + + ${lib.getExe pkgs.openssl} pkey -text -noout -in /run/agenix/mailDkim | grep Private-Key + ${lib.getExe pkgs.openssl} pkey -text -noout -in /run/agenix/mailDkim + ''}") + + with subtest("Both start without mail"): + alice.succeed("sudo -u alice ${common.scripts.expectNoNewMail}") + bob.succeed("sudo -u bob ${common.scripts.expectNoNewMail}") + + + with subtest("Alice can send an empty email to bob"): + alice.succeed("sudo -u alice ${pkgs.writeShellScript "alice-send" '' + set -xe + + echo "" | msmtp --debug --account alice bob@${server2Domain} >&2 + ''}") + + # Give the mail server some time to send the email. + sleep(${sleepTime}) + + bob.succeed("sudo -u bob ${common.scripts.expectNewSpamMail}") + + with subtest("Alice can send an non-empty email to bob"): + alice.succeed("sudo -u alice ${pkgs.writeShellScript "alice-send" '' + set -xe + + cat << EOF | msmtp --debug --account alice bob@${server2Domain} >&2 + Subject: Hi bob, I'm Alice! + + Good day, Bob! + + This is an email. + It contains a subject and a body. + I also assert utf8 support by including my last name in this very message. + + XOXO + Alice van Dåligen. + + . + EOF + ''}") + + # Give the mail server some time to send the email. + sleep(${sleepTime}) + + bob.succeed("sudo -u bob ${common.scripts.expectNewSpamMail}") + + with subtest("Bob can send an email to alice"): + bob.succeed("sudo -u bob ${pkgs.writeShellScript "bob-send" '' + set -xe + + cat << EOF | msmtp --debug --account bob alice@${server1Domain} >&2 + Hi Alice! + + Thank you for your emails! (Although they seemed kinda weird tbh :?) + + XXX + Bob Älšter + + . + EOF + ''}") + + # Give the mail server some time to send the email. + sleep(${sleepTime}) + + alice.succeed("sudo -u alice ${common.scripts.expectNewSpamMail}") + ''; + } diff --git a/tests/by-name/sn/snm-email-single-server/test.nix b/tests/by-name/sn/snm-email-single-server/test.nix new file mode 100644 index 0000000..68c6022 --- /dev/null +++ b/tests/by-name/sn/snm-email-single-server/test.nix @@ -0,0 +1,83 @@ +{ + vhack, + pkgs, + vhackPackages, + ... +}: let + domain = "mail.server.test"; + + common = import ../../../common/email/functions.nix {inherit pkgs vhackPackages;}; +in + vhack.runTest { + name = "email"; + + serverDomains = [ + {server = domain;} + ]; + + nodes = { + server = {config, ...}: { + systemd.tmpfiles.rules = [ + #- path mode user group - content + "f /passwords/alice 777 root root - alice-password" + "f /passwords/bob 777 root root - bob-password" + ]; + + vhack = { + mail = { + enable = true; + fqdn = domain; + domains = [ + domain + ]; + accounts = { + "alice@${domain}" = { + passwordFile = "/passwords/alice"; + }; + "bob@${domain}" = { + passwordFile = "/passwords/bob"; + }; + }; + }; + }; + }; + + alice = common.mkUser "alice" domain; + bob = common.mkUser "bob" domain; + }; + + services = [ + {server = "postfix.service";} + ]; + + ports = [ + {server = 993;} # imap + {server = 465;} # smtp + ]; + + testScript = {...}: + # Python + '' + with subtest("Both start without mail"): + alice.succeed("sudo -u alice ${common.scripts.expectNoNewMail}") + bob.succeed("sudo -u bob ${common.scripts.expectNoNewMail}") + + with subtest("Alice can send an email to bob"): + alice.succeed("sudo -u alice ${pkgs.writeShellScript "alice-send" '' + set -xe + + cat << EOF | msmtp --debug --account alice bob@${domain} >&2 + Hi Bob! + + This is an email. + It contains a subject and a body. + + ALICE + EOF + ''}") + bob.succeed("sudo -u bob ${common.scripts.expectNewMail}") + + server.copy_from_machine("/var/lib/", "server") + bob.copy_from_machine("/home/bob/mail", "bob") + ''; + } |
