~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Max Bowsher
  • Date: 2011-08-23 09:29:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6104.
  • Revision ID: _@maxb.eu-20110823092927-c7fnueriuunvv9mh
Per jam's review comments, get rid of features.meliae_feature, which is new in
2.5 and so will not be missed, assigning the corrected behaviour to the
features.meliae name.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 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
18
18
"""Tests of the bzr serve command."""
19
19
 
20
20
import os
21
 
import os.path
22
21
import signal
23
 
import subprocess
24
22
import sys
25
23
import thread
26
24
import threading
27
25
 
28
26
from bzrlib import (
29
27
    builtins,
30
 
    debug,
31
28
    errors,
32
29
    osutils,
33
30
    revision as _mod_revision,
 
31
    trace,
 
32
    transport,
34
33
    urlutils,
35
34
    )
36
35
from bzrlib.branch import Branch
37
36
from bzrlib.bzrdir import BzrDir
38
37
from bzrlib.smart import client, medium
39
 
from bzrlib.smart.server import BzrServerFactory, SmartTCPServer
 
38
from bzrlib.smart.server import (
 
39
    BzrServerFactory,
 
40
    SmartTCPServer,
 
41
    )
40
42
from bzrlib.tests import (
41
43
    TestCaseWithMemoryTransport,
42
44
    TestCaseWithTransport,
43
 
    TestSkipped,
44
45
    )
45
 
from bzrlib.trace import mutter
46
 
from bzrlib.transport import get_transport, remote
 
46
from bzrlib.transport import remote
47
47
 
48
48
 
49
49
class TestBzrServeBase(TestCaseWithTransport):
52
52
                                *func_args, **func_kwargs):
53
53
        """Run 'bzr serve', and run the given func in a thread once the server
54
54
        has started.
55
 
        
 
55
 
56
56
        When 'func' terminates, the server will be terminated too.
57
 
        
 
57
 
58
58
        Returns stdout and stderr.
59
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
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
66
            try:
67
67
                # Run func if set
68
68
                self.tcp_server = tcp_server
72
72
                    except Exception, e:
73
73
                        # Log errors to make some test failures a little less
74
74
                        # mysterious.
75
 
                        mutter('func broke: %r', e)
 
75
                        trace.mutter('func broke: %r', e)
76
76
            finally:
77
77
                # Then stop the server
78
 
                mutter('interrupting...')
 
78
                trace.mutter('interrupting...')
79
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
80
87
        SmartTCPServer.hooks.install_named_hook(
81
88
            'server_started_ex', on_server_start,
82
89
            'run_bzr_serve_then_func hook')
83
90
        # start a TCP server
84
91
        try:
85
 
            out, err = self.run_bzr(['serve'] + list(serve_args))
 
92
            out, err = self.run_bzr(['serve'] + list(serve_args),
 
93
                                    retcode=retcode)
86
94
        except KeyboardInterrupt, e:
87
95
            out, err = e.args
88
96
        return out, err
94
102
        super(TestBzrServe, self).setUp()
95
103
        self.disable_missing_extensions_warning()
96
104
 
 
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
 
97
129
    def assertInetServerShutsdownCleanly(self, process):
98
130
        """Shutdown the server process looking for errors."""
99
131
        # Shutdown the server: the server should shut down when it cannot read
163
195
        url = 'bzr://localhost:%d/' % port
164
196
        self.permit_url(url)
165
197
        return process, url
166
 
    
 
198
 
167
199
    def test_bzr_serve_quiet(self):
168
200
        self.make_branch('.')
169
201
        args = ['--port', 'localhost:0', '--quiet']
192
224
    def test_bzr_serve_port_readonly(self):
193
225
        """bzr server should provide a read only filesystem by default."""
194
226
        process, url = self.start_server_port()
195
 
        transport = get_transport(url)
196
 
        self.assertRaises(errors.TransportNotPossible, transport.mkdir, 'adir')
 
227
        t = transport.get_transport_from_url(url)
 
228
        self.assertRaises(errors.TransportNotPossible, t.mkdir, 'adir')
197
229
        self.assertServerFinishesCleanly(process)
198
230
 
199
231
    def test_bzr_serve_port_readwrite(self):
224
256
        # -Dhpss, and does drop some hpss logging to the file.
225
257
        self.make_branch('.')
226
258
        log_fname = os.getcwd() + '/server.log'
227
 
        self._captureVar('BZR_LOG', log_fname)
 
259
        self.overrideEnv('BZR_LOG', log_fname)
228
260
        process, transport = self.start_server_inet(['-Dhpss'])
229
261
        branch = BzrDir.open_from_transport(transport).open_branch()
230
262
        self.make_read_requests(branch)
275
307
 
276
308
class TestUserdirExpansion(TestCaseWithMemoryTransport):
277
309
 
278
 
    def fake_expanduser(self, path):
 
310
    @staticmethod
 
311
    def fake_expanduser(path):
279
312
        """A simple, environment-independent, function for the duration of this
280
313
        test.
281
314
 
333
366
        self.assertEqual(base_url, self.bzr_serve_transport.base)
334
367
        self.assertEqual(base_dir,
335
368
            server_maker.get_base_path(self.bzr_serve_transport))
336