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