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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
# Copyright 2010 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Unit tests for TranslationTemplatesBuildBehavior."""
import datetime
import logging
import os
import pytz
from testtools.deferredruntest import AsynchronousDeferredRunTest
import transaction
from twisted.internet import defer
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy
from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.buildmaster.enums import BuildStatus
from lp.buildmaster.interfaces.buildfarmjobbehavior import (
IBuildFarmJobBehavior,
)
from lp.buildmaster.interfaces.buildqueue import IBuildQueueSet
from lp.buildmaster.tests.mock_slaves import (
SlaveTestHelpers,
WaitingSlave,
)
from lp.services.config import config
from lp.services.librarian.interfaces import ILibraryFileAliasSet
from lp.services.librarian.utils import copy_and_close
from lp.testing import TestCaseWithFactory
from lp.testing.fakemethod import FakeMethod
from lp.testing.layers import LaunchpadZopelessLayer
from lp.translations.enums import RosettaImportStatus
from lp.translations.interfaces.translationimportqueue import (
ITranslationImportQueue,
)
from lp.translations.interfaces.translations import (
TranslationsBranchImportMode,
)
class FakeBuilder:
"""Pretend `Builder`."""
def __init__(self, slave):
self.slave = slave
self.cleanSlave = FakeMethod()
def slaveStatus(self):
return self.slave._status
class FakeBuildQueue:
"""Pretend `BuildQueue`."""
def __init__(self, behavior):
"""Pretend to be a BuildQueue item for the given build behavior.
Copies its builder from the behavior object.
"""
self.builder = behavior._builder
self.specific_job = behavior.buildfarmjob
self.date_started = datetime.datetime.now(pytz.UTC)
self.destroySelf = FakeMethod()
class MakeBehaviorMixin(object):
"""Provide common test methods."""
def makeBehavior(self, branch=None, use_fake_chroot=True):
"""Create a TranslationTemplatesBuildBehavior.
Anything that might communicate with build slaves and such
(which we can't really do here) is mocked up.
"""
specific_job = self.factory.makeTranslationTemplatesBuildJob(
branch=branch)
behavior = IBuildFarmJobBehavior(specific_job)
slave = WaitingSlave()
behavior._builder = removeSecurityProxy(self.factory.makeBuilder())
behavior._builder.setSlaveForTesting(slave)
if use_fake_chroot:
lf = self.factory.makeLibraryFileAlias()
self.layer.txn.commit()
behavior._getChroot = lambda: lf
return behavior
def makeProductSeriesWithBranchForTranslation(self):
productseries = self.factory.makeProductSeries()
branch = self.factory.makeProductBranch(
productseries.product)
productseries.branch = branch
productseries.translations_autoimport_mode = (
TranslationsBranchImportMode.IMPORT_TEMPLATES)
return productseries
class TestTranslationTemplatesBuildBehavior(
TestCaseWithFactory, MakeBehaviorMixin):
"""Test `TranslationTemplatesBuildBehavior`."""
layer = LaunchpadZopelessLayer
run_tests_with = AsynchronousDeferredRunTest
def setUp(self):
super(TestTranslationTemplatesBuildBehavior, self).setUp()
self.slave_helper = self.useFixture(SlaveTestHelpers())
def _becomeBuilddMaster(self):
"""Log into the database as the buildd master."""
transaction.commit()
self.layer.switchDbUser(config.builddmaster.dbuser)
def _getBuildQueueItem(self, behavior):
"""Get `BuildQueue` for an `IBuildFarmJobBehavior`."""
job = removeSecurityProxy(behavior.buildfarmjob.job)
return getUtility(IBuildQueueSet).getByJob(job.id)
def test_dispatchBuildToSlave(self):
# dispatchBuildToSlave ultimately causes the slave's build
# method to be invoked. The slave receives the URL of the
# branch it should build from.
behavior = self.makeBehavior()
buildqueue_item = self._getBuildQueueItem(behavior)
self._becomeBuilddMaster()
d = behavior.dispatchBuildToSlave(buildqueue_item, logging)
def got_dispatch((status, info)):
# call_log lives on the mock WaitingSlave and tells us what
# calls to the slave that the behaviour class made.
call_log = behavior._builder.slave.call_log
build_params = call_log[-1]
self.assertEqual('build', build_params[0])
build_type = build_params[2]
self.assertEqual('translation-templates', build_type)
branch_url = build_params[-1]['branch_url']
# The slave receives the public http URL for the branch.
self.assertEqual(
branch_url,
behavior.buildfarmjob.branch.composePublicURL())
return d.addCallback(got_dispatch)
def test_getChroot(self):
# _getChroot produces the current chroot for the current Ubuntu
# release, on the nominated architecture for
# architecture-independent builds.
ubuntu = getUtility(ILaunchpadCelebrities).ubuntu
current_ubuntu = ubuntu.currentseries
distroarchseries = current_ubuntu.nominatedarchindep
# Set an arbitrary chroot file.
fake_chroot_file = getUtility(ILibraryFileAliasSet)[1]
distroarchseries.addOrUpdateChroot(fake_chroot_file)
behavior = self.makeBehavior(use_fake_chroot=False)
chroot = behavior._getChroot()
self.assertNotEqual(None, chroot)
self.assertEqual(fake_chroot_file, chroot)
def test_readTarball(self):
behavior = self.makeBehavior()
buildqueue = FakeBuildQueue(behavior)
path = behavior.templates_tarball_path
# Poke the file we're expecting into the mock slave.
behavior._builder.slave.valid_file_hashes.append(path)
def got_tarball(filename):
tarball = open(filename, 'r')
try:
self.assertEqual(
"This is a %s" % path, tarball.read())
finally:
tarball.close()
os.remove(filename)
d = behavior._readTarball(buildqueue, {path: path}, logging)
return d.addCallback(got_tarball)
def test_updateBuild_WAITING_OK(self):
# Hopefully, a build will succeed and produce a tarball.
behavior = self.makeBehavior()
behavior._uploadTarball = FakeMethod()
queue_item = FakeBuildQueue(behavior)
builder = behavior._builder
d = behavior.dispatchBuildToSlave(queue_item, logging)
def got_dispatch((status, info)):
self.assertEqual(0, queue_item.destroySelf.call_count)
slave_call_log = behavior._builder.slave.call_log
self.assertNotIn('clean', slave_call_log)
self.assertEqual(0, behavior._uploadTarball.call_count)
return builder.slave.status()
def got_status(status):
slave_call_log = behavior._builder.slave.call_log
slave_status = {
'builder_status': status[0],
'build_status': status[1],
'filemap': {'translation-templates.tar.gz': 'foo'},
}
return behavior.updateBuild_WAITING(
queue_item, slave_status, None, logging), slave_call_log
def build_updated(ignored):
self.assertEqual(BuildStatus.FULLYBUILT, behavior.build.status)
# Log file is stored.
self.assertIsNotNone(behavior.build.log)
slave_call_log = behavior._builder.slave.call_log
self.assertEqual(1, queue_item.destroySelf.call_count)
self.assertIn('clean', slave_call_log)
self.assertEqual(1, behavior._uploadTarball.call_count)
d.addCallback(got_dispatch)
d.addCallback(got_status)
d.addCallback(build_updated)
return d
def test_updateBuild_WAITING_failed(self):
# Builds may also fail (and produce no tarball).
behavior = self.makeBehavior()
behavior._uploadTarball = FakeMethod()
queue_item = FakeBuildQueue(behavior)
builder = behavior._builder
d = behavior.dispatchBuildToSlave(queue_item, logging)
def got_dispatch((status, info)):
# Now that we've dispatched, get the status.
return builder.slave.status()
def got_status(status):
raw_status = (
'BuilderStatus.WAITING',
'BuildStatus.FAILEDTOBUILD',
status[2],
)
status_dict = {
'builder_status': raw_status[0],
'build_status': raw_status[1],
}
behavior.updateSlaveStatus(raw_status, status_dict)
self.assertNotIn('filemap', status_dict)
return behavior.updateBuild_WAITING(
queue_item, status_dict, None, logging)
def build_updated(ignored):
self.assertEqual(BuildStatus.FAILEDTOBUILD, behavior.build.status)
# Log file is stored.
self.assertIsNotNone(behavior.build.log)
self.assertEqual(1, queue_item.destroySelf.call_count)
slave_call_log = behavior._builder.slave.call_log
self.assertIn('clean', slave_call_log)
self.assertEqual(0, behavior._uploadTarball.call_count)
d.addCallback(got_dispatch)
d.addCallback(got_status)
d.addCallback(build_updated)
return d
def test_updateBuild_WAITING_notarball(self):
# Even if the build status is "OK," absence of a tarball will
# not faze the Behavior class.
behavior = self.makeBehavior()
behavior._uploadTarball = FakeMethod()
queue_item = FakeBuildQueue(behavior)
builder = behavior._builder
d = behavior.dispatchBuildToSlave(queue_item, logging)
def got_dispatch((status, info)):
return builder.slave.status()
def got_status(status):
raw_status = (
'BuilderStatus.WAITING',
'BuildStatus.OK',
status[2],
)
status_dict = {
'builder_status': raw_status[0],
'build_status': raw_status[1],
}
behavior.updateSlaveStatus(raw_status, status_dict)
self.assertFalse('filemap' in status_dict)
return behavior.updateBuild_WAITING(
queue_item, status_dict, None, logging)
def build_updated(ignored):
self.assertEqual(BuildStatus.FULLYBUILT, behavior.build.status)
self.assertEqual(1, queue_item.destroySelf.call_count)
slave_call_log = behavior._builder.slave.call_log
self.assertIn('clean', slave_call_log)
self.assertEqual(0, behavior._uploadTarball.call_count)
d.addCallback(got_dispatch)
d.addCallback(got_status)
d.addCallback(build_updated)
return d
def test_updateBuild_WAITING_uploads(self):
productseries = self.makeProductSeriesWithBranchForTranslation()
branch = productseries.branch
behavior = self.makeBehavior(branch=branch)
queue_item = FakeBuildQueue(behavior)
builder = behavior._builder
d = behavior.dispatchBuildToSlave(queue_item, logging)
def fake_getFile(sum, file):
dummy_tar = os.path.join(
os.path.dirname(__file__), 'dummy_templates.tar.gz')
tar_file = open(dummy_tar)
copy_and_close(tar_file, file)
return defer.succeed(None)
def got_dispatch((status, info)):
builder.slave.getFile = fake_getFile
builder.slave.filemap = {
'translation-templates.tar.gz': 'foo'}
return builder.slave.status()
def got_status(status):
slave_status = {
'builder_status': status[0],
'build_status': status[1],
'build_id': status[2],
}
behavior.updateSlaveStatus(status, slave_status)
return behavior.updateBuild_WAITING(
queue_item, slave_status, None, logging)
def build_updated(ignored):
self.assertEqual(BuildStatus.FULLYBUILT, behavior.build.status)
entries = getUtility(
ITranslationImportQueue).getAllEntries(target=productseries)
expected_templates = [
'po/domain.pot',
'po-other/other.pot',
'po-thethird/templ3.pot',
]
list1 = sorted(expected_templates)
list2 = sorted([entry.path for entry in entries])
self.assertEqual(list1, list2)
d.addCallback(got_dispatch)
d.addCallback(got_status)
d.addCallback(build_updated)
return d
class TestTTBuildBehaviorTranslationsQueue(
TestCaseWithFactory, MakeBehaviorMixin):
"""Test uploads to the import queue."""
layer = LaunchpadZopelessLayer
def setUp(self):
super(TestTTBuildBehaviorTranslationsQueue, self).setUp()
self.queue = getUtility(ITranslationImportQueue)
self.dummy_tar = os.path.join(
os.path.dirname(__file__), 'dummy_templates.tar.gz')
self.productseries = self.makeProductSeriesWithBranchForTranslation()
self.branch = self.productseries.branch
def test_uploadTarball(self):
# Files from the tarball end up in the import queue.
behavior = self.makeBehavior()
behavior._uploadTarball(
self.branch, file(self.dummy_tar).read(), None)
entries = self.queue.getAllEntries(target=self.productseries)
expected_templates = [
'po/domain.pot',
'po-other/other.pot',
'po-thethird/templ3.pot',
]
paths = [entry.path for entry in entries]
self.assertContentEqual(expected_templates, paths)
def test_uploadTarball_approved(self):
# Uploaded template files are automatically approved.
behavior = self.makeBehavior()
behavior._uploadTarball(
self.branch, file(self.dummy_tar).read(), None)
entries = self.queue.getAllEntries(target=self.productseries)
statuses = [entry.status for entry in entries]
self.assertEqual(
[RosettaImportStatus.APPROVED] * 3, statuses)
def test_uploadTarball_importer(self):
# Files from the tarball are owned by the branch owner.
behavior = self.makeBehavior()
behavior._uploadTarball(
self.branch, file(self.dummy_tar).read(), None)
entries = self.queue.getAllEntries(target=self.productseries)
self.assertEqual(self.branch.owner, entries[0].importer)
|