~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Martin Pool
  • Date: 2010-06-02 05:03:31 UTC
  • mto: This revision was merged to the branch mainline in revision 5279.
  • Revision ID: mbp@canonical.com-20100602050331-n2p1qt8hfsahspnv
Correct more sloppy use of the term 'Linux'

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006, 2007 Canonical Ltd
 
1
# Copyright (C) 2006-2010 Canonical Ltd
2
2
#
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
12
12
#
13
13
# You should have received a copy of the GNU General Public License
14
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
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
16
 
17
17
"""Tests for remote bzrdir/branch/repo/etc
18
18
 
19
19
These are proxy objects which act on remote objects by sending messages
20
20
through a smart client.  The proxies are to be created when attempting to open
21
 
the object given a transport that supports smartserver rpc operations. 
 
21
the object given a transport that supports smartserver rpc operations.
22
22
 
23
23
These tests correspond to tests.test_smart, which exercises the server side.
24
24
"""
25
25
 
 
26
import bz2
26
27
from cStringIO import StringIO
27
28
 
28
29
from bzrlib import (
 
30
    branch,
29
31
    bzrdir,
 
32
    config,
30
33
    errors,
 
34
    graph,
 
35
    inventory,
 
36
    inventory_delta,
 
37
    pack,
31
38
    remote,
32
39
    repository,
33
40
    tests,
 
41
    treebuilder,
 
42
    urlutils,
 
43
    versionedfile,
34
44
    )
35
45
from bzrlib.branch import Branch
36
46
from bzrlib.bzrdir import BzrDir, BzrDirFormat
37
47
from bzrlib.remote import (
38
48
    RemoteBranch,
 
49
    RemoteBranchFormat,
39
50
    RemoteBzrDir,
40
51
    RemoteBzrDirFormat,
41
52
    RemoteRepository,
 
53
    RemoteRepositoryFormat,
42
54
    )
 
55
from bzrlib.repofmt import groupcompress_repo, pack_repo
43
56
from bzrlib.revision import NULL_REVISION
44
 
from bzrlib.smart import server, medium
 
57
from bzrlib.smart import medium
45
58
from bzrlib.smart.client import _SmartClient
 
59
from bzrlib.smart.repository import SmartServerRepositoryGetParentMap
 
60
from bzrlib.tests import (
 
61
    condition_isinstance,
 
62
    split_suite_by_condition,
 
63
    multiply_tests,
 
64
    test_server,
 
65
    )
 
66
from bzrlib.transport import get_transport
46
67
from bzrlib.transport.memory import MemoryTransport
47
 
from bzrlib.transport.remote import RemoteTransport
 
68
from bzrlib.transport.remote import (
 
69
    RemoteTransport,
 
70
    RemoteSSHTransport,
 
71
    RemoteTCPTransport,
 
72
)
 
73
 
 
74
def load_tests(standard_tests, module, loader):
 
75
    to_adapt, result = split_suite_by_condition(
 
76
        standard_tests, condition_isinstance(BasicRemoteObjectTests))
 
77
    smart_server_version_scenarios = [
 
78
        ('HPSS-v2',
 
79
         {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
 
80
        ('HPSS-v3',
 
81
         {'transport_server': test_server.SmartTCPServer_for_testing})]
 
82
    return multiply_tests(to_adapt, smart_server_version_scenarios, result)
48
83
 
49
84
 
50
85
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
51
86
 
52
87
    def setUp(self):
53
 
        self.transport_server = server.SmartTCPServer_for_testing
54
88
        super(BasicRemoteObjectTests, self).setUp()
55
89
        self.transport = self.get_transport()
56
 
        self.client = self.transport.get_smart_client()
57
90
        # make a branch that can be opened over the smart transport
58
91
        self.local_wt = BzrDir.create_standalone_workingtree('.')
59
92
 
62
95
        tests.TestCaseWithTransport.tearDown(self)
63
96
 
64
97
    def test_create_remote_bzrdir(self):
65
 
        b = remote.RemoteBzrDir(self.transport)
 
98
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
66
99
        self.assertIsInstance(b, BzrDir)
67
100
 
68
101
    def test_open_remote_branch(self):
69
102
        # open a standalone branch in the working directory
70
 
        b = remote.RemoteBzrDir(self.transport)
 
103
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
71
104
        branch = b.open_branch()
72
105
        self.assertIsInstance(branch, Branch)
73
106
 
102
135
        b = BzrDir.open_from_transport(self.transport).open_branch()
103
136
        self.assertStartsWith(str(b), 'RemoteBranch(')
104
137
 
 
138
    def test_remote_bzrdir_repr(self):
 
139
        b = BzrDir.open_from_transport(self.transport)
 
140
        self.assertStartsWith(str(b), 'RemoteBzrDir(')
 
141
 
 
142
    def test_remote_branch_format_supports_stacking(self):
 
143
        t = self.transport
 
144
        self.make_branch('unstackable', format='pack-0.92')
 
145
        b = BzrDir.open_from_transport(t.clone('unstackable')).open_branch()
 
146
        self.assertFalse(b._format.supports_stacking())
 
147
        self.make_branch('stackable', format='1.9')
 
148
        b = BzrDir.open_from_transport(t.clone('stackable')).open_branch()
 
149
        self.assertTrue(b._format.supports_stacking())
 
150
 
 
151
    def test_remote_repo_format_supports_external_references(self):
 
152
        t = self.transport
 
153
        bd = self.make_bzrdir('unstackable', format='pack-0.92')
 
154
        r = bd.create_repository()
 
155
        self.assertFalse(r._format.supports_external_lookups)
 
156
        r = BzrDir.open_from_transport(t.clone('unstackable')).open_repository()
 
157
        self.assertFalse(r._format.supports_external_lookups)
 
158
        bd = self.make_bzrdir('stackable', format='1.9')
 
159
        r = bd.create_repository()
 
160
        self.assertTrue(r._format.supports_external_lookups)
 
161
        r = BzrDir.open_from_transport(t.clone('stackable')).open_repository()
 
162
        self.assertTrue(r._format.supports_external_lookups)
 
163
 
 
164
    def test_remote_branch_set_append_revisions_only(self):
 
165
        # Make a format 1.9 branch, which supports append_revisions_only
 
166
        branch = self.make_branch('branch', format='1.9')
 
167
        config = branch.get_config()
 
168
        branch.set_append_revisions_only(True)
 
169
        self.assertEqual(
 
170
            'True', config.get_user_option('append_revisions_only'))
 
171
        branch.set_append_revisions_only(False)
 
172
        self.assertEqual(
 
173
            'False', config.get_user_option('append_revisions_only'))
 
174
 
 
175
    def test_remote_branch_set_append_revisions_only_upgrade_reqd(self):
 
176
        branch = self.make_branch('branch', format='knit')
 
177
        config = branch.get_config()
 
178
        self.assertRaises(
 
179
            errors.UpgradeRequired, branch.set_append_revisions_only, True)
 
180
 
105
181
 
106
182
class FakeProtocol(object):
107
183
    """Lookalike SmartClientRequestProtocolOne allowing body reading tests."""
108
184
 
109
 
    def __init__(self, body):
110
 
        self._body_buffer = StringIO(body)
 
185
    def __init__(self, body, fake_client):
 
186
        self.body = body
 
187
        self._body_buffer = None
 
188
        self._fake_client = fake_client
111
189
 
112
190
    def read_body_bytes(self, count=-1):
113
 
        return self._body_buffer.read(count)
 
191
        if self._body_buffer is None:
 
192
            self._body_buffer = StringIO(self.body)
 
193
        bytes = self._body_buffer.read(count)
 
194
        if self._body_buffer.tell() == len(self._body_buffer.getvalue()):
 
195
            self._fake_client.expecting_body = False
 
196
        return bytes
 
197
 
 
198
    def cancel_read_body(self):
 
199
        self._fake_client.expecting_body = False
 
200
 
 
201
    def read_streamed_body(self):
 
202
        return self.body
114
203
 
115
204
 
116
205
class FakeClient(_SmartClient):
117
206
    """Lookalike for _SmartClient allowing testing."""
118
 
    
119
 
    def __init__(self, responses):
120
 
        # We don't call the super init because there is no medium.
121
 
        """create a FakeClient.
122
207
 
123
 
        :param respones: A list of response-tuple, body-data pairs to be sent
124
 
            back to callers.
125
 
        """
126
 
        self.responses = responses
 
208
    def __init__(self, fake_medium_base='fake base'):
 
209
        """Create a FakeClient."""
 
210
        self.responses = []
127
211
        self._calls = []
 
212
        self.expecting_body = False
 
213
        # if non-None, this is the list of expected calls, with only the
 
214
        # method name and arguments included.  the body might be hard to
 
215
        # compute so is not included. If a call is None, that call can
 
216
        # be anything.
 
217
        self._expected_calls = None
 
218
        _SmartClient.__init__(self, FakeMedium(self._calls, fake_medium_base))
 
219
 
 
220
    def add_expected_call(self, call_name, call_args, response_type,
 
221
        response_args, response_body=None):
 
222
        if self._expected_calls is None:
 
223
            self._expected_calls = []
 
224
        self._expected_calls.append((call_name, call_args))
 
225
        self.responses.append((response_type, response_args, response_body))
 
226
 
 
227
    def add_success_response(self, *args):
 
228
        self.responses.append(('success', args, None))
 
229
 
 
230
    def add_success_response_with_body(self, body, *args):
 
231
        self.responses.append(('success', args, body))
 
232
        if self._expected_calls is not None:
 
233
            self._expected_calls.append(None)
 
234
 
 
235
    def add_error_response(self, *args):
 
236
        self.responses.append(('error', args))
 
237
 
 
238
    def add_unknown_method_response(self, verb):
 
239
        self.responses.append(('unknown', verb))
 
240
 
 
241
    def finished_test(self):
 
242
        if self._expected_calls:
 
243
            raise AssertionError("%r finished but was still expecting %r"
 
244
                % (self, self._expected_calls[0]))
 
245
 
 
246
    def _get_next_response(self):
 
247
        try:
 
248
            response_tuple = self.responses.pop(0)
 
249
        except IndexError, e:
 
250
            raise AssertionError("%r didn't expect any more calls"
 
251
                % (self,))
 
252
        if response_tuple[0] == 'unknown':
 
253
            raise errors.UnknownSmartMethod(response_tuple[1])
 
254
        elif response_tuple[0] == 'error':
 
255
            raise errors.ErrorFromSmartServer(response_tuple[1])
 
256
        return response_tuple
 
257
 
 
258
    def _check_call(self, method, args):
 
259
        if self._expected_calls is None:
 
260
            # the test should be updated to say what it expects
 
261
            return
 
262
        try:
 
263
            next_call = self._expected_calls.pop(0)
 
264
        except IndexError:
 
265
            raise AssertionError("%r didn't expect any more calls "
 
266
                "but got %r%r"
 
267
                % (self, method, args,))
 
268
        if next_call is None:
 
269
            return
 
270
        if method != next_call[0] or args != next_call[1]:
 
271
            raise AssertionError("%r expected %r%r "
 
272
                "but got %r%r"
 
273
                % (self, next_call[0], next_call[1], method, args,))
128
274
 
129
275
    def call(self, method, *args):
 
276
        self._check_call(method, args)
130
277
        self._calls.append(('call', method, args))
131
 
        return self.responses.pop(0)[0]
 
278
        return self._get_next_response()[1]
132
279
 
133
280
    def call_expecting_body(self, method, *args):
 
281
        self._check_call(method, args)
134
282
        self._calls.append(('call_expecting_body', method, args))
135
 
        result = self.responses.pop(0)
136
 
        return result[0], FakeProtocol(result[1])
137
 
 
138
 
 
139
 
class TestBzrDirOpenBranch(tests.TestCase):
 
283
        result = self._get_next_response()
 
284
        self.expecting_body = True
 
285
        return result[1], FakeProtocol(result[2], self)
 
286
 
 
287
    def call_with_body_bytes(self, method, args, body):
 
288
        self._check_call(method, args)
 
289
        self._calls.append(('call_with_body_bytes', method, args, body))
 
290
        result = self._get_next_response()
 
291
        return result[1], FakeProtocol(result[2], self)
 
292
 
 
293
    def call_with_body_bytes_expecting_body(self, method, args, body):
 
294
        self._check_call(method, args)
 
295
        self._calls.append(('call_with_body_bytes_expecting_body', method,
 
296
            args, body))
 
297
        result = self._get_next_response()
 
298
        self.expecting_body = True
 
299
        return result[1], FakeProtocol(result[2], self)
 
300
 
 
301
    def call_with_body_stream(self, args, stream):
 
302
        # Explicitly consume the stream before checking for an error, because
 
303
        # that's what happens a real medium.
 
304
        stream = list(stream)
 
305
        self._check_call(args[0], args[1:])
 
306
        self._calls.append(('call_with_body_stream', args[0], args[1:], stream))
 
307
        result = self._get_next_response()
 
308
        # The second value returned from call_with_body_stream is supposed to
 
309
        # be a response_handler object, but so far no tests depend on that.
 
310
        response_handler = None 
 
311
        return result[1], response_handler
 
312
 
 
313
 
 
314
class FakeMedium(medium.SmartClientMedium):
 
315
 
 
316
    def __init__(self, client_calls, base):
 
317
        medium.SmartClientMedium.__init__(self, base)
 
318
        self._client_calls = client_calls
 
319
 
 
320
    def disconnect(self):
 
321
        self._client_calls.append(('disconnect medium',))
 
322
 
 
323
 
 
324
class TestVfsHas(tests.TestCase):
 
325
 
 
326
    def test_unicode_path(self):
 
327
        client = FakeClient('/')
 
328
        client.add_success_response('yes',)
 
329
        transport = RemoteTransport('bzr://localhost/', _client=client)
 
330
        filename = u'/hell\u00d8'.encode('utf8')
 
331
        result = transport.has(filename)
 
332
        self.assertEqual(
 
333
            [('call', 'has', (filename,))],
 
334
            client._calls)
 
335
        self.assertTrue(result)
 
336
 
 
337
 
 
338
class TestRemote(tests.TestCaseWithMemoryTransport):
 
339
 
 
340
    def get_branch_format(self):
 
341
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
342
        return reference_bzrdir_format.get_branch_format()
 
343
 
 
344
    def get_repo_format(self):
 
345
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
346
        return reference_bzrdir_format.repository_format
 
347
 
 
348
    def assertFinished(self, fake_client):
 
349
        """Assert that all of a FakeClient's expected calls have occurred."""
 
350
        fake_client.finished_test()
 
351
 
 
352
 
 
353
class Test_ClientMedium_remote_path_from_transport(tests.TestCase):
 
354
    """Tests for the behaviour of client_medium.remote_path_from_transport."""
 
355
 
 
356
    def assertRemotePath(self, expected, client_base, transport_base):
 
357
        """Assert that the result of
 
358
        SmartClientMedium.remote_path_from_transport is the expected value for
 
359
        a given client_base and transport_base.
 
360
        """
 
361
        client_medium = medium.SmartClientMedium(client_base)
 
362
        transport = get_transport(transport_base)
 
363
        result = client_medium.remote_path_from_transport(transport)
 
364
        self.assertEqual(expected, result)
 
365
 
 
366
    def test_remote_path_from_transport(self):
 
367
        """SmartClientMedium.remote_path_from_transport calculates a URL for
 
368
        the given transport relative to the root of the client base URL.
 
369
        """
 
370
        self.assertRemotePath('xyz/', 'bzr://host/path', 'bzr://host/xyz')
 
371
        self.assertRemotePath(
 
372
            'path/xyz/', 'bzr://host/path', 'bzr://host/path/xyz')
 
373
 
 
374
    def assertRemotePathHTTP(self, expected, transport_base, relpath):
 
375
        """Assert that the result of
 
376
        HttpTransportBase.remote_path_from_transport is the expected value for
 
377
        a given transport_base and relpath of that transport.  (Note that
 
378
        HttpTransportBase is a subclass of SmartClientMedium)
 
379
        """
 
380
        base_transport = get_transport(transport_base)
 
381
        client_medium = base_transport.get_smart_medium()
 
382
        cloned_transport = base_transport.clone(relpath)
 
383
        result = client_medium.remote_path_from_transport(cloned_transport)
 
384
        self.assertEqual(expected, result)
 
385
 
 
386
    def test_remote_path_from_transport_http(self):
 
387
        """Remote paths for HTTP transports are calculated differently to other
 
388
        transports.  They are just relative to the client base, not the root
 
389
        directory of the host.
 
390
        """
 
391
        for scheme in ['http:', 'https:', 'bzr+http:', 'bzr+https:']:
 
392
            self.assertRemotePathHTTP(
 
393
                '../xyz/', scheme + '//host/path', '../xyz/')
 
394
            self.assertRemotePathHTTP(
 
395
                'xyz/', scheme + '//host/path', 'xyz/')
 
396
 
 
397
 
 
398
class Test_ClientMedium_remote_is_at_least(tests.TestCase):
 
399
    """Tests for the behaviour of client_medium.remote_is_at_least."""
 
400
 
 
401
    def test_initially_unlimited(self):
 
402
        """A fresh medium assumes that the remote side supports all
 
403
        versions.
 
404
        """
 
405
        client_medium = medium.SmartClientMedium('dummy base')
 
406
        self.assertFalse(client_medium._is_remote_before((99, 99)))
 
407
 
 
408
    def test__remember_remote_is_before(self):
 
409
        """Calling _remember_remote_is_before ratchets down the known remote
 
410
        version.
 
411
        """
 
412
        client_medium = medium.SmartClientMedium('dummy base')
 
413
        # Mark the remote side as being less than 1.6.  The remote side may
 
414
        # still be 1.5.
 
415
        client_medium._remember_remote_is_before((1, 6))
 
416
        self.assertTrue(client_medium._is_remote_before((1, 6)))
 
417
        self.assertFalse(client_medium._is_remote_before((1, 5)))
 
418
        # Calling _remember_remote_is_before again with a lower value works.
 
419
        client_medium._remember_remote_is_before((1, 5))
 
420
        self.assertTrue(client_medium._is_remote_before((1, 5)))
 
421
        # If you call _remember_remote_is_before with a higher value it logs a
 
422
        # warning, and continues to remember the lower value.
 
423
        self.assertNotContainsRe(self.get_log(), '_remember_remote_is_before')
 
424
        client_medium._remember_remote_is_before((1, 9))
 
425
        self.assertContainsRe(self.get_log(), '_remember_remote_is_before')
 
426
        self.assertTrue(client_medium._is_remote_before((1, 5)))
 
427
 
 
428
 
 
429
class TestBzrDirCloningMetaDir(TestRemote):
 
430
 
 
431
    def test_backwards_compat(self):
 
432
        self.setup_smart_server_with_call_log()
 
433
        a_dir = self.make_bzrdir('.')
 
434
        self.reset_smart_call_log()
 
435
        verb = 'BzrDir.cloning_metadir'
 
436
        self.disable_verb(verb)
 
437
        format = a_dir.cloning_metadir()
 
438
        call_count = len([call for call in self.hpss_calls if
 
439
            call.call.method == verb])
 
440
        self.assertEqual(1, call_count)
 
441
 
 
442
    def test_branch_reference(self):
 
443
        transport = self.get_transport('quack')
 
444
        referenced = self.make_branch('referenced')
 
445
        expected = referenced.bzrdir.cloning_metadir()
 
446
        client = FakeClient(transport.base)
 
447
        client.add_expected_call(
 
448
            'BzrDir.cloning_metadir', ('quack/', 'False'),
 
449
            'error', ('BranchReference',)),
 
450
        client.add_expected_call(
 
451
            'BzrDir.open_branchV3', ('quack/',),
 
452
            'success', ('ref', self.get_url('referenced'))),
 
453
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
454
            _client=client)
 
455
        result = a_bzrdir.cloning_metadir()
 
456
        # We should have got a control dir matching the referenced branch.
 
457
        self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
 
458
        self.assertEqual(expected._repository_format, result._repository_format)
 
459
        self.assertEqual(expected._branch_format, result._branch_format)
 
460
        self.assertFinished(client)
 
461
 
 
462
    def test_current_server(self):
 
463
        transport = self.get_transport('.')
 
464
        transport = transport.clone('quack')
 
465
        self.make_bzrdir('quack')
 
466
        client = FakeClient(transport.base)
 
467
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
468
        control_name = reference_bzrdir_format.network_name()
 
469
        client.add_expected_call(
 
470
            'BzrDir.cloning_metadir', ('quack/', 'False'),
 
471
            'success', (control_name, '', ('branch', ''))),
 
472
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
473
            _client=client)
 
474
        result = a_bzrdir.cloning_metadir()
 
475
        # We should have got a reference control dir with default branch and
 
476
        # repository formats.
 
477
        # This pokes a little, just to be sure.
 
478
        self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
 
479
        self.assertEqual(None, result._repository_format)
 
480
        self.assertEqual(None, result._branch_format)
 
481
        self.assertFinished(client)
 
482
 
 
483
 
 
484
class TestBzrDirOpen(TestRemote):
 
485
 
 
486
    def make_fake_client_and_transport(self, path='quack'):
 
487
        transport = MemoryTransport()
 
488
        transport.mkdir(path)
 
489
        transport = transport.clone(path)
 
490
        client = FakeClient(transport.base)
 
491
        return client, transport
 
492
 
 
493
    def test_absent(self):
 
494
        client, transport = self.make_fake_client_and_transport()
 
495
        client.add_expected_call(
 
496
            'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
 
497
        self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
 
498
                remote.RemoteBzrDirFormat(), _client=client, _force_probe=True)
 
499
        self.assertFinished(client)
 
500
 
 
501
    def test_present_without_workingtree(self):
 
502
        client, transport = self.make_fake_client_and_transport()
 
503
        client.add_expected_call(
 
504
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
 
505
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
506
            _client=client, _force_probe=True)
 
507
        self.assertIsInstance(bd, RemoteBzrDir)
 
508
        self.assertFalse(bd.has_workingtree())
 
509
        self.assertRaises(errors.NoWorkingTree, bd.open_workingtree)
 
510
        self.assertFinished(client)
 
511
 
 
512
    def test_present_with_workingtree(self):
 
513
        client, transport = self.make_fake_client_and_transport()
 
514
        client.add_expected_call(
 
515
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
 
516
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
517
            _client=client, _force_probe=True)
 
518
        self.assertIsInstance(bd, RemoteBzrDir)
 
519
        self.assertTrue(bd.has_workingtree())
 
520
        self.assertRaises(errors.NotLocalUrl, bd.open_workingtree)
 
521
        self.assertFinished(client)
 
522
 
 
523
    def test_backwards_compat(self):
 
524
        client, transport = self.make_fake_client_and_transport()
 
525
        client.add_expected_call(
 
526
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
 
527
        client.add_expected_call(
 
528
            'BzrDir.open', ('quack/',), 'success', ('yes',))
 
529
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
530
            _client=client, _force_probe=True)
 
531
        self.assertIsInstance(bd, RemoteBzrDir)
 
532
        self.assertFinished(client)
 
533
 
 
534
    def test_backwards_compat_hpss_v2(self):
 
535
        client, transport = self.make_fake_client_and_transport()
 
536
        # Monkey-patch fake client to simulate real-world behaviour with v2
 
537
        # server: upon first RPC call detect the protocol version, and because
 
538
        # the version is 2 also do _remember_remote_is_before((1, 6)) before
 
539
        # continuing with the RPC.
 
540
        orig_check_call = client._check_call
 
541
        def check_call(method, args):
 
542
            client._medium._protocol_version = 2
 
543
            client._medium._remember_remote_is_before((1, 6))
 
544
            client._check_call = orig_check_call
 
545
            client._check_call(method, args)
 
546
        client._check_call = check_call
 
547
        client.add_expected_call(
 
548
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
 
549
        client.add_expected_call(
 
550
            'BzrDir.open', ('quack/',), 'success', ('yes',))
 
551
        bd = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
552
            _client=client, _force_probe=True)
 
553
        self.assertIsInstance(bd, RemoteBzrDir)
 
554
        self.assertFinished(client)
 
555
 
 
556
 
 
557
class TestBzrDirOpenBranch(TestRemote):
 
558
 
 
559
    def test_backwards_compat(self):
 
560
        self.setup_smart_server_with_call_log()
 
561
        self.make_branch('.')
 
562
        a_dir = BzrDir.open(self.get_url('.'))
 
563
        self.reset_smart_call_log()
 
564
        verb = 'BzrDir.open_branchV3'
 
565
        self.disable_verb(verb)
 
566
        format = a_dir.open_branch()
 
567
        call_count = len([call for call in self.hpss_calls if
 
568
            call.call.method == verb])
 
569
        self.assertEqual(1, call_count)
140
570
 
141
571
    def test_branch_present(self):
142
 
        client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no'), )])
 
572
        reference_format = self.get_repo_format()
 
573
        network_name = reference_format.network_name()
 
574
        branch_network_name = self.get_branch_format().network_name()
143
575
        transport = MemoryTransport()
144
576
        transport.mkdir('quack')
145
577
        transport = transport.clone('quack')
146
 
        bzrdir = RemoteBzrDir(transport, _client=client)
 
578
        client = FakeClient(transport.base)
 
579
        client.add_expected_call(
 
580
            'BzrDir.open_branchV3', ('quack/',),
 
581
            'success', ('branch', branch_network_name))
 
582
        client.add_expected_call(
 
583
            'BzrDir.find_repositoryV3', ('quack/',),
 
584
            'success', ('ok', '', 'no', 'no', 'no', network_name))
 
585
        client.add_expected_call(
 
586
            'Branch.get_stacked_on_url', ('quack/',),
 
587
            'error', ('NotStacked',))
 
588
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
589
            _client=client)
147
590
        result = bzrdir.open_branch()
148
 
        self.assertEqual(
149
 
            [('call', 'BzrDir.open_branch', ('///quack/',)),
150
 
             ('call', 'BzrDir.find_repository', ('///quack/',))],
151
 
            client._calls)
152
591
        self.assertIsInstance(result, RemoteBranch)
153
592
        self.assertEqual(bzrdir, result.bzrdir)
 
593
        self.assertFinished(client)
154
594
 
155
595
    def test_branch_missing(self):
156
 
        client = FakeClient([(('nobranch',), )])
157
596
        transport = MemoryTransport()
158
597
        transport.mkdir('quack')
159
598
        transport = transport.clone('quack')
160
 
        bzrdir = RemoteBzrDir(transport, _client=client)
 
599
        client = FakeClient(transport.base)
 
600
        client.add_error_response('nobranch')
 
601
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
602
            _client=client)
161
603
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
162
604
        self.assertEqual(
163
 
            [('call', 'BzrDir.open_branch', ('///quack/',))],
 
605
            [('call', 'BzrDir.open_branchV3', ('quack/',))],
164
606
            client._calls)
165
607
 
166
 
    def check_open_repository(self, rich_root, subtrees):
 
608
    def test__get_tree_branch(self):
 
609
        # _get_tree_branch is a form of open_branch, but it should only ask for
 
610
        # branch opening, not any other network requests.
 
611
        calls = []
 
612
        def open_branch(name=None):
 
613
            calls.append("Called")
 
614
            return "a-branch"
 
615
        transport = MemoryTransport()
 
616
        # no requests on the network - catches other api calls being made.
 
617
        client = FakeClient(transport.base)
 
618
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
619
            _client=client)
 
620
        # patch the open_branch call to record that it was called.
 
621
        bzrdir.open_branch = open_branch
 
622
        self.assertEqual((None, "a-branch"), bzrdir._get_tree_branch())
 
623
        self.assertEqual(["Called"], calls)
 
624
        self.assertEqual([], client._calls)
 
625
 
 
626
    def test_url_quoting_of_path(self):
 
627
        # Relpaths on the wire should not be URL-escaped.  So "~" should be
 
628
        # transmitted as "~", not "%7E".
 
629
        transport = RemoteTCPTransport('bzr://localhost/~hello/')
 
630
        client = FakeClient(transport.base)
 
631
        reference_format = self.get_repo_format()
 
632
        network_name = reference_format.network_name()
 
633
        branch_network_name = self.get_branch_format().network_name()
 
634
        client.add_expected_call(
 
635
            'BzrDir.open_branchV3', ('~hello/',),
 
636
            'success', ('branch', branch_network_name))
 
637
        client.add_expected_call(
 
638
            'BzrDir.find_repositoryV3', ('~hello/',),
 
639
            'success', ('ok', '', 'no', 'no', 'no', network_name))
 
640
        client.add_expected_call(
 
641
            'Branch.get_stacked_on_url', ('~hello/',),
 
642
            'error', ('NotStacked',))
 
643
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
644
            _client=client)
 
645
        result = bzrdir.open_branch()
 
646
        self.assertFinished(client)
 
647
 
 
648
    def check_open_repository(self, rich_root, subtrees, external_lookup='no'):
 
649
        reference_format = self.get_repo_format()
 
650
        network_name = reference_format.network_name()
 
651
        transport = MemoryTransport()
 
652
        transport.mkdir('quack')
 
653
        transport = transport.clone('quack')
167
654
        if rich_root:
168
655
            rich_response = 'yes'
169
656
        else:
172
659
            subtree_response = 'yes'
173
660
        else:
174
661
            subtree_response = 'no'
175
 
        client = FakeClient([(('ok', '', rich_response, subtree_response), ),])
176
 
        transport = MemoryTransport()
177
 
        transport.mkdir('quack')
178
 
        transport = transport.clone('quack')
179
 
        bzrdir = RemoteBzrDir(transport, _client=client)
 
662
        client = FakeClient(transport.base)
 
663
        client.add_success_response(
 
664
            'ok', '', rich_response, subtree_response, external_lookup,
 
665
            network_name)
 
666
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
667
            _client=client)
180
668
        result = bzrdir.open_repository()
181
669
        self.assertEqual(
182
 
            [('call', 'BzrDir.find_repository', ('///quack/',))],
 
670
            [('call', 'BzrDir.find_repositoryV3', ('quack/',))],
183
671
            client._calls)
184
672
        self.assertIsInstance(result, RemoteRepository)
185
673
        self.assertEqual(bzrdir, result.bzrdir)
191
679
        self.check_open_repository(False, True)
192
680
        self.check_open_repository(True, False)
193
681
        self.check_open_repository(False, False)
 
682
        self.check_open_repository(False, False, 'yes')
194
683
 
195
684
    def test_old_server(self):
196
685
        """RemoteBzrDirFormat should fail to probe if the server version is too
200
689
            RemoteBzrDirFormat.probe_transport, OldServerTransport())
201
690
 
202
691
 
 
692
class TestBzrDirCreateBranch(TestRemote):
 
693
 
 
694
    def test_backwards_compat(self):
 
695
        self.setup_smart_server_with_call_log()
 
696
        repo = self.make_repository('.')
 
697
        self.reset_smart_call_log()
 
698
        self.disable_verb('BzrDir.create_branch')
 
699
        branch = repo.bzrdir.create_branch()
 
700
        create_branch_call_count = len([call for call in self.hpss_calls if
 
701
            call.call.method == 'BzrDir.create_branch'])
 
702
        self.assertEqual(1, create_branch_call_count)
 
703
 
 
704
    def test_current_server(self):
 
705
        transport = self.get_transport('.')
 
706
        transport = transport.clone('quack')
 
707
        self.make_repository('quack')
 
708
        client = FakeClient(transport.base)
 
709
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
710
        reference_format = reference_bzrdir_format.get_branch_format()
 
711
        network_name = reference_format.network_name()
 
712
        reference_repo_fmt = reference_bzrdir_format.repository_format
 
713
        reference_repo_name = reference_repo_fmt.network_name()
 
714
        client.add_expected_call(
 
715
            'BzrDir.create_branch', ('quack/', network_name),
 
716
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
 
717
            reference_repo_name))
 
718
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
719
            _client=client)
 
720
        branch = a_bzrdir.create_branch()
 
721
        # We should have got a remote branch
 
722
        self.assertIsInstance(branch, remote.RemoteBranch)
 
723
        # its format should have the settings from the response
 
724
        format = branch._format
 
725
        self.assertEqual(network_name, format.network_name())
 
726
 
 
727
 
 
728
class TestBzrDirCreateRepository(TestRemote):
 
729
 
 
730
    def test_backwards_compat(self):
 
731
        self.setup_smart_server_with_call_log()
 
732
        bzrdir = self.make_bzrdir('.')
 
733
        self.reset_smart_call_log()
 
734
        self.disable_verb('BzrDir.create_repository')
 
735
        repo = bzrdir.create_repository()
 
736
        create_repo_call_count = len([call for call in self.hpss_calls if
 
737
            call.call.method == 'BzrDir.create_repository'])
 
738
        self.assertEqual(1, create_repo_call_count)
 
739
 
 
740
    def test_current_server(self):
 
741
        transport = self.get_transport('.')
 
742
        transport = transport.clone('quack')
 
743
        self.make_bzrdir('quack')
 
744
        client = FakeClient(transport.base)
 
745
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
 
746
        reference_format = reference_bzrdir_format.repository_format
 
747
        network_name = reference_format.network_name()
 
748
        client.add_expected_call(
 
749
            'BzrDir.create_repository', ('quack/',
 
750
                'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
 
751
                'False'),
 
752
            'success', ('ok', 'yes', 'yes', 'yes', network_name))
 
753
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
754
            _client=client)
 
755
        repo = a_bzrdir.create_repository()
 
756
        # We should have got a remote repository
 
757
        self.assertIsInstance(repo, remote.RemoteRepository)
 
758
        # its format should have the settings from the response
 
759
        format = repo._format
 
760
        self.assertTrue(format.rich_root_data)
 
761
        self.assertTrue(format.supports_tree_reference)
 
762
        self.assertTrue(format.supports_external_lookups)
 
763
        self.assertEqual(network_name, format.network_name())
 
764
 
 
765
 
 
766
class TestBzrDirOpenRepository(TestRemote):
 
767
 
 
768
    def test_backwards_compat_1_2_3(self):
 
769
        # fallback all the way to the first version.
 
770
        reference_format = self.get_repo_format()
 
771
        network_name = reference_format.network_name()
 
772
        server_url = 'bzr://example.com/'
 
773
        self.permit_url(server_url)
 
774
        client = FakeClient(server_url)
 
775
        client.add_unknown_method_response('BzrDir.find_repositoryV3')
 
776
        client.add_unknown_method_response('BzrDir.find_repositoryV2')
 
777
        client.add_success_response('ok', '', 'no', 'no')
 
778
        # A real repository instance will be created to determine the network
 
779
        # name.
 
780
        client.add_success_response_with_body(
 
781
            "Bazaar-NG meta directory, format 1\n", 'ok')
 
782
        client.add_success_response_with_body(
 
783
            reference_format.get_format_string(), 'ok')
 
784
        # PackRepository wants to do a stat
 
785
        client.add_success_response('stat', '0', '65535')
 
786
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
 
787
            _client=client)
 
788
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
 
789
            _client=client)
 
790
        repo = bzrdir.open_repository()
 
791
        self.assertEqual(
 
792
            [('call', 'BzrDir.find_repositoryV3', ('quack/',)),
 
793
             ('call', 'BzrDir.find_repositoryV2', ('quack/',)),
 
794
             ('call', 'BzrDir.find_repository', ('quack/',)),
 
795
             ('call_expecting_body', 'get', ('/quack/.bzr/branch-format',)),
 
796
             ('call_expecting_body', 'get', ('/quack/.bzr/repository/format',)),
 
797
             ('call', 'stat', ('/quack/.bzr/repository',)),
 
798
             ],
 
799
            client._calls)
 
800
        self.assertEqual(network_name, repo._format.network_name())
 
801
 
 
802
    def test_backwards_compat_2(self):
 
803
        # fallback to find_repositoryV2
 
804
        reference_format = self.get_repo_format()
 
805
        network_name = reference_format.network_name()
 
806
        server_url = 'bzr://example.com/'
 
807
        self.permit_url(server_url)
 
808
        client = FakeClient(server_url)
 
809
        client.add_unknown_method_response('BzrDir.find_repositoryV3')
 
810
        client.add_success_response('ok', '', 'no', 'no', 'no')
 
811
        # A real repository instance will be created to determine the network
 
812
        # name.
 
813
        client.add_success_response_with_body(
 
814
            "Bazaar-NG meta directory, format 1\n", 'ok')
 
815
        client.add_success_response_with_body(
 
816
            reference_format.get_format_string(), 'ok')
 
817
        # PackRepository wants to do a stat
 
818
        client.add_success_response('stat', '0', '65535')
 
819
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
 
820
            _client=client)
 
821
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
 
822
            _client=client)
 
823
        repo = bzrdir.open_repository()
 
824
        self.assertEqual(
 
825
            [('call', 'BzrDir.find_repositoryV3', ('quack/',)),
 
826
             ('call', 'BzrDir.find_repositoryV2', ('quack/',)),
 
827
             ('call_expecting_body', 'get', ('/quack/.bzr/branch-format',)),
 
828
             ('call_expecting_body', 'get', ('/quack/.bzr/repository/format',)),
 
829
             ('call', 'stat', ('/quack/.bzr/repository',)),
 
830
             ],
 
831
            client._calls)
 
832
        self.assertEqual(network_name, repo._format.network_name())
 
833
 
 
834
    def test_current_server(self):
 
835
        reference_format = self.get_repo_format()
 
836
        network_name = reference_format.network_name()
 
837
        transport = MemoryTransport()
 
838
        transport.mkdir('quack')
 
839
        transport = transport.clone('quack')
 
840
        client = FakeClient(transport.base)
 
841
        client.add_success_response('ok', '', 'no', 'no', 'no', network_name)
 
842
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
843
            _client=client)
 
844
        repo = bzrdir.open_repository()
 
845
        self.assertEqual(
 
846
            [('call', 'BzrDir.find_repositoryV3', ('quack/',))],
 
847
            client._calls)
 
848
        self.assertEqual(network_name, repo._format.network_name())
 
849
 
 
850
 
 
851
class TestBzrDirFormatInitializeEx(TestRemote):
 
852
 
 
853
    def test_success(self):
 
854
        """Simple test for typical successful call."""
 
855
        fmt = bzrdir.RemoteBzrDirFormat()
 
856
        default_format_name = BzrDirFormat.get_default_format().network_name()
 
857
        transport = self.get_transport()
 
858
        client = FakeClient(transport.base)
 
859
        client.add_expected_call(
 
860
            'BzrDirFormat.initialize_ex_1.16',
 
861
                (default_format_name, 'path', 'False', 'False', 'False', '',
 
862
                 '', '', '', 'False'),
 
863
            'success',
 
864
                ('.', 'no', 'no', 'yes', 'repo fmt', 'repo bzrdir fmt',
 
865
                 'bzrdir fmt', 'False', '', '', 'repo lock token'))
 
866
        # XXX: It would be better to call fmt.initialize_on_transport_ex, but
 
867
        # it's currently hard to test that without supplying a real remote
 
868
        # transport connected to a real server.
 
869
        result = fmt._initialize_on_transport_ex_rpc(client, 'path',
 
870
            transport, False, False, False, None, None, None, None, False)
 
871
        self.assertFinished(client)
 
872
 
 
873
    def test_error(self):
 
874
        """Error responses are translated, e.g. 'PermissionDenied' raises the
 
875
        corresponding error from the client.
 
876
        """
 
877
        fmt = bzrdir.RemoteBzrDirFormat()
 
878
        default_format_name = BzrDirFormat.get_default_format().network_name()
 
879
        transport = self.get_transport()
 
880
        client = FakeClient(transport.base)
 
881
        client.add_expected_call(
 
882
            'BzrDirFormat.initialize_ex_1.16',
 
883
                (default_format_name, 'path', 'False', 'False', 'False', '',
 
884
                 '', '', '', 'False'),
 
885
            'error',
 
886
                ('PermissionDenied', 'path', 'extra info'))
 
887
        # XXX: It would be better to call fmt.initialize_on_transport_ex, but
 
888
        # it's currently hard to test that without supplying a real remote
 
889
        # transport connected to a real server.
 
890
        err = self.assertRaises(errors.PermissionDenied,
 
891
            fmt._initialize_on_transport_ex_rpc, client, 'path', transport,
 
892
            False, False, False, None, None, None, None, False)
 
893
        self.assertEqual('path', err.path)
 
894
        self.assertEqual(': extra info', err.extra)
 
895
        self.assertFinished(client)
 
896
 
 
897
    def test_error_from_real_server(self):
 
898
        """Integration test for error translation."""
 
899
        transport = self.make_smart_server('foo')
 
900
        transport = transport.clone('no-such-path')
 
901
        fmt = bzrdir.RemoteBzrDirFormat()
 
902
        err = self.assertRaises(errors.NoSuchFile,
 
903
            fmt.initialize_on_transport_ex, transport, create_prefix=False)
 
904
 
 
905
 
203
906
class OldSmartClient(object):
204
907
    """A fake smart client for test_old_version that just returns a version one
205
908
    response to the 'hello' (query version) command.
212
915
            input_file, output_file)
213
916
        return medium.SmartClientStreamMediumRequest(client_medium)
214
917
 
 
918
    def protocol_version(self):
 
919
        return 1
 
920
 
215
921
 
216
922
class OldServerTransport(object):
217
923
    """A fake transport for test_old_server that reports it's smart server
225
931
        return OldSmartClient()
226
932
 
227
933
 
228
 
class TestBranchLastRevisionInfo(tests.TestCase):
 
934
class RemoteBzrDirTestCase(TestRemote):
 
935
 
 
936
    def make_remote_bzrdir(self, transport, client):
 
937
        """Make a RemotebzrDir using 'client' as the _client."""
 
938
        return RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
939
            _client=client)
 
940
 
 
941
 
 
942
class RemoteBranchTestCase(RemoteBzrDirTestCase):
 
943
 
 
944
    def lock_remote_branch(self, branch):
 
945
        """Trick a RemoteBranch into thinking it is locked."""
 
946
        branch._lock_mode = 'w'
 
947
        branch._lock_count = 2
 
948
        branch._lock_token = 'branch token'
 
949
        branch._repo_lock_token = 'repo token'
 
950
        branch.repository._lock_mode = 'w'
 
951
        branch.repository._lock_count = 2
 
952
        branch.repository._lock_token = 'repo token'
 
953
 
 
954
    def make_remote_branch(self, transport, client):
 
955
        """Make a RemoteBranch using 'client' as its _SmartClient.
 
956
 
 
957
        A RemoteBzrDir and RemoteRepository will also be created to fill out
 
958
        the RemoteBranch, albeit with stub values for some of their attributes.
 
959
        """
 
960
        # we do not want bzrdir to make any remote calls, so use False as its
 
961
        # _client.  If it tries to make a remote call, this will fail
 
962
        # immediately.
 
963
        bzrdir = self.make_remote_bzrdir(transport, False)
 
964
        repo = RemoteRepository(bzrdir, None, _client=client)
 
965
        branch_format = self.get_branch_format()
 
966
        format = RemoteBranchFormat(network_name=branch_format.network_name())
 
967
        return RemoteBranch(bzrdir, repo, _client=client, format=format)
 
968
 
 
969
 
 
970
class TestBranchGetParent(RemoteBranchTestCase):
 
971
 
 
972
    def test_no_parent(self):
 
973
        # in an empty branch we decode the response properly
 
974
        transport = MemoryTransport()
 
975
        client = FakeClient(transport.base)
 
976
        client.add_expected_call(
 
977
            'Branch.get_stacked_on_url', ('quack/',),
 
978
            'error', ('NotStacked',))
 
979
        client.add_expected_call(
 
980
            'Branch.get_parent', ('quack/',),
 
981
            'success', ('',))
 
982
        transport.mkdir('quack')
 
983
        transport = transport.clone('quack')
 
984
        branch = self.make_remote_branch(transport, client)
 
985
        result = branch.get_parent()
 
986
        self.assertFinished(client)
 
987
        self.assertEqual(None, result)
 
988
 
 
989
    def test_parent_relative(self):
 
990
        transport = MemoryTransport()
 
991
        client = FakeClient(transport.base)
 
992
        client.add_expected_call(
 
993
            'Branch.get_stacked_on_url', ('kwaak/',),
 
994
            'error', ('NotStacked',))
 
995
        client.add_expected_call(
 
996
            'Branch.get_parent', ('kwaak/',),
 
997
            'success', ('../foo/',))
 
998
        transport.mkdir('kwaak')
 
999
        transport = transport.clone('kwaak')
 
1000
        branch = self.make_remote_branch(transport, client)
 
1001
        result = branch.get_parent()
 
1002
        self.assertEqual(transport.clone('../foo').base, result)
 
1003
 
 
1004
    def test_parent_absolute(self):
 
1005
        transport = MemoryTransport()
 
1006
        client = FakeClient(transport.base)
 
1007
        client.add_expected_call(
 
1008
            'Branch.get_stacked_on_url', ('kwaak/',),
 
1009
            'error', ('NotStacked',))
 
1010
        client.add_expected_call(
 
1011
            'Branch.get_parent', ('kwaak/',),
 
1012
            'success', ('http://foo/',))
 
1013
        transport.mkdir('kwaak')
 
1014
        transport = transport.clone('kwaak')
 
1015
        branch = self.make_remote_branch(transport, client)
 
1016
        result = branch.get_parent()
 
1017
        self.assertEqual('http://foo/', result)
 
1018
        self.assertFinished(client)
 
1019
 
 
1020
 
 
1021
class TestBranchSetParentLocation(RemoteBranchTestCase):
 
1022
 
 
1023
    def test_no_parent(self):
 
1024
        # We call the verb when setting parent to None
 
1025
        transport = MemoryTransport()
 
1026
        client = FakeClient(transport.base)
 
1027
        client.add_expected_call(
 
1028
            'Branch.get_stacked_on_url', ('quack/',),
 
1029
            'error', ('NotStacked',))
 
1030
        client.add_expected_call(
 
1031
            'Branch.set_parent_location', ('quack/', 'b', 'r', ''),
 
1032
            'success', ())
 
1033
        transport.mkdir('quack')
 
1034
        transport = transport.clone('quack')
 
1035
        branch = self.make_remote_branch(transport, client)
 
1036
        branch._lock_token = 'b'
 
1037
        branch._repo_lock_token = 'r'
 
1038
        branch._set_parent_location(None)
 
1039
        self.assertFinished(client)
 
1040
 
 
1041
    def test_parent(self):
 
1042
        transport = MemoryTransport()
 
1043
        client = FakeClient(transport.base)
 
1044
        client.add_expected_call(
 
1045
            'Branch.get_stacked_on_url', ('kwaak/',),
 
1046
            'error', ('NotStacked',))
 
1047
        client.add_expected_call(
 
1048
            'Branch.set_parent_location', ('kwaak/', 'b', 'r', 'foo'),
 
1049
            'success', ())
 
1050
        transport.mkdir('kwaak')
 
1051
        transport = transport.clone('kwaak')
 
1052
        branch = self.make_remote_branch(transport, client)
 
1053
        branch._lock_token = 'b'
 
1054
        branch._repo_lock_token = 'r'
 
1055
        branch._set_parent_location('foo')
 
1056
        self.assertFinished(client)
 
1057
 
 
1058
    def test_backwards_compat(self):
 
1059
        self.setup_smart_server_with_call_log()
 
1060
        branch = self.make_branch('.')
 
1061
        self.reset_smart_call_log()
 
1062
        verb = 'Branch.set_parent_location'
 
1063
        self.disable_verb(verb)
 
1064
        branch.set_parent('http://foo/')
 
1065
        self.assertLength(12, self.hpss_calls)
 
1066
 
 
1067
 
 
1068
class TestBranchGetTagsBytes(RemoteBranchTestCase):
 
1069
 
 
1070
    def test_backwards_compat(self):
 
1071
        self.setup_smart_server_with_call_log()
 
1072
        branch = self.make_branch('.')
 
1073
        self.reset_smart_call_log()
 
1074
        verb = 'Branch.get_tags_bytes'
 
1075
        self.disable_verb(verb)
 
1076
        branch.tags.get_tag_dict()
 
1077
        call_count = len([call for call in self.hpss_calls if
 
1078
            call.call.method == verb])
 
1079
        self.assertEqual(1, call_count)
 
1080
 
 
1081
    def test_trivial(self):
 
1082
        transport = MemoryTransport()
 
1083
        client = FakeClient(transport.base)
 
1084
        client.add_expected_call(
 
1085
            'Branch.get_stacked_on_url', ('quack/',),
 
1086
            'error', ('NotStacked',))
 
1087
        client.add_expected_call(
 
1088
            'Branch.get_tags_bytes', ('quack/',),
 
1089
            'success', ('',))
 
1090
        transport.mkdir('quack')
 
1091
        transport = transport.clone('quack')
 
1092
        branch = self.make_remote_branch(transport, client)
 
1093
        result = branch.tags.get_tag_dict()
 
1094
        self.assertFinished(client)
 
1095
        self.assertEqual({}, result)
 
1096
 
 
1097
 
 
1098
class TestBranchSetTagsBytes(RemoteBranchTestCase):
 
1099
 
 
1100
    def test_trivial(self):
 
1101
        transport = MemoryTransport()
 
1102
        client = FakeClient(transport.base)
 
1103
        client.add_expected_call(
 
1104
            'Branch.get_stacked_on_url', ('quack/',),
 
1105
            'error', ('NotStacked',))
 
1106
        client.add_expected_call(
 
1107
            'Branch.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
 
1108
            'success', ('',))
 
1109
        transport.mkdir('quack')
 
1110
        transport = transport.clone('quack')
 
1111
        branch = self.make_remote_branch(transport, client)
 
1112
        self.lock_remote_branch(branch)
 
1113
        branch._set_tags_bytes('tags bytes')
 
1114
        self.assertFinished(client)
 
1115
        self.assertEqual('tags bytes', client._calls[-1][-1])
 
1116
 
 
1117
    def test_backwards_compatible(self):
 
1118
        transport = MemoryTransport()
 
1119
        client = FakeClient(transport.base)
 
1120
        client.add_expected_call(
 
1121
            'Branch.get_stacked_on_url', ('quack/',),
 
1122
            'error', ('NotStacked',))
 
1123
        client.add_expected_call(
 
1124
            'Branch.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
 
1125
            'unknown', ('Branch.set_tags_bytes',))
 
1126
        transport.mkdir('quack')
 
1127
        transport = transport.clone('quack')
 
1128
        branch = self.make_remote_branch(transport, client)
 
1129
        self.lock_remote_branch(branch)
 
1130
        class StubRealBranch(object):
 
1131
            def __init__(self):
 
1132
                self.calls = []
 
1133
            def _set_tags_bytes(self, bytes):
 
1134
                self.calls.append(('set_tags_bytes', bytes))
 
1135
        real_branch = StubRealBranch()
 
1136
        branch._real_branch = real_branch
 
1137
        branch._set_tags_bytes('tags bytes')
 
1138
        # Call a second time, to exercise the 'remote version already inferred'
 
1139
        # code path.
 
1140
        branch._set_tags_bytes('tags bytes')
 
1141
        self.assertFinished(client)
 
1142
        self.assertEqual(
 
1143
            [('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
 
1144
 
 
1145
 
 
1146
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
229
1147
 
230
1148
    def test_empty_branch(self):
231
1149
        # in an empty branch we decode the response properly
232
 
        client = FakeClient([(('ok', '0', 'null:'), )])
233
1150
        transport = MemoryTransport()
 
1151
        client = FakeClient(transport.base)
 
1152
        client.add_expected_call(
 
1153
            'Branch.get_stacked_on_url', ('quack/',),
 
1154
            'error', ('NotStacked',))
 
1155
        client.add_expected_call(
 
1156
            'Branch.last_revision_info', ('quack/',),
 
1157
            'success', ('ok', '0', 'null:'))
234
1158
        transport.mkdir('quack')
235
1159
        transport = transport.clone('quack')
236
 
        # we do not want bzrdir to make any remote calls
237
 
        bzrdir = RemoteBzrDir(transport, _client=False)
238
 
        branch = RemoteBranch(bzrdir, None, _client=client)
 
1160
        branch = self.make_remote_branch(transport, client)
239
1161
        result = branch.last_revision_info()
240
 
 
241
 
        self.assertEqual(
242
 
            [('call', 'Branch.last_revision_info', ('///quack/',))],
243
 
            client._calls)
 
1162
        self.assertFinished(client)
244
1163
        self.assertEqual((0, NULL_REVISION), result)
245
1164
 
246
1165
    def test_non_empty_branch(self):
247
1166
        # in a non-empty branch we also decode the response properly
248
1167
        revid = u'\xc8'.encode('utf8')
249
 
        client = FakeClient([(('ok', '2', revid), )])
250
1168
        transport = MemoryTransport()
 
1169
        client = FakeClient(transport.base)
 
1170
        client.add_expected_call(
 
1171
            'Branch.get_stacked_on_url', ('kwaak/',),
 
1172
            'error', ('NotStacked',))
 
1173
        client.add_expected_call(
 
1174
            'Branch.last_revision_info', ('kwaak/',),
 
1175
            'success', ('ok', '2', revid))
251
1176
        transport.mkdir('kwaak')
252
1177
        transport = transport.clone('kwaak')
253
 
        # we do not want bzrdir to make any remote calls
254
 
        bzrdir = RemoteBzrDir(transport, _client=False)
255
 
        branch = RemoteBranch(bzrdir, None, _client=client)
 
1178
        branch = self.make_remote_branch(transport, client)
256
1179
        result = branch.last_revision_info()
257
 
 
258
 
        self.assertEqual(
259
 
            [('call', 'Branch.last_revision_info', ('///kwaak/',))],
260
 
            client._calls)
261
1180
        self.assertEqual((2, revid), result)
262
1181
 
263
1182
 
264
 
class TestBranchSetLastRevision(tests.TestCase):
 
1183
class TestBranch_get_stacked_on_url(TestRemote):
 
1184
    """Test Branch._get_stacked_on_url rpc"""
 
1185
 
 
1186
    def test_get_stacked_on_invalid_url(self):
 
1187
        # test that asking for a stacked on url the server can't access works.
 
1188
        # This isn't perfect, but then as we're in the same process there
 
1189
        # really isn't anything we can do to be 100% sure that the server
 
1190
        # doesn't just open in - this test probably needs to be rewritten using
 
1191
        # a spawn()ed server.
 
1192
        stacked_branch = self.make_branch('stacked', format='1.9')
 
1193
        memory_branch = self.make_branch('base', format='1.9')
 
1194
        vfs_url = self.get_vfs_only_url('base')
 
1195
        stacked_branch.set_stacked_on_url(vfs_url)
 
1196
        transport = stacked_branch.bzrdir.root_transport
 
1197
        client = FakeClient(transport.base)
 
1198
        client.add_expected_call(
 
1199
            'Branch.get_stacked_on_url', ('stacked/',),
 
1200
            'success', ('ok', vfs_url))
 
1201
        # XXX: Multiple calls are bad, this second call documents what is
 
1202
        # today.
 
1203
        client.add_expected_call(
 
1204
            'Branch.get_stacked_on_url', ('stacked/',),
 
1205
            'success', ('ok', vfs_url))
 
1206
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
1207
            _client=client)
 
1208
        repo_fmt = remote.RemoteRepositoryFormat()
 
1209
        repo_fmt._custom_format = stacked_branch.repository._format
 
1210
        branch = RemoteBranch(bzrdir, RemoteRepository(bzrdir, repo_fmt),
 
1211
            _client=client)
 
1212
        result = branch.get_stacked_on_url()
 
1213
        self.assertEqual(vfs_url, result)
 
1214
 
 
1215
    def test_backwards_compatible(self):
 
1216
        # like with bzr1.6 with no Branch.get_stacked_on_url rpc
 
1217
        base_branch = self.make_branch('base', format='1.6')
 
1218
        stacked_branch = self.make_branch('stacked', format='1.6')
 
1219
        stacked_branch.set_stacked_on_url('../base')
 
1220
        client = FakeClient(self.get_url())
 
1221
        branch_network_name = self.get_branch_format().network_name()
 
1222
        client.add_expected_call(
 
1223
            'BzrDir.open_branchV3', ('stacked/',),
 
1224
            'success', ('branch', branch_network_name))
 
1225
        client.add_expected_call(
 
1226
            'BzrDir.find_repositoryV3', ('stacked/',),
 
1227
            'success', ('ok', '', 'no', 'no', 'yes',
 
1228
                stacked_branch.repository._format.network_name()))
 
1229
        # called twice, once from constructor and then again by us
 
1230
        client.add_expected_call(
 
1231
            'Branch.get_stacked_on_url', ('stacked/',),
 
1232
            'unknown', ('Branch.get_stacked_on_url',))
 
1233
        client.add_expected_call(
 
1234
            'Branch.get_stacked_on_url', ('stacked/',),
 
1235
            'unknown', ('Branch.get_stacked_on_url',))
 
1236
        # this will also do vfs access, but that goes direct to the transport
 
1237
        # and isn't seen by the FakeClient.
 
1238
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
 
1239
            remote.RemoteBzrDirFormat(), _client=client)
 
1240
        branch = bzrdir.open_branch()
 
1241
        result = branch.get_stacked_on_url()
 
1242
        self.assertEqual('../base', result)
 
1243
        self.assertFinished(client)
 
1244
        # it's in the fallback list both for the RemoteRepository and its vfs
 
1245
        # repository
 
1246
        self.assertEqual(1, len(branch.repository._fallback_repositories))
 
1247
        self.assertEqual(1,
 
1248
            len(branch.repository._real_repository._fallback_repositories))
 
1249
 
 
1250
    def test_get_stacked_on_real_branch(self):
 
1251
        base_branch = self.make_branch('base')
 
1252
        stacked_branch = self.make_branch('stacked')
 
1253
        stacked_branch.set_stacked_on_url('../base')
 
1254
        reference_format = self.get_repo_format()
 
1255
        network_name = reference_format.network_name()
 
1256
        client = FakeClient(self.get_url())
 
1257
        branch_network_name = self.get_branch_format().network_name()
 
1258
        client.add_expected_call(
 
1259
            'BzrDir.open_branchV3', ('stacked/',),
 
1260
            'success', ('branch', branch_network_name))
 
1261
        client.add_expected_call(
 
1262
            'BzrDir.find_repositoryV3', ('stacked/',),
 
1263
            'success', ('ok', '', 'yes', 'no', 'yes', network_name))
 
1264
        # called twice, once from constructor and then again by us
 
1265
        client.add_expected_call(
 
1266
            'Branch.get_stacked_on_url', ('stacked/',),
 
1267
            'success', ('ok', '../base'))
 
1268
        client.add_expected_call(
 
1269
            'Branch.get_stacked_on_url', ('stacked/',),
 
1270
            'success', ('ok', '../base'))
 
1271
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
 
1272
            remote.RemoteBzrDirFormat(), _client=client)
 
1273
        branch = bzrdir.open_branch()
 
1274
        result = branch.get_stacked_on_url()
 
1275
        self.assertEqual('../base', result)
 
1276
        self.assertFinished(client)
 
1277
        # it's in the fallback list both for the RemoteRepository.
 
1278
        self.assertEqual(1, len(branch.repository._fallback_repositories))
 
1279
        # And we haven't had to construct a real repository.
 
1280
        self.assertEqual(None, branch.repository._real_repository)
 
1281
 
 
1282
 
 
1283
class TestBranchSetLastRevision(RemoteBranchTestCase):
265
1284
 
266
1285
    def test_set_empty(self):
267
1286
        # set_revision_history([]) is translated to calling
268
1287
        # Branch.set_last_revision(path, '') on the wire.
269
 
        client = FakeClient([
270
 
            # lock_write
271
 
            (('ok', 'branch token', 'repo token'), ),
272
 
            # set_last_revision
273
 
            (('ok',), ),
274
 
            # unlock
275
 
            (('ok',), )])
276
1288
        transport = MemoryTransport()
277
1289
        transport.mkdir('branch')
278
1290
        transport = transport.clone('branch')
279
1291
 
280
 
        bzrdir = RemoteBzrDir(transport, _client=False)
281
 
        branch = RemoteBranch(bzrdir, None, _client=client)
 
1292
        client = FakeClient(transport.base)
 
1293
        client.add_expected_call(
 
1294
            'Branch.get_stacked_on_url', ('branch/',),
 
1295
            'error', ('NotStacked',))
 
1296
        client.add_expected_call(
 
1297
            'Branch.lock_write', ('branch/', '', ''),
 
1298
            'success', ('ok', 'branch token', 'repo token'))
 
1299
        client.add_expected_call(
 
1300
            'Branch.last_revision_info',
 
1301
            ('branch/',),
 
1302
            'success', ('ok', '0', 'null:'))
 
1303
        client.add_expected_call(
 
1304
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'null:',),
 
1305
            'success', ('ok',))
 
1306
        client.add_expected_call(
 
1307
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
 
1308
            'success', ('ok',))
 
1309
        branch = self.make_remote_branch(transport, client)
282
1310
        # This is a hack to work around the problem that RemoteBranch currently
283
1311
        # unnecessarily invokes _ensure_real upon a call to lock_write.
284
1312
        branch._ensure_real = lambda: None
285
1313
        branch.lock_write()
286
 
        client._calls = []
287
1314
        result = branch.set_revision_history([])
288
 
        self.assertEqual(
289
 
            [('call', 'Branch.set_last_revision',
290
 
                ('///branch/', 'branch token', 'repo token', 'null:'))],
291
 
            client._calls)
292
1315
        branch.unlock()
293
1316
        self.assertEqual(None, result)
 
1317
        self.assertFinished(client)
294
1318
 
295
1319
    def test_set_nonempty(self):
296
1320
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
297
1321
        # Branch.set_last_revision(path, rev-idN) on the wire.
298
 
        client = FakeClient([
299
 
            # lock_write
300
 
            (('ok', 'branch token', 'repo token'), ),
301
 
            # set_last_revision
302
 
            (('ok',), ),
303
 
            # unlock
304
 
            (('ok',), )])
305
1322
        transport = MemoryTransport()
306
1323
        transport.mkdir('branch')
307
1324
        transport = transport.clone('branch')
308
1325
 
309
 
        bzrdir = RemoteBzrDir(transport, _client=False)
310
 
        branch = RemoteBranch(bzrdir, None, _client=client)
 
1326
        client = FakeClient(transport.base)
 
1327
        client.add_expected_call(
 
1328
            'Branch.get_stacked_on_url', ('branch/',),
 
1329
            'error', ('NotStacked',))
 
1330
        client.add_expected_call(
 
1331
            'Branch.lock_write', ('branch/', '', ''),
 
1332
            'success', ('ok', 'branch token', 'repo token'))
 
1333
        client.add_expected_call(
 
1334
            'Branch.last_revision_info',
 
1335
            ('branch/',),
 
1336
            'success', ('ok', '0', 'null:'))
 
1337
        lines = ['rev-id2']
 
1338
        encoded_body = bz2.compress('\n'.join(lines))
 
1339
        client.add_success_response_with_body(encoded_body, 'ok')
 
1340
        client.add_expected_call(
 
1341
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id2',),
 
1342
            'success', ('ok',))
 
1343
        client.add_expected_call(
 
1344
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
 
1345
            'success', ('ok',))
 
1346
        branch = self.make_remote_branch(transport, client)
311
1347
        # This is a hack to work around the problem that RemoteBranch currently
312
1348
        # unnecessarily invokes _ensure_real upon a call to lock_write.
313
1349
        branch._ensure_real = lambda: None
314
1350
        # Lock the branch, reset the record of remote calls.
315
1351
        branch.lock_write()
316
 
        client._calls = []
317
 
 
318
1352
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
319
 
        self.assertEqual(
320
 
            [('call', 'Branch.set_last_revision',
321
 
                ('///branch/', 'branch token', 'repo token', 'rev-id2'))],
322
 
            client._calls)
323
1353
        branch.unlock()
324
1354
        self.assertEqual(None, result)
 
1355
        self.assertFinished(client)
325
1356
 
326
1357
    def test_no_such_revision(self):
327
 
        # A response of 'NoSuchRevision' is translated into an exception.
328
 
        client = FakeClient([
329
 
            # lock_write
330
 
            (('ok', 'branch token', 'repo token'), ),
331
 
            # set_last_revision
332
 
            (('NoSuchRevision', 'rev-id'), ),
333
 
            # unlock
334
 
            (('ok',), )])
335
1358
        transport = MemoryTransport()
336
1359
        transport.mkdir('branch')
337
1360
        transport = transport.clone('branch')
 
1361
        # A response of 'NoSuchRevision' is translated into an exception.
 
1362
        client = FakeClient(transport.base)
 
1363
        client.add_expected_call(
 
1364
            'Branch.get_stacked_on_url', ('branch/',),
 
1365
            'error', ('NotStacked',))
 
1366
        client.add_expected_call(
 
1367
            'Branch.lock_write', ('branch/', '', ''),
 
1368
            'success', ('ok', 'branch token', 'repo token'))
 
1369
        client.add_expected_call(
 
1370
            'Branch.last_revision_info',
 
1371
            ('branch/',),
 
1372
            'success', ('ok', '0', 'null:'))
 
1373
        # get_graph calls to construct the revision history, for the set_rh
 
1374
        # hook
 
1375
        lines = ['rev-id']
 
1376
        encoded_body = bz2.compress('\n'.join(lines))
 
1377
        client.add_success_response_with_body(encoded_body, 'ok')
 
1378
        client.add_expected_call(
 
1379
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id',),
 
1380
            'error', ('NoSuchRevision', 'rev-id'))
 
1381
        client.add_expected_call(
 
1382
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
 
1383
            'success', ('ok',))
338
1384
 
339
 
        bzrdir = RemoteBzrDir(transport, _client=False)
340
 
        branch = RemoteBranch(bzrdir, None, _client=client)
341
 
        branch._ensure_real = lambda: None
 
1385
        branch = self.make_remote_branch(transport, client)
342
1386
        branch.lock_write()
343
 
        client._calls = []
344
 
 
345
1387
        self.assertRaises(
346
1388
            errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
347
1389
        branch.unlock()
348
 
 
349
 
 
350
 
class TestBranchControlGetBranchConf(tests.TestCaseWithMemoryTransport):
351
 
    """Test branch.control_files api munging...
352
 
 
353
 
    We special case RemoteBranch.control_files.get('branch.conf') to
354
 
    call a specific API so that RemoteBranch's can intercept configuration
355
 
    file reading, allowing them to signal to the client about things like
356
 
    'email is configured for commits'.
357
 
    """
 
1390
        self.assertFinished(client)
 
1391
 
 
1392
    def test_tip_change_rejected(self):
 
1393
        """TipChangeRejected responses cause a TipChangeRejected exception to
 
1394
        be raised.
 
1395
        """
 
1396
        transport = MemoryTransport()
 
1397
        transport.mkdir('branch')
 
1398
        transport = transport.clone('branch')
 
1399
        client = FakeClient(transport.base)
 
1400
        rejection_msg_unicode = u'rejection message\N{INTERROBANG}'
 
1401
        rejection_msg_utf8 = rejection_msg_unicode.encode('utf8')
 
1402
        client.add_expected_call(
 
1403
            'Branch.get_stacked_on_url', ('branch/',),
 
1404
            'error', ('NotStacked',))
 
1405
        client.add_expected_call(
 
1406
            'Branch.lock_write', ('branch/', '', ''),
 
1407
            'success', ('ok', 'branch token', 'repo token'))
 
1408
        client.add_expected_call(
 
1409
            'Branch.last_revision_info',
 
1410
            ('branch/',),
 
1411
            'success', ('ok', '0', 'null:'))
 
1412
        lines = ['rev-id']
 
1413
        encoded_body = bz2.compress('\n'.join(lines))
 
1414
        client.add_success_response_with_body(encoded_body, 'ok')
 
1415
        client.add_expected_call(
 
1416
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id',),
 
1417
            'error', ('TipChangeRejected', rejection_msg_utf8))
 
1418
        client.add_expected_call(
 
1419
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
 
1420
            'success', ('ok',))
 
1421
        branch = self.make_remote_branch(transport, client)
 
1422
        branch._ensure_real = lambda: None
 
1423
        branch.lock_write()
 
1424
        # The 'TipChangeRejected' error response triggered by calling
 
1425
        # set_revision_history causes a TipChangeRejected exception.
 
1426
        err = self.assertRaises(
 
1427
            errors.TipChangeRejected, branch.set_revision_history, ['rev-id'])
 
1428
        # The UTF-8 message from the response has been decoded into a unicode
 
1429
        # object.
 
1430
        self.assertIsInstance(err.msg, unicode)
 
1431
        self.assertEqual(rejection_msg_unicode, err.msg)
 
1432
        branch.unlock()
 
1433
        self.assertFinished(client)
 
1434
 
 
1435
 
 
1436
class TestBranchSetLastRevisionInfo(RemoteBranchTestCase):
 
1437
 
 
1438
    def test_set_last_revision_info(self):
 
1439
        # set_last_revision_info(num, 'rev-id') is translated to calling
 
1440
        # Branch.set_last_revision_info(num, 'rev-id') on the wire.
 
1441
        transport = MemoryTransport()
 
1442
        transport.mkdir('branch')
 
1443
        transport = transport.clone('branch')
 
1444
        client = FakeClient(transport.base)
 
1445
        # get_stacked_on_url
 
1446
        client.add_error_response('NotStacked')
 
1447
        # lock_write
 
1448
        client.add_success_response('ok', 'branch token', 'repo token')
 
1449
        # query the current revision
 
1450
        client.add_success_response('ok', '0', 'null:')
 
1451
        # set_last_revision
 
1452
        client.add_success_response('ok')
 
1453
        # unlock
 
1454
        client.add_success_response('ok')
 
1455
 
 
1456
        branch = self.make_remote_branch(transport, client)
 
1457
        # Lock the branch, reset the record of remote calls.
 
1458
        branch.lock_write()
 
1459
        client._calls = []
 
1460
        result = branch.set_last_revision_info(1234, 'a-revision-id')
 
1461
        self.assertEqual(
 
1462
            [('call', 'Branch.last_revision_info', ('branch/',)),
 
1463
             ('call', 'Branch.set_last_revision_info',
 
1464
                ('branch/', 'branch token', 'repo token',
 
1465
                 '1234', 'a-revision-id'))],
 
1466
            client._calls)
 
1467
        self.assertEqual(None, result)
 
1468
 
 
1469
    def test_no_such_revision(self):
 
1470
        # A response of 'NoSuchRevision' is translated into an exception.
 
1471
        transport = MemoryTransport()
 
1472
        transport.mkdir('branch')
 
1473
        transport = transport.clone('branch')
 
1474
        client = FakeClient(transport.base)
 
1475
        # get_stacked_on_url
 
1476
        client.add_error_response('NotStacked')
 
1477
        # lock_write
 
1478
        client.add_success_response('ok', 'branch token', 'repo token')
 
1479
        # set_last_revision
 
1480
        client.add_error_response('NoSuchRevision', 'revid')
 
1481
        # unlock
 
1482
        client.add_success_response('ok')
 
1483
 
 
1484
        branch = self.make_remote_branch(transport, client)
 
1485
        # Lock the branch, reset the record of remote calls.
 
1486
        branch.lock_write()
 
1487
        client._calls = []
 
1488
 
 
1489
        self.assertRaises(
 
1490
            errors.NoSuchRevision, branch.set_last_revision_info, 123, 'revid')
 
1491
        branch.unlock()
 
1492
 
 
1493
    def test_backwards_compatibility(self):
 
1494
        """If the server does not support the Branch.set_last_revision_info
 
1495
        verb (which is new in 1.4), then the client falls back to VFS methods.
 
1496
        """
 
1497
        # This test is a little messy.  Unlike most tests in this file, it
 
1498
        # doesn't purely test what a Remote* object sends over the wire, and
 
1499
        # how it reacts to responses from the wire.  It instead relies partly
 
1500
        # on asserting that the RemoteBranch will call
 
1501
        # self._real_branch.set_last_revision_info(...).
 
1502
 
 
1503
        # First, set up our RemoteBranch with a FakeClient that raises
 
1504
        # UnknownSmartMethod, and a StubRealBranch that logs how it is called.
 
1505
        transport = MemoryTransport()
 
1506
        transport.mkdir('branch')
 
1507
        transport = transport.clone('branch')
 
1508
        client = FakeClient(transport.base)
 
1509
        client.add_expected_call(
 
1510
            'Branch.get_stacked_on_url', ('branch/',),
 
1511
            'error', ('NotStacked',))
 
1512
        client.add_expected_call(
 
1513
            'Branch.last_revision_info',
 
1514
            ('branch/',),
 
1515
            'success', ('ok', '0', 'null:'))
 
1516
        client.add_expected_call(
 
1517
            'Branch.set_last_revision_info',
 
1518
            ('branch/', 'branch token', 'repo token', '1234', 'a-revision-id',),
 
1519
            'unknown', 'Branch.set_last_revision_info')
 
1520
 
 
1521
        branch = self.make_remote_branch(transport, client)
 
1522
        class StubRealBranch(object):
 
1523
            def __init__(self):
 
1524
                self.calls = []
 
1525
            def set_last_revision_info(self, revno, revision_id):
 
1526
                self.calls.append(
 
1527
                    ('set_last_revision_info', revno, revision_id))
 
1528
            def _clear_cached_state(self):
 
1529
                pass
 
1530
        real_branch = StubRealBranch()
 
1531
        branch._real_branch = real_branch
 
1532
        self.lock_remote_branch(branch)
 
1533
 
 
1534
        # Call set_last_revision_info, and verify it behaved as expected.
 
1535
        result = branch.set_last_revision_info(1234, 'a-revision-id')
 
1536
        self.assertEqual(
 
1537
            [('set_last_revision_info', 1234, 'a-revision-id')],
 
1538
            real_branch.calls)
 
1539
        self.assertFinished(client)
 
1540
 
 
1541
    def test_unexpected_error(self):
 
1542
        # If the server sends an error the client doesn't understand, it gets
 
1543
        # turned into an UnknownErrorFromSmartServer, which is presented as a
 
1544
        # non-internal error to the user.
 
1545
        transport = MemoryTransport()
 
1546
        transport.mkdir('branch')
 
1547
        transport = transport.clone('branch')
 
1548
        client = FakeClient(transport.base)
 
1549
        # get_stacked_on_url
 
1550
        client.add_error_response('NotStacked')
 
1551
        # lock_write
 
1552
        client.add_success_response('ok', 'branch token', 'repo token')
 
1553
        # set_last_revision
 
1554
        client.add_error_response('UnexpectedError')
 
1555
        # unlock
 
1556
        client.add_success_response('ok')
 
1557
 
 
1558
        branch = self.make_remote_branch(transport, client)
 
1559
        # Lock the branch, reset the record of remote calls.
 
1560
        branch.lock_write()
 
1561
        client._calls = []
 
1562
 
 
1563
        err = self.assertRaises(
 
1564
            errors.UnknownErrorFromSmartServer,
 
1565
            branch.set_last_revision_info, 123, 'revid')
 
1566
        self.assertEqual(('UnexpectedError',), err.error_tuple)
 
1567
        branch.unlock()
 
1568
 
 
1569
    def test_tip_change_rejected(self):
 
1570
        """TipChangeRejected responses cause a TipChangeRejected exception to
 
1571
        be raised.
 
1572
        """
 
1573
        transport = MemoryTransport()
 
1574
        transport.mkdir('branch')
 
1575
        transport = transport.clone('branch')
 
1576
        client = FakeClient(transport.base)
 
1577
        # get_stacked_on_url
 
1578
        client.add_error_response('NotStacked')
 
1579
        # lock_write
 
1580
        client.add_success_response('ok', 'branch token', 'repo token')
 
1581
        # set_last_revision
 
1582
        client.add_error_response('TipChangeRejected', 'rejection message')
 
1583
        # unlock
 
1584
        client.add_success_response('ok')
 
1585
 
 
1586
        branch = self.make_remote_branch(transport, client)
 
1587
        # Lock the branch, reset the record of remote calls.
 
1588
        branch.lock_write()
 
1589
        self.addCleanup(branch.unlock)
 
1590
        client._calls = []
 
1591
 
 
1592
        # The 'TipChangeRejected' error response triggered by calling
 
1593
        # set_last_revision_info causes a TipChangeRejected exception.
 
1594
        err = self.assertRaises(
 
1595
            errors.TipChangeRejected,
 
1596
            branch.set_last_revision_info, 123, 'revid')
 
1597
        self.assertEqual('rejection message', err.msg)
 
1598
 
 
1599
 
 
1600
class TestBranchGetSetConfig(RemoteBranchTestCase):
358
1601
 
359
1602
    def test_get_branch_conf(self):
360
1603
        # in an empty branch we decode the response properly
361
 
        client = FakeClient([(('ok', ), 'config file body')])
362
 
        # we need to make a real branch because the remote_branch.control_files
363
 
        # will trigger _ensure_real.
364
 
        branch = self.make_branch('quack')
365
 
        transport = branch.bzrdir.root_transport
366
 
        # we do not want bzrdir to make any remote calls
367
 
        bzrdir = RemoteBzrDir(transport, _client=False)
368
 
        branch = RemoteBranch(bzrdir, None, _client=client)
369
 
        result = branch.control_files.get('branch.conf')
 
1604
        client = FakeClient()
 
1605
        client.add_expected_call(
 
1606
            'Branch.get_stacked_on_url', ('memory:///',),
 
1607
            'error', ('NotStacked',),)
 
1608
        client.add_success_response_with_body('# config file body', 'ok')
 
1609
        transport = MemoryTransport()
 
1610
        branch = self.make_remote_branch(transport, client)
 
1611
        config = branch.get_config()
 
1612
        config.has_explicit_nickname()
370
1613
        self.assertEqual(
371
 
            [('call_expecting_body', 'Branch.get_config_file', ('///quack/',))],
 
1614
            [('call', 'Branch.get_stacked_on_url', ('memory:///',)),
 
1615
             ('call_expecting_body', 'Branch.get_config_file', ('memory:///',))],
372
1616
            client._calls)
373
 
        self.assertEqual('config file body', result.read())
374
 
 
375
 
 
376
 
class TestBranchLockWrite(tests.TestCase):
 
1617
 
 
1618
    def test_get_multi_line_branch_conf(self):
 
1619
        # Make sure that multiple-line branch.conf files are supported
 
1620
        #
 
1621
        # https://bugs.launchpad.net/bzr/+bug/354075
 
1622
        client = FakeClient()
 
1623
        client.add_expected_call(
 
1624
            'Branch.get_stacked_on_url', ('memory:///',),
 
1625
            'error', ('NotStacked',),)
 
1626
        client.add_success_response_with_body('a = 1\nb = 2\nc = 3\n', 'ok')
 
1627
        transport = MemoryTransport()
 
1628
        branch = self.make_remote_branch(transport, client)
 
1629
        config = branch.get_config()
 
1630
        self.assertEqual(u'2', config.get_user_option('b'))
 
1631
 
 
1632
    def test_set_option(self):
 
1633
        client = FakeClient()
 
1634
        client.add_expected_call(
 
1635
            'Branch.get_stacked_on_url', ('memory:///',),
 
1636
            'error', ('NotStacked',),)
 
1637
        client.add_expected_call(
 
1638
            'Branch.lock_write', ('memory:///', '', ''),
 
1639
            'success', ('ok', 'branch token', 'repo token'))
 
1640
        client.add_expected_call(
 
1641
            'Branch.set_config_option', ('memory:///', 'branch token',
 
1642
            'repo token', 'foo', 'bar', ''),
 
1643
            'success', ())
 
1644
        client.add_expected_call(
 
1645
            'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
 
1646
            'success', ('ok',))
 
1647
        transport = MemoryTransport()
 
1648
        branch = self.make_remote_branch(transport, client)
 
1649
        branch.lock_write()
 
1650
        config = branch._get_config()
 
1651
        config.set_option('foo', 'bar')
 
1652
        branch.unlock()
 
1653
        self.assertFinished(client)
 
1654
 
 
1655
    def test_set_option_with_dict(self):
 
1656
        client = FakeClient()
 
1657
        client.add_expected_call(
 
1658
            'Branch.get_stacked_on_url', ('memory:///',),
 
1659
            'error', ('NotStacked',),)
 
1660
        client.add_expected_call(
 
1661
            'Branch.lock_write', ('memory:///', '', ''),
 
1662
            'success', ('ok', 'branch token', 'repo token'))
 
1663
        encoded_dict_value = 'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde'
 
1664
        client.add_expected_call(
 
1665
            'Branch.set_config_option_dict', ('memory:///', 'branch token',
 
1666
            'repo token', encoded_dict_value, 'foo', ''),
 
1667
            'success', ())
 
1668
        client.add_expected_call(
 
1669
            'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
 
1670
            'success', ('ok',))
 
1671
        transport = MemoryTransport()
 
1672
        branch = self.make_remote_branch(transport, client)
 
1673
        branch.lock_write()
 
1674
        config = branch._get_config()
 
1675
        config.set_option(
 
1676
            {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'},
 
1677
            'foo')
 
1678
        branch.unlock()
 
1679
        self.assertFinished(client)
 
1680
 
 
1681
    def test_backwards_compat_set_option(self):
 
1682
        self.setup_smart_server_with_call_log()
 
1683
        branch = self.make_branch('.')
 
1684
        verb = 'Branch.set_config_option'
 
1685
        self.disable_verb(verb)
 
1686
        branch.lock_write()
 
1687
        self.addCleanup(branch.unlock)
 
1688
        self.reset_smart_call_log()
 
1689
        branch._get_config().set_option('value', 'name')
 
1690
        self.assertLength(10, self.hpss_calls)
 
1691
        self.assertEqual('value', branch._get_config().get_option('name'))
 
1692
 
 
1693
    def test_backwards_compat_set_option_with_dict(self):
 
1694
        self.setup_smart_server_with_call_log()
 
1695
        branch = self.make_branch('.')
 
1696
        verb = 'Branch.set_config_option_dict'
 
1697
        self.disable_verb(verb)
 
1698
        branch.lock_write()
 
1699
        self.addCleanup(branch.unlock)
 
1700
        self.reset_smart_call_log()
 
1701
        config = branch._get_config()
 
1702
        value_dict = {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
 
1703
        config.set_option(value_dict, 'name')
 
1704
        self.assertLength(10, self.hpss_calls)
 
1705
        self.assertEqual(value_dict, branch._get_config().get_option('name'))
 
1706
 
 
1707
 
 
1708
class TestBranchLockWrite(RemoteBranchTestCase):
377
1709
 
378
1710
    def test_lock_write_unlockable(self):
379
 
        client = FakeClient([(('UnlockableTransport', ), '')])
380
1711
        transport = MemoryTransport()
 
1712
        client = FakeClient(transport.base)
 
1713
        client.add_expected_call(
 
1714
            'Branch.get_stacked_on_url', ('quack/',),
 
1715
            'error', ('NotStacked',),)
 
1716
        client.add_expected_call(
 
1717
            'Branch.lock_write', ('quack/', '', ''),
 
1718
            'error', ('UnlockableTransport',))
381
1719
        transport.mkdir('quack')
382
1720
        transport = transport.clone('quack')
383
 
        # we do not want bzrdir to make any remote calls
384
 
        bzrdir = RemoteBzrDir(transport, _client=False)
385
 
        branch = RemoteBranch(bzrdir, None, _client=client)
 
1721
        branch = self.make_remote_branch(transport, client)
386
1722
        self.assertRaises(errors.UnlockableTransport, branch.lock_write)
 
1723
        self.assertFinished(client)
 
1724
 
 
1725
 
 
1726
class TestBzrDirGetSetConfig(RemoteBzrDirTestCase):
 
1727
 
 
1728
    def test__get_config(self):
 
1729
        client = FakeClient()
 
1730
        client.add_success_response_with_body('default_stack_on = /\n', 'ok')
 
1731
        transport = MemoryTransport()
 
1732
        bzrdir = self.make_remote_bzrdir(transport, client)
 
1733
        config = bzrdir.get_config()
 
1734
        self.assertEqual('/', config.get_default_stack_on())
387
1735
        self.assertEqual(
388
 
            [('call', 'Branch.lock_write', ('///quack/', '', ''))],
 
1736
            [('call_expecting_body', 'BzrDir.get_config_file', ('memory:///',))],
389
1737
            client._calls)
390
1738
 
 
1739
    def test_set_option_uses_vfs(self):
 
1740
        self.setup_smart_server_with_call_log()
 
1741
        bzrdir = self.make_bzrdir('.')
 
1742
        self.reset_smart_call_log()
 
1743
        config = bzrdir.get_config()
 
1744
        config.set_default_stack_on('/')
 
1745
        self.assertLength(3, self.hpss_calls)
 
1746
 
 
1747
    def test_backwards_compat_get_option(self):
 
1748
        self.setup_smart_server_with_call_log()
 
1749
        bzrdir = self.make_bzrdir('.')
 
1750
        verb = 'BzrDir.get_config_file'
 
1751
        self.disable_verb(verb)
 
1752
        self.reset_smart_call_log()
 
1753
        self.assertEqual(None,
 
1754
            bzrdir._get_config().get_option('default_stack_on'))
 
1755
        self.assertLength(3, self.hpss_calls)
 
1756
 
391
1757
 
392
1758
class TestTransportIsReadonly(tests.TestCase):
393
1759
 
394
1760
    def test_true(self):
395
 
        client = FakeClient([(('yes',), '')])
 
1761
        client = FakeClient()
 
1762
        client.add_success_response('yes')
396
1763
        transport = RemoteTransport('bzr://example.com/', medium=False,
397
1764
                                    _client=client)
398
1765
        self.assertEqual(True, transport.is_readonly())
401
1768
            client._calls)
402
1769
 
403
1770
    def test_false(self):
404
 
        client = FakeClient([(('no',), '')])
 
1771
        client = FakeClient()
 
1772
        client.add_success_response('no')
405
1773
        transport = RemoteTransport('bzr://example.com/', medium=False,
406
1774
                                    _client=client)
407
1775
        self.assertEqual(False, transport.is_readonly())
411
1779
 
412
1780
    def test_error_from_old_server(self):
413
1781
        """bzr 0.15 and earlier servers don't recognise the is_readonly verb.
414
 
        
 
1782
 
415
1783
        Clients should treat it as a "no" response, because is_readonly is only
416
1784
        advisory anyway (a transport could be read-write, but then the
417
1785
        underlying filesystem could be readonly anyway).
418
1786
        """
419
 
        client = FakeClient([(
420
 
            ('error', "Generic bzr smart protocol error: "
421
 
                      "bad request 'Transport.is_readonly'"), '')])
422
 
        transport = RemoteTransport('bzr://example.com/', medium=False,
423
 
                                    _client=client)
424
 
        self.assertEqual(False, transport.is_readonly())
425
 
        self.assertEqual(
426
 
            [('call', 'Transport.is_readonly', ())],
427
 
            client._calls)
428
 
 
429
 
    def test_error_from_old_0_11_server(self):
430
 
        """Same as test_error_from_old_server, but with the slightly different
431
 
        error message from bzr 0.11 servers.
432
 
        """
433
 
        client = FakeClient([(
434
 
            ('error', "Generic bzr smart protocol error: "
435
 
                      "bad request u'Transport.is_readonly'"), '')])
436
 
        transport = RemoteTransport('bzr://example.com/', medium=False,
437
 
                                    _client=client)
438
 
        self.assertEqual(False, transport.is_readonly())
439
 
        self.assertEqual(
440
 
            [('call', 'Transport.is_readonly', ())],
441
 
            client._calls)
442
 
 
443
 
 
444
 
class TestRemoteRepository(tests.TestCase):
 
1787
        client = FakeClient()
 
1788
        client.add_unknown_method_response('Transport.is_readonly')
 
1789
        transport = RemoteTransport('bzr://example.com/', medium=False,
 
1790
                                    _client=client)
 
1791
        self.assertEqual(False, transport.is_readonly())
 
1792
        self.assertEqual(
 
1793
            [('call', 'Transport.is_readonly', ())],
 
1794
            client._calls)
 
1795
 
 
1796
 
 
1797
class TestTransportMkdir(tests.TestCase):
 
1798
 
 
1799
    def test_permissiondenied(self):
 
1800
        client = FakeClient()
 
1801
        client.add_error_response('PermissionDenied', 'remote path', 'extra')
 
1802
        transport = RemoteTransport('bzr://example.com/', medium=False,
 
1803
                                    _client=client)
 
1804
        exc = self.assertRaises(
 
1805
            errors.PermissionDenied, transport.mkdir, 'client path')
 
1806
        expected_error = errors.PermissionDenied('/client path', 'extra')
 
1807
        self.assertEqual(expected_error, exc)
 
1808
 
 
1809
 
 
1810
class TestRemoteSSHTransportAuthentication(tests.TestCaseInTempDir):
 
1811
 
 
1812
    def test_defaults_to_none(self):
 
1813
        t = RemoteSSHTransport('bzr+ssh://example.com')
 
1814
        self.assertIs(None, t._get_credentials()[0])
 
1815
 
 
1816
    def test_uses_authentication_config(self):
 
1817
        conf = config.AuthenticationConfig()
 
1818
        conf._get_config().update(
 
1819
            {'bzr+sshtest': {'scheme': 'ssh', 'user': 'bar', 'host':
 
1820
            'example.com'}})
 
1821
        conf._save()
 
1822
        t = RemoteSSHTransport('bzr+ssh://example.com')
 
1823
        self.assertEqual('bar', t._get_credentials()[0])
 
1824
 
 
1825
 
 
1826
class TestRemoteRepository(TestRemote):
445
1827
    """Base for testing RemoteRepository protocol usage.
446
 
    
447
 
    These tests contain frozen requests and responses.  We want any changes to 
 
1828
 
 
1829
    These tests contain frozen requests and responses.  We want any changes to
448
1830
    what is sent or expected to be require a thoughtful update to these tests
449
1831
    because they might break compatibility with different-versioned servers.
450
1832
    """
451
1833
 
452
 
    def setup_fake_client_and_repository(self, responses, transport_path):
 
1834
    def setup_fake_client_and_repository(self, transport_path):
453
1835
        """Create the fake client and repository for testing with.
454
 
        
 
1836
 
455
1837
        There's no real server here; we just have canned responses sent
456
1838
        back one by one.
457
 
        
 
1839
 
458
1840
        :param transport_path: Path below the root of the MemoryTransport
459
1841
            where the repository will be created.
460
1842
        """
461
 
        client = FakeClient(responses)
462
1843
        transport = MemoryTransport()
463
1844
        transport.mkdir(transport_path)
 
1845
        client = FakeClient(transport.base)
464
1846
        transport = transport.clone(transport_path)
465
1847
        # we do not want bzrdir to make any remote calls
466
 
        bzrdir = RemoteBzrDir(transport, _client=False)
 
1848
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
 
1849
            _client=False)
467
1850
        repo = RemoteRepository(bzrdir, None, _client=client)
468
1851
        return repo, client
469
1852
 
470
1853
 
 
1854
def remoted_description(format):
 
1855
    return 'Remote: ' + format.get_format_description()
 
1856
 
 
1857
 
 
1858
class TestBranchFormat(tests.TestCase):
 
1859
 
 
1860
    def test_get_format_description(self):
 
1861
        remote_format = RemoteBranchFormat()
 
1862
        real_format = branch.BranchFormat.get_default_format()
 
1863
        remote_format._network_name = real_format.network_name()
 
1864
        self.assertEqual(remoted_description(real_format),
 
1865
            remote_format.get_format_description())
 
1866
 
 
1867
 
 
1868
class TestRepositoryFormat(TestRemoteRepository):
 
1869
 
 
1870
    def test_fast_delta(self):
 
1871
        true_name = groupcompress_repo.RepositoryFormatCHK1().network_name()
 
1872
        true_format = RemoteRepositoryFormat()
 
1873
        true_format._network_name = true_name
 
1874
        self.assertEqual(True, true_format.fast_deltas)
 
1875
        false_name = pack_repo.RepositoryFormatKnitPack1().network_name()
 
1876
        false_format = RemoteRepositoryFormat()
 
1877
        false_format._network_name = false_name
 
1878
        self.assertEqual(False, false_format.fast_deltas)
 
1879
 
 
1880
    def test_get_format_description(self):
 
1881
        remote_repo_format = RemoteRepositoryFormat()
 
1882
        real_format = repository.RepositoryFormat.get_default_format()
 
1883
        remote_repo_format._network_name = real_format.network_name()
 
1884
        self.assertEqual(remoted_description(real_format),
 
1885
            remote_repo_format.get_format_description())
 
1886
 
 
1887
 
471
1888
class TestRepositoryGatherStats(TestRemoteRepository):
472
1889
 
473
1890
    def test_revid_none(self):
474
1891
        # ('ok',), body with revisions and size
475
 
        responses = [(('ok', ), 'revisions: 2\nsize: 18\n')]
476
1892
        transport_path = 'quack'
477
 
        repo, client = self.setup_fake_client_and_repository(
478
 
            responses, transport_path)
 
1893
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
1894
        client.add_success_response_with_body(
 
1895
            'revisions: 2\nsize: 18\n', 'ok')
479
1896
        result = repo.gather_stats(None)
480
1897
        self.assertEqual(
481
1898
            [('call_expecting_body', 'Repository.gather_stats',
482
 
             ('///quack/','','no'))],
 
1899
             ('quack/','','no'))],
483
1900
            client._calls)
484
1901
        self.assertEqual({'revisions': 2, 'size': 18}, result)
485
1902
 
486
1903
    def test_revid_no_committers(self):
487
1904
        # ('ok',), body without committers
488
 
        responses = [(('ok', ),
489
 
                      'firstrev: 123456.300 3600\n'
490
 
                      'latestrev: 654231.400 0\n'
491
 
                      'revisions: 2\n'
492
 
                      'size: 18\n')]
 
1905
        body = ('firstrev: 123456.300 3600\n'
 
1906
                'latestrev: 654231.400 0\n'
 
1907
                'revisions: 2\n'
 
1908
                'size: 18\n')
493
1909
        transport_path = 'quick'
494
1910
        revid = u'\xc8'.encode('utf8')
495
 
        repo, client = self.setup_fake_client_and_repository(
496
 
            responses, transport_path)
 
1911
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
1912
        client.add_success_response_with_body(body, 'ok')
497
1913
        result = repo.gather_stats(revid)
498
1914
        self.assertEqual(
499
1915
            [('call_expecting_body', 'Repository.gather_stats',
500
 
              ('///quick/', revid, 'no'))],
 
1916
              ('quick/', revid, 'no'))],
501
1917
            client._calls)
502
1918
        self.assertEqual({'revisions': 2, 'size': 18,
503
1919
                          'firstrev': (123456.300, 3600),
506
1922
 
507
1923
    def test_revid_with_committers(self):
508
1924
        # ('ok',), body with committers
509
 
        responses = [(('ok', ),
510
 
                      'committers: 128\n'
511
 
                      'firstrev: 123456.300 3600\n'
512
 
                      'latestrev: 654231.400 0\n'
513
 
                      'revisions: 2\n'
514
 
                      'size: 18\n')]
 
1925
        body = ('committers: 128\n'
 
1926
                'firstrev: 123456.300 3600\n'
 
1927
                'latestrev: 654231.400 0\n'
 
1928
                'revisions: 2\n'
 
1929
                'size: 18\n')
515
1930
        transport_path = 'buick'
516
1931
        revid = u'\xc8'.encode('utf8')
517
 
        repo, client = self.setup_fake_client_and_repository(
518
 
            responses, transport_path)
 
1932
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
1933
        client.add_success_response_with_body(body, 'ok')
519
1934
        result = repo.gather_stats(revid, True)
520
1935
        self.assertEqual(
521
1936
            [('call_expecting_body', 'Repository.gather_stats',
522
 
              ('///buick/', revid, 'yes'))],
 
1937
              ('buick/', revid, 'yes'))],
523
1938
            client._calls)
524
1939
        self.assertEqual({'revisions': 2, 'size': 18,
525
1940
                          'committers': 128,
528
1943
                         result)
529
1944
 
530
1945
 
 
1946
class TestRepositoryGetGraph(TestRemoteRepository):
 
1947
 
 
1948
    def test_get_graph(self):
 
1949
        # get_graph returns a graph with a custom parents provider.
 
1950
        transport_path = 'quack'
 
1951
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
1952
        graph = repo.get_graph()
 
1953
        self.assertNotEqual(graph._parents_provider, repo)
 
1954
 
 
1955
 
 
1956
class TestRepositoryGetParentMap(TestRemoteRepository):
 
1957
 
 
1958
    def test_get_parent_map_caching(self):
 
1959
        # get_parent_map returns from cache until unlock()
 
1960
        # setup a reponse with two revisions
 
1961
        r1 = u'\u0e33'.encode('utf8')
 
1962
        r2 = u'\u0dab'.encode('utf8')
 
1963
        lines = [' '.join([r2, r1]), r1]
 
1964
        encoded_body = bz2.compress('\n'.join(lines))
 
1965
 
 
1966
        transport_path = 'quack'
 
1967
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
1968
        client.add_success_response_with_body(encoded_body, 'ok')
 
1969
        client.add_success_response_with_body(encoded_body, 'ok')
 
1970
        repo.lock_read()
 
1971
        graph = repo.get_graph()
 
1972
        parents = graph.get_parent_map([r2])
 
1973
        self.assertEqual({r2: (r1,)}, parents)
 
1974
        # locking and unlocking deeper should not reset
 
1975
        repo.lock_read()
 
1976
        repo.unlock()
 
1977
        parents = graph.get_parent_map([r1])
 
1978
        self.assertEqual({r1: (NULL_REVISION,)}, parents)
 
1979
        self.assertEqual(
 
1980
            [('call_with_body_bytes_expecting_body',
 
1981
              'Repository.get_parent_map', ('quack/', 'include-missing:', r2),
 
1982
              '\n\n0')],
 
1983
            client._calls)
 
1984
        repo.unlock()
 
1985
        # now we call again, and it should use the second response.
 
1986
        repo.lock_read()
 
1987
        graph = repo.get_graph()
 
1988
        parents = graph.get_parent_map([r1])
 
1989
        self.assertEqual({r1: (NULL_REVISION,)}, parents)
 
1990
        self.assertEqual(
 
1991
            [('call_with_body_bytes_expecting_body',
 
1992
              'Repository.get_parent_map', ('quack/', 'include-missing:', r2),
 
1993
              '\n\n0'),
 
1994
             ('call_with_body_bytes_expecting_body',
 
1995
              'Repository.get_parent_map', ('quack/', 'include-missing:', r1),
 
1996
              '\n\n0'),
 
1997
            ],
 
1998
            client._calls)
 
1999
        repo.unlock()
 
2000
 
 
2001
    def test_get_parent_map_reconnects_if_unknown_method(self):
 
2002
        transport_path = 'quack'
 
2003
        rev_id = 'revision-id'
 
2004
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2005
        client.add_unknown_method_response('Repository.get_parent_map')
 
2006
        client.add_success_response_with_body(rev_id, 'ok')
 
2007
        self.assertFalse(client._medium._is_remote_before((1, 2)))
 
2008
        parents = repo.get_parent_map([rev_id])
 
2009
        self.assertEqual(
 
2010
            [('call_with_body_bytes_expecting_body',
 
2011
              'Repository.get_parent_map', ('quack/', 'include-missing:',
 
2012
              rev_id), '\n\n0'),
 
2013
             ('disconnect medium',),
 
2014
             ('call_expecting_body', 'Repository.get_revision_graph',
 
2015
              ('quack/', ''))],
 
2016
            client._calls)
 
2017
        # The medium is now marked as being connected to an older server
 
2018
        self.assertTrue(client._medium._is_remote_before((1, 2)))
 
2019
        self.assertEqual({rev_id: ('null:',)}, parents)
 
2020
 
 
2021
    def test_get_parent_map_fallback_parentless_node(self):
 
2022
        """get_parent_map falls back to get_revision_graph on old servers.  The
 
2023
        results from get_revision_graph are tweaked to match the get_parent_map
 
2024
        API.
 
2025
 
 
2026
        Specifically, a {key: ()} result from get_revision_graph means "no
 
2027
        parents" for that key, which in get_parent_map results should be
 
2028
        represented as {key: ('null:',)}.
 
2029
 
 
2030
        This is the test for https://bugs.launchpad.net/bzr/+bug/214894
 
2031
        """
 
2032
        rev_id = 'revision-id'
 
2033
        transport_path = 'quack'
 
2034
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2035
        client.add_success_response_with_body(rev_id, 'ok')
 
2036
        client._medium._remember_remote_is_before((1, 2))
 
2037
        parents = repo.get_parent_map([rev_id])
 
2038
        self.assertEqual(
 
2039
            [('call_expecting_body', 'Repository.get_revision_graph',
 
2040
             ('quack/', ''))],
 
2041
            client._calls)
 
2042
        self.assertEqual({rev_id: ('null:',)}, parents)
 
2043
 
 
2044
    def test_get_parent_map_unexpected_response(self):
 
2045
        repo, client = self.setup_fake_client_and_repository('path')
 
2046
        client.add_success_response('something unexpected!')
 
2047
        self.assertRaises(
 
2048
            errors.UnexpectedSmartServerResponse,
 
2049
            repo.get_parent_map, ['a-revision-id'])
 
2050
 
 
2051
    def test_get_parent_map_negative_caches_missing_keys(self):
 
2052
        self.setup_smart_server_with_call_log()
 
2053
        repo = self.make_repository('foo')
 
2054
        self.assertIsInstance(repo, RemoteRepository)
 
2055
        repo.lock_read()
 
2056
        self.addCleanup(repo.unlock)
 
2057
        self.reset_smart_call_log()
 
2058
        graph = repo.get_graph()
 
2059
        self.assertEqual({},
 
2060
            graph.get_parent_map(['some-missing', 'other-missing']))
 
2061
        self.assertLength(1, self.hpss_calls)
 
2062
        # No call if we repeat this
 
2063
        self.reset_smart_call_log()
 
2064
        graph = repo.get_graph()
 
2065
        self.assertEqual({},
 
2066
            graph.get_parent_map(['some-missing', 'other-missing']))
 
2067
        self.assertLength(0, self.hpss_calls)
 
2068
        # Asking for more unknown keys makes a request.
 
2069
        self.reset_smart_call_log()
 
2070
        graph = repo.get_graph()
 
2071
        self.assertEqual({},
 
2072
            graph.get_parent_map(['some-missing', 'other-missing',
 
2073
                'more-missing']))
 
2074
        self.assertLength(1, self.hpss_calls)
 
2075
 
 
2076
    def disableExtraResults(self):
 
2077
        self.overrideAttr(SmartServerRepositoryGetParentMap,
 
2078
                          'no_extra_results', True)
 
2079
 
 
2080
    def test_null_cached_missing_and_stop_key(self):
 
2081
        self.setup_smart_server_with_call_log()
 
2082
        # Make a branch with a single revision.
 
2083
        builder = self.make_branch_builder('foo')
 
2084
        builder.start_series()
 
2085
        builder.build_snapshot('first', None, [
 
2086
            ('add', ('', 'root-id', 'directory', ''))])
 
2087
        builder.finish_series()
 
2088
        branch = builder.get_branch()
 
2089
        repo = branch.repository
 
2090
        self.assertIsInstance(repo, RemoteRepository)
 
2091
        # Stop the server from sending extra results.
 
2092
        self.disableExtraResults()
 
2093
        repo.lock_read()
 
2094
        self.addCleanup(repo.unlock)
 
2095
        self.reset_smart_call_log()
 
2096
        graph = repo.get_graph()
 
2097
        # Query for 'first' and 'null:'.  Because 'null:' is a parent of
 
2098
        # 'first' it will be a candidate for the stop_keys of subsequent
 
2099
        # requests, and because 'null:' was queried but not returned it will be
 
2100
        # cached as missing.
 
2101
        self.assertEqual({'first': ('null:',)},
 
2102
            graph.get_parent_map(['first', 'null:']))
 
2103
        # Now query for another key.  This request will pass along a recipe of
 
2104
        # start and stop keys describing the already cached results, and this
 
2105
        # recipe's revision count must be correct (or else it will trigger an
 
2106
        # error from the server).
 
2107
        self.assertEqual({}, graph.get_parent_map(['another-key']))
 
2108
        # This assertion guards against disableExtraResults silently failing to
 
2109
        # work, thus invalidating the test.
 
2110
        self.assertLength(2, self.hpss_calls)
 
2111
 
 
2112
    def test_get_parent_map_gets_ghosts_from_result(self):
 
2113
        # asking for a revision should negatively cache close ghosts in its
 
2114
        # ancestry.
 
2115
        self.setup_smart_server_with_call_log()
 
2116
        tree = self.make_branch_and_memory_tree('foo')
 
2117
        tree.lock_write()
 
2118
        try:
 
2119
            builder = treebuilder.TreeBuilder()
 
2120
            builder.start_tree(tree)
 
2121
            builder.build([])
 
2122
            builder.finish_tree()
 
2123
            tree.set_parent_ids(['non-existant'], allow_leftmost_as_ghost=True)
 
2124
            rev_id = tree.commit('')
 
2125
        finally:
 
2126
            tree.unlock()
 
2127
        tree.lock_read()
 
2128
        self.addCleanup(tree.unlock)
 
2129
        repo = tree.branch.repository
 
2130
        self.assertIsInstance(repo, RemoteRepository)
 
2131
        # ask for rev_id
 
2132
        repo.get_parent_map([rev_id])
 
2133
        self.reset_smart_call_log()
 
2134
        # Now asking for rev_id's ghost parent should not make calls
 
2135
        self.assertEqual({}, repo.get_parent_map(['non-existant']))
 
2136
        self.assertLength(0, self.hpss_calls)
 
2137
 
 
2138
 
 
2139
class TestGetParentMapAllowsNew(tests.TestCaseWithTransport):
 
2140
 
 
2141
    def test_allows_new_revisions(self):
 
2142
        """get_parent_map's results can be updated by commit."""
 
2143
        smart_server = test_server.SmartTCPServer_for_testing()
 
2144
        self.start_server(smart_server)
 
2145
        self.make_branch('branch')
 
2146
        branch = Branch.open(smart_server.get_url() + '/branch')
 
2147
        tree = branch.create_checkout('tree', lightweight=True)
 
2148
        tree.lock_write()
 
2149
        self.addCleanup(tree.unlock)
 
2150
        graph = tree.branch.repository.get_graph()
 
2151
        # This provides an opportunity for the missing rev-id to be cached.
 
2152
        self.assertEqual({}, graph.get_parent_map(['rev1']))
 
2153
        tree.commit('message', rev_id='rev1')
 
2154
        graph = tree.branch.repository.get_graph()
 
2155
        self.assertEqual({'rev1': ('null:',)}, graph.get_parent_map(['rev1']))
 
2156
 
 
2157
 
531
2158
class TestRepositoryGetRevisionGraph(TestRemoteRepository):
532
 
    
 
2159
 
533
2160
    def test_null_revision(self):
534
2161
        # a null revision has the predictable result {}, we should have no wire
535
2162
        # traffic when calling it with this argument
536
 
        responses = [(('notused', ), '')]
537
2163
        transport_path = 'empty'
538
 
        repo, client = self.setup_fake_client_and_repository(
539
 
            responses, transport_path)
540
 
        result = repo.get_revision_graph(NULL_REVISION)
 
2164
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2165
        client.add_success_response('notused')
 
2166
        # actual RemoteRepository.get_revision_graph is gone, but there's an
 
2167
        # equivalent private method for testing
 
2168
        result = repo._get_revision_graph(NULL_REVISION)
541
2169
        self.assertEqual([], client._calls)
542
2170
        self.assertEqual({}, result)
543
2171
 
548
2176
        lines = [' '.join([r2, r1]), r1]
549
2177
        encoded_body = '\n'.join(lines)
550
2178
 
551
 
        responses = [(('ok', ), encoded_body)]
552
2179
        transport_path = 'sinhala'
553
 
        repo, client = self.setup_fake_client_and_repository(
554
 
            responses, transport_path)
555
 
        result = repo.get_revision_graph()
 
2180
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2181
        client.add_success_response_with_body(encoded_body, 'ok')
 
2182
        # actual RemoteRepository.get_revision_graph is gone, but there's an
 
2183
        # equivalent private method for testing
 
2184
        result = repo._get_revision_graph(None)
556
2185
        self.assertEqual(
557
2186
            [('call_expecting_body', 'Repository.get_revision_graph',
558
 
             ('///sinhala/', ''))],
 
2187
             ('sinhala/', ''))],
559
2188
            client._calls)
560
2189
        self.assertEqual({r1: (), r2: (r1, )}, result)
561
2190
 
568
2197
        lines = [' '.join([r2, r11, r12]), r11, r12]
569
2198
        encoded_body = '\n'.join(lines)
570
2199
 
571
 
        responses = [(('ok', ), encoded_body)]
572
2200
        transport_path = 'sinhala'
573
 
        repo, client = self.setup_fake_client_and_repository(
574
 
            responses, transport_path)
575
 
        result = repo.get_revision_graph(r2)
 
2201
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2202
        client.add_success_response_with_body(encoded_body, 'ok')
 
2203
        result = repo._get_revision_graph(r2)
576
2204
        self.assertEqual(
577
2205
            [('call_expecting_body', 'Repository.get_revision_graph',
578
 
             ('///sinhala/', r2))],
 
2206
             ('sinhala/', r2))],
579
2207
            client._calls)
580
2208
        self.assertEqual({r11: (), r12: (), r2: (r11, r12), }, result)
581
2209
 
582
2210
    def test_no_such_revision(self):
583
2211
        revid = '123'
584
 
        responses = [(('nosuchrevision', revid), '')]
585
2212
        transport_path = 'sinhala'
586
 
        repo, client = self.setup_fake_client_and_repository(
587
 
            responses, transport_path)
 
2213
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2214
        client.add_error_response('nosuchrevision', revid)
588
2215
        # also check that the right revision is reported in the error
589
2216
        self.assertRaises(errors.NoSuchRevision,
590
 
            repo.get_revision_graph, revid)
 
2217
            repo._get_revision_graph, revid)
591
2218
        self.assertEqual(
592
2219
            [('call_expecting_body', 'Repository.get_revision_graph',
593
 
             ('///sinhala/', revid))],
 
2220
             ('sinhala/', revid))],
594
2221
            client._calls)
595
2222
 
596
 
        
 
2223
    def test_unexpected_error(self):
 
2224
        revid = '123'
 
2225
        transport_path = 'sinhala'
 
2226
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2227
        client.add_error_response('AnUnexpectedError')
 
2228
        e = self.assertRaises(errors.UnknownErrorFromSmartServer,
 
2229
            repo._get_revision_graph, revid)
 
2230
        self.assertEqual(('AnUnexpectedError',), e.error_tuple)
 
2231
 
 
2232
 
 
2233
class TestRepositoryGetRevIdForRevno(TestRemoteRepository):
 
2234
 
 
2235
    def test_ok(self):
 
2236
        repo, client = self.setup_fake_client_and_repository('quack')
 
2237
        client.add_expected_call(
 
2238
            'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
 
2239
            'success', ('ok', 'rev-five'))
 
2240
        result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
 
2241
        self.assertEqual((True, 'rev-five'), result)
 
2242
        self.assertFinished(client)
 
2243
 
 
2244
    def test_history_incomplete(self):
 
2245
        repo, client = self.setup_fake_client_and_repository('quack')
 
2246
        client.add_expected_call(
 
2247
            'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
 
2248
            'success', ('history-incomplete', 10, 'rev-ten'))
 
2249
        result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
 
2250
        self.assertEqual((False, (10, 'rev-ten')), result)
 
2251
        self.assertFinished(client)
 
2252
 
 
2253
    def test_history_incomplete_with_fallback(self):
 
2254
        """A 'history-incomplete' response causes the fallback repository to be
 
2255
        queried too, if one is set.
 
2256
        """
 
2257
        # Make a repo with a fallback repo, both using a FakeClient.
 
2258
        format = remote.response_tuple_to_repo_format(
 
2259
            ('yes', 'no', 'yes', self.get_repo_format().network_name()))
 
2260
        repo, client = self.setup_fake_client_and_repository('quack')
 
2261
        repo._format = format
 
2262
        fallback_repo, ignored = self.setup_fake_client_and_repository(
 
2263
            'fallback')
 
2264
        fallback_repo._client = client
 
2265
        fallback_repo._format = format
 
2266
        repo.add_fallback_repository(fallback_repo)
 
2267
        # First the client should ask the primary repo
 
2268
        client.add_expected_call(
 
2269
            'Repository.get_rev_id_for_revno', ('quack/', 1, (42, 'rev-foo')),
 
2270
            'success', ('history-incomplete', 2, 'rev-two'))
 
2271
        # Then it should ask the fallback, using revno/revid from the
 
2272
        # history-incomplete response as the known revno/revid.
 
2273
        client.add_expected_call(
 
2274
            'Repository.get_rev_id_for_revno',('fallback/', 1, (2, 'rev-two')),
 
2275
            'success', ('ok', 'rev-one'))
 
2276
        result = repo.get_rev_id_for_revno(1, (42, 'rev-foo'))
 
2277
        self.assertEqual((True, 'rev-one'), result)
 
2278
        self.assertFinished(client)
 
2279
 
 
2280
    def test_nosuchrevision(self):
 
2281
        # 'nosuchrevision' is returned when the known-revid is not found in the
 
2282
        # remote repo.  The client translates that response to NoSuchRevision.
 
2283
        repo, client = self.setup_fake_client_and_repository('quack')
 
2284
        client.add_expected_call(
 
2285
            'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
 
2286
            'error', ('nosuchrevision', 'rev-foo'))
 
2287
        self.assertRaises(
 
2288
            errors.NoSuchRevision,
 
2289
            repo.get_rev_id_for_revno, 5, (42, 'rev-foo'))
 
2290
        self.assertFinished(client)
 
2291
 
 
2292
    def test_branch_fallback_locking(self):
 
2293
        """RemoteBranch.get_rev_id takes a read lock, and tries to call the
 
2294
        get_rev_id_for_revno verb.  If the verb is unknown the VFS fallback
 
2295
        will be invoked, which will fail if the repo is unlocked.
 
2296
        """
 
2297
        self.setup_smart_server_with_call_log()
 
2298
        tree = self.make_branch_and_memory_tree('.')
 
2299
        tree.lock_write()
 
2300
        tree.add('')
 
2301
        rev1 = tree.commit('First')
 
2302
        rev2 = tree.commit('Second')
 
2303
        tree.unlock()
 
2304
        branch = tree.branch
 
2305
        self.assertFalse(branch.is_locked())
 
2306
        self.reset_smart_call_log()
 
2307
        verb = 'Repository.get_rev_id_for_revno'
 
2308
        self.disable_verb(verb)
 
2309
        self.assertEqual(rev1, branch.get_rev_id(1))
 
2310
        self.assertLength(1, [call for call in self.hpss_calls if
 
2311
                              call.call.method == verb])
 
2312
 
 
2313
 
597
2314
class TestRepositoryIsShared(TestRemoteRepository):
598
2315
 
599
2316
    def test_is_shared(self):
600
2317
        # ('yes', ) for Repository.is_shared -> 'True'.
601
 
        responses = [(('yes', ), )]
602
2318
        transport_path = 'quack'
603
 
        repo, client = self.setup_fake_client_and_repository(
604
 
            responses, transport_path)
 
2319
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2320
        client.add_success_response('yes')
605
2321
        result = repo.is_shared()
606
2322
        self.assertEqual(
607
 
            [('call', 'Repository.is_shared', ('///quack/',))],
 
2323
            [('call', 'Repository.is_shared', ('quack/',))],
608
2324
            client._calls)
609
2325
        self.assertEqual(True, result)
610
2326
 
611
2327
    def test_is_not_shared(self):
612
2328
        # ('no', ) for Repository.is_shared -> 'False'.
613
 
        responses = [(('no', ), )]
614
2329
        transport_path = 'qwack'
615
 
        repo, client = self.setup_fake_client_and_repository(
616
 
            responses, transport_path)
 
2330
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2331
        client.add_success_response('no')
617
2332
        result = repo.is_shared()
618
2333
        self.assertEqual(
619
 
            [('call', 'Repository.is_shared', ('///qwack/',))],
 
2334
            [('call', 'Repository.is_shared', ('qwack/',))],
620
2335
            client._calls)
621
2336
        self.assertEqual(False, result)
622
2337
 
624
2339
class TestRepositoryLockWrite(TestRemoteRepository):
625
2340
 
626
2341
    def test_lock_write(self):
627
 
        responses = [(('ok', 'a token'), '')]
628
2342
        transport_path = 'quack'
629
 
        repo, client = self.setup_fake_client_and_repository(
630
 
            responses, transport_path)
631
 
        result = repo.lock_write()
 
2343
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2344
        client.add_success_response('ok', 'a token')
 
2345
        token = repo.lock_write().repository_token
632
2346
        self.assertEqual(
633
 
            [('call', 'Repository.lock_write', ('///quack/', ''))],
 
2347
            [('call', 'Repository.lock_write', ('quack/', ''))],
634
2348
            client._calls)
635
 
        self.assertEqual('a token', result)
 
2349
        self.assertEqual('a token', token)
636
2350
 
637
2351
    def test_lock_write_already_locked(self):
638
 
        responses = [(('LockContention', ), '')]
639
2352
        transport_path = 'quack'
640
 
        repo, client = self.setup_fake_client_and_repository(
641
 
            responses, transport_path)
 
2353
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2354
        client.add_error_response('LockContention')
642
2355
        self.assertRaises(errors.LockContention, repo.lock_write)
643
2356
        self.assertEqual(
644
 
            [('call', 'Repository.lock_write', ('///quack/', ''))],
 
2357
            [('call', 'Repository.lock_write', ('quack/', ''))],
645
2358
            client._calls)
646
2359
 
647
2360
    def test_lock_write_unlockable(self):
648
 
        responses = [(('UnlockableTransport', ), '')]
649
2361
        transport_path = 'quack'
650
 
        repo, client = self.setup_fake_client_and_repository(
651
 
            responses, transport_path)
 
2362
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2363
        client.add_error_response('UnlockableTransport')
652
2364
        self.assertRaises(errors.UnlockableTransport, repo.lock_write)
653
2365
        self.assertEqual(
654
 
            [('call', 'Repository.lock_write', ('///quack/', ''))],
 
2366
            [('call', 'Repository.lock_write', ('quack/', ''))],
655
2367
            client._calls)
656
2368
 
657
2369
 
 
2370
class TestRepositorySetMakeWorkingTrees(TestRemoteRepository):
 
2371
 
 
2372
    def test_backwards_compat(self):
 
2373
        self.setup_smart_server_with_call_log()
 
2374
        repo = self.make_repository('.')
 
2375
        self.reset_smart_call_log()
 
2376
        verb = 'Repository.set_make_working_trees'
 
2377
        self.disable_verb(verb)
 
2378
        repo.set_make_working_trees(True)
 
2379
        call_count = len([call for call in self.hpss_calls if
 
2380
            call.call.method == verb])
 
2381
        self.assertEqual(1, call_count)
 
2382
 
 
2383
    def test_current(self):
 
2384
        transport_path = 'quack'
 
2385
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2386
        client.add_expected_call(
 
2387
            'Repository.set_make_working_trees', ('quack/', 'True'),
 
2388
            'success', ('ok',))
 
2389
        client.add_expected_call(
 
2390
            'Repository.set_make_working_trees', ('quack/', 'False'),
 
2391
            'success', ('ok',))
 
2392
        repo.set_make_working_trees(True)
 
2393
        repo.set_make_working_trees(False)
 
2394
 
 
2395
 
658
2396
class TestRepositoryUnlock(TestRemoteRepository):
659
2397
 
660
2398
    def test_unlock(self):
661
 
        responses = [(('ok', 'a token'), ''),
662
 
                     (('ok',), '')]
663
2399
        transport_path = 'quack'
664
 
        repo, client = self.setup_fake_client_and_repository(
665
 
            responses, transport_path)
 
2400
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2401
        client.add_success_response('ok', 'a token')
 
2402
        client.add_success_response('ok')
666
2403
        repo.lock_write()
667
2404
        repo.unlock()
668
2405
        self.assertEqual(
669
 
            [('call', 'Repository.lock_write', ('///quack/', '')),
670
 
             ('call', 'Repository.unlock', ('///quack/', 'a token'))],
 
2406
            [('call', 'Repository.lock_write', ('quack/', '')),
 
2407
             ('call', 'Repository.unlock', ('quack/', 'a token'))],
671
2408
            client._calls)
672
2409
 
673
2410
    def test_unlock_wrong_token(self):
674
2411
        # If somehow the token is wrong, unlock will raise TokenMismatch.
675
 
        responses = [(('ok', 'a token'), ''),
676
 
                     (('TokenMismatch',), '')]
677
2412
        transport_path = 'quack'
678
 
        repo, client = self.setup_fake_client_and_repository(
679
 
            responses, transport_path)
 
2413
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2414
        client.add_success_response('ok', 'a token')
 
2415
        client.add_error_response('TokenMismatch')
680
2416
        repo.lock_write()
681
2417
        self.assertRaises(errors.TokenMismatch, repo.unlock)
682
2418
 
686
2422
    def test_none(self):
687
2423
        # repo.has_revision(None) should not cause any traffic.
688
2424
        transport_path = 'quack'
689
 
        responses = None
690
 
        repo, client = self.setup_fake_client_and_repository(
691
 
            responses, transport_path)
 
2425
        repo, client = self.setup_fake_client_and_repository(transport_path)
692
2426
 
693
2427
        # The null revision is always there, so has_revision(None) == True.
694
 
        self.assertEqual(True, repo.has_revision(None))
 
2428
        self.assertEqual(True, repo.has_revision(NULL_REVISION))
695
2429
 
696
2430
        # The remote repo shouldn't be accessed.
697
2431
        self.assertEqual([], client._calls)
698
2432
 
699
2433
 
 
2434
class TestRepositoryInsertStreamBase(TestRemoteRepository):
 
2435
    """Base class for Repository.insert_stream and .insert_stream_1.19
 
2436
    tests.
 
2437
    """
 
2438
    
 
2439
    def checkInsertEmptyStream(self, repo, client):
 
2440
        """Insert an empty stream, checking the result.
 
2441
 
 
2442
        This checks that there are no resume_tokens or missing_keys, and that
 
2443
        the client is finished.
 
2444
        """
 
2445
        sink = repo._get_sink()
 
2446
        fmt = repository.RepositoryFormat.get_default_format()
 
2447
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
 
2448
        self.assertEqual([], resume_tokens)
 
2449
        self.assertEqual(set(), missing_keys)
 
2450
        self.assertFinished(client)
 
2451
 
 
2452
 
 
2453
class TestRepositoryInsertStream(TestRepositoryInsertStreamBase):
 
2454
    """Tests for using Repository.insert_stream verb when the _1.19 variant is
 
2455
    not available.
 
2456
 
 
2457
    This test case is very similar to TestRepositoryInsertStream_1_19.
 
2458
    """
 
2459
 
 
2460
    def setUp(self):
 
2461
        TestRemoteRepository.setUp(self)
 
2462
        self.disable_verb('Repository.insert_stream_1.19')
 
2463
 
 
2464
    def test_unlocked_repo(self):
 
2465
        transport_path = 'quack'
 
2466
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2467
        client.add_expected_call(
 
2468
            'Repository.insert_stream_1.19', ('quack/', ''),
 
2469
            'unknown', ('Repository.insert_stream_1.19',))
 
2470
        client.add_expected_call(
 
2471
            'Repository.insert_stream', ('quack/', ''),
 
2472
            'success', ('ok',))
 
2473
        client.add_expected_call(
 
2474
            'Repository.insert_stream', ('quack/', ''),
 
2475
            'success', ('ok',))
 
2476
        self.checkInsertEmptyStream(repo, client)
 
2477
 
 
2478
    def test_locked_repo_with_no_lock_token(self):
 
2479
        transport_path = 'quack'
 
2480
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2481
        client.add_expected_call(
 
2482
            'Repository.lock_write', ('quack/', ''),
 
2483
            'success', ('ok', ''))
 
2484
        client.add_expected_call(
 
2485
            'Repository.insert_stream_1.19', ('quack/', ''),
 
2486
            'unknown', ('Repository.insert_stream_1.19',))
 
2487
        client.add_expected_call(
 
2488
            'Repository.insert_stream', ('quack/', ''),
 
2489
            'success', ('ok',))
 
2490
        client.add_expected_call(
 
2491
            'Repository.insert_stream', ('quack/', ''),
 
2492
            'success', ('ok',))
 
2493
        repo.lock_write()
 
2494
        self.checkInsertEmptyStream(repo, client)
 
2495
 
 
2496
    def test_locked_repo_with_lock_token(self):
 
2497
        transport_path = 'quack'
 
2498
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2499
        client.add_expected_call(
 
2500
            'Repository.lock_write', ('quack/', ''),
 
2501
            'success', ('ok', 'a token'))
 
2502
        client.add_expected_call(
 
2503
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
 
2504
            'unknown', ('Repository.insert_stream_1.19',))
 
2505
        client.add_expected_call(
 
2506
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
 
2507
            'success', ('ok',))
 
2508
        client.add_expected_call(
 
2509
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
 
2510
            'success', ('ok',))
 
2511
        repo.lock_write()
 
2512
        self.checkInsertEmptyStream(repo, client)
 
2513
 
 
2514
    def test_stream_with_inventory_deltas(self):
 
2515
        """'inventory-deltas' substreams cannot be sent to the
 
2516
        Repository.insert_stream verb, because not all servers that implement
 
2517
        that verb will accept them.  So when one is encountered the RemoteSink
 
2518
        immediately stops using that verb and falls back to VFS insert_stream.
 
2519
        """
 
2520
        transport_path = 'quack'
 
2521
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2522
        client.add_expected_call(
 
2523
            'Repository.insert_stream_1.19', ('quack/', ''),
 
2524
            'unknown', ('Repository.insert_stream_1.19',))
 
2525
        client.add_expected_call(
 
2526
            'Repository.insert_stream', ('quack/', ''),
 
2527
            'success', ('ok',))
 
2528
        client.add_expected_call(
 
2529
            'Repository.insert_stream', ('quack/', ''),
 
2530
            'success', ('ok',))
 
2531
        # Create a fake real repository for insert_stream to fall back on, so
 
2532
        # that we can directly see the records the RemoteSink passes to the
 
2533
        # real sink.
 
2534
        class FakeRealSink:
 
2535
            def __init__(self):
 
2536
                self.records = []
 
2537
            def insert_stream(self, stream, src_format, resume_tokens):
 
2538
                for substream_kind, substream in stream:
 
2539
                    self.records.append(
 
2540
                        (substream_kind, [record.key for record in substream]))
 
2541
                return ['fake tokens'], ['fake missing keys']
 
2542
        fake_real_sink = FakeRealSink()
 
2543
        class FakeRealRepository:
 
2544
            def _get_sink(self):
 
2545
                return fake_real_sink
 
2546
            def is_in_write_group(self):
 
2547
                return False
 
2548
            def refresh_data(self):
 
2549
                return True
 
2550
        repo._real_repository = FakeRealRepository()
 
2551
        sink = repo._get_sink()
 
2552
        fmt = repository.RepositoryFormat.get_default_format()
 
2553
        stream = self.make_stream_with_inv_deltas(fmt)
 
2554
        resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
 
2555
        # Every record from the first inventory delta should have been sent to
 
2556
        # the VFS sink.
 
2557
        expected_records = [
 
2558
            ('inventory-deltas', [('rev2',), ('rev3',)]),
 
2559
            ('texts', [('some-rev', 'some-file')])]
 
2560
        self.assertEqual(expected_records, fake_real_sink.records)
 
2561
        # The return values from the real sink's insert_stream are propagated
 
2562
        # back to the original caller.
 
2563
        self.assertEqual(['fake tokens'], resume_tokens)
 
2564
        self.assertEqual(['fake missing keys'], missing_keys)
 
2565
        self.assertFinished(client)
 
2566
 
 
2567
    def make_stream_with_inv_deltas(self, fmt):
 
2568
        """Make a simple stream with an inventory delta followed by more
 
2569
        records and more substreams to test that all records and substreams
 
2570
        from that point on are used.
 
2571
 
 
2572
        This sends, in order:
 
2573
           * inventories substream: rev1, rev2, rev3.  rev2 and rev3 are
 
2574
             inventory-deltas.
 
2575
           * texts substream: (some-rev, some-file)
 
2576
        """
 
2577
        # Define a stream using generators so that it isn't rewindable.
 
2578
        inv = inventory.Inventory(revision_id='rev1')
 
2579
        inv.root.revision = 'rev1'
 
2580
        def stream_with_inv_delta():
 
2581
            yield ('inventories', inventories_substream())
 
2582
            yield ('inventory-deltas', inventory_delta_substream())
 
2583
            yield ('texts', [
 
2584
                versionedfile.FulltextContentFactory(
 
2585
                    ('some-rev', 'some-file'), (), None, 'content')])
 
2586
        def inventories_substream():
 
2587
            # An empty inventory fulltext.  This will be streamed normally.
 
2588
            text = fmt._serializer.write_inventory_to_string(inv)
 
2589
            yield versionedfile.FulltextContentFactory(
 
2590
                ('rev1',), (), None, text)
 
2591
        def inventory_delta_substream():
 
2592
            # An inventory delta.  This can't be streamed via this verb, so it
 
2593
            # will trigger a fallback to VFS insert_stream.
 
2594
            entry = inv.make_entry(
 
2595
                'directory', 'newdir', inv.root.file_id, 'newdir-id')
 
2596
            entry.revision = 'ghost'
 
2597
            delta = [(None, 'newdir', 'newdir-id', entry)]
 
2598
            serializer = inventory_delta.InventoryDeltaSerializer(
 
2599
                versioned_root=True, tree_references=False)
 
2600
            lines = serializer.delta_to_lines('rev1', 'rev2', delta)
 
2601
            yield versionedfile.ChunkedContentFactory(
 
2602
                ('rev2',), (('rev1',)), None, lines)
 
2603
            # Another delta.
 
2604
            lines = serializer.delta_to_lines('rev1', 'rev3', delta)
 
2605
            yield versionedfile.ChunkedContentFactory(
 
2606
                ('rev3',), (('rev1',)), None, lines)
 
2607
        return stream_with_inv_delta()
 
2608
 
 
2609
 
 
2610
class TestRepositoryInsertStream_1_19(TestRepositoryInsertStreamBase):
 
2611
 
 
2612
    def test_unlocked_repo(self):
 
2613
        transport_path = 'quack'
 
2614
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2615
        client.add_expected_call(
 
2616
            'Repository.insert_stream_1.19', ('quack/', ''),
 
2617
            'success', ('ok',))
 
2618
        client.add_expected_call(
 
2619
            'Repository.insert_stream_1.19', ('quack/', ''),
 
2620
            'success', ('ok',))
 
2621
        self.checkInsertEmptyStream(repo, client)
 
2622
 
 
2623
    def test_locked_repo_with_no_lock_token(self):
 
2624
        transport_path = 'quack'
 
2625
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2626
        client.add_expected_call(
 
2627
            'Repository.lock_write', ('quack/', ''),
 
2628
            'success', ('ok', ''))
 
2629
        client.add_expected_call(
 
2630
            'Repository.insert_stream_1.19', ('quack/', ''),
 
2631
            'success', ('ok',))
 
2632
        client.add_expected_call(
 
2633
            'Repository.insert_stream_1.19', ('quack/', ''),
 
2634
            'success', ('ok',))
 
2635
        repo.lock_write()
 
2636
        self.checkInsertEmptyStream(repo, client)
 
2637
 
 
2638
    def test_locked_repo_with_lock_token(self):
 
2639
        transport_path = 'quack'
 
2640
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2641
        client.add_expected_call(
 
2642
            'Repository.lock_write', ('quack/', ''),
 
2643
            'success', ('ok', 'a token'))
 
2644
        client.add_expected_call(
 
2645
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
 
2646
            'success', ('ok',))
 
2647
        client.add_expected_call(
 
2648
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
 
2649
            'success', ('ok',))
 
2650
        repo.lock_write()
 
2651
        self.checkInsertEmptyStream(repo, client)
 
2652
 
 
2653
 
700
2654
class TestRepositoryTarball(TestRemoteRepository):
701
2655
 
702
2656
    # This is a canned tarball reponse we can validate against
718
2672
    def test_repository_tarball(self):
719
2673
        # Test that Repository.tarball generates the right operations
720
2674
        transport_path = 'repo'
721
 
        expected_responses = [(('ok',), self.tarball_content),
722
 
            ]
723
2675
        expected_calls = [('call_expecting_body', 'Repository.tarball',
724
 
                           ('///repo/', 'bz2',),),
 
2676
                           ('repo/', 'bz2',),),
725
2677
            ]
726
 
        remote_repo, client = self.setup_fake_client_and_repository(
727
 
            expected_responses, transport_path)
 
2678
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2679
        client.add_success_response_with_body(self.tarball_content, 'ok')
728
2680
        # Now actually ask for the tarball
729
 
        tarball_file = remote_repo._get_tarball('bz2')
 
2681
        tarball_file = repo._get_tarball('bz2')
730
2682
        try:
731
2683
            self.assertEqual(expected_calls, client._calls)
732
2684
            self.assertEqual(self.tarball_content, tarball_file.read())
733
2685
        finally:
734
2686
            tarball_file.close()
735
2687
 
736
 
    def test_sprout_uses_tarball(self):
737
 
        # RemoteRepository.sprout should try to use the
738
 
        # tarball command rather than accessing all the files
739
 
        transport_path = 'srcrepo'
740
 
        expected_responses = [(('ok',), self.tarball_content),
741
 
            ]
742
 
        expected_calls = [('call2', 'Repository.tarball', ('///srcrepo/', 'bz2',),),
743
 
            ]
744
 
        remote_repo, client = self.setup_fake_client_and_repository(
745
 
            expected_responses, transport_path)
746
 
        # make a regular local repository to receive the results
747
 
        dest_transport = MemoryTransport()
748
 
        dest_transport.mkdir('destrepo')
749
 
        bzrdir_format = bzrdir.format_registry.make_bzrdir('default')
750
 
        dest_bzrdir = bzrdir_format.initialize_on_transport(dest_transport)
751
 
        # try to copy...
752
 
        remote_repo.sprout(dest_bzrdir)
753
 
 
754
2688
 
755
2689
class TestRemoteRepositoryCopyContent(tests.TestCaseWithTransport):
756
2690
    """RemoteRepository.copy_content_into optimizations"""
757
2691
 
758
2692
    def test_copy_content_remote_to_local(self):
759
 
        self.transport_server = server.SmartTCPServer_for_testing
 
2693
        self.transport_server = test_server.SmartTCPServer_for_testing
760
2694
        src_repo = self.make_repository('repo1')
761
2695
        src_repo = repository.Repository.open(self.get_url('repo1'))
762
2696
        # At the moment the tarball-based copy_content_into can't write back
769
2703
        self.assertFalse(isinstance(dest_repo, RemoteRepository))
770
2704
        self.assertTrue(isinstance(src_repo, RemoteRepository))
771
2705
        src_repo.copy_content_into(dest_repo)
 
2706
 
 
2707
 
 
2708
class _StubRealPackRepository(object):
 
2709
 
 
2710
    def __init__(self, calls):
 
2711
        self.calls = calls
 
2712
        self._pack_collection = _StubPackCollection(calls)
 
2713
 
 
2714
    def is_in_write_group(self):
 
2715
        return False
 
2716
 
 
2717
    def refresh_data(self):
 
2718
        self.calls.append(('pack collection reload_pack_names',))
 
2719
 
 
2720
 
 
2721
class _StubPackCollection(object):
 
2722
 
 
2723
    def __init__(self, calls):
 
2724
        self.calls = calls
 
2725
 
 
2726
    def autopack(self):
 
2727
        self.calls.append(('pack collection autopack',))
 
2728
 
 
2729
 
 
2730
class TestRemotePackRepositoryAutoPack(TestRemoteRepository):
 
2731
    """Tests for RemoteRepository.autopack implementation."""
 
2732
 
 
2733
    def test_ok(self):
 
2734
        """When the server returns 'ok' and there's no _real_repository, then
 
2735
        nothing else happens: the autopack method is done.
 
2736
        """
 
2737
        transport_path = 'quack'
 
2738
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2739
        client.add_expected_call(
 
2740
            'PackRepository.autopack', ('quack/',), 'success', ('ok',))
 
2741
        repo.autopack()
 
2742
        self.assertFinished(client)
 
2743
 
 
2744
    def test_ok_with_real_repo(self):
 
2745
        """When the server returns 'ok' and there is a _real_repository, then
 
2746
        the _real_repository's reload_pack_name's method will be called.
 
2747
        """
 
2748
        transport_path = 'quack'
 
2749
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2750
        client.add_expected_call(
 
2751
            'PackRepository.autopack', ('quack/',),
 
2752
            'success', ('ok',))
 
2753
        repo._real_repository = _StubRealPackRepository(client._calls)
 
2754
        repo.autopack()
 
2755
        self.assertEqual(
 
2756
            [('call', 'PackRepository.autopack', ('quack/',)),
 
2757
             ('pack collection reload_pack_names',)],
 
2758
            client._calls)
 
2759
 
 
2760
    def test_backwards_compatibility(self):
 
2761
        """If the server does not recognise the PackRepository.autopack verb,
 
2762
        fallback to the real_repository's implementation.
 
2763
        """
 
2764
        transport_path = 'quack'
 
2765
        repo, client = self.setup_fake_client_and_repository(transport_path)
 
2766
        client.add_unknown_method_response('PackRepository.autopack')
 
2767
        def stub_ensure_real():
 
2768
            client._calls.append(('_ensure_real',))
 
2769
            repo._real_repository = _StubRealPackRepository(client._calls)
 
2770
        repo._ensure_real = stub_ensure_real
 
2771
        repo.autopack()
 
2772
        self.assertEqual(
 
2773
            [('call', 'PackRepository.autopack', ('quack/',)),
 
2774
             ('_ensure_real',),
 
2775
             ('pack collection autopack',)],
 
2776
            client._calls)
 
2777
 
 
2778
 
 
2779
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
 
2780
    """Base class for unit tests for bzrlib.remote._translate_error."""
 
2781
 
 
2782
    def translateTuple(self, error_tuple, **context):
 
2783
        """Call _translate_error with an ErrorFromSmartServer built from the
 
2784
        given error_tuple.
 
2785
 
 
2786
        :param error_tuple: A tuple of a smart server response, as would be
 
2787
            passed to an ErrorFromSmartServer.
 
2788
        :kwargs context: context items to call _translate_error with.
 
2789
 
 
2790
        :returns: The error raised by _translate_error.
 
2791
        """
 
2792
        # Raise the ErrorFromSmartServer before passing it as an argument,
 
2793
        # because _translate_error may need to re-raise it with a bare 'raise'
 
2794
        # statement.
 
2795
        server_error = errors.ErrorFromSmartServer(error_tuple)
 
2796
        translated_error = self.translateErrorFromSmartServer(
 
2797
            server_error, **context)
 
2798
        return translated_error
 
2799
 
 
2800
    def translateErrorFromSmartServer(self, error_object, **context):
 
2801
        """Like translateTuple, but takes an already constructed
 
2802
        ErrorFromSmartServer rather than a tuple.
 
2803
        """
 
2804
        try:
 
2805
            raise error_object
 
2806
        except errors.ErrorFromSmartServer, server_error:
 
2807
            translated_error = self.assertRaises(
 
2808
                errors.BzrError, remote._translate_error, server_error,
 
2809
                **context)
 
2810
        return translated_error
 
2811
 
 
2812
 
 
2813
class TestErrorTranslationSuccess(TestErrorTranslationBase):
 
2814
    """Unit tests for bzrlib.remote._translate_error.
 
2815
 
 
2816
    Given an ErrorFromSmartServer (which has an error tuple from a smart
 
2817
    server) and some context, _translate_error raises more specific errors from
 
2818
    bzrlib.errors.
 
2819
 
 
2820
    This test case covers the cases where _translate_error succeeds in
 
2821
    translating an ErrorFromSmartServer to something better.  See
 
2822
    TestErrorTranslationRobustness for other cases.
 
2823
    """
 
2824
 
 
2825
    def test_NoSuchRevision(self):
 
2826
        branch = self.make_branch('')
 
2827
        revid = 'revid'
 
2828
        translated_error = self.translateTuple(
 
2829
            ('NoSuchRevision', revid), branch=branch)
 
2830
        expected_error = errors.NoSuchRevision(branch, revid)
 
2831
        self.assertEqual(expected_error, translated_error)
 
2832
 
 
2833
    def test_nosuchrevision(self):
 
2834
        repository = self.make_repository('')
 
2835
        revid = 'revid'
 
2836
        translated_error = self.translateTuple(
 
2837
            ('nosuchrevision', revid), repository=repository)
 
2838
        expected_error = errors.NoSuchRevision(repository, revid)
 
2839
        self.assertEqual(expected_error, translated_error)
 
2840
 
 
2841
    def test_nobranch(self):
 
2842
        bzrdir = self.make_bzrdir('')
 
2843
        translated_error = self.translateTuple(('nobranch',), bzrdir=bzrdir)
 
2844
        expected_error = errors.NotBranchError(path=bzrdir.root_transport.base)
 
2845
        self.assertEqual(expected_error, translated_error)
 
2846
 
 
2847
    def test_nobranch_one_arg(self):
 
2848
        bzrdir = self.make_bzrdir('')
 
2849
        translated_error = self.translateTuple(
 
2850
            ('nobranch', 'extra detail'), bzrdir=bzrdir)
 
2851
        expected_error = errors.NotBranchError(
 
2852
            path=bzrdir.root_transport.base,
 
2853
            detail='extra detail')
 
2854
        self.assertEqual(expected_error, translated_error)
 
2855
 
 
2856
    def test_LockContention(self):
 
2857
        translated_error = self.translateTuple(('LockContention',))
 
2858
        expected_error = errors.LockContention('(remote lock)')
 
2859
        self.assertEqual(expected_error, translated_error)
 
2860
 
 
2861
    def test_UnlockableTransport(self):
 
2862
        bzrdir = self.make_bzrdir('')
 
2863
        translated_error = self.translateTuple(
 
2864
            ('UnlockableTransport',), bzrdir=bzrdir)
 
2865
        expected_error = errors.UnlockableTransport(bzrdir.root_transport)
 
2866
        self.assertEqual(expected_error, translated_error)
 
2867
 
 
2868
    def test_LockFailed(self):
 
2869
        lock = 'str() of a server lock'
 
2870
        why = 'str() of why'
 
2871
        translated_error = self.translateTuple(('LockFailed', lock, why))
 
2872
        expected_error = errors.LockFailed(lock, why)
 
2873
        self.assertEqual(expected_error, translated_error)
 
2874
 
 
2875
    def test_TokenMismatch(self):
 
2876
        token = 'a lock token'
 
2877
        translated_error = self.translateTuple(('TokenMismatch',), token=token)
 
2878
        expected_error = errors.TokenMismatch(token, '(remote token)')
 
2879
        self.assertEqual(expected_error, translated_error)
 
2880
 
 
2881
    def test_Diverged(self):
 
2882
        branch = self.make_branch('a')
 
2883
        other_branch = self.make_branch('b')
 
2884
        translated_error = self.translateTuple(
 
2885
            ('Diverged',), branch=branch, other_branch=other_branch)
 
2886
        expected_error = errors.DivergedBranches(branch, other_branch)
 
2887
        self.assertEqual(expected_error, translated_error)
 
2888
 
 
2889
    def test_ReadError_no_args(self):
 
2890
        path = 'a path'
 
2891
        translated_error = self.translateTuple(('ReadError',), path=path)
 
2892
        expected_error = errors.ReadError(path)
 
2893
        self.assertEqual(expected_error, translated_error)
 
2894
 
 
2895
    def test_ReadError(self):
 
2896
        path = 'a path'
 
2897
        translated_error = self.translateTuple(('ReadError', path))
 
2898
        expected_error = errors.ReadError(path)
 
2899
        self.assertEqual(expected_error, translated_error)
 
2900
 
 
2901
    def test_IncompatibleRepositories(self):
 
2902
        translated_error = self.translateTuple(('IncompatibleRepositories',
 
2903
            "repo1", "repo2", "details here"))
 
2904
        expected_error = errors.IncompatibleRepositories("repo1", "repo2",
 
2905
            "details here")
 
2906
        self.assertEqual(expected_error, translated_error)
 
2907
 
 
2908
    def test_PermissionDenied_no_args(self):
 
2909
        path = 'a path'
 
2910
        translated_error = self.translateTuple(('PermissionDenied',), path=path)
 
2911
        expected_error = errors.PermissionDenied(path)
 
2912
        self.assertEqual(expected_error, translated_error)
 
2913
 
 
2914
    def test_PermissionDenied_one_arg(self):
 
2915
        path = 'a path'
 
2916
        translated_error = self.translateTuple(('PermissionDenied', path))
 
2917
        expected_error = errors.PermissionDenied(path)
 
2918
        self.assertEqual(expected_error, translated_error)
 
2919
 
 
2920
    def test_PermissionDenied_one_arg_and_context(self):
 
2921
        """Given a choice between a path from the local context and a path on
 
2922
        the wire, _translate_error prefers the path from the local context.
 
2923
        """
 
2924
        local_path = 'local path'
 
2925
        remote_path = 'remote path'
 
2926
        translated_error = self.translateTuple(
 
2927
            ('PermissionDenied', remote_path), path=local_path)
 
2928
        expected_error = errors.PermissionDenied(local_path)
 
2929
        self.assertEqual(expected_error, translated_error)
 
2930
 
 
2931
    def test_PermissionDenied_two_args(self):
 
2932
        path = 'a path'
 
2933
        extra = 'a string with extra info'
 
2934
        translated_error = self.translateTuple(
 
2935
            ('PermissionDenied', path, extra))
 
2936
        expected_error = errors.PermissionDenied(path, extra)
 
2937
        self.assertEqual(expected_error, translated_error)
 
2938
 
 
2939
 
 
2940
class TestErrorTranslationRobustness(TestErrorTranslationBase):
 
2941
    """Unit tests for bzrlib.remote._translate_error's robustness.
 
2942
 
 
2943
    TestErrorTranslationSuccess is for cases where _translate_error can
 
2944
    translate successfully.  This class about how _translate_err behaves when
 
2945
    it fails to translate: it re-raises the original error.
 
2946
    """
 
2947
 
 
2948
    def test_unrecognised_server_error(self):
 
2949
        """If the error code from the server is not recognised, the original
 
2950
        ErrorFromSmartServer is propagated unmodified.
 
2951
        """
 
2952
        error_tuple = ('An unknown error tuple',)
 
2953
        server_error = errors.ErrorFromSmartServer(error_tuple)
 
2954
        translated_error = self.translateErrorFromSmartServer(server_error)
 
2955
        expected_error = errors.UnknownErrorFromSmartServer(server_error)
 
2956
        self.assertEqual(expected_error, translated_error)
 
2957
 
 
2958
    def test_context_missing_a_key(self):
 
2959
        """In case of a bug in the client, or perhaps an unexpected response
 
2960
        from a server, _translate_error returns the original error tuple from
 
2961
        the server and mutters a warning.
 
2962
        """
 
2963
        # To translate a NoSuchRevision error _translate_error needs a 'branch'
 
2964
        # in the context dict.  So let's give it an empty context dict instead
 
2965
        # to exercise its error recovery.
 
2966
        empty_context = {}
 
2967
        error_tuple = ('NoSuchRevision', 'revid')
 
2968
        server_error = errors.ErrorFromSmartServer(error_tuple)
 
2969
        translated_error = self.translateErrorFromSmartServer(server_error)
 
2970
        self.assertEqual(server_error, translated_error)
 
2971
        # In addition to re-raising ErrorFromSmartServer, some debug info has
 
2972
        # been muttered to the log file for developer to look at.
 
2973
        self.assertContainsRe(
 
2974
            self.get_log(),
 
2975
            "Missing key 'branch' in context")
 
2976
 
 
2977
    def test_path_missing(self):
 
2978
        """Some translations (PermissionDenied, ReadError) can determine the
 
2979
        'path' variable from either the wire or the local context.  If neither
 
2980
        has it, then an error is raised.
 
2981
        """
 
2982
        error_tuple = ('ReadError',)
 
2983
        server_error = errors.ErrorFromSmartServer(error_tuple)
 
2984
        translated_error = self.translateErrorFromSmartServer(server_error)
 
2985
        self.assertEqual(server_error, translated_error)
 
2986
        # In addition to re-raising ErrorFromSmartServer, some debug info has
 
2987
        # been muttered to the log file for developer to look at.
 
2988
        self.assertContainsRe(self.get_log(), "Missing key 'path' in context")
 
2989
 
 
2990
 
 
2991
class TestStacking(tests.TestCaseWithTransport):
 
2992
    """Tests for operations on stacked remote repositories.
 
2993
 
 
2994
    The underlying format type must support stacking.
 
2995
    """
 
2996
 
 
2997
    def test_access_stacked_remote(self):
 
2998
        # based on <http://launchpad.net/bugs/261315>
 
2999
        # make a branch stacked on another repository containing an empty
 
3000
        # revision, then open it over hpss - we should be able to see that
 
3001
        # revision.
 
3002
        base_transport = self.get_transport()
 
3003
        base_builder = self.make_branch_builder('base', format='1.9')
 
3004
        base_builder.start_series()
 
3005
        base_revid = base_builder.build_snapshot('rev-id', None,
 
3006
            [('add', ('', None, 'directory', None))],
 
3007
            'message')
 
3008
        base_builder.finish_series()
 
3009
        stacked_branch = self.make_branch('stacked', format='1.9')
 
3010
        stacked_branch.set_stacked_on_url('../base')
 
3011
        # start a server looking at this
 
3012
        smart_server = test_server.SmartTCPServer_for_testing()
 
3013
        self.start_server(smart_server)
 
3014
        remote_bzrdir = BzrDir.open(smart_server.get_url() + '/stacked')
 
3015
        # can get its branch and repository
 
3016
        remote_branch = remote_bzrdir.open_branch()
 
3017
        remote_repo = remote_branch.repository
 
3018
        remote_repo.lock_read()
 
3019
        try:
 
3020
            # it should have an appropriate fallback repository, which should also
 
3021
            # be a RemoteRepository
 
3022
            self.assertLength(1, remote_repo._fallback_repositories)
 
3023
            self.assertIsInstance(remote_repo._fallback_repositories[0],
 
3024
                RemoteRepository)
 
3025
            # and it has the revision committed to the underlying repository;
 
3026
            # these have varying implementations so we try several of them
 
3027
            self.assertTrue(remote_repo.has_revisions([base_revid]))
 
3028
            self.assertTrue(remote_repo.has_revision(base_revid))
 
3029
            self.assertEqual(remote_repo.get_revision(base_revid).message,
 
3030
                'message')
 
3031
        finally:
 
3032
            remote_repo.unlock()
 
3033
 
 
3034
    def prepare_stacked_remote_branch(self):
 
3035
        """Get stacked_upon and stacked branches with content in each."""
 
3036
        self.setup_smart_server_with_call_log()
 
3037
        tree1 = self.make_branch_and_tree('tree1', format='1.9')
 
3038
        tree1.commit('rev1', rev_id='rev1')
 
3039
        tree2 = tree1.branch.bzrdir.sprout('tree2', stacked=True
 
3040
            ).open_workingtree()
 
3041
        local_tree = tree2.branch.create_checkout('local')
 
3042
        local_tree.commit('local changes make me feel good.')
 
3043
        branch2 = Branch.open(self.get_url('tree2'))
 
3044
        branch2.lock_read()
 
3045
        self.addCleanup(branch2.unlock)
 
3046
        return tree1.branch, branch2
 
3047
 
 
3048
    def test_stacked_get_parent_map(self):
 
3049
        # the public implementation of get_parent_map obeys stacking
 
3050
        _, branch = self.prepare_stacked_remote_branch()
 
3051
        repo = branch.repository
 
3052
        self.assertEqual(['rev1'], repo.get_parent_map(['rev1']).keys())
 
3053
 
 
3054
    def test_unstacked_get_parent_map(self):
 
3055
        # _unstacked_provider.get_parent_map ignores stacking
 
3056
        _, branch = self.prepare_stacked_remote_branch()
 
3057
        provider = branch.repository._unstacked_provider
 
3058
        self.assertEqual([], provider.get_parent_map(['rev1']).keys())
 
3059
 
 
3060
    def fetch_stream_to_rev_order(self, stream):
 
3061
        result = []
 
3062
        for kind, substream in stream:
 
3063
            if not kind == 'revisions':
 
3064
                list(substream)
 
3065
            else:
 
3066
                for content in substream:
 
3067
                    result.append(content.key[-1])
 
3068
        return result
 
3069
 
 
3070
    def get_ordered_revs(self, format, order, branch_factory=None):
 
3071
        """Get a list of the revisions in a stream to format format.
 
3072
 
 
3073
        :param format: The format of the target.
 
3074
        :param order: the order that target should have requested.
 
3075
        :param branch_factory: A callable to create a trunk and stacked branch
 
3076
            to fetch from. If none, self.prepare_stacked_remote_branch is used.
 
3077
        :result: The revision ids in the stream, in the order seen,
 
3078
            the topological order of revisions in the source.
 
3079
        """
 
3080
        unordered_format = bzrdir.format_registry.get(format)()
 
3081
        target_repository_format = unordered_format.repository_format
 
3082
        # Cross check
 
3083
        self.assertEqual(order, target_repository_format._fetch_order)
 
3084
        if branch_factory is None:
 
3085
            branch_factory = self.prepare_stacked_remote_branch
 
3086
        _, stacked = branch_factory()
 
3087
        source = stacked.repository._get_source(target_repository_format)
 
3088
        tip = stacked.last_revision()
 
3089
        revs = stacked.repository.get_ancestry(tip)
 
3090
        search = graph.PendingAncestryResult([tip], stacked.repository)
 
3091
        self.reset_smart_call_log()
 
3092
        stream = source.get_stream(search)
 
3093
        if None in revs:
 
3094
            revs.remove(None)
 
3095
        # We trust that if a revision is in the stream the rest of the new
 
3096
        # content for it is too, as per our main fetch tests; here we are
 
3097
        # checking that the revisions are actually included at all, and their
 
3098
        # order.
 
3099
        return self.fetch_stream_to_rev_order(stream), revs
 
3100
 
 
3101
    def test_stacked_get_stream_unordered(self):
 
3102
        # Repository._get_source.get_stream() from a stacked repository with
 
3103
        # unordered yields the full data from both stacked and stacked upon
 
3104
        # sources.
 
3105
        rev_ord, expected_revs = self.get_ordered_revs('1.9', 'unordered')
 
3106
        self.assertEqual(set(expected_revs), set(rev_ord))
 
3107
        # Getting unordered results should have made a streaming data request
 
3108
        # from the server, then one from the backing branch.
 
3109
        self.assertLength(2, self.hpss_calls)
 
3110
 
 
3111
    def test_stacked_on_stacked_get_stream_unordered(self):
 
3112
        # Repository._get_source.get_stream() from a stacked repository which
 
3113
        # is itself stacked yields the full data from all three sources.
 
3114
        def make_stacked_stacked():
 
3115
            _, stacked = self.prepare_stacked_remote_branch()
 
3116
            tree = stacked.bzrdir.sprout('tree3', stacked=True
 
3117
                ).open_workingtree()
 
3118
            local_tree = tree.branch.create_checkout('local-tree3')
 
3119
            local_tree.commit('more local changes are better')
 
3120
            branch = Branch.open(self.get_url('tree3'))
 
3121
            branch.lock_read()
 
3122
            self.addCleanup(branch.unlock)
 
3123
            return None, branch
 
3124
        rev_ord, expected_revs = self.get_ordered_revs('1.9', 'unordered',
 
3125
            branch_factory=make_stacked_stacked)
 
3126
        self.assertEqual(set(expected_revs), set(rev_ord))
 
3127
        # Getting unordered results should have made a streaming data request
 
3128
        # from the server, and one from each backing repo
 
3129
        self.assertLength(3, self.hpss_calls)
 
3130
 
 
3131
    def test_stacked_get_stream_topological(self):
 
3132
        # Repository._get_source.get_stream() from a stacked repository with
 
3133
        # topological sorting yields the full data from both stacked and
 
3134
        # stacked upon sources in topological order.
 
3135
        rev_ord, expected_revs = self.get_ordered_revs('knit', 'topological')
 
3136
        self.assertEqual(expected_revs, rev_ord)
 
3137
        # Getting topological sort requires VFS calls still - one of which is
 
3138
        # pushing up from the bound branch.
 
3139
        self.assertLength(13, self.hpss_calls)
 
3140
 
 
3141
    def test_stacked_get_stream_groupcompress(self):
 
3142
        # Repository._get_source.get_stream() from a stacked repository with
 
3143
        # groupcompress sorting yields the full data from both stacked and
 
3144
        # stacked upon sources in groupcompress order.
 
3145
        raise tests.TestSkipped('No groupcompress ordered format available')
 
3146
        rev_ord, expected_revs = self.get_ordered_revs('dev5', 'groupcompress')
 
3147
        self.assertEqual(expected_revs, reversed(rev_ord))
 
3148
        # Getting unordered results should have made a streaming data request
 
3149
        # from the backing branch, and one from the stacked on branch.
 
3150
        self.assertLength(2, self.hpss_calls)
 
3151
 
 
3152
    def test_stacked_pull_more_than_stacking_has_bug_360791(self):
 
3153
        # When pulling some fixed amount of content that is more than the
 
3154
        # source has (because some is coming from a fallback branch, no error
 
3155
        # should be received. This was reported as bug 360791.
 
3156
        # Need three branches: a trunk, a stacked branch, and a preexisting
 
3157
        # branch pulling content from stacked and trunk.
 
3158
        self.setup_smart_server_with_call_log()
 
3159
        trunk = self.make_branch_and_tree('trunk', format="1.9-rich-root")
 
3160
        r1 = trunk.commit('start')
 
3161
        stacked_branch = trunk.branch.create_clone_on_transport(
 
3162
            self.get_transport('stacked'), stacked_on=trunk.branch.base)
 
3163
        local = self.make_branch('local', format='1.9-rich-root')
 
3164
        local.repository.fetch(stacked_branch.repository,
 
3165
            stacked_branch.last_revision())
 
3166
 
 
3167
 
 
3168
class TestRemoteBranchEffort(tests.TestCaseWithTransport):
 
3169
 
 
3170
    def setUp(self):
 
3171
        super(TestRemoteBranchEffort, self).setUp()
 
3172
        # Create a smart server that publishes whatever the backing VFS server
 
3173
        # does.
 
3174
        self.smart_server = test_server.SmartTCPServer_for_testing()
 
3175
        self.start_server(self.smart_server, self.get_server())
 
3176
        # Log all HPSS calls into self.hpss_calls.
 
3177
        _SmartClient.hooks.install_named_hook(
 
3178
            'call', self.capture_hpss_call, None)
 
3179
        self.hpss_calls = []
 
3180
 
 
3181
    def capture_hpss_call(self, params):
 
3182
        self.hpss_calls.append(params.method)
 
3183
 
 
3184
    def test_copy_content_into_avoids_revision_history(self):
 
3185
        local = self.make_branch('local')
 
3186
        remote_backing_tree = self.make_branch_and_tree('remote')
 
3187
        remote_backing_tree.commit("Commit.")
 
3188
        remote_branch_url = self.smart_server.get_url() + 'remote'
 
3189
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
 
3190
        local.repository.fetch(remote_branch.repository)
 
3191
        self.hpss_calls = []
 
3192
        remote_branch.copy_content_into(local)
 
3193
        self.assertFalse('Branch.revision_history' in self.hpss_calls)