~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart_transport.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2006-09-16 01:47:06 UTC
  • mfrom: (1910.19.16 smart-server)
  • Revision ID: pqm@pqm.ubuntu.com-20060916014706-93f2994bdb0c0850
(spiv,mpool,robertc) Create a RPC protocol as the building blocks for a smart server

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
# 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.
 
282
#
 
283
# TODO: Eventually, want to do a 'branch' command by fetching the whole
 
284
# history as one big bundle.  How?  
 
285
#
 
286
# The branch command does 'br_from.sprout', which tries to preserve the same
 
287
# format.  We don't necessarily even want that.  
 
288
#
 
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:
 
293
 
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
 
297
#
 
298
# Getting a bundle from a smart server is a bit different from reading a
 
299
# bundle from a URL:
 
300
#
 
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
 
304
#
 
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.