~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
# Copyright 2009 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

__metaclass__ = type

from zope.component import getUtility
from zope.security.proxy import removeSecurityProxy

from canonical.testing.layers import DatabaseFunctionalLayer
from lp.buildmaster.enums import BuildStatus
from lp.registry.interfaces.pocket import PackagePublishingPocket
from lp.soyuz.adapters.packagelocation import PackageLocation
from lp.soyuz.enums import (
    ArchivePurpose,
    PackagePublishingStatus,
    )
from lp.soyuz.interfaces.binarypackagebuild import IBinaryPackageBuildSet
from lp.soyuz.model.copyarchivejob import CopyArchiveJob
from lp.soyuz.model.processor import ProcessorFamilySet
from lp.testing import (
    celebrity_logged_in,
    TestCaseWithFactory,
    )


class CopyArchiveJobTests(TestCaseWithFactory):
    """Tests for CopyArchiveJob."""

    layer = DatabaseFunctionalLayer

    def test_getOopsVars(self):
        archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        target_distroseries = self.factory.makeDistroSeries()
        source_pocket = PackagePublishingPocket.RELEASE
        target_pocket = PackagePublishingPocket.BACKPORTS
        target_component = self.factory.makeComponent()
        job = CopyArchiveJob.create(
            archive, args['source_archive'], args['distroseries'],
            source_pocket, target_distroseries, target_pocket,
            target_component=target_component)
        vars = job.getOopsVars()
        self.assertIn(('archive_id', archive.id), vars)
        self.assertIn(('archive_job_id', job.context.id), vars)
        self.assertIn(('archive_job_type', job.context.job_type.title), vars)
        self.assertIn(('source_archive_id', args['source_archive'].id), vars)
        self.assertIn(
            ('source_distroseries_id', args['distroseries'].id), vars)
        self.assertIn(
            ('target_distroseries_id', target_distroseries.id), vars)
        self.assertIn(('source_pocket_value', source_pocket.value), vars)
        self.assertIn(('target_pocket_value', target_pocket.value), vars)
        self.assertIn(
            ('target_component_id', target_component.id), vars)
        self.assertIn(('merge', False), vars)

    def makeDummyArgs(self):
        args = {}
        distro = self.factory.makeDistribution()
        args['distroseries'] = self.factory.makeDistroSeries(
            distribution=distro)
        args['pocket'] = self.factory.getAnyPocket()
        args['source_archive'] = self.factory.makeArchive(
            distribution=distro)
        return args

    def test_error_if_already_exists(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            args['pocket'], args['distroseries'], args['pocket'])
        self.assertEqual(1, self._getJobCount())
        args = self.makeDummyArgs()
        self.assertRaises(
            ValueError, CopyArchiveJob.create, target_archive,
            args['source_archive'], args['distroseries'], args['pocket'],
            args['distroseries'], args['pocket'])

    def test_create_sets_source_archive_id(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        source_archive = self.factory.makeArchive()
        job = CopyArchiveJob.create(
            target_archive, source_archive, args['distroseries'],
            args['pocket'], args['distroseries'], args['pocket'])
        self.assertEqual(
            source_archive.id, job.metadata['source_archive_id'])

    def test_create_sets_source_series_id(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        source_distroseries = self.factory.makeDistroSeries()
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], source_distroseries,
            args['pocket'], args['distroseries'], args['pocket'])
        self.assertEqual(
            source_distroseries.id, job.metadata['source_distroseries_id'])

    def test_create_sets_source_pocket_value(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        source_pocket = PackagePublishingPocket.RELEASE
        target_pocket = PackagePublishingPocket.BACKPORTS
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            source_pocket, args['distroseries'], target_pocket)
        self.assertEqual(
            source_pocket.value, job.metadata['source_pocket_value'])

    def test_create_sets_target_pocket_value(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        source_pocket = PackagePublishingPocket.RELEASE
        target_pocket = PackagePublishingPocket.BACKPORTS
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            source_pocket, args['distroseries'], target_pocket)
        self.assertEqual(
            target_pocket.value, job.metadata['target_pocket_value'])

    def test_create_sets_target_distroseries_id(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        target_distroseries = self.factory.makeDistroSeries()
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            args['pocket'], target_distroseries, args['pocket'])
        self.assertEqual(
            target_distroseries.id, job.metadata['target_distroseries_id'])

    def test_create_sets_target_component_id(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        target_component = self.factory.makeComponent()
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            args['pocket'], args['distroseries'], args['pocket'],
            target_component=target_component)
        self.assertEqual(
            target_component.id, job.metadata['target_component_id'])

    def test_create_sets_target_component_id_to_None_if_unspecified(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            args['pocket'], args['distroseries'], args['pocket'])
        self.assertEqual(None, job.metadata['target_component_id'])

    def test_create_sets_proc_family_ids(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        family1 = self.factory.makeProcessorFamily(name="armel")
        family2 = self.factory.makeProcessorFamily(name="ia64")
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            args['pocket'], args['distroseries'], args['pocket'],
            proc_families=[family1, family2])
        self.assertEqual(
            [f.name for f in [family1, family2]],
            job.metadata['proc_family_names'])

    def test_error_on_merge_with_proc_families(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        family1 = self.factory.makeProcessorFamily(name="armel")
        family2 = self.factory.makeProcessorFamily(name="ia64")
        self.assertRaises(
            ValueError, CopyArchiveJob.create, target_archive,
            args['source_archive'], args['distroseries'], args['pocket'],
            args['distroseries'], args['pocket'],
            proc_families=[family1, family2], merge=True)

    def test_create_sets_source_package_set_ids(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        packagesets = [
            self.factory.makePackageset(),
            self.factory.makePackageset(),
        ]
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            args['pocket'], args['distroseries'], args['pocket'],
            packagesets=packagesets)
        self.assertEqual(
            [p.name for p in packagesets], job.metadata['packageset_names'])

    def test_create_sets_merge_False_by_default(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            args['pocket'], args['distroseries'], args['pocket'])
        self.assertEqual(False, job.metadata['merge'])

    def test_create_sets_merge_True_on_request(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            args['pocket'], args['distroseries'], args['pocket'], merge=True)
        self.assertEqual(True, job.metadata['merge'])

    def test_get_source_location(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        source_distroseries = self.factory.makeDistroSeries()
        source_pocket = PackagePublishingPocket.RELEASE
        target_pocket = PackagePublishingPocket.BACKPORTS
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], source_distroseries,
            source_pocket, args['distroseries'], target_pocket)
        location = job.getSourceLocation()
        expected_location = PackageLocation(
            args['source_archive'], source_distroseries.distribution,
            source_distroseries, source_pocket)
        self.assertEqual(expected_location, location)

    def test_get_source_location_with_packagesets(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        source_distroseries = self.factory.makeDistroSeries()
        source_pocket = PackagePublishingPocket.RELEASE
        target_pocket = PackagePublishingPocket.BACKPORTS
        packagesets = [
            self.factory.makePackageset(),
            self.factory.makePackageset(),
        ]
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], source_distroseries,
            source_pocket, args['distroseries'], target_pocket,
            packagesets=packagesets)
        location = job.getSourceLocation()
        expected_location = PackageLocation(
            args['source_archive'], source_distroseries.distribution,
            source_distroseries, source_pocket, packagesets=packagesets)
        self.assertEqual(expected_location, location)

    def test_get_target_location(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        target_distroseries = self.factory.makeDistroSeries()
        source_pocket = PackagePublishingPocket.RELEASE
        target_pocket = PackagePublishingPocket.BACKPORTS
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            source_pocket, target_distroseries, target_pocket)
        location = job.getTargetLocation()
        expected_location = PackageLocation(
            target_archive, target_distroseries.distribution,
            target_distroseries, target_pocket)
        self.assertEqual(expected_location, location)

    def test_get_target_location_with_component(self):
        target_archive = self.factory.makeArchive()
        args = self.makeDummyArgs()
        target_distroseries = self.factory.makeDistroSeries()
        source_pocket = PackagePublishingPocket.RELEASE
        target_pocket = PackagePublishingPocket.BACKPORTS
        target_component = self.factory.makeComponent()
        job = CopyArchiveJob.create(
            target_archive, args['source_archive'], args['distroseries'],
            source_pocket, target_distroseries, target_pocket,
            target_component=target_component)
        location = job.getTargetLocation()
        expected_location = PackageLocation(
            target_archive, target_distroseries.distribution,
            target_distroseries, target_pocket)
        expected_location.component = target_component
        self.assertEqual(expected_location, location)

    def _getJobs(self):
        """Return the pending CopyArchiveJobs as a list."""
        return list(CopyArchiveJob.iterReady())

    def _getJobCount(self):
        """Return the number of CopyArchiveJobs in the queue."""
        return len(self._getJobs())

    def makeSourceAndTarget(self):
        distribution = self.factory.makeDistribution(name="foobuntu")
        distroseries = self.factory.makeDistroSeries(
            distribution=distribution, name="maudlin")
        source_archive_owner = self.factory.makePerson(name="source-owner")
        source_archive = self.factory.makeArchive(
            name="source", owner=source_archive_owner,
            purpose=ArchivePurpose.PPA, distribution=distribution)
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.factory.getOrMakeSourcePackageName(
                name='bzr'),
            distroseries=distroseries, component=self.factory.makeComponent(),
            version="2.1", architecturehintlist='any',
            archive=source_archive, status=PackagePublishingStatus.PUBLISHED,
            pocket=PackagePublishingPocket.RELEASE)
        das = self.factory.makeDistroArchSeries(
            distroseries=distroseries, architecturetag="i386",
            processorfamily=ProcessorFamilySet().getByName("x86"),
            supports_virtualized=True)
        with celebrity_logged_in('admin'):
            distroseries.nominatedarchindep = das
        target_archive_owner = self.factory.makePerson()
        target_archive = self.factory.makeArchive(
            purpose=ArchivePurpose.COPY, owner=target_archive_owner,
            name="test-copy-archive", distribution=distribution,
            description="Test copy archive", enabled=False)
        return source_archive, target_archive, distroseries

    def checkPublishedSources(self, expected, archive, series):
        # We need to be admin as the archive is disabled at this point.
        with celebrity_logged_in('admin'):
            sources = archive.getPublishedSources(
                distroseries=series,
                status=(
                    PackagePublishingStatus.PENDING,
                    PackagePublishingStatus.PUBLISHED))
            actual = []
            for source in sources:
                actual.append(
                    (source.source_package_name,
                     source.source_package_version))
            self.assertEqual(sorted(expected), sorted(actual))

    def test_run(self):
        """Test that CopyArchiveJob.run() actually copies the archive.

        We just make a simple test here, and rely on PackageCloner tests
        to cover the functionality.
        """
        source_archive, target_archive, series = self.makeSourceAndTarget()
        job = CopyArchiveJob.create(
            target_archive, source_archive, series,
            PackagePublishingPocket.RELEASE, series,
            PackagePublishingPocket.RELEASE)
        job.run()
        self.checkPublishedSources([("bzr", "2.1")], target_archive, series)

    def test_run_mergeCopy(self):
        """Test that CopyArchiveJob.run() when merge=True does a mergeCopy."""
        source_archive, target_archive, series = self.makeSourceAndTarget()
        # Create the copy archive
        job = CopyArchiveJob.create(
            target_archive, source_archive, series,
            PackagePublishingPocket.RELEASE, series,
            PackagePublishingPocket.RELEASE)
        job.start()
        job.run()
        job.complete()
        # Now the two archives are in the same state, so we change the
        # source archive and request a merge to check that it works.
        # Create a new version of the apt package in the source
        self.factory.makeSourcePackagePublishingHistory(
            sourcepackagename=self.factory.getOrMakeSourcePackageName(
                name='apt'),
            distroseries=series, component=self.factory.makeComponent(),
            version="1.2", architecturehintlist='any',
            archive=source_archive, status=PackagePublishingStatus.PUBLISHED,
            pocket=PackagePublishingPocket.RELEASE)
        # Create a job to merge
        job = CopyArchiveJob.create(
            target_archive, source_archive, series,
            PackagePublishingPocket.RELEASE, series,
            PackagePublishingPocket.RELEASE, merge=True)
        job.run()
        # Check that the new apt package is in the target
        self.checkPublishedSources(
            [("bzr", "2.1"), ("apt", "1.2")], target_archive, series)

    def test_run_with_proc_families(self):
        """Test that a CopyArchiveJob job with proc_families uses them.

        If we create a CopyArchiveJob with proc_families != None then
        they should be used when cloning packages.
        """
        source_archive, target_archive, series = self.makeSourceAndTarget()
        proc_families = [ProcessorFamilySet().getByName("x86")]
        job = CopyArchiveJob.create(
            target_archive, source_archive, series,
            PackagePublishingPocket.RELEASE, series,
            PackagePublishingPocket.RELEASE, proc_families=proc_families)
        job.run()
        builds = list(
            getUtility(IBinaryPackageBuildSet).getBuildsForArchive(
            target_archive, status=BuildStatus.NEEDSBUILD))
        actual_builds = list()
        for build in builds:
            naked_build = removeSecurityProxy(build)
            spr = naked_build.source_package_release
            actual_builds.append(
                (spr.name, spr.version, naked_build.processor.family.name))
        # One build for the one package, as we specified one processor
        # family.
        self.assertEqual([("bzr", "2.1", "x86")], actual_builds)