~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/blackbox/test_serve.py

Speed up a slow memory-hungry test that doesn't need to be.
Rather than allocating a 26MB string, allocate a long list and call writelines.
Also, write 5MB rather tan 26MB, rollover is at 4MB anyway. (less I/O)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Canonical Ltd
 
1
# Copyright (C) 2006 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
27
27
 
28
28
from bzrlib import (
29
29
    builtins,
30
 
    debug,
31
30
    errors,
32
31
    osutils,
33
32
    revision as _mod_revision,
34
 
    urlutils,
35
33
    )
36
34
from bzrlib.branch import Branch
37
35
from bzrlib.bzrdir import BzrDir
38
36
from bzrlib.smart import client, medium
39
37
from bzrlib.smart.server import BzrServerFactory, SmartTCPServer
40
38
from bzrlib.tests import (
 
39
    ParamikoFeature,
41
40
    TestCaseWithMemoryTransport,
42
41
    TestCaseWithTransport,
43
42
    TestSkipped,
46
45
from bzrlib.transport import get_transport, remote
47
46
 
48
47
 
49
 
class TestBzrServeBase(TestCaseWithTransport):
50
 
 
51
 
    def run_bzr_serve_then_func(self, serve_args, retcode=0, func=None,
52
 
                                *func_args, **func_kwargs):
53
 
        """Run 'bzr serve', and run the given func in a thread once the server
54
 
        has started.
55
 
        
56
 
        When 'func' terminates, the server will be terminated too.
57
 
        
58
 
        Returns stdout and stderr.
59
 
        """
60
 
        # install hook
61
 
        def on_server_start(backing_urls, tcp_server):
62
 
            t = threading.Thread(
63
 
                target=on_server_start_thread, args=(tcp_server,))
64
 
            t.start()
65
 
        def on_server_start_thread(tcp_server):
66
 
            try:
67
 
                # Run func if set
68
 
                self.tcp_server = tcp_server
69
 
                if not func is None:
70
 
                    try:
71
 
                        func(*func_args, **func_kwargs)
72
 
                    except Exception, e:
73
 
                        # Log errors to make some test failures a little less
74
 
                        # mysterious.
75
 
                        mutter('func broke: %r', e)
76
 
            finally:
77
 
                # Then stop the server
78
 
                mutter('interrupting...')
79
 
                thread.interrupt_main()
80
 
        SmartTCPServer.hooks.install_named_hook(
81
 
            'server_started_ex', on_server_start,
82
 
            'run_bzr_serve_then_func hook')
83
 
        # start a TCP server
84
 
        try:
85
 
            out, err = self.run_bzr(['serve'] + list(serve_args))
86
 
        except KeyboardInterrupt, e:
87
 
            out, err = e.args
88
 
        return out, err
89
 
 
90
 
 
91
 
class TestBzrServe(TestBzrServeBase):
 
48
class TestBzrServe(TestCaseWithTransport):
92
49
 
93
50
    def setUp(self):
94
51
        super(TestBzrServe, self).setUp()
131
88
            finish_bzr_subprocess, a client for the server, and a transport.
132
89
        """
133
90
        # Serve from the current directory
134
 
        args = ['serve', '--inet']
135
 
        args.extend(extra_options)
136
 
        process = self.start_bzr_subprocess(args)
 
91
        process = self.start_bzr_subprocess(['serve', '--inet'])
137
92
 
138
93
        # Connect to the server
139
94
        # We use this url because while this is no valid URL to connect to this
163
118
        url = 'bzr://localhost:%d/' % port
164
119
        self.permit_url(url)
165
120
        return process, url
166
 
    
167
 
    def test_bzr_serve_quiet(self):
168
 
        self.make_branch('.')
169
 
        args = ['--port', 'localhost:0', '--quiet']
170
 
        out, err = self.run_bzr_serve_then_func(args, retcode=3)
171
 
        self.assertEqual('', out)
172
 
        self.assertEqual('', err)
173
121
 
174
122
    def test_bzr_serve_inet_readonly(self):
175
123
        """bzr server should provide a read only filesystem by default."""
183
131
 
184
132
        process, transport = self.start_server_inet(['--allow-writes'])
185
133
 
186
 
        # We get a working branch, and can create a directory
 
134
        # We get a working branch
187
135
        branch = BzrDir.open_from_transport(transport).open_branch()
188
136
        self.make_read_requests(branch)
189
 
        transport.mkdir('adir')
190
137
        self.assertInetServerShutsdownCleanly(process)
191
138
 
192
139
    def test_bzr_serve_port_readonly(self):
219
166
        self.make_read_requests(branch)
220
167
        self.assertServerFinishesCleanly(process)
221
168
 
222
 
    def test_bzr_serve_dhpss(self):
223
 
        # This is a smoke test that the server doesn't crash when run with
224
 
        # -Dhpss, and does drop some hpss logging to the file.
225
 
        self.make_branch('.')
226
 
        log_fname = os.getcwd() + '/server.log'
227
 
        self._captureVar('BZR_LOG', log_fname)
228
 
        process, transport = self.start_server_inet(['-Dhpss'])
229
 
        branch = BzrDir.open_from_transport(transport).open_branch()
230
 
        self.make_read_requests(branch)
231
 
        self.assertInetServerShutsdownCleanly(process)
232
 
        f = open(log_fname, 'rb')
233
 
        content = f.read()
234
 
        f.close()
235
 
        self.assertContainsRe(content, r'hpss request: \[[0-9-]+\]')
236
 
 
237
 
 
238
 
class TestCmdServeChrooting(TestBzrServeBase):
 
169
 
 
170
class TestCmdServeChrooting(TestCaseWithTransport):
239
171
 
240
172
    def test_serve_tcp(self):
241
173
        """'bzr serve' wraps the given --directory in a ChrootServer.
250
182
            ['--port', '127.0.0.1:0',
251
183
             '--directory', t.local_abspath('server-root'),
252
184
             '--allow-writes'],
253
 
            func=self.when_server_started)
 
185
            self.when_server_started)
254
186
        # The when_server_started method issued a find_repositoryV3 that should
255
187
        # fail with 'norepository' because there are no repositories inside the
256
188
        # --directory.
257
189
        self.assertEqual(('norepository',), self.client_resp)
258
190
 
 
191
    def run_bzr_serve_then_func(self, serve_args, func, *func_args,
 
192
            **func_kwargs):
 
193
        """Run 'bzr serve', and run the given func in a thread once the server
 
194
        has started.
 
195
        
 
196
        When 'func' terminates, the server will be terminated too.
 
197
        """
 
198
        # install hook
 
199
        def on_server_start(backing_urls, tcp_server):
 
200
            t = threading.Thread(
 
201
                target=on_server_start_thread, args=(tcp_server,))
 
202
            t.start()
 
203
        def on_server_start_thread(tcp_server):
 
204
            try:
 
205
                # Run func
 
206
                self.tcp_server = tcp_server
 
207
                try:
 
208
                    func(*func_args, **func_kwargs)
 
209
                except Exception, e:
 
210
                    # Log errors to make some test failures a little less
 
211
                    # mysterious.
 
212
                    mutter('func broke: %r', e)
 
213
            finally:
 
214
                # Then stop the server
 
215
                mutter('interrupting...')
 
216
                thread.interrupt_main()
 
217
        SmartTCPServer.hooks.install_named_hook(
 
218
            'server_started_ex', on_server_start,
 
219
            'run_bzr_serve_then_func hook')
 
220
        # start a TCP server
 
221
        try:
 
222
            self.run_bzr(['serve'] + list(serve_args))
 
223
        except KeyboardInterrupt:
 
224
            pass
 
225
 
259
226
    def when_server_started(self):
260
227
        # Connect to the TCP server and issue some requests and see what comes
261
228
        # back.
287
254
        return path
288
255
 
289
256
    def make_test_server(self, base_path='/'):
290
 
        """Make and start a BzrServerFactory, backed by a memory transport, and
 
257
        """Make and setUp a BzrServerFactory, backed by a memory transport, and
291
258
        creat '/home/user' in that transport.
292
259
        """
293
260
        bzr_server = BzrServerFactory(
311
278
        (optionally decorated with 'readonly+').  BzrServerFactory can
312
279
        determine the original --directory from that transport.
313
280
        """
314
 
        # URLs always include the trailing slash, and get_base_path returns it
315
 
        base_dir = osutils.abspath('/a/b/c') + '/'
316
 
        base_url = urlutils.local_path_to_url(base_dir) + '/'
317
281
        # Define a fake 'protocol' to capture the transport that cmd_serve
318
282
        # passes to serve_bzr.
319
283
        def capture_transport(transport, host, port, inet):
320
284
            self.bzr_serve_transport = transport
321
285
        cmd = builtins.cmd_serve()
322
286
        # Read-only
323
 
        cmd.run(directory=base_dir, protocol=capture_transport)
 
287
        cmd.run(directory='/a/b/c', protocol=capture_transport)
324
288
        server_maker = BzrServerFactory()
325
289
        self.assertEqual(
326
 
            'readonly+%s' % base_url, self.bzr_serve_transport.base)
 
290
            'readonly+file:///a/b/c/', self.bzr_serve_transport.base)
327
291
        self.assertEqual(
328
 
            base_dir, server_maker.get_base_path(self.bzr_serve_transport))
 
292
            u'/a/b/c/', server_maker.get_base_path(self.bzr_serve_transport))
329
293
        # Read-write
330
 
        cmd.run(directory=base_dir, protocol=capture_transport,
 
294
        cmd.run(directory='/a/b/c', protocol=capture_transport,
331
295
            allow_writes=True)
332
296
        server_maker = BzrServerFactory()
333
 
        self.assertEqual(base_url, self.bzr_serve_transport.base)
334
 
        self.assertEqual(base_dir,
335
 
            server_maker.get_base_path(self.bzr_serve_transport))
 
297
        self.assertEqual('file:///a/b/c/', self.bzr_serve_transport.base)
 
298
        self.assertEqual(
 
299
            u'/a/b/c/', server_maker.get_base_path(self.bzr_serve_transport))
336
300