~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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Tests for sync package jobs."""

import operator
from textwrap import dedent

from storm.store import Store
from testtools.content import text_content
from testtools.matchers import (
    Equals,
    MatchesSetwise,
    MatchesStructure,
    )
import transaction
from zope.component import getUtility
from zope.security.interfaces import Unauthorized
from zope.security.proxy import removeSecurityProxy

from canonical.config import config
from lp.services.database.lpstorm import IStore
from canonical.launchpad.webapp.testing import verifyObject
from canonical.testing import (
    LaunchpadFunctionalLayer,
    LaunchpadZopelessLayer,
    ZopelessDatabaseLayer,
    )
from lp.bugs.interfaces.bugtask import BugTaskStatus
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.interfaces.series import SeriesStatus
from lp.registry.model.distroseriesdifferencecomment import (
    DistroSeriesDifferenceComment,
    )
from lp.services.features.testing import FeatureFixture
from lp.services.job.interfaces.job import (
    JobStatus,
    SuspendJobException,
    )
from lp.soyuz.adapters.overrides import SourceOverride
from lp.soyuz.enums import (
    ArchivePurpose,
    PackageCopyPolicy,
    PackageUploadStatus,
    SourcePackageFormat,
    )
from lp.soyuz.interfaces.archive import CannotCopy
from lp.soyuz.interfaces.component import IComponentSet
from lp.soyuz.interfaces.packagecopyjob import (
    IPackageCopyJob,
    IPackageCopyJobSource,
    IPlainPackageCopyJob,
    IPlainPackageCopyJobSource,
    )
from lp.soyuz.interfaces.publishing import PackagePublishingStatus
from lp.soyuz.interfaces.queue import IPackageUploadSet
from lp.soyuz.interfaces.section import ISectionSet
from lp.soyuz.interfaces.sourcepackageformat import (
    ISourcePackageFormatSelectionSet,
    )
from lp.soyuz.model.distroseriesdifferencejob import (
    FEATURE_FLAG_ENABLE_MODULE,
    )
from lp.soyuz.model.packagecopyjob import PackageCopyJob
from lp.soyuz.model.queue import PackageUpload
from lp.soyuz.tests.test_publishing import SoyuzTestPublisher
from lp.testing import (
    person_logged_in,
    run_script,
    TestCaseWithFactory,
    )
from lp.testing.fakemethod import FakeMethod
from lp.testing.mail_helpers import pop_notifications
from lp.testing.matchers import Provides


def get_dsd_comments(dsd):
    """Retrieve `DistroSeriesDifferenceComment`s for `dsd`."""
    return IStore(dsd).find(
        DistroSeriesDifferenceComment,
        DistroSeriesDifferenceComment.distro_series_difference == dsd)


class LocalTestHelper:
    """Put test helpers that want to be in the test classes here."""

    dbuser = config.IPlainPackageCopyJobSource.dbuser

    def makeJob(self, dsd=None, **kwargs):
        """Create a `PlainPackageCopyJob` that would resolve `dsd`."""
        if dsd is None:
            dsd = self.factory.makeDistroSeriesDifference()
        source_archive = dsd.parent_series.main_archive
        target_archive = dsd.derived_series.main_archive
        target_distroseries = dsd.derived_series
        target_pocket = self.factory.getAnyPocket()
        requester = self.factory.makePerson()
        return getUtility(IPlainPackageCopyJobSource).create(
            dsd.source_package_name.name, source_archive, target_archive,
            target_distroseries, target_pocket, requester=requester,
            package_version=dsd.parent_source_version, **kwargs)

    def makePPAJob(self, source_archive=None, target_archive=None, **kwargs):
        if source_archive is None:
            source_archive = self.factory.makeArchive(
                purpose=ArchivePurpose.PPA)
        if target_archive is None:
            target_archive = self.factory.makeArchive(
                purpose=ArchivePurpose.PPA)
        source_name = self.factory.getUniqueString('src-name')
        target_series = self.factory.makeDistroSeries()
        target_pocket = self.factory.getAnyPocket()
        requester = self.factory.makePerson()
        return getUtility(IPlainPackageCopyJobSource).create(
            source_name, source_archive, target_archive,
            target_series, target_pocket, requester=requester,
            package_version="1.0", **kwargs)

    def runJob(self, job):
        """Helper to switch to the right DB user and run the job."""
        # We are basically mimicking the job runner here.
        self.layer.txn.commit()
        self.layer.switchDbUser(self.dbuser)
        # Set the state to RUNNING.
        job.start()
        # Commit the RUNNING state.
        self.layer.txn.commit()
        try:
            job.run()
        except SuspendJobException:
            # Re-raise this one as many tests check for its presence.
            raise
        except:
            transaction.abort()
            job.fail()
        else:
            job.complete()


class PlainPackageCopyJobTests(TestCaseWithFactory, LocalTestHelper):
    """Test case for PlainPackageCopyJob."""

    layer = LaunchpadZopelessLayer

    def test_job_implements_IPlainPackageCopyJob(self):
        job = self.makeJob()
        self.assertTrue(verifyObject(IPlainPackageCopyJob, job))

    def test_job_source_implements_IPlainPackageCopyJobSource(self):
        job_source = getUtility(IPlainPackageCopyJobSource)
        self.assertTrue(verifyObject(IPlainPackageCopyJobSource, job_source))

    def test_create(self):
        # A PackageCopyJob can be created and stores its arguments.
        distroseries = self.factory.makeDistroSeries()
        archive1 = self.factory.makeArchive(distroseries.distribution)
        archive2 = self.factory.makeArchive(distroseries.distribution)
        requester = self.factory.makePerson()
        sponsored = self.factory.makePerson()
        source = getUtility(IPlainPackageCopyJobSource)
        job = source.create(
            package_name="foo", source_archive=archive1,
            target_archive=archive2, target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            package_version="1.0-1", include_binaries=False,
            copy_policy=PackageCopyPolicy.MASS_SYNC,
            requester=requester, sponsored=sponsored)
        self.assertProvides(job, IPackageCopyJob)
        self.assertEquals(archive1.id, job.source_archive_id)
        self.assertEquals(archive1, job.source_archive)
        self.assertEquals(archive2.id, job.target_archive_id)
        self.assertEquals(archive2, job.target_archive)
        self.assertEquals(distroseries, job.target_distroseries)
        self.assertEquals(PackagePublishingPocket.RELEASE, job.target_pocket)
        self.assertEqual("foo", job.package_name)
        self.assertEqual("1.0-1", job.package_version)
        self.assertEquals(False, job.include_binaries)
        self.assertEquals(PackageCopyPolicy.MASS_SYNC, job.copy_policy)
        self.assertEqual(requester, job.requester)
        self.assertEqual(sponsored, job.sponsored)

    def test_createMultiple_creates_one_job_per_copy(self):
        mother = self.factory.makeDistroSeriesParent()
        derived_series = mother.derived_series
        father = self.factory.makeDistroSeriesParent(
            derived_series=derived_series)
        mother_package = self.factory.makeSourcePackageName()
        father_package = self.factory.makeSourcePackageName()
        requester = self.factory.makePerson()
        job_source = getUtility(IPlainPackageCopyJobSource)
        copy_tasks = [
            (
                mother_package.name,
                "1.5mother1",
                mother.parent_series.main_archive,
                derived_series.main_archive,
                PackagePublishingPocket.RELEASE,
                ),
            (
                father_package.name,
                "0.9father1",
                father.parent_series.main_archive,
                derived_series.main_archive,
                PackagePublishingPocket.UPDATES,
                ),
            ]
        job_ids = list(
            job_source.createMultiple(mother.derived_series, copy_tasks,
                                      requester))
        jobs = list(job_source.getActiveJobs(derived_series.main_archive))
        self.assertContentEqual(job_ids, [job.id for job in jobs])
        self.assertEqual(len(copy_tasks), len(set([job.job for job in jobs])))
        # Get jobs into the same order as copy_tasks, for ease of
        # comparison.
        if jobs[0].package_name != mother_package.name:
            jobs = reversed(jobs)
        requested_copies = [
            (
                job.package_name,
                job.package_version,
                job.source_archive,
                job.target_archive,
                job.target_pocket,
                )
            for job in jobs]
        self.assertEqual(copy_tasks, requested_copies)

        # The passed requester should be the same on all jobs.
        actual_requester = set(job.requester for job in jobs)
        self.assertEqual(1, len(actual_requester))
        self.assertEqual(requester, jobs[0].requester)

    def test_getActiveJobs(self):
        # getActiveJobs() can retrieve all active jobs for an archive.
        distroseries = self.factory.makeDistroSeries()
        archive1 = self.factory.makeArchive(distroseries.distribution)
        archive2 = self.factory.makeArchive(distroseries.distribution)
        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        job = source.create(
            package_name="foo", source_archive=archive1,
            target_archive=archive2, target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            package_version="1.0-1", include_binaries=False,
            requester=requester)
        self.assertContentEqual([job], source.getActiveJobs(archive2))

    def test_getActiveJobs_gets_oldest_first(self):
        # getActiveJobs returns the oldest available job first.
        dsd = self.factory.makeDistroSeriesDifference()
        target_archive = dsd.derived_series.main_archive
        jobs = [self.makeJob(dsd) for counter in xrange(2)]
        source = getUtility(IPlainPackageCopyJobSource)
        self.assertEqual(jobs[0], source.getActiveJobs(target_archive)[0])

    def test_getActiveJobs_only_returns_waiting_jobs(self):
        # getActiveJobs ignores jobs that aren't in the WAITING state.
        job = self.makeJob()
        removeSecurityProxy(job).job._status = JobStatus.RUNNING
        source = getUtility(IPlainPackageCopyJobSource)
        self.assertContentEqual([], source.getActiveJobs(job.target_archive))

    def test_run_raises_errors(self):
        # A job reports unexpected errors as exceptions.
        class Boom(Exception):
            pass

        job = self.makeJob()
        removeSecurityProxy(job).attemptCopy = FakeMethod(failure=Boom())

        self.assertRaises(Boom, job.run)

    def test_run_posts_copy_failure_as_comment(self):
        # If the job fails with a CannotCopy exception, it swallows the
        # exception and posts a DistroSeriesDifferenceComment with the
        # failure message.
        dsd = self.factory.makeDistroSeriesDifference()
        self.factory.makeArchive(distribution=dsd.derived_series.distribution)
        job = self.makeJob(dsd)
        removeSecurityProxy(job).attemptCopy = FakeMethod(
            failure=CannotCopy("Server meltdown"))

        # The job's error handling will abort, so commit the objects we
        # created as would have happened in real life.
        transaction.commit()

        job.run()

        self.assertEqual(
            ["Server meltdown"],
            [comment.body_text for comment in get_dsd_comments(dsd)])

    def test_run_cannot_copy_unknown_package(self):
        # Attempting to copy an unknown package is reported as a
        # failure.
        distroseries = self.factory.makeDistroSeries()
        archive1 = self.factory.makeArchive(distroseries.distribution)
        archive2 = self.factory.makeArchive(distroseries.distribution)
        requester = self.factory.makePerson()
        job_source = getUtility(IPlainPackageCopyJobSource)
        job = job_source.create(
            package_name="foo", source_archive=archive1,
            target_archive=archive2, target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            package_version="1.0-1", include_binaries=False,
            requester=requester)
        naked_job = removeSecurityProxy(job)
        naked_job.reportFailure = FakeMethod()

        job.run()

        self.assertEqual(1, naked_job.reportFailure.call_count)

    def test_target_ppa_non_release_pocket(self):
        # When copying to a PPA archive the target must be the release pocket.
        distroseries = self.factory.makeDistroSeries()
        package = self.factory.makeSourcePackageName()
        archive1 = self.factory.makeArchive(distroseries.distribution)
        archive2 = self.factory.makeArchive(distroseries.distribution)
        requester = self.factory.makePerson()
        source = getUtility(IPlainPackageCopyJobSource)
        job = source.create(
            package_name=package.name, source_archive=archive1,
            target_archive=archive2, target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.UPDATES,
            include_binaries=False, package_version='1.0',
            requester=requester)

        naked_job = removeSecurityProxy(job)
        naked_job.reportFailure = FakeMethod()

        job.run()

        self.assertEqual(1, naked_job.reportFailure.call_count)

    def test_run(self):
        # A proper test run synchronizes packages.

        # Turn on DSD jobs.
        self.useFixture(FeatureFixture({FEATURE_FLAG_ENABLE_MODULE: 'on'}))

        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest

        # Synchronise from breezy-autotest to a brand new distro derived
        # from breezy.
        breezy_archive = self.factory.makeArchive(
            distroseries.distribution, purpose=ArchivePurpose.PRIMARY)
        dsp = self.factory.makeDistroSeriesParent(parent_series=distroseries)
        target_series = dsp.derived_series
        target_archive = self.factory.makeArchive(
            target_series.distribution, purpose=ArchivePurpose.PRIMARY)
        getUtility(ISourcePackageFormatSelectionSet).add(
            target_series, SourcePackageFormat.FORMAT_1_0)

        publisher.getPubSource(
            distroseries=distroseries, sourcename="libc",
            version="2.8-1", status=PackagePublishingStatus.PUBLISHED,
            archive=breezy_archive)
        # The target archive needs ancestry so the package is
        # auto-accepted.
        publisher.getPubSource(
            distroseries=target_series, sourcename="libc",
            version="2.8-0", status=PackagePublishingStatus.PUBLISHED,
            archive=target_archive)

        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        with person_logged_in(target_archive.owner):
            target_archive.newComponentUploader(requester, "main")
        job = source.create(
            package_name="libc",
            source_archive=breezy_archive, target_archive=target_archive,
            target_distroseries=target_series,
            target_pocket=PackagePublishingPocket.RELEASE,
            package_version="2.8-1", include_binaries=False,
            requester=requester)
        self.assertEqual("libc", job.package_name)
        self.assertEqual("2.8-1", job.package_version)

        # Make sure everything hits the database, switching db users
        # aborts.
        transaction.commit()
        self.layer.switchDbUser(self.dbuser)
        job.run()

        published_sources = target_archive.getPublishedSources(
            name="libc", version="2.8-1")
        self.assertIsNot(None, published_sources.any())

        # The copy should have sent an email too. (see
        # soyuz/scripts/tests/test_copypackage.py for detailed
        # notification tests)
        emails = pop_notifications()
        self.assertTrue(len(emails) > 0)

        # Switch back to a db user that has permission to clean up
        # featureflag.
        self.layer.switchDbUser('launchpad_main')

    def test_getOopsVars(self):
        distroseries = self.factory.makeDistroSeries()
        archive1 = self.factory.makeArchive(distroseries.distribution)
        archive2 = self.factory.makeArchive(distroseries.distribution)
        requester = self.factory.makePerson()
        source = getUtility(IPlainPackageCopyJobSource)
        job = source.create(
            package_name="foo", source_archive=archive1,
            target_archive=archive2, target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            package_version="1.0-1", include_binaries=False,
            requester=requester)
        oops_vars = job.getOopsVars()
        naked_job = removeSecurityProxy(job)
        self.assertIn(
            ('source_archive_id', archive1.id), oops_vars)
        self.assertIn(
            ('target_archive_id', archive2.id), oops_vars)
        self.assertIn(
            ('target_distroseries_id', distroseries.id), oops_vars)
        self.assertIn(
            ('package_copy_job_id', naked_job.context.id), oops_vars)
        self.assertIn(
            ('package_copy_job_type', naked_job.context.job_type.title),
            oops_vars)

    def test_smoke(self):
        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest
        archive1 = self.factory.makeArchive(distroseries.distribution)
        archive2 = self.factory.makeArchive(distroseries.distribution)
        requester = self.factory.makePerson()
        publisher.getPubSource(
            distroseries=distroseries, sourcename="libc",
            version="2.8-1", status=PackagePublishingStatus.PUBLISHED,
            archive=archive1)
        getUtility(IPlainPackageCopyJobSource).create(
            package_name="libc", source_archive=archive1,
            target_archive=archive2, target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            package_version="2.8-1", include_binaries=False,
            requester=requester)
        with person_logged_in(archive2.owner):
            archive2.newComponentUploader(requester, "main")
        transaction.commit()

        out, err, exit_code = run_script(
            "LP_DEBUG_SQL=1 cronscripts/process-job-source.py -vv %s" % (
                IPlainPackageCopyJobSource.getName()))

        self.addDetail("stdout", text_content(out))
        self.addDetail("stderr", text_content(err))

        self.assertEqual(0, exit_code)
        copied_source_package = archive2.getPublishedSources(
            name="libc", version="2.8-1", exact_match=True).first()
        self.assertIsNot(copied_source_package, None)

    def test___repr__(self):
        distroseries = self.factory.makeDistroSeries()
        archive1 = self.factory.makeArchive(distroseries.distribution)
        archive2 = self.factory.makeArchive(distroseries.distribution)
        requester = self.factory.makePerson()
        source = getUtility(IPlainPackageCopyJobSource)
        job = source.create(
            package_name="foo", source_archive=archive1,
            target_archive=archive2, target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            package_version="1.0-1", include_binaries=True,
            requester=requester)
        self.assertEqual(
            ("<PlainPackageCopyJob to copy package foo from "
             "{distroseries.distribution.name}/{archive1.name} to "
             "{distroseries.distribution.name}/{archive2.name}, "
             "RELEASE pocket, in {distroseries.distribution.name} "
             "{distroseries.name}, including binaries>").format(
                distroseries=distroseries, archive1=archive1,
                archive2=archive2),
            repr(job))

    def test_getPendingJobsPerPackage_finds_jobs(self):
        # getPendingJobsPerPackage finds jobs, and the packages they
        # belong to.
        dsd = self.factory.makeDistroSeriesDifference()
        job = self.makeJob(dsd)
        job_source = getUtility(IPlainPackageCopyJobSource)
        self.assertEqual(
            {dsd.source_package_name.name: job},
            job_source.getPendingJobsPerPackage(dsd.derived_series))

    def test_getPendingJobsPerPackage_ignores_other_distroseries(self):
        # getPendingJobsPerPackage only looks for jobs on the indicated
        # distroseries.
        self.makeJob()
        other_series = self.factory.makeDistroSeries()
        job_source = getUtility(IPlainPackageCopyJobSource)
        self.assertEqual(
            {}, job_source.getPendingJobsPerPackage(other_series))

    def test_getPendingJobsPerPackage_only_returns_pending_jobs(self):
        # getPendingJobsPerPackage ignores jobs that have already been
        # run.
        dsd = self.factory.makeDistroSeriesDifference()
        job = self.makeJob(dsd)
        job_source = getUtility(IPlainPackageCopyJobSource)
        found_by_state = {}
        for status in JobStatus.items:
            removeSecurityProxy(job).job._status = status
            result = job_source.getPendingJobsPerPackage(dsd.derived_series)
            if len(result) > 0:
                found_by_state[status] = result[dsd.source_package_name.name]
        expected = {
            JobStatus.WAITING: job,
            JobStatus.RUNNING: job,
            JobStatus.SUSPENDED: job,
        }
        self.assertEqual(expected, found_by_state)

    def test_getPendingJobsPerPackage_distinguishes_jobs(self):
        # getPendingJobsPerPackage associates the right job with the
        # right package.
        derived_series = self.factory.makeDistroSeries()
        dsds = [
            self.factory.makeDistroSeriesDifference(
                derived_series=derived_series)
            for counter in xrange(2)]
        jobs = map(self.makeJob, dsds)
        job_source = getUtility(IPlainPackageCopyJobSource)
        self.assertEqual(
            dict(zip([dsd.source_package_name.name for dsd in dsds], jobs)),
            job_source.getPendingJobsPerPackage(derived_series))

    def test_getPendingJobsPerPackage_picks_oldest_job_for_dsd(self):
        # If there are multiple jobs for one package,
        # getPendingJobsPerPackage picks the oldest.
        dsd = self.factory.makeDistroSeriesDifference()
        jobs = [self.makeJob(dsd) for counter in xrange(2)]
        job_source = getUtility(IPlainPackageCopyJobSource)
        self.assertEqual(
            {dsd.source_package_name.name: jobs[0]},
            job_source.getPendingJobsPerPackage(dsd.derived_series))

    def test_getPendingJobsPerPackage_ignores_dsds_without_jobs(self):
        # getPendingJobsPerPackage produces no dict entry for packages
        # that have no pending jobs, even if they do have DSDs.
        dsd = self.factory.makeDistroSeriesDifference()
        job_source = getUtility(IPlainPackageCopyJobSource)
        self.assertEqual(
            {}, job_source.getPendingJobsPerPackage(dsd.derived_series))

    def test_getIncompleteJobsForArchive_finds_jobs_in_right_archive(self):
        # getIncompleteJobsForArchive should return all the jobs in an
        # specified archive.
        target1 = self.factory.makeArchive(purpose=ArchivePurpose.PPA)
        target2 = self.factory.makeArchive(purpose=ArchivePurpose.PPA)
        job_source = getUtility(IPlainPackageCopyJobSource)
        target1_jobs = [
            self.makePPAJob(target_archive=target1)
            for counter in xrange(2)]
        self.makePPAJob(target2)

        pending_jobs = list(job_source.getIncompleteJobsForArchive(target1))
        has_both_jobs = MatchesSetwise(*(map(Equals, target1_jobs)))
        self.assertThat(pending_jobs, has_both_jobs)

    def test_getIncompleteJobsForArchive_finds_failed_and_running_jobs(self):
        # getIncompleteJobsForArchive should return only waiting, failed
        # and running jobs.
        jobs = []
        ppa = self.factory.makeArchive(purpose=ArchivePurpose.PPA)
        for status in JobStatus.items:
            job = self.makePPAJob(target_archive=ppa)
            removeSecurityProxy(job).job._status = status

        job_source = getUtility(IPlainPackageCopyJobSource)
        found_jobs = job_source.getIncompleteJobsForArchive(ppa)
        found_statuses = [job.status for job in found_jobs]
        self.assertThat(
            [JobStatus.WAITING, JobStatus.RUNNING, JobStatus.FAILED],
            MatchesSetwise(*(map(Equals, found_statuses))))

    def test_copying_to_main_archive_ancestry_overrides(self):
        # The job will complete right away for auto-approved copies to a
        # main archive and apply any ancestry overrides.
        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest

        target_archive = self.factory.makeArchive(
            distroseries.distribution, purpose=ArchivePurpose.PRIMARY)
        source_archive = self.factory.makeArchive()

        # Publish a package in the source archive with some overridable
        # properties set to known values.
        publisher.getPubSource(
            distroseries=distroseries, sourcename="libc",
            component='universe', section='web',
            version="2.8-1", status=PackagePublishingStatus.PUBLISHED,
            archive=source_archive)

        # Now put the same named package in the target archive with
        # different override values.
        publisher.getPubSource(
            distroseries=distroseries, sourcename="libc",
            component='restricted', section='games',
            version="2.8-0", status=PackagePublishingStatus.PUBLISHED,
            archive=target_archive)

        # Now, run the copy job, which should auto-approve the copy and
        # override the package with the existing values in the
        # target_archive.

        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        with person_logged_in(target_archive.owner):
            target_archive.newComponentUploader(requester, "restricted")
        job = source.create(
            package_name="libc",
            package_version="2.8-1",
            source_archive=source_archive,
            target_archive=target_archive,
            target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            include_binaries=False,
            requester=requester)

        self.runJob(job)

        new_publication = target_archive.getPublishedSources(
            name='libc', version='2.8-1').one()
        self.assertEqual('restricted', new_publication.component.name)
        self.assertEqual('games', new_publication.section.name)

        # There should also be a PackageDiff generated between the new
        # publication and the ancestry.
        [diff] = new_publication.sourcepackagerelease.package_diffs
        self.assertIsNot(None, diff)

    def test_copying_to_ppa_archive(self):
        # Packages can be copied into PPA archives.
        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest

        target_archive = self.factory.makeArchive(
            distroseries.distribution, purpose=ArchivePurpose.PPA)
        source_archive = self.factory.makeArchive()

        # Publish a package in the source archive with some overridable
        # properties set to known values.
        publisher.getPubSource(
            distroseries=distroseries, sourcename="libc",
            component='universe', section='web',
            version="2.8-1", status=PackagePublishingStatus.PUBLISHED,
            archive=source_archive)

        # Now, run the copy job.
        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        with person_logged_in(target_archive.owner):
            target_archive.newComponentUploader(requester, "main")
        job = source.create(
            package_name="libc",
            package_version="2.8-1",
            source_archive=source_archive,
            target_archive=target_archive,
            target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            include_binaries=False,
            requester=requester)

        self.runJob(job)
        self.assertEqual(JobStatus.COMPLETED, job.status)

        new_publication = target_archive.getPublishedSources(
            name='libc', version='2.8-1').one()
        self.assertEqual('main', new_publication.component.name)
        self.assertEqual('web', new_publication.section.name)

    def test_copying_to_main_archive_manual_overrides(self):
        # Test processing a packagecopyjob that has manual overrides.
        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest

        target_archive = self.factory.makeArchive(
            distroseries.distribution, purpose=ArchivePurpose.PRIMARY)
        source_archive = self.factory.makeArchive()

        # Publish a package in the source archive with some overridable
        # properties set to known values.
        source_package = publisher.getPubSource(
            distroseries=distroseries, sourcename="copyme",
            component='universe', section='web',
            version="2.8-1", status=PackagePublishingStatus.PUBLISHED,
            archive=source_archive)

        # Now, run the copy job, which should raise an error because
        # there's no ancestry.
        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        with person_logged_in(target_archive.owner):
            target_archive.newComponentUploader(requester, "main")
        job = source.create(
            package_name="copyme",
            package_version="2.8-1",
            source_archive=source_archive,
            target_archive=target_archive,
            target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            include_binaries=False,
            requester=requester)

        self.assertRaises(SuspendJobException, self.runJob, job)
        # Simulate the job runner suspending after getting a
        # SuspendJobException
        job.suspend()
        self.layer.txn.commit()
        self.layer.switchDbUser("launchpad_main")

        # Add some overrides to the job.
        package = source_package.sourcepackagerelease.sourcepackagename
        restricted = getUtility(IComponentSet)['restricted']
        editors = getUtility(ISectionSet)['editors']
        override = SourceOverride(package, restricted, editors)
        job.addSourceOverride(override)

        # Accept the upload to release the job then run it.
        pu = getUtility(IPackageUploadSet).getByPackageCopyJobIDs(
            [removeSecurityProxy(job).context.id]).one()
        pu.acceptFromQueue()
        self.runJob(job)

        # The copied source should have the manual overrides, not the
        # original values.
        new_publication = target_archive.getPublishedSources(
            name='copyme', version='2.8-1').one()
        self.assertEqual('restricted', new_publication.component.name)
        self.assertEqual('editors', new_publication.section.name)

    def test_copying_to_main_archive_with_no_ancestry(self):
        # The job should suspend itself and create a packageupload with
        # a reference to the package_copy_job.
        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest

        target_archive = self.factory.makeArchive(
            distroseries.distribution, purpose=ArchivePurpose.PRIMARY)
        source_archive = self.factory.makeArchive()

        # Publish a package in the source archive with some overridable
        # properties set to known values.
        publisher.getPubSource(
            distroseries=distroseries, sourcename="copyme",
            component='multiverse', section='web',
            version="2.8-1", status=PackagePublishingStatus.PUBLISHED,
            archive=source_archive)

        # There is no package of the same name already in the target
        # archive.
        existing_sources = target_archive.getPublishedSources(name='copyme')
        self.assertEqual(None, existing_sources.any())

        # Now, run the copy job.
        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        job = source.create(
            package_name="copyme",
            package_version="2.8-1",
            source_archive=source_archive,
            target_archive=target_archive,
            target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            include_binaries=False,
            requester=requester)

        # The job should be suspended and there's a PackageUpload with
        # its package_copy_job set.
        self.assertRaises(SuspendJobException, self.runJob, job)
        pu = Store.of(target_archive).find(
            PackageUpload,
            PackageUpload.package_copy_job_id == job.id).one()
        pcj = removeSecurityProxy(job).context
        self.assertEqual(pcj, pu.package_copy_job)

        # The job metadata should contain default overrides from the
        # UnknownOverridePolicy policy.
        self.assertEqual('universe', pcj.metadata['component_override'])

    def createCopyJob(self, sourcename, component, section, version):
        # Helper method to create a package copy job for a package with
        # the given sourcename, component, section and version.
        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest

        target_archive = self.factory.makeArchive(
            distroseries.distribution, purpose=ArchivePurpose.PRIMARY)
        source_archive = self.factory.makeArchive()

        # Publish a package in the source archive with some overridable
        # properties set to known values.
        publisher.getPubSource(
            distroseries=distroseries, sourcename=sourcename,
            component=component, section=section,
            version=version, status=PackagePublishingStatus.PUBLISHED,
            archive=source_archive)

        # Now, run the copy job.
        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        job = source.create(
            package_name=sourcename,
            package_version=version,
            source_archive=source_archive,
            target_archive=target_archive,
            target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            include_binaries=False,
            requester=requester)

        # Run the job so it gains a PackageUpload.
        self.assertRaises(SuspendJobException, self.runJob, job)
        pcj = removeSecurityProxy(job).context
        return pcj

    def test_copying_to_main_archive_debian_override_contrib(self):
        # The job uses the overrides to map debian components to
        # the right components.
        # 'contrib' gets mapped to 'multiverse'.

        # Create debian component.
        self.factory.makeComponent('contrib')
        # Create a copy job for a package in 'contrib'.
        pcj = self.createCopyJob('package', 'contrib', 'web', '2.8.1')

        self.assertEqual('multiverse', pcj.metadata['component_override'])

    def test_copying_to_main_archive_debian_override_nonfree(self):
        # The job uses the overrides to map debian components to
        # the right components.
        # 'nonfree' gets mapped to 'multiverse'.

        # Create debian component.
        self.factory.makeComponent('non-free')
        # Create a copy job for a package in 'non-free'.
        pcj = self.createCopyJob('package', 'non-free', 'web', '2.8.1')

        self.assertEqual('multiverse', pcj.metadata['component_override'])

    def test_copying_to_main_archive_unapproved(self):
        # Uploading to a series that is in a state that precludes auto
        # approval will cause the job to suspend and a packageupload
        # created in the UNAPPROVED state.
        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest
        # The series is frozen so it won't auto-approve new packages.
        distroseries.status = SeriesStatus.FROZEN

        target_archive = self.factory.makeArchive(
            distroseries.distribution, purpose=ArchivePurpose.PRIMARY)
        source_archive = self.factory.makeArchive()

        # Publish a package in the source archive.
        publisher.getPubSource(
            distroseries=distroseries, sourcename="copyme",
            version="2.8-1", status=PackagePublishingStatus.PUBLISHED,
            component='multiverse', section='web',
            archive=source_archive)

        # Now put the same named package in the target archive so it has
        # ancestry.
        publisher.getPubSource(
            distroseries=distroseries, sourcename="copyme",
            version="2.8-0", status=PackagePublishingStatus.PUBLISHED,
            component='main', section='games',
            archive=target_archive)

        # Now, run the copy job.
        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        job = source.create(
            package_name="copyme",
            package_version="2.8-1",
            source_archive=source_archive,
            target_archive=target_archive,
            target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            include_binaries=False,
            requester=requester)

        # The job should be suspended and there's a PackageUpload with
        # its package_copy_job set in the UNAPPROVED queue.
        self.assertRaises(SuspendJobException, self.runJob, job)

        pu = Store.of(target_archive).find(
            PackageUpload,
            PackageUpload.package_copy_job_id == job.id).one()
        pcj = removeSecurityProxy(job).context
        self.assertEqual(pcj, pu.package_copy_job)
        self.assertEqual(PackageUploadStatus.UNAPPROVED, pu.status)

        # The job's metadata should contain the override ancestry from
        # the target archive.
        self.assertEqual('main', pcj.metadata['component_override'])

    def test_copying_after_job_released(self):
        # The first pass of the job may have created a PackageUpload and
        # suspended the job.  Here we test the second run to make sure
        # that it actually copies the package.
        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest
        distroseries.changeslist = "changes@example.com"

        target_archive = self.factory.makeArchive(
            distroseries.distribution, purpose=ArchivePurpose.PRIMARY)
        source_archive = self.factory.makeArchive()

        # Publish a package in the source archive.
        publisher.getPubSource(
            distroseries=distroseries, sourcename="copyme",
            version="2.8-1", status=PackagePublishingStatus.PUBLISHED,
            archive=source_archive)

        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson(
            displayname="Nancy Requester", email="requester@example.com")
        with person_logged_in(target_archive.owner):
            target_archive.newComponentUploader(requester, "main")
        job = source.create(
            package_name="copyme",
            package_version="2.8-1",
            source_archive=source_archive,
            target_archive=target_archive,
            target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            include_binaries=False,
            requester=requester)

        # Run the job so it gains a PackageUpload.
        self.assertRaises(SuspendJobException, self.runJob, job)
        # Simulate the job runner suspending after getting a
        # SuspendJobException
        job.suspend()
        self.layer.txn.commit()
        self.layer.switchDbUser("launchpad_main")

        # Accept the upload to release the job then run it.
        pu = getUtility(IPackageUploadSet).getByPackageCopyJobIDs(
            [removeSecurityProxy(job).context.id]).one()
        pu.acceptFromQueue()
        # Clear existing emails so we can see only the ones the job
        # generates later.
        pop_notifications()
        self.runJob(job)

        # The job should have set the PU status to DONE:
        self.assertEqual(PackageUploadStatus.DONE, pu.status)

        # Make sure packages were actually copied.
        existing_sources = target_archive.getPublishedSources(name='copyme')
        self.assertIsNot(None, existing_sources.any())

        # It would be nice to test emails in a separate test but it would
        # require all of the same setup as above again so we might as well
        # do it here.
        emails = pop_notifications(sort_key=operator.itemgetter('To'))

        # We expect an uploader email and an announcement to the changeslist.
        self.assertEquals(2, len(emails))
        self.assertIn("requester@example.com", emails[0]['To'])
        self.assertIn("changes@example.com", emails[1]['To'])
        self.assertEqual(
            "Nancy Requester <requester@example.com>",
            emails[1]['From'])

    def test_copying_closes_bugs(self):
        # Copying a package into a primary archive should close any bugs
        # mentioned in its changelog for versions added since the most
        # recently published version in the target.

        # Firstly, lots of boring set up.
        publisher = SoyuzTestPublisher()
        publisher.prepareBreezyAutotest()
        distroseries = publisher.breezy_autotest
        target_archive = self.factory.makeArchive(
            distroseries.distribution, purpose=ArchivePurpose.PRIMARY)
        source_archive = self.factory.makeArchive()
        bug280 = self.factory.makeBug()
        bug281 = self.factory.makeBug()
        bug282 = self.factory.makeBug()

        # Publish a package in the source archive and give it a changelog
        # entry that closes a bug.
        source_pub = self.factory.makeSourcePackagePublishingHistory(
            distroseries=distroseries, sourcepackagename="libc",
            version="2.8-2", status=PackagePublishingStatus.PUBLISHED,
            archive=source_archive)
        spr = removeSecurityProxy(source_pub).sourcepackagerelease
        changelog = dedent("""\
            libc (2.8-2) unstable; urgency=low

              * closes: %s

             -- Foo Bar <foo@example.com>  Tue, 01 Jan 1970 01:50:41 +0000

            libc (2.8-1) unstable; urgency=low

              * closes: %s

             -- Foo Bar <foo@example.com>  Tue, 01 Jan 1970 01:50:41 +0000

            libc (2.8-0) unstable; urgency=low

              * closes: %s

             -- Foo Bar <foo@example.com>  Tue, 01 Jan 1970 01:50:41 +0000
            """ % (bug282.id, bug281.id, bug280.id))
        spr.changelog = self.factory.makeLibraryFileAlias(content=changelog)
        spr.changelog_entry = "dummy"
        self.layer.txn.commit()  # Librarian.
        bugtask280 = self.factory.makeBugTask(
            target=spr.sourcepackage, bug=bug280)
        bugtask281 = self.factory.makeBugTask(
            target=spr.sourcepackage, bug=bug281)
        bugtask282 = self.factory.makeBugTask(
            target=spr.sourcepackage, bug=bug282)

        # Now put the same named package in the target archive at the
        # oldest version in the changelog.
        publisher.getPubSource(
            distroseries=distroseries, sourcename="libc",
            version="2.8-0", status=PackagePublishingStatus.PUBLISHED,
            archive=target_archive)

        # Run the copy job.
        source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        with person_logged_in(target_archive.owner):
            target_archive.newComponentUploader(requester, "main")
        job = source.create(
            package_name="libc",
            package_version="2.8-2",
            source_archive=source_archive,
            target_archive=target_archive,
            target_distroseries=distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            include_binaries=False,
            requester=requester)
        self.runJob(job)

        # All the bugs apart from the one for 2.8-0 should be fixed.
        self.assertEqual(BugTaskStatus.FIXRELEASED, bugtask282.status)
        self.assertEqual(BugTaskStatus.FIXRELEASED, bugtask281.status)
        self.assertEqual(BugTaskStatus.NEW, bugtask280.status)

    def test_findMatchingDSDs_matches_all_DSDs_for_job(self):
        # findMatchingDSDs finds matching DSDs for any of the packages
        # in the job.
        dsd = self.factory.makeDistroSeriesDifference()
        naked_job = removeSecurityProxy(self.makeJob(dsd))
        self.assertContentEqual([dsd], naked_job.findMatchingDSDs())

    def test_findMatchingDSDs_ignores_other_source_series(self):
        # findMatchingDSDs tries to ignore DSDs that are for different
        # parent series than the job's source series.  (This can't be
        # done with perfect precision because the job doesn't keep track
        # of source distroseries, but in practice it should be good
        # enough).
        dsd = self.factory.makeDistroSeriesDifference()
        naked_job = removeSecurityProxy(self.makeJob(dsd))

        # If the dsd differs only in parent series, that's enough to
        # make it a non-match.
        removeSecurityProxy(dsd).parent_series = (
            self.factory.makeDistroSeries())

        self.assertContentEqual([], naked_job.findMatchingDSDs())

    def test_findMatchingDSDs_ignores_other_packages(self):
        # findMatchingDSDs does not return DSDs that are similar to the
        # information in the job, but are for different packages.
        dsd = self.factory.makeDistroSeriesDifference()
        self.factory.makeDistroSeriesDifference(
            derived_series=dsd.derived_series,
            parent_series=dsd.parent_series)
        naked_job = removeSecurityProxy(self.makeJob(dsd))
        self.assertContentEqual([dsd], naked_job.findMatchingDSDs())

    def test_addSourceOverride(self):
        # Test the addOverride method which adds an ISourceOverride to the
        # metadata.
        name = self.factory.makeSourcePackageName()
        component = self.factory.makeComponent()
        section = self.factory.makeSection()
        pcj = self.factory.makePlainPackageCopyJob()
        self.layer.txn.commit()
        self.layer.switchDbUser('sync_packages')

        override = SourceOverride(
            source_package_name=name,
            component=component,
            section=section)
        pcj.addSourceOverride(override)

        metadata_component = getUtility(
            IComponentSet)[pcj.metadata["component_override"]]
        metadata_section = getUtility(
            ISectionSet)[pcj.metadata["section_override"]]
        matcher = MatchesStructure.byEquality(
            component=metadata_component,
            section=metadata_section)
        self.assertThat(override, matcher)

    def test_addSourceOverride_accepts_None_component_as_no_change(self):
        # When given an override with None as the component,
        # addSourceOverride will update the section but not the
        # component.
        pcj = self.factory.makePlainPackageCopyJob()
        old_component = self.factory.makeComponent()
        old_section = self.factory.makeSection()
        pcj.addSourceOverride(SourceOverride(
            source_package_name=pcj.package_name,
            component=old_component, section=old_section))
        new_section = self.factory.makeSection()
        pcj.addSourceOverride(SourceOverride(
            source_package_name=pcj.package_name,
            component=None, section=new_section))
        self.assertEqual(old_component.name, pcj.component_name)
        self.assertEqual(new_section.name, pcj.section_name)

    def test_addSourceOverride_accepts_None_section_as_no_change(self):
        # When given an override with None for the section,
        # addSourceOverride will update the component but not the
        # section.
        pcj = self.factory.makePlainPackageCopyJob()
        old_component = self.factory.makeComponent()
        old_section = self.factory.makeSection()
        pcj.addSourceOverride(SourceOverride(
            source_package_name=pcj.package_name,
            component=old_component, section=old_section))
        new_component = self.factory.makeComponent()
        pcj.addSourceOverride(SourceOverride(
            source_package_name=pcj.package_name,
            component=new_component, section=None))
        self.assertEqual(new_component.name, pcj.component_name)
        self.assertEqual(old_section.name, pcj.section_name)

    def test_getSourceOverride(self):
        # Test the getSourceOverride which gets an ISourceOverride from
        # the metadata.
        name = self.factory.makeSourcePackageName()
        component = self.factory.makeComponent()
        section = self.factory.makeSection()
        pcj = self.factory.makePlainPackageCopyJob(
            package_name=name.name, package_version="1.0")
        self.layer.txn.commit()
        self.layer.switchDbUser('sync_packages')

        override = SourceOverride(
            source_package_name=name,
            component=component,
            section=section)
        pcj.addSourceOverride(override)

        self.assertEqual(override, pcj.getSourceOverride())

    def test_getPolicyImplementation_returns_policy(self):
        # getPolicyImplementation returns the ICopyPolicy that was
        # chosen for the job.
        dsd = self.factory.makeDistroSeriesDifference()
        for policy in PackageCopyPolicy.items:
            naked_job = removeSecurityProxy(
                self.makeJob(dsd, copy_policy=policy))
            self.assertEqual(
                policy, naked_job.getPolicyImplementation().enum_value)

    def test_rejects_PackageUpload_when_job_fails(self):
        # If a PCJ with a PU fails when running then we need to ensure the
        # PU gets rejected.
        target_archive = self.factory.makeArchive(
            purpose=ArchivePurpose.PRIMARY)
        source_archive = self.factory.makeArchive()
        source_pub = self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename="copyme",
            version="1.0",
            archive=source_archive,
            status=PackagePublishingStatus.PUBLISHED)
        job_source = getUtility(IPlainPackageCopyJobSource)
        requester = self.factory.makePerson()
        job = job_source.create(
            package_name="copyme",
            package_version="1.0",
            source_archive=source_archive,
            target_archive=target_archive,
            target_distroseries=source_pub.distroseries,
            target_pocket=PackagePublishingPocket.RELEASE,
            include_binaries=False,
            requester=requester)

        # Run the job so it gains a PackageUpload.
        self.assertRaises(SuspendJobException, self.runJob, job)
        # Simulate the job runner suspending after getting a
        # SuspendJobException
        job.suspend()
        self.layer.txn.commit()
        self.layer.switchDbUser("launchpad_main")

        # Patch the job's attemptCopy() method so it just raises an
        # exception.
        def blow_up():
            raise Exception("I blew up")
        naked_job = removeSecurityProxy(job)
        self.patch(naked_job, "attemptCopy", blow_up)

        # Accept the upload to release the job then run it.
        pu = getUtility(IPackageUploadSet).getByPackageCopyJobIDs(
            [removeSecurityProxy(job).context.id]).one()
        pu.acceptFromQueue()
        self.runJob(job)

        # The job should have set the PU status to REJECTED.
        self.assertEqual(PackageUploadStatus.REJECTED, pu.status)


class TestPlainPackageCopyJobPermissions(TestCaseWithFactory):

    layer = LaunchpadFunctionalLayer

    def test_extendMetadata_edit_privilege_by_queue_admin(self):
        # A person who has any queue admin rights can edit the copy job.
        archive = self.factory.makeArchive(purpose=ArchivePurpose.PRIMARY)
        pcj = self.factory.makePlainPackageCopyJob(target_archive=archive)
        queue_admin = self.factory.makePerson()
        with person_logged_in(pcj.target_archive.owner):
            pcj.target_archive.newQueueAdmin(queue_admin, "main")
        with person_logged_in(queue_admin):
            # This won't blow up.
            pcj.extendMetadata({})

    def test_extendMetadata_edit_privilege_by_other(self):
        # Random people cannot edit the copy job.
        pcj = self.factory.makePlainPackageCopyJob()
        self.assertRaises(
            Unauthorized, getattr, pcj, "extendMetadata")

    def test_PPA_edit_privilege_by_owner(self):
        # A PCJ for a PPA allows the PPA owner to edit it.
        ppa = self.factory.makeArchive(purpose=ArchivePurpose.PPA)
        pcj = self.factory.makePlainPackageCopyJob(target_archive=ppa)
        with person_logged_in(ppa.owner):
            # This will not throw an exception.
            pcj.extendMetadata({})

    def test_PPA_edit_privilege_by_other(self):
        # A PCJ for a PPA does not allow non-owners to edit it.
        ppa = self.factory.makeArchive(purpose=ArchivePurpose.PPA)
        pcj = self.factory.makePlainPackageCopyJob(target_archive=ppa)
        person = self.factory.makePerson()
        with person_logged_in(person):
            self.assertRaises(
                Unauthorized, getattr, pcj, "extendMetadata")


class TestPlainPackageCopyJobDbPrivileges(TestCaseWithFactory,
                                          LocalTestHelper):
    """Test that `PlainPackageCopyJob` has the database privileges it needs.

    This test looks for errors, not failures.  It's here only to see that
    these operations don't run into any privilege limitations.
    """

    layer = LaunchpadZopelessLayer

    def test_findMatchingDSDs(self):
        job = self.makeJob()
        transaction.commit()
        self.layer.switchDbUser(self.dbuser)
        removeSecurityProxy(job).findMatchingDSDs()

    def test_reportFailure(self):
        job = self.makeJob()
        transaction.commit()
        self.layer.switchDbUser(self.dbuser)
        removeSecurityProxy(job).reportFailure(CannotCopy("Mommy it hurts"))


class TestPackageCopyJobSource(TestCaseWithFactory):
    """Test the `IPackageCopyJob` utility."""

    layer = ZopelessDatabaseLayer

    def test_implements_interface(self):
        job_source = getUtility(IPackageCopyJobSource)
        self.assertThat(job_source, Provides(IPackageCopyJobSource))

    def test_wrap_accepts_None(self):
        job_source = getUtility(IPackageCopyJobSource)
        self.assertIs(None, job_source.wrap(None))

    def test_wrap_wraps_PlainPackageCopyJob(self):
        original_ppcj = self.factory.makePlainPackageCopyJob()
        IStore(PackageCopyJob).flush()
        pcj = IStore(PackageCopyJob).get(PackageCopyJob, original_ppcj.id)
        self.assertNotEqual(None, pcj)
        job_source = getUtility(IPackageCopyJobSource)
        self.assertEqual(original_ppcj, job_source.wrap(pcj))