/
fakemaster.py
220 lines (166 loc) · 5.98 KB
/
fakemaster.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
# 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
import os
import mock
import weakref
from buildbot import config
from buildbot import interfaces
from buildbot.status import build
from buildbot.test.fake import bslavemanager
from buildbot.test.fake import fakedata
from buildbot.test.fake import fakedb
from buildbot.test.fake import fakemq
from buildbot.test.fake import pbmanager
from buildbot.test.fake.botmaster import FakeBotMaster
from buildbot.util import service
from twisted.internet import defer
from zope.interface import implements
class FakeCache(object):
"""Emulate an L{AsyncLRUCache}, but without any real caching. This
I{does} do the weakref part, to catch un-weakref-able objects."""
def __init__(self, name, miss_fn):
self.name = name
self.miss_fn = miss_fn
def get(self, key, **kwargs):
d = self.miss_fn(key, **kwargs)
@d.addCallback
def mkref(x):
if x is not None:
weakref.ref(x)
return x
return d
def put(self, key, val):
pass
class FakeCaches(object):
def get_cache(self, name, miss_fn):
return FakeCache(name, miss_fn)
class FakeStatus(service.BuildbotService):
name = "status"
lastBuilderStatus = None
def builderAdded(self, name, basedir, tags=None, description=None):
bs = FakeBuilderStatus(self.master)
self.lastBuilderStatus = bs
return bs
def getBuilderNames(self):
return []
def getSlaveNames(self):
return []
def slaveConnected(self, name):
pass
def build_started(self, brid, buildername, build_status):
pass
def getURLForBuild(self, builder_name, build_number):
return "URLForBuild/%s/%d" % (builder_name, build_number)
def getURLForBuildrequest(self, buildrequestid):
return "URLForBuildrequest/%d" % (buildrequestid,)
def subscribe(self, _):
pass
def getTitle(self):
return "myBuildbot"
def getURLForThing(self, _):
return "h://thing"
def getBuildbotURL(self):
return "h://bb.me"
class FakeBuilderStatus(object):
implements(interfaces.IBuilderStatus)
def __init__(self, master=None, buildername="Builder"):
if master:
self.master = master
self.botmaster = master.botmaster
self.basedir = os.path.join(master.basedir, 'bldr')
self.lastBuildStatus = None
self._tags = None
self.name = buildername
def setDescription(self, description):
self._description = description
def getDescription(self):
return self._description
def getTags(self):
return self._tags
def setTags(self, tags):
self._tags = tags
def matchesAnyTag(self, tags):
return set(self._tags) & set(tags)
def setSlavenames(self, names):
pass
def setCacheSize(self, size):
pass
def setBigState(self, state):
pass
def newBuild(self):
bld = build.BuildStatus(self, self.master, 3)
self.lastBuildStatus = bld
return bld
def buildStarted(self, builderStatus):
pass
def addPointEvent(self, text):
pass
class FakeLogRotation(object):
rotateLength = 42
maxRotatedFiles = 42
class FakeMaster(service.MasterService):
"""
Create a fake Master instance: a Mock with some convenience
implementations:
- Non-caching implementation for C{self.caches}
"""
def __init__(self, master_id=fakedb.FakeBuildRequestsComponent.MASTER_ID):
service.MasterService.__init__(self)
self._master_id = master_id
self.objectids = []
self.config = config.MasterConfig()
self.caches = FakeCaches()
self.pbmanager = pbmanager.FakePBManager()
self.basedir = 'basedir'
self.botmaster = FakeBotMaster(master=self)
self.botmaster.parent = self
self.status = FakeStatus()
self.status.setServiceParent(self)
self.name = 'fake:/master'
self.masterid = master_id
self.buildslaves = bslavemanager.FakeBuildslaveManager(self)
self.log_rotation = FakeLogRotation()
self.db = mock.Mock()
self.next_objectid = 0
def getObjectId(sched_name, class_name):
k = (sched_name, class_name)
try:
rv = self.objectids[k]
except KeyError:
rv = self.objectids[k] = self.next_objectid
self.next_objectid += 1
return defer.succeed(rv)
self.db.state.getObjectId = getObjectId
def getObjectId(self):
return defer.succeed(self._master_id)
def subscribeToBuildRequests(self, callback):
pass
# Leave this alias, in case we want to add more behavior later
def make_master(wantMq=False, wantDb=False, wantData=False,
testcase=None, url=None, **kwargs):
master = FakeMaster(**kwargs)
if url:
master.buildbotURL = url
if wantData:
wantMq = wantDb = True
if wantMq:
assert testcase is not None, "need testcase for wantMq"
master.mq = fakemq.FakeMQConnector(master, testcase)
if wantDb:
assert testcase is not None, "need testcase for wantDb"
master.db = fakedb.FakeDBConnector(master, testcase)
if wantData:
master.data = fakedata.FakeDataConnector(master, testcase)
return master