~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: John Arbash Meinel
  • Date: 2010-02-17 17:11:16 UTC
  • mfrom: (4797.2.17 2.1)
  • mto: (4797.2.18 2.1)
  • mto: This revision was merged to the branch mainline in revision 5055.
  • Revision ID: john@arbash-meinel.com-20100217171116-h7t9223ystbnx5h8
merge bzr.2.1 in preparation for NEWS entry.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Canonical Ltd
 
1
# Copyright (C) 2006-2010 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
27
27
 
28
28
from bzrlib import (
29
29
    builtins,
 
30
    debug,
30
31
    errors,
31
32
    osutils,
32
33
    revision as _mod_revision,
 
34
    urlutils,
33
35
    )
34
36
from bzrlib.branch import Branch
35
37
from bzrlib.bzrdir import BzrDir
36
38
from bzrlib.smart import client, medium
37
39
from bzrlib.smart.server import BzrServerFactory, SmartTCPServer
38
40
from bzrlib.tests import (
39
 
    ParamikoFeature,
40
41
    TestCaseWithMemoryTransport,
41
42
    TestCaseWithTransport,
42
43
    TestSkipped,
45
46
from bzrlib.transport import get_transport, remote
46
47
 
47
48
 
48
 
class TestBzrServe(TestCaseWithTransport):
 
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
92
 
50
93
    def setUp(self):
51
94
        super(TestBzrServe, self).setUp()
88
131
            finish_bzr_subprocess, a client for the server, and a transport.
89
132
        """
90
133
        # Serve from the current directory
91
 
        process = self.start_bzr_subprocess(['serve', '--inet'])
 
134
        args = ['serve', '--inet']
 
135
        args.extend(extra_options)
 
136
        process = self.start_bzr_subprocess(args)
92
137
 
93
138
        # Connect to the server
94
139
        # We use this url because while this is no valid URL to connect to this
118
163
        url = 'bzr://localhost:%d/' % port
119
164
        self.permit_url(url)
120
165
        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)
121
173
 
122
174
    def test_bzr_serve_inet_readonly(self):
123
175
        """bzr server should provide a read only filesystem by default."""
131
183
 
132
184
        process, transport = self.start_server_inet(['--allow-writes'])
133
185
 
134
 
        # We get a working branch
 
186
        # We get a working branch, and can create a directory
135
187
        branch = BzrDir.open_from_transport(transport).open_branch()
136
188
        self.make_read_requests(branch)
 
189
        transport.mkdir('adir')
137
190
        self.assertInetServerShutsdownCleanly(process)
138
191
 
139
192
    def test_bzr_serve_port_readonly(self):
166
219
        self.make_read_requests(branch)
167
220
        self.assertServerFinishesCleanly(process)
168
221
 
169
 
 
170
 
class TestCmdServeChrooting(TestCaseWithTransport):
 
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):
171
239
 
172
240
    def test_serve_tcp(self):
173
241
        """'bzr serve' wraps the given --directory in a ChrootServer.
182
250
            ['--port', '127.0.0.1:0',
183
251
             '--directory', t.local_abspath('server-root'),
184
252
             '--allow-writes'],
185
 
            self.when_server_started)
 
253
            func=self.when_server_started)
186
254
        # The when_server_started method issued a find_repositoryV3 that should
187
255
        # fail with 'norepository' because there are no repositories inside the
188
256
        # --directory.
189
257
        self.assertEqual(('norepository',), self.client_resp)
190
258
 
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
 
 
226
259
    def when_server_started(self):
227
260
        # Connect to the TCP server and issue some requests and see what comes
228
261
        # back.
254
287
        return path
255
288
 
256
289
    def make_test_server(self, base_path='/'):
257
 
        """Make and setUp a BzrServerFactory, backed by a memory transport, and
 
290
        """Make and start a BzrServerFactory, backed by a memory transport, and
258
291
        creat '/home/user' in that transport.
259
292
        """
260
293
        bzr_server = BzrServerFactory(
278
311
        (optionally decorated with 'readonly+').  BzrServerFactory can
279
312
        determine the original --directory from that transport.
280
313
        """
 
314
        # URLs always include the trailing slash, and get_base_path returns it
 
315
        base_dir = osutils.abspath('/a/b/c') + '/'
 
316
        base_url = urlutils.local_path_to_url(base_dir) + '/'
281
317
        # Define a fake 'protocol' to capture the transport that cmd_serve
282
318
        # passes to serve_bzr.
283
319
        def capture_transport(transport, host, port, inet):
284
320
            self.bzr_serve_transport = transport
285
321
        cmd = builtins.cmd_serve()
286
322
        # Read-only
287
 
        cmd.run(directory='/a/b/c', protocol=capture_transport)
 
323
        cmd.run(directory=base_dir, protocol=capture_transport)
288
324
        server_maker = BzrServerFactory()
289
325
        self.assertEqual(
290
 
            'readonly+file:///a/b/c/', self.bzr_serve_transport.base)
 
326
            'readonly+%s' % base_url, self.bzr_serve_transport.base)
291
327
        self.assertEqual(
292
 
            u'/a/b/c/', server_maker.get_base_path(self.bzr_serve_transport))
 
328
            base_dir, server_maker.get_base_path(self.bzr_serve_transport))
293
329
        # Read-write
294
 
        cmd.run(directory='/a/b/c', protocol=capture_transport,
 
330
        cmd.run(directory=base_dir, protocol=capture_transport,
295
331
            allow_writes=True)
296
332
        server_maker = BzrServerFactory()
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))
 
333
        self.assertEqual(base_url, self.bzr_serve_transport.base)
 
334
        self.assertEqual(base_dir,
 
335
            server_maker.get_base_path(self.bzr_serve_transport))
300
336