Skip to content

Commit

Permalink
Merge branch 'protocols-fakeconnection' of git://github.com/djmitche/…
Browse files Browse the repository at this point in the history
…buildbot into slave-proto
  • Loading branch information
djmitche committed Oct 16, 2013
2 parents 0ed16dc + b6d7f74 commit deb9f6e
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 43 deletions.
46 changes: 24 additions & 22 deletions master/buildbot/buildslave/protocols/pb.py
Expand Up @@ -106,6 +106,21 @@ def loseConnection(self):
self.stopKeepaliveTimer()
self.mind.broker.transport.loseConnection()

# keepalive handling

def doKeepalive(self):
return self.mind.callRemote('print', message="keepalive")

def stopKeepaliveTimer(self):
if self.keepalive_timer and self.keepalive_timer.active():
self.keepalive_timer.cancel()
self.keepalive_timer = None

def startKeepaliveTimer(self):
assert self.keepalive_interval
self.keepalive_timer = reactor.callLater(self.keepalive_interval,
self.doKeepalive)

# methods to send messages to the slave

def remotePrint(self, message):
Expand Down Expand Up @@ -140,24 +155,12 @@ def cache_builders(builders):
d.addCallback(cache_builders)
return d

# perspective methods called by the slave

def perspective_keepalive(self):
self.buildslave.messageReceivedFromSlave()

def perspective_shutdown(self):
self.buildslave.messageReceivedFromSlave()
self.buildslave.shutdownRequested()

def startCommands(self, remoteCommand, builderName, commandId, commandName, args):
slavebuilder = self.builders.get(builderName)
return slavebuilder.callRemote('startCommand',
remoteCommand, commandId, commandName, args
)

def doKeepalive(self):
return self.mind.callRemote('print', message="keepalive")

@defer.inlineCallbacks
def remoteShutdown(self):
# First, try the "new" way - calling our own remote's shutdown
Expand Down Expand Up @@ -214,16 +217,15 @@ def remoteStartBuild(self, builderName):
slavebuilder = self.builders.get(builderName)
return slavebuilder.callRemote('startBuild')

def stopKeepaliveTimer(self):
if self.keepalive_timer and self.keepalive_timer.active():
self.keepalive_timer.cancel()
self.keepalive_timer = None

def startKeepaliveTimer(self):
assert self.keepalive_interval
self.keepalive_timer = reactor.callLater(self.keepalive_interval,
self.doKeepalive)

def remoteInterruptCommand(self, commandId, why):
return defer.maybeDeferred(self.mind.callRemote, "interruptCommand",
commandId, why)

# perspective methods called by the slave

def perspective_keepalive(self):
self.buildslave.messageReceivedFromSlave()

def perspective_shutdown(self):
self.buildslave.messageReceivedFromSlave()
self.buildslave.shutdownRequested()
58 changes: 58 additions & 0 deletions master/buildbot/test/fake/fakeprotocol.py
@@ -0,0 +1,58 @@
# 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 twisted.internet import defer
from buildbot.buildslave.protocols import base

class FakeConnection(base.Connection):

def __init__(self, master, buildslave):
base.Connection.__init__(self, master, buildslave)
self._connected = True
self.remoteCalls = []
self.builders = {} # { name : isBusy }

# users of the fake can add to this as desired
self.info = {'slave_commands': [], 'version': '0.8.2'}

def remotePrint(self, message):
self.remoteCalls.append(('remotePrint', message))
return defer.succeed(None)

def remoteGetSlaveInfo(self):
self.remoteCalls.append(('remoteGetSlaveInfo',))
return defer.succeed(self.slaveInfo)

def remoteSetBuilderList(self, builders):
self.remoteCalls.append(('remoteSetBuilderList', builders[:]))
self.builders = dict((b, False) for b in builders)
return defer.succeed(None)

def startCommands(self, remoteCommand, builderName, commandId, commandName, args):
self.remoteCalls.append(('startCommands', remoteCommand, builderName,
commandId, commandName, args))
return defer.succeed(None)

def remoteShutdown(self):
self.remoteCalls.append(('remoteShutdown',))
return defer.succeed(None)

def remoteStartBuild(self, builderName):
self.remoteCalls.append(('remoteStartBuild', builderName))
return defer.succeed(None)

def remoteInterruptCommand(self, commandId, why):
self.remoteCalls.append(('remoteInterruptCommand', commandId, why))
return defer.succeed(None)
1 change: 1 addition & 0 deletions master/buildbot/test/fake/slave.py
Expand Up @@ -15,3 +15,4 @@

class FakeSlave(object):
slave_system = 'posix'
slavename = 'fakeslave'
13 changes: 6 additions & 7 deletions master/buildbot/test/unit/test_buildslave_base.py
Expand Up @@ -18,8 +18,7 @@
from twisted.internet import defer, task, reactor
from buildbot import config, locks
from buildbot.buildslave import base
from buildbot.buildslave.protocols import base as protocols_base
from buildbot.test.fake import fakemaster, fakedb, bslavemanager
from buildbot.test.fake import fakemaster, fakedb, bslavemanager, fakeprotocol
from buildbot.test.fake.botmaster import FakeBotMaster

class TestAbstractBuildSlave(unittest.TestCase):
Expand All @@ -40,7 +39,7 @@ def createBuildslave(self, name='bot', password='pass', attached=False, **kwargs
slave.master = self.master
slave.botmaster = self.botmaster
if attached:
slave.conn = mock.Mock(spec=protocols_base.Connection)
slave.conn = fakeprotocol.FakeConnection(self.master, slave)
return slave

def test_constructor_minimal(self):
Expand Down Expand Up @@ -254,7 +253,7 @@ def test_attached_remoteGetSlaveInfo(self):
ENVIRON = {}
COMMANDS = {'cmd1': '1', 'cmd2': '1'}

conn = mock.Mock()
conn = fakeprotocol.FakeConnection(slave.master, slave)
conn.info = {
'admin': 'TheAdmin',
'host': 'TheHost',
Expand All @@ -281,7 +280,7 @@ def test_attached_callsMaybeStartBuildsForSlave(self):
slave = self.createBuildslave()
yield slave.startService()

conn = mock.Mock()
conn = fakeprotocol.FakeConnection(slave.master, slave)
conn.info = {}
yield slave.attached(conn)

Expand All @@ -301,7 +300,7 @@ def test_attached_slaveInfoUpdates(self):
slave = self.createBuildslave()
yield slave.startService()

conn = mock.Mock()
conn = fakeprotocol.FakeConnection(slave.master, slave)
conn.info = {
'admin': 'TheAdmin',
'host': 'TheHost',
Expand Down Expand Up @@ -329,7 +328,7 @@ def test_slave_shutdown(self):
yield slave.startService()

yield slave.shutdown()
slave.conn.remoteShutdown.assert_called_with()
self.assertEqual(slave.conn.remoteCalls, [('remoteShutdown',)])

@defer.inlineCallbacks
def test_slave_shutdown_not_connected(self):
Expand Down
10 changes: 9 additions & 1 deletion master/buildbot/test/unit/test_buildslave_protocols_base.py
Expand Up @@ -17,7 +17,7 @@
import mock
from twisted.trial import unittest
from buildbot.buildslave.protocols import base
from buildbot.test.fake import fakemaster
from buildbot.test.fake import fakemaster, fakeprotocol
from buildbot.test.util import protocols


Expand All @@ -28,6 +28,14 @@ def test_constructor(self):
self.assertEqual(listener.master, master)


class TestFakeConnection(protocols.ConnectionInterfaceTest, unittest.TestCase):

def setUp(self):
self.master = fakemaster.make_master()
self.buildslave = mock.Mock()
self.conn = fakeprotocol.FakeConnection(self.master, self.buildslave)


class TestConnection(protocols.ConnectionInterfaceTest, unittest.TestCase):

def setUp(self):
Expand Down
16 changes: 3 additions & 13 deletions master/buildbot/test/unit/test_process_build.py
Expand Up @@ -22,6 +22,7 @@
from buildbot.status.results import FAILURE, SUCCESS, WARNINGS, RETRY, EXCEPTION
from buildbot.locks import SlaveLock
from buildbot.process.buildstep import LoggingBuildStep
from buildbot.test.fake import slave, fakeprotocol
from buildbot.test.fake.fakemaster import FakeBotMaster
from buildbot import config

Expand Down Expand Up @@ -109,8 +110,10 @@ def setUp(self):

self.master.botmaster = FakeBotMaster(master=self.master)

self.slave = slave.FakeSlave()
self.builder = self.createBuilder()
self.build = Build([r])
self.build.conn = fakeprotocol.FakeConnection(self.master, self.slave)
self.build.setBuilder(self.builder)

def createBuilder(self):
Expand Down Expand Up @@ -477,7 +480,6 @@ def testStepDone(self):
b = self.build
b.results = [SUCCESS]
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
terminate = b.stepDone(SUCCESS, step)
self.assertEqual(terminate, False)
Expand All @@ -487,7 +489,6 @@ def testStepDoneHaltOnFailure(self):
b = self.build
b.results = []
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
step.haltOnFailure = True
terminate = b.stepDone(FAILURE, step)
Expand All @@ -498,7 +499,6 @@ def testStepDoneHaltOnFailureNoFlunkOnFailure(self):
b = self.build
b.results = []
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
step.flunkOnFailure = False
step.haltOnFailure = True
Expand All @@ -510,7 +510,6 @@ def testStepDoneFlunkOnWarningsFlunkOnFailure(self):
b = self.build
b.results = []
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
step.flunkOnFailure = True
step.flunkOnWarnings = True
Expand All @@ -523,7 +522,6 @@ def testStepDoneNoWarnOnWarnings(self):
b = self.build
b.results = [SUCCESS]
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
step.warnOnWarnings = False
terminate = b.stepDone(WARNINGS, step)
Expand All @@ -534,7 +532,6 @@ def testStepDoneWarnings(self):
b = self.build
b.results = [SUCCESS]
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
terminate = b.stepDone(WARNINGS, step)
self.assertEqual(terminate, False)
Expand All @@ -544,7 +541,6 @@ def testStepDoneFail(self):
b = self.build
b.results = [SUCCESS]
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
terminate = b.stepDone(FAILURE, step)
self.assertEqual(terminate, False)
Expand All @@ -554,7 +550,6 @@ def testStepDoneFailOverridesWarnings(self):
b = self.build
b.results = [SUCCESS, WARNINGS]
b.result = WARNINGS
b.conn = Mock()
step = FakeBuildStep()
terminate = b.stepDone(FAILURE, step)
self.assertEqual(terminate, False)
Expand All @@ -564,7 +559,6 @@ def testStepDoneWarnOnFailure(self):
b = self.build
b.results = [SUCCESS]
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
step.warnOnFailure = True
step.flunkOnFailure = False
Expand All @@ -576,7 +570,6 @@ def testStepDoneFlunkOnWarnings(self):
b = self.build
b.results = [SUCCESS]
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
step.flunkOnWarnings = True
terminate = b.stepDone(WARNINGS, step)
Expand All @@ -587,7 +580,6 @@ def testStepDoneHaltOnFailureFlunkOnWarnings(self):
b = self.build
b.results = [SUCCESS]
b.result = SUCCESS
b.conn = Mock()
step = FakeBuildStep()
step.flunkOnWarnings = True
self.haltOnFailure = True
Expand All @@ -599,7 +591,6 @@ def testStepDoneWarningsDontOverrideFailure(self):
b = self.build
b.results = [FAILURE]
b.result = FAILURE
b.conn = Mock()
step = FakeBuildStep()
terminate = b.stepDone(WARNINGS, step)
self.assertEqual(terminate, False)
Expand All @@ -609,7 +600,6 @@ def testStepDoneRetryOverridesAnythingElse(self):
b = self.build
b.results = [RETRY]
b.result = RETRY
b.conn = Mock()
step = FakeBuildStep()
step.alwaysRun = True
b.stepDone(WARNINGS, step)
Expand Down

0 comments on commit deb9f6e

Please sign in to comment.