/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_transport.py

Merge from TestCaseWithMemoryTransport.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006 Canonical Ltd
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""Tests for smart transport"""
 
18
 
 
19
# all of this deals with byte strings so this is safe
 
20
from cStringIO import StringIO
 
21
import subprocess
 
22
import sys
 
23
 
 
24
import bzrlib
 
25
from bzrlib import (
 
26
        bzrdir,
 
27
        errors,
 
28
        tests,
 
29
        )
 
30
from bzrlib.transport import (
 
31
        get_transport,
 
32
        local,
 
33
        memory,
 
34
        smart,
 
35
        )
 
36
 
 
37
 
 
38
class SmartClientTests(tests.TestCase):
 
39
 
 
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)
 
44
 
 
45
 
 
46
class TCPClientTests(tests.TestCaseWithTransport):
 
47
 
 
48
    def setUp(self):
 
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
 
53
        # transport.
 
54
        self.transport_server = smart.SmartTCPServer_for_testing
 
55
 
 
56
    def test_plausible_url(self):
 
57
        self.assert_(self.get_url().startswith('bzr://'))
 
58
 
 
59
    def test_probe_transport(self):
 
60
        t = self.get_transport()
 
61
        self.assertIsInstance(t, smart.SmartTransport)
 
62
 
 
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)
 
67
 
 
68
 
 
69
class BasicSmartTests(tests.TestCase):
 
70
    
 
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())
 
79
 
 
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'
 
88
                         '17\n'
 
89
                         'contents\nof\nfile\n'
 
90
                         'done\n',
 
91
                         from_server.getvalue())
 
92
 
 
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()
 
100
        try:
 
101
            transport = smart.SmartTCPTransport(server.get_url())
 
102
            try:
 
103
                transport.get('something')
 
104
            except errors.TransportError, e:
 
105
                self.assertContainsRe(str(e), 'some random exception')
 
106
            else:
 
107
                self.fail("get did not raise expected error")
 
108
        finally:
 
109
            server.stop_background_thread()
 
110
 
 
111
    def test_server_subprocess(self):
 
112
        """Talk to a server started as a subprocess
 
113
        
 
114
        This is similar to running it over ssh, except that it runs in the same machine 
 
115
        without ssh intermediating.
 
116
        """
 
117
        args = [sys.executable, sys.argv[0], 'serve', '--inet']
 
118
        child = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
 
119
                                 close_fds=True)
 
120
        conn = smart.SmartStreamClient(lambda: (child.stdout, child.stdin))
 
121
        conn.query_version()
 
122
        conn.query_version()
 
123
        conn.disconnect()
 
124
        returncode = child.wait()
 
125
        self.assertEquals(0, returncode)
 
126
 
 
127
 
 
128
class SmartTCPTests(tests.TestCase):
 
129
    """Tests for connection to TCP server.
 
130
    
 
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.
 
133
    """
 
134
 
 
135
    def setUp(self):
 
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())
 
141
 
 
142
    def tearDown(self):
 
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()
 
148
        
 
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,}/')
 
152
 
 
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"))
 
158
 
 
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())
 
164
        
 
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
 
169
        # for users.
 
170
        try:
 
171
            self.transport.get('not%20a%20file')
 
172
        except errors.NoSuchFile, e:
 
173
            self.assertEqual('not%20a%20file', e.path)
 
174
        else:
 
175
            self.fail("get did not raise expected error")
 
176
 
 
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)
 
183
 
 
184
    def test__remote_path(self):
 
185
        self.assertEquals('/foo/bar',
 
186
                          self.transport._remote_path('foo/bar'))
 
187
 
 
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/',
 
192
                          conn2.base)
 
193
 
 
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'))
 
209
 
 
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)
 
216
 
 
217
 
 
218
class SmartServerTests(tests.TestCaseWithTransport):
 
219
    """Test that call directly into the server logic, bypassing the network."""
 
220
 
 
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)
 
226
        
 
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')])
 
231
        wt.add('hello')
 
232
        rev_id = wt.commit('add hello')
 
233
        
 
234
        server = smart.SmartServer(self.get_transport())
 
235
        response = server.dispatch_command('get_bundle', ('.', rev_id))
 
236
        bundle = serializer.read_bundle(StringIO(response.body))
 
237
 
 
238
 
 
239
class SmartTransportRegistration(tests.TestCase):
 
240
 
 
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)
 
245
 
 
246
 
 
247
class FakeClient(smart.SmartStreamClient):
 
248
    """Emulate a client for testing a transport's use of the client."""
 
249
 
 
250
    def __init__(self):
 
251
        smart.SmartStreamClient.__init__(self, None)
 
252
        self._calls = []
 
253
 
 
254
    def _call(self, *args):
 
255
        self._calls.append(('_call', args))
 
256
        return ('ok', )
 
257
 
 
258
    def _recv_bulk(self):
 
259
        return 'bar'
 
260
 
 
261
 
 
262
class TestSmartTransport(tests.TestCase):
 
263
        
 
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)
 
268
 
 
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)
 
272
 
 
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)
 
277
 
 
278
 
 
279
class InstrumentedClient(smart.SmartStreamClient):
 
280
    """A smart client whose writes are stored to a supplied list."""
 
281
 
 
282
    def __init__(self, write_output_list):
 
283
        smart.SmartStreamClient.__init__(self, None)
 
284
        self._write_output_list = write_output_list
 
285
 
 
286
    def _ensure_connection(self):
 
287
        """We are never strictly connected."""
 
288
 
 
289
    def _write_and_flush(self, bytes):
 
290
        self._write_output_list.append(bytes)
 
291
 
 
292
 
 
293
class InstrumentedServerProtocol(smart.SmartStreamServer):
 
294
    """A smart server which is backed by memory and saves its write requests."""
 
295
 
 
296
    def __init__(self, write_output_list):
 
297
        smart.SmartStreamServer.__init__(self, None, None,
 
298
            memory.MemoryTransport())
 
299
        self._write_output_list = write_output_list
 
300
 
 
301
    def _write_and_flush(self, bytes):
 
302
        self._write_output_list.append(bytes)
 
303
 
 
304
 
 
305
class TestSmartProtocol(tests.TestCase):
 
306
    """Tests for the smart protocol.
 
307
 
 
308
    Each test case gets a smart_server and smart_client created during setUp().
 
309
 
 
310
    It is planned that the client can be called with self.call_client() giving
 
311
    it an expected server response, which will be fed into it when it tries to
 
312
    read. Likewise, self.call_server will call a servers method with a canned
 
313
    serialised client request. Output done by the client or server for these
 
314
    calls will be captured to self.to_server and self.to_client. Each element
 
315
    in the list is a write call from the client or server respectively.
 
316
    """
 
317
 
 
318
    def setUp(self):
 
319
        super(TestSmartProtocol, self).setUp()
 
320
        self.to_server = []
 
321
        self.to_client = []
 
322
        self.smart_client = InstrumentedClient(self.to_server)
 
323
        self.smart_server = InstrumentedServerProtocol(self.to_client)
 
324
 
 
325
    def assertOffsetSerialisation(self, expected_offsets, expected_serialised,
 
326
        client, server_protocol):
 
327
        """Check that smart (de)serialises offsets as expected.
 
328
        
 
329
        We check both serialisation and deserialisation at the same time
 
330
        to ensure that the round tripping cannot skew: both directions should
 
331
        be as expected.
 
332
        
 
333
        :param expected_offsets: a readv offset list.
 
334
        :param expected_seralised: an expected serial form of the offsets.
 
335
        :param server: a SmartServer instance.
 
336
        """
 
337
        offsets = server_protocol.smart_server._deserialise_offsets(
 
338
            expected_serialised)
 
339
        self.assertEqual(expected_offsets, offsets)
 
340
        serialised = client._serialise_offsets(offsets)
 
341
        self.assertEqual(expected_serialised, serialised)
 
342
 
 
343
    def test_server_offset_serialisation(self):
 
344
        """The Smart protocol serialises offsets as a comma and \n string.
 
345
 
 
346
        We check a number of boundary cases are as expected: empty, one offset,
 
347
        one with the order of reads not increasing (an out of order read), and
 
348
        one that should coalesce.
 
349
        """
 
350
        self.assertOffsetSerialisation([], '',
 
351
            self.smart_client, self.smart_server)
 
352
        self.assertOffsetSerialisation([(1,2)], '1,2',
 
353
            self.smart_client, self.smart_server)
 
354
        self.assertOffsetSerialisation([(10,40), (0,5)], '10,40\n0,5',
 
355
            self.smart_client, self.smart_server)
 
356
        self.assertOffsetSerialisation([(1,2), (3,4), (100, 200)],
 
357
            '1,2\n3,4\n100,200', self.smart_client, self.smart_server)
 
358
 
 
359
 
 
360
# TODO: Client feature that does get_bundle and then installs that into a
 
361
# branch; this can be used in place of the regular pull/fetch operation when
 
362
# coming from a smart server.
 
363
#
 
364
# TODO: Eventually, want to do a 'branch' command by fetching the whole
 
365
# history as one big bundle.  How?  
 
366
#
 
367
# The branch command does 'br_from.sprout', which tries to preserve the same
 
368
# format.  We don't necessarily even want that.  
 
369
#
 
370
# It might be simpler to handle cmd_pull first, which does a simpler fetch()
 
371
# operation from one branch into another.  It already has some code for
 
372
# pulling from a bundle, which it does by trying to see if the destination is
 
373
# a bundle file.  So it seems the logic for pull ought to be:
 
374
 
375
#  - if it's a smart server, get a bundle from there and install that
 
376
#  - if it's a bundle, install that
 
377
#  - if it's a branch, pull from there
 
378
#
 
379
# Getting a bundle from a smart server is a bit different from reading a
 
380
# bundle from a URL:
 
381
#
 
382
#  - we can reasonably remember the URL we last read from 
 
383
#  - you can specify a revision number to pull, and we need to pass it across
 
384
#    to the server as a limit on what will be requested
 
385
#
 
386
# TODO: Given a URL, determine whether it is a smart server or not (or perhaps
 
387
# otherwise whether it's a bundle?)  Should this be a property or method of
 
388
# the transport?  For the ssh protocol, we always know it's a smart server.
 
389
# For http, we potentially need to probe.  But if we're explicitly given
 
390
# bzr+http:// then we can skip that for now.