~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

(jameinel) Allow 'bzr serve' to interpret SIGHUP as a graceful shutdown.
 (bug #795025) (John A Meinel)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2011 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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
 
18
18
"""Tests of the bzr serve command."""
19
19
 
20
20
import os
21
21
import signal
22
 
import subprocess
23
22
import sys
 
23
import thread
24
24
import threading
 
25
import time
25
26
 
26
27
from bzrlib import (
 
28
    builtins,
 
29
    config,
27
30
    errors,
28
31
    osutils,
29
32
    revision as _mod_revision,
 
33
    trace,
 
34
    transport,
 
35
    urlutils,
30
36
    )
31
37
from bzrlib.branch import Branch
32
38
from bzrlib.bzrdir import BzrDir
33
 
from bzrlib.errors import ParamikoNotPresent
34
 
from bzrlib.smart import medium
35
 
from bzrlib.tests import TestCaseWithTransport, TestSkipped
36
 
from bzrlib.transport import get_transport, remote
37
 
 
38
 
 
39
 
class TestBzrServe(TestCaseWithTransport):
 
39
from bzrlib.smart import client, medium
 
40
from bzrlib.smart.server import (
 
41
    BzrServerFactory,
 
42
    SmartTCPServer,
 
43
    )
 
44
from bzrlib.tests import (
 
45
    TestCaseWithMemoryTransport,
 
46
    TestCaseWithTransport,
 
47
    )
 
48
from bzrlib.transport import remote
 
49
 
 
50
 
 
51
class TestBzrServeBase(TestCaseWithTransport):
 
52
 
 
53
    def run_bzr_serve_then_func(self, serve_args, retcode=0, func=None,
 
54
                                *func_args, **func_kwargs):
 
55
        """Run 'bzr serve', and run the given func in a thread once the server
 
56
        has started.
 
57
 
 
58
        When 'func' terminates, the server will be terminated too.
 
59
 
 
60
        Returns stdout and stderr.
 
61
        """
 
62
        def on_server_start_thread(tcp_server):
 
63
            """This runs concurrently with the server thread.
 
64
 
 
65
            The server is interrupted as soon as ``func`` finishes, even if an
 
66
            exception is encountered.
 
67
            """
 
68
            try:
 
69
                # Run func if set
 
70
                self.tcp_server = tcp_server
 
71
                if func is not None:
 
72
                    try:
 
73
                        func(*func_args, **func_kwargs)
 
74
                    except Exception, e:
 
75
                        # Log errors to make some test failures a little less
 
76
                        # mysterious.
 
77
                        trace.mutter('func broke: %r', e)
 
78
            finally:
 
79
                # Then stop the server
 
80
                trace.mutter('interrupting...')
 
81
                thread.interrupt_main()
 
82
        # When the hook is fired, it just starts ``on_server_start_thread`` and
 
83
        # return
 
84
        def on_server_start(backing_urls, tcp_server):
 
85
            t = threading.Thread(
 
86
                target=on_server_start_thread, args=(tcp_server,))
 
87
            t.start()
 
88
        # install hook
 
89
        SmartTCPServer.hooks.install_named_hook(
 
90
            'server_started_ex', on_server_start,
 
91
            'run_bzr_serve_then_func hook')
 
92
        # It seesm thread.interrupt_main() will not raise KeyboardInterrupt
 
93
        # until after socket.accept returns. So we set the timeout low to make
 
94
        # the test faster.
 
95
        self.overrideAttr(SmartTCPServer, '_ACCEPT_TIMEOUT', 0.1)
 
96
        # start a TCP server
 
97
        try:
 
98
            out, err = self.run_bzr(['serve'] + list(serve_args),
 
99
                                    retcode=retcode)
 
100
        except KeyboardInterrupt, e:
 
101
            out, err = e.args
 
102
        return out, err
 
103
 
 
104
 
 
105
class TestBzrServe(TestBzrServeBase):
 
106
 
 
107
    def setUp(self):
 
108
        super(TestBzrServe, self).setUp()
 
109
        self.disable_missing_extensions_warning()
 
110
 
 
111
    def test_server_exception_with_hook(self):
 
112
        """Catch exception from the server in the server_exception hook.
 
113
 
 
114
        We use ``run_bzr_serve_then_func`` without a ``func`` so the server
 
115
        will receive a KeyboardInterrupt exception we want to catch.
 
116
        """
 
117
        def hook(exception):
 
118
            if exception[0] is KeyboardInterrupt:
 
119
                sys.stderr.write('catching KeyboardInterrupt\n')
 
120
                return True
 
121
            else:
 
122
                return False
 
123
        SmartTCPServer.hooks.install_named_hook(
 
124
            'server_exception', hook,
 
125
            'test_server_except_hook hook')
 
126
        args = ['--port', 'localhost:0', '--quiet']
 
127
        out, err = self.run_bzr_serve_then_func(args, retcode=0)
 
128
        self.assertEqual('catching KeyboardInterrupt\n', err)
 
129
 
 
130
    def test_server_exception_no_hook(self):
 
131
        """test exception without hook returns error"""
 
132
        args = []
 
133
        out, err = self.run_bzr_serve_then_func(args, retcode=3)
40
134
 
41
135
    def assertInetServerShutsdownCleanly(self, process):
42
136
        """Shutdown the server process looking for errors."""
48
142
        result = self.finish_bzr_subprocess(process)
49
143
        self.assertEqual('', result[0])
50
144
        self.assertEqual('', result[1])
51
 
    
 
145
 
52
146
    def assertServerFinishesCleanly(self, process):
53
147
        """Shutdown the bzr serve instance process looking for errors."""
54
148
        # Shutdown the server
75
169
            finish_bzr_subprocess, a client for the server, and a transport.
76
170
        """
77
171
        # Serve from the current directory
78
 
        process = self.start_bzr_subprocess(['serve', '--inet'])
 
172
        args = ['serve', '--inet']
 
173
        args.extend(extra_options)
 
174
        process = self.start_bzr_subprocess(args)
79
175
 
80
176
        # Connect to the server
81
177
        # We use this url because while this is no valid URL to connect to this
82
178
        # server instance, the transport needs a URL.
83
179
        url = 'bzr://localhost/'
 
180
        self.permit_url(url)
84
181
        client_medium = medium.SmartSimplePipesClientMedium(
85
182
            process.stdout, process.stdin, url)
86
183
        transport = remote.RemoteTransport(url, medium=client_medium)
101
198
        prefix = 'listening on port: '
102
199
        self.assertStartsWith(port_line, prefix)
103
200
        port = int(port_line[len(prefix):])
104
 
        return process,'bzr://localhost:%d/' % port
 
201
        url = 'bzr://localhost:%d/' % port
 
202
        self.permit_url(url)
 
203
        return process, url
 
204
 
 
205
    def test_bzr_serve_quiet(self):
 
206
        self.make_branch('.')
 
207
        args = ['--port', 'localhost:0', '--quiet']
 
208
        out, err = self.run_bzr_serve_then_func(args, retcode=3)
 
209
        self.assertEqual('', out)
 
210
        self.assertEqual('', err)
105
211
 
106
212
    def test_bzr_serve_inet_readonly(self):
107
213
        """bzr server should provide a read only filesystem by default."""
115
221
 
116
222
        process, transport = self.start_server_inet(['--allow-writes'])
117
223
 
118
 
        # We get a working branch
 
224
        # We get a working branch, and can create a directory
119
225
        branch = BzrDir.open_from_transport(transport).open_branch()
120
226
        self.make_read_requests(branch)
 
227
        transport.mkdir('adir')
121
228
        self.assertInetServerShutsdownCleanly(process)
122
229
 
123
230
    def test_bzr_serve_port_readonly(self):
124
231
        """bzr server should provide a read only filesystem by default."""
125
232
        process, url = self.start_server_port()
126
 
        transport = get_transport(url)
127
 
        self.assertRaises(errors.TransportNotPossible, transport.mkdir, 'adir')
 
233
        t = transport.get_transport_from_url(url)
 
234
        self.assertRaises(errors.TransportNotPossible, t.mkdir, 'adir')
128
235
        self.assertServerFinishesCleanly(process)
129
236
 
130
237
    def test_bzr_serve_port_readwrite(self):
138
245
        self.make_read_requests(branch)
139
246
        self.assertServerFinishesCleanly(process)
140
247
 
141
 
    def test_bzr_connect_to_bzr_ssh(self):
142
 
        """User acceptance that get_transport of a bzr+ssh:// behaves correctly.
143
 
 
144
 
        bzr+ssh:// should cause bzr to run a remote bzr smart server over SSH.
145
 
        """
146
 
        try:
147
 
            from bzrlib.transport.sftp import SFTPServer
148
 
        except ParamikoNotPresent:
149
 
            raise TestSkipped('Paramiko not installed')
150
 
        from bzrlib.tests.stub_sftp import StubServer
151
 
        
 
248
    def test_bzr_serve_supports_protocol(self):
152
249
        # Make a branch
153
 
        self.make_branch('a_branch')
154
 
 
155
 
        # Start an SSH server
156
 
        self.command_executed = []
157
 
        # XXX: This is horrible -- we define a really dumb SSH server that
158
 
        # executes commands, and manage the hooking up of stdin/out/err to the
159
 
        # SSH channel ourselves.  Surely this has already been implemented
160
 
        # elsewhere?
161
 
        class StubSSHServer(StubServer):
162
 
 
163
 
            test = self
164
 
 
165
 
            def check_channel_exec_request(self, channel, command):
166
 
                self.test.command_executed.append(command)
167
 
                proc = subprocess.Popen(
168
 
                    command, shell=True, stdin=subprocess.PIPE,
169
 
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
170
 
                
171
 
                # XXX: horribly inefficient, not to mention ugly.
172
 
                # Start a thread for each of stdin/out/err, and relay bytes from
173
 
                # the subprocess to channel and vice versa.
174
 
                def ferry_bytes(read, write, close):
175
 
                    while True:
176
 
                        bytes = read(1)
177
 
                        if bytes == '':
178
 
                            close()
179
 
                            break
180
 
                        write(bytes)
181
 
 
182
 
                file_functions = [
183
 
                    (channel.recv, proc.stdin.write, proc.stdin.close),
184
 
                    (proc.stdout.read, channel.sendall, channel.close),
185
 
                    (proc.stderr.read, channel.sendall_stderr, channel.close)]
186
 
                for read, write, close in file_functions:
187
 
                    t = threading.Thread(
188
 
                        target=ferry_bytes, args=(read, write, close))
189
 
                    t.start()
190
 
 
191
 
                return True
192
 
 
193
 
        ssh_server = SFTPServer(StubSSHServer)
194
 
        # XXX: We *don't* want to override the default SSH vendor, so we set
195
 
        # _vendor to what _get_ssh_vendor returns.
196
 
        ssh_server.setUp()
197
 
        self.addCleanup(ssh_server.tearDown)
198
 
        port = ssh_server._listener.port
199
 
 
200
 
        # Access the branch via a bzr+ssh URL.  The BZR_REMOTE_PATH environment
201
 
        # variable is used to tell bzr what command to run on the remote end.
202
 
        path_to_branch = osutils.abspath('a_branch')
203
 
        
204
 
        orig_bzr_remote_path = os.environ.get('BZR_REMOTE_PATH')
205
 
        bzr_remote_path = self.get_bzr_path()
206
 
        if sys.platform == 'win32':
207
 
            bzr_remote_path = sys.executable + ' ' + self.get_bzr_path()
208
 
        os.environ['BZR_REMOTE_PATH'] = bzr_remote_path
 
250
        self.make_branch('.')
 
251
 
 
252
        process, url = self.start_server_port(['--allow-writes',
 
253
                                               '--protocol=bzr'])
 
254
 
 
255
        # Connect to the server
 
256
        branch = Branch.open(url)
 
257
        self.make_read_requests(branch)
 
258
        self.assertServerFinishesCleanly(process)
 
259
 
 
260
    def test_bzr_serve_dhpss(self):
 
261
        # This is a smoke test that the server doesn't crash when run with
 
262
        # -Dhpss, and does drop some hpss logging to the file.
 
263
        self.make_branch('.')
 
264
        log_fname = os.getcwd() + '/server.log'
 
265
        self.overrideEnv('BZR_LOG', log_fname)
 
266
        process, transport = self.start_server_inet(['-Dhpss'])
 
267
        branch = BzrDir.open_from_transport(transport).open_branch()
 
268
        self.make_read_requests(branch)
 
269
        self.assertInetServerShutsdownCleanly(process)
 
270
        f = open(log_fname, 'rb')
 
271
        content = f.read()
 
272
        f.close()
 
273
        self.assertContainsRe(content, r'hpss request: \[[0-9-]+\]')
 
274
 
 
275
    def test_bzr_serve_supports_configurable_timeout(self):
 
276
        gs = config.GlobalStack()
 
277
        gs.set('serve.client_timeout', 0.2)
 
278
        process, url = self.start_server_port()
 
279
        self.build_tree_contents([('a_file', 'contents\n')])
 
280
        # We can connect and issue a request
 
281
        t = transport.get_transport_from_url(url)
 
282
        self.assertEqual('contents\n', t.get_bytes('a_file'))
 
283
        # However, if we just wait for more content from the server, it will
 
284
        # eventually disconnect us.
 
285
        # TODO: Use something like signal.alarm() so that if the server doesn't
 
286
        #       properly handle the timeout, we end up failing the test instead
 
287
        #       of hanging forever.
 
288
        m = t.get_smart_medium()
 
289
        m.read_bytes(1)
 
290
        # Now, we wait for timeout to trigger
 
291
        err = process.stderr.readline()
 
292
        self.assertEqual(
 
293
            'Connection Timeout: disconnecting client after 0.2 seconds\n',
 
294
            err)
 
295
        self.assertServerFinishesCleanly(process)
 
296
 
 
297
    def test_bzr_serve_supports_client_timeout(self):
 
298
        process, url = self.start_server_port(['--client-timeout=0.1'])
 
299
        self.build_tree_contents([('a_file', 'contents\n')])
 
300
        # We can connect and issue a request
 
301
        t = transport.get_transport_from_url(url)
 
302
        self.assertEqual('contents\n', t.get_bytes('a_file'))
 
303
        # However, if we just wait for more content from the server, it will
 
304
        # eventually disconnect us.
 
305
        # TODO: Use something like signal.alarm() so that if the server doesn't
 
306
        #       properly handle the timeout, we end up failing the test instead
 
307
        #       of hanging forever.
 
308
        m = t.get_smart_medium()
 
309
        m.read_bytes(1)
 
310
        # Now, we wait for timeout to trigger
 
311
        err = process.stderr.readline()
 
312
        self.assertEqual(
 
313
            'Connection Timeout: disconnecting client after 0.1 seconds\n',
 
314
            err)
 
315
        self.assertServerFinishesCleanly(process)
 
316
 
 
317
    def test_bzr_serve_graceful_shutdown(self):
 
318
        big_contents = 'a'*64*1024
 
319
        self.build_tree_contents([('bigfile', big_contents)])
 
320
        process, url = self.start_server_port(['--client-timeout=1.0'])
 
321
        t = transport.get_transport_from_url(url)
 
322
        m = t.get_smart_medium()
 
323
        c = client._SmartClient(m)
 
324
        # Start, but don't finish a response
 
325
        resp, response_handler = c.call_expecting_body('get', 'bigfile')
 
326
        self.assertEqual(('ok',), resp)
 
327
        # Note: process.send_signal is a Python 2.6ism
 
328
        process.send_signal(signal.SIGHUP)
 
329
        # Wait for the server to notice the signal, and then read the actual
 
330
        # body of the response. That way we know that it is waiting for the
 
331
        # request to finish
 
332
        self.assertEqual('Requested to stop gracefully\n',
 
333
                         process.stderr.readline())
 
334
        self.assertEqual('Waiting for 1 client(s) to finish\n',
 
335
                         process.stderr.readline())
 
336
        body = response_handler.read_body_bytes()
 
337
        if body != big_contents:
 
338
            self.fail('Failed to properly read the contents of "bigfile"')
 
339
        # Now that our request is finished, the medium should notice it has
 
340
        # been disconnected.
 
341
        self.assertEqual('', m.read_bytes(1))
 
342
        # And the server should be stopping
 
343
        self.assertEqual(0, process.wait())
 
344
 
 
345
 
 
346
class TestCmdServeChrooting(TestBzrServeBase):
 
347
 
 
348
    def test_serve_tcp(self):
 
349
        """'bzr serve' wraps the given --directory in a ChrootServer.
 
350
 
 
351
        So requests that search up through the parent directories (like
 
352
        find_repositoryV3) will give "not found" responses, rather than
 
353
        InvalidURLJoin or jail break errors.
 
354
        """
 
355
        t = self.get_transport()
 
356
        t.mkdir('server-root')
 
357
        self.run_bzr_serve_then_func(
 
358
            ['--port', '127.0.0.1:0',
 
359
             '--directory', t.local_abspath('server-root'),
 
360
             '--allow-writes'],
 
361
            func=self.when_server_started)
 
362
        # The when_server_started method issued a find_repositoryV3 that should
 
363
        # fail with 'norepository' because there are no repositories inside the
 
364
        # --directory.
 
365
        self.assertEqual(('norepository',), self.client_resp)
 
366
 
 
367
    def when_server_started(self):
 
368
        # Connect to the TCP server and issue some requests and see what comes
 
369
        # back.
 
370
        client_medium = medium.SmartTCPClientMedium(
 
371
            '127.0.0.1', self.tcp_server.port,
 
372
            'bzr://localhost:%d/' % (self.tcp_server.port,))
 
373
        smart_client = client._SmartClient(client_medium)
 
374
        resp = smart_client.call('mkdir', 'foo', '')
 
375
        resp = smart_client.call('BzrDirFormat.initialize', 'foo/')
209
376
        try:
210
 
            if sys.platform == 'win32':
211
 
                path_to_branch = os.path.splitdrive(path_to_branch)[1]
212
 
            branch = Branch.open(
213
 
                'bzr+ssh://fred:secret@localhost:%d%s' % (port, path_to_branch))
214
 
            self.make_read_requests(branch)
215
 
            # Check we can perform write operations
216
 
            branch.bzrdir.root_transport.mkdir('foo')
217
 
        finally:
218
 
            # Restore the BZR_REMOTE_PATH environment variable back to its
219
 
            # original state.
220
 
            if orig_bzr_remote_path is None:
221
 
                del os.environ['BZR_REMOTE_PATH']
222
 
            else:
223
 
                os.environ['BZR_REMOTE_PATH'] = orig_bzr_remote_path
224
 
 
225
 
        self.assertEqual(
226
 
            ['%s serve --inet --directory=/ --allow-writes'
227
 
             % bzr_remote_path],
228
 
            self.command_executed)
229
 
        
 
377
            resp = smart_client.call('BzrDir.find_repositoryV3', 'foo/')
 
378
        except errors.ErrorFromSmartServer, e:
 
379
            resp = e.error_tuple
 
380
        self.client_resp = resp
 
381
        client_medium.disconnect()
 
382
 
 
383
 
 
384
class TestUserdirExpansion(TestCaseWithMemoryTransport):
 
385
 
 
386
    @staticmethod
 
387
    def fake_expanduser(path):
 
388
        """A simple, environment-independent, function for the duration of this
 
389
        test.
 
390
 
 
391
        Paths starting with a path segment of '~user' will expand to start with
 
392
        '/home/user/'.  Every other path will be unchanged.
 
393
        """
 
394
        if path.split('/', 1)[0] == '~user':
 
395
            return '/home/user' + path[len('~user'):]
 
396
        return path
 
397
 
 
398
    def make_test_server(self, base_path='/'):
 
399
        """Make and start a BzrServerFactory, backed by a memory transport, and
 
400
        creat '/home/user' in that transport.
 
401
        """
 
402
        bzr_server = BzrServerFactory(
 
403
            self.fake_expanduser, lambda t: base_path)
 
404
        mem_transport = self.get_transport()
 
405
        mem_transport.mkdir_multi(['home', 'home/user'])
 
406
        bzr_server.set_up(mem_transport, None, None, inet=True, timeout=4.0)
 
407
        self.addCleanup(bzr_server.tear_down)
 
408
        return bzr_server
 
409
 
 
410
    def test_bzr_serve_expands_userdir(self):
 
411
        bzr_server = self.make_test_server()
 
412
        self.assertTrue(bzr_server.smart_server.backing_transport.has('~user'))
 
413
 
 
414
    def test_bzr_serve_does_not_expand_userdir_outside_base(self):
 
415
        bzr_server = self.make_test_server('/foo')
 
416
        self.assertFalse(bzr_server.smart_server.backing_transport.has('~user'))
 
417
 
 
418
    def test_get_base_path(self):
 
419
        """cmd_serve will turn the --directory option into a LocalTransport
 
420
        (optionally decorated with 'readonly+').  BzrServerFactory can
 
421
        determine the original --directory from that transport.
 
422
        """
 
423
        # URLs always include the trailing slash, and get_base_path returns it
 
424
        base_dir = osutils.abspath('/a/b/c') + '/'
 
425
        base_url = urlutils.local_path_to_url(base_dir) + '/'
 
426
        # Define a fake 'protocol' to capture the transport that cmd_serve
 
427
        # passes to serve_bzr.
 
428
        def capture_transport(transport, host, port, inet, timeout):
 
429
            self.bzr_serve_transport = transport
 
430
        cmd = builtins.cmd_serve()
 
431
        # Read-only
 
432
        cmd.run(directory=base_dir, protocol=capture_transport)
 
433
        server_maker = BzrServerFactory()
 
434
        self.assertEqual(
 
435
            'readonly+%s' % base_url, self.bzr_serve_transport.base)
 
436
        self.assertEqual(
 
437
            base_dir, server_maker.get_base_path(self.bzr_serve_transport))
 
438
        # Read-write
 
439
        cmd.run(directory=base_dir, protocol=capture_transport,
 
440
            allow_writes=True)
 
441
        server_maker = BzrServerFactory()
 
442
        self.assertEqual(base_url, self.bzr_serve_transport.base)
 
443
        self.assertEqual(base_dir,
 
444
            server_maker.get_base_path(self.bzr_serve_transport))