1import ./make-test-python.nix {
2 name = "dovecot";
3
4 nodes.machine = { pkgs, ... }: {
5 imports = [ common/user-account.nix ];
6 services.postfix.enable = true;
7 services.dovecot2 = {
8 enable = true;
9 protocols = [ "imap" "pop3" ];
10 modules = [ pkgs.dovecot_pigeonhole ];
11 mailUser = "vmail";
12 mailGroup = "vmail";
13 };
14 environment.systemPackages = let
15 sendTestMail = pkgs.writeScriptBin "send-testmail" ''
16 #!${pkgs.runtimeShell}
17 exec sendmail -vt <<MAIL
18 From: root@localhost
19 To: alice@localhost
20 Subject: Very important!
21
22 Hello world!
23 MAIL
24 '';
25
26 sendTestMailViaDeliveryAgent = pkgs.writeScriptBin "send-lda" ''
27 #!${pkgs.runtimeShell}
28
29 exec ${pkgs.dovecot}/libexec/dovecot/deliver -d bob <<MAIL
30 From: root@localhost
31 To: bob@localhost
32 Subject: Something else...
33
34 I'm running short of ideas!
35 MAIL
36 '';
37
38 testImap = pkgs.writeScriptBin "test-imap" ''
39 #!${pkgs.python3.interpreter}
40 import imaplib
41
42 with imaplib.IMAP4('localhost') as imap:
43 imap.login('alice', 'foobar')
44 imap.select()
45 status, refs = imap.search(None, 'ALL')
46 assert status == 'OK'
47 assert len(refs) == 1
48 status, msg = imap.fetch(refs[0], 'BODY[TEXT]')
49 assert status == 'OK'
50 assert msg[0][1].strip() == b'Hello world!'
51 '';
52
53 testPop = pkgs.writeScriptBin "test-pop" ''
54 #!${pkgs.python3.interpreter}
55 import poplib
56
57 pop = poplib.POP3('localhost')
58 try:
59 pop.user('bob')
60 pop.pass_('foobar')
61 assert len(pop.list()[1]) == 1
62 status, fullmail, size = pop.retr(1)
63 assert status.startswith(b'+OK ')
64 body = b"".join(fullmail[fullmail.index(b""):]).strip()
65 assert body == b"I'm running short of ideas!"
66 finally:
67 pop.quit()
68 '';
69
70 in [ sendTestMail sendTestMailViaDeliveryAgent testImap testPop ];
71 };
72
73 testScript = ''
74 machine.wait_for_unit("postfix.service")
75 machine.wait_for_unit("dovecot2.service")
76 machine.succeed("send-testmail")
77 machine.succeed("send-lda")
78 machine.wait_until_fails('[ "$(postqueue -p)" != "Mail queue is empty" ]')
79 machine.succeed("test-imap")
80 machine.succeed("test-pop")
81 '';
82}