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