1
# Copyright (C) 2006-2010 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
38
37
from bzrlib.smart import client, medium
39
38
from bzrlib.smart.server import BzrServerFactory, SmartTCPServer
40
39
from bzrlib.tests import (
41
41
TestCaseWithMemoryTransport,
42
42
TestCaseWithTransport,
46
46
from bzrlib.transport import get_transport, remote
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):
49
class TestBzrServe(TestCaseWithTransport):
94
52
super(TestBzrServe, self).setUp()
131
89
finish_bzr_subprocess, a client for the server, and a transport.
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'])
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
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)
174
123
def test_bzr_serve_inet_readonly(self):
175
124
"""bzr server should provide a read only filesystem by default."""
184
133
process, transport = self.start_server_inet(['--allow-writes'])
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)
192
140
def test_bzr_serve_port_readonly(self):
219
167
self.make_read_requests(branch)
220
168
self.assertServerFinishesCleanly(process)
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):
171
class TestCmdServeChrooting(TestCaseWithTransport):
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
257
190
self.assertEqual(('norepository',), self.client_resp)
192
def run_bzr_serve_then_func(self, serve_args, func, *func_args,
194
"""Run 'bzr serve', and run the given func in a thread once the server
197
When 'func' terminates, the server will be terminated too.
200
def on_server_start(backing_urls, tcp_server):
201
t = threading.Thread(
202
target=on_server_start_thread, args=(tcp_server,))
204
def on_server_start_thread(tcp_server):
207
self.tcp_server = tcp_server
209
func(*func_args, **func_kwargs)
211
# Log errors to make some test failures a little less
213
mutter('func broke: %r', e)
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')
223
self.run_bzr(['serve'] + list(serve_args))
224
except KeyboardInterrupt:
259
227
def when_server_started(self):
260
228
# Connect to the TCP server and issue some requests and see what comes
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.
293
261
bzr_server = BzrServerFactory(