/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
5935.1.1 by Vincent Ladeuil
Preserve $HOME/.bzr.log from tests
23
from bzrlib import (
24
    osutils,
25
    trace,
26
    )
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
27
from bzrlib.bzrdir import BzrDir
28
from bzrlib.smart import medium
29
from bzrlib.transport import remote
30
5017.2.2 by Martin Pool
Add import tariff tests
31
from bzrlib.plugin import (
32
    are_plugins_disabled,
33
    )
34
35
from bzrlib.tests import (
36
    TestCaseWithTransport,
37
    )
38
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
39
old_format_modules = [
40
    'bzrlib.repofmt.knitrepo',
41
    'bzrlib.repofmt.knitpack_repo',
42
    'bzrlib.plugins.weave_fmt.branch',
43
    'bzrlib.plugins.weave_fmt.bzrdir',
44
    'bzrlib.plugins.weave_fmt.repository',
45
    'bzrlib.plugins.weave_fmt.workingtree',
46
    'bzrlib.weave',
47
    'bzrlib.weavefile',
48
    'bzrlib.xml4',
49
    'bzrlib.xml5',
50
    'bzrlib.xml6',
51
    'bzrlib.xml7',
52
    ]
53
5017.2.2 by Martin Pool
Add import tariff tests
54
55
class TestImportTariffs(TestCaseWithTransport):
56
    """Check how many modules are loaded for some representative scenarios.
57
58
    See the Testing Guide in the developer documentation for more explanation.
59
    """
60
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
61
    def setUp(self):
62
        # Preserve some env vars as we want to escape the isolation for them
63
        self.preserved_env_vars = {}
64
        for name in ('BZR_HOME', 'BZR_PLUGIN_PATH', 'BZR_DISABLE_PLUGINS',
65
                     'BZR_PLUGINS_AT', 'HOME'):
66
            self.preserved_env_vars[name] = os.environ.get(name)
67
        super(TestImportTariffs, self).setUp()
5935.1.1 by Vincent Ladeuil
Preserve $HOME/.bzr.log from tests
68
        self.log_path = osutils.pathjoin(self.test_home_dir, '.bzr.log')
69
        self.overrideEnv('BZR_LOG', self.log_path)
70
71
    def test_log_path_overriden(self):
72
        # ensure we get the log file in the right place
73
        actual_log_path = trace._get_bzr_log_filename()
74
        self.assertStartsWith(actual_log_path, self.test_home_dir)
75
        self.assertEquals(self.log_path, actual_log_path)
76
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
77
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
78
    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
79
        """Run a bzr process and capture the imports.
5018.1.8 by Martin Pool
doc
80
81
        This is fairly expensive because we start a subprocess, so we aim to
82
        cover representative rather than exhaustive cases.
83
        """
5017.2.2 by Martin Pool
Add import tariff tests
84
        # We use PYTHON_VERBOSE rather than --profile-importts because in
85
        # experimentation the profile-imports output seems to not always show
86
        # the modules you'd expect; this can be debugged but python -v seems
87
        # more likely to always show everything.  And we use the environment
88
        # variable rather than 'python -v' in the hope it will work even if
89
        # 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.
90
5017.2.2 by Martin Pool
Add import tariff tests
91
        # Normally we want test isolation from the real $HOME but here we
92
        # explicitly do want to test against things installed there, therefore
93
        # we pass it through.
5570.3.7 by Vincent Ladeuil
For test_import_tariff we want to preserve the env vars *before* isolation.
94
        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.
95
        kwargs = dict(env_changes=env_changes,
96
                      allow_plugins=(not are_plugins_disabled()))
97
        if stderr_file:
98
            # We don't want to update the whole call chain so we insert stderr
99
            # *iff* we need to
100
            kwargs['stderr'] = stderr_file
101
        return self.start_bzr_subprocess(args, **kwargs)
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
102
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
103
    def check_forbidden_modules(self, err, forbidden_imports):
104
        """Check for forbidden modules in stderr.
105
106
        :param err: Standard error
107
        :param forbidden_imports: List of forbidden modules
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
108
        """
5304.1.1 by Vincent Ladeuil
Pass BZR_PLUGINS_AT and BZR_DISABLE_PLINGS to the subprocess fpr test_import_tariff
109
        self.addDetail('subprocess_stderr',
5017.2.2 by Martin Pool
Add import tariff tests
110
            content.Content(content.ContentType("text", "plain"),
111
                lambda:[err]))
112
113
        bad_modules = []
114
        for module_name in forbidden_imports:
115
            if err.find("\nimport %s " % module_name) != -1:
116
                bad_modules.append(module_name)
117
118
        if bad_modules:
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
119
            self.fail("command loaded forbidden modules %r"
120
                % (bad_modules,))
121
122
    def finish_bzr_subprocess_with_import_check(self, process,
123
            args, forbidden_imports):
124
        """Finish subprocess and check specific modules have not been
125
        imported.
126
127
        :param forbidden_imports: List of fully-qualified Python module names
128
            that should not be loaded while running this command.
129
        """
130
        (out, err) = self.finish_bzr_subprocess(process,
131
            universal_newlines=False, process_args=args)
132
        self.check_forbidden_modules(err, forbidden_imports)
5017.2.2 by Martin Pool
Add import tariff tests
133
        return out, err
134
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
135
    def run_command_check_imports(self, args, forbidden_imports):
136
        """Run bzr ARGS in a subprocess and check its imports.
137
138
        This is fairly expensive because we start a subprocess, so we aim to
139
        cover representative rather than exhaustive cases.
140
141
        :param forbidden_imports: List of fully-qualified Python module names
142
            that should not be loaded while running this command.
143
        """
144
        process = self.start_bzr_subprocess_with_import_check(args)
145
        self.finish_bzr_subprocess_with_import_check(process, args,
146
            forbidden_imports)
147
5017.2.2 by Martin Pool
Add import tariff tests
148
    def test_import_tariffs_working(self):
149
        # check some guaranteed-true and false imports to be sure we're
150
        # measuring correctly
151
        self.make_branch_and_tree('.')
152
        self.run_command_check_imports(['st'],
153
            ['nonexistentmodulename', 'anothernonexistentmodule'])
154
        self.assertRaises(AssertionError,
155
            self.run_command_check_imports,
156
            ['st'],
157
            ['bzrlib.tree'])
158
159
    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.
160
        # 'st' in a default format working tree shouldn't need many modules
5017.2.2 by Martin Pool
Add import tariff tests
161
        self.make_branch_and_tree('.')
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
162
        self.run_command_check_imports(['st'], [
5757.8.2 by Jelmer Vernooij
Avoid annotate import during 'bzr st'.
163
            'bzrlib.annotate',
5745.2.2 by Jelmer Vernooij
Add atomicfile to test import tariff blacklist.
164
            'bzrlib.atomicfile',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
165
            'bzrlib.bugtracker',
5018.1.11 by Martin Pool
Check bundle commands are not loaded for 'bzr st' invocation
166
            'bzrlib.bundle.commands',
5127.1.1 by Martin Pool
version-info is lazily loaded
167
            'bzrlib.cmd_version_info',
5671.2.2 by Jelmer Vernooij
Remove unused serializer class.
168
            'bzrlib.externalcommand',
5745.3.2 by Jelmer Vernooij
Add filters to import tariff blacklist.
169
            'bzrlib.filters',
5749.1.1 by Jelmer Vernooij
Remove bzrlib.foreign from the test_import_tariff blacklist.
170
            # foreign branch plugins import the foreign_vcs_registry from 
171
            # bzrlib.foreign so it can't be blacklisted
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
172
            'bzrlib.gpg',
173
            'bzrlib.info',
5757.8.1 by Jelmer Vernooij
Avoid bzrlib.knit imports when using groupcompress repositories.
174
            '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.
175
            'bzrlib.merge3',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
176
            'bzrlib.merge_directive',
177
            '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.
178
            'bzrlib.patiencediff',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
179
            'bzrlib.remote',
5745.3.3 by Jelmer Vernooij
Add rules to blacklist.
180
            'bzrlib.rules',
5127.1.4 by Martin Pool
Lazy-load sign-my-commits
181
            'bzrlib.sign_my_commits',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
182
            'bzrlib.smart',
5712.3.15 by Jelmer Vernooij
Remove unused register format functions.
183
            'bzrlib.smart.client',
5757.8.5 by Jelmer Vernooij
Add smart medium/server to the blacklist.
184
            'bzrlib.smart.medium',
185
            '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.
186
            'bzrlib.transform',
5671.2.1 by Jelmer Vernooij
Add some more forbidden imports to the import tariff test.
187
            'bzrlib.version_info_formats.format_rio',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
188
            'getpass',
5271.1.1 by Martin Pool
Test that Kerberos is no longer loaded.
189
            'kerberos',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
190
            'smtplib',
191
            'tarfile',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
192
            'tempfile',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
193
            ] + old_format_modules)
5127.1.2 by Martin Pool
Lazy-load conflict commands
194
        # TODO: similar test for repository-only operations, checking we avoid
195
        # loading wt-specific stuff
196
        #
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
197
        # See https://bugs.launchpad.net/bzr/+bug/553017
5510.1.1 by Martin von Gagern
Ensure 'bzr help commands' doesn't import testtools.
198
199
    def test_help_commands(self):
200
        # See https://bugs.launchpad.net/bzr/+bug/663773
201
        self.run_command_check_imports(['help', 'commands'], [
202
            'testtools',
203
            ])
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
204
205
    def test_simple_serve(self):
206
        # 'serve' in a default format working tree shouldn't need many modules
207
        tree = self.make_branch_and_tree('.')
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
208
        # Capture the bzr serve process' stderr in a file to avoid deadlocks
209
        # while the smart client interacts with it.
210
        stderr_file = open('bzr-serve.stderr', 'w')
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
211
        process = self.start_bzr_subprocess_with_import_check(['serve',
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
212
            '--inet', '-d', tree.basedir], stderr_file=stderr_file)
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
213
        url = 'bzr://localhost/'
214
        self.permit_url(url)
215
        client_medium = medium.SmartSimplePipesClientMedium(
216
            process.stdout, process.stdin, url)
217
        transport = remote.RemoteTransport(url, medium=client_medium)
218
        branch = BzrDir.open_from_transport(transport).open_branch()
219
        process.stdin.close()
220
        # Hide stdin from the subprocess module, so it won't fail to close it.
221
        process.stdin = None
222
        (out, err) = self.finish_bzr_subprocess(process,
223
            universal_newlines=False)
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
224
        stderr_file.close()
225
        with open('bzr-serve.stderr', 'r') as stderr_file:
226
            err = stderr_file.read()
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
227
        self.check_forbidden_modules(err,
228
            ['bzrlib.annotate',
229
            'bzrlib.atomicfile',
230
            'bzrlib.bugtracker',
231
            'bzrlib.bundle.commands',
232
            'bzrlib.cmd_version_info',
5816.3.2 by Jelmer Vernooij
Add workingtree4 and dirstate to the forbidden list.
233
            'bzrlib.dirstate',
5816.3.3 by Jelmer Vernooij
Add tariff test to make sure working tree isn't opened by 'bzr serve'
234
            'bzrlib._dirstate_helpers_py',
235
            'bzrlib._dirstate_helpers_pyx',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
236
            'bzrlib.externalcommand',
237
            'bzrlib.filters',
238
            # foreign branch plugins import the foreign_vcs_registry from 
239
            # bzrlib.foreign so it can't be blacklisted
240
            'bzrlib.gpg',
241
            'bzrlib.info',
242
            'bzrlib.knit',
243
            'bzrlib.merge3',
244
            'bzrlib.merge_directive',
245
            'bzrlib.msgeditor',
246
            'bzrlib.patiencediff',
247
            'bzrlib.remote',
248
            'bzrlib.rules',
249
            'bzrlib.sign_my_commits',
250
            'bzrlib.smart.client',
251
            'bzrlib.transform',
252
            'bzrlib.version_info_formats.format_rio',
5816.3.2 by Jelmer Vernooij
Add workingtree4 and dirstate to the forbidden list.
253
            'bzrlib.workingtree_4',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
254
            'getpass',
255
            'kerberos',
256
            'smtplib',
257
            'tarfile',
258
            'tempfile',
259
            ] + old_format_modules)