/
scheduler.py
214 lines (179 loc) · 8.88 KB
/
scheduler.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
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
from buildbot.schedulers import base
from buildbot.test.fake import fakedb
from buildbot.test.fake import fakemaster
from buildbot.test.util import interfaces
from twisted.internet import defer
class SchedulerMixin(interfaces.InterfaceTests):
"""
This class fakes out enough of a master and the various relevant database
connectors to test schedulers. All of the database methods have identical
signatures to the real database connectors, but for ease of testing always
return an already-fired Deferred, meaning that there is no need to wait for
events to complete.
This class is tightly coupled with the various L{buildbot.test.fake.fakedb}
module. All instance variables are only available after C{attachScheduler}
has been called.
@ivar sched: scheduler instance
@ivar master: the fake master
@ivar db: the fake db (same as C{self.master.db}, but shorter)
"""
OTHER_MASTER_ID = 93
def setUpScheduler(self):
self.master = fakemaster.make_master(testcase=self,
wantDb=True, wantMq=True, wantData=True)
def tearDownScheduler(self):
pass
def attachScheduler(self, scheduler, objectid,
overrideBuildsetMethods=False):
"""Set up a scheduler with a fake master and db; sets self.sched, and
sets the master's basedir to the absolute path of 'basedir' in the test
directory.
If C{overrideBuildsetMethods} is true, then all of the
addBuildsetForXxx methods are overriden to simply append the method
name and arguments to self.addBuildsetCalls. These overriden methods
return buildsets starting with 500 and buildrequest IDs starting with
100.
For C{addBuildsetForSourceStamp}, this also overrides DB API methods
C{addSourceStamp} and C{addSourceStampSet}, and uses that information
to generate C{addBuildsetForSourceStamp} results.
@returns: scheduler
"""
scheduler.objectid = objectid
# set up a fake master
db = self.db = self.master.db
self.mq = self.master.mq
scheduler.master = self.master
db.insertTestData([
fakedb.Object(id=objectid, name=scheduler.name,
class_name='SomeScheduler'),
])
if overrideBuildsetMethods:
for method in (
'addBuildsetForSourceStampsWithDefaults',
'addBuildsetForChanges',
'addBuildsetForSourceStamps'):
actual = getattr(scheduler, method)
fake = getattr(self, 'fake_%s' % method)
self.assertArgSpecMatches(actual, fake)
setattr(scheduler, method, fake)
self.addBuildsetCalls = []
self._bsidGenerator = iter(xrange(500, 999))
self._bridGenerator = iter(xrange(100, 999))
# temporarily override the sourcestamp and sourcestampset methods
self.addedSourceStamps = []
self.addedSourceStampSets = []
def fake_addSourceStamp(**kwargs):
self.assertEqual(kwargs['sourcestampsetid'],
400 + len(self.addedSourceStampSets) - 1)
self.addedSourceStamps.append(kwargs)
return defer.succeed(300 + len(self.addedSourceStamps) - 1)
self.db.sourcestamps.addSourceStamp = fake_addSourceStamp
def fake_addSourceStampSet():
self.addedSourceStampSets.append([])
return defer.succeed(400 + len(self.addedSourceStampSets) - 1)
self.db.sourcestamps.addSourceStampSet = fake_addSourceStampSet
# patch methods to detect a failure to upcall the activate and
# deactivate methods .. unless we're testing BaseScheduler
def patch(meth):
oldMethod = getattr(scheduler, meth)
def newMethod():
self._parentMethodCalled = False
d = defer.maybeDeferred(oldMethod)
@d.addCallback
def check(rv):
self.assertTrue(self._parentMethodCalled,
"'%s' did not call its parent" % meth)
return rv
return d
setattr(scheduler, meth, newMethod)
oldParent = getattr(base.BaseScheduler, meth)
def newParent(self_):
self._parentMethodCalled = True
return oldParent(self_)
self.patch(base.BaseScheduler, meth, newParent)
if scheduler.__class__.activate != base.BaseScheduler.activate:
patch('activate')
if scheduler.__class__.deactivate != base.BaseScheduler.deactivate:
patch('deactivate')
self.sched = scheduler
return scheduler
def setSchedulerToMaster(self, otherMaster):
self.master.data.updates.schedulerIds[self.sched.name] = self.sched.objectid
if otherMaster:
self.master.data.updates.schedulerMasters[self.sched.objectid] = otherMaster
else:
del self.master.data.updates.schedulerMasters[self.sched.objectid]
class FakeChange:
who = ''
files = []
comments = ''
isdir = 0
links = None
revision = None
when = None
branch = None
category = None
revlink = ''
properties = {}
repository = ''
project = ''
codebase = ''
def makeFakeChange(self, **kwargs):
"""Utility method to make a fake Change object with the given
attributes"""
ch = self.FakeChange()
ch.__dict__.update(kwargs)
return ch
def _addBuildsetReturnValue(self, builderNames):
# XXX: TODO return builderids
if builderNames is None:
builderNames = self.sched.builderNames
bsid = self._bsidGenerator.next()
brids = dict(zip(builderNames, self._bridGenerator))
return defer.succeed((bsid, brids))
def fake_addBuildsetForSourceStampsWithDefaults(self, reason, sourcestamps,
waited_for=False, properties=None,
builderNames=None, **kw):
properties = properties.asDict()
self.assertIsInstance(sourcestamps, list)
sourcestamps.sort()
self.addBuildsetCalls.append(('addBuildsetForSourceStampsWithDefaults',
dict(reason=reason, sourcestamps=sourcestamps,
waited_for=waited_for, properties=properties,
builderNames=builderNames)))
return self._addBuildsetReturnValue(builderNames)
def fake_addBuildsetForChanges(self, waited_for=False, reason='', external_idstring=None,
changeids=[], builderNames=None, properties=None, **kw):
properties = properties.asDict() if properties is not None else None
self.addBuildsetCalls.append(('addBuildsetForChanges',
dict(waited_for=waited_for, reason=reason,
external_idstring=external_idstring,
changeids=changeids,
properties=properties, builderNames=builderNames,
)))
return self._addBuildsetReturnValue(builderNames)
def fake_addBuildsetForSourceStamps(self, waited_for=False, sourcestamps=[],
reason='', external_idstring=None, properties=None,
builderNames=None, **kw):
properties = properties.asDict() if properties is not None else None
self.assertIsInstance(sourcestamps, list)
sourcestamps.sort()
self.addBuildsetCalls.append(('addBuildsetForSourceStamp',
dict(reason=reason, external_idstring=external_idstring,
properties=properties, builderNames=builderNames,
sourcestamps=sourcestamps)))
return self._addBuildsetReturnValue(builderNames)