WebStatus: yes create public_html/ at startup, otherwise we get internal server error...
[buildbot.git] / buildbot / test / test_maildir.py
blobb79cbd319415b4f4a7a211c155de3b1a039b0b36
1 # -*- test-case-name: buildbot.test.test_maildir -*-
3 from twisted.trial import unittest
4 import os, shutil
5 from buildbot.changes.mail import FCMaildirSource
6 from twisted.internet import defer, reactor, task
7 from twisted.python import util, log
9 class TimeOutError(Exception):
10 """The message were not received in a timely fashion"""
12 class MaildirTest(unittest.TestCase):
13 SECONDS_PER_MESSAGE = 1.0
15 def setUp(self):
16 log.msg("creating empty maildir")
17 self.maildir = "test-maildir"
18 if os.path.isdir(self.maildir):
19 shutil.rmtree(self.maildir)
20 log.msg("removing stale maildir")
21 os.mkdir(self.maildir)
22 os.mkdir(os.path.join(self.maildir, "cur"))
23 os.mkdir(os.path.join(self.maildir, "new"))
24 os.mkdir(os.path.join(self.maildir, "tmp"))
25 self.source = None
27 def tearDown(self):
28 log.msg("removing old maildir")
29 shutil.rmtree(self.maildir)
30 if self.source:
31 return self.source.stopService()
33 def addChange(self, c):
34 # NOTE: this assumes every message results in a Change, which isn't
35 # true for msg8-prefix
36 log.msg("got change")
37 self.changes.append(c)
39 def deliverMail(self, msg):
40 log.msg("delivering", msg)
41 newdir = os.path.join(self.maildir, "new")
42 # to do this right, use safecat
43 shutil.copy(msg, newdir)
45 def poll(self, changes, count, d):
46 if len(changes) == count:
47 d.callback("passed")
49 def testMaildir(self):
50 self.changes = []
51 s = self.source = FCMaildirSource(self.maildir)
52 s.parent = self
53 s.startService()
54 testfiles_dir = util.sibpath(__file__, "mail")
55 testfiles = [msg for msg in os.listdir(testfiles_dir)
56 if msg.startswith("freshcvs")]
57 assert testfiles
58 testfiles.sort()
59 count = len(testfiles)
60 d = defer.Deferred()
62 i = 1
63 for i in range(count):
64 msg = testfiles[i]
65 reactor.callLater(self.SECONDS_PER_MESSAGE*i, self.deliverMail,
66 os.path.join(testfiles_dir, msg))
67 self.loop = task.LoopingCall(self.poll, self.changes, count, d)
68 self.loop.start(0.1)
69 t = reactor.callLater(self.SECONDS_PER_MESSAGE*count + 15,
70 d.errback, TimeOutError)
71 # TODO: verify the messages, should use code from test_mailparse but
72 # I'm not sure how to factor the verification routines out in a
73 # useful fashion
75 #for i in range(count):
76 # msg, check = test_messages[i]
77 # check(self, self.changes[i])
79 def _shutdown(res):
80 if t.active():
81 t.cancel()
82 self.loop.stop()
83 return res
84 d.addBoth(_shutdown)
86 return d
88 # TODO: it would be nice to set this timeout after counting the number of
89 # messages in buildbot/test/mail/msg*, but I suspect trial wants to have
90 # this number before the method starts, and maybe even before setUp()
91 testMaildir.timeout = SECONDS_PER_MESSAGE*9 + 15