aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot-0.8.8-py2.7.egg/buildbot/test/unit/test_buildslave_base.py
blob: 250115aa0a27b1b67555a9b6fca293f9515ea100 (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
# 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 twisted.trial import unittest
from twisted.internet import defer
from buildbot import config, locks
from buildbot.buildslave import base
from buildbot.test.fake import fakemaster, pbmanager
from buildbot.test.fake.botmaster import FakeBotMaster

class TestAbstractBuildSlave(unittest.TestCase):

    class ConcreteBuildSlave(base.AbstractBuildSlave):
        pass

    def test_constructor_minimal(self):
        bs = self.ConcreteBuildSlave('bot', 'pass')
        self.assertEqual(bs.slavename, 'bot')
        self.assertEqual(bs.password, 'pass')
        self.assertEqual(bs.max_builds, None)
        self.assertEqual(bs.notify_on_missing, [])
        self.assertEqual(bs.missing_timeout, 3600)
        self.assertEqual(bs.properties.getProperty('slavename'), 'bot')
        self.assertEqual(bs.access, [])
        self.assertEqual(bs.keepalive_interval, 3600)

    def test_constructor_full(self):
        lock1, lock2 = mock.Mock(name='lock1'), mock.Mock(name='lock2')
        bs = self.ConcreteBuildSlave('bot', 'pass',
                max_builds=2,
                notify_on_missing=['me@me.com'],
                missing_timeout=120,
                properties={'a':'b'},
                locks=[lock1, lock2],
                keepalive_interval=60)
        self.assertEqual(bs.max_builds, 2)
        self.assertEqual(bs.notify_on_missing, ['me@me.com'])
        self.assertEqual(bs.missing_timeout, 120)
        self.assertEqual(bs.properties.getProperty('a'), 'b')
        self.assertEqual(bs.access, [lock1, lock2])
        self.assertEqual(bs.keepalive_interval, 60)

    def test_constructor_notify_on_missing_not_list(self):
        bs = self.ConcreteBuildSlave('bot', 'pass',
                notify_on_missing='foo@foo.com')
        # turned into a list:
        self.assertEqual(bs.notify_on_missing, ['foo@foo.com'])

    def test_constructor_notify_on_missing_not_string(self):
        self.assertRaises(config.ConfigErrors, lambda :
            self.ConcreteBuildSlave('bot', 'pass',
                    notify_on_missing=['a@b.com', 13]))

    @defer.inlineCallbacks
    def do_test_reconfigService(self, old, old_port, new, new_port):
        master = self.master = fakemaster.make_master()
        old.master = master
        if old_port:
            self.old_registration = old.registration = \
                    pbmanager.FakeRegistration(master.pbmanager, old_port, old.slavename)
            old.registered_port = old_port
        old.missing_timer = mock.Mock(name='missing_timer')
        old.startService()

        new_config = mock.Mock()
        new_config.slavePortnum = new_port
        new_config.slaves = [ new ]

        yield old.reconfigService(new_config)

    @defer.inlineCallbacks
    def test_reconfigService_attrs(self):
        old = self.ConcreteBuildSlave('bot', 'pass',
                max_builds=2,
                notify_on_missing=['me@me.com'],
                missing_timeout=120,
                properties={'a':'b'},
                keepalive_interval=60)
        new = self.ConcreteBuildSlave('bot', 'pass',
                max_builds=3,
                notify_on_missing=['her@me.com'],
                missing_timeout=121,
                properties={'a':'c'},
                keepalive_interval=61)

        old.updateSlave = mock.Mock(side_effect=lambda : defer.succeed(None))

        yield self.do_test_reconfigService(old, 'tcp:1234', new, 'tcp:1234')

        self.assertEqual(old.max_builds, 3)
        self.assertEqual(old.notify_on_missing, ['her@me.com'])
        self.assertEqual(old.missing_timeout, 121)
        self.assertEqual(old.properties.getProperty('a'), 'c')
        self.assertEqual(old.keepalive_interval, 61)
        self.assertEqual(self.master.pbmanager._registrations, [])
        self.assertTrue(old.updateSlave.called)

    @defer.inlineCallbacks
    def test_reconfigService_has_properties(self):
        old = self.ConcreteBuildSlave('bot', 'pass')
        yield self.do_test_reconfigService(old, 'tcp:1234', old, 'tcp:1234')
        self.assertTrue(old.properties.getProperty('slavename'), 'bot')

    @defer.inlineCallbacks
    def test_reconfigService_initial_registration(self):
        old = self.ConcreteBuildSlave('bot', 'pass')
        yield self.do_test_reconfigService(old, None, old, 'tcp:1234')
        self.assertEqual(self.master.pbmanager._registrations, [('tcp:1234', 'bot', 'pass')])

    @defer.inlineCallbacks
    def test_reconfigService_reregister_password(self):
        old = self.ConcreteBuildSlave('bot', 'pass')
        new = self.ConcreteBuildSlave('bot', 'newpass')

        yield self.do_test_reconfigService(old, 'tcp:1234', new, 'tcp:1234')

        self.assertEqual(old.password, 'newpass')
        self.assertEqual(self.master.pbmanager._unregistrations, [('tcp:1234', 'bot')])
        self.assertEqual(self.master.pbmanager._registrations, [('tcp:1234', 'bot', 'newpass')])

    @defer.inlineCallbacks
    def test_reconfigService_reregister_port(self):
        old = self.ConcreteBuildSlave('bot', 'pass')
        new = self.ConcreteBuildSlave('bot', 'pass')

        yield self.do_test_reconfigService(old, 'tcp:1234', new, 'tcp:5678')

        self.assertEqual(self.master.pbmanager._unregistrations, [('tcp:1234', 'bot')])
        self.assertEqual(self.master.pbmanager._registrations, [('tcp:5678', 'bot', 'pass')])

    @defer.inlineCallbacks
    def test_stopService(self):
        master = self.master = fakemaster.make_master()
        slave = self.ConcreteBuildSlave('bot', 'pass')
        slave.master = master
        slave.startService()

        config = mock.Mock()
        config.slavePortnum = "tcp:1234"
        config.slaves = [ slave ]

        yield slave.reconfigService(config)
        yield slave.stopService()

        self.assertEqual(self.master.pbmanager._unregistrations, [('tcp:1234', 'bot')])
        self.assertEqual(self.master.pbmanager._registrations, [('tcp:1234', 'bot', 'pass')])

    # FIXME: Test that reconfig properly deals with
    #   1) locks
    #   2) telling slave about builder
    #   3) missing timer
    # in both the initial config and a reconfiguration.

    def test_startMissingTimer_no_parent(self):
        bs = self.ConcreteBuildSlave('bot', 'pass',
                notify_on_missing=['abc'],
                missing_timeout=10)
        bs.startMissingTimer()
        self.assertEqual(bs.missing_timer, None)

    def test_startMissingTimer_no_timeout(self):
        bs = self.ConcreteBuildSlave('bot', 'pass',
                notify_on_missing=['abc'],
                missing_timeout=0)
        bs.parent = mock.Mock()
        bs.startMissingTimer()
        self.assertEqual(bs.missing_timer, None)

    def test_startMissingTimer_no_notify(self):
        bs = self.ConcreteBuildSlave('bot', 'pass',
                missing_timeout=3600)
        bs.parent = mock.Mock()
        bs.startMissingTimer()
        self.assertEqual(bs.missing_timer, None)

    def test_missing_timer(self):
        bs = self.ConcreteBuildSlave('bot', 'pass',
                notify_on_missing=['abc'],
                missing_timeout=100)
        bs.parent = mock.Mock()
        bs.startMissingTimer()
        self.assertNotEqual(bs.missing_timer, None)
        bs.stopMissingTimer()
        self.assertEqual(bs.missing_timer, None)

    def test_setServiceParent_started(self):
        master = self.master = fakemaster.make_master()
        botmaster = FakeBotMaster(master)
        botmaster.startService()
        bs = self.ConcreteBuildSlave('bot', 'pass')
        bs.setServiceParent(botmaster)
        self.assertEqual(bs.botmaster, botmaster)
        self.assertEqual(bs.master, master)

    def test_setServiceParent_masterLocks(self):
        """
        http://trac.buildbot.net/ticket/2278
        """
        master = self.master = fakemaster.make_master()
        botmaster = FakeBotMaster(master)
        botmaster.startService()
        lock = locks.MasterLock('masterlock')
        bs = self.ConcreteBuildSlave('bot', 'pass', locks = [lock.access("counting")])
        bs.setServiceParent(botmaster)

    def test_setServiceParent_slaveLocks(self):
        """
        http://trac.buildbot.net/ticket/2278
        """
        master = self.master = fakemaster.make_master()
        botmaster = FakeBotMaster(master)
        botmaster.startService()
        lock = locks.SlaveLock('lock')
        bs = self.ConcreteBuildSlave('bot', 'pass', locks = [lock.access("counting")])
        bs.setServiceParent(botmaster)