~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-01-13 02:43:21 UTC
  • mfrom: (5582.9.2 funky-chars)
  • Revision ID: pqm@pqm.ubuntu.com-20110113024321-d1ssmy4knbv806zp
(jelmer) Avoid hardcoded list of repository formats that do not support
 funky characters in filenames. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
25
27
 
26
28
from bzrlib import (
27
29
    builtins,
 
30
    debug,
28
31
    errors,
29
32
    osutils,
30
33
    revision as _mod_revision,
31
 
    trace,
32
34
    transport,
33
35
    urlutils,
34
36
    )
35
37
from bzrlib.branch import Branch
36
38
from bzrlib.bzrdir import BzrDir
37
39
from bzrlib.smart import client, medium
38
 
from bzrlib.smart.server import (
39
 
    BzrServerFactory,
40
 
    SmartTCPServer,
41
 
    )
 
40
from bzrlib.smart.server import BzrServerFactory, SmartTCPServer
42
41
from bzrlib.tests import (
43
42
    TestCaseWithMemoryTransport,
44
43
    TestCaseWithTransport,
 
44
    TestSkipped,
45
45
    )
 
46
from bzrlib.trace import mutter
46
47
from bzrlib.transport import remote
47
48
 
48
49
 
52
53
                                *func_args, **func_kwargs):
53
54
        """Run 'bzr serve', and run the given func in a thread once the server
54
55
        has started.
55
 
 
 
56
        
56
57
        When 'func' terminates, the server will be terminated too.
57
 
 
 
58
        
58
59
        Returns stdout and stderr.
59
60
        """
 
61
        # install hook
 
62
        def on_server_start(backing_urls, tcp_server):
 
63
            t = threading.Thread(
 
64
                target=on_server_start_thread, args=(tcp_server,))
 
65
            t.start()
60
66
        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
67
            try:
67
68
                # Run func if set
68
69
                self.tcp_server = tcp_server
72
73
                    except Exception, e:
73
74
                        # Log errors to make some test failures a little less
74
75
                        # mysterious.
75
 
                        trace.mutter('func broke: %r', e)
 
76
                        mutter('func broke: %r', e)
76
77
            finally:
77
78
                # Then stop the server
78
 
                trace.mutter('interrupting...')
 
79
                mutter('interrupting...')
79
80
                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
81
        SmartTCPServer.hooks.install_named_hook(
88
82
            'server_started_ex', on_server_start,
89
83
            'run_bzr_serve_then_func hook')
90
84
        # start a TCP server
91
85
        try:
92
 
            out, err = self.run_bzr(['serve'] + list(serve_args),
93
 
                                    retcode=retcode)
 
86
            out, err = self.run_bzr(['serve'] + list(serve_args))
94
87
        except KeyboardInterrupt, e:
95
88
            out, err = e.args
96
89
        return out, err
102
95
        super(TestBzrServe, self).setUp()
103
96
        self.disable_missing_extensions_warning()
104
97
 
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
98
    def assertInetServerShutsdownCleanly(self, process):
130
99
        """Shutdown the server process looking for errors."""
131
100
        # Shutdown the server: the server should shut down when it cannot read
195
164
        url = 'bzr://localhost:%d/' % port
196
165
        self.permit_url(url)
197
166
        return process, url
198
 
 
 
167
    
199
168
    def test_bzr_serve_quiet(self):
200
169
        self.make_branch('.')
201
170
        args = ['--port', 'localhost:0', '--quiet']
307
276
 
308
277
class TestUserdirExpansion(TestCaseWithMemoryTransport):
309
278
 
310
 
    @staticmethod
311
 
    def fake_expanduser(path):
 
279
    def fake_expanduser(self, path):
312
280
        """A simple, environment-independent, function for the duration of this
313
281
        test.
314
282
 
366
334
        self.assertEqual(base_url, self.bzr_serve_transport.base)
367
335
        self.assertEqual(base_dir,
368
336
            server_maker.get_base_path(self.bzr_serve_transport))
 
337