/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
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
65
    def start_bzr_subprocess_with_import_check(self, args, stderr_file=None):
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
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)
5898.2.6 by Vincent Ladeuil
The missing bit ! stderr_file needs to be propagated with care down to Popen or we either don't use it or override useful default values.
82
        kwargs = dict(env_changes=env_changes,
83
                      allow_plugins=(not are_plugins_disabled()))
84
        if stderr_file:
85
            # We don't want to update the whole call chain so we insert stderr
86
            # *iff* we need to
87
            kwargs['stderr'] = stderr_file
88
        return self.start_bzr_subprocess(args, **kwargs)
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
89
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
90
    def check_forbidden_modules(self, err, forbidden_imports):
91
        """Check for forbidden modules in stderr.
92
93
        :param err: Standard error
94
        :param forbidden_imports: List of forbidden modules
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
95
        """
5304.1.1 by Vincent Ladeuil
Pass BZR_PLUGINS_AT and BZR_DISABLE_PLINGS to the subprocess fpr test_import_tariff
96
        self.addDetail('subprocess_stderr',
5017.2.2 by Martin Pool
Add import tariff tests
97
            content.Content(content.ContentType("text", "plain"),
98
                lambda:[err]))
99
100
        bad_modules = []
101
        for module_name in forbidden_imports:
102
            if err.find("\nimport %s " % module_name) != -1:
103
                bad_modules.append(module_name)
104
105
        if bad_modules:
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
106
            self.fail("command loaded forbidden modules %r"
107
                % (bad_modules,))
108
109
    def finish_bzr_subprocess_with_import_check(self, process,
110
            args, forbidden_imports):
111
        """Finish subprocess and check specific modules have not been
112
        imported.
113
114
        :param forbidden_imports: List of fully-qualified Python module names
115
            that should not be loaded while running this command.
116
        """
117
        (out, err) = self.finish_bzr_subprocess(process,
118
            universal_newlines=False, process_args=args)
119
        self.check_forbidden_modules(err, forbidden_imports)
5017.2.2 by Martin Pool
Add import tariff tests
120
        return out, err
121
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
122
    def run_command_check_imports(self, args, forbidden_imports):
123
        """Run bzr ARGS in a subprocess and check its imports.
124
125
        This is fairly expensive because we start a subprocess, so we aim to
126
        cover representative rather than exhaustive cases.
127
128
        :param forbidden_imports: List of fully-qualified Python module names
129
            that should not be loaded while running this command.
130
        """
131
        process = self.start_bzr_subprocess_with_import_check(args)
132
        self.finish_bzr_subprocess_with_import_check(process, args,
133
            forbidden_imports)
134
5017.2.2 by Martin Pool
Add import tariff tests
135
    def test_import_tariffs_working(self):
136
        # check some guaranteed-true and false imports to be sure we're
137
        # measuring correctly
138
        self.make_branch_and_tree('.')
139
        self.run_command_check_imports(['st'],
140
            ['nonexistentmodulename', 'anothernonexistentmodule'])
141
        self.assertRaises(AssertionError,
142
            self.run_command_check_imports,
143
            ['st'],
144
            ['bzrlib.tree'])
145
146
    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.
147
        # 'st' in a default format working tree shouldn't need many modules
5017.2.2 by Martin Pool
Add import tariff tests
148
        self.make_branch_and_tree('.')
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
149
        self.run_command_check_imports(['st'], [
5757.8.2 by Jelmer Vernooij
Avoid annotate import during 'bzr st'.
150
            'bzrlib.annotate',
5745.2.2 by Jelmer Vernooij
Add atomicfile to test import tariff blacklist.
151
            'bzrlib.atomicfile',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
152
            'bzrlib.bugtracker',
5018.1.11 by Martin Pool
Check bundle commands are not loaded for 'bzr st' invocation
153
            'bzrlib.bundle.commands',
5127.1.1 by Martin Pool
version-info is lazily loaded
154
            'bzrlib.cmd_version_info',
5671.2.2 by Jelmer Vernooij
Remove unused serializer class.
155
            'bzrlib.externalcommand',
5745.3.2 by Jelmer Vernooij
Add filters to import tariff blacklist.
156
            'bzrlib.filters',
5749.1.1 by Jelmer Vernooij
Remove bzrlib.foreign from the test_import_tariff blacklist.
157
            # foreign branch plugins import the foreign_vcs_registry from 
158
            # bzrlib.foreign so it can't be blacklisted
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
159
            'bzrlib.gpg',
160
            'bzrlib.info',
5757.8.1 by Jelmer Vernooij
Avoid bzrlib.knit imports when using groupcompress repositories.
161
            '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.
162
            'bzrlib.merge3',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
163
            'bzrlib.merge_directive',
164
            '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.
165
            'bzrlib.patiencediff',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
166
            'bzrlib.remote',
5745.3.3 by Jelmer Vernooij
Add rules to blacklist.
167
            'bzrlib.rules',
5127.1.4 by Martin Pool
Lazy-load sign-my-commits
168
            'bzrlib.sign_my_commits',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
169
            'bzrlib.smart',
5712.3.15 by Jelmer Vernooij
Remove unused register format functions.
170
            'bzrlib.smart.client',
5757.8.5 by Jelmer Vernooij
Add smart medium/server to the blacklist.
171
            'bzrlib.smart.medium',
172
            '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.
173
            'bzrlib.transform',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
174
            'bzrlib.version_info_formats.format_rio',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
175
            'getpass',
5271.1.1 by Martin Pool
Test that Kerberos is no longer loaded.
176
            'kerberos',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
177
            'smtplib',
178
            'tarfile',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
179
            'tempfile',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
180
            ] + old_format_modules)
5127.1.2 by Martin Pool
Lazy-load conflict commands
181
        # TODO: similar test for repository-only operations, checking we avoid
182
        # loading wt-specific stuff
183
        #
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
184
        # See https://bugs.launchpad.net/bzr/+bug/553017
5510.1.1 by Martin von Gagern
Ensure 'bzr help commands' doesn't import testtools.
185
186
    def test_help_commands(self):
187
        # See https://bugs.launchpad.net/bzr/+bug/663773
188
        self.run_command_check_imports(['help', 'commands'], [
189
            'testtools',
190
            ])
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
191
192
    def test_simple_serve(self):
193
        # 'serve' in a default format working tree shouldn't need many modules
194
        tree = self.make_branch_and_tree('.')
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
195
        # Capture the bzr serve process' stderr in a file to avoid deadlocks
196
        # while the smart client interacts with it.
197
        stderr_file = open('bzr-serve.stderr', 'w')
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
198
        process = self.start_bzr_subprocess_with_import_check(['serve',
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
199
            '--inet', '-d', tree.basedir], stderr_file=stderr_file)
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
200
        url = 'bzr://localhost/'
201
        self.permit_url(url)
202
        client_medium = medium.SmartSimplePipesClientMedium(
203
            process.stdout, process.stdin, url)
204
        transport = remote.RemoteTransport(url, medium=client_medium)
205
        branch = BzrDir.open_from_transport(transport).open_branch()
206
        process.stdin.close()
207
        # Hide stdin from the subprocess module, so it won't fail to close it.
208
        process.stdin = None
209
        (out, err) = self.finish_bzr_subprocess(process,
210
            universal_newlines=False)
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
211
        stderr_file.close()
212
        with open('bzr-serve.stderr', 'r') as stderr_file:
213
            err = stderr_file.read()
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
214
        self.check_forbidden_modules(err,
215
            ['bzrlib.annotate',
216
            'bzrlib.atomicfile',
217
            'bzrlib.bugtracker',
218
            'bzrlib.bundle.commands',
219
            'bzrlib.cmd_version_info',
5816.3.2 by Jelmer Vernooij
Add workingtree4 and dirstate to the forbidden list.
220
            'bzrlib.dirstate',
5816.3.3 by Jelmer Vernooij
Add tariff test to make sure working tree isn't opened by 'bzr serve'
221
            'bzrlib._dirstate_helpers_py',
222
            'bzrlib._dirstate_helpers_pyx',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
223
            'bzrlib.externalcommand',
224
            'bzrlib.filters',
225
            # foreign branch plugins import the foreign_vcs_registry from 
226
            # bzrlib.foreign so it can't be blacklisted
227
            'bzrlib.gpg',
228
            'bzrlib.info',
229
            'bzrlib.knit',
230
            'bzrlib.merge3',
231
            'bzrlib.merge_directive',
232
            'bzrlib.msgeditor',
233
            'bzrlib.patiencediff',
234
            'bzrlib.remote',
235
            'bzrlib.rules',
236
            'bzrlib.sign_my_commits',
237
            'bzrlib.smart.client',
238
            'bzrlib.transform',
239
            'bzrlib.version_info_formats.format_rio',
5816.3.2 by Jelmer Vernooij
Add workingtree4 and dirstate to the forbidden list.
240
            'bzrlib.workingtree_4',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
241
            'getpass',
242
            'kerberos',
243
            'smtplib',
244
            'tarfile',
245
            'tempfile',
246
            ] + old_format_modules)