This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_misc_scheduler_nightly.py
275 lines (220 loc) · 11.6 KB
/
test_misc_scheduler_nightly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import os
import shutil
from twisted.trial import unittest
from buildbot.schedulers.basic import Scheduler
from buildbot.db import dbspec, connector
from buildbot.db.schema.manager import DBSchemaManager
from buildbot.changes.changes import Change
from buildbotcustom.misc_scheduler import lastChange, lastGoodRev, \
getLatestRev, getLastBuiltRevision, lastGoodFunc, lastRevFunc
from buildbotcustom.scheduler import SpecificNightly
import mock
def createTestData(db):
# Set up our test data
# Sourcestamps
db.runQueryNow("""INSERT INTO sourcestamps (`id`, `branch`, `revision`) VALUES (1, 'b1', 'r1')""")
db.runQueryNow("""INSERT INTO sourcestamps (`id`, `branch`, `revision`) VALUES (2, 'b2', 'r2')""")
# Change
db.runQueryNow("""INSERT INTO changes
(`changeid`, `author`, `is_dir`, `comments`, `when_timestamp`, `branch`, `revision`, `revlink`)
VALUES
(1, 'me', 0, 'here', 1, 'b1', 'r1', 'from poller')""")
db.runQueryNow("""INSERT INTO changes
(`changeid`, `author`, `is_dir`, `comments`, `when_timestamp`, `branch`, `revision`, `revlink`)
VALUES
(2, 'me', 0, 'here', 1, 'b2', 'r234567890', 'from poller')""")
db.runQueryNow("""INSERT INTO changes
(`changeid`, `author`, `is_dir`, `comments`, `when_timestamp`, `branch`, `revision`, `revlink`)
VALUES
(3, 'me', 0, 'here', 88200, 'b2-l10n', 'r98765432', 'from poller')""")
# Buildsets
for i in range(1, 3):
db.runQueryNow("""INSERT INTO buildsets (`id`, `sourcestampid`, `submitted_at`) VALUES (%(i)i, %(i)i, 1)""" % dict(i=i))
# Build requests
# Two builders on branch1 that succeed
db.runQueryNow("""INSERT INTO buildrequests (`buildsetid`, `complete`, `results`, `buildername`, `complete_at`, `submitted_at`) VALUES (1, 1, 0, 'builder1', 2, 1)""")
db.runQueryNow("""INSERT INTO buildrequests (`buildsetid`, `complete`, `results`, `buildername`, `complete_at`, `submitted_at`) VALUES (1, 1, 0, 'builder2', 2, 1)""")
# Two builders on branch2, one succeeds, one fails
db.runQueryNow("""INSERT INTO buildrequests (`buildsetid`, `complete`, `results`, `buildername`, `complete_at`, `submitted_at`) VALUES (2, 1, 0, 'builder1', 2, 1)""")
db.runQueryNow("""INSERT INTO buildrequests (`buildsetid`, `complete`, `results`, `buildername`, `complete_at`, `submitted_at`) VALUES (2, 1, 2, 'builder2', 2, 1)""")
class TestLastGoodFuncs(unittest.TestCase):
basedir = "test_misc_scheduler_nightly"
def setUp(self):
if os.path.exists(self.basedir):
shutil.rmtree(self.basedir)
os.makedirs(self.basedir)
spec = dbspec.DBSpec.from_url("sqlite:///state.sqlite", self.basedir)
manager = DBSchemaManager(spec, self.basedir)
manager.upgrade()
self.dbc = connector.DBConnector(spec)
self.dbc.start()
self.s = Scheduler(name="s", builderNames=["b1"])
self.s.parent = mock.Mock()
self.s.parent.db = self.dbc
return self.dbc.addSchedulers([self.s])
def tearDown(self):
self.dbc.stop()
shutil.rmtree(self.basedir)
def test_lastChange(self):
# First, we need to add a few changes!
c1 = Change(who='me!', branch='b1', revision='1', files=[],
comments='really important', revlink='from poller')
c2 = Change(who='me!', branch='b2', revision='2', files=[],
comments='really important', revlink='from poller')
c3 = Change(who='me!', branch='b1', revision='3', files=[],
comments='really important', revlink='from poller')
for c in [c1, c2, c3]:
self.dbc.addChangeToDatabase(c)
c = self.dbc.runInteractionNow(
lambda t: lastChange(self.dbc, t, 'b1'))
self.assertEquals(c.revision, c3.revision)
c = self.dbc.runInteractionNow(
lambda t: lastChange(self.dbc, t, 'b2'))
self.assertEquals(c.revision, c2.revision)
def test_lastChange_ignores_changes_with_no_revlink(self):
c1 = Change(who='me!', branch='b1', revision='1', files=[],
comments='really important')
self.dbc.addChangeToDatabase(c1)
c = self.dbc.runInteractionNow(
lambda t: lastChange(self.dbc, t, 'b1'))
self.assertEquals(c, None)
def test_lastGoodRev(self):
createTestData(self.dbc)
# Check that we can find the good revision for builder1, builder2
rev = self.dbc.runInteractionNow(lambda t: lastGoodRev(
self.dbc, t, 'b1', ['builder1', 'builder2'], 0, 3))
self.assertEquals(rev, 'r1')
# Check that we can't find a good revision after the last builds on
# builder1, builder2 are done.
rev = self.dbc.runInteractionNow(lambda t: lastGoodRev(
self.dbc, t, 'b1', ['builder1', 'builder2'], 4, 6))
self.assertEquals(rev, None)
# Check that we can't find a good revision on branch b2
rev = self.dbc.runInteractionNow(lambda t: lastGoodRev(
self.dbc, t, 'b2', ['builder1', 'builder2'], 0, 3))
self.assertEquals(rev, None)
def test_getLatestRev(self):
# First, we need to add a few changes!
c1 = Change(who='me!', branch='b1', revision='1', files=[], comments='really important', when=1, revlink='from poller')
c2 = Change(who='me!', branch='b2', revision='2', files=[], comments='really important', when=2, revlink='from poller')
c3 = Change(who='me!', branch='b1', revision='3', files=[], comments='really important', when=3, revlink='from poller')
for c in [c1, c2, c3]:
self.dbc.addChangeToDatabase(c)
rev = self.dbc.runInteractionNow(
lambda t: getLatestRev(self.dbc, t, 'b1', '1', '3'))
self.assertEquals(rev, '3')
# Revision 2 isn't on branch b1, so revision 1 should be latest
rev = self.dbc.runInteractionNow(
lambda t: getLatestRev(self.dbc, t, 'b1', '1', '2'))
self.assertEquals(rev, '1')
# Revision 1 and 1 are the same
rev = self.dbc.runInteractionNow(
lambda t: getLatestRev(self.dbc, t, 'b1', '1', '1'))
self.assertEquals(rev, '1')
def test_getLastBuiltRevision(self):
createTestData(self.dbc)
# r1 is the latest on branch b1
rev = self.dbc.runInteractionNow(lambda t: getLastBuiltRevision(
self.dbc, t, 'b1', ['builder1', 'builder2']))
self.assertEquals(rev, 'r1')
# We do LIKE matching on changes.revision so we can get the full
# revisions.
rev = self.dbc.runInteractionNow(lambda t: getLastBuiltRevision(
self.dbc, t, 'b2', ['builder1', 'builder2']))
self.assertEquals(rev, 'r234567890')
# Nothing has happened on branch b3
rev = self.dbc.runInteractionNow(lambda t: getLastBuiltRevision(
self.dbc, t, 'b3', ['builder1', 'builder2']))
self.assertEquals(rev, None)
def test_lastGoodFunc(self):
createTestData(self.dbc)
with mock.patch('time.time') as t:
# Check that ssFunc returns something for both branches
t.return_value = 10
ssFunc = lastGoodFunc('b1', ['builder1', 'builder2'])
ss = self.dbc.runInteractionNow(lambda t: ssFunc(self.s, t))
self.assertEquals(ss.revision, 'r1')
t.return_value = 10
ssFunc = lastGoodFunc('b2', ['builder1', 'builder2'])
ss = self.dbc.runInteractionNow(lambda t: ssFunc(self.s, t))
self.assertEquals(ss.revision, 'r234567890')
# Check that ssFunc returns None if triggerBuildIfNoChanges=False
# and there are no good builds in the past 24 hours.
# We achieve this by faking the clock
t.return_value = 48 * 3600
ssFunc = lastGoodFunc(
'b1', ['builder1', 'builder2'], triggerBuildIfNoChanges=False)
ss = self.dbc.runInteractionNow(lambda t: ssFunc(self.s, t))
self.assertEquals(ss, None)
# Check that ssFunc returns something if triggerBuildIfNoChanges=False
# and there are no good builds in the past 24 hours, but there are
# l10n changes.
t.return_value = 25 * 3600
ssFunc = lastGoodFunc('b2', ['builder1', 'builder2'], triggerBuildIfNoChanges=False, l10nBranch='b2-l10n')
ss = self.dbc.runInteractionNow(lambda t: ssFunc(self.s, t))
self.assertEquals(ss.revision, 'r234567890')
# Check that ssFunc returns None if triggerBuildIfNoChanges=False
# and there are no good builds or l10n changes in the past 24
# hours.
t.return_value = 72 * 3600
ssFunc = lastGoodFunc('b2', ['builder1', 'builder2'], triggerBuildIfNoChanges=False, l10nBranch='b2-l10n')
ss = self.dbc.runInteractionNow(lambda t: ssFunc(self.s, t))
self.assertEquals(ss, None)
def test_lastRevFunc(self):
createTestData(self.dbc)
self.s.builderNames = ['builder1']
# Check that ssFunc returns something for both branches
ssFunc = lastRevFunc('b1')
ss = self.dbc.runInteractionNow(lambda t: ssFunc(self.s, t))
self.assertEquals(ss.revision, 'r1')
ssFunc = lastRevFunc('b2')
ss = self.dbc.runInteractionNow(lambda t: ssFunc(self.s, t))
self.assertEquals(ss.revision, 'r234567890')
# Check that ssFunc returns None if triggerBuildIfNoChanges=False
# and we already built the revision
ssFunc = lastRevFunc('b1', triggerBuildIfNoChanges=False)
ss = self.dbc.runInteractionNow(lambda t: ssFunc(self.s, t))
self.assertEquals(ss, None)
# Check that ssFunc returns the later revision if we already built
# something old
c1 = Change(who='me!', branch='b1', revision='r345', files=[], comments='really important', when=2, revlink='from poller')
self.dbc.addChangeToDatabase(c1)
ssFunc = lastRevFunc('b1', triggerBuildIfNoChanges=False)
ss = self.dbc.runInteractionNow(lambda t: ssFunc(self.s, t))
self.assertEquals(ss.revision, 'r345')
class TestSpecificNightlyScheduler(unittest.TestCase):
basedir = "test_misc_scheduler_nightly_scheduler"
def setUp(self):
if os.path.exists(self.basedir):
shutil.rmtree(self.basedir)
os.makedirs(self.basedir)
spec = dbspec.DBSpec.from_url("sqlite:///state.sqlite", self.basedir)
manager = DBSchemaManager(spec, self.basedir)
manager.upgrade()
self.dbc = connector.DBConnector(spec)
self.dbc.start()
def tearDown(self):
self.dbc.stop()
shutil.rmtree(self.basedir)
def testSpecificNightlyScheduler(self):
createTestData(self.dbc)
ssFunc = lastGoodFunc('b2', ['builder1', 'builder2'])
s = SpecificNightly(ssFunc, name='s', builderNames=['nightly1'])
s.parent = mock.Mock()
s.parent.db = self.dbc
d = self.dbc.addSchedulers([s])
def startBuild(ign):
return self.dbc.runInteractionNow(lambda t: s.start_HEAD_build(t))
d.addCallback(startBuild)
def check(ign):
# Check that we have a buildrequest for revision r1
req = self.dbc.runQueryNow("""
SELECT * FROM buildrequests, buildsets, sourcestamps
WHERE
buildrequests.buildsetid = buildsets.id AND
buildsets.sourcestampid = sourcestamps.id AND
buildername='nightly1' AND
revision = 'r1'
""")
self.assertEquals(len(req), 1)
return d