1 import ./make-test-python.nix {
5 smtp1 = { pkgs, ... }: {
6 imports = [ common/user-account.nix ];
8 firewall.allowedTCPPorts = [ 25 ];
10 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [
11 { address = "192.168.1.1"; prefixLength = 24; }
14 environment.systemPackages = [ pkgs.opensmtpd ];
15 services.opensmtpd = {
17 extraServerArgs = [ "-v" ];
18 serverConfiguration = ''
21 # DO NOT DO THIS IN PRODUCTION!
22 # Setting up authentication requires a certificate which is painful in
23 # a test environment, but THIS WOULD BE DANGEROUS OUTSIDE OF A
24 # WELL-CONTROLLED ENVIRONMENT!
25 match from any for any action do_relay
30 smtp2 = { pkgs, ... }: {
31 imports = [ common/user-account.nix ];
33 firewall.allowedTCPPorts = [ 25 143 ];
35 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [
36 { address = "192.168.1.2"; prefixLength = 24; }
39 environment.systemPackages = [ pkgs.opensmtpd ];
40 services.opensmtpd = {
42 extraServerArgs = [ "-v" ];
43 serverConfiguration = ''
45 action dovecot_deliver mda \
46 "${pkgs.dovecot}/libexec/dovecot/deliver -d %{user.username}"
47 match from any for local action dovecot_deliver
53 mailLocation = "maildir:~/mail";
54 protocols = [ "imap" ];
58 client = { pkgs, ... }: {
61 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [
62 { address = "192.168.1.3"; prefixLength = 24; }
65 environment.systemPackages = let
66 sendTestMail = pkgs.writeScriptBin "send-a-test-mail" ''
67 #!${pkgs.python3.interpreter}
70 with smtplib.SMTP('192.168.1.1') as smtp:
71 smtp.sendmail('alice@[192.168.1.1]', 'bob@[192.168.1.2]', """
80 checkMailLanded = pkgs.writeScriptBin "check-mail-landed" ''
81 #!${pkgs.python3.interpreter}
84 with imaplib.IMAP4('192.168.1.2', 143) as imap:
85 imap.login('bob', 'foobar')
87 status, refs = imap.search(None, 'ALL')
90 status, msg = imap.fetch(refs[0], 'BODY[TEXT]')
93 print("===> content:", content)
94 split = content.split(b'\r\n')
95 print("===> split:", split)
97 print("===> lastline:", lastline)
98 assert lastline.strip() == b'Hello World'
100 in [ sendTestMail checkMailLanded ];
107 client.systemctl("start network-online.target")
108 client.wait_for_unit("network-online.target")
109 smtp1.wait_for_unit("opensmtpd")
110 smtp2.wait_for_unit("opensmtpd")
111 smtp2.wait_for_unit("dovecot2")
113 # To prevent sporadic failures during daemon startup, make sure
114 # services are listening on their ports before sending requests
115 smtp1.wait_for_open_port(25)
116 smtp2.wait_for_open_port(25)
117 smtp2.wait_for_open_port(143)
119 client.succeed("send-a-test-mail")
120 smtp1.wait_until_fails("smtpctl show queue | egrep .")
121 smtp2.wait_until_fails("smtpctl show queue | egrep .")
122 client.succeed("check-mail-landed >&2")