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
|
# Copyright 2011 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
__metaclass__ = type
from datetime import (
datetime,
timedelta,
)
import pytz
from textwrap import dedent
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy
from canonical.config import config
from canonical.launchpad.webapp import canonical_url
from canonical.testing.layers import LaunchpadFunctionalLayer
from lp.app.browser.tales import DurationFormatterAPI
from lp.archivepublisher.utils import get_ppa_reference
from lp.buildmaster.enums import BuildStatus
from lp.registry.interfaces.person import IPersonSet
from lp.soyuz.enums import ArchivePurpose
from lp.soyuz.interfaces.publishing import PackagePublishingPocket
from lp.soyuz.tests.test_publishing import SoyuzTestPublisher
from lp.testing import (
person_logged_in,
TestCaseWithFactory,
)
from lp.testing.mail_helpers import pop_notifications
from lp.testing.sampledata import ADMIN_EMAIL
class TestBuildNotify(TestCaseWithFactory):
layer = LaunchpadFunctionalLayer
def setUp(self):
super(TestBuildNotify, self).setUp()
self.admin = getUtility(IPersonSet).getByEmail(ADMIN_EMAIL)
# Create all of the items we need to create builds
self.pf = self.factory.makeProcessorFamily()
pf_proc = self.pf.addProcessor(self.factory.getUniqueString(), '', '')
self.distroseries = self.factory.makeDistroSeries()
self.das = self.factory.makeDistroArchSeries(
distroseries=self.distroseries, processorfamily=self.pf,
supports_virtualized=True)
self.creator = self.factory.makePerson(email='test@example.com')
self.gpgkey = self.factory.makeGPGKey(owner=self.creator)
self.archive = self.factory.makeArchive(
distribution=self.distroseries.distribution,
purpose=ArchivePurpose.PRIMARY)
self.ppa = self.factory.makeArchive()
buildd_admins = getUtility(IPersonSet).getByName(
'launchpad-buildd-admins')
self.buildd_admins_email = []
with person_logged_in(self.admin):
self.publisher = SoyuzTestPublisher()
self.publisher.prepareBreezyAutotest()
self.distroseries.nominatedarchindep = self.das
self.publisher.addFakeChroots(distroseries=self.distroseries)
self.builder = self.factory.makeBuilder(processor=pf_proc)
for member in buildd_admins.activemembers:
self.buildd_admins_email.append(member.preferredemail.email)
self.builds = []
def create_builds(self, archive):
for status in BuildStatus.items:
spph = self.publisher.getPubSource(
sourcename=self.factory.getUniqueString(),
version="%s.%s" % (
self.factory.getUniqueInteger(), status.value),
distroseries=self.distroseries, architecturehintlist='any',
creator=self.creator, archive=archive)
spph.sourcepackagerelease.dscsigningkey = self.gpgkey
[build] = spph.createMissingBuilds()
with person_logged_in(self.admin):
build.status = status
build.builder = self.builder
if status != BuildStatus.BUILDING:
build.buildqueue_record.destroySelf()
else:
build.buildqueue_record.builder = self.builder
build.date_started = datetime.now(pytz.UTC)
build.date_finished = build.date_started + timedelta(
minutes=5 * (status.value + 1))
self.builds.append(build)
def _assert_mail_is_correct(self, build, notification, ppa=False):
# Assert that the mail sent (which is in notification), matches
# the data from the build
self.assertEquals('test@example.com',
notification['X-Creator-Recipient'])
self.assertEquals(
self.das.architecturetag, notification['X-Launchpad-Build-Arch'])
self.assertEquals(
'main', notification['X-Launchpad-Build-Component'])
self.assertEquals(
build.status.name, notification['X-Launchpad-Build-State'])
if ppa is True:
self.assertEquals(
get_ppa_reference(self.ppa), notification['X-Launchpad-PPA'])
body = notification.get_payload(decode=True)
duration = DurationFormatterAPI(build.duration).approximateduration()
build_log = 'None'
if ppa is True:
archive = '%s PPA' % get_ppa_reference(build.archive)
source = 'not available'
else:
archive = '%s primary archive' % (
self.distroseries.distribution.name)
source = canonical_url(build.distributionsourcepackagerelease)
builder = canonical_url(build.builder)
if build.status == BuildStatus.BUILDING:
duration = 'not finished'
build_log = 'see builder page'
elif (
build.status == BuildStatus.SUPERSEDED or
build.status == BuildStatus.NEEDSBUILD):
duration = 'not available'
build_log = 'not available'
builder = 'not available'
elif build.status == BuildStatus.UPLOADING:
duration = 'uploading'
build_log = 'see builder page'
builder = 'not available'
expected_body = dedent("""
* Source Package: %s
* Version: %s
* Architecture: %s
* Archive: %s
* Component: main
* State: %s
* Duration: %s
* Build Log: %s
* Builder: %s
* Source: %s
If you want further information about this situation, feel free to
contact a member of the Launchpad Buildd Administrators team.
--
%s
%s
""" % (
build.source_package_release.sourcepackagename.name,
build.source_package_release.version, self.das.architecturetag,
archive, build.status.title, duration, build_log, builder,
source, build.title, canonical_url(build)))
self.assertEquals(expected_body, body)
def test_notify_buildd_admins(self):
# A build will cause an e-mail to be sent out to the buildd-admins,
# for primary archive builds.
self.create_builds(self.archive)
build = self.builds[BuildStatus.FAILEDTOBUILD.value]
build.notify()
expected_emails = self.buildd_admins_email + ['test@example.com']
notifications = pop_notifications()
actual_emails = [n['To'] for n in notifications]
self.assertEquals(expected_emails, actual_emails)
def test_ppa_does_not_notify_buildd_admins(self):
# A build for a PPA does not notify the buildd admins.
self.create_builds(self.ppa)
build = self.builds[BuildStatus.FAILEDTOBUILD.value]
build.notify()
notifications = pop_notifications()
# An e-mail is sent to the archive owner, as well as the creator
self.assertEquals(2, len(notifications))
def test_notify_failed_to_build(self):
# An e-mail is sent to the source package creator on build failures.
self.create_builds(self.archive)
build = self.builds[BuildStatus.FAILEDTOBUILD.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification)
def test_notify_failed_to_build_ppa(self):
# An e-mail is sent to the source package creator on build failures.
self.create_builds(archive=self.ppa)
build = self.builds[BuildStatus.FAILEDTOBUILD.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification, ppa=True)
def test_notify_needs_building(self):
# We can notify the creator when the build is needing to be built.
self.create_builds(self.archive)
build = self.builds[BuildStatus.NEEDSBUILD.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification)
def test_notify_needs_building_ppa(self):
# We can notify the creator when the build is needing to be built.
self.create_builds(self.ppa)
build = self.builds[BuildStatus.NEEDSBUILD.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification, ppa=True)
def test_notify_successfully_built(self):
# Successful builds don't notify anyone.
self.create_builds(self.archive)
build = self.builds[BuildStatus.FULLYBUILT.value]
build.notify()
self.assertEqual([], pop_notifications())
def test_notify_dependency_wait(self):
# We can notify the creator when the build can't find a dependency.
self.create_builds(self.archive)
build = self.builds[BuildStatus.MANUALDEPWAIT.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification)
def test_notify_dependency_wait_ppa(self):
# We can notify the creator when the build can't find a dependency.
self.create_builds(self.ppa)
build = self.builds[BuildStatus.MANUALDEPWAIT.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification, ppa=True)
def test_notify_chroot_problem(self):
# We can notify the creator when the builder the build attempted to
# be built on has an internal problem.
self.create_builds(self.archive)
build = self.builds[BuildStatus.CHROOTWAIT.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification)
def test_notify_chroot_problem_ppa(self):
# We can notify the creator when the builder the build attempted to
# be built on has an internal problem.
self.create_builds(self.ppa)
build = self.builds[BuildStatus.CHROOTWAIT.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification, ppa=True)
def test_notify_build_for_superseded_source(self):
# We can notify the creator when the source package had a newer
# version uploaded before this build had a chance to be dispatched.
self.create_builds(self.archive)
build = self.builds[BuildStatus.SUPERSEDED.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification)
def test_notify_build_for_superseded_source_ppa(self):
# We can notify the creator when the source package had a newer
# version uploaded before this build had a chance to be dispatched.
self.create_builds(self.ppa)
build = self.builds[BuildStatus.SUPERSEDED.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification, ppa=True)
def test_notify_currently_building(self):
# We can notify the creator when the build is currently building.
self.create_builds(self.archive)
build = self.builds[BuildStatus.BUILDING.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification)
def test_notify_currently_building_ppa(self):
# We can notify the creator when the build is currently building.
self.create_builds(self.ppa)
build = self.builds[BuildStatus.BUILDING.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification, ppa=True)
def test_notify_uploading_build(self):
# We can notify the creator when the build has completed, and binary
# packages are being uploaded by the builder.
self.create_builds(self.archive)
build = self.builds[BuildStatus.UPLOADING.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification)
def test_notify_uploading_build_ppa(self):
# We can notify the creator when the build has completed, and binary
# packages are being uploaded by the builder.
self.create_builds(self.ppa)
build = self.builds[BuildStatus.UPLOADING.value]
build.notify()
notification = pop_notifications()[1]
self._assert_mail_is_correct(build, notification, ppa=True)
def test_copied_into_ppa_does_not_spam(self):
# When a package is copied into a PPA, we don't send mail to the
# original creator of the source package.
self.create_builds(self.archive)
build = self.builds[BuildStatus.FULLYBUILT.value]
spph = build.current_source_publication
ppa_spph = spph.copyTo(
self.distroseries, PackagePublishingPocket.RELEASE, self.ppa)
[ppa_build] = ppa_spph.createMissingBuilds()
ppa_build.notify()
notifications = pop_notifications()
self.assertEquals(1, len(notifications))
def test_notify_owner_supresses_mail(self):
# When the 'notify_owner' config option is False, we don't send mail
# to the owner of the SPR.
self.create_builds(self.archive)
build = self.builds[BuildStatus.FAILEDTOBUILD.value]
notify_owner = dedent("""
[builddmaster]
send_build_notification: True
notify_owner: False
""")
config.push('notify_owner', notify_owner)
build.notify()
notifications = pop_notifications()
actual_emails = [n['To'] for n in notifications]
self.assertEquals(self.buildd_admins_email, actual_emails)
# And undo what we just did.
config_data = config.pop('notify_owner')
def test_build_notification_supresses_mail(self):
# When the 'build_notification' config option is False, we don't
# send any mail at all.
self.create_builds(self.archive)
build = self.builds[BuildStatus.FULLYBUILT.value]
send_build_notification = dedent("""
[builddmaster]
send_build_notification: False
""")
config.push('send_build_notification', send_build_notification)
build.notify()
notifications = pop_notifications()
self.assertEquals(0, len(notifications))
# And undo what we just did.
config_data = config.pop('send_build_notification')
def test_sponsored_upload_notification(self):
# If the signing key is different to the creator, they are both
# notified.
sponsor = self.factory.makePerson('sponsor@example.com')
key = self.factory.makeGPGKey(owner=sponsor)
self.create_builds(self.archive)
build = self.builds[BuildStatus.FAILEDTOBUILD.value]
spr = build.current_source_publication.sourcepackagerelease
# Push past the security proxy
removeSecurityProxy(spr).dscsigningkey = key
build.notify()
notifications = pop_notifications()
expected_emails = self.buildd_admins_email + [
'sponsor@example.com', 'test@example.com']
actual_emails = [n['To'] for n in notifications]
self.assertEquals(expected_emails, actual_emails)
|