~launchpad-pqm/launchpad/devel

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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests for source package builds."""

__metaclass__ = type

from datetime import (
    datetime,
    timedelta,
    )
import re

from pytz import utc
from storm.locals import Store
from testtools.deferredruntest import AsynchronousDeferredRunTest
import transaction
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy

from lp.app.errors import NotFoundError
from lp.buildmaster.enums import BuildStatus
from lp.buildmaster.interfaces.buildqueue import IBuildQueue
from lp.buildmaster.model.builder import BuilderSlave
from lp.buildmaster.model.buildfarmjob import BuildFarmJob
from lp.buildmaster.model.packagebuild import PackageBuild
from lp.buildmaster.tests.mock_slaves import WaitingSlave
from lp.buildmaster.tests.test_packagebuild import (
    TestGetUploadMethodsMixin,
    TestHandleStatusMixin,
    )
from lp.code.interfaces.sourcepackagerecipebuild import (
    ISourcePackageRecipeBuild,
    ISourcePackageRecipeBuildJob,
    ISourcePackageRecipeBuildSource,
    )
from lp.code.mail.sourcepackagerecipebuild import (
    SourcePackageRecipeBuildMailer,
    )
from lp.code.model.sourcepackagerecipebuild import SourcePackageRecipeBuild
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.interfaces.series import SeriesStatus
from lp.services.database.lpstorm import IStore
from lp.services.log.logger import BufferLogger
from lp.services.mail.sendmail import format_address
from lp.services.webapp.authorization import check_permission
from lp.services.webapp.testing import verifyObject
from lp.soyuz.interfaces.processor import IProcessorFamilySet
from lp.soyuz.model.processor import ProcessorFamily
from lp.testing import (
    ANONYMOUS,
    login,
    person_logged_in,
    TestCaseWithFactory,
    )
from lp.testing.fakemethod import FakeMethod
from lp.testing.layers import (
    LaunchpadFunctionalLayer,
    LaunchpadZopelessLayer,
    )
from lp.testing.mail_helpers import pop_notifications


class TestSourcePackageRecipeBuild(TestCaseWithFactory):
    """Test the source package build object."""

    layer = LaunchpadFunctionalLayer

    def makeSourcePackageRecipeBuild(self):
        """Create a `SourcePackageRecipeBuild` for testing."""
        person = self.factory.makePerson()
        distroseries = self.factory.makeDistroSeries()
        distroseries_i386 = distroseries.newArch(
            'i386', ProcessorFamily.get(1), False, person,
            supports_virtualized=True)
        removeSecurityProxy(distroseries).nominatedarchindep = (
            distroseries_i386)

        return getUtility(ISourcePackageRecipeBuildSource).new(
            distroseries=distroseries,
            recipe=self.factory.makeSourcePackageRecipe(
                distroseries=distroseries),
            archive=self.factory.makeArchive(),
            requester=person)

    def test_providesInterfaces(self):
        # SourcePackageRecipeBuild provides IPackageBuild and
        # ISourcePackageRecipeBuild.
        spb = self.makeSourcePackageRecipeBuild()
        self.assertProvides(spb, ISourcePackageRecipeBuild)

    def test_implements_interface(self):
        build = self.makeSourcePackageRecipeBuild()
        verifyObject(ISourcePackageRecipeBuild, build)

    def test_saves_record(self):
        # A source package recipe build can be stored in the database
        spb = self.makeSourcePackageRecipeBuild()
        transaction.commit()
        self.assertProvides(spb, ISourcePackageRecipeBuild)

    def test_makeJob(self):
        # A build farm job can be obtained from a SourcePackageRecipeBuild
        spb = self.makeSourcePackageRecipeBuild()
        job = spb.makeJob()
        self.assertProvides(job, ISourcePackageRecipeBuildJob)

    def test_queueBuild(self):
        spb = self.makeSourcePackageRecipeBuild()
        bq = spb.queueBuild(spb)

        self.assertProvides(bq, IBuildQueue)
        self.assertProvides(bq.specific_job, ISourcePackageRecipeBuildJob)
        self.assertEqual(True, bq.virtualized)

        # The processor for SourcePackageRecipeBuilds should not be None.
        # They do require specific environments.
        self.assertNotEqual(None, bq.processor)
        self.assertEqual(
            spb.distroseries.nominatedarchindep.default_processor,
            bq.processor)
        self.assertEqual(bq, spb.buildqueue_record)

    def test_getBuildCookie(self):
        # A build cookie is made up of the job type and record id.
        # The uploadprocessor relies on this format.
        sprb = self.makeSourcePackageRecipeBuild()
        Store.of(sprb).flush()
        cookie = sprb.getBuildCookie()
        expected_cookie = "RECIPEBRANCHBUILD-%d" % sprb.id
        self.assertEquals(expected_cookie, cookie)

    def test_title(self):
        # A recipe build's title currently consists of the base
        # branch's unique name.
        spb = self.makeSourcePackageRecipeBuild()
        title = "%s recipe build" % spb.recipe.base_branch.unique_name
        self.assertEqual(spb.title, title)

    def test_getTitle(self):
        # A recipe build job's title is the same as its build's title.
        spb = self.makeSourcePackageRecipeBuild()
        job = spb.makeJob()
        self.assertEqual(job.getTitle(), spb.title)

    def test_distribution(self):
        # A source package recipe build has a distribution derived from
        # its series.
        spb = self.makeSourcePackageRecipeBuild()
        self.assertEqual(spb.distroseries.distribution, spb.distribution)

    def test_current_component(self):
        # Since recipes build only into PPAs, they always build in main.
        # PPAs lack indices for other components.
        spb = self.makeSourcePackageRecipeBuild()
        self.assertEqual('main', spb.current_component.name)

    def test_is_private(self):
        # A source package recipe build is currently always public.
        spb = self.makeSourcePackageRecipeBuild()
        self.assertEqual(False, spb.is_private)

    def test_view_private_branch(self):
        """Recipebuilds with private branches are restricted."""
        owner = self.factory.makePerson()
        branch = self.factory.makeAnyBranch(owner=owner)
        with person_logged_in(owner):
            recipe = self.factory.makeSourcePackageRecipe(branches=[branch])
            build = self.factory.makeSourcePackageRecipeBuild(recipe=recipe)
            self.assertTrue(check_permission('launchpad.View', build))
        removeSecurityProxy(branch).explicitly_private = True
        with person_logged_in(self.factory.makePerson()):
            self.assertFalse(check_permission('launchpad.View', build))
        login(ANONYMOUS)
        self.assertFalse(check_permission('launchpad.View', build))

    def test_view_private_archive(self):
        """Recipebuilds with private branches are restricted."""
        owner = self.factory.makePerson()
        archive = self.factory.makeArchive(owner=owner, private=True)
        build = self.factory.makeSourcePackageRecipeBuild(archive=archive)
        with person_logged_in(owner):
            self.assertTrue(check_permission('launchpad.View', build))
        with person_logged_in(self.factory.makePerson()):
            self.assertFalse(check_permission('launchpad.View', build))
        login(ANONYMOUS)
        self.assertFalse(check_permission('launchpad.View', build))

    def test_estimateDuration(self):
        # If there are no successful builds, estimate 10 minutes.
        spb = self.makeSourcePackageRecipeBuild()
        cur_date = self.factory.getUniqueDate()
        self.assertEqual(timedelta(minutes=10), spb.estimateDuration())
        for minutes in [20, 5, 1]:
            build = removeSecurityProxy(
                self.factory.makeSourcePackageRecipeBuild(recipe=spb.recipe))
            build.date_started = cur_date
            build.date_finished = cur_date + timedelta(minutes=minutes)
        self.assertEqual(timedelta(minutes=5), spb.estimateDuration())

    def test_getFileByName(self):
        """getFileByName returns the logs when requested by name."""
        spb = self.factory.makeSourcePackageRecipeBuild()
        removeSecurityProxy(spb).log = (
            self.factory.makeLibraryFileAlias(filename='buildlog.txt.gz'))
        self.assertEqual(spb.log, spb.getFileByName('buildlog.txt.gz'))
        self.assertRaises(NotFoundError, spb.getFileByName, 'foo')
        removeSecurityProxy(spb).log = (
            self.factory.makeLibraryFileAlias(filename='foo'))
        self.assertEqual(spb.log, spb.getFileByName('foo'))
        self.assertRaises(NotFoundError, spb.getFileByName, 'buildlog.txt.gz')
        removeSecurityProxy(spb).upload_log = (
            self.factory.makeLibraryFileAlias(filename='upload.txt.gz'))
        self.assertEqual(spb.upload_log, spb.getFileByName('upload.txt.gz'))

    def test_binary_builds(self):
        """The binary_builds property should be populated automatically."""
        spb = self.factory.makeSourcePackageRecipeBuild()
        multiverse = self.factory.makeComponent(name='multiverse')
        spr = self.factory.makeSourcePackageRelease(
            source_package_recipe_build=spb, component=multiverse)
        self.assertEqual([], list(spb.binary_builds))
        binary = self.factory.makeBinaryPackageBuild(spr)
        self.factory.makeBinaryPackageBuild()
        Store.of(binary).flush()
        self.assertEqual([binary], list(spb.binary_builds))

    def test_manifest(self):
        """Manifest should start empty, but accept SourcePackageRecipeData."""
        recipe = self.factory.makeSourcePackageRecipe()
        build = recipe.requestBuild(
            recipe.daily_build_archive, recipe.owner,
            list(recipe.distroseries)[0], PackagePublishingPocket.RELEASE)
        self.assertIs(None, build.manifest)
        self.assertIs(None, build.getManifestText())
        manifest_text = self.factory.makeRecipeText()
        removeSecurityProxy(build).setManifestText(manifest_text)
        self.assertEqual(manifest_text, build.getManifestText())
        self.assertIsNot(None, build.manifest)
        IStore(build).flush()
        manifest_text = self.factory.makeRecipeText()
        removeSecurityProxy(build).setManifestText(manifest_text)
        self.assertEqual(manifest_text, build.getManifestText())
        removeSecurityProxy(build).setManifestText(None)
        self.assertIs(None, build.manifest)

    def test_makeDailyBuilds(self):
        self.assertEqual([], SourcePackageRecipeBuild.makeDailyBuilds())
        recipe = self.factory.makeSourcePackageRecipe(build_daily=True)
        [build] = SourcePackageRecipeBuild.makeDailyBuilds()
        self.assertEqual(recipe, build.recipe)
        self.assertEqual(list(recipe.distroseries), [build.distroseries])

    def test_makeDailyBuilds_skips_missing_archive(self):
        """When creating daily builds, skip ones that are already pending."""
        recipe = self.factory.makeSourcePackageRecipe(
            build_daily=True, is_stale=True)
        with person_logged_in(recipe.owner):
            recipe.daily_build_archive = None
        builds = SourcePackageRecipeBuild.makeDailyBuilds()
        self.assertEqual([], builds)

    def test_makeDailyBuilds_logs_builds(self):
        # If a logger is passed into the makeDailyBuilds method, each recipe
        # that a build is requested for gets logged.
        owner = self.factory.makePerson(name='eric')
        self.factory.makeSourcePackageRecipe(
            owner=owner, name=u'funky-recipe', build_daily=True)
        logger = BufferLogger()
        SourcePackageRecipeBuild.makeDailyBuilds(logger)
        self.assertEqual(
            'DEBUG Recipe eric/funky-recipe is stale\n'
            'DEBUG  - build requested for Warty (4.10)\n',
            logger.getLogBuffer())

    def test_makeDailyBuilds_clears_is_stale(self):
        recipe = self.factory.makeSourcePackageRecipe(
            build_daily=True, is_stale=True)
        SourcePackageRecipeBuild.makeDailyBuilds()[0]
        self.assertFalse(recipe.is_stale)

    def test_makeDailyBuilds_skips_if_built_in_last_24_hours(self):
        # We won't create a build during makeDailyBuilds() if the recipe
        # has been built in the last 24 hours.
        recipe = self.factory.makeSourcePackageRecipe(
            build_daily=True, is_stale=True)
        recipe.requestBuild(
            recipe.daily_build_archive, recipe.owner,
            list(recipe.distroseries)[0], PackagePublishingPocket.RELEASE)
        daily_builds = SourcePackageRecipeBuild.makeDailyBuilds()
        self.assertEqual([], daily_builds)

    def test_makeDailyBuilds_skips_non_stale_builds(self):
        # If the recipe isn't stale, makeDailyBuilds() won't create a build.
        self.factory.makeSourcePackageRecipe(
            build_daily=True, is_stale=False)
        daily_builds = SourcePackageRecipeBuild.makeDailyBuilds()
        self.assertEqual([], daily_builds)

    def test_makeDailyBuilds_skips_builds_already_queued(self):
        # If the recipe already has an identical build pending,
        # makeDailyBuilds() won't create a build.
        owner = self.factory.makePerson(name='eric')
        recipe = self.factory.makeSourcePackageRecipe(
            owner=owner, name=u'funky-recipe', build_daily=True,
            is_stale=True)
        series = list(recipe.distroseries)[0]
        existing_build = recipe.requestBuild(
            recipe.daily_build_archive, recipe.owner, series,
            PackagePublishingPocket.RELEASE)
        removeSecurityProxy(existing_build).date_created = (
            datetime.now(utc) - timedelta(hours=24, seconds=1))
        removeSecurityProxy(recipe).is_stale = True

        logger = BufferLogger()
        daily_builds = SourcePackageRecipeBuild.makeDailyBuilds(logger)
        self.assertEqual([], daily_builds)
        self.assertEqual(
            'DEBUG Recipe eric/funky-recipe is stale\n'
            'DEBUG  - build already pending for Warty (4.10)\n',
            logger.getLogBuffer())

    def test_makeDailyBuilds_skips_disabled_archive(self):
        # If the recipe's daily build archive is disabled, makeDailyBuilds()
        # won't create a build.
        owner = self.factory.makePerson(name='eric')
        recipe = self.factory.makeSourcePackageRecipe(
            owner=owner, name=u'funky-recipe', build_daily=True,
            is_stale=True)
        archive = self.factory.makeArchive(owner=recipe.owner, name="ppa")
        removeSecurityProxy(recipe).daily_build_archive = archive
        removeSecurityProxy(archive).disable()

        logger = BufferLogger()
        daily_builds = SourcePackageRecipeBuild.makeDailyBuilds(logger)
        self.assertEqual([], daily_builds)
        self.assertEqual(
            'DEBUG Recipe eric/funky-recipe is stale\n'
            'DEBUG  - daily build failed for Warty (4.10): ' +
            "ArchiveDisabled(u'PPA for Eric is disabled.',)\n",
            logger.getLogBuffer())

    def test_makeDailyBuilds_skips_archive_with_no_permission(self):
        # If the recipe's daily build archive cannot be uploaded to due to
        # insufficient permissions, makeDailyBuilds() won't create a build.
        owner = self.factory.makePerson(name='eric')
        recipe = self.factory.makeSourcePackageRecipe(
            owner=owner, name=u'funky-recipe', build_daily=True,
            is_stale=True)
        archive = self.factory.makeArchive(name="ppa")
        removeSecurityProxy(recipe).daily_build_archive = archive

        logger = BufferLogger()
        daily_builds = SourcePackageRecipeBuild.makeDailyBuilds(logger)
        self.assertEqual([], daily_builds)
        self.assertEqual(
            'DEBUG Recipe eric/funky-recipe is stale\n'
            'DEBUG  - daily build failed for Warty (4.10): '
            "CannotUploadToPPA('Signer has no upload rights "
            "to this PPA.',)\n",
            logger.getLogBuffer())

    def test_makeDailyBuilds_with_an_older_build(self):
        # If a previous build is more than 24 hours old, and the recipe is
        # stale, we'll fire another off.
        recipe = self.factory.makeSourcePackageRecipe(
            build_daily=True, is_stale=True)
        build = recipe.requestBuild(
            recipe.daily_build_archive, recipe.owner,
            list(recipe.distroseries)[0], PackagePublishingPocket.RELEASE)
        nb = removeSecurityProxy(build)
        nb.date_created = datetime.now(utc) - timedelta(hours=24, seconds=1)
        # The build also needs to be completed
        nb.status = BuildStatus.FULLYBUILT
        daily_builds = SourcePackageRecipeBuild.makeDailyBuilds()
        self.assertEquals(1, len(daily_builds))
        actual_title = [b.title for b in daily_builds]
        self.assertEquals([build.title], actual_title)

    def test_makeDailyBuilds_with_an_older_and_newer_build(self):
        # If a recipe has been built twice, and the most recent build is
        # within 24 hours, makeDailyBuilds() won't create a build.
        recipe = self.factory.makeSourcePackageRecipe(
            build_daily=True, is_stale=True)
        for timediff in (timedelta(hours=24, seconds=1), timedelta(hours=8)):
            build = recipe.requestBuild(
                recipe.daily_build_archive, recipe.owner,
                list(recipe.distroseries)[0],
                PackagePublishingPocket.RELEASE)
            nb = removeSecurityProxy(build)
            nb.date_created = datetime.now(utc) - timediff
            # The build also needs to be completed
            nb.status = BuildStatus.FULLYBUILT
        daily_builds = SourcePackageRecipeBuild.makeDailyBuilds()
        self.assertEquals([], list(daily_builds))

    def test_makeDailyBuilds_with_new_build_different_archive(self):
        # If a recipe has been built into an archive that isn't the
        # daily_build_archive, we will create a build.
        recipe = self.factory.makeSourcePackageRecipe(
            build_daily=True, is_stale=True)
        archive = self.factory.makeArchive(owner=recipe.owner)
        build = recipe.requestBuild(
            archive, recipe.owner, list(recipe.distroseries)[0],
            PackagePublishingPocket.RELEASE)
        nb = removeSecurityProxy(build)
        nb.date_created = datetime.now(utc) - timedelta(hours=8)
        # The build also needs to be completed
        nb.status = BuildStatus.FULLYBUILT
        daily_builds = SourcePackageRecipeBuild.makeDailyBuilds()
        actual_title = [b.title for b in daily_builds]
        self.assertEquals([build.title], actual_title)

    def test_makeDailyBuilds_with_disallowed_series(self):
        # If a recipe is set to build into a disallowed series,
        # makeDailyBuilds won't OOPS.
        recipe = self.factory.makeSourcePackageRecipe(
            build_daily=True, is_stale=True)
        self.factory.makeArchive(owner=recipe.owner)
        logger = BufferLogger()
        distroseries = list(recipe.distroseries)[0]
        removeSecurityProxy(distroseries).status = SeriesStatus.OBSOLETE
        SourcePackageRecipeBuild.makeDailyBuilds(logger)
        self.assertEquals([], self.oopses)
        self.assertIn(
            "DEBUG  - cannot build against Warty (4.10).",
            logger.getLogBuffer())

    def test_getRecentBuilds(self):
        """Recent builds match the same person, series and receipe.

        Builds do not match if they are older than 24 hours, or have a
        different requester, series or recipe.
        """
        requester = self.factory.makePerson()
        recipe = self.factory.makeSourcePackageRecipe()
        series = self.factory.makeDistroSeries()
        now = self.factory.getUniqueDate()
        build = self.factory.makeSourcePackageRecipeBuild(recipe=recipe,
            requester=requester)
        self.factory.makeSourcePackageRecipeBuild(
            recipe=recipe, distroseries=series)
        self.factory.makeSourcePackageRecipeBuild(
            requester=requester, distroseries=series)

        def get_recent():
            Store.of(build).flush()
            return SourcePackageRecipeBuild.getRecentBuilds(
                requester, recipe, series, _now=now)
        self.assertContentEqual([], get_recent())
        yesterday = now - timedelta(days=1)
        recent_build = self.factory.makeSourcePackageRecipeBuild(
            recipe=recipe, distroseries=series, requester=requester,
            date_created=yesterday)
        self.assertContentEqual([], get_recent())
        a_second = timedelta(seconds=1)
        removeSecurityProxy(recent_build).date_created += a_second
        self.assertContentEqual([recent_build], get_recent())

    def test_destroySelf(self):
        # ISourcePackageRecipeBuild should make sure to remove jobs and build
        # queue entries and then invalidate itself.
        build = self.factory.makeSourcePackageRecipeBuild()
        build.destroySelf()

    def test_destroySelf_clears_release(self):
        # Destroying a sourcepackagerecipebuild removes references to it from
        # its releases.
        build = self.factory.makeSourcePackageRecipeBuild()
        release = self.factory.makeSourcePackageRelease(
            source_package_recipe_build=build)
        self.assertEqual(build, release.source_package_recipe_build)
        build.destroySelf()
        self.assertIs(None, release.source_package_recipe_build)
        transaction.commit()

    def test_destroySelf_destroys_referenced(self):
        # Destroying a sourcepackagerecipebuild also destroys the
        # PackageBuild and BuildFarmJob it references.
        build = self.factory.makeSourcePackageRecipeBuild()
        store = Store.of(build)
        naked_build = removeSecurityProxy(build)
        # Ensure database ids are set.
        store.flush()
        package_build_id = naked_build.package_build_id
        build_farm_job_id = naked_build.package_build.build_farm_job_id
        build.destroySelf()
        result = store.find(PackageBuild, PackageBuild.id == package_build_id)
        self.assertIs(None, result.one())
        result = store.find(
            BuildFarmJob, BuildFarmJob.id == build_farm_job_id)
        self.assertIs(None, result.one())

    def test_cancelBuild(self):
        # ISourcePackageRecipeBuild should make sure to remove jobs and build
        # queue entries and then invalidate itself.
        build = self.factory.makeSourcePackageRecipeBuild()
        build.cancelBuild()

        self.assertEqual(
            BuildStatus.SUPERSEDED,
            build.status)

    def test_getSpecificJob(self):
        # getSpecificJob returns the SourcePackageRecipeBuild
        sprb = self.makeSourcePackageRecipeBuild()
        Store.of(sprb).flush()
        job = sprb.build_farm_job.getSpecificJob()
        self.assertEqual(sprb, job)

    def test_getUploader(self):
        # For ACL purposes the uploader is the build requester.
        build = self.makeSourcePackageRecipeBuild()
        self.assertEquals(build.requester,
            build.getUploader(None))

    def test_getByBuildFarmJob(self):
        sprb = self.makeSourcePackageRecipeBuild()
        Store.of(sprb).flush()
        self.assertEqual(
            sprb,
            SourcePackageRecipeBuild.getByBuildFarmJob(sprb.build_farm_job))

    def test_getByBuildFarmJobs(self):
        sprbs = [self.makeSourcePackageRecipeBuild() for i in range(10)]
        Store.of(sprbs[0]).flush()
        self.assertContentEqual(
            sprbs,
            SourcePackageRecipeBuild.getByBuildFarmJobs(
                [sprb.build_farm_job for sprb in sprbs]))

    def test_getByBuildFarmJobs_empty(self):
        self.assertContentEqual(
            [],
            SourcePackageRecipeBuild.getByBuildFarmJobs([]))


class TestAsBuildmaster(TestCaseWithFactory):

    layer = LaunchpadZopelessLayer

    def test_notify(self):
        """We do not send mail on completion of source package recipe builds.

        See bug 778437.
        """
        person = self.factory.makePerson(name='person')
        cake = self.factory.makeSourcePackageRecipe(
            name=u'recipe', owner=person)
        pantry = self.factory.makeArchive(name='ppa')
        secret = self.factory.makeDistroSeries(name=u'distroseries')
        build = self.factory.makeSourcePackageRecipeBuild(
            recipe=cake, distroseries=secret, archive=pantry)
        removeSecurityProxy(build).status = BuildStatus.FULLYBUILT
        IStore(build).flush()
        build.notify()
        self.assertEquals(0, len(pop_notifications()))

    def assertBuildMessageValid(self, build, message):
        # Not currently used; can be used if we do want to check about any
        # notifications sent in other cases.
        requester = build.requester
        requester_address = format_address(
            requester.displayname, requester.preferredemail.email)
        mailer = SourcePackageRecipeBuildMailer.forStatus(build)
        expected = mailer.generateEmail(
            requester.preferredemail.email, requester)
        self.assertEqual(
            requester_address, re.sub(r'\n\t+', ' ', message['To']))
        self.assertEqual(expected.subject, message['Subject'].replace(
            '\n\t', ' '))
        self.assertEqual(
            expected.body, message.get_payload(decode=True))

    def test_notify_when_recipe_deleted(self):
        """Notify does nothing if recipe has been deleted."""
        person = self.factory.makePerson(name='person')
        cake = self.factory.makeSourcePackageRecipe(
            name=u'recipe', owner=person)
        pantry = self.factory.makeArchive(name='ppa')
        secret = self.factory.makeDistroSeries(name=u'distroseries')
        build = self.factory.makeSourcePackageRecipeBuild(
            recipe=cake, distroseries=secret, archive=pantry)
        removeSecurityProxy(build).status = BuildStatus.FULLYBUILT
        cake.destroySelf()
        IStore(build).flush()
        build.notify()
        notifications = pop_notifications()
        self.assertEquals(0, len(notifications))


class TestBuildNotifications(TestCaseWithFactory):

    layer = LaunchpadZopelessLayer

    run_tests_with = AsynchronousDeferredRunTest.make_factory(timeout=20)

    def prepare_build(self, fake_successful_upload=False):
        queue_record = self.factory.makeSourcePackageRecipeBuildJob()
        build = queue_record.specific_job.build
        naked_build = removeSecurityProxy(build)
        naked_build.status = BuildStatus.FULLYBUILT
        naked_build.date_started = self.factory.getUniqueDate()
        if fake_successful_upload:
            naked_build.verifySuccessfulUpload = FakeMethod(
                result=True)
        queue_record.builder = self.factory.makeBuilder()
        slave = WaitingSlave('BuildStatus.OK')
        self.patch(BuilderSlave, 'makeBuilderSlave', FakeMethod(slave))
        return build

    def assertDeferredNotifyCount(self, status, build, expected_count):
        d = build.handleStatus(status, None, {'filemap': {}})

        def cb(result):
            self.assertEqual(expected_count, len(pop_notifications()))

        d.addCallback(cb)
        return d

    def test_handleStatus_PACKAGEFAIL(self):
        """Failing to build the package immediately sends a notification."""
        return self.assertDeferredNotifyCount(
            "PACKAGEFAIL", self.prepare_build(), 1)

    def test_handleStatus_OK(self):
        """Building the source package does _not_ immediately send mail.

        (The archive uploader mail send one later.
        """
        return self.assertDeferredNotifyCount(
            "OK", self.prepare_build(), 0)

#XXX 2011-05-20 gmb bug=785679
#    This test has been disabled since it broke intermittently in
#    buildbot (but does not fail in isolation locally).
##    def test_handleStatus_OK_successful_upload(self):
##        return self.assertDeferredNotifyCount(
##            "OK", self.prepare_build(True), 0)


class MakeSPRecipeBuildMixin:
    """Provide the common makeBuild method returning a queued build."""

    def makeBuild(self):
        person = self.factory.makePerson()
        distroseries = self.factory.makeDistroSeries()
        processor_fam = getUtility(IProcessorFamilySet).getByName('x86')
        distroseries_i386 = distroseries.newArch(
            'i386', processor_fam, False, person,
            supports_virtualized=True)
        distroseries.nominatedarchindep = distroseries_i386
        build = self.factory.makeSourcePackageRecipeBuild(
            distroseries=distroseries,
            status=BuildStatus.FULLYBUILT,
            duration=timedelta(minutes=5))
        build.queueBuild(build)
        return build


class TestGetUploadMethodsForSPRecipeBuild(
    MakeSPRecipeBuildMixin, TestGetUploadMethodsMixin, TestCaseWithFactory):
    """IPackageBuild.getUpload-related methods work with SPRecipe builds."""


class TestHandleStatusForSPRBuild(
    MakeSPRecipeBuildMixin, TestHandleStatusMixin, TestCaseWithFactory):
    """IPackageBuild.handleStatus works with SPRecipe builds."""