~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Vincent Ladeuil
  • Date: 2009-10-27 14:04:29 UTC
  • mto: (4772.1.1 integration)
  • mto: This revision was merged to the branch mainline in revision 4773.
  • Revision ID: v.ladeuil+lp@free.fr-20091027140429-br7sqcmk3zstngmm
Fix some typos.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 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
18
18
"""Tests of the bzr serve command."""
19
19
 
20
20
import os
 
21
import os.path
21
22
import signal
 
23
import subprocess
22
24
import sys
23
25
import thread
24
26
import threading
28
30
    errors,
29
31
    osutils,
30
32
    revision as _mod_revision,
31
 
    trace,
32
 
    transport,
33
 
    urlutils,
34
33
    )
35
34
from bzrlib.branch import Branch
36
35
from bzrlib.bzrdir import BzrDir
37
36
from bzrlib.smart import client, medium
38
 
from bzrlib.smart.server import (
39
 
    BzrServerFactory,
40
 
    SmartTCPServer,
41
 
    )
 
37
from bzrlib.smart.server import BzrServerFactory, SmartTCPServer
42
38
from bzrlib.tests import (
 
39
    ParamikoFeature,
43
40
    TestCaseWithMemoryTransport,
44
41
    TestCaseWithTransport,
 
42
    TestSkipped,
45
43
    )
46
 
from bzrlib.transport import remote
47
 
 
48
 
 
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
 
        def on_server_start_thread(tcp_server):
61
 
            """This runs concurrently with the server thread.
62
 
 
63
 
            The server is interrupted as soon as ``func`` finishes, even if an
64
 
            exception is encountered.
65
 
            """
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
 
                        trace.mutter('func broke: %r', e)
76
 
            finally:
77
 
                # Then stop the server
78
 
                trace.mutter('interrupting...')
79
 
                thread.interrupt_main()
80
 
        # When the hook is fired, it just starts ``on_server_start_thread`` and
81
 
        # return
82
 
        def on_server_start(backing_urls, tcp_server):
83
 
            t = threading.Thread(
84
 
                target=on_server_start_thread, args=(tcp_server,))
85
 
            t.start()
86
 
        # install hook
87
 
        SmartTCPServer.hooks.install_named_hook(
88
 
            'server_started_ex', on_server_start,
89
 
            'run_bzr_serve_then_func hook')
90
 
        # start a TCP server
91
 
        try:
92
 
            out, err = self.run_bzr(['serve'] + list(serve_args),
93
 
                                    retcode=retcode)
94
 
        except KeyboardInterrupt, e:
95
 
            out, err = e.args
96
 
        return out, err
97
 
 
98
 
 
99
 
class TestBzrServe(TestBzrServeBase):
 
44
from bzrlib.trace import mutter
 
45
from bzrlib.transport import get_transport, remote
 
46
 
 
47
 
 
48
class TestBzrServe(TestCaseWithTransport):
100
49
 
101
50
    def setUp(self):
102
51
        super(TestBzrServe, self).setUp()
103
52
        self.disable_missing_extensions_warning()
104
53
 
105
 
    def test_server_exception_with_hook(self):
106
 
        """Catch exception from the server in the server_exception hook.
107
 
 
108
 
        We use ``run_bzr_serve_then_func`` without a ``func`` so the server
109
 
        will receive a KeyboardInterrupt exception we want to catch.
110
 
        """
111
 
        def hook(exception):
112
 
            if exception[0] is KeyboardInterrupt:
113
 
                sys.stderr.write('catching KeyboardInterrupt\n')
114
 
                return True
115
 
            else:
116
 
                return False
117
 
        SmartTCPServer.hooks.install_named_hook(
118
 
            'server_exception', hook,
119
 
            'test_server_except_hook hook')
120
 
        args = ['--port', 'localhost:0', '--quiet']
121
 
        out, err = self.run_bzr_serve_then_func(args, retcode=0)
122
 
        self.assertEqual('catching KeyboardInterrupt\n', err)
123
 
 
124
 
    def test_server_exception_no_hook(self):
125
 
        """test exception without hook returns error"""
126
 
        args = []
127
 
        out, err = self.run_bzr_serve_then_func(args, retcode=3)
128
 
 
129
54
    def assertInetServerShutsdownCleanly(self, process):
130
55
        """Shutdown the server process looking for errors."""
131
56
        # Shutdown the server: the server should shut down when it cannot read
163
88
            finish_bzr_subprocess, a client for the server, and a transport.
164
89
        """
165
90
        # Serve from the current directory
166
 
        args = ['serve', '--inet']
167
 
        args.extend(extra_options)
168
 
        process = self.start_bzr_subprocess(args)
 
91
        process = self.start_bzr_subprocess(['serve', '--inet'])
169
92
 
170
93
        # Connect to the server
171
94
        # We use this url because while this is no valid URL to connect to this
196
119
        self.permit_url(url)
197
120
        return process, url
198
121
 
199
 
    def test_bzr_serve_quiet(self):
200
 
        self.make_branch('.')
201
 
        args = ['--port', 'localhost:0', '--quiet']
202
 
        out, err = self.run_bzr_serve_then_func(args, retcode=3)
203
 
        self.assertEqual('', out)
204
 
        self.assertEqual('', err)
205
 
 
206
122
    def test_bzr_serve_inet_readonly(self):
207
123
        """bzr server should provide a read only filesystem by default."""
208
124
        process, transport = self.start_server_inet()
215
131
 
216
132
        process, transport = self.start_server_inet(['--allow-writes'])
217
133
 
218
 
        # We get a working branch, and can create a directory
 
134
        # We get a working branch
219
135
        branch = BzrDir.open_from_transport(transport).open_branch()
220
136
        self.make_read_requests(branch)
221
 
        transport.mkdir('adir')
222
137
        self.assertInetServerShutsdownCleanly(process)
223
138
 
224
139
    def test_bzr_serve_port_readonly(self):
225
140
        """bzr server should provide a read only filesystem by default."""
226
141
        process, url = self.start_server_port()
227
 
        t = transport.get_transport(url)
228
 
        self.assertRaises(errors.TransportNotPossible, t.mkdir, 'adir')
 
142
        transport = get_transport(url)
 
143
        self.assertRaises(errors.TransportNotPossible, transport.mkdir, 'adir')
229
144
        self.assertServerFinishesCleanly(process)
230
145
 
231
146
    def test_bzr_serve_port_readwrite(self):
251
166
        self.make_read_requests(branch)
252
167
        self.assertServerFinishesCleanly(process)
253
168
 
254
 
    def test_bzr_serve_dhpss(self):
255
 
        # This is a smoke test that the server doesn't crash when run with
256
 
        # -Dhpss, and does drop some hpss logging to the file.
257
 
        self.make_branch('.')
258
 
        log_fname = os.getcwd() + '/server.log'
259
 
        self.overrideEnv('BZR_LOG', log_fname)
260
 
        process, transport = self.start_server_inet(['-Dhpss'])
261
 
        branch = BzrDir.open_from_transport(transport).open_branch()
262
 
        self.make_read_requests(branch)
263
 
        self.assertInetServerShutsdownCleanly(process)
264
 
        f = open(log_fname, 'rb')
265
 
        content = f.read()
266
 
        f.close()
267
 
        self.assertContainsRe(content, r'hpss request: \[[0-9-]+\]')
268
 
 
269
 
 
270
 
class TestCmdServeChrooting(TestBzrServeBase):
 
169
 
 
170
class TestCmdServeChrooting(TestCaseWithTransport):
271
171
 
272
172
    def test_serve_tcp(self):
273
173
        """'bzr serve' wraps the given --directory in a ChrootServer.
282
182
            ['--port', '127.0.0.1:0',
283
183
             '--directory', t.local_abspath('server-root'),
284
184
             '--allow-writes'],
285
 
            func=self.when_server_started)
 
185
            self.when_server_started)
286
186
        # The when_server_started method issued a find_repositoryV3 that should
287
187
        # fail with 'norepository' because there are no repositories inside the
288
188
        # --directory.
289
189
        self.assertEqual(('norepository',), self.client_resp)
290
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
 
291
226
    def when_server_started(self):
292
227
        # Connect to the TCP server and issue some requests and see what comes
293
228
        # back.
307
242
 
308
243
class TestUserdirExpansion(TestCaseWithMemoryTransport):
309
244
 
310
 
    @staticmethod
311
 
    def fake_expanduser(path):
 
245
    def fake_expanduser(self, path):
312
246
        """A simple, environment-independent, function for the duration of this
313
247
        test.
314
248
 
320
254
        return path
321
255
 
322
256
    def make_test_server(self, base_path='/'):
323
 
        """Make and start a BzrServerFactory, backed by a memory transport, and
 
257
        """Make and setUp a BzrServerFactory, backed by a memory transport, and
324
258
        creat '/home/user' in that transport.
325
259
        """
326
260
        bzr_server = BzrServerFactory(
344
278
        (optionally decorated with 'readonly+').  BzrServerFactory can
345
279
        determine the original --directory from that transport.
346
280
        """
347
 
        # URLs always include the trailing slash, and get_base_path returns it
348
 
        base_dir = osutils.abspath('/a/b/c') + '/'
349
 
        base_url = urlutils.local_path_to_url(base_dir) + '/'
350
281
        # Define a fake 'protocol' to capture the transport that cmd_serve
351
282
        # passes to serve_bzr.
352
283
        def capture_transport(transport, host, port, inet):
353
284
            self.bzr_serve_transport = transport
354
285
        cmd = builtins.cmd_serve()
355
286
        # Read-only
356
 
        cmd.run(directory=base_dir, protocol=capture_transport)
 
287
        cmd.run(directory='/a/b/c', protocol=capture_transport)
357
288
        server_maker = BzrServerFactory()
358
289
        self.assertEqual(
359
 
            'readonly+%s' % base_url, self.bzr_serve_transport.base)
 
290
            'readonly+file:///a/b/c/', self.bzr_serve_transport.base)
360
291
        self.assertEqual(
361
 
            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))
362
293
        # Read-write
363
 
        cmd.run(directory=base_dir, protocol=capture_transport,
 
294
        cmd.run(directory='/a/b/c', protocol=capture_transport,
364
295
            allow_writes=True)
365
296
        server_maker = BzrServerFactory()
366
 
        self.assertEqual(base_url, self.bzr_serve_transport.base)
367
 
        self.assertEqual(base_dir,
368
 
            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))
 
300