/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
5939.3.2 by Andrew Bennetts
Take a slightly more direct approach by largely preserving BZR_DISABLE_PLUGINS/BZR_PLUGINS_AT.
20
import os
21
5017.2.2 by Martin Pool
Add import tariff tests
22
from testtools import content
23
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from .. import (
5939.3.2 by Andrew Bennetts
Take a slightly more direct approach by largely preserving BZR_DISABLE_PLUGINS/BZR_PLUGINS_AT.
25
    plugins as _mod_plugins,
5935.1.1 by Vincent Ladeuil
Preserve $HOME/.bzr.log from tests
26
    trace,
27
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
28
from ..controldir import ControlDir
6670.4.16 by Jelmer Vernooij
Move smart to breezy.bzr.
29
from ..bzr.smart import medium
7058.3.8 by Jelmer Vernooij
Fix import tariff tests.
30
from ..sixish import PY3
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
31
from ..transport import remote
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
32
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
33
from . import (
5017.2.2 by Martin Pool
Add import tariff tests
34
    TestCaseWithTransport,
35
    )
36
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
37
old_format_modules = [
6670.4.5 by Jelmer Vernooij
Move breezy.repofmt contents to breezy.bzr.
38
    'breezy.bzr.knitrepo',
39
    'breezy.bzr.knitpack_repo',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
40
    'breezy.plugins.weave_fmt.branch',
41
    'breezy.plugins.weave_fmt.bzrdir',
42
    'breezy.plugins.weave_fmt.repository',
43
    'breezy.plugins.weave_fmt.workingtree',
6670.4.12 by Jelmer Vernooij
Move inventorytree to breezy.bzr.
44
    'breezy.bzr.weave',
45
    'breezy.bzr.weavefile',
46
    'breezy.bzr.xml4',
47
    'breezy.bzr.xml5',
48
    'breezy.bzr.xml6',
49
    'breezy.bzr.xml7',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
50
    ]
51
5017.2.2 by Martin Pool
Add import tariff tests
52
5956.1.1 by Jelmer Vernooij
Add ImportTariffTestCase.
53
class ImportTariffTestCase(TestCaseWithTransport):
5956.1.2 by Jelmer Vernooij
Review feedback from Vincent.
54
    """Check how many modules are loaded for some representative scenarios.
55
56
    See the Testing Guide in the developer documentation for more explanation.
57
58
59
    We must respect the setup used by the selftest command regarding
60
    plugins. This allows the user to control which plugins are in effect while
61
    running these tests and respect the import policies defined here.
62
63
    When failures are encountered for a given plugin, they can generally be
64
    addressed by using lazy import or lazy hook registration.
65
    """
5017.2.2 by Martin Pool
Add import tariff tests
66
5939.3.2 by Andrew Bennetts
Take a slightly more direct approach by largely preserving BZR_DISABLE_PLUGINS/BZR_PLUGINS_AT.
67
    def setUp(self):
68
        self.preserved_env_vars = {}
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
69
        for name in ('BRZ_PLUGIN_PATH', 'BRZ_DISABLE_PLUGINS', 'BRZ_PLUGINS_AT'
5939.3.4 by Vincent Ladeuil
Even better to not rely on an implementation detail.
70
                     ):
5939.3.2 by Andrew Bennetts
Take a slightly more direct approach by largely preserving BZR_DISABLE_PLUGINS/BZR_PLUGINS_AT.
71
            self.preserved_env_vars[name] = os.environ.get(name)
5956.1.1 by Jelmer Vernooij
Add ImportTariffTestCase.
72
        super(ImportTariffTestCase, self).setUp()
5939.3.2 by Andrew Bennetts
Take a slightly more direct approach by largely preserving BZR_DISABLE_PLUGINS/BZR_PLUGINS_AT.
73
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
74
    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
75
        """Run a bzr process and capture the imports.
5018.1.8 by Martin Pool
doc
76
77
        This is fairly expensive because we start a subprocess, so we aim to
78
        cover representative rather than exhaustive cases.
79
        """
5939.3.1 by Andrew Bennetts
Don't use real /home/andrew in test_import_tariffs. We can test the real plugins and be properly isolated from the user's config and log.
80
        # We use PYTHON_VERBOSE rather than --profile-imports because in
5017.2.2 by Martin Pool
Add import tariff tests
81
        # experimentation the profile-imports output seems to not always show
82
        # the modules you'd expect; this can be debugged but python -v seems
83
        # more likely to always show everything.  And we use the environment
84
        # variable rather than 'python -v' in the hope it will work even if
85
        # bzr is frozen and python is not explicitly specified. -- mbp 20100208
5939.3.4 by Vincent Ladeuil
Even better to not rely on an implementation detail.
86
        env_changes = dict(PYTHONVERBOSE='1', **self.preserved_env_vars)
5939.3.1 by Andrew Bennetts
Don't use real /home/andrew in test_import_tariffs. We can test the real plugins and be properly isolated from the user's config and log.
87
        trace.mutter('Setting env for bzr subprocess: %r', env_changes)
6651.4.1 by Martin
Rewrite of the plugin module for Python 3 compat and general sanity
88
        kwargs = dict(env_changes=env_changes, allow_plugins=False)
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.
89
        if stderr_file:
90
            # We don't want to update the whole call chain so we insert stderr
91
            # *iff* we need to
92
            kwargs['stderr'] = stderr_file
93
        return self.start_bzr_subprocess(args, **kwargs)
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
94
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
95
    def check_forbidden_modules(self, err, forbidden_imports):
96
        """Check for forbidden modules in stderr.
97
98
        :param err: Standard error
99
        :param forbidden_imports: List of forbidden modules
5816.1.2 by Jelmer Vernooij
Factor out start_bzr_subprocess_with_import_check
100
        """
7058.3.9 by Jelmer Vernooij
Review comments.
101
        err = err.decode('utf-8')
5304.1.1 by Vincent Ladeuil
Pass BZR_PLUGINS_AT and BZR_DISABLE_PLINGS to the subprocess fpr test_import_tariff
102
        self.addDetail('subprocess_stderr',
7143.15.2 by Jelmer Vernooij
Run autopep8.
103
                       content.text_content(err))
5017.2.2 by Martin Pool
Add import tariff tests
104
105
        bad_modules = []
106
        for module_name in forbidden_imports:
7058.3.8 by Jelmer Vernooij
Fix import tariff tests.
107
            if PY3:
7058.3.9 by Jelmer Vernooij
Review comments.
108
                if err.find("\nimport '%s' " % module_name) != -1:
7058.3.8 by Jelmer Vernooij
Fix import tariff tests.
109
                    bad_modules.append(module_name)
110
            else:
111
                if err.find("\nimport %s " % module_name) != -1:
112
                    bad_modules.append(module_name)
5017.2.2 by Martin Pool
Add import tariff tests
113
114
        if bad_modules:
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
115
            self.fail("command loaded forbidden modules %r"
7143.15.2 by Jelmer Vernooij
Run autopep8.
116
                      % (bad_modules,))
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
117
118
    def finish_bzr_subprocess_with_import_check(self, process,
7143.15.2 by Jelmer Vernooij
Run autopep8.
119
                                                args, forbidden_imports):
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
120
        """Finish subprocess and check specific modules have not been
121
        imported.
122
123
        :param forbidden_imports: List of fully-qualified Python module names
124
            that should not be loaded while running this command.
125
        """
126
        (out, err) = self.finish_bzr_subprocess(process,
7143.15.2 by Jelmer Vernooij
Run autopep8.
127
                                                universal_newlines=False, process_args=args)
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
128
        self.check_forbidden_modules(err, forbidden_imports)
5017.2.2 by Martin Pool
Add import tariff tests
129
        return out, err
130
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
131
    def run_command_check_imports(self, args, forbidden_imports):
132
        """Run bzr ARGS in a subprocess and check its imports.
133
134
        This is fairly expensive because we start a subprocess, so we aim to
135
        cover representative rather than exhaustive cases.
136
137
        :param forbidden_imports: List of fully-qualified Python module names
138
            that should not be loaded while running this command.
139
        """
140
        process = self.start_bzr_subprocess_with_import_check(args)
141
        self.finish_bzr_subprocess_with_import_check(process, args,
7143.15.2 by Jelmer Vernooij
Run autopep8.
142
                                                     forbidden_imports)
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
143
5956.1.1 by Jelmer Vernooij
Add ImportTariffTestCase.
144
145
class TestImportTariffs(ImportTariffTestCase):
5956.1.2 by Jelmer Vernooij
Review feedback from Vincent.
146
    """Basic import tariff tests for some common bzr commands"""
5956.1.1 by Jelmer Vernooij
Add ImportTariffTestCase.
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'],
7143.15.2 by Jelmer Vernooij
Run autopep8.
153
                                       ['nonexistentmodulename', 'anothernonexistentmodule'])
5017.2.2 by Martin Pool
Add import tariff tests
154
        self.assertRaises(AssertionError,
7143.15.2 by Jelmer Vernooij
Run autopep8.
155
                          self.run_command_check_imports,
156
                          ['st'],
157
                          ['breezy.tree'])
5017.2.2 by Martin Pool
Add import tariff tests
158
7490.64.1 by Jelmer Vernooij
Add import tariff test for 'brz st' in a git repository.
159
    def test_simple_local_bzr(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
7490.64.1 by Jelmer Vernooij
Add import tariff test for 'brz st' in a git repository.
161
        self.make_branch_and_tree('.', format='bzr')
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
162
        self.run_command_check_imports(['st'], [
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
163
            'breezy.annotate',
164
            'breezy.atomicfile',
165
            'breezy.bugtracker',
166
            'breezy.bundle.commands',
167
            'breezy.cmd_version_info',
168
            'breezy.externalcommand',
169
            'breezy.filters',
170
            'breezy.hashcache',
7143.15.2 by Jelmer Vernooij
Run autopep8.
171
            # foreign branch plugins import the foreign_vcs_registry from
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
172
            # breezy.foreign so it can't be blacklisted
173
            'breezy.gpg',
174
            'breezy.info',
6670.4.12 by Jelmer Vernooij
Move inventorytree to breezy.bzr.
175
            'breezy.bzr.knit',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
176
            'breezy.merge3',
177
            'breezy.merge_directive',
178
            'breezy.msgeditor',
6670.4.14 by Jelmer Vernooij
Move remote to breezy.bzr.
179
            'breezy.bzr.remote',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
180
            'breezy.rules',
181
            'breezy.sign_my_commits',
6670.4.16 by Jelmer Vernooij
Move smart to breezy.bzr.
182
            'breezy.bzr.smart',
183
            'breezy.bzr.smart.client',
184
            'breezy.bzr.smart.medium',
185
            'breezy.bzr.smart.server',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
186
            'breezy.transform',
187
            'breezy.version_info_formats.format_rio',
6670.4.12 by Jelmer Vernooij
Move inventorytree to breezy.bzr.
188
            'breezy.bzr.xml_serializer',
189
            'breezy.bzr.xml8',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
190
            'getpass',
5271.1.1 by Martin Pool
Test that Kerberos is no longer loaded.
191
            'kerberos',
7058.3.12 by Jelmer Vernooij
Lazy import shutil; on some platforms, it triggers an import of tarfile.
192
            'shutil',
6379.4.1 by Jelmer Vernooij
add test for importing ssl/socket.
193
            'ssl',
194
            'socket',
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
195
            'smtplib',
196
            'tarfile',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
197
            'tempfile',
6379.1.1 by Jelmer Vernooij
Avoid importing tty and termios if we don't have to.
198
            'termios',
199
            'tty',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
200
            ] + old_format_modules)
5127.1.2 by Martin Pool
Lazy-load conflict commands
201
        # TODO: similar test for repository-only operations, checking we avoid
202
        # loading wt-specific stuff
203
        #
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
204
        # See https://bugs.launchpad.net/bzr/+bug/553017
5510.1.1 by Martin von Gagern
Ensure 'bzr help commands' doesn't import testtools.
205
7490.64.1 by Jelmer Vernooij
Add import tariff test for 'brz st' in a git repository.
206
    def test_simple_local_git(self):
207
        # 'st' in a default format working tree shouldn't need many modules
208
        self.make_branch_and_tree('.', format='git')
7490.64.3 by Jelmer Vernooij
Fix import tariff test.
209
        from dulwich import __version__ as dulwich_version
210
211
        if dulwich_version >= (0, 20, 4):
212
            forbidden = ['shutil', 'tempfile', 'ssl']
213
        elif PY3:
214
            forbidden = ['ssl']
215
        else:
216
            forbidden = []
217
7490.64.1 by Jelmer Vernooij
Add import tariff test for 'brz st' in a git repository.
218
        self.run_command_check_imports(['st'], [
219
            'breezy.annotate',
220
            'breezy.bugtracker',
221
            'breezy.bundle.commands',
222
            'breezy.cmd_version_info',
223
            'breezy.externalcommand',
224
            'breezy.filters',
225
            'breezy.hashcache',
226
            # foreign branch plugins import the foreign_vcs_registry from
227
            # breezy.foreign so it can't be blacklisted
228
            'breezy.gpg',
229
            'breezy.info',
230
            'breezy.bzr.knit',
7490.83.5 by Jelmer Vernooij
Fix import tariff test.
231
            'breezy.merge',
7490.64.1 by Jelmer Vernooij
Add import tariff test for 'brz st' in a git repository.
232
            'breezy.merge3',
233
            'breezy.merge_directive',
234
            'breezy.msgeditor',
235
            'breezy.bzr.remote',
236
            'breezy.rules',
237
            'breezy.sign_my_commits',
238
            'breezy.bzr.smart',
239
            'breezy.bzr.smart.client',
240
            'breezy.bzr.smart.medium',
241
            'breezy.bzr.smart.server',
242
            'breezy.transform',
243
            'breezy.version_info_formats.format_rio',
244
            'breezy.bzr.xml_serializer',
245
            'breezy.bzr.xml8',
246
            'breezy.bzr.inventory',
247
            'breezy.bzr.bzrdir',
248
            'breezy.git.remote',
249
            'breezy.git.commit',
250
            'getpass',
251
            'kerberos',
252
            'smtplib',
253
            'tarfile',
254
            'termios',
255
            'tty',
7490.64.3 by Jelmer Vernooij
Fix import tariff test.
256
            ] + old_format_modules + forbidden)
7490.64.1 by Jelmer Vernooij
Add import tariff test for 'brz st' in a git repository.
257
5510.1.1 by Martin von Gagern
Ensure 'bzr help commands' doesn't import testtools.
258
    def test_help_commands(self):
259
        # See https://bugs.launchpad.net/bzr/+bug/663773
260
        self.run_command_check_imports(['help', 'commands'], [
261
            'testtools',
262
            ])
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
263
264
    def test_simple_serve(self):
265
        # 'serve' in a default format working tree shouldn't need many modules
266
        tree = self.make_branch_and_tree('.')
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
267
        # Capture the bzr serve process' stderr in a file to avoid deadlocks
268
        # while the smart client interacts with it.
269
        stderr_file = open('bzr-serve.stderr', 'w')
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
270
        process = self.start_bzr_subprocess_with_import_check(['serve',
7143.15.2 by Jelmer Vernooij
Run autopep8.
271
                                                               '--inet', '-d', tree.basedir], stderr_file=stderr_file)
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
272
        url = 'bzr://localhost/'
273
        self.permit_url(url)
274
        client_medium = medium.SmartSimplePipesClientMedium(
275
            process.stdout, process.stdin, url)
276
        transport = remote.RemoteTransport(url, medium=client_medium)
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
277
        branch = ControlDir.open_from_transport(transport).open_branch()
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
278
        process.stdin.close()
279
        # Hide stdin from the subprocess module, so it won't fail to close it.
280
        process.stdin = None
281
        (out, err) = self.finish_bzr_subprocess(process,
7143.15.2 by Jelmer Vernooij
Run autopep8.
282
                                                universal_newlines=False)
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
283
        stderr_file.close()
7058.3.8 by Jelmer Vernooij
Fix import tariff tests.
284
        with open('bzr-serve.stderr', 'rb') as stderr_file:
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
285
            err = stderr_file.read()
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
286
        self.check_forbidden_modules(err,
7143.15.2 by Jelmer Vernooij
Run autopep8.
287
                                     ['breezy.annotate',
288
                                      'breezy.atomicfile',
289
                                      'breezy.bugtracker',
290
                                      'breezy.bundle.commands',
291
                                      'breezy.cmd_version_info',
292
                                      'breezy.bzr.dirstate',
293
                                      'breezy.bzr._dirstate_helpers_py',
294
                                      'breezy.bzr._dirstate_helpers_pyx',
295
                                      'breezy.externalcommand',
296
                                      'breezy.filters',
297
                                      'breezy.hashcache',
298
                                      # foreign branch plugins import the foreign_vcs_registry from
299
                                      # breezy.foreign so it can't be blacklisted
300
                                      'breezy.gpg',
301
                                      'breezy.info',
302
                                      'breezy.bzr.knit',
303
                                      'breezy.merge3',
304
                                      'breezy.merge_directive',
305
                                      'breezy.msgeditor',
306
                                      'breezy.bzr.remote',
307
                                      'breezy.rules',
308
                                      'breezy.sign_my_commits',
309
                                      'breezy.bzr.smart.client',
310
                                      'breezy.transform',
311
                                      'breezy.version_info_formats.format_rio',
312
                                      'breezy.bzr.workingtree_4',
313
                                      'breezy.bzr.xml_serializer',
314
                                      'breezy.bzr.xml8',
315
                                      'getpass',
316
                                      'kerberos',
317
                                      'smtplib',
318
                                      'tarfile',
319
                                      'tempfile',
320
                                      'termios',
321
                                      'tty',
322
                                      ] + old_format_modules)