1 # -*- test-case-name: buildbot.test.test_bonsaipoller -*-
3 from twisted
.trial
import unittest
4 from buildbot
.changes
.bonsaipoller
import FileNode
, CiNode
, BonsaiResult
, \
5 BonsaiParser
, BonsaiPoller
, InvalidResultError
, EmptyResult
7 from StringIO
import StringIO
8 from copy
import deepcopy
11 log1
= "Add Bug 338541a"
12 who1
= "sar@gmail.com"
14 log2
= "bug 357427 add static ctor/dtor methods"
15 who2
= "aarrg@ooacm.org"
17 log3
= "Testing log #3 lbah blah"
18 who3
= "huoents@hueont.net"
21 file1
= "mozilla/testing/mochitest/tests/index.html"
23 file2
= "mozilla/testing/mochitest/tests/test_bug338541.xhtml"
25 file3
= "mozilla/xpcom/threads/nsAutoLock.cpp"
27 file4
= "mozilla/xpcom/threads/nsAutoLock.h"
29 file5
= "mozilla/xpcom/threads/test.cpp"
33 files
.append(FileNode(rev1
,file1
))
34 nodes
.append(CiNode(log1
, who1
, date1
, files
))
37 files
.append(FileNode(rev2
, file2
))
38 files
.append(FileNode(rev3
, file3
))
39 nodes
.append(CiNode(log2
, who2
, date2
, files
))
41 nodes
.append(CiNode(log3
, who3
, date3
, []))
43 goodParsedResult
= BonsaiResult(nodes
)
45 goodUnparsedResult
= """\
48 <ci who="%s" date="%d">
54 <ci who="%s" date="%d">
61 <ci who="%s" date="%d">
67 """ % (who1
, date1
, log1
, rev1
, file1
,
68 who2
, date2
, log2
, rev2
, file2
, rev3
, file3
,
71 badUnparsedResult
= deepcopy(goodUnparsedResult
)
72 badUnparsedResult
= badUnparsedResult
.replace("</queryResults>", "")
74 invalidDateResult
= deepcopy(goodUnparsedResult
)
75 invalidDateResult
= invalidDateResult
.replace(str(date1
), "foobar")
77 missingRevisionResult
= deepcopy(goodUnparsedResult
)
78 missingRevisionResult
= missingRevisionResult
.replace("rev=\""+rev3
+"\"", "")
80 missingFilenameResult
= deepcopy(goodUnparsedResult
)
81 missingFilenameResult
= missingFilenameResult
.replace(file2
, "")
83 duplicateLogResult
= deepcopy(goodUnparsedResult
)
84 duplicateLogResult
= re
.sub("<log>"+log1
+"</log>",
85 "<log>blah</log><log>blah</log>",
88 duplicateFilesResult
= deepcopy(goodUnparsedResult
)
89 duplicateFilesResult
= re
.sub("<files>\s*</files>",
90 "<files></files><files></files>",
93 missingCiResult
= deepcopy(goodUnparsedResult
)
94 r
= re
.compile("<ci.*</ci>", re
.DOTALL | re
.MULTILINE
)
95 missingCiResult
= re
.sub(r
, "", missingCiResult
)
97 badResultMsgs
= { 'badUnparsedResult':
98 "BonsaiParser did not raise an exception when given a bad query",
100 "BonsaiParser did not raise an exception when given an invalid date",
101 'missingRevisionResult':
102 "BonsaiParser did not raise an exception when a revision was missing",
103 'missingFilenameResult':
104 "BonsaiParser did not raise an exception when a filename was missing",
105 'duplicateLogResult':
106 "BonsaiParser did not raise an exception when there was two <log> tags",
107 'duplicateFilesResult':
108 "BonsaiParser did not raise an exception when there was two <files> tags",
110 "BonsaiParser did not raise an exception when there was no <ci> tags"
113 class FakeBonsaiPoller(BonsaiPoller
):
115 BonsaiPoller
.__init
__(self
, "fake url", "fake module", "fake branch")
117 class TestBonsaiPoller(unittest
.TestCase
):
118 def testFullyFormedResult(self
):
119 br
= BonsaiParser(StringIO(goodUnparsedResult
))
120 result
= br
.getData()
121 # make sure the result is a BonsaiResult
122 self
.failUnless(isinstance(result
, BonsaiResult
))
123 # test for successful parsing
124 self
.failUnlessEqual(goodParsedResult
, result
,
125 "BonsaiParser did not return the expected BonsaiResult")
127 def testBadUnparsedResult(self
):
129 BonsaiParser(StringIO(badUnparsedResult
))
130 self
.fail(badResultMsgs
["badUnparsedResult"])
131 except InvalidResultError
:
134 def testInvalidDateResult(self
):
136 BonsaiParser(StringIO(invalidDateResult
))
137 self
.fail(badResultMsgs
["invalidDateResult"])
138 except InvalidResultError
:
141 def testMissingRevisionResult(self
):
143 BonsaiParser(StringIO(missingRevisionResult
))
144 self
.fail(badResultMsgs
["missingRevisionResult"])
145 except InvalidResultError
:
148 def testMissingFilenameResult(self
):
150 BonsaiParser(StringIO(missingFilenameResult
))
151 self
.fail(badResultMsgs
["missingFilenameResult"])
152 except InvalidResultError
:
155 def testDuplicateLogResult(self
):
157 BonsaiParser(StringIO(duplicateLogResult
))
158 self
.fail(badResultMsgs
["duplicateLogResult"])
159 except InvalidResultError
:
162 def testDuplicateFilesResult(self
):
164 BonsaiParser(StringIO(duplicateFilesResult
))
165 self
.fail(badResultMsgs
["duplicateFilesResult"])
166 except InvalidResultError
:
169 def testMissingCiResult(self
):
171 BonsaiParser(StringIO(missingCiResult
))
172 self
.fail(badResultMsgs
["missingCiResult"])
176 def testChangeNotSubmitted(self
):
177 "Make sure a change is not submitted if the BonsaiParser fails"
178 poller
= FakeBonsaiPoller()
179 lastChangeBefore
= poller
.lastChange
180 poller
._process
_changes
(StringIO(badUnparsedResult
))
181 # self.lastChange will not be updated if the change was not submitted
182 self
.failUnlessEqual(lastChangeBefore
, poller
.lastChange
)