~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

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

  • Committer: Alexander Belchenko
  • Date: 2012-03-29 08:34:13 UTC
  • mto: (6015.44.14 2.4)
  • mto: This revision was merged to the branch mainline in revision 6513.
  • Revision ID: bialix@ukr.net-20120329083413-d4bqqdtfn2yrxp4f
change st_dev, st_ino, st_uid, st_gid from int members to properties.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
from bzrlib import (
27
27
    builtins,
28
 
    config,
29
28
    errors,
30
29
    osutils,
31
30
    revision as _mod_revision,
34
33
    urlutils,
35
34
    )
36
35
from bzrlib.branch import Branch
37
 
from bzrlib.controldir import ControlDir
 
36
from bzrlib.bzrdir import BzrDir
38
37
from bzrlib.smart import client, medium
39
38
from bzrlib.smart.server import (
40
39
    BzrServerFactory,
67
66
            try:
68
67
                # Run func if set
69
68
                self.tcp_server = tcp_server
70
 
                if func is not None:
 
69
                if not func is None:
71
70
                    try:
72
71
                        func(*func_args, **func_kwargs)
73
72
                    except Exception, e:
88
87
        SmartTCPServer.hooks.install_named_hook(
89
88
            'server_started_ex', on_server_start,
90
89
            'run_bzr_serve_then_func hook')
91
 
        # It seesm thread.interrupt_main() will not raise KeyboardInterrupt
92
 
        # until after socket.accept returns. So we set the timeout low to make
93
 
        # the test faster.
94
 
        self.overrideAttr(SmartTCPServer, '_ACCEPT_TIMEOUT', 0.1)
95
90
        # start a TCP server
96
91
        try:
97
92
            out, err = self.run_bzr(['serve'] + list(serve_args),
122
117
        SmartTCPServer.hooks.install_named_hook(
123
118
            'server_exception', hook,
124
119
            'test_server_except_hook hook')
125
 
        args = ['--listen', 'localhost', '--port', '0', '--quiet']
 
120
        args = ['--port', 'localhost:0', '--quiet']
126
121
        out, err = self.run_bzr_serve_then_func(args, retcode=0)
127
122
        self.assertEqual('catching KeyboardInterrupt\n', err)
128
123
 
190
185
            finish_bzr_subprocess, and the base url for the server.
191
186
        """
192
187
        # Serve from the current directory
193
 
        args = ['serve', '--listen', 'localhost', '--port', '0']
 
188
        args = ['serve', '--port', 'localhost:0']
194
189
        args.extend(extra_options)
195
190
        process = self.start_bzr_subprocess(args, skip_if_plan_to_signal=True)
196
191
        port_line = process.stderr.readline()
203
198
 
204
199
    def test_bzr_serve_quiet(self):
205
200
        self.make_branch('.')
206
 
        args = ['--listen', 'localhost', '--port', '0', '--quiet']
 
201
        args = ['--port', 'localhost:0', '--quiet']
207
202
        out, err = self.run_bzr_serve_then_func(args, retcode=3)
208
203
        self.assertEqual('', out)
209
204
        self.assertEqual('', err)
221
216
        process, transport = self.start_server_inet(['--allow-writes'])
222
217
 
223
218
        # We get a working branch, and can create a directory
224
 
        branch = ControlDir.open_from_transport(transport).open_branch()
 
219
        branch = BzrDir.open_from_transport(transport).open_branch()
225
220
        self.make_read_requests(branch)
226
221
        transport.mkdir('adir')
227
222
        self.assertInetServerShutsdownCleanly(process)
229
224
    def test_bzr_serve_port_readonly(self):
230
225
        """bzr server should provide a read only filesystem by default."""
231
226
        process, url = self.start_server_port()
232
 
        t = transport.get_transport_from_url(url)
 
227
        t = transport.get_transport(url)
233
228
        self.assertRaises(errors.TransportNotPossible, t.mkdir, 'adir')
234
229
        self.assertServerFinishesCleanly(process)
235
230
 
263
258
        log_fname = os.getcwd() + '/server.log'
264
259
        self.overrideEnv('BZR_LOG', log_fname)
265
260
        process, transport = self.start_server_inet(['-Dhpss'])
266
 
        branch = ControlDir.open_from_transport(transport).open_branch()
 
261
        branch = BzrDir.open_from_transport(transport).open_branch()
267
262
        self.make_read_requests(branch)
268
263
        self.assertInetServerShutsdownCleanly(process)
269
264
        f = open(log_fname, 'rb')
271
266
        f.close()
272
267
        self.assertContainsRe(content, r'hpss request: \[[0-9-]+\]')
273
268
 
274
 
    def test_bzr_serve_supports_configurable_timeout(self):
275
 
        gs = config.GlobalStack()
276
 
        gs.set('serve.client_timeout', 0.2)
277
 
        # Save the config as the subprocess will use it
278
 
        gs.store.save()
279
 
        process, url = self.start_server_port()
280
 
        self.build_tree_contents([('a_file', 'contents\n')])
281
 
        # We can connect and issue a request
282
 
        t = transport.get_transport_from_url(url)
283
 
        self.assertEqual('contents\n', t.get_bytes('a_file'))
284
 
        # However, if we just wait for more content from the server, it will
285
 
        # eventually disconnect us.
286
 
        m = t.get_smart_medium()
287
 
        m.read_bytes(1)
288
 
        # Now, we wait for timeout to trigger
289
 
        err = process.stderr.readline()
290
 
        self.assertEqual(
291
 
            'Connection Timeout: disconnecting client after 0.2 seconds\n',
292
 
            err)
293
 
        self.assertServerFinishesCleanly(process)
294
 
 
295
 
    def test_bzr_serve_supports_client_timeout(self):
296
 
        process, url = self.start_server_port(['--client-timeout=0.1'])
297
 
        self.build_tree_contents([('a_file', 'contents\n')])
298
 
        # We can connect and issue a request
299
 
        t = transport.get_transport_from_url(url)
300
 
        self.assertEqual('contents\n', t.get_bytes('a_file'))
301
 
        # However, if we just wait for more content from the server, it will
302
 
        # eventually disconnect us.
303
 
        # TODO: Use something like signal.alarm() so that if the server doesn't
304
 
        #       properly handle the timeout, we end up failing the test instead
305
 
        #       of hanging forever.
306
 
        m = t.get_smart_medium()
307
 
        m.read_bytes(1)
308
 
        # Now, we wait for timeout to trigger
309
 
        err = process.stderr.readline()
310
 
        self.assertEqual(
311
 
            'Connection Timeout: disconnecting client after 0.1 seconds\n',
312
 
            err)
313
 
        self.assertServerFinishesCleanly(process)
314
 
 
315
 
    def test_bzr_serve_graceful_shutdown(self):
316
 
        big_contents = 'a'*64*1024
317
 
        self.build_tree_contents([('bigfile', big_contents)])
318
 
        process, url = self.start_server_port(['--client-timeout=1.0'])
319
 
        t = transport.get_transport_from_url(url)
320
 
        m = t.get_smart_medium()
321
 
        c = client._SmartClient(m)
322
 
        # Start, but don't finish a response
323
 
        resp, response_handler = c.call_expecting_body('get', 'bigfile')
324
 
        self.assertEqual(('ok',), resp)
325
 
        # Note: process.send_signal is a Python 2.6ism
326
 
        process.send_signal(signal.SIGHUP)
327
 
        # Wait for the server to notice the signal, and then read the actual
328
 
        # body of the response. That way we know that it is waiting for the
329
 
        # request to finish
330
 
        self.assertEqual('Requested to stop gracefully\n',
331
 
                         process.stderr.readline())
332
 
        self.assertEqual('Waiting for 1 client(s) to finish\n',
333
 
                         process.stderr.readline())
334
 
        body = response_handler.read_body_bytes()
335
 
        if body != big_contents:
336
 
            self.fail('Failed to properly read the contents of "bigfile"')
337
 
        # Now that our request is finished, the medium should notice it has
338
 
        # been disconnected.
339
 
        self.assertEqual('', m.read_bytes(1))
340
 
        # And the server should be stopping
341
 
        self.assertEqual(0, process.wait())
342
 
 
343
269
 
344
270
class TestCmdServeChrooting(TestBzrServeBase):
345
271
 
353
279
        t = self.get_transport()
354
280
        t.mkdir('server-root')
355
281
        self.run_bzr_serve_then_func(
356
 
            ['--listen', '127.0.0.1', '--port', '0',
 
282
            ['--port', '127.0.0.1:0',
357
283
             '--directory', t.local_abspath('server-root'),
358
284
             '--allow-writes'],
359
285
            func=self.when_server_started)
401
327
            self.fake_expanduser, lambda t: base_path)
402
328
        mem_transport = self.get_transport()
403
329
        mem_transport.mkdir_multi(['home', 'home/user'])
404
 
        bzr_server.set_up(mem_transport, None, None, inet=True, timeout=4.0)
 
330
        bzr_server.set_up(mem_transport, None, None, inet=True)
405
331
        self.addCleanup(bzr_server.tear_down)
406
332
        return bzr_server
407
333
 
423
349
        base_url = urlutils.local_path_to_url(base_dir) + '/'
424
350
        # Define a fake 'protocol' to capture the transport that cmd_serve
425
351
        # passes to serve_bzr.
426
 
        def capture_transport(transport, host, port, inet, timeout):
 
352
        def capture_transport(transport, host, port, inet):
427
353
            self.bzr_serve_transport = transport
428
354
        cmd = builtins.cmd_serve()
429
355
        # Read-only
440
366
        self.assertEqual(base_url, self.bzr_serve_transport.base)
441
367
        self.assertEqual(base_dir,
442
368
            server_maker.get_base_path(self.bzr_serve_transport))
443
 
        # Read-only, from a URL
444
 
        cmd.run(directory=base_url, protocol=capture_transport)
445
 
        server_maker = BzrServerFactory()
446
 
        self.assertEqual(
447
 
            'readonly+%s' % base_url, self.bzr_serve_transport.base)
448
 
        self.assertEqual(
449
 
            base_dir, server_maker.get_base_path(self.bzr_serve_transport))