1
# Copyright (C) 2006 Canonical Ltd
1
# Copyright (C) 2006-2010 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
28
28
from bzrlib import (
32
33
revision as _mod_revision,
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 (
40
41
TestCaseWithMemoryTransport,
41
42
TestCaseWithTransport,
45
46
from bzrlib.transport import get_transport, remote
48
class TestBzrServe(TestCaseWithTransport):
49
class TestBzrServeBase(TestCaseWithTransport):
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
56
When 'func' terminates, the server will be terminated too.
58
Returns stdout and stderr.
61
def on_server_start(backing_urls, tcp_server):
63
target=on_server_start_thread, args=(tcp_server,))
65
def on_server_start_thread(tcp_server):
68
self.tcp_server = tcp_server
71
func(*func_args, **func_kwargs)
73
# Log errors to make some test failures a little less
75
mutter('func broke: %r', e)
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')
85
out, err = self.run_bzr(['serve'] + list(serve_args))
86
except KeyboardInterrupt, e:
91
class TestBzrServe(TestBzrServeBase):
51
94
super(TestBzrServe, self).setUp()
88
131
finish_bzr_subprocess, a client for the server, and a transport.
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)
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
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)
122
174
def test_bzr_serve_inet_readonly(self):
123
175
"""bzr server should provide a read only filesystem by default."""
132
184
process, transport = self.start_server_inet(['--allow-writes'])
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)
139
192
def test_bzr_serve_port_readonly(self):
166
219
self.make_read_requests(branch)
167
220
self.assertServerFinishesCleanly(process)
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')
235
self.assertContainsRe(content, r'hpss request: \[[0-9-]+\]')
238
class TestCmdServeChrooting(TestBzrServeBase):
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
189
257
self.assertEqual(('norepository',), self.client_resp)
191
def run_bzr_serve_then_func(self, serve_args, func, *func_args,
193
"""Run 'bzr serve', and run the given func in a thread once the server
196
When 'func' terminates, the server will be terminated too.
199
def on_server_start(backing_urls, tcp_server):
200
t = threading.Thread(
201
target=on_server_start_thread, args=(tcp_server,))
203
def on_server_start_thread(tcp_server):
206
self.tcp_server = tcp_server
208
func(*func_args, **func_kwargs)
210
# Log errors to make some test failures a little less
212
mutter('func broke: %r', e)
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')
222
self.run_bzr(['serve'] + list(serve_args))
223
except KeyboardInterrupt:
226
259
def when_server_started(self):
227
260
# Connect to the TCP server and issue some requests and see what comes
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.
260
293
bzr_server = BzrServerFactory(
278
311
(optionally decorated with 'readonly+'). BzrServerFactory can
279
312
determine the original --directory from that transport.
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()
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))
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)
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))