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

"""Runs the doctests for archiveuploader module."""

__metaclass__ = type

import os
import unittest

from zope.component import getUtility

from lp.archiveuploader.nascentupload import NascentUpload
from lp.archiveuploader.tests import (
    datadir,
    getPolicy,
    )
from lp.archiveuploader.uploadpolicy import ArchiveUploadType
from lp.registry.interfaces.distribution import IDistributionSet
from lp.services.librarian.model import LibraryFileAlias
from lp.services.log.logger import DevNullLogger
from lp.soyuz.interfaces.component import IComponentSet
from lp.soyuz.model.component import ComponentSelection
from lp.testing import (
    login,
    logout,
    )
from lp.testing.gpgkeys import import_public_test_keys
from lp.testing.layers import LaunchpadZopelessLayer
from lp.testing.systemdocs import (
    LayeredDocFileSuite,
    setGlobs,
    )


def getUploadForSource(upload_path):
    """Return a NascentUpload object for a source."""
    policy = getPolicy(name='sync', distro='ubuntu', distroseries='hoary')
    return NascentUpload.from_changesfile_path(
        datadir(upload_path), policy, DevNullLogger())


def getPPAUploadForSource(upload_path, ppa):
    """Return a NascentUpload object for a PPA source."""
    policy = getPolicy(name='insecure', distro='ubuntu', distroseries='hoary')
    policy.archive = ppa
    return NascentUpload.from_changesfile_path(
        datadir(upload_path), policy, DevNullLogger())


def getUploadForBinary(upload_path):
    """Return a NascentUpload object for binaries."""
    policy = getPolicy(name='sync', distro='ubuntu', distroseries='hoary')
    policy.accepted_type = ArchiveUploadType.BINARY_ONLY
    return NascentUpload.from_changesfile_path(
        datadir(upload_path), policy, DevNullLogger())


def testGlobalsSetup(test):
    """Inject useful helper functions in tests globals.

    We can use the getUpload* without unnecessary imports.
    """
    import_public_test_keys()
    setGlobs(test)
    test.globs['getUploadForSource'] = getUploadForSource
    test.globs['getUploadForBinary'] = getUploadForBinary
    test.globs['getPPAUploadForSource'] = getPPAUploadForSource


def prepareHoaryForUploads(test):
    """Prepare ubuntu/hoary to receive uploads.

    Ensure ubuntu/hoary is ready to receive and build new uploads in
    the RELEASE pocket (they are auto-overridden to the 'universe'
    component).
    """
    ubuntu = getUtility(IDistributionSet)['ubuntu']
    hoary = ubuntu['hoary']

    # Allow uploads to the universe component.
    universe = getUtility(IComponentSet)['universe']
    ComponentSelection(distroseries=hoary, component=universe)

    # Create a fake hoary/i386 chroot.
    fake_chroot = LibraryFileAlias.get(1)
    hoary['i386'].addOrUpdateChroot(fake_chroot)

    LaunchpadZopelessLayer.txn.commit()


def setUp(test):
    """Setup a typical nascentupload test environment.

    Use 'uploader' datebase user in a LaunchpadZopelessLayer transaction.
    Log in as a Launchpad admin (foo.bar@canonical.com).
    Setup test globals and prepare hoary for uploads
    """
    login('foo.bar@canonical.com')
    testGlobalsSetup(test)
    prepareHoaryForUploads(test)
    LaunchpadZopelessLayer.switchDbUser('uploader')


def tearDown(test):
    logout()


def test_suite():
    suite = unittest.TestSuite()
    tests_dir = os.path.dirname(os.path.realpath(__file__))

    filenames = [
        filename
        for filename in os.listdir(tests_dir)
        if filename.lower().endswith('.txt')
        ]

    for filename in sorted(filenames):
        test = LayeredDocFileSuite(
            filename, setUp=setUp, tearDown=tearDown,
            layer=LaunchpadZopelessLayer)
        suite.addTest(test)

    return suite