1
# Copyright (C) 2006-2011 Canonical Ltd
1
# Copyright (C) 2006 Canonical Ltd
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
30
32
revision as _mod_revision,
35
35
from bzrlib.branch import Branch
36
36
from bzrlib.bzrdir import BzrDir
37
37
from bzrlib.smart import client, medium
38
from bzrlib.smart.server import (
38
from bzrlib.smart.server import BzrServerFactory, SmartTCPServer
42
39
from bzrlib.tests import (
43
41
TestCaseWithMemoryTransport,
44
42
TestCaseWithTransport,
46
from bzrlib.transport import remote
45
from bzrlib.trace import mutter
46
from bzrlib.transport import get_transport, remote
49
48
class TestBzrServeBase(TestCaseWithTransport):
52
51
*func_args, **func_kwargs):
53
52
"""Run 'bzr serve', and run the given func in a thread once the server
56
55
When 'func' terminates, the server will be terminated too.
58
57
Returns stdout and stderr.
60
def on_server_start(backing_urls, tcp_server):
62
target=on_server_start_thread, args=(tcp_server,))
60
64
def on_server_start_thread(tcp_server):
61
"""This runs concurrently with the server thread.
63
The server is interrupted as soon as ``func`` finishes, even if an
64
exception is encountered.
68
67
self.tcp_server = tcp_server
72
71
except Exception, e:
73
72
# Log errors to make some test failures a little less
75
trace.mutter('func broke: %r', e)
74
mutter('func broke: %r', e)
77
76
# Then stop the server
78
trace.mutter('interrupting...')
77
mutter('interrupting...')
79
78
thread.interrupt_main()
80
# When the hook is fired, it just starts ``on_server_start_thread`` and
82
def on_server_start(backing_urls, tcp_server):
84
target=on_server_start_thread, args=(tcp_server,))
87
79
SmartTCPServer.hooks.install_named_hook(
88
80
'server_started_ex', on_server_start,
89
81
'run_bzr_serve_then_func hook')
90
82
# start a TCP server
92
out, err = self.run_bzr(['serve'] + list(serve_args),
84
out, err = self.run_bzr(['serve'] + list(serve_args))
94
85
except KeyboardInterrupt, e:
102
93
super(TestBzrServe, self).setUp()
103
94
self.disable_missing_extensions_warning()
105
def test_server_exception_with_hook(self):
106
"""Catch exception from the server in the server_exception hook.
108
We use ``run_bzr_serve_then_func`` without a ``func`` so the server
109
will receive a KeyboardInterrupt exception we want to catch.
112
if exception[0] is KeyboardInterrupt:
113
sys.stderr.write('catching KeyboardInterrupt\n')
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)
124
def test_server_exception_no_hook(self):
125
"""test exception without hook returns error"""
127
out, err = self.run_bzr_serve_then_func(args, retcode=3)
129
96
def assertInetServerShutsdownCleanly(self, process):
130
97
"""Shutdown the server process looking for errors."""
131
98
# Shutdown the server: the server should shut down when it cannot read
163
130
finish_bzr_subprocess, a client for the server, and a transport.
165
132
# Serve from the current directory
166
args = ['serve', '--inet']
167
args.extend(extra_options)
168
process = self.start_bzr_subprocess(args)
133
process = self.start_bzr_subprocess(['serve', '--inet'])
170
135
# Connect to the server
171
136
# We use this url because while this is no valid URL to connect to this
216
181
process, transport = self.start_server_inet(['--allow-writes'])
218
# We get a working branch, and can create a directory
183
# We get a working branch
219
184
branch = BzrDir.open_from_transport(transport).open_branch()
220
185
self.make_read_requests(branch)
221
transport.mkdir('adir')
222
186
self.assertInetServerShutsdownCleanly(process)
224
188
def test_bzr_serve_port_readonly(self):
225
189
"""bzr server should provide a read only filesystem by default."""
226
190
process, url = self.start_server_port()
227
t = transport.get_transport_from_url(url)
228
self.assertRaises(errors.TransportNotPossible, t.mkdir, 'adir')
191
transport = get_transport(url)
192
self.assertRaises(errors.TransportNotPossible, transport.mkdir, 'adir')
229
193
self.assertServerFinishesCleanly(process)
231
195
def test_bzr_serve_port_readwrite(self):
251
215
self.make_read_requests(branch)
252
216
self.assertServerFinishesCleanly(process)
254
def test_bzr_serve_dhpss(self):
255
# This is a smoke test that the server doesn't crash when run with
256
# -Dhpss, and does drop some hpss logging to the file.
257
self.make_branch('.')
258
log_fname = os.getcwd() + '/server.log'
259
self.overrideEnv('BZR_LOG', log_fname)
260
process, transport = self.start_server_inet(['-Dhpss'])
261
branch = BzrDir.open_from_transport(transport).open_branch()
262
self.make_read_requests(branch)
263
self.assertInetServerShutsdownCleanly(process)
264
f = open(log_fname, 'rb')
267
self.assertContainsRe(content, r'hpss request: \[[0-9-]+\]')
270
219
class TestCmdServeChrooting(TestBzrServeBase):
308
257
class TestUserdirExpansion(TestCaseWithMemoryTransport):
311
def fake_expanduser(path):
259
def fake_expanduser(self, path):
312
260
"""A simple, environment-independent, function for the duration of this
322
270
def make_test_server(self, base_path='/'):
323
"""Make and start a BzrServerFactory, backed by a memory transport, and
271
"""Make and setUp a BzrServerFactory, backed by a memory transport, and
324
272
creat '/home/user' in that transport.
326
274
bzr_server = BzrServerFactory(