~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: 2009-11-17 09:21:07 UTC
  • mfrom: (4800.1.3 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20091117092107-8ag9sr4hd0cubo6r
(mbp) really fix Japanese sphinx

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
from bzrlib import (
29
29
    builtins,
30
 
    debug,
31
30
    errors,
32
31
    osutils,
33
32
    revision as _mod_revision,
38
37
from bzrlib.smart import client, medium
39
38
from bzrlib.smart.server import BzrServerFactory, SmartTCPServer
40
39
from bzrlib.tests import (
 
40
    ParamikoFeature,
41
41
    TestCaseWithMemoryTransport,
42
42
    TestCaseWithTransport,
43
43
    TestSkipped,
46
46
from bzrlib.transport import get_transport, remote
47
47
 
48
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
 
        # 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):
 
49
class TestBzrServe(TestCaseWithTransport):
92
50
 
93
51
    def setUp(self):
94
52
        super(TestBzrServe, self).setUp()
131
89
            finish_bzr_subprocess, a client for the server, and a transport.
132
90
        """
133
91
        # Serve from the current directory
134
 
        args = ['serve', '--inet']
135
 
        args.extend(extra_options)
136
 
        process = self.start_bzr_subprocess(args)
 
92
        process = self.start_bzr_subprocess(['serve', '--inet'])
137
93
 
138
94
        # Connect to the server
139
95
        # We use this url because while this is no valid URL to connect to this
163
119
        url = 'bzr://localhost:%d/' % port
164
120
        self.permit_url(url)
165
121
        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
122
 
174
123
    def test_bzr_serve_inet_readonly(self):
175
124
        """bzr server should provide a read only filesystem by default."""
183
132
 
184
133
        process, transport = self.start_server_inet(['--allow-writes'])
185
134
 
186
 
        # We get a working branch, and can create a directory
 
135
        # We get a working branch
187
136
        branch = BzrDir.open_from_transport(transport).open_branch()
188
137
        self.make_read_requests(branch)
189
 
        transport.mkdir('adir')
190
138
        self.assertInetServerShutsdownCleanly(process)
191
139
 
192
140
    def test_bzr_serve_port_readonly(self):
219
167
        self.make_read_requests(branch)
220
168
        self.assertServerFinishesCleanly(process)
221
169
 
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):
 
170
 
 
171
class TestCmdServeChrooting(TestCaseWithTransport):
239
172
 
240
173
    def test_serve_tcp(self):
241
174
        """'bzr serve' wraps the given --directory in a ChrootServer.
250
183
            ['--port', '127.0.0.1:0',
251
184
             '--directory', t.local_abspath('server-root'),
252
185
             '--allow-writes'],
253
 
            func=self.when_server_started)
 
186
            self.when_server_started)
254
187
        # The when_server_started method issued a find_repositoryV3 that should
255
188
        # fail with 'norepository' because there are no repositories inside the
256
189
        # --directory.
257
190
        self.assertEqual(('norepository',), self.client_resp)
258
191
 
 
192
    def run_bzr_serve_then_func(self, serve_args, func, *func_args,
 
193
            **func_kwargs):
 
194
        """Run 'bzr serve', and run the given func in a thread once the server
 
195
        has started.
 
196
        
 
197
        When 'func' terminates, the server will be terminated too.
 
198
        """
 
199
        # install hook
 
200
        def on_server_start(backing_urls, tcp_server):
 
201
            t = threading.Thread(
 
202
                target=on_server_start_thread, args=(tcp_server,))
 
203
            t.start()
 
204
        def on_server_start_thread(tcp_server):
 
205
            try:
 
206
                # Run func
 
207
                self.tcp_server = tcp_server
 
208
                try:
 
209
                    func(*func_args, **func_kwargs)
 
210
                except Exception, e:
 
211
                    # Log errors to make some test failures a little less
 
212
                    # mysterious.
 
213
                    mutter('func broke: %r', e)
 
214
            finally:
 
215
                # Then stop the server
 
216
                mutter('interrupting...')
 
217
                thread.interrupt_main()
 
218
        SmartTCPServer.hooks.install_named_hook(
 
219
            'server_started_ex', on_server_start,
 
220
            'run_bzr_serve_then_func hook')
 
221
        # start a TCP server
 
222
        try:
 
223
            self.run_bzr(['serve'] + list(serve_args))
 
224
        except KeyboardInterrupt:
 
225
            pass
 
226
 
259
227
    def when_server_started(self):
260
228
        # Connect to the TCP server and issue some requests and see what comes
261
229
        # back.
287
255
        return path
288
256
 
289
257
    def make_test_server(self, base_path='/'):
290
 
        """Make and start a BzrServerFactory, backed by a memory transport, and
 
258
        """Make and setUp a BzrServerFactory, backed by a memory transport, and
291
259
        creat '/home/user' in that transport.
292
260
        """
293
261
        bzr_server = BzrServerFactory(