~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/archiveuploader/tests/test_uploadprocessor.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-18 13:47:37 UTC
  • mfrom: (13697.5.4 missing-test-bug-793607)
  • Revision ID: launchpad@pqm.canonical.com-20110818134737-izfe81407gzutbs3
[r=rvb,
        stevenk] [r=stevenk][bug=793607] Run tests in test_uploadprocessor.py
        with user 'uploader'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
import tempfile
17
17
 
18
18
from storm.locals import Store
 
19
import transaction
19
20
from zope.component import (
20
21
    getGlobalSiteManager,
21
22
    getUtility,
53
54
from lp.registry.interfaces.sourcepackage import SourcePackageFileType
54
55
from lp.registry.interfaces.sourcepackagename import ISourcePackageNameSet
55
56
from lp.registry.model.sourcepackagename import SourcePackageName
 
57
from lp.services.features.testing import FeatureFixture
56
58
from lp.services.log.logger import BufferLogger
57
59
from lp.services.mail import stub
58
60
from lp.soyuz.enums import (
77
79
from lp.soyuz.model.binarypackagename import BinaryPackageName
78
80
from lp.soyuz.model.binarypackagerelease import BinaryPackageRelease
79
81
from lp.soyuz.model.component import Component
 
82
from lp.soyuz.model.distroseriesdifferencejob import (
 
83
    FEATURE_FLAG_ENABLE_MODULE,
 
84
    )
80
85
from lp.soyuz.model.publishing import (
81
86
    BinaryPackagePublishingHistory,
82
87
    SourcePackagePublishingHistory,
114
119
    """Base class for functional tests over uploadprocessor.py."""
115
120
    layer = LaunchpadZopelessLayer
116
121
 
 
122
    def switchToUploader(self):
 
123
        transaction.commit()
 
124
        self.layer.switchDbUser("uploader")
 
125
 
 
126
    def switchToAdmin(self):
 
127
        transaction.commit()
 
128
        self.layer.switchDbUser("launchpad_main")
 
129
 
117
130
    def setUp(self):
118
131
        super(TestUploadProcessorBase, self).setUp()
119
132
 
142
155
 
143
156
        self.log = BufferLogger()
144
157
 
 
158
        self.switchToAdmin()
 
159
        self.useFixture(FeatureFixture({
 
160
            FEATURE_FLAG_ENABLE_MODULE: u'on',
 
161
        }))
 
162
        self.switchToUploader()
 
163
 
145
164
    def tearDown(self):
146
165
        shutil.rmtree(self.queue_folder)
 
166
        self.switchToAdmin()
147
167
        super(TestUploadProcessorBase, self).tearDown()
148
168
 
149
169
    def getUploadProcessor(self, txn, builds=None):
 
170
        self.switchToAdmin()
150
171
        if builds is None:
151
172
            builds = self.options.builds
152
173
 
160
181
            policy.setOptions(self.options)
161
182
            return policy
162
183
 
163
 
        return UploadProcessor(
 
184
        upload_processor = UploadProcessor(
164
185
            self.options.base_fsroot, self.options.dryrun,
165
186
            self.options.nomails, builds, self.options.keep, getPolicy, txn,
166
187
            self.log)
 
188
        self.switchToUploader()
 
189
        return upload_processor
167
190
 
168
191
    def publishPackage(self, packagename, version, source=True, archive=None):
169
192
        """Publish a single package that is currently NEW in the queue."""
 
193
        self.switchToAdmin()
 
194
 
170
195
        packagename = unicode(packagename)
171
196
        if version is not None:
172
197
            version = unicode(version)
180
205
            pubrec = queue_item.sources[0].publish(self.log)
181
206
        else:
182
207
            pubrec = queue_item.builds[0].publish(self.log)
 
208
        self.switchToUploader()
183
209
        return pubrec
184
210
 
185
211
    def assertLogContains(self, line):
222
248
        :param permitted_formats: list of SourcePackageFormats to allow
223
249
            in the new distroseries. Only permits '1.0' by default.
224
250
        """
 
251
        self.switchToAdmin()
 
252
 
225
253
        self.ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
226
254
        bat = self.ubuntu['breezy-autotest']
227
255
        self.breezy = self.ubuntu.newSeries(
245
273
                getUtility(ISourcePackageFormatSelectionSet).add(
246
274
                    self.breezy, format)
247
275
 
 
276
        self.switchToUploader()
 
277
 
248
278
    def addMockFile(self, filename, content="anything"):
249
279
        """Return a librarian file."""
250
280
        return getUtility(ILibraryFileAliasSet).create(
295
325
    def setupBreezyAndGetUploadProcessor(self, policy=None):
296
326
        """Setup Breezy and return an upload processor for it."""
297
327
        self.setupBreezy()
 
328
        self.switchToAdmin()
298
329
        self.layer.txn.commit()
299
330
        if policy is not None:
300
331
            self.options.context = policy
301
 
        return self.getUploadProcessor(self.layer.txn)
 
332
        upload_processor = self.getUploadProcessor(self.layer.txn)
 
333
        self.switchToUploader()
 
334
        return upload_processor
302
335
 
303
336
    def assertEmail(self, contents=None, recipients=None):
304
337
        """Check last email content and recipients.
592
625
        # Accept and publish the upload.
593
626
        # This is required so that the next upload of a later version of
594
627
        # the same package will work correctly.
 
628
        self.switchToAdmin()
595
629
        queue_items = self.breezy.getPackageUploads(
596
630
            status=PackageUploadStatus.NEW, name=u"bar",
597
631
            version=u"1.0-1", exact_match=True)
607
641
        # existing package will be allowed, but unapproved.
608
642
        self.breezy.status = SeriesStatus.FROZEN
609
643
        self.layer.txn.commit()
 
644
        self.switchToUploader()
610
645
 
611
646
        # Upload a newer version of bar.
612
647
        upload_dir = self.queueUpload("bar_1.0-2")
672
707
        bar_bin_pubs = self.publishPackage('bar', '1.0-1', source=False)
673
708
        # Mangle its publishing component to "restricted" so we can check
674
709
        # the copy archive ancestry override later.
 
710
        self.switchToAdmin()
675
711
        restricted = getUtility(IComponentSet)["restricted"]
676
712
        for pub in bar_bin_pubs:
677
713
            pub.component = restricted
 
714
        self.switchToUploader()
678
715
 
679
716
        # Create a COPY archive for building in non-virtual builds.
680
717
        uploader = getUtility(IPersonSet).getByName('name16')
716
753
 
717
754
        # The copy archive binary published component should have been
718
755
        # inherited from the main archive's.
 
756
        self.switchToAdmin()
719
757
        copy_bin_pubs = queue_items[0].realiseUpload()
720
758
        for pub in copy_bin_pubs:
721
759
            self.assertEqual(pub.component.name, restricted.name)
 
760
        self.switchToUploader()
722
761
 
723
762
    def testCopyArchiveUploadToCurrentDistro(self):
724
763
        """Check binary copy archive uploads to RELEASE pockets.
774
813
        [bar_binary_pub] = self.publishPackage("bar", "1.0-1", source=False)
775
814
 
776
815
        # Prepare ubuntu/breezy-autotest to build sources in i386.
 
816
        self.switchToAdmin()
777
817
        breezy_autotest = self.ubuntu['breezy-autotest']
778
818
        breezy_autotest_i386 = breezy_autotest['i386']
779
819
        breezy_autotest.nominatedarchindep = breezy_autotest_i386
780
820
        fake_chroot = self.addMockFile('fake_chroot.tar.gz')
781
821
        breezy_autotest_i386.addOrUpdateChroot(fake_chroot)
782
822
        self.layer.txn.commit()
 
823
        self.switchToUploader()
783
824
 
784
825
        # Copy 'bar-1.0-1' source from breezy to breezy-autotest and
785
826
        # create a build there (this would never happen in reality, it
1195
1236
        self.setupBreezy()
1196
1237
 
1197
1238
        # Check unstable states:
1198
 
 
1199
1239
        self.breezy.status = SeriesStatus.DEVELOPMENT
1200
1240
        self.layer.txn.commit()
1201
1241
        self._uploadPartnerToNonReleasePocketAndCheckFail()
1456
1496
        # permissions to all components at upload time.
1457
1497
        uploader = getUtility(IPersonSet).getByName('name16')
1458
1498
        distro_team = getUtility(IPersonSet).getByName('ubuntu-team')
 
1499
        self.switchToAdmin()
1459
1500
        uploader.leave(distro_team)
1460
1501
 
1461
1502
        # Now give name16 specific permissions to "restricted" only.
1464
1505
            archive=self.ubuntu.main_archive,
1465
1506
            permission=ArchivePermissionType.UPLOAD, person=uploader,
1466
1507
            component=restricted)
 
1508
        self.switchToUploader()
1467
1509
 
1468
1510
        uploadprocessor = self.getUploadProcessor(self.layer.txn)
1469
1511
 
1486
1528
            u"Signer is not permitted to upload to the component 'universe'.")
1487
1529
 
1488
1530
        # Now add permission to upload "bar" for name16.
 
1531
        self.switchToAdmin()
1489
1532
        bar_package = getUtility(ISourcePackageNameSet).queryByName("bar")
1490
1533
        ArchivePermission(
1491
1534
            archive=self.ubuntu.main_archive,
1492
1535
            permission=ArchivePermissionType.UPLOAD, person=uploader,
1493
1536
            sourcepackagename=bar_package)
 
1537
        self.switchToUploader()
1494
1538
 
1495
1539
        # Upload the package again.
1496
1540
        self.processUpload(uploadprocessor, upload_dir)
1508
1552
        # permissions to all components at upload time.
1509
1553
        uploader = getUtility(IPersonSet).getByName('name16')
1510
1554
        distro_team = getUtility(IPersonSet).getByName('ubuntu-team')
 
1555
        self.switchToAdmin()
1511
1556
        uploader.leave(distro_team)
1512
1557
 
1513
1558
        # Now give name16 specific permissions to "restricted" only.
1516
1561
            archive=self.ubuntu.main_archive,
1517
1562
            permission=ArchivePermissionType.UPLOAD, person=uploader,
1518
1563
            component=restricted)
 
1564
        self.switchToUploader()
1519
1565
 
1520
1566
        uploadprocessor = self.getUploadProcessor(self.layer.txn)
1521
1567
 
1539
1585
 
1540
1586
        # Now put in place a package set, add 'bar' to it and define a
1541
1587
        # permission for the former.
 
1588
        self.switchToAdmin()
1542
1589
        bar_package = getUtility(ISourcePackageNameSet).queryByName("bar")
1543
1590
        ap_set = getUtility(IArchivePermissionSet)
1544
1591
        ps_set = getUtility(IPackagesetSet)
1550
1597
        foo_ps.add((bar_package, ))
1551
1598
        ap_set.newPackagesetUploader(
1552
1599
            self.ubuntu.main_archive, uploader, foo_ps)
 
1600
        self.switchToUploader()
1553
1601
 
1554
1602
        # The uploader now does have a package set based upload permissions
1555
1603
        # to 'bar' in 'grumpy' but not in 'breezy'.
1571
1619
            msg['Subject'], 'bar_1.0-2_source.changes rejected')
1572
1620
 
1573
1621
        # Grant the permissions in the proper series.
 
1622
        self.switchToAdmin()
1574
1623
        breezy_ps = ps_set.new(
1575
1624
            u'foo-pkg-set-breezy', u'Packages that require special care.',
1576
1625
            uploader, distroseries=self.breezy)
1577
1626
        breezy_ps.add((bar_package, ))
1578
1627
        ap_set.newPackagesetUploader(
1579
1628
            self.ubuntu.main_archive, uploader, breezy_ps)
 
1629
        self.switchToUploader()
1580
1630
        # The uploader now does have a package set based upload permission
1581
1631
        # to 'bar' in 'breezy'.
1582
1632
        self.assertTrue(
1817
1867
            status=PackageUploadStatus.NEW, name=u"bar",
1818
1868
            version=u"1.0-1", exact_match=True)
1819
1869
        self.assertEqual(queue_items.count(), 1)
 
1870
        self.switchToAdmin()
1820
1871
        queue_item = queue_items[0]
1821
1872
        queue_item.setAccepted()
1822
1873
        pubrec = queue_item.sources[0].publish(self.log)
1824
1875
        pubrec.datepublished = UTC_NOW
1825
1876
        queue_item.setDone()
1826
1877
        self.PGPSignatureNotPreserved(archive=self.breezy.main_archive)
 
1878
        self.switchToUploader()
1827
1879
 
1828
1880
 
1829
1881
class TestBuildUploadProcessor(TestUploadProcessorBase):
1873
1925
        [build] = source_pub.createMissingBuilds()
1874
1926
 
1875
1927
        # Move the source from the accepted queue.
 
1928
        self.switchToAdmin()
1876
1929
        [queue_item] = self.breezy.getPackageUploads(
1877
1930
            status=PackageUploadStatus.ACCEPTED,
1878
1931
            version=u"1.0-1", name=u"bar")
1884
1937
 
1885
1938
        build.status = BuildStatus.UPLOADING
1886
1939
        build.date_finished = UTC_NOW
 
1940
        self.switchToUploader()
1887
1941
 
1888
1942
        # Upload and accept a binary for the primary archive source.
1889
1943
        shutil.rmtree(upload_dir)
1917
1971
        [build] = source_pub.createMissingBuilds()
1918
1972
 
1919
1973
        # Move the source from the accepted queue.
 
1974
        self.switchToAdmin()
1920
1975
        [queue_item] = self.breezy.getPackageUploads(
1921
1976
            status=PackageUploadStatus.ACCEPTED,
1922
1977
            version=u"1.0-1", name=u"bar")
1923
1978
        queue_item.setDone()
1924
1979
 
1925
1980
        build.buildqueue_record.markAsBuilding(self.factory.makeBuilder())
1926
 
 
1927
1981
        build.status = BuildStatus.UPLOADING
 
1982
        self.switchToUploader()
1928
1983
 
1929
1984
        # Upload and accept a binary for the primary archive source.
1930
1985
        shutil.rmtree(upload_dir)
1948
2003
 
1949
2004
    def doSuccessRecipeBuild(self):
1950
2005
        # Upload a source package
 
2006
        self.switchToAdmin()
1951
2007
        archive = self.factory.makeArchive()
1952
2008
        archive.require_virtualized = False
1953
2009
        build = self.factory.makeSourcePackageRecipeBuild(sourcename=u"bar",
1955
2011
            requester=archive.owner)
1956
2012
        self.assertEquals(archive.owner, build.requester)
1957
2013
        self.factory.makeSourcePackageRecipeBuildJob(recipe_build=build)
 
2014
        self.switchToUploader()
1958
2015
        # Commit so the build cookie has the right ids.
1959
2016
        self.layer.txn.commit()
1960
2017
        leaf_name = build.getUploadDirLeaf(build.getBuildCookie())
1998
2055
        # A source package recipe build will fail if no files are present.
1999
2056
 
2000
2057
        # Upload a source package
 
2058
        self.switchToAdmin()
2001
2059
        archive = self.factory.makeArchive()
2002
2060
        archive.require_virtualized = False
2003
2061
        build = self.factory.makeSourcePackageRecipeBuild(sourcename=u"bar",
2010
2068
        self.options.context = 'buildd'
2011
2069
        self.options.builds = True
2012
2070
        build.jobStarted()
 
2071
        self.switchToUploader()
2013
2072
        # Commit so date_started is recorded and doesn't cause constraint
2014
2073
        # violations later.
2015
2074
        Store.of(build).flush()
2040
2099
        # A source package recipe build will fail if the recipe is deleted.
2041
2100
 
2042
2101
        # Upload a source package
 
2102
        self.switchToAdmin()
2043
2103
        archive = self.factory.makeArchive()
2044
2104
        archive.require_virtualized = False
2045
2105
        build = self.factory.makeSourcePackageRecipeBuild(sourcename=u"bar",
2046
2106
            distroseries=self.breezy, archive=archive)
2047
2107
        self.factory.makeSourcePackageRecipeBuildJob(recipe_build=build)
 
2108
        self.switchToUploader()
2048
2109
        # Commit so the build cookie has the right ids.
2049
2110
        Store.of(build).flush()
2050
2111
        leaf_name = build.getUploadDirLeaf(build.getBuildCookie())
2052
2113
        self.options.context = 'buildd'
2053
2114
        self.options.builds = True
2054
2115
        build.jobStarted()
 
2116
        self.switchToAdmin()
2055
2117
        build.recipe.destroySelf()
 
2118
        self.switchToUploader()
2056
2119
        # Commit so date_started is recorded and doesn't cause constraint
2057
2120
        # violations later.
2058
2121
        Store.of(build).flush()
2079
2142
        [build] = source_pub.createMissingBuilds()
2080
2143
 
2081
2144
        # Move the source from the accepted queue.
 
2145
        self.switchToAdmin()
2082
2146
        [queue_item] = self.breezy.getPackageUploads(
2083
2147
            status=PackageUploadStatus.ACCEPTED,
2084
2148
            version=u"1.0-1", name=u"bar")
2086
2150
 
2087
2151
        build.buildqueue_record.markAsBuilding(self.factory.makeBuilder())
2088
2152
        build.status = BuildStatus.BUILDING
 
2153
        self.switchToUploader()
2089
2154
 
2090
2155
        shutil.rmtree(upload_dir)
2091
2156