at 23.11-pre 4.0 kB view raw
1import ./make-test-python.nix { 2 name = "opensmtpd"; 3 4 nodes = { 5 smtp1 = { pkgs, ... }: { 6 imports = [ common/user-account.nix ]; 7 networking = { 8 firewall.allowedTCPPorts = [ 25 ]; 9 useDHCP = false; 10 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [ 11 { address = "192.168.1.1"; prefixLength = 24; } 12 ]; 13 }; 14 environment.systemPackages = [ pkgs.opensmtpd ]; 15 services.opensmtpd = { 16 enable = true; 17 extraServerArgs = [ "-v" ]; 18 serverConfiguration = '' 19 listen on 0.0.0.0 20 action do_relay relay 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 26 ''; 27 }; 28 }; 29 30 smtp2 = { pkgs, ... }: { 31 imports = [ common/user-account.nix ]; 32 networking = { 33 firewall.allowedTCPPorts = [ 25 143 ]; 34 useDHCP = false; 35 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [ 36 { address = "192.168.1.2"; prefixLength = 24; } 37 ]; 38 }; 39 environment.systemPackages = [ pkgs.opensmtpd ]; 40 services.opensmtpd = { 41 enable = true; 42 extraServerArgs = [ "-v" ]; 43 serverConfiguration = '' 44 listen on 0.0.0.0 45 action dovecot_deliver mda \ 46 "${pkgs.dovecot}/libexec/dovecot/deliver -d %{user.username}" 47 match from any for local action dovecot_deliver 48 ''; 49 }; 50 services.dovecot2 = { 51 enable = true; 52 enableImap = true; 53 mailLocation = "maildir:~/mail"; 54 protocols = [ "imap" ]; 55 }; 56 }; 57 58 client = { pkgs, ... }: { 59 networking = { 60 useDHCP = false; 61 interfaces.eth1.ipv4.addresses = pkgs.lib.mkOverride 0 [ 62 { address = "192.168.1.3"; prefixLength = 24; } 63 ]; 64 }; 65 environment.systemPackages = let 66 sendTestMail = pkgs.writeScriptBin "send-a-test-mail" '' 67 #!${pkgs.python3.interpreter} 68 import smtplib, sys 69 70 with smtplib.SMTP('192.168.1.1') as smtp: 71 smtp.sendmail('alice@[192.168.1.1]', 'bob@[192.168.1.2]', """ 72 From: alice@smtp1 73 To: bob@smtp2 74 Subject: Test 75 76 Hello World 77 """) 78 ''; 79 80 checkMailLanded = pkgs.writeScriptBin "check-mail-landed" '' 81 #!${pkgs.python3.interpreter} 82 import imaplib 83 84 with imaplib.IMAP4('192.168.1.2', 143) as imap: 85 imap.login('bob', 'foobar') 86 imap.select() 87 status, refs = imap.search(None, 'ALL') 88 assert status == 'OK' 89 assert len(refs) == 1 90 status, msg = imap.fetch(refs[0], 'BODY[TEXT]') 91 assert status == 'OK' 92 content = msg[0][1] 93 print("===> content:", content) 94 split = content.split(b'\r\n') 95 print("===> split:", split) 96 lastline = split[-3] 97 print("===> lastline:", lastline) 98 assert lastline.strip() == b'Hello World' 99 ''; 100 in [ sendTestMail checkMailLanded ]; 101 }; 102 }; 103 104 testScript = '' 105 start_all() 106 107 client.wait_for_unit("network-online.target") 108 smtp1.wait_for_unit("opensmtpd") 109 smtp2.wait_for_unit("opensmtpd") 110 smtp2.wait_for_unit("dovecot2") 111 112 # To prevent sporadic failures during daemon startup, make sure 113 # services are listening on their ports before sending requests 114 smtp1.wait_for_open_port(25) 115 smtp2.wait_for_open_port(25) 116 smtp2.wait_for_open_port(143) 117 118 client.succeed("send-a-test-mail") 119 smtp1.wait_until_fails("smtpctl show queue | egrep .") 120 smtp2.wait_until_fails("smtpctl show queue | egrep .") 121 client.succeed("check-mail-landed >&2") 122 ''; 123 124 meta.timeout = 1800; 125}