/
fakemaster.py
159 lines (127 loc) · 4.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
# 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 weakref
from twisted.internet import defer, reactor
from twisted.internet.protocol import ServerFactory
from buildbot.mq import connector as mqconnector
from buildbot.data import connector as dataconnector
from buildbot.test.fake import fakedb, fakemq, fakedata
from buildbot.test.fake import pbmanager
from buildbot.www import service
from buildbot.test.fake.botmaster import FakeBotMaster
from buildbot import config
import mock
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)
def mkref(x):
if x is not None:
weakref.ref(x)
return x
d.addCallback(mkref)
return d
class FakeCaches(object):
def get_cache(self, name, miss_fn):
return FakeCache(name, miss_fn)
class FakeStatus(object):
def builderAdded(self, name, basedir, category=None, description=None):
return FakeBuilderStatus()
def getBuilderNames(self):
return []
def getSlaveNames(self):
return []
def slaveConnected(self, name):
pass
class FakeBuilderStatus(object):
def setDescription(self, description):
self._description = description
def getDescription(self):
return self._description
def setCategory(self, category):
self._category = category
def getCategory(self):
return self._category
def setSlavenames(self, names):
pass
def setCacheSize(self, size):
pass
def setBigState(self, state):
pass
class FakeMaster(object):
"""
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):
self._master_id = master_id
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.master = self
self.name = 'fake:/master'
self.masterid = master_id
def getObjectId(self):
return defer.succeed(self._master_id)
def subscribeToBuildRequests(self, callback):
pass
# work around http://code.google.com/p/mock/issues/detail?id=105
def _get_child_mock(self, **kw):
return mock.Mock(**kw)
# Leave this alias, in case we want to add more behavior later
def make_master(wantMq=False, wantDb=False, wantData=False,
testcase=None, **kwargs):
master = FakeMaster(**kwargs)
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
# this config has real mq, real data, real www, but fakedb, and no build engine for ui test
@defer.inlineCallbacks
def make_master_for_uitest(port):
tcp = reactor.listenTCP(port, ServerFactory())
port = tcp._realPortNumber
yield tcp.stopListening()
url = 'http://localhost:'+str(port)+"/"
master = FakeMaster()
master.db = fakedb.FakeDBConnector(master, mock.Mock())
master.mq = mqconnector.MQConnector(master)
master.config.mq = dict(type='simple')
master.mq.setup()
class testHookedDataConnector(dataconnector.DataConnector):
submodules = dataconnector.DataConnector.submodules + ['buildbot.data.testhooks']
master.data = testHookedDataConnector(master)
master.config.www = dict(url=url, port=port)
master.www = service.WWWService(master)
master.data.updates.playTestScenario("buildbot.test.scenarios.base.BaseScenario.populateBaseDb")
yield master.www.startService()
yield master.www.reconfigService(master.config)
defer.returnValue(master)