~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_remote.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2008-09-11 06:10:59 UTC
  • mfrom: (3702.1.1 trivial)
  • Revision ID: pqm@pqm.ubuntu.com-20080911061059-svzqfejar17ui4zw
(mbp) KnitVersionedFiles repr

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2011 Canonical Ltd
 
1
# Copyright (C) 2006, 2007, 2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
"""
27
27
from cStringIO import StringIO
28
28
 
29
29
from bzrlib import (
30
 
    branch,
31
 
    bzrdir,
32
 
    config,
33
 
    controldir,
34
30
    errors,
35
 
    graph as _mod_graph,
36
 
    inventory,
37
 
    inventory_delta,
 
31
    graph,
 
32
    pack,
38
33
    remote,
39
34
    repository,
40
35
    tests,
41
 
    transport,
42
 
    treebuilder,
43
 
    versionedfile,
44
36
    )
45
37
from bzrlib.branch import Branch
46
 
from bzrlib.bzrdir import (
47
 
    BzrDir,
48
 
    BzrDirFormat,
49
 
    RemoteBzrProber,
50
 
    )
 
38
from bzrlib.bzrdir import BzrDir, BzrDirFormat
51
39
from bzrlib.remote import (
52
40
    RemoteBranch,
53
 
    RemoteBranchFormat,
54
41
    RemoteBzrDir,
55
42
    RemoteBzrDirFormat,
56
43
    RemoteRepository,
57
 
    RemoteRepositoryFormat,
58
44
    )
59
 
from bzrlib.repofmt import groupcompress_repo, knitpack_repo
60
45
from bzrlib.revision import NULL_REVISION
61
 
from bzrlib.smart import medium, request
 
46
from bzrlib.smart import server, medium
62
47
from bzrlib.smart.client import _SmartClient
63
 
from bzrlib.smart.repository import (
64
 
    SmartServerRepositoryGetParentMap,
65
 
    SmartServerRepositoryGetStream_1_19,
66
 
    )
67
 
from bzrlib.tests import (
68
 
    test_server,
69
 
    )
70
 
from bzrlib.tests.scenarios import load_tests_apply_scenarios
 
48
from bzrlib.symbol_versioning import one_four
 
49
from bzrlib.transport import get_transport, http
71
50
from bzrlib.transport.memory import MemoryTransport
72
 
from bzrlib.transport.remote import (
73
 
    RemoteTransport,
74
 
    RemoteSSHTransport,
75
 
    RemoteTCPTransport,
76
 
    )
77
 
 
78
 
 
79
 
load_tests = load_tests_apply_scenarios
 
51
from bzrlib.transport.remote import RemoteTransport, RemoteTCPTransport
80
52
 
81
53
 
82
54
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
83
55
 
84
 
    scenarios = [
85
 
        ('HPSS-v2',
86
 
            {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
87
 
        ('HPSS-v3',
88
 
            {'transport_server': test_server.SmartTCPServer_for_testing})]
89
 
 
90
 
 
91
56
    def setUp(self):
 
57
        self.transport_server = server.SmartTCPServer_for_testing
92
58
        super(BasicRemoteObjectTests, self).setUp()
93
59
        self.transport = self.get_transport()
94
60
        # make a branch that can be opened over the smart transport
95
61
        self.local_wt = BzrDir.create_standalone_workingtree('.')
96
 
        self.addCleanup(self.transport.disconnect)
 
62
 
 
63
    def tearDown(self):
 
64
        self.transport.disconnect()
 
65
        tests.TestCaseWithTransport.tearDown(self)
97
66
 
98
67
    def test_create_remote_bzrdir(self):
99
 
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
 
68
        b = remote.RemoteBzrDir(self.transport)
100
69
        self.assertIsInstance(b, BzrDir)
101
70
 
102
71
    def test_open_remote_branch(self):
103
72
        # open a standalone branch in the working directory
104
 
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
 
73
        b = remote.RemoteBzrDir(self.transport)
105
74
        branch = b.open_branch()
106
75
        self.assertIsInstance(branch, Branch)
107
76
 
123
92
    def test_find_correct_format(self):
124
93
        """Should open a RemoteBzrDir over a RemoteTransport"""
125
94
        fmt = BzrDirFormat.find_format(self.transport)
126
 
        self.assertTrue(bzrdir.RemoteBzrProber
127
 
                        in controldir.ControlDirFormat._server_probers)
128
 
        self.assertIsInstance(fmt, RemoteBzrDirFormat)
 
95
        self.assertTrue(RemoteBzrDirFormat
 
96
                        in BzrDirFormat._control_server_formats)
 
97
        self.assertIsInstance(fmt, remote.RemoteBzrDirFormat)
129
98
 
130
99
    def test_open_detected_smart_format(self):
131
100
        fmt = BzrDirFormat.find_format(self.transport)
136
105
        b = BzrDir.open_from_transport(self.transport).open_branch()
137
106
        self.assertStartsWith(str(b), 'RemoteBranch(')
138
107
 
139
 
    def test_remote_bzrdir_repr(self):
140
 
        b = BzrDir.open_from_transport(self.transport)
141
 
        self.assertStartsWith(str(b), 'RemoteBzrDir(')
142
 
 
143
 
    def test_remote_branch_format_supports_stacking(self):
144
 
        t = self.transport
145
 
        self.make_branch('unstackable', format='pack-0.92')
146
 
        b = BzrDir.open_from_transport(t.clone('unstackable')).open_branch()
147
 
        self.assertFalse(b._format.supports_stacking())
148
 
        self.make_branch('stackable', format='1.9')
149
 
        b = BzrDir.open_from_transport(t.clone('stackable')).open_branch()
150
 
        self.assertTrue(b._format.supports_stacking())
151
 
 
152
 
    def test_remote_repo_format_supports_external_references(self):
153
 
        t = self.transport
154
 
        bd = self.make_bzrdir('unstackable', format='pack-0.92')
155
 
        r = bd.create_repository()
156
 
        self.assertFalse(r._format.supports_external_lookups)
157
 
        r = BzrDir.open_from_transport(t.clone('unstackable')).open_repository()
158
 
        self.assertFalse(r._format.supports_external_lookups)
159
 
        bd = self.make_bzrdir('stackable', format='1.9')
160
 
        r = bd.create_repository()
161
 
        self.assertTrue(r._format.supports_external_lookups)
162
 
        r = BzrDir.open_from_transport(t.clone('stackable')).open_repository()
163
 
        self.assertTrue(r._format.supports_external_lookups)
164
 
 
165
 
    def test_remote_branch_set_append_revisions_only(self):
166
 
        # Make a format 1.9 branch, which supports append_revisions_only
167
 
        branch = self.make_branch('branch', format='1.9')
168
 
        config = branch.get_config()
169
 
        branch.set_append_revisions_only(True)
170
 
        self.assertEqual(
171
 
            'True', config.get_user_option('append_revisions_only'))
172
 
        branch.set_append_revisions_only(False)
173
 
        self.assertEqual(
174
 
            'False', config.get_user_option('append_revisions_only'))
175
 
 
176
 
    def test_remote_branch_set_append_revisions_only_upgrade_reqd(self):
177
 
        branch = self.make_branch('branch', format='knit')
178
 
        config = branch.get_config()
179
 
        self.assertRaises(
180
 
            errors.UpgradeRequired, branch.set_append_revisions_only, True)
181
 
 
182
108
 
183
109
class FakeProtocol(object):
184
110
    """Lookalike SmartClientRequestProtocolOne allowing body reading tests."""
205
131
 
206
132
class FakeClient(_SmartClient):
207
133
    """Lookalike for _SmartClient allowing testing."""
208
 
 
 
134
    
209
135
    def __init__(self, fake_medium_base='fake base'):
210
 
        """Create a FakeClient."""
 
136
        """Create a FakeClient.
 
137
 
 
138
        :param responses: A list of response-tuple, body-data pairs to be sent
 
139
            back to callers.  A special case is if the response-tuple is
 
140
            'unknown verb', then a UnknownSmartMethod will be raised for that
 
141
            call, using the second element of the tuple as the verb in the
 
142
            exception.
 
143
        """
211
144
        self.responses = []
212
145
        self._calls = []
213
146
        self.expecting_body = False
214
 
        # if non-None, this is the list of expected calls, with only the
215
 
        # method name and arguments included.  the body might be hard to
216
 
        # compute so is not included. If a call is None, that call can
217
 
        # be anything.
218
 
        self._expected_calls = None
219
147
        _SmartClient.__init__(self, FakeMedium(self._calls, fake_medium_base))
220
148
 
221
 
    def add_expected_call(self, call_name, call_args, response_type,
222
 
        response_args, response_body=None):
223
 
        if self._expected_calls is None:
224
 
            self._expected_calls = []
225
 
        self._expected_calls.append((call_name, call_args))
226
 
        self.responses.append((response_type, response_args, response_body))
227
 
 
228
149
    def add_success_response(self, *args):
229
150
        self.responses.append(('success', args, None))
230
151
 
231
152
    def add_success_response_with_body(self, body, *args):
232
153
        self.responses.append(('success', args, body))
233
 
        if self._expected_calls is not None:
234
 
            self._expected_calls.append(None)
235
154
 
236
155
    def add_error_response(self, *args):
237
156
        self.responses.append(('error', args))
239
158
    def add_unknown_method_response(self, verb):
240
159
        self.responses.append(('unknown', verb))
241
160
 
242
 
    def finished_test(self):
243
 
        if self._expected_calls:
244
 
            raise AssertionError("%r finished but was still expecting %r"
245
 
                % (self, self._expected_calls[0]))
246
 
 
247
161
    def _get_next_response(self):
248
 
        try:
249
 
            response_tuple = self.responses.pop(0)
250
 
        except IndexError, e:
251
 
            raise AssertionError("%r didn't expect any more calls"
252
 
                % (self,))
 
162
        response_tuple = self.responses.pop(0)
253
163
        if response_tuple[0] == 'unknown':
254
164
            raise errors.UnknownSmartMethod(response_tuple[1])
255
165
        elif response_tuple[0] == 'error':
256
166
            raise errors.ErrorFromSmartServer(response_tuple[1])
257
167
        return response_tuple
258
168
 
259
 
    def _check_call(self, method, args):
260
 
        if self._expected_calls is None:
261
 
            # the test should be updated to say what it expects
262
 
            return
263
 
        try:
264
 
            next_call = self._expected_calls.pop(0)
265
 
        except IndexError:
266
 
            raise AssertionError("%r didn't expect any more calls "
267
 
                "but got %r%r"
268
 
                % (self, method, args,))
269
 
        if next_call is None:
270
 
            return
271
 
        if method != next_call[0] or args != next_call[1]:
272
 
            raise AssertionError("%r expected %r%r "
273
 
                "but got %r%r"
274
 
                % (self, next_call[0], next_call[1], method, args,))
275
 
 
276
169
    def call(self, method, *args):
277
 
        self._check_call(method, args)
278
170
        self._calls.append(('call', method, args))
279
171
        return self._get_next_response()[1]
280
172
 
281
173
    def call_expecting_body(self, method, *args):
282
 
        self._check_call(method, args)
283
174
        self._calls.append(('call_expecting_body', method, args))
284
175
        result = self._get_next_response()
285
176
        self.expecting_body = True
286
177
        return result[1], FakeProtocol(result[2], self)
287
178
 
288
 
    def call_with_body_bytes(self, method, args, body):
289
 
        self._check_call(method, args)
290
 
        self._calls.append(('call_with_body_bytes', method, args, body))
291
 
        result = self._get_next_response()
292
 
        return result[1], FakeProtocol(result[2], self)
293
 
 
294
179
    def call_with_body_bytes_expecting_body(self, method, args, body):
295
 
        self._check_call(method, args)
296
180
        self._calls.append(('call_with_body_bytes_expecting_body', method,
297
181
            args, body))
298
182
        result = self._get_next_response()
299
183
        self.expecting_body = True
300
184
        return result[1], FakeProtocol(result[2], self)
301
185
 
302
 
    def call_with_body_stream(self, args, stream):
303
 
        # Explicitly consume the stream before checking for an error, because
304
 
        # that's what happens a real medium.
305
 
        stream = list(stream)
306
 
        self._check_call(args[0], args[1:])
307
 
        self._calls.append(('call_with_body_stream', args[0], args[1:], stream))
308
 
        result = self._get_next_response()
309
 
        # The second value returned from call_with_body_stream is supposed to
310
 
        # be a response_handler object, but so far no tests depend on that.
311
 
        response_handler = None 
312
 
        return result[1], response_handler
313
 
 
314
186
 
315
187
class FakeMedium(medium.SmartClientMedium):
316
188
 
336
208
        self.assertTrue(result)
337
209
 
338
210
 
339
 
class TestRemote(tests.TestCaseWithMemoryTransport):
340
 
 
341
 
    def get_branch_format(self):
342
 
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
343
 
        return reference_bzrdir_format.get_branch_format()
344
 
 
345
 
    def get_repo_format(self):
346
 
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
347
 
        return reference_bzrdir_format.repository_format
348
 
 
349
 
    def assertFinished(self, fake_client):
350
 
        """Assert that all of a FakeClient's expected calls have occurred."""
351
 
        fake_client.finished_test()
352
 
 
353
 
 
354
211
class Test_ClientMedium_remote_path_from_transport(tests.TestCase):
355
212
    """Tests for the behaviour of client_medium.remote_path_from_transport."""
356
213
 
360
217
        a given client_base and transport_base.
361
218
        """
362
219
        client_medium = medium.SmartClientMedium(client_base)
363
 
        t = transport.get_transport(transport_base)
364
 
        result = client_medium.remote_path_from_transport(t)
 
220
        transport = get_transport(transport_base)
 
221
        result = client_medium.remote_path_from_transport(transport)
365
222
        self.assertEqual(expected, result)
366
223
 
367
224
    def test_remote_path_from_transport(self):
378
235
        a given transport_base and relpath of that transport.  (Note that
379
236
        HttpTransportBase is a subclass of SmartClientMedium)
380
237
        """
381
 
        base_transport = transport.get_transport(transport_base)
 
238
        base_transport = get_transport(transport_base)
382
239
        client_medium = base_transport.get_smart_medium()
383
240
        cloned_transport = base_transport.clone(relpath)
384
241
        result = client_medium.remote_path_from_transport(cloned_transport)
385
242
        self.assertEqual(expected, result)
386
 
 
 
243
        
387
244
    def test_remote_path_from_transport_http(self):
388
245
        """Remote paths for HTTP transports are calculated differently to other
389
246
        transports.  They are just relative to the client base, not the root
405
262
        """
406
263
        client_medium = medium.SmartClientMedium('dummy base')
407
264
        self.assertFalse(client_medium._is_remote_before((99, 99)))
408
 
 
 
265
    
409
266
    def test__remember_remote_is_before(self):
410
267
        """Calling _remember_remote_is_before ratchets down the known remote
411
268
        version.
419
276
        # Calling _remember_remote_is_before again with a lower value works.
420
277
        client_medium._remember_remote_is_before((1, 5))
421
278
        self.assertTrue(client_medium._is_remote_before((1, 5)))
422
 
        # If you call _remember_remote_is_before with a higher value it logs a
423
 
        # warning, and continues to remember the lower value.
424
 
        self.assertNotContainsRe(self.get_log(), '_remember_remote_is_before')
425
 
        client_medium._remember_remote_is_before((1, 9))
426
 
        self.assertContainsRe(self.get_log(), '_remember_remote_is_before')
427
 
        self.assertTrue(client_medium._is_remote_before((1, 5)))
428
 
 
429
 
 
430
 
class TestBzrDirCloningMetaDir(TestRemote):
431
 
 
432
 
    def test_backwards_compat(self):
433
 
        self.setup_smart_server_with_call_log()
434
 
        a_dir = self.make_bzrdir('.')
435
 
        self.reset_smart_call_log()
436
 
        verb = 'BzrDir.cloning_metadir'
437
 
        self.disable_verb(verb)
438
 
        format = a_dir.cloning_metadir()
439
 
        call_count = len([call for call in self.hpss_calls if
440
 
            call.call.method == verb])
441
 
        self.assertEqual(1, call_count)
442
 
 
443
 
    def test_branch_reference(self):
444
 
        transport = self.get_transport('quack')
445
 
        referenced = self.make_branch('referenced')
446
 
        expected = referenced.bzrdir.cloning_metadir()
447
 
        client = FakeClient(transport.base)
448
 
        client.add_expected_call(
449
 
            'BzrDir.cloning_metadir', ('quack/', 'False'),
450
 
            'error', ('BranchReference',)),
451
 
        client.add_expected_call(
452
 
            'BzrDir.open_branchV3', ('quack/',),
453
 
            'success', ('ref', self.get_url('referenced'))),
454
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
455
 
            _client=client)
456
 
        result = a_bzrdir.cloning_metadir()
457
 
        # We should have got a control dir matching the referenced branch.
458
 
        self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
459
 
        self.assertEqual(expected._repository_format, result._repository_format)
460
 
        self.assertEqual(expected._branch_format, result._branch_format)
461
 
        self.assertFinished(client)
462
 
 
463
 
    def test_current_server(self):
464
 
        transport = self.get_transport('.')
465
 
        transport = transport.clone('quack')
466
 
        self.make_bzrdir('quack')
467
 
        client = FakeClient(transport.base)
468
 
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
469
 
        control_name = reference_bzrdir_format.network_name()
470
 
        client.add_expected_call(
471
 
            'BzrDir.cloning_metadir', ('quack/', 'False'),
472
 
            'success', (control_name, '', ('branch', ''))),
473
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
474
 
            _client=client)
475
 
        result = a_bzrdir.cloning_metadir()
476
 
        # We should have got a reference control dir with default branch and
477
 
        # repository formats.
478
 
        # This pokes a little, just to be sure.
479
 
        self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
480
 
        self.assertEqual(None, result._repository_format)
481
 
        self.assertEqual(None, result._branch_format)
482
 
        self.assertFinished(client)
483
 
 
484
 
 
485
 
class TestBzrDirOpen(TestRemote):
486
 
 
487
 
    def make_fake_client_and_transport(self, path='quack'):
488
 
        transport = MemoryTransport()
489
 
        transport.mkdir(path)
490
 
        transport = transport.clone(path)
491
 
        client = FakeClient(transport.base)
492
 
        return client, transport
493
 
 
494
 
    def test_absent(self):
495
 
        client, transport = self.make_fake_client_and_transport()
496
 
        client.add_expected_call(
497
 
            'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
498
 
        self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
499
 
                RemoteBzrDirFormat(), _client=client, _force_probe=True)
500
 
        self.assertFinished(client)
501
 
 
502
 
    def test_present_without_workingtree(self):
503
 
        client, transport = self.make_fake_client_and_transport()
504
 
        client.add_expected_call(
505
 
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
506
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
507
 
            _client=client, _force_probe=True)
508
 
        self.assertIsInstance(bd, RemoteBzrDir)
509
 
        self.assertFalse(bd.has_workingtree())
510
 
        self.assertRaises(errors.NoWorkingTree, bd.open_workingtree)
511
 
        self.assertFinished(client)
512
 
 
513
 
    def test_present_with_workingtree(self):
514
 
        client, transport = self.make_fake_client_and_transport()
515
 
        client.add_expected_call(
516
 
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
517
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
518
 
            _client=client, _force_probe=True)
519
 
        self.assertIsInstance(bd, RemoteBzrDir)
520
 
        self.assertTrue(bd.has_workingtree())
521
 
        self.assertRaises(errors.NotLocalUrl, bd.open_workingtree)
522
 
        self.assertFinished(client)
523
 
 
524
 
    def test_backwards_compat(self):
525
 
        client, transport = self.make_fake_client_and_transport()
526
 
        client.add_expected_call(
527
 
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
528
 
        client.add_expected_call(
529
 
            'BzrDir.open', ('quack/',), 'success', ('yes',))
530
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
531
 
            _client=client, _force_probe=True)
532
 
        self.assertIsInstance(bd, RemoteBzrDir)
533
 
        self.assertFinished(client)
534
 
 
535
 
    def test_backwards_compat_hpss_v2(self):
536
 
        client, transport = self.make_fake_client_and_transport()
537
 
        # Monkey-patch fake client to simulate real-world behaviour with v2
538
 
        # server: upon first RPC call detect the protocol version, and because
539
 
        # the version is 2 also do _remember_remote_is_before((1, 6)) before
540
 
        # continuing with the RPC.
541
 
        orig_check_call = client._check_call
542
 
        def check_call(method, args):
543
 
            client._medium._protocol_version = 2
544
 
            client._medium._remember_remote_is_before((1, 6))
545
 
            client._check_call = orig_check_call
546
 
            client._check_call(method, args)
547
 
        client._check_call = check_call
548
 
        client.add_expected_call(
549
 
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
550
 
        client.add_expected_call(
551
 
            'BzrDir.open', ('quack/',), 'success', ('yes',))
552
 
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
553
 
            _client=client, _force_probe=True)
554
 
        self.assertIsInstance(bd, RemoteBzrDir)
555
 
        self.assertFinished(client)
556
 
 
557
 
 
558
 
class TestBzrDirOpenBranch(TestRemote):
559
 
 
560
 
    def test_backwards_compat(self):
561
 
        self.setup_smart_server_with_call_log()
562
 
        self.make_branch('.')
563
 
        a_dir = BzrDir.open(self.get_url('.'))
564
 
        self.reset_smart_call_log()
565
 
        verb = 'BzrDir.open_branchV3'
566
 
        self.disable_verb(verb)
567
 
        format = a_dir.open_branch()
568
 
        call_count = len([call for call in self.hpss_calls if
569
 
            call.call.method == verb])
570
 
        self.assertEqual(1, call_count)
 
279
        # You cannot call _remember_remote_is_before with a larger value.
 
280
        self.assertRaises(
 
281
            AssertionError, client_medium._remember_remote_is_before, (1, 9))
 
282
 
 
283
 
 
284
class TestBzrDirOpenBranch(tests.TestCase):
571
285
 
572
286
    def test_branch_present(self):
573
 
        reference_format = self.get_repo_format()
574
 
        network_name = reference_format.network_name()
575
 
        branch_network_name = self.get_branch_format().network_name()
576
287
        transport = MemoryTransport()
577
288
        transport.mkdir('quack')
578
289
        transport = transport.clone('quack')
579
290
        client = FakeClient(transport.base)
580
 
        client.add_expected_call(
581
 
            'BzrDir.open_branchV3', ('quack/',),
582
 
            'success', ('branch', branch_network_name))
583
 
        client.add_expected_call(
584
 
            'BzrDir.find_repositoryV3', ('quack/',),
585
 
            'success', ('ok', '', 'no', 'no', 'no', network_name))
586
 
        client.add_expected_call(
587
 
            'Branch.get_stacked_on_url', ('quack/',),
588
 
            'error', ('NotStacked',))
589
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
590
 
            _client=client)
 
291
        client.add_success_response('ok', '')
 
292
        client.add_success_response('ok', '', 'no', 'no', 'no')
 
293
        bzrdir = RemoteBzrDir(transport, _client=client)
591
294
        result = bzrdir.open_branch()
 
295
        self.assertEqual(
 
296
            [('call', 'BzrDir.open_branch', ('quack/',)),
 
297
             ('call', 'BzrDir.find_repositoryV2', ('quack/',))],
 
298
            client._calls)
592
299
        self.assertIsInstance(result, RemoteBranch)
593
300
        self.assertEqual(bzrdir, result.bzrdir)
594
 
        self.assertFinished(client)
595
301
 
596
302
    def test_branch_missing(self):
597
303
        transport = MemoryTransport()
599
305
        transport = transport.clone('quack')
600
306
        client = FakeClient(transport.base)
601
307
        client.add_error_response('nobranch')
602
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
603
 
            _client=client)
 
308
        bzrdir = RemoteBzrDir(transport, _client=client)
604
309
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
605
310
        self.assertEqual(
606
 
            [('call', 'BzrDir.open_branchV3', ('quack/',))],
 
311
            [('call', 'BzrDir.open_branch', ('quack/',))],
607
312
            client._calls)
608
313
 
609
314
    def test__get_tree_branch(self):
610
315
        # _get_tree_branch is a form of open_branch, but it should only ask for
611
316
        # branch opening, not any other network requests.
612
317
        calls = []
613
 
        def open_branch(name=None):
 
318
        def open_branch():
614
319
            calls.append("Called")
615
320
            return "a-branch"
616
321
        transport = MemoryTransport()
617
322
        # no requests on the network - catches other api calls being made.
618
323
        client = FakeClient(transport.base)
619
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
620
 
            _client=client)
 
324
        bzrdir = RemoteBzrDir(transport, _client=client)
621
325
        # patch the open_branch call to record that it was called.
622
326
        bzrdir.open_branch = open_branch
623
327
        self.assertEqual((None, "a-branch"), bzrdir._get_tree_branch())
629
333
        # transmitted as "~", not "%7E".
630
334
        transport = RemoteTCPTransport('bzr://localhost/~hello/')
631
335
        client = FakeClient(transport.base)
632
 
        reference_format = self.get_repo_format()
633
 
        network_name = reference_format.network_name()
634
 
        branch_network_name = self.get_branch_format().network_name()
635
 
        client.add_expected_call(
636
 
            'BzrDir.open_branchV3', ('~hello/',),
637
 
            'success', ('branch', branch_network_name))
638
 
        client.add_expected_call(
639
 
            'BzrDir.find_repositoryV3', ('~hello/',),
640
 
            'success', ('ok', '', 'no', 'no', 'no', network_name))
641
 
        client.add_expected_call(
642
 
            'Branch.get_stacked_on_url', ('~hello/',),
643
 
            'error', ('NotStacked',))
644
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
645
 
            _client=client)
 
336
        client.add_success_response('ok', '')
 
337
        client.add_success_response('ok', '', 'no', 'no', 'no')
 
338
        bzrdir = RemoteBzrDir(transport, _client=client)
646
339
        result = bzrdir.open_branch()
647
 
        self.assertFinished(client)
 
340
        self.assertEqual(
 
341
            [('call', 'BzrDir.open_branch', ('~hello/',)),
 
342
             ('call', 'BzrDir.find_repositoryV2', ('~hello/',))],
 
343
            client._calls)
648
344
 
649
345
    def check_open_repository(self, rich_root, subtrees, external_lookup='no'):
650
 
        reference_format = self.get_repo_format()
651
 
        network_name = reference_format.network_name()
652
346
        transport = MemoryTransport()
653
347
        transport.mkdir('quack')
654
348
        transport = transport.clone('quack')
662
356
            subtree_response = 'no'
663
357
        client = FakeClient(transport.base)
664
358
        client.add_success_response(
665
 
            'ok', '', rich_response, subtree_response, external_lookup,
666
 
            network_name)
667
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
668
 
            _client=client)
 
359
            'ok', '', rich_response, subtree_response, external_lookup)
 
360
        bzrdir = RemoteBzrDir(transport, _client=client)
669
361
        result = bzrdir.open_repository()
670
362
        self.assertEqual(
671
 
            [('call', 'BzrDir.find_repositoryV3', ('quack/',))],
 
363
            [('call', 'BzrDir.find_repositoryV2', ('quack/',))],
672
364
            client._calls)
673
365
        self.assertIsInstance(result, RemoteRepository)
674
366
        self.assertEqual(bzrdir, result.bzrdir)
687
379
        old.
688
380
        """
689
381
        self.assertRaises(errors.NotBranchError,
690
 
            RemoteBzrProber.probe_transport, OldServerTransport())
691
 
 
692
 
 
693
 
class TestBzrDirCreateBranch(TestRemote):
694
 
 
695
 
    def test_backwards_compat(self):
696
 
        self.setup_smart_server_with_call_log()
697
 
        repo = self.make_repository('.')
698
 
        self.reset_smart_call_log()
699
 
        self.disable_verb('BzrDir.create_branch')
700
 
        branch = repo.bzrdir.create_branch()
701
 
        create_branch_call_count = len([call for call in self.hpss_calls if
702
 
            call.call.method == 'BzrDir.create_branch'])
703
 
        self.assertEqual(1, create_branch_call_count)
704
 
 
705
 
    def test_current_server(self):
706
 
        transport = self.get_transport('.')
707
 
        transport = transport.clone('quack')
708
 
        self.make_repository('quack')
709
 
        client = FakeClient(transport.base)
710
 
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
711
 
        reference_format = reference_bzrdir_format.get_branch_format()
712
 
        network_name = reference_format.network_name()
713
 
        reference_repo_fmt = reference_bzrdir_format.repository_format
714
 
        reference_repo_name = reference_repo_fmt.network_name()
715
 
        client.add_expected_call(
716
 
            'BzrDir.create_branch', ('quack/', network_name),
717
 
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
718
 
            reference_repo_name))
719
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
720
 
            _client=client)
721
 
        branch = a_bzrdir.create_branch()
722
 
        # We should have got a remote branch
723
 
        self.assertIsInstance(branch, remote.RemoteBranch)
724
 
        # its format should have the settings from the response
725
 
        format = branch._format
726
 
        self.assertEqual(network_name, format.network_name())
727
 
 
728
 
    def test_already_open_repo_and_reused_medium(self):
729
 
        """Bug 726584: create_branch(..., repository=repo) should work
730
 
        regardless of what the smart medium's base URL is.
731
 
        """
732
 
        self.transport_server = test_server.SmartTCPServer_for_testing
733
 
        transport = self.get_transport('.')
734
 
        repo = self.make_repository('quack')
735
 
        # Client's medium rooted a transport root (not at the bzrdir)
736
 
        client = FakeClient(transport.base)
737
 
        transport = transport.clone('quack')
738
 
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
739
 
        reference_format = reference_bzrdir_format.get_branch_format()
740
 
        network_name = reference_format.network_name()
741
 
        reference_repo_fmt = reference_bzrdir_format.repository_format
742
 
        reference_repo_name = reference_repo_fmt.network_name()
743
 
        client.add_expected_call(
744
 
            'BzrDir.create_branch', ('extra/quack/', network_name),
745
 
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
746
 
            reference_repo_name))
747
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
748
 
            _client=client)
749
 
        branch = a_bzrdir.create_branch(repository=repo)
750
 
        # We should have got a remote branch
751
 
        self.assertIsInstance(branch, remote.RemoteBranch)
752
 
        # its format should have the settings from the response
753
 
        format = branch._format
754
 
        self.assertEqual(network_name, format.network_name())
755
 
 
756
 
 
757
 
class TestBzrDirCreateRepository(TestRemote):
758
 
 
759
 
    def test_backwards_compat(self):
760
 
        self.setup_smart_server_with_call_log()
761
 
        bzrdir = self.make_bzrdir('.')
762
 
        self.reset_smart_call_log()
763
 
        self.disable_verb('BzrDir.create_repository')
764
 
        repo = bzrdir.create_repository()
765
 
        create_repo_call_count = len([call for call in self.hpss_calls if
766
 
            call.call.method == 'BzrDir.create_repository'])
767
 
        self.assertEqual(1, create_repo_call_count)
768
 
 
769
 
    def test_current_server(self):
770
 
        transport = self.get_transport('.')
771
 
        transport = transport.clone('quack')
772
 
        self.make_bzrdir('quack')
773
 
        client = FakeClient(transport.base)
774
 
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
775
 
        reference_format = reference_bzrdir_format.repository_format
776
 
        network_name = reference_format.network_name()
777
 
        client.add_expected_call(
778
 
            'BzrDir.create_repository', ('quack/',
779
 
                'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
780
 
                'False'),
781
 
            'success', ('ok', 'yes', 'yes', 'yes', network_name))
782
 
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
783
 
            _client=client)
784
 
        repo = a_bzrdir.create_repository()
785
 
        # We should have got a remote repository
786
 
        self.assertIsInstance(repo, remote.RemoteRepository)
787
 
        # its format should have the settings from the response
788
 
        format = repo._format
789
 
        self.assertTrue(format.rich_root_data)
790
 
        self.assertTrue(format.supports_tree_reference)
791
 
        self.assertTrue(format.supports_external_lookups)
792
 
        self.assertEqual(network_name, format.network_name())
793
 
 
794
 
 
795
 
class TestBzrDirOpenRepository(TestRemote):
796
 
 
797
 
    def test_backwards_compat_1_2_3(self):
798
 
        # fallback all the way to the first version.
799
 
        reference_format = self.get_repo_format()
800
 
        network_name = reference_format.network_name()
801
 
        server_url = 'bzr://example.com/'
802
 
        self.permit_url(server_url)
803
 
        client = FakeClient(server_url)
804
 
        client.add_unknown_method_response('BzrDir.find_repositoryV3')
805
 
        client.add_unknown_method_response('BzrDir.find_repositoryV2')
 
382
            RemoteBzrDirFormat.probe_transport, OldServerTransport())
 
383
 
 
384
 
 
385
class TestBzrDirOpenRepository(tests.TestCase):
 
386
 
 
387
    def test_backwards_compat_1_2(self):
 
388
        transport = MemoryTransport()
 
389
        transport.mkdir('quack')
 
390
        transport = transport.clone('quack')
 
391
        client = FakeClient(transport.base)
 
392
        client.add_unknown_method_response('RemoteRepository.find_repositoryV2')
806
393
        client.add_success_response('ok', '', 'no', 'no')
807
 
        # A real repository instance will be created to determine the network
808
 
        # name.
809
 
        client.add_success_response_with_body(
810
 
            "Bazaar-NG meta directory, format 1\n", 'ok')
811
 
        client.add_success_response_with_body(
812
 
            reference_format.get_format_string(), 'ok')
813
 
        # PackRepository wants to do a stat
814
 
        client.add_success_response('stat', '0', '65535')
815
 
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
816
 
            _client=client)
817
 
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
818
 
            _client=client)
819
 
        repo = bzrdir.open_repository()
820
 
        self.assertEqual(
821
 
            [('call', 'BzrDir.find_repositoryV3', ('quack/',)),
822
 
             ('call', 'BzrDir.find_repositoryV2', ('quack/',)),
823
 
             ('call', 'BzrDir.find_repository', ('quack/',)),
824
 
             ('call_expecting_body', 'get', ('/quack/.bzr/branch-format',)),
825
 
             ('call_expecting_body', 'get', ('/quack/.bzr/repository/format',)),
826
 
             ('call', 'stat', ('/quack/.bzr/repository',)),
827
 
             ],
828
 
            client._calls)
829
 
        self.assertEqual(network_name, repo._format.network_name())
830
 
 
831
 
    def test_backwards_compat_2(self):
832
 
        # fallback to find_repositoryV2
833
 
        reference_format = self.get_repo_format()
834
 
        network_name = reference_format.network_name()
835
 
        server_url = 'bzr://example.com/'
836
 
        self.permit_url(server_url)
837
 
        client = FakeClient(server_url)
838
 
        client.add_unknown_method_response('BzrDir.find_repositoryV3')
839
 
        client.add_success_response('ok', '', 'no', 'no', 'no')
840
 
        # A real repository instance will be created to determine the network
841
 
        # name.
842
 
        client.add_success_response_with_body(
843
 
            "Bazaar-NG meta directory, format 1\n", 'ok')
844
 
        client.add_success_response_with_body(
845
 
            reference_format.get_format_string(), 'ok')
846
 
        # PackRepository wants to do a stat
847
 
        client.add_success_response('stat', '0', '65535')
848
 
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
849
 
            _client=client)
850
 
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
851
 
            _client=client)
852
 
        repo = bzrdir.open_repository()
853
 
        self.assertEqual(
854
 
            [('call', 'BzrDir.find_repositoryV3', ('quack/',)),
855
 
             ('call', 'BzrDir.find_repositoryV2', ('quack/',)),
856
 
             ('call_expecting_body', 'get', ('/quack/.bzr/branch-format',)),
857
 
             ('call_expecting_body', 'get', ('/quack/.bzr/repository/format',)),
858
 
             ('call', 'stat', ('/quack/.bzr/repository',)),
859
 
             ],
860
 
            client._calls)
861
 
        self.assertEqual(network_name, repo._format.network_name())
862
 
 
863
 
    def test_current_server(self):
864
 
        reference_format = self.get_repo_format()
865
 
        network_name = reference_format.network_name()
866
 
        transport = MemoryTransport()
867
 
        transport.mkdir('quack')
868
 
        transport = transport.clone('quack')
869
 
        client = FakeClient(transport.base)
870
 
        client.add_success_response('ok', '', 'no', 'no', 'no', network_name)
871
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
872
 
            _client=client)
873
 
        repo = bzrdir.open_repository()
874
 
        self.assertEqual(
875
 
            [('call', 'BzrDir.find_repositoryV3', ('quack/',))],
876
 
            client._calls)
877
 
        self.assertEqual(network_name, repo._format.network_name())
878
 
 
879
 
 
880
 
class TestBzrDirFormatInitializeEx(TestRemote):
881
 
 
882
 
    def test_success(self):
883
 
        """Simple test for typical successful call."""
884
 
        fmt = RemoteBzrDirFormat()
885
 
        default_format_name = BzrDirFormat.get_default_format().network_name()
886
 
        transport = self.get_transport()
887
 
        client = FakeClient(transport.base)
888
 
        client.add_expected_call(
889
 
            'BzrDirFormat.initialize_ex_1.16',
890
 
                (default_format_name, 'path', 'False', 'False', 'False', '',
891
 
                 '', '', '', 'False'),
892
 
            'success',
893
 
                ('.', 'no', 'no', 'yes', 'repo fmt', 'repo bzrdir fmt',
894
 
                 'bzrdir fmt', 'False', '', '', 'repo lock token'))
895
 
        # XXX: It would be better to call fmt.initialize_on_transport_ex, but
896
 
        # it's currently hard to test that without supplying a real remote
897
 
        # transport connected to a real server.
898
 
        result = fmt._initialize_on_transport_ex_rpc(client, 'path',
899
 
            transport, False, False, False, None, None, None, None, False)
900
 
        self.assertFinished(client)
901
 
 
902
 
    def test_error(self):
903
 
        """Error responses are translated, e.g. 'PermissionDenied' raises the
904
 
        corresponding error from the client.
905
 
        """
906
 
        fmt = RemoteBzrDirFormat()
907
 
        default_format_name = BzrDirFormat.get_default_format().network_name()
908
 
        transport = self.get_transport()
909
 
        client = FakeClient(transport.base)
910
 
        client.add_expected_call(
911
 
            'BzrDirFormat.initialize_ex_1.16',
912
 
                (default_format_name, 'path', 'False', 'False', 'False', '',
913
 
                 '', '', '', 'False'),
914
 
            'error',
915
 
                ('PermissionDenied', 'path', 'extra info'))
916
 
        # XXX: It would be better to call fmt.initialize_on_transport_ex, but
917
 
        # it's currently hard to test that without supplying a real remote
918
 
        # transport connected to a real server.
919
 
        err = self.assertRaises(errors.PermissionDenied,
920
 
            fmt._initialize_on_transport_ex_rpc, client, 'path', transport,
921
 
            False, False, False, None, None, None, None, False)
922
 
        self.assertEqual('path', err.path)
923
 
        self.assertEqual(': extra info', err.extra)
924
 
        self.assertFinished(client)
925
 
 
926
 
    def test_error_from_real_server(self):
927
 
        """Integration test for error translation."""
928
 
        transport = self.make_smart_server('foo')
929
 
        transport = transport.clone('no-such-path')
930
 
        fmt = RemoteBzrDirFormat()
931
 
        err = self.assertRaises(errors.NoSuchFile,
932
 
            fmt.initialize_on_transport_ex, transport, create_prefix=False)
 
394
        bzrdir = RemoteBzrDir(transport, _client=client)
 
395
        repo = bzrdir.open_repository()
 
396
        self.assertEqual(
 
397
            [('call', 'BzrDir.find_repositoryV2', ('quack/',)),
 
398
             ('call', 'BzrDir.find_repository', ('quack/',))],
 
399
            client._calls)
933
400
 
934
401
 
935
402
class OldSmartClient(object):
960
427
        return OldSmartClient()
961
428
 
962
429
 
963
 
class RemoteBzrDirTestCase(TestRemote):
964
 
 
965
 
    def make_remote_bzrdir(self, transport, client):
966
 
        """Make a RemotebzrDir using 'client' as the _client."""
967
 
        return RemoteBzrDir(transport, RemoteBzrDirFormat(),
968
 
            _client=client)
969
 
 
970
 
 
971
 
class RemoteBranchTestCase(RemoteBzrDirTestCase):
972
 
 
973
 
    def lock_remote_branch(self, branch):
974
 
        """Trick a RemoteBranch into thinking it is locked."""
975
 
        branch._lock_mode = 'w'
976
 
        branch._lock_count = 2
977
 
        branch._lock_token = 'branch token'
978
 
        branch._repo_lock_token = 'repo token'
979
 
        branch.repository._lock_mode = 'w'
980
 
        branch.repository._lock_count = 2
981
 
        branch.repository._lock_token = 'repo token'
 
430
class RemoteBranchTestCase(tests.TestCase):
982
431
 
983
432
    def make_remote_branch(self, transport, client):
984
433
        """Make a RemoteBranch using 'client' as its _SmartClient.
985
 
 
 
434
        
986
435
        A RemoteBzrDir and RemoteRepository will also be created to fill out
987
436
        the RemoteBranch, albeit with stub values for some of their attributes.
988
437
        """
989
438
        # we do not want bzrdir to make any remote calls, so use False as its
990
439
        # _client.  If it tries to make a remote call, this will fail
991
440
        # immediately.
992
 
        bzrdir = self.make_remote_bzrdir(transport, False)
 
441
        bzrdir = RemoteBzrDir(transport, _client=False)
993
442
        repo = RemoteRepository(bzrdir, None, _client=client)
994
 
        branch_format = self.get_branch_format()
995
 
        format = RemoteBranchFormat(network_name=branch_format.network_name())
996
 
        return RemoteBranch(bzrdir, repo, _client=client, format=format)
997
 
 
998
 
 
999
 
class TestBranchGetParent(RemoteBranchTestCase):
1000
 
 
1001
 
    def test_no_parent(self):
1002
 
        # in an empty branch we decode the response properly
1003
 
        transport = MemoryTransport()
1004
 
        client = FakeClient(transport.base)
1005
 
        client.add_expected_call(
1006
 
            'Branch.get_stacked_on_url', ('quack/',),
1007
 
            'error', ('NotStacked',))
1008
 
        client.add_expected_call(
1009
 
            'Branch.get_parent', ('quack/',),
1010
 
            'success', ('',))
1011
 
        transport.mkdir('quack')
1012
 
        transport = transport.clone('quack')
1013
 
        branch = self.make_remote_branch(transport, client)
1014
 
        result = branch.get_parent()
1015
 
        self.assertFinished(client)
1016
 
        self.assertEqual(None, result)
1017
 
 
1018
 
    def test_parent_relative(self):
1019
 
        transport = MemoryTransport()
1020
 
        client = FakeClient(transport.base)
1021
 
        client.add_expected_call(
1022
 
            'Branch.get_stacked_on_url', ('kwaak/',),
1023
 
            'error', ('NotStacked',))
1024
 
        client.add_expected_call(
1025
 
            'Branch.get_parent', ('kwaak/',),
1026
 
            'success', ('../foo/',))
1027
 
        transport.mkdir('kwaak')
1028
 
        transport = transport.clone('kwaak')
1029
 
        branch = self.make_remote_branch(transport, client)
1030
 
        result = branch.get_parent()
1031
 
        self.assertEqual(transport.clone('../foo').base, result)
1032
 
 
1033
 
    def test_parent_absolute(self):
1034
 
        transport = MemoryTransport()
1035
 
        client = FakeClient(transport.base)
1036
 
        client.add_expected_call(
1037
 
            'Branch.get_stacked_on_url', ('kwaak/',),
1038
 
            'error', ('NotStacked',))
1039
 
        client.add_expected_call(
1040
 
            'Branch.get_parent', ('kwaak/',),
1041
 
            'success', ('http://foo/',))
1042
 
        transport.mkdir('kwaak')
1043
 
        transport = transport.clone('kwaak')
1044
 
        branch = self.make_remote_branch(transport, client)
1045
 
        result = branch.get_parent()
1046
 
        self.assertEqual('http://foo/', result)
1047
 
        self.assertFinished(client)
1048
 
 
1049
 
 
1050
 
class TestBranchSetParentLocation(RemoteBranchTestCase):
1051
 
 
1052
 
    def test_no_parent(self):
1053
 
        # We call the verb when setting parent to None
1054
 
        transport = MemoryTransport()
1055
 
        client = FakeClient(transport.base)
1056
 
        client.add_expected_call(
1057
 
            'Branch.get_stacked_on_url', ('quack/',),
1058
 
            'error', ('NotStacked',))
1059
 
        client.add_expected_call(
1060
 
            'Branch.set_parent_location', ('quack/', 'b', 'r', ''),
1061
 
            'success', ())
1062
 
        transport.mkdir('quack')
1063
 
        transport = transport.clone('quack')
1064
 
        branch = self.make_remote_branch(transport, client)
1065
 
        branch._lock_token = 'b'
1066
 
        branch._repo_lock_token = 'r'
1067
 
        branch._set_parent_location(None)
1068
 
        self.assertFinished(client)
1069
 
 
1070
 
    def test_parent(self):
1071
 
        transport = MemoryTransport()
1072
 
        client = FakeClient(transport.base)
1073
 
        client.add_expected_call(
1074
 
            'Branch.get_stacked_on_url', ('kwaak/',),
1075
 
            'error', ('NotStacked',))
1076
 
        client.add_expected_call(
1077
 
            'Branch.set_parent_location', ('kwaak/', 'b', 'r', 'foo'),
1078
 
            'success', ())
1079
 
        transport.mkdir('kwaak')
1080
 
        transport = transport.clone('kwaak')
1081
 
        branch = self.make_remote_branch(transport, client)
1082
 
        branch._lock_token = 'b'
1083
 
        branch._repo_lock_token = 'r'
1084
 
        branch._set_parent_location('foo')
1085
 
        self.assertFinished(client)
1086
 
 
1087
 
    def test_backwards_compat(self):
1088
 
        self.setup_smart_server_with_call_log()
1089
 
        branch = self.make_branch('.')
1090
 
        self.reset_smart_call_log()
1091
 
        verb = 'Branch.set_parent_location'
1092
 
        self.disable_verb(verb)
1093
 
        branch.set_parent('http://foo/')
1094
 
        self.assertLength(12, self.hpss_calls)
1095
 
 
1096
 
 
1097
 
class TestBranchGetTagsBytes(RemoteBranchTestCase):
1098
 
 
1099
 
    def test_backwards_compat(self):
1100
 
        self.setup_smart_server_with_call_log()
1101
 
        branch = self.make_branch('.')
1102
 
        self.reset_smart_call_log()
1103
 
        verb = 'Branch.get_tags_bytes'
1104
 
        self.disable_verb(verb)
1105
 
        branch.tags.get_tag_dict()
1106
 
        call_count = len([call for call in self.hpss_calls if
1107
 
            call.call.method == verb])
1108
 
        self.assertEqual(1, call_count)
1109
 
 
1110
 
    def test_trivial(self):
1111
 
        transport = MemoryTransport()
1112
 
        client = FakeClient(transport.base)
1113
 
        client.add_expected_call(
1114
 
            'Branch.get_stacked_on_url', ('quack/',),
1115
 
            'error', ('NotStacked',))
1116
 
        client.add_expected_call(
1117
 
            'Branch.get_tags_bytes', ('quack/',),
1118
 
            'success', ('',))
1119
 
        transport.mkdir('quack')
1120
 
        transport = transport.clone('quack')
1121
 
        branch = self.make_remote_branch(transport, client)
1122
 
        result = branch.tags.get_tag_dict()
1123
 
        self.assertFinished(client)
1124
 
        self.assertEqual({}, result)
1125
 
 
1126
 
 
1127
 
class TestBranchSetTagsBytes(RemoteBranchTestCase):
1128
 
 
1129
 
    def test_trivial(self):
1130
 
        transport = MemoryTransport()
1131
 
        client = FakeClient(transport.base)
1132
 
        client.add_expected_call(
1133
 
            'Branch.get_stacked_on_url', ('quack/',),
1134
 
            'error', ('NotStacked',))
1135
 
        client.add_expected_call(
1136
 
            'Branch.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
1137
 
            'success', ('',))
1138
 
        transport.mkdir('quack')
1139
 
        transport = transport.clone('quack')
1140
 
        branch = self.make_remote_branch(transport, client)
1141
 
        self.lock_remote_branch(branch)
1142
 
        branch._set_tags_bytes('tags bytes')
1143
 
        self.assertFinished(client)
1144
 
        self.assertEqual('tags bytes', client._calls[-1][-1])
1145
 
 
1146
 
    def test_backwards_compatible(self):
1147
 
        transport = MemoryTransport()
1148
 
        client = FakeClient(transport.base)
1149
 
        client.add_expected_call(
1150
 
            'Branch.get_stacked_on_url', ('quack/',),
1151
 
            'error', ('NotStacked',))
1152
 
        client.add_expected_call(
1153
 
            'Branch.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
1154
 
            'unknown', ('Branch.set_tags_bytes',))
1155
 
        transport.mkdir('quack')
1156
 
        transport = transport.clone('quack')
1157
 
        branch = self.make_remote_branch(transport, client)
1158
 
        self.lock_remote_branch(branch)
1159
 
        class StubRealBranch(object):
1160
 
            def __init__(self):
1161
 
                self.calls = []
1162
 
            def _set_tags_bytes(self, bytes):
1163
 
                self.calls.append(('set_tags_bytes', bytes))
1164
 
        real_branch = StubRealBranch()
1165
 
        branch._real_branch = real_branch
1166
 
        branch._set_tags_bytes('tags bytes')
1167
 
        # Call a second time, to exercise the 'remote version already inferred'
1168
 
        # code path.
1169
 
        branch._set_tags_bytes('tags bytes')
1170
 
        self.assertFinished(client)
1171
 
        self.assertEqual(
1172
 
            [('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
1173
 
 
1174
 
 
1175
 
class TestBranchHeadsToFetch(RemoteBranchTestCase):
1176
 
 
1177
 
    def test_uses_last_revision_info_and_tags_by_default(self):
1178
 
        transport = MemoryTransport()
1179
 
        client = FakeClient(transport.base)
1180
 
        client.add_expected_call(
1181
 
            'Branch.get_stacked_on_url', ('quack/',),
1182
 
            'error', ('NotStacked',))
1183
 
        client.add_expected_call(
1184
 
            'Branch.last_revision_info', ('quack/',),
1185
 
            'success', ('ok', '1', 'rev-tip'))
1186
 
        client.add_expected_call(
1187
 
            'Branch.get_config_file', ('quack/',),
1188
 
            'success', ('ok',), '')
1189
 
        transport.mkdir('quack')
1190
 
        transport = transport.clone('quack')
1191
 
        branch = self.make_remote_branch(transport, client)
1192
 
        result = branch.heads_to_fetch()
1193
 
        self.assertFinished(client)
1194
 
        self.assertEqual((set(['rev-tip']), set()), result)
1195
 
 
1196
 
    def test_uses_last_revision_info_and_tags_when_set(self):
1197
 
        transport = MemoryTransport()
1198
 
        client = FakeClient(transport.base)
1199
 
        client.add_expected_call(
1200
 
            'Branch.get_stacked_on_url', ('quack/',),
1201
 
            'error', ('NotStacked',))
1202
 
        client.add_expected_call(
1203
 
            'Branch.last_revision_info', ('quack/',),
1204
 
            'success', ('ok', '1', 'rev-tip'))
1205
 
        client.add_expected_call(
1206
 
            'Branch.get_config_file', ('quack/',),
1207
 
            'success', ('ok',), 'branch.fetch_tags = True')
1208
 
        # XXX: this will break if the default format's serialization of tags
1209
 
        # changes, or if the RPC for fetching tags changes from get_tags_bytes.
1210
 
        client.add_expected_call(
1211
 
            'Branch.get_tags_bytes', ('quack/',),
1212
 
            'success', ('d5:tag-17:rev-foo5:tag-27:rev-bare',))
1213
 
        transport.mkdir('quack')
1214
 
        transport = transport.clone('quack')
1215
 
        branch = self.make_remote_branch(transport, client)
1216
 
        result = branch.heads_to_fetch()
1217
 
        self.assertFinished(client)
1218
 
        self.assertEqual(
1219
 
            (set(['rev-tip']), set(['rev-foo', 'rev-bar'])), result)
1220
 
 
1221
 
    def test_uses_rpc_for_formats_with_non_default_heads_to_fetch(self):
1222
 
        transport = MemoryTransport()
1223
 
        client = FakeClient(transport.base)
1224
 
        client.add_expected_call(
1225
 
            'Branch.get_stacked_on_url', ('quack/',),
1226
 
            'error', ('NotStacked',))
1227
 
        client.add_expected_call(
1228
 
            'Branch.heads_to_fetch', ('quack/',),
1229
 
            'success', (['tip'], ['tagged-1', 'tagged-2']))
1230
 
        transport.mkdir('quack')
1231
 
        transport = transport.clone('quack')
1232
 
        branch = self.make_remote_branch(transport, client)
1233
 
        branch._format._use_default_local_heads_to_fetch = lambda: False
1234
 
        result = branch.heads_to_fetch()
1235
 
        self.assertFinished(client)
1236
 
        self.assertEqual((set(['tip']), set(['tagged-1', 'tagged-2'])), result)
1237
 
 
1238
 
    def make_branch_with_tags(self):
1239
 
        self.setup_smart_server_with_call_log()
1240
 
        # Make a branch with a single revision.
1241
 
        builder = self.make_branch_builder('foo')
1242
 
        builder.start_series()
1243
 
        builder.build_snapshot('tip', None, [
1244
 
            ('add', ('', 'root-id', 'directory', ''))])
1245
 
        builder.finish_series()
1246
 
        branch = builder.get_branch()
1247
 
        # Add two tags to that branch
1248
 
        branch.tags.set_tag('tag-1', 'rev-1')
1249
 
        branch.tags.set_tag('tag-2', 'rev-2')
1250
 
        return branch
1251
 
 
1252
 
    def test_backwards_compatible(self):
1253
 
        branch = self.make_branch_with_tags()
1254
 
        c = branch.get_config()
1255
 
        c.set_user_option('branch.fetch_tags', 'True')
1256
 
        self.addCleanup(branch.lock_read().unlock)
1257
 
        # Disable the heads_to_fetch verb
1258
 
        verb = 'Branch.heads_to_fetch'
1259
 
        self.disable_verb(verb)
1260
 
        self.reset_smart_call_log()
1261
 
        result = branch.heads_to_fetch()
1262
 
        self.assertEqual((set(['tip']), set(['rev-1', 'rev-2'])), result)
1263
 
        self.assertEqual(
1264
 
            ['Branch.last_revision_info', 'Branch.get_config_file',
1265
 
             'Branch.get_tags_bytes'],
1266
 
            [call.call.method for call in self.hpss_calls])
1267
 
 
1268
 
    def test_backwards_compatible_no_tags(self):
1269
 
        branch = self.make_branch_with_tags()
1270
 
        c = branch.get_config()
1271
 
        c.set_user_option('branch.fetch_tags', 'False')
1272
 
        self.addCleanup(branch.lock_read().unlock)
1273
 
        # Disable the heads_to_fetch verb
1274
 
        verb = 'Branch.heads_to_fetch'
1275
 
        self.disable_verb(verb)
1276
 
        self.reset_smart_call_log()
1277
 
        result = branch.heads_to_fetch()
1278
 
        self.assertEqual((set(['tip']), set()), result)
1279
 
        self.assertEqual(
1280
 
            ['Branch.last_revision_info', 'Branch.get_config_file'],
1281
 
            [call.call.method for call in self.hpss_calls])
 
443
        return RemoteBranch(bzrdir, repo, _client=client)
1282
444
 
1283
445
 
1284
446
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
1287
449
        # in an empty branch we decode the response properly
1288
450
        transport = MemoryTransport()
1289
451
        client = FakeClient(transport.base)
1290
 
        client.add_expected_call(
1291
 
            'Branch.get_stacked_on_url', ('quack/',),
1292
 
            'error', ('NotStacked',))
1293
 
        client.add_expected_call(
1294
 
            'Branch.last_revision_info', ('quack/',),
1295
 
            'success', ('ok', '0', 'null:'))
 
452
        client.add_success_response('ok', '0', 'null:')
1296
453
        transport.mkdir('quack')
1297
454
        transport = transport.clone('quack')
1298
455
        branch = self.make_remote_branch(transport, client)
1299
456
        result = branch.last_revision_info()
1300
 
        self.assertFinished(client)
 
457
 
 
458
        self.assertEqual(
 
459
            [('call', 'Branch.last_revision_info', ('quack/',))],
 
460
            client._calls)
1301
461
        self.assertEqual((0, NULL_REVISION), result)
1302
462
 
1303
463
    def test_non_empty_branch(self):
1305
465
        revid = u'\xc8'.encode('utf8')
1306
466
        transport = MemoryTransport()
1307
467
        client = FakeClient(transport.base)
1308
 
        client.add_expected_call(
1309
 
            'Branch.get_stacked_on_url', ('kwaak/',),
1310
 
            'error', ('NotStacked',))
1311
 
        client.add_expected_call(
1312
 
            'Branch.last_revision_info', ('kwaak/',),
1313
 
            'success', ('ok', '2', revid))
 
468
        client.add_success_response('ok', '2', revid)
1314
469
        transport.mkdir('kwaak')
1315
470
        transport = transport.clone('kwaak')
1316
471
        branch = self.make_remote_branch(transport, client)
1317
472
        result = branch.last_revision_info()
 
473
 
 
474
        self.assertEqual(
 
475
            [('call', 'Branch.last_revision_info', ('kwaak/',))],
 
476
            client._calls)
1318
477
        self.assertEqual((2, revid), result)
1319
478
 
1320
479
 
1321
 
class TestBranch_get_stacked_on_url(TestRemote):
1322
 
    """Test Branch._get_stacked_on_url rpc"""
1323
 
 
1324
 
    def test_get_stacked_on_invalid_url(self):
1325
 
        # test that asking for a stacked on url the server can't access works.
1326
 
        # This isn't perfect, but then as we're in the same process there
1327
 
        # really isn't anything we can do to be 100% sure that the server
1328
 
        # doesn't just open in - this test probably needs to be rewritten using
1329
 
        # a spawn()ed server.
1330
 
        stacked_branch = self.make_branch('stacked', format='1.9')
1331
 
        memory_branch = self.make_branch('base', format='1.9')
1332
 
        vfs_url = self.get_vfs_only_url('base')
1333
 
        stacked_branch.set_stacked_on_url(vfs_url)
1334
 
        transport = stacked_branch.bzrdir.root_transport
1335
 
        client = FakeClient(transport.base)
1336
 
        client.add_expected_call(
1337
 
            'Branch.get_stacked_on_url', ('stacked/',),
1338
 
            'success', ('ok', vfs_url))
1339
 
        # XXX: Multiple calls are bad, this second call documents what is
1340
 
        # today.
1341
 
        client.add_expected_call(
1342
 
            'Branch.get_stacked_on_url', ('stacked/',),
1343
 
            'success', ('ok', vfs_url))
1344
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
1345
 
            _client=client)
1346
 
        repo_fmt = remote.RemoteRepositoryFormat()
1347
 
        repo_fmt._custom_format = stacked_branch.repository._format
1348
 
        branch = RemoteBranch(bzrdir, RemoteRepository(bzrdir, repo_fmt),
1349
 
            _client=client)
1350
 
        result = branch.get_stacked_on_url()
1351
 
        self.assertEqual(vfs_url, result)
1352
 
 
1353
 
    def test_backwards_compatible(self):
1354
 
        # like with bzr1.6 with no Branch.get_stacked_on_url rpc
1355
 
        base_branch = self.make_branch('base', format='1.6')
1356
 
        stacked_branch = self.make_branch('stacked', format='1.6')
1357
 
        stacked_branch.set_stacked_on_url('../base')
1358
 
        client = FakeClient(self.get_url())
1359
 
        branch_network_name = self.get_branch_format().network_name()
1360
 
        client.add_expected_call(
1361
 
            'BzrDir.open_branchV3', ('stacked/',),
1362
 
            'success', ('branch', branch_network_name))
1363
 
        client.add_expected_call(
1364
 
            'BzrDir.find_repositoryV3', ('stacked/',),
1365
 
            'success', ('ok', '', 'no', 'no', 'yes',
1366
 
                stacked_branch.repository._format.network_name()))
1367
 
        # called twice, once from constructor and then again by us
1368
 
        client.add_expected_call(
1369
 
            'Branch.get_stacked_on_url', ('stacked/',),
1370
 
            'unknown', ('Branch.get_stacked_on_url',))
1371
 
        client.add_expected_call(
1372
 
            'Branch.get_stacked_on_url', ('stacked/',),
1373
 
            'unknown', ('Branch.get_stacked_on_url',))
1374
 
        # this will also do vfs access, but that goes direct to the transport
1375
 
        # and isn't seen by the FakeClient.
1376
 
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1377
 
            RemoteBzrDirFormat(), _client=client)
1378
 
        branch = bzrdir.open_branch()
1379
 
        result = branch.get_stacked_on_url()
1380
 
        self.assertEqual('../base', result)
1381
 
        self.assertFinished(client)
1382
 
        # it's in the fallback list both for the RemoteRepository and its vfs
1383
 
        # repository
1384
 
        self.assertEqual(1, len(branch.repository._fallback_repositories))
1385
 
        self.assertEqual(1,
1386
 
            len(branch.repository._real_repository._fallback_repositories))
1387
 
 
1388
 
    def test_get_stacked_on_real_branch(self):
1389
 
        base_branch = self.make_branch('base')
1390
 
        stacked_branch = self.make_branch('stacked')
1391
 
        stacked_branch.set_stacked_on_url('../base')
1392
 
        reference_format = self.get_repo_format()
1393
 
        network_name = reference_format.network_name()
1394
 
        client = FakeClient(self.get_url())
1395
 
        branch_network_name = self.get_branch_format().network_name()
1396
 
        client.add_expected_call(
1397
 
            'BzrDir.open_branchV3', ('stacked/',),
1398
 
            'success', ('branch', branch_network_name))
1399
 
        client.add_expected_call(
1400
 
            'BzrDir.find_repositoryV3', ('stacked/',),
1401
 
            'success', ('ok', '', 'yes', 'no', 'yes', network_name))
1402
 
        # called twice, once from constructor and then again by us
1403
 
        client.add_expected_call(
1404
 
            'Branch.get_stacked_on_url', ('stacked/',),
1405
 
            'success', ('ok', '../base'))
1406
 
        client.add_expected_call(
1407
 
            'Branch.get_stacked_on_url', ('stacked/',),
1408
 
            'success', ('ok', '../base'))
1409
 
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
1410
 
            RemoteBzrDirFormat(), _client=client)
1411
 
        branch = bzrdir.open_branch()
1412
 
        result = branch.get_stacked_on_url()
1413
 
        self.assertEqual('../base', result)
1414
 
        self.assertFinished(client)
1415
 
        # it's in the fallback list both for the RemoteRepository.
1416
 
        self.assertEqual(1, len(branch.repository._fallback_repositories))
1417
 
        # And we haven't had to construct a real repository.
1418
 
        self.assertEqual(None, branch.repository._real_repository)
1419
 
 
1420
 
 
1421
480
class TestBranchSetLastRevision(RemoteBranchTestCase):
1422
481
 
1423
482
    def test_set_empty(self):
1424
 
        # _set_last_revision_info('null:') is translated to calling
 
483
        # set_revision_history([]) is translated to calling
1425
484
        # Branch.set_last_revision(path, '') on the wire.
1426
485
        transport = MemoryTransport()
1427
486
        transport.mkdir('branch')
1428
487
        transport = transport.clone('branch')
1429
488
 
1430
489
        client = FakeClient(transport.base)
1431
 
        client.add_expected_call(
1432
 
            'Branch.get_stacked_on_url', ('branch/',),
1433
 
            'error', ('NotStacked',))
1434
 
        client.add_expected_call(
1435
 
            'Branch.lock_write', ('branch/', '', ''),
1436
 
            'success', ('ok', 'branch token', 'repo token'))
1437
 
        client.add_expected_call(
1438
 
            'Branch.last_revision_info',
1439
 
            ('branch/',),
1440
 
            'success', ('ok', '0', 'null:'))
1441
 
        client.add_expected_call(
1442
 
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'null:',),
1443
 
            'success', ('ok',))
1444
 
        client.add_expected_call(
1445
 
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
1446
 
            'success', ('ok',))
 
490
        # lock_write
 
491
        client.add_success_response('ok', 'branch token', 'repo token')
 
492
        # set_last_revision
 
493
        client.add_success_response('ok')
 
494
        # unlock
 
495
        client.add_success_response('ok')
1447
496
        branch = self.make_remote_branch(transport, client)
1448
 
        # This is a hack to work around the problem that RemoteBranch currently
1449
 
        # unnecessarily invokes _ensure_real upon a call to lock_write.
1450
 
        branch._ensure_real = lambda: None
1451
497
        branch.lock_write()
1452
 
        result = branch._set_last_revision(NULL_REVISION)
 
498
        client._calls = []
 
499
        result = branch.set_revision_history([])
 
500
        self.assertEqual(
 
501
            [('call', 'Branch.set_last_revision',
 
502
                ('branch/', 'branch token', 'repo token', 'null:'))],
 
503
            client._calls)
1453
504
        branch.unlock()
1454
505
        self.assertEqual(None, result)
1455
 
        self.assertFinished(client)
1456
506
 
1457
507
    def test_set_nonempty(self):
1458
 
        # set_last_revision_info(N, rev-idN) is translated to calling
 
508
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
1459
509
        # Branch.set_last_revision(path, rev-idN) on the wire.
1460
510
        transport = MemoryTransport()
1461
511
        transport.mkdir('branch')
1462
512
        transport = transport.clone('branch')
1463
513
 
1464
514
        client = FakeClient(transport.base)
1465
 
        client.add_expected_call(
1466
 
            'Branch.get_stacked_on_url', ('branch/',),
1467
 
            'error', ('NotStacked',))
1468
 
        client.add_expected_call(
1469
 
            'Branch.lock_write', ('branch/', '', ''),
1470
 
            'success', ('ok', 'branch token', 'repo token'))
1471
 
        client.add_expected_call(
1472
 
            'Branch.last_revision_info',
1473
 
            ('branch/',),
1474
 
            'success', ('ok', '0', 'null:'))
1475
 
        lines = ['rev-id2']
1476
 
        encoded_body = bz2.compress('\n'.join(lines))
1477
 
        client.add_success_response_with_body(encoded_body, 'ok')
1478
 
        client.add_expected_call(
1479
 
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id2',),
1480
 
            'success', ('ok',))
1481
 
        client.add_expected_call(
1482
 
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
1483
 
            'success', ('ok',))
 
515
        # lock_write
 
516
        client.add_success_response('ok', 'branch token', 'repo token')
 
517
        # set_last_revision
 
518
        client.add_success_response('ok')
 
519
        # unlock
 
520
        client.add_success_response('ok')
1484
521
        branch = self.make_remote_branch(transport, client)
1485
 
        # This is a hack to work around the problem that RemoteBranch currently
1486
 
        # unnecessarily invokes _ensure_real upon a call to lock_write.
1487
 
        branch._ensure_real = lambda: None
1488
522
        # Lock the branch, reset the record of remote calls.
1489
523
        branch.lock_write()
1490
 
        result = branch._set_last_revision('rev-id2')
 
524
        client._calls = []
 
525
 
 
526
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
 
527
        self.assertEqual(
 
528
            [('call', 'Branch.set_last_revision',
 
529
                ('branch/', 'branch token', 'repo token', 'rev-id2'))],
 
530
            client._calls)
1491
531
        branch.unlock()
1492
532
        self.assertEqual(None, result)
1493
 
        self.assertFinished(client)
1494
533
 
1495
534
    def test_no_such_revision(self):
1496
535
        transport = MemoryTransport()
1498
537
        transport = transport.clone('branch')
1499
538
        # A response of 'NoSuchRevision' is translated into an exception.
1500
539
        client = FakeClient(transport.base)
1501
 
        client.add_expected_call(
1502
 
            'Branch.get_stacked_on_url', ('branch/',),
1503
 
            'error', ('NotStacked',))
1504
 
        client.add_expected_call(
1505
 
            'Branch.lock_write', ('branch/', '', ''),
1506
 
            'success', ('ok', 'branch token', 'repo token'))
1507
 
        client.add_expected_call(
1508
 
            'Branch.last_revision_info',
1509
 
            ('branch/',),
1510
 
            'success', ('ok', '0', 'null:'))
1511
 
        # get_graph calls to construct the revision history, for the set_rh
1512
 
        # hook
1513
 
        lines = ['rev-id']
1514
 
        encoded_body = bz2.compress('\n'.join(lines))
1515
 
        client.add_success_response_with_body(encoded_body, 'ok')
1516
 
        client.add_expected_call(
1517
 
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id',),
1518
 
            'error', ('NoSuchRevision', 'rev-id'))
1519
 
        client.add_expected_call(
1520
 
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
1521
 
            'success', ('ok',))
 
540
        # lock_write
 
541
        client.add_success_response('ok', 'branch token', 'repo token')
 
542
        # set_last_revision
 
543
        client.add_error_response('NoSuchRevision', 'rev-id')
 
544
        # unlock
 
545
        client.add_success_response('ok')
1522
546
 
1523
547
        branch = self.make_remote_branch(transport, client)
1524
548
        branch.lock_write()
 
549
        client._calls = []
 
550
 
1525
551
        self.assertRaises(
1526
 
            errors.NoSuchRevision, branch._set_last_revision, 'rev-id')
 
552
            errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
1527
553
        branch.unlock()
1528
 
        self.assertFinished(client)
1529
554
 
1530
555
    def test_tip_change_rejected(self):
1531
556
        """TipChangeRejected responses cause a TipChangeRejected exception to
1535
560
        transport.mkdir('branch')
1536
561
        transport = transport.clone('branch')
1537
562
        client = FakeClient(transport.base)
 
563
        # lock_write
 
564
        client.add_success_response('ok', 'branch token', 'repo token')
 
565
        # set_last_revision
1538
566
        rejection_msg_unicode = u'rejection message\N{INTERROBANG}'
1539
567
        rejection_msg_utf8 = rejection_msg_unicode.encode('utf8')
1540
 
        client.add_expected_call(
1541
 
            'Branch.get_stacked_on_url', ('branch/',),
1542
 
            'error', ('NotStacked',))
1543
 
        client.add_expected_call(
1544
 
            'Branch.lock_write', ('branch/', '', ''),
1545
 
            'success', ('ok', 'branch token', 'repo token'))
1546
 
        client.add_expected_call(
1547
 
            'Branch.last_revision_info',
1548
 
            ('branch/',),
1549
 
            'success', ('ok', '0', 'null:'))
1550
 
        lines = ['rev-id']
1551
 
        encoded_body = bz2.compress('\n'.join(lines))
1552
 
        client.add_success_response_with_body(encoded_body, 'ok')
1553
 
        client.add_expected_call(
1554
 
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id',),
1555
 
            'error', ('TipChangeRejected', rejection_msg_utf8))
1556
 
        client.add_expected_call(
1557
 
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
1558
 
            'success', ('ok',))
 
568
        client.add_error_response('TipChangeRejected', rejection_msg_utf8)
 
569
        # unlock
 
570
        client.add_success_response('ok')
 
571
 
1559
572
        branch = self.make_remote_branch(transport, client)
1560
 
        branch._ensure_real = lambda: None
1561
573
        branch.lock_write()
 
574
        self.addCleanup(branch.unlock)
 
575
        client._calls = []
 
576
 
1562
577
        # The 'TipChangeRejected' error response triggered by calling
1563
 
        # set_last_revision_info causes a TipChangeRejected exception.
 
578
        # set_revision_history causes a TipChangeRejected exception.
1564
579
        err = self.assertRaises(
1565
 
            errors.TipChangeRejected,
1566
 
            branch._set_last_revision, 'rev-id')
 
580
            errors.TipChangeRejected, branch.set_revision_history, ['rev-id'])
1567
581
        # The UTF-8 message from the response has been decoded into a unicode
1568
582
        # object.
1569
583
        self.assertIsInstance(err.msg, unicode)
1570
584
        self.assertEqual(rejection_msg_unicode, err.msg)
1571
 
        branch.unlock()
1572
 
        self.assertFinished(client)
1573
585
 
1574
586
 
1575
587
class TestBranchSetLastRevisionInfo(RemoteBranchTestCase):
1581
593
        transport.mkdir('branch')
1582
594
        transport = transport.clone('branch')
1583
595
        client = FakeClient(transport.base)
1584
 
        # get_stacked_on_url
1585
 
        client.add_error_response('NotStacked')
1586
596
        # lock_write
1587
597
        client.add_success_response('ok', 'branch token', 'repo token')
1588
 
        # query the current revision
1589
 
        client.add_success_response('ok', '0', 'null:')
1590
598
        # set_last_revision
1591
599
        client.add_success_response('ok')
1592
600
        # unlock
1598
606
        client._calls = []
1599
607
        result = branch.set_last_revision_info(1234, 'a-revision-id')
1600
608
        self.assertEqual(
1601
 
            [('call', 'Branch.last_revision_info', ('branch/',)),
1602
 
             ('call', 'Branch.set_last_revision_info',
 
609
            [('call', 'Branch.set_last_revision_info',
1603
610
                ('branch/', 'branch token', 'repo token',
1604
611
                 '1234', 'a-revision-id'))],
1605
612
            client._calls)
1611
618
        transport.mkdir('branch')
1612
619
        transport = transport.clone('branch')
1613
620
        client = FakeClient(transport.base)
1614
 
        # get_stacked_on_url
1615
 
        client.add_error_response('NotStacked')
1616
621
        # lock_write
1617
622
        client.add_success_response('ok', 'branch token', 'repo token')
1618
623
        # set_last_revision
1629
634
            errors.NoSuchRevision, branch.set_last_revision_info, 123, 'revid')
1630
635
        branch.unlock()
1631
636
 
 
637
    def lock_remote_branch(self, branch):
 
638
        """Trick a RemoteBranch into thinking it is locked."""
 
639
        branch._lock_mode = 'w'
 
640
        branch._lock_count = 2
 
641
        branch._lock_token = 'branch token'
 
642
        branch._repo_lock_token = 'repo token'
 
643
        branch.repository._lock_mode = 'w'
 
644
        branch.repository._lock_count = 2
 
645
        branch.repository._lock_token = 'repo token'
 
646
 
1632
647
    def test_backwards_compatibility(self):
1633
648
        """If the server does not support the Branch.set_last_revision_info
1634
649
        verb (which is new in 1.4), then the client falls back to VFS methods.
1645
660
        transport.mkdir('branch')
1646
661
        transport = transport.clone('branch')
1647
662
        client = FakeClient(transport.base)
1648
 
        client.add_expected_call(
1649
 
            'Branch.get_stacked_on_url', ('branch/',),
1650
 
            'error', ('NotStacked',))
1651
 
        client.add_expected_call(
1652
 
            'Branch.last_revision_info',
1653
 
            ('branch/',),
1654
 
            'success', ('ok', '0', 'null:'))
1655
 
        client.add_expected_call(
1656
 
            'Branch.set_last_revision_info',
1657
 
            ('branch/', 'branch token', 'repo token', '1234', 'a-revision-id',),
1658
 
            'unknown', 'Branch.set_last_revision_info')
1659
 
 
 
663
        client.add_unknown_method_response('Branch.set_last_revision_info')
1660
664
        branch = self.make_remote_branch(transport, client)
1661
665
        class StubRealBranch(object):
1662
666
            def __init__(self):
1673
677
        # Call set_last_revision_info, and verify it behaved as expected.
1674
678
        result = branch.set_last_revision_info(1234, 'a-revision-id')
1675
679
        self.assertEqual(
 
680
            [('call', 'Branch.set_last_revision_info',
 
681
                ('branch/', 'branch token', 'repo token',
 
682
                 '1234', 'a-revision-id')),],
 
683
            client._calls)
 
684
        self.assertEqual(
1676
685
            [('set_last_revision_info', 1234, 'a-revision-id')],
1677
686
            real_branch.calls)
1678
 
        self.assertFinished(client)
1679
687
 
1680
688
    def test_unexpected_error(self):
1681
 
        # If the server sends an error the client doesn't understand, it gets
1682
 
        # turned into an UnknownErrorFromSmartServer, which is presented as a
1683
 
        # non-internal error to the user.
 
689
        # A response of 'NoSuchRevision' is translated into an exception.
1684
690
        transport = MemoryTransport()
1685
691
        transport.mkdir('branch')
1686
692
        transport = transport.clone('branch')
1687
693
        client = FakeClient(transport.base)
1688
 
        # get_stacked_on_url
1689
 
        client.add_error_response('NotStacked')
1690
694
        # lock_write
1691
695
        client.add_success_response('ok', 'branch token', 'repo token')
1692
696
        # set_last_revision
1713
717
        transport.mkdir('branch')
1714
718
        transport = transport.clone('branch')
1715
719
        client = FakeClient(transport.base)
1716
 
        # get_stacked_on_url
1717
 
        client.add_error_response('NotStacked')
1718
720
        # lock_write
1719
721
        client.add_success_response('ok', 'branch token', 'repo token')
1720
722
        # set_last_revision
1736
738
        self.assertEqual('rejection message', err.msg)
1737
739
 
1738
740
 
1739
 
class TestBranchGetSetConfig(RemoteBranchTestCase):
 
741
class TestBranchControlGetBranchConf(tests.TestCaseWithMemoryTransport):
 
742
    """Getting the branch configuration should use an abstract method not vfs.
 
743
    """
1740
744
 
1741
745
    def test_get_branch_conf(self):
1742
 
        # in an empty branch we decode the response properly
1743
 
        client = FakeClient()
1744
 
        client.add_expected_call(
1745
 
            'Branch.get_stacked_on_url', ('memory:///',),
1746
 
            'error', ('NotStacked',),)
1747
 
        client.add_success_response_with_body('# config file body', 'ok')
1748
 
        transport = MemoryTransport()
1749
 
        branch = self.make_remote_branch(transport, client)
1750
 
        config = branch.get_config()
1751
 
        config.has_explicit_nickname()
1752
 
        self.assertEqual(
1753
 
            [('call', 'Branch.get_stacked_on_url', ('memory:///',)),
1754
 
             ('call_expecting_body', 'Branch.get_config_file', ('memory:///',))],
1755
 
            client._calls)
1756
 
 
1757
 
    def test_get_multi_line_branch_conf(self):
1758
 
        # Make sure that multiple-line branch.conf files are supported
1759
 
        #
1760
 
        # https://bugs.launchpad.net/bzr/+bug/354075
1761
 
        client = FakeClient()
1762
 
        client.add_expected_call(
1763
 
            'Branch.get_stacked_on_url', ('memory:///',),
1764
 
            'error', ('NotStacked',),)
1765
 
        client.add_success_response_with_body('a = 1\nb = 2\nc = 3\n', 'ok')
1766
 
        transport = MemoryTransport()
1767
 
        branch = self.make_remote_branch(transport, client)
1768
 
        config = branch.get_config()
1769
 
        self.assertEqual(u'2', config.get_user_option('b'))
1770
 
 
1771
 
    def test_set_option(self):
1772
 
        client = FakeClient()
1773
 
        client.add_expected_call(
1774
 
            'Branch.get_stacked_on_url', ('memory:///',),
1775
 
            'error', ('NotStacked',),)
1776
 
        client.add_expected_call(
1777
 
            'Branch.lock_write', ('memory:///', '', ''),
1778
 
            'success', ('ok', 'branch token', 'repo token'))
1779
 
        client.add_expected_call(
1780
 
            'Branch.set_config_option', ('memory:///', 'branch token',
1781
 
            'repo token', 'foo', 'bar', ''),
1782
 
            'success', ())
1783
 
        client.add_expected_call(
1784
 
            'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
1785
 
            'success', ('ok',))
1786
 
        transport = MemoryTransport()
1787
 
        branch = self.make_remote_branch(transport, client)
1788
 
        branch.lock_write()
1789
 
        config = branch._get_config()
1790
 
        config.set_option('foo', 'bar')
1791
 
        branch.unlock()
1792
 
        self.assertFinished(client)
1793
 
 
1794
 
    def test_set_option_with_dict(self):
1795
 
        client = FakeClient()
1796
 
        client.add_expected_call(
1797
 
            'Branch.get_stacked_on_url', ('memory:///',),
1798
 
            'error', ('NotStacked',),)
1799
 
        client.add_expected_call(
1800
 
            'Branch.lock_write', ('memory:///', '', ''),
1801
 
            'success', ('ok', 'branch token', 'repo token'))
1802
 
        encoded_dict_value = 'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde'
1803
 
        client.add_expected_call(
1804
 
            'Branch.set_config_option_dict', ('memory:///', 'branch token',
1805
 
            'repo token', encoded_dict_value, 'foo', ''),
1806
 
            'success', ())
1807
 
        client.add_expected_call(
1808
 
            'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
1809
 
            'success', ('ok',))
1810
 
        transport = MemoryTransport()
1811
 
        branch = self.make_remote_branch(transport, client)
1812
 
        branch.lock_write()
1813
 
        config = branch._get_config()
1814
 
        config.set_option(
1815
 
            {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'},
1816
 
            'foo')
1817
 
        branch.unlock()
1818
 
        self.assertFinished(client)
1819
 
 
1820
 
    def test_backwards_compat_set_option(self):
1821
 
        self.setup_smart_server_with_call_log()
1822
 
        branch = self.make_branch('.')
1823
 
        verb = 'Branch.set_config_option'
1824
 
        self.disable_verb(verb)
1825
 
        branch.lock_write()
1826
 
        self.addCleanup(branch.unlock)
1827
 
        self.reset_smart_call_log()
1828
 
        branch._get_config().set_option('value', 'name')
1829
 
        self.assertLength(10, self.hpss_calls)
1830
 
        self.assertEqual('value', branch._get_config().get_option('name'))
1831
 
 
1832
 
    def test_backwards_compat_set_option_with_dict(self):
1833
 
        self.setup_smart_server_with_call_log()
1834
 
        branch = self.make_branch('.')
1835
 
        verb = 'Branch.set_config_option_dict'
1836
 
        self.disable_verb(verb)
1837
 
        branch.lock_write()
1838
 
        self.addCleanup(branch.unlock)
1839
 
        self.reset_smart_call_log()
1840
 
        config = branch._get_config()
1841
 
        value_dict = {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
1842
 
        config.set_option(value_dict, 'name')
1843
 
        self.assertLength(10, self.hpss_calls)
1844
 
        self.assertEqual(value_dict, branch._get_config().get_option('name'))
 
746
        raise tests.KnownFailure('branch.conf is not retrieved by get_config_file')
 
747
        ## # We should see that branch.get_config() does a single rpc to get the
 
748
        ## # remote configuration file, abstracting away where that is stored on
 
749
        ## # the server.  However at the moment it always falls back to using the
 
750
        ## # vfs, and this would need some changes in config.py.
 
751
 
 
752
        ## # in an empty branch we decode the response properly
 
753
        ## client = FakeClient([(('ok', ), '# config file body')], self.get_url())
 
754
        ## # we need to make a real branch because the remote_branch.control_files
 
755
        ## # will trigger _ensure_real.
 
756
        ## branch = self.make_branch('quack')
 
757
        ## transport = branch.bzrdir.root_transport
 
758
        ## # we do not want bzrdir to make any remote calls
 
759
        ## bzrdir = RemoteBzrDir(transport, _client=False)
 
760
        ## branch = RemoteBranch(bzrdir, None, _client=client)
 
761
        ## config = branch.get_config()
 
762
        ## self.assertEqual(
 
763
        ##     [('call_expecting_body', 'Branch.get_config_file', ('quack/',))],
 
764
        ##     client._calls)
1845
765
 
1846
766
 
1847
767
class TestBranchLockWrite(RemoteBranchTestCase):
1849
769
    def test_lock_write_unlockable(self):
1850
770
        transport = MemoryTransport()
1851
771
        client = FakeClient(transport.base)
1852
 
        client.add_expected_call(
1853
 
            'Branch.get_stacked_on_url', ('quack/',),
1854
 
            'error', ('NotStacked',),)
1855
 
        client.add_expected_call(
1856
 
            'Branch.lock_write', ('quack/', '', ''),
1857
 
            'error', ('UnlockableTransport',))
 
772
        client.add_error_response('UnlockableTransport')
1858
773
        transport.mkdir('quack')
1859
774
        transport = transport.clone('quack')
1860
775
        branch = self.make_remote_branch(transport, client)
1861
776
        self.assertRaises(errors.UnlockableTransport, branch.lock_write)
1862
 
        self.assertFinished(client)
1863
 
 
1864
 
 
1865
 
class TestBzrDirGetSetConfig(RemoteBzrDirTestCase):
1866
 
 
1867
 
    def test__get_config(self):
1868
 
        client = FakeClient()
1869
 
        client.add_success_response_with_body('default_stack_on = /\n', 'ok')
1870
 
        transport = MemoryTransport()
1871
 
        bzrdir = self.make_remote_bzrdir(transport, client)
1872
 
        config = bzrdir.get_config()
1873
 
        self.assertEqual('/', config.get_default_stack_on())
1874
777
        self.assertEqual(
1875
 
            [('call_expecting_body', 'BzrDir.get_config_file', ('memory:///',))],
 
778
            [('call', 'Branch.lock_write', ('quack/', '', ''))],
1876
779
            client._calls)
1877
780
 
1878
 
    def test_set_option_uses_vfs(self):
1879
 
        self.setup_smart_server_with_call_log()
1880
 
        bzrdir = self.make_bzrdir('.')
1881
 
        self.reset_smart_call_log()
1882
 
        config = bzrdir.get_config()
1883
 
        config.set_default_stack_on('/')
1884
 
        self.assertLength(3, self.hpss_calls)
1885
 
 
1886
 
    def test_backwards_compat_get_option(self):
1887
 
        self.setup_smart_server_with_call_log()
1888
 
        bzrdir = self.make_bzrdir('.')
1889
 
        verb = 'BzrDir.get_config_file'
1890
 
        self.disable_verb(verb)
1891
 
        self.reset_smart_call_log()
1892
 
        self.assertEqual(None,
1893
 
            bzrdir._get_config().get_option('default_stack_on'))
1894
 
        self.assertLength(3, self.hpss_calls)
1895
 
 
1896
781
 
1897
782
class TestTransportIsReadonly(tests.TestCase):
1898
783
 
1918
803
 
1919
804
    def test_error_from_old_server(self):
1920
805
        """bzr 0.15 and earlier servers don't recognise the is_readonly verb.
1921
 
 
 
806
        
1922
807
        Clients should treat it as a "no" response, because is_readonly is only
1923
808
        advisory anyway (a transport could be read-write, but then the
1924
809
        underlying filesystem could be readonly anyway).
1933
818
            client._calls)
1934
819
 
1935
820
 
1936
 
class TestTransportMkdir(tests.TestCase):
1937
 
 
1938
 
    def test_permissiondenied(self):
1939
 
        client = FakeClient()
1940
 
        client.add_error_response('PermissionDenied', 'remote path', 'extra')
1941
 
        transport = RemoteTransport('bzr://example.com/', medium=False,
1942
 
                                    _client=client)
1943
 
        exc = self.assertRaises(
1944
 
            errors.PermissionDenied, transport.mkdir, 'client path')
1945
 
        expected_error = errors.PermissionDenied('/client path', 'extra')
1946
 
        self.assertEqual(expected_error, exc)
1947
 
 
1948
 
 
1949
 
class TestRemoteSSHTransportAuthentication(tests.TestCaseInTempDir):
1950
 
 
1951
 
    def test_defaults_to_none(self):
1952
 
        t = RemoteSSHTransport('bzr+ssh://example.com')
1953
 
        self.assertIs(None, t._get_credentials()[0])
1954
 
 
1955
 
    def test_uses_authentication_config(self):
1956
 
        conf = config.AuthenticationConfig()
1957
 
        conf._get_config().update(
1958
 
            {'bzr+sshtest': {'scheme': 'ssh', 'user': 'bar', 'host':
1959
 
            'example.com'}})
1960
 
        conf._save()
1961
 
        t = RemoteSSHTransport('bzr+ssh://example.com')
1962
 
        self.assertEqual('bar', t._get_credentials()[0])
1963
 
 
1964
 
 
1965
 
class TestRemoteRepository(TestRemote):
 
821
class TestRemoteRepository(tests.TestCase):
1966
822
    """Base for testing RemoteRepository protocol usage.
1967
 
 
1968
 
    These tests contain frozen requests and responses.  We want any changes to
 
823
    
 
824
    These tests contain frozen requests and responses.  We want any changes to 
1969
825
    what is sent or expected to be require a thoughtful update to these tests
1970
826
    because they might break compatibility with different-versioned servers.
1971
827
    """
1972
828
 
1973
829
    def setup_fake_client_and_repository(self, transport_path):
1974
830
        """Create the fake client and repository for testing with.
1975
 
 
 
831
        
1976
832
        There's no real server here; we just have canned responses sent
1977
833
        back one by one.
1978
 
 
 
834
        
1979
835
        :param transport_path: Path below the root of the MemoryTransport
1980
836
            where the repository will be created.
1981
837
        """
1984
840
        client = FakeClient(transport.base)
1985
841
        transport = transport.clone(transport_path)
1986
842
        # we do not want bzrdir to make any remote calls
1987
 
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
1988
 
            _client=False)
 
843
        bzrdir = RemoteBzrDir(transport, _client=False)
1989
844
        repo = RemoteRepository(bzrdir, None, _client=client)
1990
845
        return repo, client
1991
846
 
1992
847
 
1993
 
def remoted_description(format):
1994
 
    return 'Remote: ' + format.get_format_description()
1995
 
 
1996
 
 
1997
 
class TestBranchFormat(tests.TestCase):
1998
 
 
1999
 
    def test_get_format_description(self):
2000
 
        remote_format = RemoteBranchFormat()
2001
 
        real_format = branch.format_registry.get_default()
2002
 
        remote_format._network_name = real_format.network_name()
2003
 
        self.assertEqual(remoted_description(real_format),
2004
 
            remote_format.get_format_description())
2005
 
 
2006
 
 
2007
 
class TestRepositoryFormat(TestRemoteRepository):
2008
 
 
2009
 
    def test_fast_delta(self):
2010
 
        true_name = groupcompress_repo.RepositoryFormat2a().network_name()
2011
 
        true_format = RemoteRepositoryFormat()
2012
 
        true_format._network_name = true_name
2013
 
        self.assertEqual(True, true_format.fast_deltas)
2014
 
        false_name = knitpack_repo.RepositoryFormatKnitPack1().network_name()
2015
 
        false_format = RemoteRepositoryFormat()
2016
 
        false_format._network_name = false_name
2017
 
        self.assertEqual(False, false_format.fast_deltas)
2018
 
 
2019
 
    def test_get_format_description(self):
2020
 
        remote_repo_format = RemoteRepositoryFormat()
2021
 
        real_format = repository.format_registry.get_default()
2022
 
        remote_repo_format._network_name = real_format.network_name()
2023
 
        self.assertEqual(remoted_description(real_format),
2024
 
            remote_repo_format.get_format_description())
2025
 
 
2026
 
 
2027
848
class TestRepositoryGatherStats(TestRemoteRepository):
2028
849
 
2029
850
    def test_revid_none(self):
2085
906
class TestRepositoryGetGraph(TestRemoteRepository):
2086
907
 
2087
908
    def test_get_graph(self):
2088
 
        # get_graph returns a graph with a custom parents provider.
 
909
        # get_graph returns a graph with the repository as the
 
910
        # parents_provider.
2089
911
        transport_path = 'quack'
2090
912
        repo, client = self.setup_fake_client_and_repository(transport_path)
2091
913
        graph = repo.get_graph()
2092
 
        self.assertNotEqual(graph._parents_provider, repo)
 
914
        self.assertEqual(graph._parents_provider, repo)
2093
915
 
2094
916
 
2095
917
class TestRepositoryGetParentMap(TestRemoteRepository):
2117
939
        self.assertEqual({r1: (NULL_REVISION,)}, parents)
2118
940
        self.assertEqual(
2119
941
            [('call_with_body_bytes_expecting_body',
2120
 
              'Repository.get_parent_map', ('quack/', 'include-missing:', r2),
2121
 
              '\n\n0')],
 
942
              'Repository.get_parent_map', ('quack/', r2), '\n\n0')],
2122
943
            client._calls)
2123
944
        repo.unlock()
2124
945
        # now we call again, and it should use the second response.
2128
949
        self.assertEqual({r1: (NULL_REVISION,)}, parents)
2129
950
        self.assertEqual(
2130
951
            [('call_with_body_bytes_expecting_body',
2131
 
              'Repository.get_parent_map', ('quack/', 'include-missing:', r2),
2132
 
              '\n\n0'),
 
952
              'Repository.get_parent_map', ('quack/', r2), '\n\n0'),
2133
953
             ('call_with_body_bytes_expecting_body',
2134
 
              'Repository.get_parent_map', ('quack/', 'include-missing:', r1),
2135
 
              '\n\n0'),
 
954
              'Repository.get_parent_map', ('quack/', r1), '\n\n0'),
2136
955
            ],
2137
956
            client._calls)
2138
957
        repo.unlock()
2139
958
 
2140
959
    def test_get_parent_map_reconnects_if_unknown_method(self):
2141
960
        transport_path = 'quack'
2142
 
        rev_id = 'revision-id'
2143
961
        repo, client = self.setup_fake_client_and_repository(transport_path)
2144
 
        client.add_unknown_method_response('Repository.get_parent_map')
2145
 
        client.add_success_response_with_body(rev_id, 'ok')
 
962
        client.add_unknown_method_response('Repository,get_parent_map')
 
963
        client.add_success_response_with_body('', 'ok')
2146
964
        self.assertFalse(client._medium._is_remote_before((1, 2)))
2147
 
        parents = repo.get_parent_map([rev_id])
 
965
        rev_id = 'revision-id'
 
966
        expected_deprecations = [
 
967
            'bzrlib.remote.RemoteRepository.get_revision_graph was deprecated '
 
968
            'in version 1.4.']
 
969
        parents = self.callDeprecated(
 
970
            expected_deprecations, repo.get_parent_map, [rev_id])
2148
971
        self.assertEqual(
2149
972
            [('call_with_body_bytes_expecting_body',
2150
 
              'Repository.get_parent_map', ('quack/', 'include-missing:',
2151
 
              rev_id), '\n\n0'),
 
973
              'Repository.get_parent_map', ('quack/', rev_id), '\n\n0'),
2152
974
             ('disconnect medium',),
2153
975
             ('call_expecting_body', 'Repository.get_revision_graph',
2154
976
              ('quack/', ''))],
2155
977
            client._calls)
2156
978
        # The medium is now marked as being connected to an older server
2157
979
        self.assertTrue(client._medium._is_remote_before((1, 2)))
2158
 
        self.assertEqual({rev_id: ('null:',)}, parents)
2159
980
 
2160
981
    def test_get_parent_map_fallback_parentless_node(self):
2161
982
        """get_parent_map falls back to get_revision_graph on old servers.  The
2173
994
        repo, client = self.setup_fake_client_and_repository(transport_path)
2174
995
        client.add_success_response_with_body(rev_id, 'ok')
2175
996
        client._medium._remember_remote_is_before((1, 2))
2176
 
        parents = repo.get_parent_map([rev_id])
 
997
        expected_deprecations = [
 
998
            'bzrlib.remote.RemoteRepository.get_revision_graph was deprecated '
 
999
            'in version 1.4.']
 
1000
        parents = self.callDeprecated(
 
1001
            expected_deprecations, repo.get_parent_map, [rev_id])
2177
1002
        self.assertEqual(
2178
1003
            [('call_expecting_body', 'Repository.get_revision_graph',
2179
1004
             ('quack/', ''))],
2187
1012
            errors.UnexpectedSmartServerResponse,
2188
1013
            repo.get_parent_map, ['a-revision-id'])
2189
1014
 
2190
 
    def test_get_parent_map_negative_caches_missing_keys(self):
2191
 
        self.setup_smart_server_with_call_log()
2192
 
        repo = self.make_repository('foo')
2193
 
        self.assertIsInstance(repo, RemoteRepository)
2194
 
        repo.lock_read()
2195
 
        self.addCleanup(repo.unlock)
2196
 
        self.reset_smart_call_log()
2197
 
        graph = repo.get_graph()
2198
 
        self.assertEqual({},
2199
 
            graph.get_parent_map(['some-missing', 'other-missing']))
2200
 
        self.assertLength(1, self.hpss_calls)
2201
 
        # No call if we repeat this
2202
 
        self.reset_smart_call_log()
2203
 
        graph = repo.get_graph()
2204
 
        self.assertEqual({},
2205
 
            graph.get_parent_map(['some-missing', 'other-missing']))
2206
 
        self.assertLength(0, self.hpss_calls)
2207
 
        # Asking for more unknown keys makes a request.
2208
 
        self.reset_smart_call_log()
2209
 
        graph = repo.get_graph()
2210
 
        self.assertEqual({},
2211
 
            graph.get_parent_map(['some-missing', 'other-missing',
2212
 
                'more-missing']))
2213
 
        self.assertLength(1, self.hpss_calls)
2214
 
 
2215
 
    def disableExtraResults(self):
2216
 
        self.overrideAttr(SmartServerRepositoryGetParentMap,
2217
 
                          'no_extra_results', True)
2218
 
 
2219
 
    def test_null_cached_missing_and_stop_key(self):
2220
 
        self.setup_smart_server_with_call_log()
2221
 
        # Make a branch with a single revision.
2222
 
        builder = self.make_branch_builder('foo')
2223
 
        builder.start_series()
2224
 
        builder.build_snapshot('first', None, [
2225
 
            ('add', ('', 'root-id', 'directory', ''))])
2226
 
        builder.finish_series()
2227
 
        branch = builder.get_branch()
2228
 
        repo = branch.repository
2229
 
        self.assertIsInstance(repo, RemoteRepository)
2230
 
        # Stop the server from sending extra results.
2231
 
        self.disableExtraResults()
2232
 
        repo.lock_read()
2233
 
        self.addCleanup(repo.unlock)
2234
 
        self.reset_smart_call_log()
2235
 
        graph = repo.get_graph()
2236
 
        # Query for 'first' and 'null:'.  Because 'null:' is a parent of
2237
 
        # 'first' it will be a candidate for the stop_keys of subsequent
2238
 
        # requests, and because 'null:' was queried but not returned it will be
2239
 
        # cached as missing.
2240
 
        self.assertEqual({'first': ('null:',)},
2241
 
            graph.get_parent_map(['first', 'null:']))
2242
 
        # Now query for another key.  This request will pass along a recipe of
2243
 
        # start and stop keys describing the already cached results, and this
2244
 
        # recipe's revision count must be correct (or else it will trigger an
2245
 
        # error from the server).
2246
 
        self.assertEqual({}, graph.get_parent_map(['another-key']))
2247
 
        # This assertion guards against disableExtraResults silently failing to
2248
 
        # work, thus invalidating the test.
2249
 
        self.assertLength(2, self.hpss_calls)
2250
 
 
2251
 
    def test_get_parent_map_gets_ghosts_from_result(self):
2252
 
        # asking for a revision should negatively cache close ghosts in its
2253
 
        # ancestry.
2254
 
        self.setup_smart_server_with_call_log()
2255
 
        tree = self.make_branch_and_memory_tree('foo')
2256
 
        tree.lock_write()
2257
 
        try:
2258
 
            builder = treebuilder.TreeBuilder()
2259
 
            builder.start_tree(tree)
2260
 
            builder.build([])
2261
 
            builder.finish_tree()
2262
 
            tree.set_parent_ids(['non-existant'], allow_leftmost_as_ghost=True)
2263
 
            rev_id = tree.commit('')
2264
 
        finally:
2265
 
            tree.unlock()
2266
 
        tree.lock_read()
2267
 
        self.addCleanup(tree.unlock)
2268
 
        repo = tree.branch.repository
2269
 
        self.assertIsInstance(repo, RemoteRepository)
2270
 
        # ask for rev_id
2271
 
        repo.get_parent_map([rev_id])
2272
 
        self.reset_smart_call_log()
2273
 
        # Now asking for rev_id's ghost parent should not make calls
2274
 
        self.assertEqual({}, repo.get_parent_map(['non-existant']))
2275
 
        self.assertLength(0, self.hpss_calls)
2276
 
 
2277
 
 
2278
 
class TestGetParentMapAllowsNew(tests.TestCaseWithTransport):
2279
 
 
2280
 
    def test_allows_new_revisions(self):
2281
 
        """get_parent_map's results can be updated by commit."""
2282
 
        smart_server = test_server.SmartTCPServer_for_testing()
2283
 
        self.start_server(smart_server)
2284
 
        self.make_branch('branch')
2285
 
        branch = Branch.open(smart_server.get_url() + '/branch')
2286
 
        tree = branch.create_checkout('tree', lightweight=True)
2287
 
        tree.lock_write()
2288
 
        self.addCleanup(tree.unlock)
2289
 
        graph = tree.branch.repository.get_graph()
2290
 
        # This provides an opportunity for the missing rev-id to be cached.
2291
 
        self.assertEqual({}, graph.get_parent_map(['rev1']))
2292
 
        tree.commit('message', rev_id='rev1')
2293
 
        graph = tree.branch.repository.get_graph()
2294
 
        self.assertEqual({'rev1': ('null:',)}, graph.get_parent_map(['rev1']))
2295
 
 
2296
1015
 
2297
1016
class TestRepositoryGetRevisionGraph(TestRemoteRepository):
2298
 
 
 
1017
    
2299
1018
    def test_null_revision(self):
2300
1019
        # a null revision has the predictable result {}, we should have no wire
2301
1020
        # traffic when calling it with this argument
2302
1021
        transport_path = 'empty'
2303
1022
        repo, client = self.setup_fake_client_and_repository(transport_path)
2304
1023
        client.add_success_response('notused')
2305
 
        # actual RemoteRepository.get_revision_graph is gone, but there's an
2306
 
        # equivalent private method for testing
2307
 
        result = repo._get_revision_graph(NULL_REVISION)
 
1024
        result = self.applyDeprecated(one_four, repo.get_revision_graph,
 
1025
            NULL_REVISION)
2308
1026
        self.assertEqual([], client._calls)
2309
1027
        self.assertEqual({}, result)
2310
1028
 
2318
1036
        transport_path = 'sinhala'
2319
1037
        repo, client = self.setup_fake_client_and_repository(transport_path)
2320
1038
        client.add_success_response_with_body(encoded_body, 'ok')
2321
 
        # actual RemoteRepository.get_revision_graph is gone, but there's an
2322
 
        # equivalent private method for testing
2323
 
        result = repo._get_revision_graph(None)
 
1039
        result = self.applyDeprecated(one_four, repo.get_revision_graph)
2324
1040
        self.assertEqual(
2325
1041
            [('call_expecting_body', 'Repository.get_revision_graph',
2326
1042
             ('sinhala/', ''))],
2339
1055
        transport_path = 'sinhala'
2340
1056
        repo, client = self.setup_fake_client_and_repository(transport_path)
2341
1057
        client.add_success_response_with_body(encoded_body, 'ok')
2342
 
        result = repo._get_revision_graph(r2)
 
1058
        result = self.applyDeprecated(one_four, repo.get_revision_graph, r2)
2343
1059
        self.assertEqual(
2344
1060
            [('call_expecting_body', 'Repository.get_revision_graph',
2345
1061
             ('sinhala/', r2))],
2353
1069
        client.add_error_response('nosuchrevision', revid)
2354
1070
        # also check that the right revision is reported in the error
2355
1071
        self.assertRaises(errors.NoSuchRevision,
2356
 
            repo._get_revision_graph, revid)
 
1072
            self.applyDeprecated, one_four, repo.get_revision_graph, revid)
2357
1073
        self.assertEqual(
2358
1074
            [('call_expecting_body', 'Repository.get_revision_graph',
2359
1075
             ('sinhala/', revid))],
2365
1081
        repo, client = self.setup_fake_client_and_repository(transport_path)
2366
1082
        client.add_error_response('AnUnexpectedError')
2367
1083
        e = self.assertRaises(errors.UnknownErrorFromSmartServer,
2368
 
            repo._get_revision_graph, revid)
 
1084
            self.applyDeprecated, one_four, repo.get_revision_graph, revid)
2369
1085
        self.assertEqual(('AnUnexpectedError',), e.error_tuple)
2370
1086
 
2371
 
 
2372
 
class TestRepositoryGetRevIdForRevno(TestRemoteRepository):
2373
 
 
2374
 
    def test_ok(self):
2375
 
        repo, client = self.setup_fake_client_and_repository('quack')
2376
 
        client.add_expected_call(
2377
 
            'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
2378
 
            'success', ('ok', 'rev-five'))
2379
 
        result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
2380
 
        self.assertEqual((True, 'rev-five'), result)
2381
 
        self.assertFinished(client)
2382
 
 
2383
 
    def test_history_incomplete(self):
2384
 
        repo, client = self.setup_fake_client_and_repository('quack')
2385
 
        client.add_expected_call(
2386
 
            'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
2387
 
            'success', ('history-incomplete', 10, 'rev-ten'))
2388
 
        result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
2389
 
        self.assertEqual((False, (10, 'rev-ten')), result)
2390
 
        self.assertFinished(client)
2391
 
 
2392
 
    def test_history_incomplete_with_fallback(self):
2393
 
        """A 'history-incomplete' response causes the fallback repository to be
2394
 
        queried too, if one is set.
2395
 
        """
2396
 
        # Make a repo with a fallback repo, both using a FakeClient.
2397
 
        format = remote.response_tuple_to_repo_format(
2398
 
            ('yes', 'no', 'yes', self.get_repo_format().network_name()))
2399
 
        repo, client = self.setup_fake_client_and_repository('quack')
2400
 
        repo._format = format
2401
 
        fallback_repo, ignored = self.setup_fake_client_and_repository(
2402
 
            'fallback')
2403
 
        fallback_repo._client = client
2404
 
        fallback_repo._format = format
2405
 
        repo.add_fallback_repository(fallback_repo)
2406
 
        # First the client should ask the primary repo
2407
 
        client.add_expected_call(
2408
 
            'Repository.get_rev_id_for_revno', ('quack/', 1, (42, 'rev-foo')),
2409
 
            'success', ('history-incomplete', 2, 'rev-two'))
2410
 
        # Then it should ask the fallback, using revno/revid from the
2411
 
        # history-incomplete response as the known revno/revid.
2412
 
        client.add_expected_call(
2413
 
            'Repository.get_rev_id_for_revno',('fallback/', 1, (2, 'rev-two')),
2414
 
            'success', ('ok', 'rev-one'))
2415
 
        result = repo.get_rev_id_for_revno(1, (42, 'rev-foo'))
2416
 
        self.assertEqual((True, 'rev-one'), result)
2417
 
        self.assertFinished(client)
2418
 
 
2419
 
    def test_nosuchrevision(self):
2420
 
        # 'nosuchrevision' is returned when the known-revid is not found in the
2421
 
        # remote repo.  The client translates that response to NoSuchRevision.
2422
 
        repo, client = self.setup_fake_client_and_repository('quack')
2423
 
        client.add_expected_call(
2424
 
            'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
2425
 
            'error', ('nosuchrevision', 'rev-foo'))
2426
 
        self.assertRaises(
2427
 
            errors.NoSuchRevision,
2428
 
            repo.get_rev_id_for_revno, 5, (42, 'rev-foo'))
2429
 
        self.assertFinished(client)
2430
 
 
2431
 
    def test_branch_fallback_locking(self):
2432
 
        """RemoteBranch.get_rev_id takes a read lock, and tries to call the
2433
 
        get_rev_id_for_revno verb.  If the verb is unknown the VFS fallback
2434
 
        will be invoked, which will fail if the repo is unlocked.
2435
 
        """
2436
 
        self.setup_smart_server_with_call_log()
2437
 
        tree = self.make_branch_and_memory_tree('.')
2438
 
        tree.lock_write()
2439
 
        tree.add('')
2440
 
        rev1 = tree.commit('First')
2441
 
        rev2 = tree.commit('Second')
2442
 
        tree.unlock()
2443
 
        branch = tree.branch
2444
 
        self.assertFalse(branch.is_locked())
2445
 
        self.reset_smart_call_log()
2446
 
        verb = 'Repository.get_rev_id_for_revno'
2447
 
        self.disable_verb(verb)
2448
 
        self.assertEqual(rev1, branch.get_rev_id(1))
2449
 
        self.assertLength(1, [call for call in self.hpss_calls if
2450
 
                              call.call.method == verb])
2451
 
 
2452
 
 
 
1087
        
2453
1088
class TestRepositoryIsShared(TestRemoteRepository):
2454
1089
 
2455
1090
    def test_is_shared(self):
2481
1116
        transport_path = 'quack'
2482
1117
        repo, client = self.setup_fake_client_and_repository(transport_path)
2483
1118
        client.add_success_response('ok', 'a token')
2484
 
        token = repo.lock_write().repository_token
 
1119
        result = repo.lock_write()
2485
1120
        self.assertEqual(
2486
1121
            [('call', 'Repository.lock_write', ('quack/', ''))],
2487
1122
            client._calls)
2488
 
        self.assertEqual('a token', token)
 
1123
        self.assertEqual('a token', result)
2489
1124
 
2490
1125
    def test_lock_write_already_locked(self):
2491
1126
        transport_path = 'quack'
2506
1141
            client._calls)
2507
1142
 
2508
1143
 
2509
 
class TestRepositorySetMakeWorkingTrees(TestRemoteRepository):
2510
 
 
2511
 
    def test_backwards_compat(self):
2512
 
        self.setup_smart_server_with_call_log()
2513
 
        repo = self.make_repository('.')
2514
 
        self.reset_smart_call_log()
2515
 
        verb = 'Repository.set_make_working_trees'
2516
 
        self.disable_verb(verb)
2517
 
        repo.set_make_working_trees(True)
2518
 
        call_count = len([call for call in self.hpss_calls if
2519
 
            call.call.method == verb])
2520
 
        self.assertEqual(1, call_count)
2521
 
 
2522
 
    def test_current(self):
2523
 
        transport_path = 'quack'
2524
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2525
 
        client.add_expected_call(
2526
 
            'Repository.set_make_working_trees', ('quack/', 'True'),
2527
 
            'success', ('ok',))
2528
 
        client.add_expected_call(
2529
 
            'Repository.set_make_working_trees', ('quack/', 'False'),
2530
 
            'success', ('ok',))
2531
 
        repo.set_make_working_trees(True)
2532
 
        repo.set_make_working_trees(False)
2533
 
 
2534
 
 
2535
1144
class TestRepositoryUnlock(TestRemoteRepository):
2536
1145
 
2537
1146
    def test_unlock(self):
2570
1179
        self.assertEqual([], client._calls)
2571
1180
 
2572
1181
 
2573
 
class TestRepositoryInsertStreamBase(TestRemoteRepository):
2574
 
    """Base class for Repository.insert_stream and .insert_stream_1.19
2575
 
    tests.
2576
 
    """
2577
 
    
2578
 
    def checkInsertEmptyStream(self, repo, client):
2579
 
        """Insert an empty stream, checking the result.
2580
 
 
2581
 
        This checks that there are no resume_tokens or missing_keys, and that
2582
 
        the client is finished.
2583
 
        """
2584
 
        sink = repo._get_sink()
2585
 
        fmt = repository.format_registry.get_default()
2586
 
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
2587
 
        self.assertEqual([], resume_tokens)
2588
 
        self.assertEqual(set(), missing_keys)
2589
 
        self.assertFinished(client)
2590
 
 
2591
 
 
2592
 
class TestRepositoryInsertStream(TestRepositoryInsertStreamBase):
2593
 
    """Tests for using Repository.insert_stream verb when the _1.19 variant is
2594
 
    not available.
2595
 
 
2596
 
    This test case is very similar to TestRepositoryInsertStream_1_19.
2597
 
    """
2598
 
 
2599
 
    def setUp(self):
2600
 
        TestRemoteRepository.setUp(self)
2601
 
        self.disable_verb('Repository.insert_stream_1.19')
2602
 
 
2603
 
    def test_unlocked_repo(self):
2604
 
        transport_path = 'quack'
2605
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2606
 
        client.add_expected_call(
2607
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2608
 
            'unknown', ('Repository.insert_stream_1.19',))
2609
 
        client.add_expected_call(
2610
 
            'Repository.insert_stream', ('quack/', ''),
2611
 
            'success', ('ok',))
2612
 
        client.add_expected_call(
2613
 
            'Repository.insert_stream', ('quack/', ''),
2614
 
            'success', ('ok',))
2615
 
        self.checkInsertEmptyStream(repo, client)
2616
 
 
2617
 
    def test_locked_repo_with_no_lock_token(self):
2618
 
        transport_path = 'quack'
2619
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2620
 
        client.add_expected_call(
2621
 
            'Repository.lock_write', ('quack/', ''),
2622
 
            'success', ('ok', ''))
2623
 
        client.add_expected_call(
2624
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2625
 
            'unknown', ('Repository.insert_stream_1.19',))
2626
 
        client.add_expected_call(
2627
 
            'Repository.insert_stream', ('quack/', ''),
2628
 
            'success', ('ok',))
2629
 
        client.add_expected_call(
2630
 
            'Repository.insert_stream', ('quack/', ''),
2631
 
            'success', ('ok',))
2632
 
        repo.lock_write()
2633
 
        self.checkInsertEmptyStream(repo, client)
2634
 
 
2635
 
    def test_locked_repo_with_lock_token(self):
2636
 
        transport_path = 'quack'
2637
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2638
 
        client.add_expected_call(
2639
 
            'Repository.lock_write', ('quack/', ''),
2640
 
            'success', ('ok', 'a token'))
2641
 
        client.add_expected_call(
2642
 
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
2643
 
            'unknown', ('Repository.insert_stream_1.19',))
2644
 
        client.add_expected_call(
2645
 
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
2646
 
            'success', ('ok',))
2647
 
        client.add_expected_call(
2648
 
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
2649
 
            'success', ('ok',))
2650
 
        repo.lock_write()
2651
 
        self.checkInsertEmptyStream(repo, client)
2652
 
 
2653
 
    def test_stream_with_inventory_deltas(self):
2654
 
        """'inventory-deltas' substreams cannot be sent to the
2655
 
        Repository.insert_stream verb, because not all servers that implement
2656
 
        that verb will accept them.  So when one is encountered the RemoteSink
2657
 
        immediately stops using that verb and falls back to VFS insert_stream.
2658
 
        """
2659
 
        transport_path = 'quack'
2660
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2661
 
        client.add_expected_call(
2662
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2663
 
            'unknown', ('Repository.insert_stream_1.19',))
2664
 
        client.add_expected_call(
2665
 
            'Repository.insert_stream', ('quack/', ''),
2666
 
            'success', ('ok',))
2667
 
        client.add_expected_call(
2668
 
            'Repository.insert_stream', ('quack/', ''),
2669
 
            'success', ('ok',))
2670
 
        # Create a fake real repository for insert_stream to fall back on, so
2671
 
        # that we can directly see the records the RemoteSink passes to the
2672
 
        # real sink.
2673
 
        class FakeRealSink:
2674
 
            def __init__(self):
2675
 
                self.records = []
2676
 
            def insert_stream(self, stream, src_format, resume_tokens):
2677
 
                for substream_kind, substream in stream:
2678
 
                    self.records.append(
2679
 
                        (substream_kind, [record.key for record in substream]))
2680
 
                return ['fake tokens'], ['fake missing keys']
2681
 
        fake_real_sink = FakeRealSink()
2682
 
        class FakeRealRepository:
2683
 
            def _get_sink(self):
2684
 
                return fake_real_sink
2685
 
            def is_in_write_group(self):
2686
 
                return False
2687
 
            def refresh_data(self):
2688
 
                return True
2689
 
        repo._real_repository = FakeRealRepository()
2690
 
        sink = repo._get_sink()
2691
 
        fmt = repository.format_registry.get_default()
2692
 
        stream = self.make_stream_with_inv_deltas(fmt)
2693
 
        resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
2694
 
        # Every record from the first inventory delta should have been sent to
2695
 
        # the VFS sink.
2696
 
        expected_records = [
2697
 
            ('inventory-deltas', [('rev2',), ('rev3',)]),
2698
 
            ('texts', [('some-rev', 'some-file')])]
2699
 
        self.assertEqual(expected_records, fake_real_sink.records)
2700
 
        # The return values from the real sink's insert_stream are propagated
2701
 
        # back to the original caller.
2702
 
        self.assertEqual(['fake tokens'], resume_tokens)
2703
 
        self.assertEqual(['fake missing keys'], missing_keys)
2704
 
        self.assertFinished(client)
2705
 
 
2706
 
    def make_stream_with_inv_deltas(self, fmt):
2707
 
        """Make a simple stream with an inventory delta followed by more
2708
 
        records and more substreams to test that all records and substreams
2709
 
        from that point on are used.
2710
 
 
2711
 
        This sends, in order:
2712
 
           * inventories substream: rev1, rev2, rev3.  rev2 and rev3 are
2713
 
             inventory-deltas.
2714
 
           * texts substream: (some-rev, some-file)
2715
 
        """
2716
 
        # Define a stream using generators so that it isn't rewindable.
2717
 
        inv = inventory.Inventory(revision_id='rev1')
2718
 
        inv.root.revision = 'rev1'
2719
 
        def stream_with_inv_delta():
2720
 
            yield ('inventories', inventories_substream())
2721
 
            yield ('inventory-deltas', inventory_delta_substream())
2722
 
            yield ('texts', [
2723
 
                versionedfile.FulltextContentFactory(
2724
 
                    ('some-rev', 'some-file'), (), None, 'content')])
2725
 
        def inventories_substream():
2726
 
            # An empty inventory fulltext.  This will be streamed normally.
2727
 
            text = fmt._serializer.write_inventory_to_string(inv)
2728
 
            yield versionedfile.FulltextContentFactory(
2729
 
                ('rev1',), (), None, text)
2730
 
        def inventory_delta_substream():
2731
 
            # An inventory delta.  This can't be streamed via this verb, so it
2732
 
            # will trigger a fallback to VFS insert_stream.
2733
 
            entry = inv.make_entry(
2734
 
                'directory', 'newdir', inv.root.file_id, 'newdir-id')
2735
 
            entry.revision = 'ghost'
2736
 
            delta = [(None, 'newdir', 'newdir-id', entry)]
2737
 
            serializer = inventory_delta.InventoryDeltaSerializer(
2738
 
                versioned_root=True, tree_references=False)
2739
 
            lines = serializer.delta_to_lines('rev1', 'rev2', delta)
2740
 
            yield versionedfile.ChunkedContentFactory(
2741
 
                ('rev2',), (('rev1',)), None, lines)
2742
 
            # Another delta.
2743
 
            lines = serializer.delta_to_lines('rev1', 'rev3', delta)
2744
 
            yield versionedfile.ChunkedContentFactory(
2745
 
                ('rev3',), (('rev1',)), None, lines)
2746
 
        return stream_with_inv_delta()
2747
 
 
2748
 
 
2749
 
class TestRepositoryInsertStream_1_19(TestRepositoryInsertStreamBase):
2750
 
 
2751
 
    def test_unlocked_repo(self):
2752
 
        transport_path = 'quack'
2753
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2754
 
        client.add_expected_call(
2755
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2756
 
            'success', ('ok',))
2757
 
        client.add_expected_call(
2758
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2759
 
            'success', ('ok',))
2760
 
        self.checkInsertEmptyStream(repo, client)
2761
 
 
2762
 
    def test_locked_repo_with_no_lock_token(self):
2763
 
        transport_path = 'quack'
2764
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2765
 
        client.add_expected_call(
2766
 
            'Repository.lock_write', ('quack/', ''),
2767
 
            'success', ('ok', ''))
2768
 
        client.add_expected_call(
2769
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2770
 
            'success', ('ok',))
2771
 
        client.add_expected_call(
2772
 
            'Repository.insert_stream_1.19', ('quack/', ''),
2773
 
            'success', ('ok',))
2774
 
        repo.lock_write()
2775
 
        self.checkInsertEmptyStream(repo, client)
2776
 
 
2777
 
    def test_locked_repo_with_lock_token(self):
2778
 
        transport_path = 'quack'
2779
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2780
 
        client.add_expected_call(
2781
 
            'Repository.lock_write', ('quack/', ''),
2782
 
            'success', ('ok', 'a token'))
2783
 
        client.add_expected_call(
2784
 
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
2785
 
            'success', ('ok',))
2786
 
        client.add_expected_call(
2787
 
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
2788
 
            'success', ('ok',))
2789
 
        repo.lock_write()
2790
 
        self.checkInsertEmptyStream(repo, client)
2791
 
 
2792
 
 
2793
1182
class TestRepositoryTarball(TestRemoteRepository):
2794
1183
 
2795
1184
    # This is a canned tarball reponse we can validate against
2829
1218
    """RemoteRepository.copy_content_into optimizations"""
2830
1219
 
2831
1220
    def test_copy_content_remote_to_local(self):
2832
 
        self.transport_server = test_server.SmartTCPServer_for_testing
 
1221
        self.transport_server = server.SmartTCPServer_for_testing
2833
1222
        src_repo = self.make_repository('repo1')
2834
1223
        src_repo = repository.Repository.open(self.get_url('repo1'))
2835
1224
        # At the moment the tarball-based copy_content_into can't write back
2844
1233
        src_repo.copy_content_into(dest_repo)
2845
1234
 
2846
1235
 
2847
 
class _StubRealPackRepository(object):
2848
 
 
2849
 
    def __init__(self, calls):
2850
 
        self.calls = calls
2851
 
        self._pack_collection = _StubPackCollection(calls)
2852
 
 
2853
 
    def is_in_write_group(self):
2854
 
        return False
2855
 
 
2856
 
    def refresh_data(self):
2857
 
        self.calls.append(('pack collection reload_pack_names',))
2858
 
 
2859
 
 
2860
 
class _StubPackCollection(object):
2861
 
 
2862
 
    def __init__(self, calls):
2863
 
        self.calls = calls
2864
 
 
2865
 
    def autopack(self):
2866
 
        self.calls.append(('pack collection autopack',))
2867
 
 
2868
 
 
2869
 
class TestRemotePackRepositoryAutoPack(TestRemoteRepository):
2870
 
    """Tests for RemoteRepository.autopack implementation."""
2871
 
 
2872
 
    def test_ok(self):
2873
 
        """When the server returns 'ok' and there's no _real_repository, then
2874
 
        nothing else happens: the autopack method is done.
2875
 
        """
2876
 
        transport_path = 'quack'
2877
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2878
 
        client.add_expected_call(
2879
 
            'PackRepository.autopack', ('quack/',), 'success', ('ok',))
2880
 
        repo.autopack()
2881
 
        self.assertFinished(client)
2882
 
 
2883
 
    def test_ok_with_real_repo(self):
2884
 
        """When the server returns 'ok' and there is a _real_repository, then
2885
 
        the _real_repository's reload_pack_name's method will be called.
2886
 
        """
2887
 
        transport_path = 'quack'
2888
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2889
 
        client.add_expected_call(
2890
 
            'PackRepository.autopack', ('quack/',),
2891
 
            'success', ('ok',))
2892
 
        repo._real_repository = _StubRealPackRepository(client._calls)
2893
 
        repo.autopack()
2894
 
        self.assertEqual(
2895
 
            [('call', 'PackRepository.autopack', ('quack/',)),
2896
 
             ('pack collection reload_pack_names',)],
2897
 
            client._calls)
2898
 
 
2899
 
    def test_backwards_compatibility(self):
2900
 
        """If the server does not recognise the PackRepository.autopack verb,
2901
 
        fallback to the real_repository's implementation.
2902
 
        """
2903
 
        transport_path = 'quack'
2904
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2905
 
        client.add_unknown_method_response('PackRepository.autopack')
2906
 
        def stub_ensure_real():
2907
 
            client._calls.append(('_ensure_real',))
2908
 
            repo._real_repository = _StubRealPackRepository(client._calls)
2909
 
        repo._ensure_real = stub_ensure_real
2910
 
        repo.autopack()
2911
 
        self.assertEqual(
2912
 
            [('call', 'PackRepository.autopack', ('quack/',)),
2913
 
             ('_ensure_real',),
2914
 
             ('pack collection autopack',)],
2915
 
            client._calls)
2916
 
 
2917
 
    def test_oom_error_reporting(self):
2918
 
        """An out-of-memory condition on the server is reported clearly"""
2919
 
        transport_path = 'quack'
2920
 
        repo, client = self.setup_fake_client_and_repository(transport_path)
2921
 
        client.add_expected_call(
2922
 
            'PackRepository.autopack', ('quack/',),
2923
 
            'error', ('MemoryError',))
2924
 
        err = self.assertRaises(errors.BzrError, repo.autopack)
2925
 
        self.assertContainsRe(str(err), "^remote server out of mem")
2926
 
 
2927
 
 
2928
1236
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
2929
1237
    """Base class for unit tests for bzrlib.remote._translate_error."""
2930
1238
 
2958
1266
                **context)
2959
1267
        return translated_error
2960
1268
 
2961
 
 
 
1269
    
2962
1270
class TestErrorTranslationSuccess(TestErrorTranslationBase):
2963
1271
    """Unit tests for bzrlib.remote._translate_error.
2964
 
 
 
1272
    
2965
1273
    Given an ErrorFromSmartServer (which has an error tuple from a smart
2966
1274
    server) and some context, _translate_error raises more specific errors from
2967
1275
    bzrlib.errors.
2993
1301
        expected_error = errors.NotBranchError(path=bzrdir.root_transport.base)
2994
1302
        self.assertEqual(expected_error, translated_error)
2995
1303
 
2996
 
    def test_nobranch_one_arg(self):
2997
 
        bzrdir = self.make_bzrdir('')
2998
 
        translated_error = self.translateTuple(
2999
 
            ('nobranch', 'extra detail'), bzrdir=bzrdir)
3000
 
        expected_error = errors.NotBranchError(
3001
 
            path=bzrdir.root_transport.base,
3002
 
            detail='extra detail')
3003
 
        self.assertEqual(expected_error, translated_error)
3004
 
 
3005
 
    def test_norepository(self):
3006
 
        bzrdir = self.make_bzrdir('')
3007
 
        translated_error = self.translateTuple(('norepository',),
3008
 
            bzrdir=bzrdir)
3009
 
        expected_error = errors.NoRepositoryPresent(bzrdir)
3010
 
        self.assertEqual(expected_error, translated_error)
3011
 
 
3012
1304
    def test_LockContention(self):
3013
1305
        translated_error = self.translateTuple(('LockContention',))
3014
1306
        expected_error = errors.LockContention('(remote lock)')
3042
1334
        expected_error = errors.DivergedBranches(branch, other_branch)
3043
1335
        self.assertEqual(expected_error, translated_error)
3044
1336
 
3045
 
    def test_NotStacked(self):
3046
 
        branch = self.make_branch('')
3047
 
        translated_error = self.translateTuple(('NotStacked',), branch=branch)
3048
 
        expected_error = errors.NotStacked(branch)
3049
 
        self.assertEqual(expected_error, translated_error)
3050
 
 
3051
 
    def test_ReadError_no_args(self):
3052
 
        path = 'a path'
3053
 
        translated_error = self.translateTuple(('ReadError',), path=path)
3054
 
        expected_error = errors.ReadError(path)
3055
 
        self.assertEqual(expected_error, translated_error)
3056
 
 
3057
 
    def test_ReadError(self):
3058
 
        path = 'a path'
3059
 
        translated_error = self.translateTuple(('ReadError', path))
3060
 
        expected_error = errors.ReadError(path)
3061
 
        self.assertEqual(expected_error, translated_error)
3062
 
 
3063
 
    def test_IncompatibleRepositories(self):
3064
 
        translated_error = self.translateTuple(('IncompatibleRepositories',
3065
 
            "repo1", "repo2", "details here"))
3066
 
        expected_error = errors.IncompatibleRepositories("repo1", "repo2",
3067
 
            "details here")
3068
 
        self.assertEqual(expected_error, translated_error)
3069
 
 
3070
 
    def test_PermissionDenied_no_args(self):
3071
 
        path = 'a path'
3072
 
        translated_error = self.translateTuple(('PermissionDenied',),
3073
 
            path=path)
3074
 
        expected_error = errors.PermissionDenied(path)
3075
 
        self.assertEqual(expected_error, translated_error)
3076
 
 
3077
 
    def test_PermissionDenied_one_arg(self):
3078
 
        path = 'a path'
3079
 
        translated_error = self.translateTuple(('PermissionDenied', path))
3080
 
        expected_error = errors.PermissionDenied(path)
3081
 
        self.assertEqual(expected_error, translated_error)
3082
 
 
3083
 
    def test_PermissionDenied_one_arg_and_context(self):
3084
 
        """Given a choice between a path from the local context and a path on
3085
 
        the wire, _translate_error prefers the path from the local context.
3086
 
        """
3087
 
        local_path = 'local path'
3088
 
        remote_path = 'remote path'
3089
 
        translated_error = self.translateTuple(
3090
 
            ('PermissionDenied', remote_path), path=local_path)
3091
 
        expected_error = errors.PermissionDenied(local_path)
3092
 
        self.assertEqual(expected_error, translated_error)
3093
 
 
3094
 
    def test_PermissionDenied_two_args(self):
3095
 
        path = 'a path'
3096
 
        extra = 'a string with extra info'
3097
 
        translated_error = self.translateTuple(
3098
 
            ('PermissionDenied', path, extra))
3099
 
        expected_error = errors.PermissionDenied(path, extra)
3100
 
        self.assertEqual(expected_error, translated_error)
3101
 
 
3102
 
    # GZ 2011-03-02: TODO test for PermissionDenied with non-ascii 'extra'
3103
 
 
3104
 
    def test_NoSuchFile_context_path(self):
3105
 
        local_path = "local path"
3106
 
        translated_error = self.translateTuple(('ReadError', "remote path"),
3107
 
            path=local_path)
3108
 
        expected_error = errors.ReadError(local_path)
3109
 
        self.assertEqual(expected_error, translated_error)
3110
 
 
3111
 
    def test_NoSuchFile_without_context(self):
3112
 
        remote_path = "remote path"
3113
 
        translated_error = self.translateTuple(('ReadError', remote_path))
3114
 
        expected_error = errors.ReadError(remote_path)
3115
 
        self.assertEqual(expected_error, translated_error)
3116
 
 
3117
 
    def test_ReadOnlyError(self):
3118
 
        translated_error = self.translateTuple(('ReadOnlyError',))
3119
 
        expected_error = errors.TransportNotPossible("readonly transport")
3120
 
        self.assertEqual(expected_error, translated_error)
3121
 
 
3122
 
    def test_MemoryError(self):
3123
 
        translated_error = self.translateTuple(('MemoryError',))
3124
 
        self.assertStartsWith(str(translated_error),
3125
 
            "remote server out of memory")
3126
 
 
3127
 
    def test_generic_IndexError_no_classname(self):
3128
 
        err = errors.ErrorFromSmartServer(('error', "list index out of range"))
3129
 
        translated_error = self.translateErrorFromSmartServer(err)
3130
 
        expected_error = errors.UnknownErrorFromSmartServer(err)
3131
 
        self.assertEqual(expected_error, translated_error)
3132
 
 
3133
 
    # GZ 2011-03-02: TODO test generic non-ascii error string
3134
 
 
3135
 
    def test_generic_KeyError(self):
3136
 
        err = errors.ErrorFromSmartServer(('error', 'KeyError', "1"))
3137
 
        translated_error = self.translateErrorFromSmartServer(err)
3138
 
        expected_error = errors.UnknownErrorFromSmartServer(err)
3139
 
        self.assertEqual(expected_error, translated_error)
3140
 
 
3141
1337
 
3142
1338
class TestErrorTranslationRobustness(TestErrorTranslationBase):
3143
1339
    """Unit tests for bzrlib.remote._translate_error's robustness.
3144
 
 
 
1340
    
3145
1341
    TestErrorTranslationSuccess is for cases where _translate_error can
3146
1342
    translate successfully.  This class about how _translate_err behaves when
3147
1343
    it fails to translate: it re-raises the original error.
3173
1369
        # In addition to re-raising ErrorFromSmartServer, some debug info has
3174
1370
        # been muttered to the log file for developer to look at.
3175
1371
        self.assertContainsRe(
3176
 
            self.get_log(),
 
1372
            self._get_log(keep_log_file=True),
3177
1373
            "Missing key 'branch' in context")
3178
 
 
3179
 
    def test_path_missing(self):
3180
 
        """Some translations (PermissionDenied, ReadError) can determine the
3181
 
        'path' variable from either the wire or the local context.  If neither
3182
 
        has it, then an error is raised.
3183
 
        """
3184
 
        error_tuple = ('ReadError',)
3185
 
        server_error = errors.ErrorFromSmartServer(error_tuple)
3186
 
        translated_error = self.translateErrorFromSmartServer(server_error)
3187
 
        self.assertEqual(server_error, translated_error)
3188
 
        # In addition to re-raising ErrorFromSmartServer, some debug info has
3189
 
        # been muttered to the log file for developer to look at.
3190
 
        self.assertContainsRe(self.get_log(), "Missing key 'path' in context")
3191
 
 
3192
 
 
3193
 
class TestStacking(tests.TestCaseWithTransport):
3194
 
    """Tests for operations on stacked remote repositories.
3195
 
 
3196
 
    The underlying format type must support stacking.
3197
 
    """
3198
 
 
3199
 
    def test_access_stacked_remote(self):
3200
 
        # based on <http://launchpad.net/bugs/261315>
3201
 
        # make a branch stacked on another repository containing an empty
3202
 
        # revision, then open it over hpss - we should be able to see that
3203
 
        # revision.
3204
 
        base_transport = self.get_transport()
3205
 
        base_builder = self.make_branch_builder('base', format='1.9')
3206
 
        base_builder.start_series()
3207
 
        base_revid = base_builder.build_snapshot('rev-id', None,
3208
 
            [('add', ('', None, 'directory', None))],
3209
 
            'message')
3210
 
        base_builder.finish_series()
3211
 
        stacked_branch = self.make_branch('stacked', format='1.9')
3212
 
        stacked_branch.set_stacked_on_url('../base')
3213
 
        # start a server looking at this
3214
 
        smart_server = test_server.SmartTCPServer_for_testing()
3215
 
        self.start_server(smart_server)
3216
 
        remote_bzrdir = BzrDir.open(smart_server.get_url() + '/stacked')
3217
 
        # can get its branch and repository
3218
 
        remote_branch = remote_bzrdir.open_branch()
3219
 
        remote_repo = remote_branch.repository
3220
 
        remote_repo.lock_read()
3221
 
        try:
3222
 
            # it should have an appropriate fallback repository, which should also
3223
 
            # be a RemoteRepository
3224
 
            self.assertLength(1, remote_repo._fallback_repositories)
3225
 
            self.assertIsInstance(remote_repo._fallback_repositories[0],
3226
 
                RemoteRepository)
3227
 
            # and it has the revision committed to the underlying repository;
3228
 
            # these have varying implementations so we try several of them
3229
 
            self.assertTrue(remote_repo.has_revisions([base_revid]))
3230
 
            self.assertTrue(remote_repo.has_revision(base_revid))
3231
 
            self.assertEqual(remote_repo.get_revision(base_revid).message,
3232
 
                'message')
3233
 
        finally:
3234
 
            remote_repo.unlock()
3235
 
 
3236
 
    def prepare_stacked_remote_branch(self):
3237
 
        """Get stacked_upon and stacked branches with content in each."""
3238
 
        self.setup_smart_server_with_call_log()
3239
 
        tree1 = self.make_branch_and_tree('tree1', format='1.9')
3240
 
        tree1.commit('rev1', rev_id='rev1')
3241
 
        tree2 = tree1.branch.bzrdir.sprout('tree2', stacked=True
3242
 
            ).open_workingtree()
3243
 
        local_tree = tree2.branch.create_checkout('local')
3244
 
        local_tree.commit('local changes make me feel good.')
3245
 
        branch2 = Branch.open(self.get_url('tree2'))
3246
 
        branch2.lock_read()
3247
 
        self.addCleanup(branch2.unlock)
3248
 
        return tree1.branch, branch2
3249
 
 
3250
 
    def test_stacked_get_parent_map(self):
3251
 
        # the public implementation of get_parent_map obeys stacking
3252
 
        _, branch = self.prepare_stacked_remote_branch()
3253
 
        repo = branch.repository
3254
 
        self.assertEqual(['rev1'], repo.get_parent_map(['rev1']).keys())
3255
 
 
3256
 
    def test_unstacked_get_parent_map(self):
3257
 
        # _unstacked_provider.get_parent_map ignores stacking
3258
 
        _, branch = self.prepare_stacked_remote_branch()
3259
 
        provider = branch.repository._unstacked_provider
3260
 
        self.assertEqual([], provider.get_parent_map(['rev1']).keys())
3261
 
 
3262
 
    def fetch_stream_to_rev_order(self, stream):
3263
 
        result = []
3264
 
        for kind, substream in stream:
3265
 
            if not kind == 'revisions':
3266
 
                list(substream)
3267
 
            else:
3268
 
                for content in substream:
3269
 
                    result.append(content.key[-1])
3270
 
        return result
3271
 
 
3272
 
    def get_ordered_revs(self, format, order, branch_factory=None):
3273
 
        """Get a list of the revisions in a stream to format format.
3274
 
 
3275
 
        :param format: The format of the target.
3276
 
        :param order: the order that target should have requested.
3277
 
        :param branch_factory: A callable to create a trunk and stacked branch
3278
 
            to fetch from. If none, self.prepare_stacked_remote_branch is used.
3279
 
        :result: The revision ids in the stream, in the order seen,
3280
 
            the topological order of revisions in the source.
3281
 
        """
3282
 
        unordered_format = bzrdir.format_registry.get(format)()
3283
 
        target_repository_format = unordered_format.repository_format
3284
 
        # Cross check
3285
 
        self.assertEqual(order, target_repository_format._fetch_order)
3286
 
        if branch_factory is None:
3287
 
            branch_factory = self.prepare_stacked_remote_branch
3288
 
        _, stacked = branch_factory()
3289
 
        source = stacked.repository._get_source(target_repository_format)
3290
 
        tip = stacked.last_revision()
3291
 
        stacked.repository._ensure_real()
3292
 
        graph = stacked.repository.get_graph()
3293
 
        revs = [r for (r,ps) in graph.iter_ancestry([tip])
3294
 
                if r != NULL_REVISION]
3295
 
        revs.reverse()
3296
 
        search = _mod_graph.PendingAncestryResult([tip], stacked.repository)
3297
 
        self.reset_smart_call_log()
3298
 
        stream = source.get_stream(search)
3299
 
        # We trust that if a revision is in the stream the rest of the new
3300
 
        # content for it is too, as per our main fetch tests; here we are
3301
 
        # checking that the revisions are actually included at all, and their
3302
 
        # order.
3303
 
        return self.fetch_stream_to_rev_order(stream), revs
3304
 
 
3305
 
    def test_stacked_get_stream_unordered(self):
3306
 
        # Repository._get_source.get_stream() from a stacked repository with
3307
 
        # unordered yields the full data from both stacked and stacked upon
3308
 
        # sources.
3309
 
        rev_ord, expected_revs = self.get_ordered_revs('1.9', 'unordered')
3310
 
        self.assertEqual(set(expected_revs), set(rev_ord))
3311
 
        # Getting unordered results should have made a streaming data request
3312
 
        # from the server, then one from the backing branch.
3313
 
        self.assertLength(2, self.hpss_calls)
3314
 
 
3315
 
    def test_stacked_on_stacked_get_stream_unordered(self):
3316
 
        # Repository._get_source.get_stream() from a stacked repository which
3317
 
        # is itself stacked yields the full data from all three sources.
3318
 
        def make_stacked_stacked():
3319
 
            _, stacked = self.prepare_stacked_remote_branch()
3320
 
            tree = stacked.bzrdir.sprout('tree3', stacked=True
3321
 
                ).open_workingtree()
3322
 
            local_tree = tree.branch.create_checkout('local-tree3')
3323
 
            local_tree.commit('more local changes are better')
3324
 
            branch = Branch.open(self.get_url('tree3'))
3325
 
            branch.lock_read()
3326
 
            self.addCleanup(branch.unlock)
3327
 
            return None, branch
3328
 
        rev_ord, expected_revs = self.get_ordered_revs('1.9', 'unordered',
3329
 
            branch_factory=make_stacked_stacked)
3330
 
        self.assertEqual(set(expected_revs), set(rev_ord))
3331
 
        # Getting unordered results should have made a streaming data request
3332
 
        # from the server, and one from each backing repo
3333
 
        self.assertLength(3, self.hpss_calls)
3334
 
 
3335
 
    def test_stacked_get_stream_topological(self):
3336
 
        # Repository._get_source.get_stream() from a stacked repository with
3337
 
        # topological sorting yields the full data from both stacked and
3338
 
        # stacked upon sources in topological order.
3339
 
        rev_ord, expected_revs = self.get_ordered_revs('knit', 'topological')
3340
 
        self.assertEqual(expected_revs, rev_ord)
3341
 
        # Getting topological sort requires VFS calls still - one of which is
3342
 
        # pushing up from the bound branch.
3343
 
        self.assertLength(14, self.hpss_calls)
3344
 
 
3345
 
    def test_stacked_get_stream_groupcompress(self):
3346
 
        # Repository._get_source.get_stream() from a stacked repository with
3347
 
        # groupcompress sorting yields the full data from both stacked and
3348
 
        # stacked upon sources in groupcompress order.
3349
 
        raise tests.TestSkipped('No groupcompress ordered format available')
3350
 
        rev_ord, expected_revs = self.get_ordered_revs('dev5', 'groupcompress')
3351
 
        self.assertEqual(expected_revs, reversed(rev_ord))
3352
 
        # Getting unordered results should have made a streaming data request
3353
 
        # from the backing branch, and one from the stacked on branch.
3354
 
        self.assertLength(2, self.hpss_calls)
3355
 
 
3356
 
    def test_stacked_pull_more_than_stacking_has_bug_360791(self):
3357
 
        # When pulling some fixed amount of content that is more than the
3358
 
        # source has (because some is coming from a fallback branch, no error
3359
 
        # should be received. This was reported as bug 360791.
3360
 
        # Need three branches: a trunk, a stacked branch, and a preexisting
3361
 
        # branch pulling content from stacked and trunk.
3362
 
        self.setup_smart_server_with_call_log()
3363
 
        trunk = self.make_branch_and_tree('trunk', format="1.9-rich-root")
3364
 
        r1 = trunk.commit('start')
3365
 
        stacked_branch = trunk.branch.create_clone_on_transport(
3366
 
            self.get_transport('stacked'), stacked_on=trunk.branch.base)
3367
 
        local = self.make_branch('local', format='1.9-rich-root')
3368
 
        local.repository.fetch(stacked_branch.repository,
3369
 
            stacked_branch.last_revision())
3370
 
 
3371
 
 
3372
 
class TestRemoteBranchEffort(tests.TestCaseWithTransport):
3373
 
 
3374
 
    def setUp(self):
3375
 
        super(TestRemoteBranchEffort, self).setUp()
3376
 
        # Create a smart server that publishes whatever the backing VFS server
3377
 
        # does.
3378
 
        self.smart_server = test_server.SmartTCPServer_for_testing()
3379
 
        self.start_server(self.smart_server, self.get_server())
3380
 
        # Log all HPSS calls into self.hpss_calls.
3381
 
        _SmartClient.hooks.install_named_hook(
3382
 
            'call', self.capture_hpss_call, None)
3383
 
        self.hpss_calls = []
3384
 
 
3385
 
    def capture_hpss_call(self, params):
3386
 
        self.hpss_calls.append(params.method)
3387
 
 
3388
 
    def test_copy_content_into_avoids_revision_history(self):
3389
 
        local = self.make_branch('local')
3390
 
        builder = self.make_branch_builder('remote')
3391
 
        builder.build_commit(message="Commit.")
3392
 
        remote_branch_url = self.smart_server.get_url() + 'remote'
3393
 
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3394
 
        local.repository.fetch(remote_branch.repository)
3395
 
        self.hpss_calls = []
3396
 
        remote_branch.copy_content_into(local)
3397
 
        self.assertFalse('Branch.revision_history' in self.hpss_calls)
3398
 
 
3399
 
    def test_fetch_everything_needs_just_one_call(self):
3400
 
        local = self.make_branch('local')
3401
 
        builder = self.make_branch_builder('remote')
3402
 
        builder.build_commit(message="Commit.")
3403
 
        remote_branch_url = self.smart_server.get_url() + 'remote'
3404
 
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3405
 
        self.hpss_calls = []
3406
 
        local.repository.fetch(
3407
 
            remote_branch.repository,
3408
 
            fetch_spec=_mod_graph.EverythingResult(remote_branch.repository))
3409
 
        self.assertEqual(['Repository.get_stream_1.19'], self.hpss_calls)
3410
 
 
3411
 
    def override_verb(self, verb_name, verb):
3412
 
        request_handlers = request.request_handlers
3413
 
        orig_verb = request_handlers.get(verb_name)
3414
 
        request_handlers.register(verb_name, verb, override_existing=True)
3415
 
        self.addCleanup(request_handlers.register, verb_name, orig_verb,
3416
 
                override_existing=True)
3417
 
 
3418
 
    def test_fetch_everything_backwards_compat(self):
3419
 
        """Can fetch with EverythingResult even with pre 2.4 servers.
3420
1374
        
3421
 
        Pre-2.4 do not support 'everything' searches with the
3422
 
        Repository.get_stream_1.19 verb.
3423
 
        """
3424
 
        verb_log = []
3425
 
        class OldGetStreamVerb(SmartServerRepositoryGetStream_1_19):
3426
 
            """A version of the Repository.get_stream_1.19 verb patched to
3427
 
            reject 'everything' searches the way 2.3 and earlier do.
3428
 
            """
3429
 
            def recreate_search(self, repository, search_bytes,
3430
 
                                discard_excess=False):
3431
 
                verb_log.append(search_bytes.split('\n', 1)[0])
3432
 
                if search_bytes == 'everything':
3433
 
                    return (None,
3434
 
                            request.FailedSmartServerResponse(('BadSearch',)))
3435
 
                return super(OldGetStreamVerb,
3436
 
                        self).recreate_search(repository, search_bytes,
3437
 
                            discard_excess=discard_excess)
3438
 
        self.override_verb('Repository.get_stream_1.19', OldGetStreamVerb)
3439
 
        local = self.make_branch('local')
3440
 
        builder = self.make_branch_builder('remote')
3441
 
        builder.build_commit(message="Commit.")
3442
 
        remote_branch_url = self.smart_server.get_url() + 'remote'
3443
 
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
3444
 
        self.hpss_calls = []
3445
 
        local.repository.fetch(
3446
 
            remote_branch.repository,
3447
 
            fetch_spec=_mod_graph.EverythingResult(remote_branch.repository))
3448
 
        # make sure the overridden verb was used
3449
 
        self.assertLength(1, verb_log)
3450
 
        # more than one HPSS call is needed, but because it's a VFS callback
3451
 
        # its hard to predict exactly how many.
3452
 
        self.assertTrue(len(self.hpss_calls) > 1)
3453
 
 
3454
 
 
3455
 
class TestUpdateBoundBranchWithModifiedBoundLocation(
3456
 
    tests.TestCaseWithTransport):
3457
 
    """Ensure correct handling of bound_location modifications.
3458
 
 
3459
 
    This is tested against a smart server as http://pad.lv/786980 was about a
3460
 
    ReadOnlyError (write attempt during a read-only transaction) which can only
3461
 
    happen in this context.
3462
 
    """
3463
 
 
3464
 
    def setUp(self):
3465
 
        super(TestUpdateBoundBranchWithModifiedBoundLocation, self).setUp()
3466
 
        self.transport_server = test_server.SmartTCPServer_for_testing
3467
 
 
3468
 
    def make_master_and_checkout(self, master_name, checkout_name):
3469
 
        # Create the master branch and its associated checkout
3470
 
        self.master = self.make_branch_and_tree(master_name)
3471
 
        self.checkout = self.master.branch.create_checkout(checkout_name)
3472
 
        # Modify the master branch so there is something to update
3473
 
        self.master.commit('add stuff')
3474
 
        self.last_revid = self.master.commit('even more stuff')
3475
 
        self.bound_location = self.checkout.branch.get_bound_location()
3476
 
 
3477
 
    def assertUpdateSucceeds(self, new_location):
3478
 
        self.checkout.branch.set_bound_location(new_location)
3479
 
        self.checkout.update()
3480
 
        self.assertEquals(self.last_revid, self.checkout.last_revision())
3481
 
 
3482
 
    def test_without_final_slash(self):
3483
 
        self.make_master_and_checkout('master', 'checkout')
3484
 
        # For unclear reasons some users have a bound_location without a final
3485
 
        # '/', simulate that by forcing such a value
3486
 
        self.assertEndsWith(self.bound_location, '/')
3487
 
        self.assertUpdateSucceeds(self.bound_location.rstrip('/'))
3488
 
 
3489
 
    def test_plus_sign(self):
3490
 
        self.make_master_and_checkout('+master', 'checkout')
3491
 
        self.assertUpdateSucceeds(self.bound_location.replace('%2B', '+', 1))
3492
 
 
3493
 
    def test_tilda(self):
3494
 
        # Embed ~ in the middle of the path just to avoid any $HOME
3495
 
        # interpretation
3496
 
        self.make_master_and_checkout('mas~ter', 'checkout')
3497
 
        self.assertUpdateSucceeds(self.bound_location.replace('%2E', '~', 1))