This repository has been archived by the owner on Jun 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_misc_nextslaves.py
220 lines (181 loc) · 9.69 KB
/
test_misc_nextslaves.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
from __future__ import with_statement
import time
import tempfile
import mock
from twisted.trial import unittest
import buildbotcustom.misc
from buildbotcustom.misc import _nextSlowIdleSlave, _nextL10nSlave,\
_nextFastSlave, _nextFastReservedSlave, _nextSlowSlave,\
setReservedFileName, _partitionUnreservedSlaves
class TestNextSlaveFuncs(unittest.TestCase):
def setUp(self):
# Reset these each time
buildbotcustom.misc.fastRegexes = ['fast']
buildbotcustom.misc.nReservedSlaves = 0
# Prevent looking for reserved slaves file
buildbotcustom.misc._checkedReservedSlaveFile = time.time()
self.slaves = slaves = []
for name in ('fast1', 'fast2', 'fast3', 'slow1', 'slow2', 'slow3'):
slave = mock.Mock()
slave.slave.slavename = name
slaves.append(slave)
self.slow_slaves = [s for s in self.slaves if "slow" in s.slave.slavename]
self.fast_slaves = [s for s in self.slaves if "fast" in s.slave.slavename]
self.builder = builder = mock.Mock()
builder.builder_status.buildCache.keys.return_value = []
builder.slaves = self.slaves
def test_nextFastSlave_AllAvail(self):
"""Test that _nextFastSlave and _nextFastReservedSlave return a fast
slave when all slaves are available."""
for func in _nextFastReservedSlave, _nextFastSlave:
slave = func(self.builder, self.slaves, only_fast=True)
self.assert_(slave.slave.slavename.startswith("fast"))
def test_nextFastSlave_OnlySlowAvail(self):
"""Test that _nextFastSlave and _nextFastReservedSlave return None
slave when only slow slaves are available, and only_fast is True."""
for func in _nextFastReservedSlave, _nextFastSlave:
slave = func(self.builder, self.slow_slaves, only_fast=True)
self.assert_(slave is None)
def test_nextFastSlave_OnlySlowAvail_notOnlyFast(self):
"""Test that _nextFastSlave and _nextFastReservedSlave return a slow
slave when only slow slaves are available and only_fast is False."""
for func in _nextFastReservedSlave, _nextFastSlave:
slave = func(self.builder, self.slow_slaves, only_fast=False)
self.assert_(slave.slave.slavename.startswith("slow"))
def test_nextFastReservedSlave_reserved(self):
"""Test that _nextFastReservedSlave returns a fast slave if there's one
reserved."""
buildbotcustom.misc.nReservedSlaves = 1
# Only one fast slave available
available_slaves = [s for s in self.slaves if s.slave.slavename == 'fast2']
slave = _nextFastReservedSlave(self.builder, available_slaves)
self.assert_(slave.slave.slavename == "fast2")
def test_nextFastSlave_reserved(self):
"""Test that _nextFastSlave returns None if there's one slave
reserved."""
buildbotcustom.misc.nReservedSlaves = 1
# Only one fast slave available
available_slaves = [s for s in self.slaves if s.slave.slavename == 'fast2']
slave = _nextFastSlave(self.builder, available_slaves)
self.assert_(slave is None)
def test_nextFastSlave_allslow(self):
"""Test that _nextFastSlave works if the builder is configured with
just slow slaves. This handles the case for platforms that don't have a
fast/slow distinction."""
buildbotcustom.misc.nReservedSlaves = 1
self.builder.slaves = self.slow_slaves
slave = _nextFastSlave(self.builder, self.slow_slaves, only_fast=True)
self.assert_(slave.slavename.startswith('slow'))
def test_partitionUnreservedSlaves_allslow_reserved(self):
"""Test that _partitionUnreservedSlaves works if the builder is
configured with just slow slaves and has slave reservations. Some of
the slow slaves should be reserved in this case."""
buildbotcustom.misc.nReservedSlaves = 2
slaves = self.slow_slaves
fast, slow = _partitionUnreservedSlaves(slaves)
self.assert_(len(fast) == 0, [s.slave.slavename for s in fast])
self.assert_(len(slow) == 1, [s.slave.slavename for s in slow])
def test_nextSlowSlave_allslow_allreserved(self):
"""Test that _nextSlowSlave returns None when all slaves are reserved,
and there are only slow slaves."""
buildbotcustom.misc.nReservedSlaves = 3
self.builder.slaves = self.slow_slaves
slave = _nextSlowSlave(self.builder, self.slow_slaves)
self.assert_(slave is None)
def test_nextSlave_allslow_allreserved(self):
"""Test that _nextFastReservedSlave returns None when all slaves are
reserved, and there are only slow slaves."""
buildbotcustom.misc.nReservedSlaves = 3
self.builder.slaves = self.slow_slaves
slave = _nextFastReservedSlave(self.builder, self.slow_slaves, only_fast=True)
self.assert_(slave is not None)
def test_nextSlowSlave(self):
"""Test that _nextSlowSlave returns a slow slave if one is available."""
slave = _nextSlowSlave(self.builder, self.slaves)
self.assert_(slave.slave.slavename.startswith("slow"))
def test_nextSlowSlave_OnlyFastAvail(self):
"""Test that _nextSlowSlave returns a fast slave if no slow slaves are
available."""
slave = _nextSlowSlave(self.builder, self.fast_slaves)
self.assert_(slave.slave.slavename.startswith("fast"))
def test_nextSlowIdleSlave_avail(self):
"""Test that _nextSlowIdleSlave returns a slow slave if enough slow
slaves are available."""
func = _nextSlowIdleSlave(1)
slave = func(self.builder, self.slaves)
self.assert_(slave.slave.slavename.startswith("slow"))
def test_nextSlowIdleSlave_unavail(self):
"""Test that _nextSlowIdleSlave returns None if not enough slow
slaves are available."""
func = _nextSlowIdleSlave(5)
slave = func(self.builder, self.slaves)
self.assert_(slave is None)
def test_nextL10nSlave_avail(self):
"""Test that _nextL10nSlave returns a slow slave if the first slow
slave is available."""
func = _nextL10nSlave(1)
slave = func(self.builder, self.slaves)
self.assert_(slave.slave.slavename == 'slow1')
def test_nextL10nSlave_unavail(self):
"""Test that _nextL10nSlave returns None if the first slow slave is not
available."""
func = _nextL10nSlave(1)
available_slaves = [s for s in self.slaves if s.slave.slavename != 'slow1']
slave = func(self.builder, available_slaves)
self.assert_(slave is None)
def test_update_reserved(self):
"""Test that updates to the reserved file are obeyed, and that calls to
the _nextFast functions pick it up."""
reservedFile = tempfile.NamedTemporaryFile()
buildbotcustom.misc._checkedReservedSlaveFile = 0
# Need to fake out time.time
with mock.patch.object(time, 'time') as time_method:
setReservedFileName(reservedFile.name)
time_method.return_value = 0
self.assertEquals(buildbotcustom.misc.nReservedSlaves, 0)
# Only one fast slave available, but none are reserved yet
available_slaves = [s for s in self.slaves if s.slave.slavename == 'fast2']
slave = _nextFastSlave(self.builder, available_slaves)
self.assert_(slave.slave.slavename == 'fast2')
# Reserve 1 slave
reservedFile.write('1')
reservedFile.flush()
time_method.return_value = 61
# Only one fast slave available, but 1 is reserved
available_slaves = [s for s in self.slaves if s.slave.slavename == 'fast2']
# Check that the regular function doesn't get it
slave = _nextFastSlave(self.builder, available_slaves, only_fast=True)
self.assertEquals(buildbotcustom.misc.nReservedSlaves, 1)
self.assert_(slave is None)
# But our reserved function now does
slave = _nextFastReservedSlave(self.builder, available_slaves, only_fast=True)
self.assert_(slave.slave.slavename == 'fast2')
def test_update_reserved_blank(self):
"""Test that updates to the reserved file are obeyed, and that calls to
the _nextFast functions pick it up."""
reservedFile = tempfile.NamedTemporaryFile()
reservedFile.write('5')
reservedFile.flush()
buildbotcustom.misc._checkedReservedSlaveFile = 0
# Need to fake out time.time
with mock.patch.object(time, 'time') as time_method:
setReservedFileName(reservedFile.name)
time_method.return_value = 61
self.assertEquals(buildbotcustom.misc.nReservedSlaves, 0)
# Only one fast slave available, but all are reserved yet
available_slaves = [s for s in self.slaves if s.slave.slavename == 'fast2']
slave = _nextFastSlave(self.builder, available_slaves)
self.assert_(slave is None)
self.assertEquals(buildbotcustom.misc.nReservedSlaves, 5)
# Empty out reserved slaves file
reservedFile.seek(0)
reservedFile.write('')
reservedFile.truncate()
reservedFile.flush()
time_method.return_value = buildbotcustom.misc._checkedReservedSlaveFile + 61
# Only one fast slave available, but none are reserved
available_slaves = [s for s in self.slaves if s.slave.slavename == 'fast2']
# Check that the regular function gets it
slave = _nextFastSlave(self.builder, available_slaves, only_fast=True)
self.assertEquals(buildbotcustom.misc.nReservedSlaves, 0)
self.assert_(slave.slave.slavename == 'fast2')