1 # -*- test-case-name: buildbot.test.test_maildir -*-
3 from twisted
.trial
import unittest
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
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"))
28 log
.msg("removing old maildir")
29 shutil
.rmtree(self
.maildir
)
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
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
:
49 def testMaildir(self
):
51 s
= self
.source
= FCMaildirSource(self
.maildir
)
54 testfiles_dir
= util
.sibpath(__file__
, "mail")
55 testfiles
= [msg
for msg
in os
.listdir(testfiles_dir
)
56 if msg
.startswith("freshcvs")]
59 count
= len(testfiles
)
63 for i
in range(count
):
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
)
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
75 #for i in range(count):
76 # msg, check = test_messages[i]
77 # check(self, self.changes[i])
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