1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for smart transport"""
19
# all of this deals with byte strings so this is safe
20
from cStringIO import StringIO
30
from bzrlib.transport import (
38
class SmartClientTests(tests.TestCase):
40
def test_construct_smart_stream_client(self):
41
# make a new client; this really wants a connector function that returns
42
# two fifos or sockets but the constructor should not do any IO
43
client = smart.SmartStreamClient(None)
46
class TCPClientTests(tests.TestCaseWithTransport):
49
super(TCPClientTests, self).setUp()
50
# We're allowed to set the transport class here, so that we don't use
51
# the default or a parameterized class, but rather use the
52
# TestCaseWithTransport infrastructure to set up a smart server and
54
self.transport_server = smart.SmartTCPServer_for_testing
56
def test_plausible_url(self):
57
self.assert_(self.get_url().startswith('bzr://'))
59
def test_probe_transport(self):
60
t = self.get_transport()
61
self.assertIsInstance(t, smart.SmartTransport)
63
def test_get_client_from_transport(self):
64
t = self.get_transport()
65
client = t.get_smart_client()
66
self.assertIsInstance(client, smart.SmartStreamClient)
69
class BasicSmartTests(tests.TestCase):
71
def test_smart_query_version(self):
72
"""Feed a canned query version to a server"""
73
to_server = StringIO('hello\n')
74
from_server = StringIO()
75
server = smart.SmartStreamServer(to_server, from_server, local.LocalTransport('file:///'))
76
server._serve_one_request()
77
self.assertEqual('ok\0011\n',
78
from_server.getvalue())
80
def test_canned_get_response(self):
81
transport = memory.MemoryTransport('memory:///')
82
transport.put_bytes('testfile', 'contents\nof\nfile\n')
83
to_server = StringIO('get\001./testfile\n')
84
from_server = StringIO()
85
server = smart.SmartStreamServer(to_server, from_server, transport)
86
server._serve_one_request()
87
self.assertEqual('ok\n'
89
'contents\nof\nfile\n'
91
from_server.getvalue())
93
def test_get_error_unexpected(self):
94
"""Error reported by server with no specific representation"""
95
class FlakyTransport(object):
96
def get_bytes(self, path):
97
raise Exception("some random exception from inside server")
98
server = smart.SmartTCPServer(backing_transport=FlakyTransport())
99
server.start_background_thread()
101
transport = smart.SmartTCPTransport(server.get_url())
103
transport.get('something')
104
except errors.TransportError, e:
105
self.assertContainsRe(str(e), 'some random exception')
107
self.fail("get did not raise expected error")
109
server.stop_background_thread()
111
def test_server_subprocess(self):
112
"""Talk to a server started as a subprocess
114
This is similar to running it over ssh, except that it runs in the same machine
115
without ssh intermediating.
117
args = [sys.executable, sys.argv[0], 'serve', '--inet']
118
child = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
120
conn = smart.SmartStreamClient(lambda: (child.stdout, child.stdin))
124
returncode = child.wait()
125
self.assertEquals(0, returncode)
128
class SmartTCPTests(tests.TestCase):
129
"""Tests for connection to TCP server.
131
All of these tests are run with a server running on another thread serving
132
a MemoryTransport, and a connection to it already open.
136
super(SmartTCPTests, self).setUp()
137
self.backing_transport = memory.MemoryTransport()
138
self.server = smart.SmartTCPServer(self.backing_transport)
139
self.server.start_background_thread()
140
self.transport = smart.SmartTCPTransport(self.server.get_url())
143
if getattr(self, 'transport', None):
144
self.transport.disconnect()
145
if getattr(self, 'server', None):
146
self.server.stop_background_thread()
147
super(SmartTCPTests, self).tearDown()
149
def test_start_tcp_server(self):
150
url = self.server.get_url()
151
self.assertContainsRe(url, r'^bzr://127\.0\.0\.1:[0-9]{2,}/')
153
def test_smart_transport_has(self):
154
"""Checking for file existence over smart."""
155
self.backing_transport.put_bytes("foo", "contents of foo\n")
156
self.assertTrue(self.transport.has("foo"))
157
self.assertFalse(self.transport.has("non-foo"))
159
def test_smart_transport_get(self):
160
"""Read back a file over smart."""
161
self.backing_transport.put_bytes("foo", "contents\nof\nfoo\n")
162
fp = self.transport.get("foo")
163
self.assertEqual('contents\nof\nfoo\n', fp.read())
165
def test_get_error_enoent(self):
166
"""Error reported from server getting nonexistent file."""
167
# The path in a raised NoSuchFile exception should be the precise path
168
# asked for by the client. This gives meaningful and unsurprising errors
171
self.transport.get('not%20a%20file')
172
except errors.NoSuchFile, e:
173
self.assertEqual('not%20a%20file', e.path)
175
self.fail("get did not raise expected error")
177
def test_simple_clone_conn(self):
178
"""Test that cloning reuses the same connection."""
179
# we create a real connection not a loopback one, but it will use the
180
# same server and pipes
181
conn2 = self.transport.clone('.')
182
self.assertTrue(self.transport._client is conn2._client)
184
def test__remote_path(self):
185
self.assertEquals('/foo/bar',
186
self.transport._remote_path('foo/bar'))
188
def test_clone_changes_base(self):
189
"""Cloning transport produces one with a new base location"""
190
conn2 = self.transport.clone('subdir')
191
self.assertEquals(self.transport.base + 'subdir/',
194
def test_open_dir(self):
195
"""Test changing directory"""
196
transport = self.transport
197
self.backing_transport.mkdir('toffee')
198
self.backing_transport.mkdir('toffee/apple')
199
self.assertEquals('/toffee', transport._remote_path('toffee'))
200
toffee_trans = transport.clone('toffee')
201
# Check that each transport has only the contents of its directory
202
# directly visible. If state was being held in the wrong object, it's
203
# conceivable that cloning a transport would alter the state of the
204
# cloned-from transport.
205
self.assertTrue(transport.has('toffee'))
206
self.assertFalse(toffee_trans.has('toffee'))
207
self.assertFalse(transport.has('apple'))
208
self.assertTrue(toffee_trans.has('apple'))
210
def test_open_bzrdir(self):
211
"""Open an existing bzrdir over smart transport"""
212
transport = self.transport
213
t = self.backing_transport
214
bzrdir.BzrDirFormat.get_default_format().initialize_on_transport(t)
215
result_dir = bzrdir.BzrDir.open_containing_from_transport(transport)
218
class SmartServerTests(tests.TestCaseWithTransport):
219
"""Test that call directly into the server logic, bypassing the network."""
221
def test_hello(self):
222
server = smart.SmartServer(None)
223
response = server.dispatch_command('hello', ())
224
self.assertEqual(('ok', '1'), response.args)
225
self.assertEqual(None, response.body)
227
def test_get_bundle(self):
228
from bzrlib.bundle import serializer
229
wt = self.make_branch_and_tree('.')
230
self.build_tree_contents([('hello', 'hello world')])
232
rev_id = wt.commit('add hello')
234
server = smart.SmartServer(self.get_transport())
235
response = server.dispatch_command('get_bundle', ('.', rev_id))
236
bundle = serializer.read_bundle(StringIO(response.body))
239
class SmartTransportRegistration(tests.TestCase):
241
def test_registration(self):
242
t = get_transport('bzr+ssh://example.com/path')
243
self.assertIsInstance(t, smart.SmartSSHTransport)
244
self.assertEqual('example.com', t._host)
247
class FakeClient(smart.SmartStreamClient):
248
"""Emulate a client for testing a transport's use of the client."""
251
smart.SmartStreamClient.__init__(self, None)
254
def _call(self, *args):
255
self._calls.append(('_call', args))
258
def _recv_bulk(self):
262
class TestSmartTransport(tests.TestCase):
264
def test_use_connection_factory(self):
265
# We want to be able to pass a client as a parameter to SmartTransport.
266
client = FakeClient()
267
transport = smart.SmartTransport('bzr://localhost/', client=client)
269
# We want to make sure the client is used when the first remote
270
# method is called. No method should have been called yet.
271
self.assertEqual([], client._calls)
273
# Now call a method that should result in a single request.
274
self.assertEqual('bar', transport.get_bytes('foo'))
275
# The only call to _call should have been to get /foo.
276
self.assertEqual([('_call', ('get', '/foo'))], client._calls)
279
# TODO: Client feature that does get_bundle and then installs that into a
280
# branch; this can be used in place of the regular pull/fetch operation when
281
# coming from a smart server.
283
# TODO: Eventually, want to do a 'branch' command by fetching the whole
284
# history as one big bundle. How?
286
# The branch command does 'br_from.sprout', which tries to preserve the same
287
# format. We don't necessarily even want that.
289
# It might be simpler to handle cmd_pull first, which does a simpler fetch()
290
# operation from one branch into another. It already has some code for
291
# pulling from a bundle, which it does by trying to see if the destination is
292
# a bundle file. So it seems the logic for pull ought to be:
294
# - if it's a smart server, get a bundle from there and install that
295
# - if it's a bundle, install that
296
# - if it's a branch, pull from there
298
# Getting a bundle from a smart server is a bit different from reading a
301
# - we can reasonably remember the URL we last read from
302
# - you can specify a revision number to pull, and we need to pass it across
303
# to the server as a limit on what will be requested
305
# TODO: Given a URL, determine whether it is a smart server or not (or perhaps
306
# otherwise whether it's a bundle?) Should this be a property or method of
307
# the transport? For the ssh protocol, we always know it's a smart server.
308
# For http, we potentially need to probe. But if we're explicitly given
309
# bzr+http:// then we can skip that for now.