/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2010, 2011 Canonical Ltd
5017.2.2 by Martin Pool
Add import tariff tests
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
18
"""Tests for how many modules are loaded in executing various commands."""
19
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
20
import os
5017.2.2 by Martin Pool
Add import tariff tests
21
from testtools import content
22
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
23
from bzrlib.bzrdir import BzrDir
24
from bzrlib.smart import medium
25
from bzrlib.transport import remote
26
5017.2.2 by Martin Pool
Add import tariff tests
27
from bzrlib.plugin import (
28
    are_plugins_disabled,
29
    )
30
31
from bzrlib.tests import (
32
    TestCaseWithTransport,
33
    )
34
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
35
old_format_modules = [
36
    'bzrlib.repofmt.knitrepo',
37
    'bzrlib.repofmt.knitpack_repo',
38
    'bzrlib.plugins.weave_fmt.branch',
39
    'bzrlib.plugins.weave_fmt.bzrdir',
40
    'bzrlib.plugins.weave_fmt.repository',
41
    'bzrlib.plugins.weave_fmt.workingtree',
42
    'bzrlib.weave',
43
    'bzrlib.weavefile',
44
    'bzrlib.xml4',
45
    'bzrlib.xml5',
46
    'bzrlib.xml6',
47
    'bzrlib.xml7',
48
    ]
49
5017.2.2 by Martin Pool
Add import tariff tests
50
51
class TestImportTariffs(TestCaseWithTransport):
52
    """Check how many modules are loaded for some representative scenarios.
53
54
    See the Testing Guide in the developer documentation for more explanation.
55
    """
56
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
57
    def setUp(self):
58
        # Preserve some env vars as we want to escape the isolation for them
59
        self.preserved_env_vars = {}
60
        for name in ('BZR_HOME', 'BZR_PLUGIN_PATH', 'BZR_DISABLE_PLUGINS',
61
                     'BZR_PLUGINS_AT', 'HOME'):
62
            self.preserved_env_vars[name] = os.environ.get(name)
63
        super(TestImportTariffs, self).setUp()
64
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
65
    def start_bzr_subprocess_with_import_check(self, args):
66
        """Run a bzr process and capture the imports.
5018.1.8 by Martin Pool
doc
67
68
        This is fairly expensive because we start a subprocess, so we aim to
69
        cover representative rather than exhaustive cases.
70
        """
5017.2.2 by Martin Pool
Add import tariff tests
71
        # We use PYTHON_VERBOSE rather than --profile-importts because in
72
        # experimentation the profile-imports output seems to not always show
73
        # the modules you'd expect; this can be debugged but python -v seems
74
        # more likely to always show everything.  And we use the environment
75
        # variable rather than 'python -v' in the hope it will work even if
76
        # bzr is frozen and python is not explicitly specified. -- mbp 20100208
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
77
5017.2.2 by Martin Pool
Add import tariff tests
78
        # Normally we want test isolation from the real $HOME but here we
79
        # explicitly do want to test against things installed there, therefore
80
        # we pass it through.
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
81
        env_changes = dict(PYTHONVERBOSE='1', **self.preserved_env_vars)
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
82
        return self.start_bzr_subprocess(args, env_changes=env_changes,
83
            allow_plugins=(not are_plugins_disabled()))
84
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
85
    def check_forbidden_modules(self, err, forbidden_imports):
86
        """Check for forbidden modules in stderr.
87
88
        :param err: Standard error
89
        :param forbidden_imports: List of forbidden modules
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
90
        """
5304.1.1 by Vincent Ladeuil
Pass BZR_PLUGINS_AT and BZR_DISABLE_PLINGS to the subprocess fpr test_import_tariff
91
        self.addDetail('subprocess_stderr',
5017.2.2 by Martin Pool
Add import tariff tests
92
            content.Content(content.ContentType("text", "plain"),
93
                lambda:[err]))
94
95
        bad_modules = []
96
        for module_name in forbidden_imports:
97
            if err.find("\nimport %s " % module_name) != -1:
98
                bad_modules.append(module_name)
99
100
        if bad_modules:
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
101
            self.fail("command loaded forbidden modules %r"
102
                % (bad_modules,))
103
104
    def finish_bzr_subprocess_with_import_check(self, process,
105
            args, forbidden_imports):
106
        """Finish subprocess and check specific modules have not been
107
        imported.
108
109
        :param forbidden_imports: List of fully-qualified Python module names
110
            that should not be loaded while running this command.
111
        """
112
        (out, err) = self.finish_bzr_subprocess(process,
113
            universal_newlines=False, process_args=args)
114
        self.check_forbidden_modules(err, forbidden_imports)
5017.2.2 by Martin Pool
Add import tariff tests
115
        return out, err
116
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
117
    def run_command_check_imports(self, args, forbidden_imports):
118
        """Run bzr ARGS in a subprocess and check its imports.
119
120
        This is fairly expensive because we start a subprocess, so we aim to
121
        cover representative rather than exhaustive cases.
122
123
        :param forbidden_imports: List of fully-qualified Python module names
124
            that should not be loaded while running this command.
125
        """
126
        process = self.start_bzr_subprocess_with_import_check(args)
127
        self.finish_bzr_subprocess_with_import_check(process, args,
128
            forbidden_imports)
129
5017.2.2 by Martin Pool
Add import tariff tests
130
    def test_import_tariffs_working(self):
131
        # check some guaranteed-true and false imports to be sure we're
132
        # measuring correctly
133
        self.make_branch_and_tree('.')
134
        self.run_command_check_imports(['st'],
135
            ['nonexistentmodulename', 'anothernonexistentmodule'])
136
        self.assertRaises(AssertionError,
137
            self.run_command_check_imports,
138
            ['st'],
139
            ['bzrlib.tree'])
140
141
    def test_simple_local(self):
5698.1.1 by Jelmer Vernooij
Add bzrlib.workingtree_2 to the list of forbidden modules for 'bzr st' in a 2a tree.
142
        # 'st' in a default format working tree shouldn't need many modules
5017.2.2 by Martin Pool
Add import tariff tests
143
        self.make_branch_and_tree('.')
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
144
        self.run_command_check_imports(['st'], [
5757.8.2 by Jelmer Vernooij
Avoid annotate import during 'bzr st'.
145
            'bzrlib.annotate',
5745.2.2 by Jelmer Vernooij
Add atomicfile to test import tariff blacklist.
146
            'bzrlib.atomicfile',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
147
            'bzrlib.bugtracker',
5018.1.11 by Martin Pool
Check bundle commands are not loaded for 'bzr st' invocation
148
            'bzrlib.bundle.commands',
5127.1.1 by Martin Pool
version-info is lazily loaded
149
            'bzrlib.cmd_version_info',
5671.2.2 by Jelmer Vernooij
Remove unused serializer class.
150
            'bzrlib.externalcommand',
5745.3.2 by Jelmer Vernooij
Add filters to import tariff blacklist.
151
            'bzrlib.filters',
5749.1.1 by Jelmer Vernooij
Remove bzrlib.foreign from the test_import_tariff blacklist.
152
            # foreign branch plugins import the foreign_vcs_registry from 
153
            # bzrlib.foreign so it can't be blacklisted
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
154
            'bzrlib.gpg',
155
            'bzrlib.info',
5757.8.1 by Jelmer Vernooij
Avoid bzrlib.knit imports when using groupcompress repositories.
156
            'bzrlib.knit',
5279.1.1 by Andrew Bennetts
lazy_import most things in merge.py; add a few representative modules to the import tariff tests; tweak a couple of other modules so that patiencediff is not necessarily imported; remove a bunch of unused imports from test_knit.py.
157
            'bzrlib.merge3',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
158
            'bzrlib.merge_directive',
159
            'bzrlib.msgeditor',
5279.1.1 by Andrew Bennetts
lazy_import most things in merge.py; add a few representative modules to the import tariff tests; tweak a couple of other modules so that patiencediff is not necessarily imported; remove a bunch of unused imports from test_knit.py.
160
            'bzrlib.patiencediff',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
161
            'bzrlib.remote',
5745.3.3 by Jelmer Vernooij
Add rules to blacklist.
162
            'bzrlib.rules',
5127.1.4 by Martin Pool
Lazy-load sign-my-commits
163
            'bzrlib.sign_my_commits',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
164
            'bzrlib.smart',
5712.3.15 by Jelmer Vernooij
Remove unused register format functions.
165
            'bzrlib.smart.client',
5757.8.5 by Jelmer Vernooij
Add smart medium/server to the blacklist.
166
            'bzrlib.smart.medium',
167
            'bzrlib.smart.server',
5279.1.1 by Andrew Bennetts
lazy_import most things in merge.py; add a few representative modules to the import tariff tests; tweak a couple of other modules so that patiencediff is not necessarily imported; remove a bunch of unused imports from test_knit.py.
168
            'bzrlib.transform',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
169
            'bzrlib.version_info_formats.format_rio',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
170
            'getpass',
5271.1.1 by Martin Pool
Test that Kerberos is no longer loaded.
171
            'kerberos',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
172
            'smtplib',
173
            'tarfile',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
174
            'tempfile',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
175
            ] + old_format_modules)
5127.1.2 by Martin Pool
Lazy-load conflict commands
176
        # TODO: similar test for repository-only operations, checking we avoid
177
        # loading wt-specific stuff
178
        #
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
179
        # See https://bugs.launchpad.net/bzr/+bug/553017
5510.1.1 by Martin von Gagern
Ensure 'bzr help commands' doesn't import testtools.
180
181
    def test_help_commands(self):
182
        # See https://bugs.launchpad.net/bzr/+bug/663773
183
        self.run_command_check_imports(['help', 'commands'], [
184
            'testtools',
185
            ])
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
186
187
    def test_simple_serve(self):
188
        # 'serve' in a default format working tree shouldn't need many modules
189
        tree = self.make_branch_and_tree('.')
190
        process = self.start_bzr_subprocess_with_import_check(['serve',
191
            '--inet', '-d', tree.basedir])
192
        url = 'bzr://localhost/'
193
        self.permit_url(url)
194
        client_medium = medium.SmartSimplePipesClientMedium(
195
            process.stdout, process.stdin, url)
196
        transport = remote.RemoteTransport(url, medium=client_medium)
197
        branch = BzrDir.open_from_transport(transport).open_branch()
198
        process.stdin.close()
199
        # Hide stdin from the subprocess module, so it won't fail to close it.
200
        process.stdin = None
201
        (out, err) = self.finish_bzr_subprocess(process,
202
            universal_newlines=False)
203
        self.check_forbidden_modules(err,
204
            ['bzrlib.annotate',
205
            'bzrlib.atomicfile',
206
            'bzrlib.bugtracker',
207
            'bzrlib.bundle.commands',
208
            'bzrlib.cmd_version_info',
5816.3.2 by Jelmer Vernooij
Add workingtree4 and dirstate to the forbidden list.
209
            'bzrlib.dirstate',
5816.3.3 by Jelmer Vernooij
Add tariff test to make sure working tree isn't opened by 'bzr serve'
210
            'bzrlib._dirstate_helpers_py',
211
            'bzrlib._dirstate_helpers_pyx',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
212
            'bzrlib.externalcommand',
213
            'bzrlib.filters',
214
            # foreign branch plugins import the foreign_vcs_registry from 
215
            # bzrlib.foreign so it can't be blacklisted
216
            'bzrlib.gpg',
217
            'bzrlib.info',
218
            'bzrlib.knit',
219
            'bzrlib.merge3',
220
            'bzrlib.merge_directive',
221
            'bzrlib.msgeditor',
222
            'bzrlib.patiencediff',
223
            'bzrlib.remote',
224
            'bzrlib.rules',
225
            'bzrlib.sign_my_commits',
226
            'bzrlib.smart.client',
227
            'bzrlib.transform',
228
            'bzrlib.version_info_formats.format_rio',
5816.3.2 by Jelmer Vernooij
Add workingtree4 and dirstate to the forbidden list.
229
            'bzrlib.workingtree_4',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
230
            'getpass',
231
            'kerberos',
232
            'smtplib',
233
            'tarfile',
234
            'tempfile',
235
            ] + old_format_modules)