~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_import_tariff.py

  • Committer: Vincent Ladeuil
  • Date: 2011-07-06 09:22:00 UTC
  • mfrom: (6008 +trunk)
  • mto: (6012.1.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 6013.
  • Revision ID: v.ladeuil+lp@free.fr-20110706092200-7iai2mwzc0sqdsvf
MergingĀ inĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
"""Tests for how many modules are loaded in executing various commands."""
19
19
 
20
20
import os
 
21
 
21
22
from testtools import content
22
23
 
 
24
from bzrlib import (
 
25
    plugins as _mod_plugins,
 
26
    trace,
 
27
    )
 
28
from bzrlib.bzrdir import BzrDir
 
29
from bzrlib.smart import medium
 
30
from bzrlib.transport import remote
 
31
 
23
32
from bzrlib.plugin import (
24
33
    are_plugins_disabled,
25
34
    )
28
37
    TestCaseWithTransport,
29
38
    )
30
39
 
31
 
 
32
 
class TestImportTariffs(TestCaseWithTransport):
 
40
old_format_modules = [
 
41
    'bzrlib.repofmt.knitrepo',
 
42
    'bzrlib.repofmt.knitpack_repo',
 
43
    'bzrlib.plugins.weave_fmt.branch',
 
44
    'bzrlib.plugins.weave_fmt.bzrdir',
 
45
    'bzrlib.plugins.weave_fmt.repository',
 
46
    'bzrlib.plugins.weave_fmt.workingtree',
 
47
    'bzrlib.weave',
 
48
    'bzrlib.weavefile',
 
49
    'bzrlib.xml4',
 
50
    'bzrlib.xml5',
 
51
    'bzrlib.xml6',
 
52
    'bzrlib.xml7',
 
53
    ]
 
54
 
 
55
 
 
56
class ImportTariffTestCase(TestCaseWithTransport):
33
57
    """Check how many modules are loaded for some representative scenarios.
34
58
 
35
59
    See the Testing Guide in the developer documentation for more explanation.
 
60
 
 
61
 
 
62
    We must respect the setup used by the selftest command regarding
 
63
    plugins. This allows the user to control which plugins are in effect while
 
64
    running these tests and respect the import policies defined here.
 
65
 
 
66
    When failures are encountered for a given plugin, they can generally be
 
67
    addressed by using lazy import or lazy hook registration.
36
68
    """
37
69
 
38
70
    def setUp(self):
39
 
        # Preserve some env vars as we want to escape the isolation for them
40
71
        self.preserved_env_vars = {}
41
 
        for name in ('BZR_HOME', 'BZR_PLUGIN_PATH', 'BZR_DISABLE_PLUGINS',
42
 
                     'BZR_PLUGINS_AT', 'HOME'):
 
72
        for name in ('BZR_PLUGIN_PATH', 'BZR_DISABLE_PLUGINS', 'BZR_PLUGINS_AT'
 
73
                     ):
43
74
            self.preserved_env_vars[name] = os.environ.get(name)
44
 
        super(TestImportTariffs, self).setUp()
 
75
        super(ImportTariffTestCase, self).setUp()
45
76
 
46
 
    def run_command_check_imports(self, args, forbidden_imports):
47
 
        """Run bzr ARGS in a subprocess and check its imports.
 
77
    def start_bzr_subprocess_with_import_check(self, args, stderr_file=None):
 
78
        """Run a bzr process and capture the imports.
48
79
 
49
80
        This is fairly expensive because we start a subprocess, so we aim to
50
81
        cover representative rather than exhaustive cases.
51
 
 
52
 
        :param forbidden_imports: List of fully-qualified Python module names
53
 
            that should not be loaded while running this command.
54
82
        """
55
 
        # We use PYTHON_VERBOSE rather than --profile-importts because in
 
83
        # We use PYTHON_VERBOSE rather than --profile-imports because in
56
84
        # experimentation the profile-imports output seems to not always show
57
85
        # the modules you'd expect; this can be debugged but python -v seems
58
86
        # more likely to always show everything.  And we use the environment
59
87
        # variable rather than 'python -v' in the hope it will work even if
60
88
        # bzr is frozen and python is not explicitly specified. -- mbp 20100208
61
 
 
62
 
        # Normally we want test isolation from the real $HOME but here we
63
 
        # explicitly do want to test against things installed there, therefore
64
 
        # we pass it through.
65
89
        env_changes = dict(PYTHONVERBOSE='1', **self.preserved_env_vars)
66
 
        out, err = self.run_bzr_subprocess(args,
67
 
            allow_plugins=(not are_plugins_disabled()),
68
 
            env_changes=env_changes)
69
 
 
 
90
        trace.mutter('Setting env for bzr subprocess: %r', env_changes)
 
91
        kwargs = dict(env_changes=env_changes,
 
92
                      allow_plugins=(not are_plugins_disabled()))
 
93
        if stderr_file:
 
94
            # We don't want to update the whole call chain so we insert stderr
 
95
            # *iff* we need to
 
96
            kwargs['stderr'] = stderr_file
 
97
        return self.start_bzr_subprocess(args, **kwargs)
 
98
 
 
99
    def check_forbidden_modules(self, err, forbidden_imports):
 
100
        """Check for forbidden modules in stderr.
 
101
 
 
102
        :param err: Standard error
 
103
        :param forbidden_imports: List of forbidden modules
 
104
        """
70
105
        self.addDetail('subprocess_stderr',
71
106
            content.Content(content.ContentType("text", "plain"),
72
107
                lambda:[err]))
77
112
                bad_modules.append(module_name)
78
113
 
79
114
        if bad_modules:
80
 
            self.fail("command %r loaded forbidden modules %r"
81
 
                % (args, bad_modules))
 
115
            self.fail("command loaded forbidden modules %r"
 
116
                % (bad_modules,))
 
117
 
 
118
    def finish_bzr_subprocess_with_import_check(self, process,
 
119
            args, forbidden_imports):
 
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,
 
127
            universal_newlines=False, process_args=args)
 
128
        self.check_forbidden_modules(err, forbidden_imports)
82
129
        return out, err
83
130
 
 
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,
 
142
            forbidden_imports)
 
143
 
 
144
 
 
145
class TestImportTariffs(ImportTariffTestCase):
 
146
    """Basic import tariff tests for some common bzr commands"""
 
147
 
84
148
    def test_import_tariffs_working(self):
85
149
        # check some guaranteed-true and false imports to be sure we're
86
150
        # measuring correctly
93
157
            ['bzrlib.tree'])
94
158
 
95
159
    def test_simple_local(self):
96
 
        # 'st' in a working tree shouldn't need many modules
 
160
        # 'st' in a default format working tree shouldn't need many modules
97
161
        self.make_branch_and_tree('.')
98
162
        self.run_command_check_imports(['st'], [
 
163
            'bzrlib.annotate',
 
164
            'bzrlib.atomicfile',
 
165
            'bzrlib.bugtracker',
99
166
            'bzrlib.bundle.commands',
100
167
            'bzrlib.cmd_version_info',
101
 
            'bzrlib.foreign',
 
168
            'bzrlib.externalcommand',
 
169
            'bzrlib.filters',
 
170
            # foreign branch plugins import the foreign_vcs_registry from 
 
171
            # bzrlib.foreign so it can't be blacklisted
 
172
            'bzrlib.gpg',
 
173
            'bzrlib.info',
 
174
            'bzrlib.knit',
102
175
            'bzrlib.merge3',
 
176
            'bzrlib.merge_directive',
 
177
            'bzrlib.msgeditor',
103
178
            'bzrlib.patiencediff',
104
179
            'bzrlib.remote',
 
180
            'bzrlib.rules',
105
181
            'bzrlib.sign_my_commits',
106
182
            'bzrlib.smart',
 
183
            'bzrlib.smart.client',
 
184
            'bzrlib.smart.medium',
 
185
            'bzrlib.smart.server',
107
186
            'bzrlib.transform',
 
187
            'bzrlib.version_info_formats.format_rio',
 
188
            'getpass',
108
189
            'kerberos',
109
190
            'smtplib',
110
191
            'tarfile',
111
 
            ])
 
192
            'tempfile',
 
193
            ] + old_format_modules)
112
194
        # TODO: similar test for repository-only operations, checking we avoid
113
195
        # loading wt-specific stuff
114
196
        #
119
201
        self.run_command_check_imports(['help', 'commands'], [
120
202
            'testtools',
121
203
            ])
 
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('.')
 
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')
 
211
        process = self.start_bzr_subprocess_with_import_check(['serve',
 
212
            '--inet', '-d', tree.basedir], stderr_file=stderr_file)
 
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)
 
224
        stderr_file.close()
 
225
        with open('bzr-serve.stderr', 'r') as stderr_file:
 
226
            err = stderr_file.read()
 
227
        self.check_forbidden_modules(err,
 
228
            ['bzrlib.annotate',
 
229
            'bzrlib.atomicfile',
 
230
            'bzrlib.bugtracker',
 
231
            'bzrlib.bundle.commands',
 
232
            'bzrlib.cmd_version_info',
 
233
            'bzrlib.dirstate',
 
234
            'bzrlib._dirstate_helpers_py',
 
235
            'bzrlib._dirstate_helpers_pyx',
 
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',
 
253
            'bzrlib.workingtree_4',
 
254
            'getpass',
 
255
            'kerberos',
 
256
            'smtplib',
 
257
            'tarfile',
 
258
            'tempfile',
 
259
            ] + old_format_modules)