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
|
# Copyright 2009-2010 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Test native publication workflow for Soyuz. """
import datetime
import operator
import os
import shutil
from StringIO import StringIO
import tempfile
import pytz
from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy
from canonical.config import config
from canonical.database.constants import UTC_NOW
from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet
from canonical.launchpad.webapp.errorlog import ErrorReportingUtility
from canonical.launchpad.webapp.interfaces import NotFoundError
from canonical.testing import (
DatabaseFunctionalLayer, LaunchpadZopelessLayer)
from lp.archivepublisher.config import Config
from lp.archivepublisher.diskpool import DiskPool
from lp.buildmaster.interfaces.buildbase import BuildStatus
from lp.registry.interfaces.distribution import IDistributionSet
from lp.registry.interfaces.person import IPersonSet
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.registry.interfaces.sourcepackage import SourcePackageUrgency
from lp.registry.interfaces.sourcepackagename import ISourcePackageNameSet
from lp.soyuz.model.processor import ProcessorFamily
from lp.soyuz.model.publishing import (
SourcePackagePublishingHistory, BinaryPackagePublishingHistory)
from lp.soyuz.interfaces.archive import ArchivePurpose
from lp.soyuz.interfaces.archivearch import IArchiveArchSet
from lp.soyuz.interfaces.binarypackagename import IBinaryPackageNameSet
from lp.soyuz.interfaces.binarypackagerelease import BinaryPackageFormat
from lp.soyuz.interfaces.component import IComponentSet
from lp.soyuz.interfaces.section import ISectionSet
from lp.soyuz.interfaces.publishing import (
IPublishingSet, PackagePublishingPriority, PackagePublishingStatus)
from lp.soyuz.interfaces.queue import PackageUploadStatus
from canonical.launchpad.scripts import FakeLogger
from lp.testing import TestCaseWithFactory
from lp.testing.factory import (
LaunchpadObjectFactory, remove_security_proxy_and_shout_at_engineer)
from lp.testing.fakemethod import FakeMethod
class SoyuzTestPublisher:
"""Helper class able to publish coherent source and binaries in Soyuz."""
def __init__(self):
self.factory = LaunchpadObjectFactory()
self.default_package_name = 'foo'
def setUpDefaultDistroSeries(self, distroseries=None):
"""Set up a distroseries that will be used by default.
This distro series is used to publish packages in, if you don't
specify any when using the publishing methods.
It also sets up a person that can act as the default uploader,
and makes sure that the default package name exists in the
database.
:param distroseries: The `IDistroSeries` to use as default. If
it's None, one will be created.
:return: The `IDistroSeries` that got set as default.
"""
if distroseries is None:
distroseries = self.factory.makeDistroRelease()
self.distroseries = distroseries
# Set up a person that has a GPG key.
self.person = getUtility(IPersonSet).getByName('name16')
# Make sure the name exists in the database, to make it easier
# to get packages from distributions and distro series.
name_set = getUtility(ISourcePackageNameSet)
name_set.getOrCreateByName(self.default_package_name)
return self.distroseries
def prepareBreezyAutotest(self):
"""Prepare ubuntutest/breezy-autotest for publications.
It's also called during the normal test-case setUp.
"""
self.ubuntutest = getUtility(IDistributionSet)['ubuntutest']
self.breezy_autotest = self.ubuntutest['breezy-autotest']
self.setUpDefaultDistroSeries(self.breezy_autotest)
# Only create the DistroArchSeries needed if they do not exist yet.
# This makes it easier to experiment at the python command line
# (using "make harness").
try:
self.breezy_autotest_i386 = self.breezy_autotest['i386']
except NotFoundError:
self.breezy_autotest_i386 = self.breezy_autotest.newArch(
'i386', ProcessorFamily.get(1), False, self.person,
supports_virtualized=True)
try:
self.breezy_autotest_hppa = self.breezy_autotest['hppa']
except NotFoundError:
self.breezy_autotest_hppa = self.breezy_autotest.newArch(
'hppa', ProcessorFamily.get(4), False, self.person)
self.breezy_autotest.nominatedarchindep = self.breezy_autotest_i386
fake_chroot = self.addMockFile('fake_chroot.tar.gz')
self.breezy_autotest_i386.addOrUpdateChroot(fake_chroot)
self.breezy_autotest_hppa.addOrUpdateChroot(fake_chroot)
def addFakeChroots(self, distroseries=None):
"""Add fake chroots for all the architectures in distroseries."""
if distroseries is None:
distroseries = self.distroseries
fake_chroot = self.addMockFile('fake_chroot.tar.gz')
for arch in distroseries.architectures:
arch.addOrUpdateChroot(fake_chroot)
def regetBreezyAutotest(self):
self.ubuntutest = getUtility(IDistributionSet)['ubuntutest']
self.breezy_autotest = self.ubuntutest['breezy-autotest']
self.person = getUtility(IPersonSet).getByName('name16')
self.breezy_autotest_i386 = self.breezy_autotest['i386']
self.breezy_autotest_hppa = self.breezy_autotest['hppa']
def addMockFile(self, filename, filecontent='nothing', restricted=False):
"""Add a mock file in Librarian.
Returns a ILibraryFileAlias corresponding to the file uploaded.
"""
library_file = getUtility(ILibraryFileAliasSet).create(
filename, len(filecontent), StringIO(filecontent),
'application/text', restricted=restricted)
return library_file
def addPackageUpload(self, archive, distroseries,
pocket=PackagePublishingPocket.RELEASE,
changes_file_name="foo_666_source.changes",
changes_file_content="fake changes file content",
upload_status=PackageUploadStatus.DONE):
signing_key = self.person.gpg_keys[0]
package_upload = distroseries.createQueueEntry(
pocket, changes_file_name, changes_file_content, archive,
signing_key)
status_to_method = {
PackageUploadStatus.DONE: 'setDone',
PackageUploadStatus.ACCEPTED: 'setAccepted',
}
naked_package_upload = remove_security_proxy_and_shout_at_engineer(
package_upload)
method = getattr(
naked_package_upload, status_to_method[upload_status])
method()
return package_upload
def getPubSource(self, sourcename=None, version='666', component='main',
filename=None, section='base',
filecontent='I do not care about sources.',
changes_file_content="Fake: fake changes file content",
status=PackagePublishingStatus.PENDING,
pocket=PackagePublishingPocket.RELEASE,
urgency=SourcePackageUrgency.LOW,
scheduleddeletiondate=None, dateremoved=None,
distroseries=None, archive=None, builddepends=None,
builddependsindep=None, architecturehintlist='all',
dsc_standards_version='3.6.2', dsc_format='1.0',
dsc_binaries='foo-bin', build_conflicts=None,
build_conflicts_indep=None,
dsc_maintainer_rfc822='Foo Bar <foo@bar.com>',
maintainer=None, creator=None, date_uploaded=UTC_NOW,
spr_only=False):
"""Return a mock source publishing record.
if spr_only is specified, the source is not published and the
sourcepackagerelease object is returned instead.
"""
if sourcename is None:
sourcename = self.default_package_name
spn = getUtility(ISourcePackageNameSet).getOrCreateByName(sourcename)
component = getUtility(IComponentSet)[component]
section = getUtility(ISectionSet)[section]
if distroseries is None:
distroseries = self.distroseries
if archive is None:
archive = distroseries.main_archive
if maintainer is None:
maintainer = self.person
if creator is None:
creator = self.person
spr = distroseries.createUploadedSourcePackageRelease(
sourcepackagename=spn,
maintainer=maintainer,
creator=creator,
component=component,
section=section,
urgency=urgency,
version=version,
builddepends=builddepends,
builddependsindep=builddependsindep,
build_conflicts=build_conflicts,
build_conflicts_indep=build_conflicts_indep,
architecturehintlist=architecturehintlist,
changelog=None,
changelog_entry=None,
dsc=None,
copyright='placeholder ...',
dscsigningkey=self.person.gpg_keys[0],
dsc_maintainer_rfc822=dsc_maintainer_rfc822,
dsc_standards_version=dsc_standards_version,
dsc_format=dsc_format,
dsc_binaries=dsc_binaries,
archive=archive, dateuploaded=date_uploaded)
changes_file_name = "%s_%s_source.changes" % (sourcename, version)
if spr_only:
upload_status = PackageUploadStatus.ACCEPTED
else:
upload_status = PackageUploadStatus.DONE
package_upload = self.addPackageUpload(
archive, distroseries, pocket,
changes_file_name=changes_file_name,
changes_file_content=changes_file_content,
upload_status=upload_status)
naked_package_upload = remove_security_proxy_and_shout_at_engineer(
package_upload)
naked_package_upload.addSource(spr)
if filename is None:
filename = "%s_%s.dsc" % (sourcename, version)
alias = self.addMockFile(
filename, filecontent, restricted=archive.private)
spr.addFile(alias)
if spr_only:
return spr
if status == PackagePublishingStatus.PUBLISHED:
datepublished = UTC_NOW
else:
datepublished = None
spph = SourcePackagePublishingHistory(
distroseries=distroseries,
sourcepackagerelease=spr,
component=spr.component,
section=spr.section,
status=status,
datecreated=date_uploaded,
dateremoved=dateremoved,
datepublished=datepublished,
scheduleddeletiondate=scheduleddeletiondate,
pocket=pocket,
archive=archive)
return spph
def getPubBinaries(self, binaryname='foo-bin', summary='Foo app is great',
description='Well ...\nit does nothing, though',
shlibdep=None, depends=None, recommends=None,
suggests=None, conflicts=None, replaces=None,
provides=None, pre_depends=None, enhances=None,
breaks=None, filecontent='bbbiiinnnaaarrryyy',
changes_file_content="Fake: fake changes file",
status=PackagePublishingStatus.PENDING,
pocket=PackagePublishingPocket.RELEASE,
format=BinaryPackageFormat.DEB,
scheduleddeletiondate=None, dateremoved=None,
distroseries=None,
archive=None,
pub_source=None,
version='666',
architecturespecific=False,
builder=None,
component='main'):
"""Return a list of binary publishing records."""
if distroseries is None:
distroseries = self.distroseries
if archive is None:
archive = distroseries.main_archive
if pub_source is None:
sourcename = "%s" % binaryname.split('-')[0]
if architecturespecific:
architecturehintlist = 'any'
else:
architecturehintlist = 'all'
pub_source = self.getPubSource(
sourcename=sourcename, status=status, pocket=pocket,
archive=archive, distroseries=distroseries,
version=version, architecturehintlist=architecturehintlist,
component=component)
else:
archive = pub_source.archive
builds = pub_source.createMissingBuilds()
published_binaries = []
for build in builds:
build.builder = builder
binarypackagerelease = self.uploadBinaryForBuild(
build, binaryname, filecontent, summary, description,
shlibdep, depends, recommends, suggests, conflicts, replaces,
provides, pre_depends, enhances, breaks, format)
pub_binaries = self.publishBinaryInArchive(
binarypackagerelease, archive, status, pocket,
scheduleddeletiondate, dateremoved)
published_binaries.extend(pub_binaries)
package_upload = self.addPackageUpload(
archive, distroseries, pocket,
changes_file_content=changes_file_content,
changes_file_name='%s_%s_%s.changes' %
(binaryname, binarypackagerelease.version,
build.arch_tag))
package_upload.addBuild(build)
return sorted(
published_binaries, key=operator.attrgetter('id'), reverse=True)
def uploadBinaryForBuild(
self, build, binaryname, filecontent="anything",
summary="summary", description="description", shlibdep=None,
depends=None, recommends=None, suggests=None, conflicts=None,
replaces=None, provides=None, pre_depends=None, enhances=None,
breaks=None, format=BinaryPackageFormat.DEB, debug_package=None):
"""Return the corresponding `BinaryPackageRelease`."""
sourcepackagerelease = build.source_package_release
distroarchseries = build.distro_arch_series
architecturespecific = (
not sourcepackagerelease.architecturehintlist == 'all')
binarypackagename = getUtility(
IBinaryPackageNameSet).getOrCreateByName(binaryname)
binarypackagerelease = build.createBinaryPackageRelease(
version=sourcepackagerelease.version,
component=sourcepackagerelease.component,
section=sourcepackagerelease.section,
binarypackagename=binarypackagename,
summary=summary,
description=description,
shlibdeps=shlibdep,
depends=depends,
recommends=recommends,
suggests=suggests,
conflicts=conflicts,
replaces=replaces,
provides=provides,
pre_depends=pre_depends,
enhances=enhances,
breaks=breaks,
essential=False,
installedsize=100,
architecturespecific=architecturespecific,
binpackageformat=format,
priority=PackagePublishingPriority.STANDARD,
debug_package=debug_package)
# Create the corresponding binary file.
if architecturespecific:
filearchtag = distroarchseries.architecturetag
else:
filearchtag = 'all'
filename = '%s_%s_%s.%s' % (binaryname, sourcepackagerelease.version,
filearchtag, format.name.lower())
alias = self.addMockFile(
filename, filecontent=filecontent,
restricted=build.archive.private)
binarypackagerelease.addFile(alias)
# Adjust the build record in way it looks complete.
naked_build = removeSecurityProxy(build)
naked_build.status = BuildStatus.FULLYBUILT
naked_build.date_finished = datetime.datetime(
2008, 1, 1, 0, 5, 0, tzinfo=pytz.UTC)
naked_build.date_started = (
build.date_finished - datetime.timedelta(minutes=5))
buildlog_filename = 'buildlog_%s-%s-%s.%s_%s_%s.txt.gz' % (
build.distribution.name,
build.distro_series.name,
build.distro_arch_series.architecturetag,
build.source_package_release.name,
build.source_package_release.version,
build.status.name)
naked_build.log = self.addMockFile(
buildlog_filename, filecontent='Built!',
restricted=build.archive.private)
return binarypackagerelease
def publishBinaryInArchive(
self, binarypackagerelease, archive,
status=PackagePublishingStatus.PENDING,
pocket=PackagePublishingPocket.RELEASE,
scheduleddeletiondate=None, dateremoved=None):
"""Return the corresponding BinaryPackagePublishingHistory."""
distroarchseries = binarypackagerelease.build.distro_arch_series
# Publish the binary.
if binarypackagerelease.architecturespecific:
archs = [distroarchseries]
else:
archs = distroarchseries.distroseries.architectures
pub_binaries = []
for arch in archs:
pub = BinaryPackagePublishingHistory(
distroarchseries=arch,
binarypackagerelease=binarypackagerelease,
component=binarypackagerelease.component,
section=binarypackagerelease.section,
priority=binarypackagerelease.priority,
status=status,
scheduleddeletiondate=scheduleddeletiondate,
dateremoved=dateremoved,
datecreated=UTC_NOW,
pocket=pocket,
archive=archive)
if status == PackagePublishingStatus.PUBLISHED:
pub.datepublished = UTC_NOW
pub_binaries.append(pub)
return pub_binaries
def _findChangesFile(self, top, name_fragment):
"""File with given name fragment in directory tree starting at top."""
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
if (name.endswith('.changes') and
name.find(name_fragment) > -1):
return os.path.join(root, name)
return None
def createSource(
self, archive, sourcename, version, distroseries=None,
new_version=None):
"""Create source with meaningful '.changes' file."""
top = 'lib/lp/archiveuploader/tests/data/suite'
name_fragment = '%s_%s' % (sourcename, version)
changesfile_path = self._findChangesFile(top, name_fragment)
source = None
if changesfile_path is not None:
if new_version is None:
new_version = version
changesfile_content = ''
handle = open(changesfile_path, 'r')
try:
changesfile_content = handle.read()
finally:
handle.close()
source = self.getPubSource(
sourcename=sourcename, archive=archive, version=new_version,
changes_file_content=changesfile_content,
distroseries=distroseries)
return source
class TestNativePublishingBase(TestCaseWithFactory, SoyuzTestPublisher):
layer = LaunchpadZopelessLayer
dbuser = config.archivepublisher.dbuser
def __init__(self, methodName='runTest'):
super(TestNativePublishingBase, self).__init__(methodName=methodName)
SoyuzTestPublisher.__init__(self)
def setUp(self):
"""Setup a pool dir, the librarian, and instantiate the DiskPool."""
super(TestNativePublishingBase, self).setUp()
self.layer.switchDbUser(config.archivepublisher.dbuser)
self.prepareBreezyAutotest()
self.config = Config(self.ubuntutest)
self.config.setupArchiveDirs()
self.pool_dir = self.config.poolroot
self.temp_dir = self.config.temproot
self.logger = FakeLogger()
self.logger.message = FakeMethod()
self.disk_pool = DiskPool(self.pool_dir, self.temp_dir, self.logger)
def tearDown(self):
"""Tear down blows the pool dir away."""
super(TestNativePublishingBase, self).tearDown()
shutil.rmtree(self.config.distroroot)
def getPubSource(self, *args, **kwargs):
"""Overrides `SoyuzTestPublisher.getPubSource`.
Commits the transaction before returning, this way the rest of
the test will immediately notice the just-created records.
"""
source = SoyuzTestPublisher.getPubSource(self, *args, **kwargs)
self.layer.commit()
return source
def getPubBinaries(self, *args, **kwargs):
"""Overrides `SoyuzTestPublisher.getPubBinaries`.
Commits the transaction before returning, this way the rest of
the test will immediately notice the just-created records.
"""
binaries = SoyuzTestPublisher.getPubBinaries(self, *args, **kwargs)
self.layer.commit()
return binaries
def checkPublication(self, pub, status):
"""Assert the publication has the given status."""
self.assertEqual(
pub.status, status, "%s is not %s (%s)" % (
pub.displayname, status.name, pub.status.name))
def checkPublications(self, pubs, status):
"""Assert the given publications have the given status.
See `checkPublication`.
"""
for pub in pubs:
self.checkPublication(pub, status)
def checkPastDate(self, date, lag=None):
"""Assert given date is older than 'now'.
Optionally the user can pass a 'lag' which will be added to 'now'
before comparing.
"""
UTC = pytz.timezone("UTC")
limit = datetime.datetime.now(UTC)
if lag is not None:
limit = limit + lag
self.assertTrue(date < limit, "%s >= %s" % (date, limit))
def checkSuperseded(self, pubs, supersededby=None):
self.checkPublications(pubs, PackagePublishingStatus.SUPERSEDED)
for pub in pubs:
self.checkPastDate(pub.datesuperseded)
if supersededby is not None:
if isinstance(pub, BinaryPackagePublishingHistory):
dominant = supersededby.binarypackagerelease.build
else:
dominant = supersededby.sourcepackagerelease
self.assertEquals(dominant, pub.supersededby)
else:
self.assertIs(None, pub.supersededby)
class TestNativePublishing(TestNativePublishingBase):
def test_publish_source(self):
# Source publications result in a PUBLISHED publishing record and
# the corresponding files are dumped in the disk pool/.
pub_source = self.getPubSource(filecontent='Hello world')
pub_source.publish(self.disk_pool, self.logger)
self.assertEqual(
PackagePublishingStatus.PUBLISHED,
pub_source.status)
pool_path = "%s/main/f/foo/foo_666.dsc" % self.pool_dir
self.assertEqual(open(pool_path).read().strip(), 'Hello world')
def test_publish_binaries(self):
# Binary publications result in a PUBLISHED publishing record and
# the corresponding files are dumped in the disk pool/.
pub_binary = self.getPubBinaries(filecontent='Hello world')[0]
pub_binary.publish(self.disk_pool, self.logger)
self.assertEqual(
PackagePublishingStatus.PUBLISHED,
pub_binary.status)
pool_path = "%s/main/f/foo/foo-bin_666_all.deb" % self.pool_dir
self.assertEqual(open(pool_path).read().strip(), 'Hello world')
def testPublishingOverwriteFileInPool(self):
"""Test if publishOne refuses to overwrite a file in pool.
Check if it also keeps the original file content.
It's done by publishing 'foo' by-hand and ensuring it
has a special content, then publish 'foo' again, via publisher,
and finally check one of the 'foo' files content.
"""
foo_path = os.path.join(self.pool_dir, 'main', 'f', 'foo')
os.makedirs(foo_path)
foo_dsc_path = os.path.join(foo_path, 'foo_666.dsc')
foo_dsc = open(foo_dsc_path, 'w')
foo_dsc.write('Hello world')
foo_dsc.close()
pub_source = self.getPubSource(filecontent="Something")
pub_source.publish(self.disk_pool, self.logger)
# And an oops should be filed for the error.
error_utility = ErrorReportingUtility()
error_report = error_utility.getLastOopsReport()
fp = StringIO()
error_report.write(fp)
error_text = fp.getvalue()
self.assertTrue("PoolFileOverwriteError" in error_text)
self.layer.commit()
self.assertEqual(
pub_source.status, PackagePublishingStatus.PENDING)
self.assertEqual(open(foo_dsc_path).read().strip(), 'Hello world')
def testPublishingDifferentContents(self):
"""Test if publishOne refuses to overwrite its own publication."""
pub_source = self.getPubSource(filecontent='foo is happy')
pub_source.publish(self.disk_pool, self.logger)
self.layer.commit()
foo_name = "%s/main/f/foo/foo_666.dsc" % self.pool_dir
pub_source.sync()
self.assertEqual(
pub_source.status, PackagePublishingStatus.PUBLISHED)
self.assertEqual(open(foo_name).read().strip(), 'foo is happy')
# try to publish 'foo' again with a different content, it
# raises internally and keeps the files with the original
# content.
pub_source2 = self.getPubSource(filecontent='foo is depressing')
pub_source2.publish(self.disk_pool, self.logger)
self.layer.commit()
pub_source2.sync()
self.assertEqual(
pub_source2.status, PackagePublishingStatus.PENDING)
self.assertEqual(open(foo_name).read().strip(), 'foo is happy')
def testPublishingAlreadyInPool(self):
"""Test if publishOne works if file is already in Pool.
It should identify that the file has the same content and
mark it as PUBLISHED.
"""
pub_source = self.getPubSource(
sourcename='bar', filecontent='bar is good')
pub_source.publish(self.disk_pool, self.logger)
self.layer.commit()
bar_name = "%s/main/b/bar/bar_666.dsc" % self.pool_dir
self.assertEqual(open(bar_name).read().strip(), 'bar is good')
pub_source.sync()
self.assertEqual(
pub_source.status, PackagePublishingStatus.PUBLISHED)
pub_source2 = self.getPubSource(
sourcename='bar', filecontent='bar is good')
pub_source2.publish(self.disk_pool, self.logger)
self.layer.commit()
pub_source2.sync()
self.assertEqual(
pub_source2.status, PackagePublishingStatus.PUBLISHED)
def testPublishingSymlink(self):
"""Test if publishOne moving publication between components.
After check if the pool file contents as the same, it should
create a symlink in the new pointing to the original file.
"""
content = 'am I a file or a symbolic link ?'
# publish sim.dsc in main and re-publish in universe
pub_source = self.getPubSource(
sourcename='sim', filecontent=content)
pub_source2 = self.getPubSource(
sourcename='sim', component='universe', filecontent=content)
pub_source.publish(self.disk_pool, self.logger)
pub_source2.publish(self.disk_pool, self.logger)
self.layer.commit()
pub_source.sync()
pub_source2.sync()
self.assertEqual(
pub_source.status, PackagePublishingStatus.PUBLISHED)
self.assertEqual(
pub_source2.status, PackagePublishingStatus.PUBLISHED)
# check the resulted symbolic link
sim_universe = "%s/universe/s/sim/sim_666.dsc" % self.pool_dir
self.assertEqual(
os.readlink(sim_universe), '../../../main/s/sim/sim_666.dsc')
# if the contexts don't match it raises, so the publication
# remains pending.
pub_source3 = self.getPubSource(
sourcename='sim', component='restricted',
filecontent='It is all my fault')
pub_source3.publish(self.disk_pool, self.logger)
self.layer.commit()
pub_source3.sync()
self.assertEqual(
pub_source3.status, PackagePublishingStatus.PENDING)
def testPublishInAnotherArchive(self):
"""Publication in another archive
Basically test if publishing records target to other archive
than Distribution.main_archive work as expected
"""
cprov = getUtility(IPersonSet).getByName('cprov')
test_pool_dir = tempfile.mkdtemp()
test_temp_dir = tempfile.mkdtemp()
test_disk_pool = DiskPool(test_pool_dir, test_temp_dir, self.logger)
pub_source = self.getPubSource(
sourcename="foo", filecontent='Am I a PPA Record ?',
archive=cprov.archive)
pub_source.publish(test_disk_pool, self.logger)
self.layer.commit()
pub_source.sync()
self.assertEqual(pub_source.status, PackagePublishingStatus.PUBLISHED)
self.assertEqual(pub_source.sourcepackagerelease.upload_archive,
cprov.archive)
foo_name = "%s/main/f/foo/foo_666.dsc" % test_pool_dir
self.assertEqual(open(foo_name).read().strip(), 'Am I a PPA Record ?')
# Remove locally created dir.
shutil.rmtree(test_pool_dir)
shutil.rmtree(test_temp_dir)
class OverrideFromAncestryTestCase(TestCaseWithFactory):
"""Test `IPublishing.overrideFromAncestry`.
When called from a `SourcePackagePublishingHistory` or a
`BinaryPackagePublishingHistory` it sets the object target component
according to its ancestry if available or falls back to the component
it was originally uploaded to.
"""
layer = LaunchpadZopelessLayer
def setUp(self):
TestCaseWithFactory.setUp(self)
self.test_publisher = SoyuzTestPublisher()
self.test_publisher.prepareBreezyAutotest()
def test_overrideFromAncestry_only_works_for_pending_records(self):
# overrideFromAncestry only accepts PENDING publishing records.
source = self.test_publisher.getPubSource()
forbidden_status = [
item
for item in PackagePublishingStatus.items
if item is not PackagePublishingStatus.PENDING]
for status in forbidden_status:
source.status = status
self.layer.commit()
self.assertRaisesWithContent(
AssertionError,
'Cannot override published records.',
source.overrideFromAncestry)
def makeSource(self):
"""Return a 'source' publication.
It's pending publication with binaries in a brand new PPA
and in 'main' component.
"""
test_archive = self.factory.makeArchive(
distribution=self.test_publisher.ubuntutest,
purpose = ArchivePurpose.PPA)
source = self.test_publisher.getPubSource(archive=test_archive)
self.test_publisher.getPubBinaries(pub_source=source)
return source
def copyAndCheck(self, pub_record, series, component_name):
"""Copy and check if overrideFromAncestry is working as expected.
The copied publishing record is targeted to the same component
as its source, but override_from_ancestry changes it to follow
the ancestry or fallback to the SPR/BPR original component.
"""
copied = pub_record.copyTo(
series, pub_record.pocket, series.main_archive)
# Cope with heterogeneous results from copyTo().
try:
copies = tuple(copied)
except TypeError:
copies = (copied, )
for copy in copies:
self.assertEquals(copy.component, pub_record.component)
copy.overrideFromAncestry()
self.layer.commit()
self.assertEquals(copy.component.name, 'universe')
def test_overrideFromAncestry_fallback_to_source_component(self):
# overrideFromancestry on the lack of ancestry, falls back to the
# component the source was originally uploaded to.
source = self.makeSource()
# Adjust the source package release original component.
universe = getUtility(IComponentSet)['universe']
source.sourcepackagerelease.component = universe
self.copyAndCheck(source, source.distroseries, 'universe')
def test_overrideFromAncestry_fallback_to_binary_component(self):
# overrideFromAncestry on the lack of ancestry, falls back to the
# component the binary was originally uploaded to.
binary = self.makeSource().getPublishedBinaries()[0]
# Adjust the binary package release original component.
universe = getUtility(IComponentSet)['universe']
removeSecurityProxy(binary.binarypackagerelease).component = universe
self.copyAndCheck(
binary, binary.distroarchseries.distroseries, 'universe')
def test_overrideFromAncestry_follow_ancestry_source_component(self):
# overrideFromAncestry finds and uses the component of the most
# recent PUBLISHED publication of the same name in the same
#location.
source = self.makeSource()
# Create a published ancestry source in the copy destination
# targeted to 'universe' and also 2 other noise source
# publications, a pending source target to 'restricted' and
# a published, but older, one target to 'multiverse'.
self.test_publisher.getPubSource(component='restricted')
self.test_publisher.getPubSource(
component='multiverse', status=PackagePublishingStatus.PUBLISHED)
self.test_publisher.getPubSource(
component='universe', status=PackagePublishingStatus.PUBLISHED)
# Overridden copy it targeted to 'universe'.
self.copyAndCheck(source, source.distroseries, 'universe')
def test_overrideFromAncestry_follow_ancestry_binary_component(self):
# overrideFromAncestry finds and uses the component of the most
# recent published publication of the same name in the same
# location.
binary = self.makeSource().getPublishedBinaries()[0]
# Create a published ancestry binary in the copy destination
# targeted to 'universe'.
restricted_source = self.test_publisher.getPubSource(
component='restricted')
self.test_publisher.getPubBinaries(pub_source=restricted_source)
multiverse_source = self.test_publisher.getPubSource(
component='multiverse')
self.test_publisher.getPubBinaries(
pub_source=multiverse_source,
status=PackagePublishingStatus.PUBLISHED)
ancestry_source = self.test_publisher.getPubSource(
component='universe')
self.test_publisher.getPubBinaries(
pub_source=ancestry_source,
status=PackagePublishingStatus.PUBLISHED)
# Overridden copy it targeted to 'universe'.
self.copyAndCheck(
binary, binary.distroarchseries.distroseries, 'universe')
class BuildRecordCreationTests(TestNativePublishingBase):
"""Test the creation of build records."""
def setUp(self):
super(BuildRecordCreationTests, self).setUp()
self.distro = self.factory.makeDistribution()
self.distroseries = self.factory.makeDistroSeries(
distribution=self.distro, name="crazy")
self.archive = self.factory.makeArchive()
self.avr_family = self.factory.makeProcessorFamily(
name="avr", restricted=True)
self.factory.makeProcessor(self.avr_family, "avr2001")
self.avr_distroarch = self.factory.makeDistroArchSeries(
architecturetag='avr', processorfamily=self.avr_family,
distroseries=self.distroseries, supports_virtualized=True)
self.sparc_family = self.factory.makeProcessorFamily(name="sparc",
restricted=False)
self.factory.makeProcessor(self.sparc_family, "sparc64")
self.sparc_distroarch = self.factory.makeDistroArchSeries(
architecturetag='sparc', processorfamily=self.sparc_family,
distroseries=self.distroseries, supports_virtualized=True)
self.distroseries.nominatedarchindep = self.sparc_distroarch
self.addFakeChroots(self.distroseries)
def getPubSource(self, architecturehintlist):
"""Return a mock source package publishing record for the archive
and architecture used in this testcase.
:param architecturehintlist: Architecture hint list
(e.g. "i386 amd64")
"""
return super(BuildRecordCreationTests, self).getPubSource(
archive=self.archive, distroseries=self.distroseries,
architecturehintlist=architecturehintlist)
def test__getAllowedArchitectures_restricted(self):
"""Test _getAllowedArchitectures doesn't return unrestricted
archs.
For a normal archive, only unrestricted architectures should
be used.
"""
available_archs = [self.sparc_distroarch, self.avr_distroarch]
pubrec = self.getPubSource(architecturehintlist='any')
self.assertEquals([self.sparc_distroarch],
pubrec._getAllowedArchitectures(available_archs))
def test__getAllowedArchitectures_restricted_override(self):
"""Test _getAllowedArchitectures honors overrides of restricted archs.
Restricted architectures should only be allowed if there is
an explicit ArchiveArch association with the archive.
"""
available_archs = [self.sparc_distroarch, self.avr_distroarch]
getUtility(IArchiveArchSet).new(self.archive, self.avr_family)
pubrec = self.getPubSource(architecturehintlist='any')
self.assertEquals([self.sparc_distroarch, self.avr_distroarch],
pubrec._getAllowedArchitectures(available_archs))
def test_createMissingBuilds_restricts_any(self):
"""createMissingBuilds() should limit builds targeted at 'any'
architecture to those allowed for the archive.
"""
pubrec = self.getPubSource(architecturehintlist='any')
builds = pubrec.createMissingBuilds()
self.assertEquals(1, len(builds))
self.assertEquals(self.sparc_distroarch, builds[0].distro_arch_series)
def test_createMissingBuilds_restricts_explicitlist(self):
"""createMissingBuilds() limits builds targeted at a variety of
architectures architecture to those allowed for the archive.
"""
pubrec = self.getPubSource(architecturehintlist='sparc i386 avr')
builds = pubrec.createMissingBuilds()
self.assertEquals(1, len(builds))
self.assertEquals(self.sparc_distroarch, builds[0].distro_arch_series)
def test_createMissingBuilds_restricts_all(self):
"""createMissingBuilds() should limit builds targeted at 'all'
architectures to the nominated independent architecture,
if that is allowed for the archive.
"""
pubrec = self.getPubSource(architecturehintlist='all')
builds = pubrec.createMissingBuilds()
self.assertEquals(1, len(builds))
self.assertEquals(self.sparc_distroarch, builds[0].distro_arch_series)
def test_createMissingBuilds_restrict_override(self):
"""createMissingBuilds() should limit builds targeted at 'any'
architecture to architectures that are unrestricted or
explicitly associated with the archive.
"""
getUtility(IArchiveArchSet).new(self.archive, self.avr_family)
pubrec = self.getPubSource(architecturehintlist='any')
builds = pubrec.createMissingBuilds()
self.assertEquals(2, len(builds))
self.assertEquals(self.avr_distroarch, builds[0].distro_arch_series)
self.assertEquals(self.sparc_distroarch, builds[1].distro_arch_series)
class PublishingSetTests(TestCaseWithFactory):
layer = DatabaseFunctionalLayer
def setUp(self):
super(PublishingSetTests, self).setUp()
self.distroseries = self.factory.makeDistroSeries()
self.archive = self.factory.makeArchive(
distribution=self.distroseries.distribution)
self.publishing = self.factory.makeSourcePackagePublishingHistory(
distroseries=self.distroseries, archive=self.archive)
self.publishing_set = getUtility(IPublishingSet)
def test_getByIdAndArchive_finds_record(self):
record = self.publishing_set.getByIdAndArchive(
self.publishing.id, self.archive)
self.assertEqual(self.publishing, record)
def test_getByIdAndArchive_finds_record_explicit_source(self):
record = self.publishing_set.getByIdAndArchive(
self.publishing.id, self.archive, source=True)
self.assertEqual(self.publishing, record)
def test_getByIdAndArchive_wrong_archive(self):
wrong_archive = self.factory.makeArchive()
record = self.publishing_set.getByIdAndArchive(
self.publishing.id, wrong_archive)
self.assertEqual(None, record)
def makeBinaryPublishing(self):
distroarchseries = self.factory.makeDistroArchSeries(
distroseries=self.distroseries)
binary_publishing = self.factory.makeBinaryPackagePublishingHistory(
archive=self.archive, distroarchseries=distroarchseries)
return binary_publishing
def test_getByIdAndArchive_wrong_type(self):
self.makeBinaryPublishing()
record = self.publishing_set.getByIdAndArchive(
self.publishing.id, self.archive, source=False)
self.assertEqual(None, record)
def test_getByIdAndArchive_finds_binary(self):
binary_publishing = self.makeBinaryPublishing()
record = self.publishing_set.getByIdAndArchive(
binary_publishing.id, self.archive, source=False)
self.assertEqual(binary_publishing, record)
def test_getByIdAndArchive_binary_wrong_archive(self):
binary_publishing = self.makeBinaryPublishing()
wrong_archive = self.factory.makeArchive()
record = self.publishing_set.getByIdAndArchive(
binary_publishing.id, wrong_archive, source=False)
self.assertEqual(None, record)
class TestSourceDomination(TestNativePublishingBase):
"""Test SourcePackagePublishingHistory.supersede() operates correctly."""
def testSupersede(self):
"""Check that supersede() without arguments works."""
source = self.getPubSource()
source.supersede()
self.checkSuperseded([source])
def testSupersedeWithDominant(self):
"""Check that supersede() with a dominant publication works."""
source = self.getPubSource()
super_source = self.getPubSource()
source.supersede(super_source)
self.checkSuperseded([source], super_source)
def testSupersedingSupersededSourceFails(self):
"""Check that supersede() fails with a superseded source.
Sources should not be superseded twice. If a second attempt is made,
the Dominator's lookups are buggy.
"""
source = self.getPubSource()
super_source = self.getPubSource()
source.supersede(super_source)
self.checkSuperseded([source], super_source)
# Manually set a date in the past, so we can confirm that
# the second supersede() fails properly.
source.datesuperseded = datetime.datetime(
2006, 12, 25, tzinfo=pytz.timezone("UTC"))
super_date = source.datesuperseded
self.assertRaises(AssertionError, source.supersede, super_source)
self.checkSuperseded([source], super_source)
self.assertEquals(super_date, source.datesuperseded)
class TestBinaryDomination(TestNativePublishingBase):
"""Test BinaryPackagePublishingHistory.supersede() operates correctly."""
def testSupersede(self):
"""Check that supersede() without arguments works."""
bins = self.getPubBinaries(architecturespecific=True)
bins[0].supersede()
self.checkSuperseded([bins[0]])
self.checkPublication(bins[1], PackagePublishingStatus.PENDING)
def testSupersedeWithDominant(self):
"""Check that supersede() with a dominant publication works."""
bins = self.getPubBinaries(architecturespecific=True)
super_bins = self.getPubBinaries(architecturespecific=True)
bins[0].supersede(super_bins[0])
self.checkSuperseded([bins[0]], super_bins[0])
self.checkPublication(bins[1], PackagePublishingStatus.PENDING)
def testSupersedesArchIndepBinariesAtomically(self):
"""Check that supersede() supersedes arch-indep binaries atomically.
Architecture-independent binaries should be removed from all
architectures when they are superseded on at least one (bug #48760).
"""
bins = self.getPubBinaries(architecturespecific=False)
super_bins = self.getPubBinaries(architecturespecific=False)
bins[0].supersede(super_bins[0])
self.checkSuperseded(bins, super_bins[0])
def testAtomicDominationRespectsOverrides(self):
"""Check that atomic domination only covers identical overrides.
This is important, as otherwise newly-overridden arch-indep binaries
will supersede themselves, and vanish entirely (bug #178102).
"""
bins = self.getPubBinaries(architecturespecific=False)
universe = getUtility(IComponentSet)['universe']
super_bins = []
for bin in bins:
super_bins.append(bin.changeOverride(new_component=universe))
bins[0].supersede(super_bins[0])
self.checkSuperseded(bins, super_bins[0])
self.checkPublications(super_bins, PackagePublishingStatus.PENDING)
def testSupersedingSupersededArchSpecificBinaryFails(self):
"""Check that supersede() fails with a superseded arch-dep binary.
Architecture-specific binaries should not normally be superseded
twice. If a second attempt is made, the Dominator's lookups are buggy.
"""
bin = self.getPubBinaries(architecturespecific=True)[0]
super_bin = self.getPubBinaries(architecturespecific=True)[0]
bin.supersede(super_bin)
# Manually set a date in the past, so we can confirm that
# the second supersede() fails properly.
bin.datesuperseded = datetime.datetime(
2006, 12, 25, tzinfo=pytz.timezone("UTC"))
super_date = bin.datesuperseded
self.assertRaises(AssertionError, bin.supersede, super_bin)
self.checkSuperseded([bin], super_bin)
self.assertEquals(super_date, bin.datesuperseded)
def testSkipsSupersededArchIndependentBinary(self):
"""Check that supersede() skips a superseded arch-indep binary.
Since all publications of an architecture-independent binary are
superseded atomically, they may be superseded again later. In that
case, we skip the domination, leaving the old date unchanged.
"""
bin = self.getPubBinaries(architecturespecific=False)[0]
super_bin = self.getPubBinaries(architecturespecific=False)[0]
bin.supersede(super_bin)
self.checkSuperseded([bin], super_bin)
# Manually set a date in the past, so we can confirm that
# the second supersede() skips properly.
bin.datesuperseded = datetime.datetime(
2006, 12, 25, tzinfo=pytz.timezone("UTC"))
super_date = bin.datesuperseded
bin.supersede(super_bin)
self.checkSuperseded([bin], super_bin)
self.assertEquals(super_date, bin.datesuperseded)
class TestBinaryGetOtherPublications(TestNativePublishingBase):
"""Test BinaryPackagePublishingHistory._getOtherPublications() works."""
def checkOtherPublications(self, this, others):
self.assertEquals(
set(removeSecurityProxy(this)._getOtherPublications()),
set(others))
def testFindsOtherArchIndepPublications(self):
"""Arch-indep publications with the same overrides should be found."""
bins = self.getPubBinaries(architecturespecific=False)
self.checkOtherPublications(bins[0], bins)
def testDoesntFindArchSpecificPublications(self):
"""Arch-dep publications shouldn't be found."""
bins = self.getPubBinaries(architecturespecific=True)
self.checkOtherPublications(bins[0], [bins[0]])
def testDoesntFindPublicationsInOtherArchives(self):
"""Publications in other archives shouldn't be found."""
bins = self.getPubBinaries(architecturespecific=False)
foreign_bins = bins[0].copyTo(
bins[0].distroarchseries.distroseries, bins[0].pocket,
self.factory.makeArchive())
self.checkOtherPublications(bins[0], bins)
self.checkOtherPublications(foreign_bins[0], foreign_bins)
def testDoesntFindPublicationsWithDifferentOverrides(self):
"""Publications with different overrides shouldn't be found."""
bins = self.getPubBinaries(architecturespecific=False)
universe = getUtility(IComponentSet)['universe']
foreign_bin = bins[0].changeOverride(new_component=universe)
self.checkOtherPublications(bins[0], bins)
self.checkOtherPublications(foreign_bin, [foreign_bin])
def testDoesntFindSupersededPublications(self):
"""Superseded publications shouldn't be found."""
bins = self.getPubBinaries(architecturespecific=False)
self.checkOtherPublications(bins[0], bins)
# This will supersede both atomically.
bins[0].supersede()
self.checkOtherPublications(bins[0], [])
|