aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_process_botmaster_BotMaster.py
blob: ff45c90780172849261a2df91c9064d63014a278 (plain)
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
# 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 mock
from zope.interface import implements
from twisted.trial import unittest
from twisted.internet import defer
from twisted.application import service
from buildbot.process.botmaster import BotMaster
from buildbot.process import factory
from buildbot import config, interfaces
from buildbot.test.fake import fakemaster

class TestCleanShutdown(unittest.TestCase):
    def setUp(self):
        self.botmaster = BotMaster(mock.Mock())
        self.reactor = mock.Mock()
        self.botmaster.startService()

    def assertReactorStopped(self, _=None):
        self.assertTrue(self.reactor.stop.called)

    def assertReactorNotStopped(self, _=None):
        self.assertFalse(self.reactor.stop.called)

    def makeFakeBuild(self):
        self.fake_builder = builder = mock.Mock()
        build = mock.Mock()
        builder.builder_status.getCurrentBuilds.return_value = [build]

        self.build_deferred = defer.Deferred()
        build.waitUntilFinished.return_value = self.build_deferred

        self.botmaster.builders = mock.Mock()
        self.botmaster.builders.values.return_value = [builder]

    def finishFakeBuild(self):
        self.fake_builder.builder_status.getCurrentBuilds.return_value = []
        self.build_deferred.callback(None)

    # tests

    def test_shutdown_idle(self):
        """Test that the master shuts down when it's idle"""
        self.botmaster.cleanShutdown(_reactor=self.reactor)
        self.assertReactorStopped()

    def test_shutdown_busy(self):
        """Test that the master shuts down after builds finish"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # check that we haven't stopped yet, since there's a running build
        self.assertReactorNotStopped()

        # try to shut it down again, just to check that this does not fail
        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # Now we cause the build to finish
        self.finishFakeBuild()

        # And now we should be stopped
        self.assertReactorStopped()

    def test_shutdown_cancel_not_shutting_down(self):
        """Test that calling cancelCleanShutdown when none is in progress
        works"""
        # this just shouldn't fail..
        self.botmaster.cancelCleanShutdown()

    def test_shutdown_cancel(self):
        """Test that we can cancel a shutdown"""
        self.makeFakeBuild()

        self.botmaster.cleanShutdown(_reactor=self.reactor)

        # Next we check that we haven't stopped yet, since there's a running
        # build.
        self.assertReactorNotStopped()

        # but the BuildRequestDistributor should not be running
        self.assertFalse(self.botmaster.brd.running)

        # Cancel the shutdown
        self.botmaster.cancelCleanShutdown()

        # Now we cause the build to finish
        self.finishFakeBuild()

        # We should still be running!
        self.assertReactorNotStopped()

        # and the BuildRequestDistributor should be, as well
        self.assertTrue(self.botmaster.brd.running)


class FakeBuildSlave(config.ReconfigurableServiceMixin, service.Service):

    implements(interfaces.IBuildSlave)

    reconfig_count = 0

    def __init__(self, slavename):
        self.slavename = slavename

    def reconfigService(self, new_config):
        self.reconfig_count += 1
        return defer.succeed(None)


class FakeBuildSlave2(FakeBuildSlave):
    pass


class TestBotMaster(unittest.TestCase):

    def setUp(self):
        self.master = fakemaster.make_master()
        self.botmaster = BotMaster(self.master)
        self.new_config = mock.Mock()
        self.botmaster.startService()

    def tearDown(self):
        return self.botmaster.stopService()

    def test_reconfigService(self):
        # check that reconfigServiceSlaves and reconfigServiceBuilders are
        # both called; they will be tested invidually below
        self.patch(self.botmaster, 'reconfigServiceBuilders',
                mock.Mock(side_effect=lambda c : defer.succeed(None)))
        self.patch(self.botmaster, 'reconfigServiceSlaves',
                mock.Mock(side_effect=lambda c : defer.succeed(None)))
        self.patch(self.botmaster, 'maybeStartBuildsForAllBuilders',
                mock.Mock())

        old_config, new_config = mock.Mock(), mock.Mock()
        d = self.botmaster.reconfigService(new_config)
        @d.addCallback
        def check(_):
            self.botmaster.reconfigServiceBuilders.assert_called_with(
                    new_config)
            self.botmaster.reconfigServiceSlaves.assert_called_with(
                    new_config)
            self.assertTrue(
                    self.botmaster.maybeStartBuildsForAllBuilders.called)
        return d

    @defer.inlineCallbacks
    def test_reconfigServiceSlaves_add_remove(self):
        sl = FakeBuildSlave('sl1')
        self.new_config.slaves = [ sl ]

        yield self.botmaster.reconfigServiceSlaves(self.new_config)

        self.assertIdentical(sl.parent, self.botmaster)
        self.assertEqual(self.botmaster.slaves, { 'sl1' : sl })

        self.new_config.slaves = [ ]

        yield self.botmaster.reconfigServiceSlaves(self.new_config)

        self.assertIdentical(sl.parent, None)
        self.assertIdentical(sl.master, None)

    @defer.inlineCallbacks
    def test_reconfigServiceSlaves_reconfig(self):
        sl = FakeBuildSlave('sl1')
        self.botmaster.slaves = dict(sl1=sl)
        sl.setServiceParent(self.botmaster)
        sl.master = self.master
        sl.botmaster = self.botmaster

        sl_new = FakeBuildSlave('sl1')
        self.new_config.slaves = [ sl_new ]

        yield self.botmaster.reconfigServiceSlaves(self.new_config)

        # sl was not replaced..
        self.assertIdentical(self.botmaster.slaves['sl1'], sl)

    @defer.inlineCallbacks
    def test_reconfigServiceSlaves_class_changes(self):
        sl = FakeBuildSlave('sl1')
        self.botmaster.slaves = dict(sl1=sl)
        sl.setServiceParent(self.botmaster)
        sl.master = self.master
        sl.botmaster = self.botmaster

        sl_new = FakeBuildSlave2('sl1')
        self.new_config.slaves = [ sl_new ]

        yield self.botmaster.reconfigServiceSlaves(self.new_config)

        # sl *was* replaced (different class)
        self.assertIdentical(self.botmaster.slaves['sl1'], sl_new)

    @defer.inlineCallbacks
    def test_reconfigServiceBuilders_add_remove(self):
        bc = config.BuilderConfig(name='bldr', factory=factory.BuildFactory(),
                            slavename='f')
        self.new_config.builders = [ bc ]

        yield self.botmaster.reconfigServiceBuilders(self.new_config)

        bldr = self.botmaster.builders['bldr']
        self.assertIdentical(bldr.parent, self.botmaster)
        self.assertIdentical(bldr.master, self.master)
        self.assertEqual(self.botmaster.builderNames, [ 'bldr' ])

        self.new_config.builders = [ ]

        yield self.botmaster.reconfigServiceBuilders(self.new_config)

        self.assertIdentical(bldr.parent, None)
        self.assertIdentical(bldr.master, None)
        self.assertEqual(self.botmaster.builders, {})
        self.assertEqual(self.botmaster.builderNames, [])

    def test_maybeStartBuildsForBuilder(self):
        brd = self.botmaster.brd = mock.Mock()

        self.botmaster.maybeStartBuildsForBuilder('frank')

        brd.maybeStartBuildsOn.assert_called_once_with(['frank'])

    def test_maybeStartBuildsForSlave(self):
        brd = self.botmaster.brd = mock.Mock()
        b1 = mock.Mock(name='frank')
        b1.name = 'frank'
        b2 = mock.Mock(name='larry')
        b2.name = 'larry'
        self.botmaster.getBuildersForSlave = mock.Mock(return_value=[b1, b2])

        self.botmaster.maybeStartBuildsForSlave('centos')

        self.botmaster.getBuildersForSlave.assert_called_once_with('centos')
        brd.maybeStartBuildsOn.assert_called_once_with(['frank', 'larry'])

    def test_maybeStartBuildsForAll(self):
        brd = self.botmaster.brd = mock.Mock()
        self.botmaster.builderNames = ['frank', 'larry']

        self.botmaster.maybeStartBuildsForAllBuilders()

        brd.maybeStartBuildsOn.assert_called_once_with(['frank', 'larry'])