/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
        """
5304.1.1 by Vincent Ladeuil
Pass BZR_PLUGINS_AT and BZR_DISABLE_PLINGS to the subprocess fpr test_import_tariff
100
        self.addDetail('subprocess_stderr',
5017.2.2 by Martin Pool
Add import tariff tests
101
            content.Content(content.ContentType("text", "plain"),
102
                lambda:[err]))
103
104
        bad_modules = []
105
        for module_name in forbidden_imports:
106
            if err.find("\nimport %s " % module_name) != -1:
107
                bad_modules.append(module_name)
108
109
        if bad_modules:
5816.1.3 by Jelmer Vernooij
Split up tariff functions.
110
            self.fail("command loaded forbidden modules %r"
111
                % (bad_modules,))
112
113
    def finish_bzr_subprocess_with_import_check(self, process,
114
            args, forbidden_imports):
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,
122
            universal_newlines=False, process_args=args)
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,
137
            forbidden_imports)
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'],
148
            ['nonexistentmodulename', 'anothernonexistentmodule'])
149
        self.assertRaises(AssertionError,
150
            self.run_command_check_imports,
151
            ['st'],
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
152
            ['breezy.tree'])
5017.2.2 by Martin Pool
Add import tariff tests
153
154
    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.
155
        # 'st' in a default format working tree shouldn't need many modules
5017.2.2 by Martin Pool
Add import tariff tests
156
        self.make_branch_and_tree('.')
5017.2.4 by Martin Pool
Move or remove some unconditionally loaded code
157
        self.run_command_check_imports(['st'], [
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',
5749.1.1 by Jelmer Vernooij
Remove bzrlib.foreign from the test_import_tariff blacklist.
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',
174
            'breezy.patiencediff',
6670.4.14 by Jelmer Vernooij
Move remote to breezy.bzr.
175
            'breezy.bzr.remote',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
176
            'breezy.rules',
177
            'breezy.sign_my_commits',
6670.4.16 by Jelmer Vernooij
Move smart to breezy.bzr.
178
            'breezy.bzr.smart',
179
            'breezy.bzr.smart.client',
180
            'breezy.bzr.smart.medium',
181
            'breezy.bzr.smart.server',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
182
            'breezy.transform',
183
            'breezy.version_info_formats.format_rio',
6670.4.12 by Jelmer Vernooij
Move inventorytree to breezy.bzr.
184
            'breezy.bzr.xml_serializer',
185
            'breezy.bzr.xml8',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
186
            'getpass',
5271.1.1 by Martin Pool
Test that Kerberos is no longer loaded.
187
            'kerberos',
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',
5753.2.2 by Jelmer Vernooij
Remove some unnecessary imports, clean up lazy imports.
192
            'tempfile',
6379.1.1 by Jelmer Vernooij
Avoid importing tty and termios if we don't have to.
193
            'termios',
194
            'tty',
6379.4.3 by Jelmer Vernooij
Check for urllib in tariff test.
195
            'urllib',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
196
            ] + old_format_modules)
5127.1.2 by Martin Pool
Lazy-load conflict commands
197
        # TODO: similar test for repository-only operations, checking we avoid
198
        # loading wt-specific stuff
199
        #
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
200
        # See https://bugs.launchpad.net/bzr/+bug/553017
5510.1.1 by Martin von Gagern
Ensure 'bzr help commands' doesn't import testtools.
201
202
    def test_help_commands(self):
203
        # See https://bugs.launchpad.net/bzr/+bug/663773
204
        self.run_command_check_imports(['help', 'commands'], [
205
            'testtools',
206
            ])
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
207
208
    def test_simple_serve(self):
209
        # 'serve' in a default format working tree shouldn't need many modules
210
        tree = self.make_branch_and_tree('.')
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
211
        # Capture the bzr serve process' stderr in a file to avoid deadlocks
212
        # while the smart client interacts with it.
213
        stderr_file = open('bzr-serve.stderr', 'w')
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
214
        process = self.start_bzr_subprocess_with_import_check(['serve',
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
215
            '--inet', '-d', tree.basedir], stderr_file=stderr_file)
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
216
        url = 'bzr://localhost/'
217
        self.permit_url(url)
218
        client_medium = medium.SmartSimplePipesClientMedium(
219
            process.stdout, process.stdin, url)
220
        transport = remote.RemoteTransport(url, medium=client_medium)
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
221
        branch = ControlDir.open_from_transport(transport).open_branch()
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
222
        process.stdin.close()
223
        # Hide stdin from the subprocess module, so it won't fail to close it.
224
        process.stdin = None
225
        (out, err) = self.finish_bzr_subprocess(process,
226
            universal_newlines=False)
5898.2.1 by Andrew Bennetts
Fix deadlock in TestImportTariffs.test_simple_serve.
227
        stderr_file.close()
228
        with open('bzr-serve.stderr', 'r') as stderr_file:
229
            err = stderr_file.read()
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
230
        self.check_forbidden_modules(err,
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
231
            ['breezy.annotate',
232
            'breezy.atomicfile',
233
            'breezy.bugtracker',
234
            'breezy.bundle.commands',
235
            'breezy.cmd_version_info',
6670.4.12 by Jelmer Vernooij
Move inventorytree to breezy.bzr.
236
            'breezy.bzr.dirstate',
237
            'breezy.bzr._dirstate_helpers_py',
238
            'breezy.bzr._dirstate_helpers_pyx',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
239
            'breezy.externalcommand',
240
            'breezy.filters',
241
            'breezy.hashcache',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
242
            # foreign branch plugins import the foreign_vcs_registry from 
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
243
            # breezy.foreign so it can't be blacklisted
244
            'breezy.gpg',
245
            'breezy.info',
6670.4.12 by Jelmer Vernooij
Move inventorytree to breezy.bzr.
246
            'breezy.bzr.knit',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
247
            'breezy.merge3',
248
            'breezy.merge_directive',
249
            'breezy.msgeditor',
250
            'breezy.patiencediff',
6670.4.14 by Jelmer Vernooij
Move remote to breezy.bzr.
251
            'breezy.bzr.remote',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
252
            'breezy.rules',
253
            'breezy.sign_my_commits',
6670.4.16 by Jelmer Vernooij
Move smart to breezy.bzr.
254
            'breezy.bzr.smart.client',
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
255
            'breezy.transform',
256
            'breezy.version_info_formats.format_rio',
6670.4.12 by Jelmer Vernooij
Move inventorytree to breezy.bzr.
257
            'breezy.bzr.workingtree_4',
258
            'breezy.bzr.xml_serializer',
259
            'breezy.bzr.xml8',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
260
            'getpass',
261
            'kerberos',
262
            'smtplib',
263
            'tarfile',
264
            'tempfile',
6379.1.1 by Jelmer Vernooij
Avoid importing tty and termios if we don't have to.
265
            'termios',
266
            'tty',
5816.1.4 by Jelmer Vernooij
Add import tariff test for the smart server.
267
            ] + old_format_modules)