~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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
# Copyright 2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Test `DistroSeriesDifferenceJob` and utility."""

__metaclass__ = type

import transaction
from psycopg2 import ProgrammingError
from zope.component import getUtility
from zope.interface.verify import verifyObject
from zope.security.proxy import removeSecurityProxy

from canonical.launchpad.interfaces.lpstorm import IMasterStore
from canonical.launchpad.scripts.tests import run_script
from canonical.testing.layers import (
    LaunchpadZopelessLayer,
    ZopelessDatabaseLayer,
    )
from lp.registry.enum import (
    DistroSeriesDifferenceStatus,
    DistroSeriesDifferenceType,
    )
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.model.distroseriesdifference import DistroSeriesDifference
from lp.services.features.testing import FeatureFixture
from lp.services.job.interfaces.job import JobStatus
from lp.soyuz.enums import PackagePublishingStatus
from lp.soyuz.interfaces.distributionjob import (
    DistributionJobType,
    IDistroSeriesDifferenceJobSource,
    )
from lp.soyuz.model.distributionjob import DistributionJob
from lp.soyuz.model.distroseriesdifferencejob import (
    create_job,
    DistroSeriesDifferenceJob,
    FEATURE_FLAG_ENABLE_MODULE,
    find_waiting_jobs,
    make_metadata,
    may_require_job,
    )
from lp.testing import TestCaseWithFactory


def find_dsd_for(dsp, package):
    """Find `DistroSeriesDifference`.

    :param dsp: `DistroSeriesParent`.
    :param package: `SourcePackageName`.
    """
    store = IMasterStore(DistroSeriesDifference)
    return store.find(
        DistroSeriesDifference,
        DistroSeriesDifference.derived_series == dsp.derived_series,
        DistroSeriesDifference.parent_series == dsp.parent_series,
        DistroSeriesDifference.source_package_name == package)


class TestDistroSeriesDifferenceJobSource(TestCaseWithFactory):
    """Tests for `IDistroSeriesDifferenceJobSource`."""

    layer = ZopelessDatabaseLayer

    def setUp(self):
        super(TestDistroSeriesDifferenceJobSource, self).setUp()
        self.useFixture(FeatureFixture({FEATURE_FLAG_ENABLE_MODULE: u'on'}))

    def getJobSource(self):
        return getUtility(IDistroSeriesDifferenceJobSource)

    def makeDerivedDistroSeries(self):
        dsp = self.factory.makeDistroSeriesParent()
        return dsp.derived_series

    def test_baseline(self):
        verifyObject(IDistroSeriesDifferenceJobSource, self.getJobSource())

    def test_make_metadata_is_consistent(self):
        package = self.factory.makeSourcePackageName()
        parent_series = self.factory.makeDistroSeries()
        self.assertEqual(
            make_metadata(package, parent_series),
            make_metadata(package, parent_series))

    def test_make_metadata_distinguishes_packages(self):
        parent_series = self.factory.makeDistroSeries()
        one_package = self.factory.makeSourcePackageName()
        another_package = self.factory.makeSourcePackageName()
        self.assertNotEqual(
            make_metadata(one_package, parent_series),
            make_metadata(another_package, parent_series))

    def test_make_metadata_distinguishes_parents(self):
        package = self.factory.makeSourcePackageName()
        one_parent = self.factory.makeDistroSeries()
        another_parent = self.factory.makeDistroSeries()
        self.assertNotEqual(
            make_metadata(package, one_parent),
            make_metadata(package, another_parent))

    def test_may_require_job_accepts_none_derived_series(self):
        parent_series = self.factory.makeDistroSeriesParent().parent_series
        package = self.factory.makeSourcePackageName()
        self.assertFalse(may_require_job(None, package, parent_series))

    def test_may_require_job_accepts_none_parent_series(self):
        derived_series = self.makeDerivedDistroSeries()
        package = self.factory.makeSourcePackageName()
        self.assertTrue(may_require_job(derived_series, package, None))

    def test_may_require_job_allows_new_jobs(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        self.assertTrue(may_require_job(
            dsp.derived_series, package, dsp.parent_series))

    def test_may_require_job_forbids_redundant_jobs(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        create_job(dsp.derived_series, package, dsp.parent_series)
        self.assertFalse(
            may_require_job(dsp.derived_series, package, dsp.parent_series))

    def test_may_require_job_forbids_jobs_on_nonderived_series(self):
        sourcepackage = self.factory.makeSourcePackage()
        self.assertFalse(may_require_job(
            sourcepackage.distroseries, sourcepackage.sourcepackagename,
            None))

    def test_may_require_job_forbids_jobs_for_intra_distro_derivation(self):
        package = self.factory.makeSourcePackageName()
        parent = self.factory.makeDistroSeries()
        child = self.factory.makeDistroSeries(
            distribution=parent.distribution, previous_series=parent)
        self.assertFalse(may_require_job(child, package, parent))

    def test_may_require_job_only_considers_waiting_jobs_for_redundancy(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        existing_job = create_job(
            dsp.derived_series, package, dsp.parent_series)
        existing_job.job.start()
        self.assertTrue(
            may_require_job(dsp.derived_series, package, dsp.parent_series))

    def test_create_job_creates_waiting_job(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        dsdjob = create_job(dsp.derived_series, package, dsp.parent_series)
        self.assertEqual(JobStatus.WAITING, dsdjob.job.status)

    def find_waiting_jobs_finds_waiting_jobs(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        job = create_job(dsp.derived_series, package, dsp.parent_series)
        self.assertContentEqual(
            [job],
            find_waiting_jobs(dsp.derived_series, package, dsp.parent_series))

    def find_waiting_jobs_ignores_other_derived_series(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        create_job(dsp.derived_series, package, dsp.parent_series)
        other_series = self.factory.makeDistroSeries()
        self.assertContentEqual(
            [], find_waiting_jobs(other_series, package, dsp.parent_series))

    def find_waiting_jobs_ignores_other_parent_series(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        create_job(dsp.derived_series, package, dsp.parent_series)
        other_series = self.factory.makeDistroSeries()
        self.assertContentEqual(
            [], find_waiting_jobs(dsp.derived_series, package, other_series))

    def test_find_waiting_jobs_ignores_other_packages(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        create_job(dsp.derived_series, package, dsp.parent_series)
        other_package = self.factory.makeSourcePackageName()
        self.assertContentEqual(
            [],
            find_waiting_jobs(
                dsp.derived_series, other_package, dsp.parent_series))

    def test_find_waiting_jobs_considers_only_waiting_jobs(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        job = create_job(dsp.derived_series, package, dsp.parent_series)
        job.start()
        self.assertContentEqual(
            [],
            find_waiting_jobs(dsp.derived_series, package, dsp.parent_series))
        job.complete()
        self.assertContentEqual(
            [],
            find_waiting_jobs(dsp.derived_series, package, dsp.parent_series))

    def test_createForPackagedPublication_creates_jobs_for_its_child(self):
        dsp = self.factory.makeDistroSeriesParent()
        parent_dsp = self.factory.makeDistroSeriesParent(
            derived_series=dsp.parent_series)
        package = self.factory.makeSourcePackageName()
        # Create a job for the derived_series parent, which should create
        # two jobs. One for derived_series, and the other for its child.
        self.getJobSource().createForPackagePublication(
            parent_dsp.derived_series, package,
            PackagePublishingPocket.RELEASE, parent_dsp.parent_series)
        jobs = sum([
            find_waiting_jobs(dsp.derived_series, package, dsp.parent_series)
            for dsp in [parent_dsp, parent_dsp]],
            [])
        self.assertEqual(
            [package.id, package.id],
            [job.metadata["sourcepackagename"] for job in jobs])

    def test_createForPackagePublication_creates_job_for_derived_series(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        self.getJobSource().createForPackagePublication(
            dsp.derived_series, package, PackagePublishingPocket.RELEASE,
            dsp.parent_series)
        jobs = find_waiting_jobs(
            dsp.derived_series, package, dsp.parent_series)
        self.assertEqual(
            [package.id], [job.metadata["sourcepackagename"] for job in jobs])

    def test_createForPackagePublication_obeys_feature_flag(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        self.useFixture(FeatureFixture({FEATURE_FLAG_ENABLE_MODULE: ''}))
        self.getJobSource().createForPackagePublication(
            dsp.derived_series, package, PackagePublishingPocket.RELEASE,
            dsp.parent_series)
        self.assertContentEqual(
            [],
            find_waiting_jobs(dsp.derived_series, package, dsp.parent_series))

    def test_createForPackagePublication_ignores_backports_and_proposed(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        self.getJobSource().createForPackagePublication(
            dsp.derived_series, package, PackagePublishingPocket.BACKPORTS,
            dsp.parent_series)
        self.getJobSource().createForPackagePublication(
            dsp.derived_series, package, PackagePublishingPocket.PROPOSED,
            dsp.parent_series)
        self.assertContentEqual(
            [],
            find_waiting_jobs(dsp.derived_series, package, dsp.parent_series))

    def test_getPendingJobsForDifferences_finds_job(self):
        dsd = self.factory.makeDistroSeriesDifference()
        job = create_job(dsd.derived_series, dsd.source_package_name)
        self.assertEqual(
            {dsd: [job]},
            self.getJobSource().getPendingJobsForDifferences(
                dsd.derived_series, [dsd]))

    def test_getPendingJobsForDifferences_ignores_other_package(self):
        dsd = self.factory.makeDistroSeriesDifference()
        create_job(dsd.derived_series, self.factory.makeSourcePackageName())
        self.assertEqual(
            {},
            self.getJobSource().getPendingJobsForDifferences(
                dsd.derived_series, [dsd]))

    def test_getPendingJobsForDifferences_ignores_other_derived_series(self):
        dsd = self.factory.makeDistroSeriesDifference()
        create_job(self.makeDerivedDistroSeries(), dsd.source_package_name)
        self.assertEqual(
            {},
            self.getJobSource().getPendingJobsForDifferences(
                dsd.derived_series, [dsd]))

    def test_getPendingJobsForDifferences_ignores_other_parent_series(self):
        # XXX JeroenVermeulen 2011-05-26 bug=758906: Can't test this
        # until we can specify the right parent series when creating a
        # job.
        return

    def test_getPendingJobsForDifferences_ignores_non_pending_jobs(self):
        dsd = self.factory.makeDistroSeriesDifference()
        job = create_job(dsd.derived_series, dsd.source_package_name)
        removeSecurityProxy(job).job._status = JobStatus.COMPLETED
        self.assertEqual(
            {},
            self.getJobSource().getPendingJobsForDifferences(
                dsd.derived_series, [dsd]))

    def test_getPendingJobsForDifferences_ignores_other_job_types(self):
        # XXX JeroenVermeulen 2011-05-26 bug=758906: Once parent_series
        # is incorporated into the job type, set it to dsd.parent_series
        # on the fake job, or this test will become silently meaningless.
        dsd = self.factory.makeDistroSeriesDifference()
        DistributionJob(
            distribution=dsd.derived_series.distribution,
            distroseries=dsd.derived_series,
            job_type=DistributionJobType.INITIALISE_SERIES,
            metadata={"sourcepackagename": dsd.source_package_name.id})
        self.assertEqual(
            {},
            self.getJobSource().getPendingJobsForDifferences(
                dsd.derived_series, [dsd]))

    def test_cronscript(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        self.getJobSource().createForPackagePublication(
            dsp.derived_series, package, PackagePublishingPocket.RELEASE,
            dsp.parent_series)
        # Make changes visible to the process we'll be spawning.
        transaction.commit()
        return_code, stdout, stderr = run_script(
            'cronscripts/distroseriesdifference_job.py', ['-v'])
        # The cronscript ran how we expected it to.
        self.assertEqual(return_code, 0)
        self.assertIn(
            'INFO    Ran 1 DistroSeriesDifferenceJob jobs.', stderr)
        # And it did what we expected.
        jobs = find_waiting_jobs(
            dsp.derived_series, package, dsp.parent_series)
        self.assertContentEqual([], jobs)
        self.assertEqual(1, find_dsd_for(dsp, package).count())

    def test_job_runner_does_not_create_multiple_dsds(self):
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        job = self.getJobSource().createForPackagePublication(
            dsp.derived_series, package, PackagePublishingPocket.RELEASE,
            dsp.parent_series)
        job[0].start()
        job[0].run()
        # Complete the job so we can create another.
        job[0].job.complete()
        # The first job would have created a DSD for us.
        self.assertEqual(1, find_dsd_for(dsp, package).count())
        # If we run the job again, it will not create another DSD.
        job = self.getJobSource().createForPackagePublication(
            dsp.derived_series, package, PackagePublishingPocket.RELEASE,
            dsp.parent_series)
        job[0].start()
        job[0].run()
        self.assertEqual(1, find_dsd_for(dsp, package).count())

    def test_packageset_filter_passes_inherited_packages(self):
        dsp = self.factory.makeDistroSeriesParent()
        # Parent must have a packageset or the filter will pass anyway.
        self.factory.makePackageset(distroseries=dsp.parent_series)
        package = self.factory.makeSourcePackageName()
        # Package is not in the packageset _but_ both the parent and
        # derived series have it.
        self.factory.makeSourcePackagePublishingHistory(
            distroseries=dsp.parent_series, sourcepackagename=package)
        self.factory.makeSourcePackagePublishingHistory(
            distroseries=dsp.derived_series, sourcepackagename=package)
        job = create_job(dsp.derived_series, package, dsp.parent_series)
        self.assertTrue(job.passesPackagesetFilter())

    def test_packageset_filter_passes_packages_unique_to_derived_series(self):
        dsp = self.factory.makeDistroSeriesParent()
        # Parent must have a packageset or the filter will pass anyway.
        self.factory.makePackageset(distroseries=dsp.parent_series)
        package = self.factory.makeSourcePackageName()
        # Package exists in the derived series but not in the parent
        # series.
        self.factory.makeSourcePackagePublishingHistory(
            distroseries=dsp.derived_series, sourcepackagename=package)
        job = create_job(dsp.derived_series, package, dsp.parent_series)
        self.assertTrue(job.passesPackagesetFilter())

    def test_packageset_filter_passes_all_if_parent_has_no_packagesets(self):
        # Debian in particular has no packagesets.  If the parent series
        # has no packagesets, the packageset filter passes all packages.
        dsp = self.factory.makeDistroSeriesParent()
        package = self.factory.makeSourcePackageName()
        self.factory.makeSourcePackagePublishingHistory(
            distroseries=dsp.parent_series, sourcepackagename=package)
        job = create_job(dsp.derived_series, package, dsp.parent_series)
        self.assertTrue(job.passesPackagesetFilter())

    def makeInheritedPackageSet(self, distro_series_parent, packages=()):
        """Simulate an inherited `Packageset`.

        Creates a packageset in the parent that has an equivalent in
        `derived_series`.
        """
        parent_packageset = self.factory.makePackageset(
            distroseries=distro_series_parent.parent_series,
            packages=packages)
        return self.factory.makePackageset(
            distroseries=distro_series_parent.derived_series,
            packages=packages, name=parent_packageset.name,
            owner=parent_packageset.owner, related_set=parent_packageset)

    def test_packageset_filter_passes_package_in_inherited_packageset(self):
        dsp = self.factory.makeDistroSeriesParent()
        # Package is in a packageset on the parent that the derived
        # series also has.
        package = self.factory.makeSourcePackageName()
        self.makeInheritedPackageSet(dsp, [package])
        # Package is in parent series and in a packageset that the
        # derived series inherited.
        self.factory.makeSourcePackagePublishingHistory(
            distroseries=dsp.parent_series, sourcepackagename=package)
        job = create_job(dsp.derived_series, package, dsp.parent_series)
        self.assertTrue(job.passesPackagesetFilter())

    def test_packageset_filter_blocks_unwanted_parent_package(self):
        dsp = self.factory.makeDistroSeriesParent()
        self.makeInheritedPackageSet(dsp)
        package = self.factory.makeSourcePackageName()
        # Package is in the parent series but not in a packageset shared
        # between the derived series and the parent series.
        self.factory.makeSourcePackagePublishingHistory(
            distroseries=dsp.parent_series, sourcepackagename=package)
        job = create_job(dsp.derived_series, package, dsp.parent_series)
        self.assertFalse(job.passesPackagesetFilter())


class TestDistroSeriesDifferenceJobEndToEnd(TestCaseWithFactory):

    layer = LaunchpadZopelessLayer

    def setUp(self):
        super(TestDistroSeriesDifferenceJobEndToEnd, self).setUp()
        self.useFixture(FeatureFixture({FEATURE_FLAG_ENABLE_MODULE: u'on'}))
        self.store = IMasterStore(DistroSeriesDifference)

    def getJobSource(self):
        return getUtility(IDistroSeriesDifferenceJobSource)

    def makeDerivedDistroSeries(self):
        dsp = self.factory.makeDistroSeriesParent()
        return dsp

    def createPublication(self, source_package_name, versions, distroseries,
                          archive=None):
        if archive is None:
            archive = distroseries.main_archive
        changelog_lfa = self.factory.makeChangelog(
            source_package_name.name, versions)
        # Commit for the Librarian's sake.
        transaction.commit()
        spr = self.factory.makeSourcePackageRelease(
            sourcepackagename=source_package_name, version=versions[0],
            changelog=changelog_lfa)
        return self.factory.makeSourcePackagePublishingHistory(
            sourcepackagerelease=spr, archive=archive,
            distroseries=distroseries,
            status=PackagePublishingStatus.PUBLISHED,
            pocket=PackagePublishingPocket.RELEASE)

    def findDSD(self, derived_series, source_package_name):
        return self.store.find(
            DistroSeriesDifference,
            DistroSeriesDifference.derived_series == derived_series,
            DistroSeriesDifference.source_package_name ==
            source_package_name)

    def runJob(self, job):
        transaction.commit()
        self.layer.switchDbUser('distroseriesdifferencejob')
        dsdjob = DistroSeriesDifferenceJob(job)
        dsdjob.start()
        dsdjob.run()
        dsdjob.complete()
        transaction.commit()
        self.layer.switchDbUser('launchpad')

    def test_parent_gets_newer(self):
        # When a new source package is uploaded to the parent distroseries,
        # a job is created that updates the relevant DSD.
        dsp = self.makeDerivedDistroSeries()
        source_package_name = self.factory.makeSourcePackageName()
        self.createPublication(
            source_package_name, ['1.0-1derived1', '1.0-1'],
            dsp.derived_series)
        self.createPublication(
            source_package_name, ['1.0-1'], dsp.parent_series)

        # Creating the SPPHs has created jobs for us, so grab them off
        # the queue.
        jobs = find_waiting_jobs(dsp.derived_series, source_package_name)
        self.runJob(jobs[0])
        ds_diff = find_dsd_for(dsp, source_package_name)
        self.assertEqual(1, ds_diff.count())
        self.assertEqual('1.0-1', ds_diff[0].parent_source_version)
        self.assertEqual('1.0-1derived1', ds_diff[0].source_version)
        self.assertEqual('1.0-1', ds_diff[0].base_version)
        # Now create a 1.0-2 upload to the parent.
        self.createPublication(
            source_package_name, ['1.0-2', '1.0-1'],
            dsp.parent_series)
        jobs = find_waiting_jobs(dsp.derived_series, source_package_name)
        self.runJob(jobs[0])
        # And the DSD we have a hold of will have updated.
        self.assertEqual('1.0-2', ds_diff[0].parent_source_version)
        self.assertEqual('1.0-1derived1', ds_diff[0].source_version)
        self.assertEqual('1.0-1', ds_diff[0].base_version)

    def test_child_gets_newer(self):
        # When a new source is uploaded to the child distroseries, the DSD is
        # updated and auto-blacklisted.
        dsp = self.makeDerivedDistroSeries()
        source_package_name = self.factory.makeSourcePackageName()
        self.createPublication(
            source_package_name, ['1.0-1'], dsp.derived_series)
        self.createPublication(
            source_package_name, ['1.0-1'], dsp.parent_series)
        jobs = find_waiting_jobs(dsp.derived_series, source_package_name)
        self.runJob(jobs[0])
        ds_diff = find_dsd_for(dsp, source_package_name)
        self.assertEqual(
            DistroSeriesDifferenceStatus.RESOLVED, ds_diff[0].status)
        self.createPublication(
            source_package_name, ['2.0-0derived1', '1.0-1'],
            dsp.derived_series)
        jobs = find_waiting_jobs(dsp.derived_series, source_package_name)
        self.runJob(jobs[0])
        self.assertEqual(
            DistroSeriesDifferenceStatus.BLACKLISTED_CURRENT,
            ds_diff[0].status)
        self.assertEqual('1.0-1', ds_diff[0].base_version)

    def test_child_is_synced(self):
        # If the source package gets 'synced' to the child from the parent,
        # the job correctly updates the DSD.
        dsp = self.makeDerivedDistroSeries()
        derived_series = dsp.derived_series
        source_package_name = self.factory.makeSourcePackageName()
        self.createPublication(
            source_package_name, ['1.0-1derived1', '1.0-1'], derived_series)
        self.createPublication(
            source_package_name, ['1.0-2', '1.0-1'], dsp.parent_series)
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        ds_diff = self.findDSD(derived_series, source_package_name)
        self.assertEqual('1.0-1', ds_diff[0].base_version)
        self.createPublication(
            source_package_name, ['1.0-2', '1.0-1'], derived_series)
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        self.assertEqual(
            DistroSeriesDifferenceStatus.RESOLVED, ds_diff[0].status)

    def test_only_in_child(self):
        # If a source package only exists in the child distroseries, the DSD
        # is created with the right type.
        dsp = self.makeDerivedDistroSeries()
        derived_series = dsp.derived_series
        source_package_name = self.factory.makeSourcePackageName()
        self.createPublication(
            source_package_name, ['1.0-0derived1'], derived_series)
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        ds_diff = self.findDSD(derived_series, source_package_name)
        self.assertEqual(
            DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES,
            ds_diff[0].difference_type)

    def test_only_in_parent(self):
        # If a source package only exists in the parent distroseries, the DSD
        # is created with the right type.
        dsp = self.makeDerivedDistroSeries()
        derived_series = dsp.derived_series
        source_package_name = self.factory.makeSourcePackageName()
        self.createPublication(
            source_package_name, ['1.0-1'], dsp.parent_series)
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        ds_diff = self.findDSD(derived_series, source_package_name)
        self.assertEqual(
            DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES,
            ds_diff[0].difference_type)

    def test_deleted_in_parent(self):
        # If a source package is deleted in the parent, a job is created, and
        # the DSD is updated correctly.
        dsp = self.makeDerivedDistroSeries()
        derived_series = dsp.derived_series
        source_package_name = self.factory.makeSourcePackageName()
        self.createPublication(
            source_package_name, ['1.0-1'], derived_series)
        spph = self.createPublication(
            source_package_name, ['1.0-1'], dsp.parent_series)
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        ds_diff = self.findDSD(derived_series, source_package_name)
        self.assertEqual(
            DistroSeriesDifferenceStatus.RESOLVED, ds_diff[0].status)
        spph.requestDeletion(self.factory.makePerson())
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        self.assertEqual(
            DistroSeriesDifferenceType.UNIQUE_TO_DERIVED_SERIES,
            ds_diff[0].difference_type)

    def test_deleted_in_child(self):
        # If a source package is deleted in the child, a job is created, and
        # the DSD is updated correctly.
        dsp = self.makeDerivedDistroSeries()
        derived_series = dsp.derived_series
        source_package_name = self.factory.makeSourcePackageName()
        spph = self.createPublication(
            source_package_name, ['1.0-1'], derived_series)
        self.createPublication(
            source_package_name, ['1.0-1'], dsp.parent_series)
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        ds_diff = self.findDSD(derived_series, source_package_name)
        self.assertEqual(
            DistroSeriesDifferenceStatus.RESOLVED, ds_diff[0].status)
        spph.requestDeletion(self.factory.makePerson())
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        self.assertEqual(
            DistroSeriesDifferenceType.MISSING_FROM_DERIVED_SERIES,
            ds_diff[0].difference_type)

    def test_no_job_for_PPA(self):
        # If a source package is uploaded to a PPA, a job is not created.
        dsp = self.makeDerivedDistroSeries()
        source_package_name = self.factory.makeSourcePackageName()
        ppa = self.factory.makeArchive()
        self.createPublication(
            source_package_name, ['1.0-1'], dsp.derived_series, ppa)
        self.assertContentEqual(
            [], find_waiting_jobs(dsp.derived_series, source_package_name))

    def test_no_job_for_PPA_with_deleted_source(self):
        # If a source package is deleted from a PPA, no job is created.
        dsp = self.makeDerivedDistroSeries()
        derived_series = dsp.derived_series
        source_package_name = self.factory.makeSourcePackageName()
        ppa = self.factory.makeArchive()
        spph = self.createPublication(
            source_package_name, ['1.0-1'], derived_series, ppa)
        spph.requestDeletion(ppa.owner)
        self.assertContentEqual(
            [], find_waiting_jobs(derived_series, source_package_name))

    def test_update_deletes_diffs(self):
        # When a DSD is updated, the diffs are invalidated.
        dsp = self.makeDerivedDistroSeries()
        derived_series = dsp.derived_series
        source_package_name = self.factory.makeSourcePackageName()
        self.createPublication(
            source_package_name, ['1.0-1derived1', '1.0-1'], derived_series)
        self.createPublication(
            source_package_name, ['1.0-2', '1.0-1'], dsp.parent_series)
        spr = self.factory.makeSourcePackageRelease(
            sourcepackagename=source_package_name, version='1.0-1')
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagerelease=spr,
            archive=dsp.parent_series.main_archive,
            distroseries=dsp.parent_series,
            status=PackagePublishingStatus.SUPERSEDED)
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        ds_diff = self.findDSD(derived_series, source_package_name)
        ds_diff[0].requestPackageDiffs(self.factory.makePerson())
        self.assertIsNot(None, ds_diff[0].package_diff)
        self.assertIsNot(None, ds_diff[0].parent_package_diff)
        self.createPublication(
            source_package_name, ['1.0-3', '1.0-2', '1.0-1'],
            dsp.parent_series)
        jobs = find_waiting_jobs(derived_series, source_package_name)
        self.runJob(jobs[0])
        # Since the diff showing the changes from 1.0-1 to 1.0-1derived1 is
        # still valid, it isn't reset, but the parent diff is.
        self.assertIsNot(None, ds_diff[0].package_diff)
        self.assertIs(None, ds_diff[0].parent_package_diff)


class TestDistroSeriesDifferenceJobPermissions(TestCaseWithFactory):
    """Database permissions test for `DistroSeriesDifferenceJob`."""

    layer = LaunchpadZopelessLayer

    def test_permissions(self):
        script_users = [
            'archivepublisher',
            'gina',
            'queued',
            'uploader',
            ]
        dsp = self.factory.makeDistroSeriesParent()
        parent = dsp.parent_series
        derived = dsp.derived_series
        packages = dict(
            (user, self.factory.makeSourcePackageName())
            for user in script_users)
        transaction.commit()
        for user in script_users:
            self.layer.switchDbUser(user)
            try:
                create_job(derived, packages[user], parent)
            except ProgrammingError, e:
                self.assertTrue(
                    False,
                    "Database role %s was unable to create a job.  "
                    "Error was: %s" % (user, e))

        # The test is that we get here without exceptions.
        pass