/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5557.1.7 by John Arbash Meinel
Merge in the bzr.dev 5582
1
# Copyright (C) 2006-2011 Canonical Ltd
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
16
17
"""Tests for remote bzrdir/branch/repo/etc
18
19
These are proxy objects which act on remote objects by sending messages
20
through a smart client.  The proxies are to be created when attempting to open
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
21
the object given a transport that supports smartserver rpc operations.
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
22
23
These tests correspond to tests.test_smart, which exercises the server side.
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
24
"""
25
3211.5.2 by Robert Collins
Change RemoteRepository.get_parent_map to use bz2 not gzip for compression.
26
import bz2
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
27
from cStringIO import StringIO
6280.9.4 by Jelmer Vernooij
use zlib instead.
28
import zlib
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
29
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
30
from bzrlib import (
4792.1.1 by Andrew Bennetts
Show real branch/repo format description in 'info -v' over HPSS.
31
    branch,
3834.3.2 by Andrew Bennetts
Preserve BzrBranch5's _synchronize_history code without affecting Branch or BzrBranch7; add effort test for RemoteBranch.copy_content_into.
32
    bzrdir,
3777.1.3 by Aaron Bentley
Use SSH default username from authentication.conf
33
    config,
5363.2.9 by Jelmer Vernooij
Fix some tests.
34
    controldir,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
35
    errors,
5972.3.16 by Jelmer Vernooij
Rename import.
36
    graph as _mod_graph,
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
37
    inventory,
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
38
    inventory_delta,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
39
    remote,
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
40
    repository,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
41
    tests,
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
42
    transport,
4190.1.4 by Robert Collins
Cache ghosts when we can get them from a RemoteRepository in get_parent_map.
43
    treebuilder,
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
44
    versionedfile,
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
45
    vf_search,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
46
    )
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
47
from bzrlib.branch import Branch
5363.2.9 by Jelmer Vernooij
Fix some tests.
48
from bzrlib.bzrdir import (
49
    BzrDir,
50
    BzrDirFormat,
51
    RemoteBzrProber,
52
    )
6280.9.1 by Jelmer Vernooij
Add remote side of Repository.iter_revisions.
53
from bzrlib.chk_serializer import chk_bencode_serializer
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
54
from bzrlib.remote import (
55
    RemoteBranch,
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
56
    RemoteBranchFormat,
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
57
    RemoteBzrDir,
5712.3.17 by Jelmer Vernooij
more fixes.
58
    RemoteBzrDirFormat,
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
59
    RemoteRepository,
4183.5.1 by Robert Collins
Add RepositoryFormat.fast_deltas to signal fast delta creation.
60
    RemoteRepositoryFormat,
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
61
    )
5757.1.7 by Jelmer Vernooij
Fix more imports.
62
from bzrlib.repofmt import groupcompress_repo, knitpack_repo
6280.9.1 by Jelmer Vernooij
Add remote side of Repository.iter_revisions.
63
from bzrlib.revision import (
64
    NULL_REVISION,
65
    Revision,
66
    )
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
67
from bzrlib.smart import medium, request
2018.5.159 by Andrew Bennetts
Rename SmartClient to _SmartClient.
68
from bzrlib.smart.client import _SmartClient
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
69
from bzrlib.smart.repository import (
70
    SmartServerRepositoryGetParentMap,
71
    SmartServerRepositoryGetStream_1_19,
72
    )
6165.4.7 by Jelmer Vernooij
More fixes.
73
from bzrlib.symbol_versioning import deprecated_in
4118.1.1 by Andrew Bennetts
Fix performance regression (many small round-trips) when pushing to a remote pack, and tidy the tests.
74
from bzrlib.tests import (
5017.3.28 by Vincent Ladeuil
selftest -s bt.test_remote passing
75
    test_server,
4118.1.1 by Andrew Bennetts
Fix performance regression (many small round-trips) when pushing to a remote pack, and tidy the tests.
76
    )
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
77
from bzrlib.tests.scenarios import load_tests_apply_scenarios
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
78
from bzrlib.transport.memory import MemoryTransport
3777.1.3 by Aaron Bentley
Use SSH default username from authentication.conf
79
from bzrlib.transport.remote import (
80
    RemoteTransport,
81
    RemoteSSHTransport,
82
    RemoteTCPTransport,
5579.3.1 by Jelmer Vernooij
Remove unused imports.
83
    )
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
84
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
85
86
load_tests = load_tests_apply_scenarios
87
88
89
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
90
91
    scenarios = [
4104.4.2 by Robert Collins
Fix test_source for 1.13 landing.
92
        ('HPSS-v2',
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
93
            {'transport_server': test_server.SmartTCPServer_for_testing_v2_only}),
4104.4.2 by Robert Collins
Fix test_source for 1.13 landing.
94
        ('HPSS-v3',
5559.2.2 by Martin Pool
Change to using standard load_tests_apply_scenarios.
95
            {'transport_server': test_server.SmartTCPServer_for_testing})]
96
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
97
98
    def setUp(self):
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
99
        super(BasicRemoteObjectTests, self).setUp()
100
        self.transport = self.get_transport()
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
101
        # make a branch that can be opened over the smart transport
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
102
        self.local_wt = BzrDir.create_standalone_workingtree('.')
4986.2.1 by Martin Pool
Remove tearDown in tests in favor of addCleanup
103
        self.addCleanup(self.transport.disconnect)
2018.5.171 by Andrew Bennetts
Disconnect RemoteTransports in some tests to avoid tripping up test_strace with leftover threads from previous tests.
104
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
105
    def test_create_remote_bzrdir(self):
5712.3.17 by Jelmer Vernooij
more fixes.
106
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
107
        self.assertIsInstance(b, BzrDir)
108
109
    def test_open_remote_branch(self):
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
110
        # open a standalone branch in the working directory
5712.3.17 by Jelmer Vernooij
more fixes.
111
        b = remote.RemoteBzrDir(self.transport, RemoteBzrDirFormat())
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
112
        branch = b.open_branch()
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
113
        self.assertIsInstance(branch, Branch)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
114
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
115
    def test_remote_repository(self):
116
        b = BzrDir.open_from_transport(self.transport)
117
        repo = b.open_repository()
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
118
        revid = u'\xc823123123'.encode('utf8')
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
119
        self.assertFalse(repo.has_revision(revid))
120
        self.local_wt.commit(message='test commit', rev_id=revid)
121
        self.assertTrue(repo.has_revision(revid))
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
122
123
    def test_remote_branch_revision_history(self):
124
        b = BzrDir.open_from_transport(self.transport).open_branch()
6165.4.7 by Jelmer Vernooij
More fixes.
125
        self.assertEqual([],
126
            self.applyDeprecated(deprecated_in((2, 5, 0)), b.revision_history))
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
127
        r1 = self.local_wt.commit('1st commit')
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
128
        r2 = self.local_wt.commit('1st commit', rev_id=u'\xc8'.encode('utf8'))
6165.4.7 by Jelmer Vernooij
More fixes.
129
        self.assertEqual([r1, r2],
130
            self.applyDeprecated(deprecated_in((2, 5, 0)), b.revision_history))
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
131
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
132
    def test_find_correct_format(self):
2018.5.20 by Andrew Bennetts
Move bzrlib/transport/smart/_smart.py to bzrlib/transport/remote.py and rename SmartTransport to RemoteTransport (Robert Collins, Andrew Bennetts)
133
        """Should open a RemoteBzrDir over a RemoteTransport"""
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
134
        fmt = BzrDirFormat.find_format(self.transport)
5363.2.9 by Jelmer Vernooij
Fix some tests.
135
        self.assertTrue(bzrdir.RemoteBzrProber
136
                        in controldir.ControlDirFormat._server_probers)
5712.3.17 by Jelmer Vernooij
more fixes.
137
        self.assertIsInstance(fmt, RemoteBzrDirFormat)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
138
139
    def test_open_detected_smart_format(self):
140
        fmt = BzrDirFormat.find_format(self.transport)
141
        d = fmt.open(self.transport)
142
        self.assertIsInstance(d, BzrDir)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
143
2477.1.1 by Martin Pool
Add RemoteBranch repr
144
    def test_remote_branch_repr(self):
145
        b = BzrDir.open_from_transport(self.transport).open_branch()
146
        self.assertStartsWith(str(b), 'RemoteBranch(')
147
4964.2.1 by Martin Pool
Add RemoteBzrDir repr
148
    def test_remote_bzrdir_repr(self):
149
        b = BzrDir.open_from_transport(self.transport)
150
        self.assertStartsWith(str(b), 'RemoteBzrDir(')
151
4103.2.2 by Andrew Bennetts
Fix RemoteBranchFormat.supports_stacking()
152
    def test_remote_branch_format_supports_stacking(self):
153
        t = self.transport
154
        self.make_branch('unstackable', format='pack-0.92')
155
        b = BzrDir.open_from_transport(t.clone('unstackable')).open_branch()
156
        self.assertFalse(b._format.supports_stacking())
157
        self.make_branch('stackable', format='1.9')
158
        b = BzrDir.open_from_transport(t.clone('stackable')).open_branch()
159
        self.assertTrue(b._format.supports_stacking())
160
4118.1.1 by Andrew Bennetts
Fix performance regression (many small round-trips) when pushing to a remote pack, and tidy the tests.
161
    def test_remote_repo_format_supports_external_references(self):
162
        t = self.transport
163
        bd = self.make_bzrdir('unstackable', format='pack-0.92')
164
        r = bd.create_repository()
165
        self.assertFalse(r._format.supports_external_lookups)
166
        r = BzrDir.open_from_transport(t.clone('unstackable')).open_repository()
167
        self.assertFalse(r._format.supports_external_lookups)
168
        bd = self.make_bzrdir('stackable', format='1.9')
169
        r = bd.create_repository()
170
        self.assertTrue(r._format.supports_external_lookups)
171
        r = BzrDir.open_from_transport(t.clone('stackable')).open_repository()
172
        self.assertTrue(r._format.supports_external_lookups)
173
4301.3.1 by Andrew Bennetts
Implement RemoteBranch.set_append_revisions_only.
174
    def test_remote_branch_set_append_revisions_only(self):
175
        # Make a format 1.9 branch, which supports append_revisions_only
176
        branch = self.make_branch('branch', format='1.9')
177
        config = branch.get_config()
178
        branch.set_append_revisions_only(True)
179
        self.assertEqual(
180
            'True', config.get_user_option('append_revisions_only'))
181
        branch.set_append_revisions_only(False)
182
        self.assertEqual(
183
            'False', config.get_user_option('append_revisions_only'))
184
185
    def test_remote_branch_set_append_revisions_only_upgrade_reqd(self):
186
        branch = self.make_branch('branch', format='knit')
187
        config = branch.get_config()
188
        self.assertRaises(
4301.3.3 by Andrew Bennetts
Move check onto base Branch class, and add a supports_set_append_revisions_only method to BranchFormat, as suggested by Robert.
189
            errors.UpgradeRequired, branch.set_append_revisions_only, True)
4301.3.1 by Andrew Bennetts
Implement RemoteBranch.set_append_revisions_only.
190
3691.2.4 by Martin Pool
Add FakeRemoteTransport to clarify test_remote
191
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
192
class FakeProtocol(object):
193
    """Lookalike SmartClientRequestProtocolOne allowing body reading tests."""
194
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
195
    def __init__(self, body, fake_client):
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
196
        self.body = body
197
        self._body_buffer = None
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
198
        self._fake_client = fake_client
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
199
200
    def read_body_bytes(self, count=-1):
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
201
        if self._body_buffer is None:
202
            self._body_buffer = StringIO(self.body)
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
203
        bytes = self._body_buffer.read(count)
204
        if self._body_buffer.tell() == len(self._body_buffer.getvalue()):
205
            self._fake_client.expecting_body = False
206
        return bytes
207
208
    def cancel_read_body(self):
209
        self._fake_client.expecting_body = False
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
210
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
211
    def read_streamed_body(self):
212
        return self.body
213
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
214
2018.5.159 by Andrew Bennetts
Rename SmartClient to _SmartClient.
215
class FakeClient(_SmartClient):
216
    """Lookalike for _SmartClient allowing testing."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
217
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
218
    def __init__(self, fake_medium_base='fake base'):
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
219
        """Create a FakeClient."""
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
220
        self.responses = []
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
221
        self._calls = []
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
222
        self.expecting_body = False
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
223
        # if non-None, this is the list of expected calls, with only the
224
        # method name and arguments included.  the body might be hard to
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
225
        # compute so is not included. If a call is None, that call can
226
        # be anything.
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
227
        self._expected_calls = None
3431.3.2 by Andrew Bennetts
Remove 'base' from _SmartClient entirely, now that the medium has it.
228
        _SmartClient.__init__(self, FakeMedium(self._calls, fake_medium_base))
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
229
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
230
    def add_expected_call(self, call_name, call_args, response_type,
231
        response_args, response_body=None):
232
        if self._expected_calls is None:
233
            self._expected_calls = []
234
        self._expected_calls.append((call_name, call_args))
3691.2.8 by Martin Pool
Update some test_remote tests for Branch.get_stacked_on_url and with clearer assertions
235
        self.responses.append((response_type, response_args, response_body))
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
236
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
237
    def add_success_response(self, *args):
238
        self.responses.append(('success', args, None))
239
240
    def add_success_response_with_body(self, body, *args):
241
        self.responses.append(('success', args, body))
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
242
        if self._expected_calls is not None:
243
            self._expected_calls.append(None)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
244
245
    def add_error_response(self, *args):
246
        self.responses.append(('error', args))
247
248
    def add_unknown_method_response(self, verb):
249
        self.responses.append(('unknown', verb))
250
4523.3.2 by Andrew Bennetts
Adjust according to Robert's review.
251
    def finished_test(self):
252
        if self._expected_calls:
253
            raise AssertionError("%r finished but was still expecting %r"
254
                % (self, self._expected_calls[0]))
255
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
256
    def _get_next_response(self):
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
257
        try:
258
            response_tuple = self.responses.pop(0)
259
        except IndexError, e:
260
            raise AssertionError("%r didn't expect any more calls"
261
                % (self,))
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
262
        if response_tuple[0] == 'unknown':
263
            raise errors.UnknownSmartMethod(response_tuple[1])
264
        elif response_tuple[0] == 'error':
265
            raise errors.ErrorFromSmartServer(response_tuple[1])
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
266
        return response_tuple
267
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
268
    def _check_call(self, method, args):
269
        if self._expected_calls is None:
270
            # the test should be updated to say what it expects
271
            return
272
        try:
273
            next_call = self._expected_calls.pop(0)
274
        except IndexError:
275
            raise AssertionError("%r didn't expect any more calls "
276
                "but got %r%r"
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
277
                % (self, method, args,))
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
278
        if next_call is None:
279
            return
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
280
        if method != next_call[0] or args != next_call[1]:
281
            raise AssertionError("%r expected %r%r "
282
                "but got %r%r"
3691.2.8 by Martin Pool
Update some test_remote tests for Branch.get_stacked_on_url and with clearer assertions
283
                % (self, next_call[0], next_call[1], method, args,))
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
284
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
285
    def call(self, method, *args):
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
286
        self._check_call(method, args)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
287
        self._calls.append(('call', method, args))
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
288
        return self._get_next_response()[1]
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
289
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
290
    def call_expecting_body(self, method, *args):
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
291
        self._check_call(method, args)
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
292
        self._calls.append(('call_expecting_body', method, args))
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
293
        result = self._get_next_response()
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
294
        self.expecting_body = True
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
295
        return result[1], FakeProtocol(result[2], self)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
296
4634.36.1 by Andrew Bennetts
Fix trivial bug in RemoteBranch._set_tags_bytes, and add some unit tests for it.
297
    def call_with_body_bytes(self, method, args, body):
298
        self._check_call(method, args)
299
        self._calls.append(('call_with_body_bytes', method, args, body))
300
        result = self._get_next_response()
301
        return result[1], FakeProtocol(result[2], self)
302
3184.1.10 by Robert Collins
Change the smart server verb for Repository.stream_revisions_chunked to use SearchResults as the request mechanism for downloads.
303
    def call_with_body_bytes_expecting_body(self, method, args, body):
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
304
        self._check_call(method, args)
3184.1.10 by Robert Collins
Change the smart server verb for Repository.stream_revisions_chunked to use SearchResults as the request mechanism for downloads.
305
        self._calls.append(('call_with_body_bytes_expecting_body', method,
306
            args, body))
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
307
        result = self._get_next_response()
3184.1.10 by Robert Collins
Change the smart server verb for Repository.stream_revisions_chunked to use SearchResults as the request mechanism for downloads.
308
        self.expecting_body = True
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
309
        return result[1], FakeProtocol(result[2], self)
3184.1.10 by Robert Collins
Change the smart server verb for Repository.stream_revisions_chunked to use SearchResults as the request mechanism for downloads.
310
3842.3.9 by Andrew Bennetts
Backing up the stream so that we can fallback correctly.
311
    def call_with_body_stream(self, args, stream):
312
        # Explicitly consume the stream before checking for an error, because
313
        # that's what happens a real medium.
314
        stream = list(stream)
315
        self._check_call(args[0], args[1:])
316
        self._calls.append(('call_with_body_stream', args[0], args[1:], stream))
4144.3.2 by Andrew Bennetts
Use Repository.insert_stream_locked if there is a lock_token for the remote repo.
317
        result = self._get_next_response()
4144.3.3 by Andrew Bennetts
Tweaks based on review from Robert.
318
        # The second value returned from call_with_body_stream is supposed to
319
        # be a response_handler object, but so far no tests depend on that.
320
        response_handler = None 
321
        return result[1], response_handler
3842.3.9 by Andrew Bennetts
Backing up the stream so that we can fallback correctly.
322
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
323
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
324
class FakeMedium(medium.SmartClientMedium):
3104.4.2 by Andrew Bennetts
All tests passing.
325
3431.3.1 by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media.
326
    def __init__(self, client_calls, base):
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
327
        medium.SmartClientMedium.__init__(self, base)
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
328
        self._client_calls = client_calls
3213.1.1 by Andrew Bennetts
Recover (by reconnecting) if the server turns out not to understand the new requests in 1.2 that send bodies.
329
330
    def disconnect(self):
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
331
        self._client_calls.append(('disconnect medium',))
3104.4.2 by Andrew Bennetts
All tests passing.
332
333
3192.2.1 by Andrew Bennetts
Don't transmit URL-escaped relpaths in the smart protocol, which is back to how things worked in bzr 1.1 and earlier.
334
class TestVfsHas(tests.TestCase):
335
336
    def test_unicode_path(self):
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
337
        client = FakeClient('/')
338
        client.add_success_response('yes',)
3192.2.1 by Andrew Bennetts
Don't transmit URL-escaped relpaths in the smart protocol, which is back to how things worked in bzr 1.1 and earlier.
339
        transport = RemoteTransport('bzr://localhost/', _client=client)
340
        filename = u'/hell\u00d8'.encode('utf8')
341
        result = transport.has(filename)
342
        self.assertEqual(
343
            [('call', 'has', (filename,))],
344
            client._calls)
345
        self.assertTrue(result)
346
347
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
348
class TestRemote(tests.TestCaseWithMemoryTransport):
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
349
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
350
    def get_branch_format(self):
351
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
352
        return reference_bzrdir_format.get_branch_format()
353
4053.1.2 by Robert Collins
Actually make this branch work.
354
    def get_repo_format(self):
355
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
356
        return reference_bzrdir_format.repository_format
357
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
358
    def assertFinished(self, fake_client):
359
        """Assert that all of a FakeClient's expected calls have occurred."""
4523.3.2 by Andrew Bennetts
Adjust according to Robert's review.
360
        fake_client.finished_test()
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
361
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
362
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
363
class Test_ClientMedium_remote_path_from_transport(tests.TestCase):
364
    """Tests for the behaviour of client_medium.remote_path_from_transport."""
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
365
366
    def assertRemotePath(self, expected, client_base, transport_base):
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
367
        """Assert that the result of
368
        SmartClientMedium.remote_path_from_transport is the expected value for
369
        a given client_base and transport_base.
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
370
        """
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
371
        client_medium = medium.SmartClientMedium(client_base)
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
372
        t = transport.get_transport(transport_base)
373
        result = client_medium.remote_path_from_transport(t)
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
374
        self.assertEqual(expected, result)
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
375
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
376
    def test_remote_path_from_transport(self):
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
377
        """SmartClientMedium.remote_path_from_transport calculates a URL for
378
        the given transport relative to the root of the client base URL.
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
379
        """
380
        self.assertRemotePath('xyz/', 'bzr://host/path', 'bzr://host/xyz')
381
        self.assertRemotePath(
382
            'path/xyz/', 'bzr://host/path', 'bzr://host/path/xyz')
383
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
384
    def assertRemotePathHTTP(self, expected, transport_base, relpath):
385
        """Assert that the result of
386
        HttpTransportBase.remote_path_from_transport is the expected value for
387
        a given transport_base and relpath of that transport.  (Note that
388
        HttpTransportBase is a subclass of SmartClientMedium)
389
        """
5273.1.7 by Vincent Ladeuil
No more use of the get_transport imported *symbol*, all uses are through
390
        base_transport = transport.get_transport(transport_base)
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
391
        client_medium = base_transport.get_smart_medium()
392
        cloned_transport = base_transport.clone(relpath)
393
        result = client_medium.remote_path_from_transport(cloned_transport)
394
        self.assertEqual(expected, result)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
395
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
396
    def test_remote_path_from_transport_http(self):
397
        """Remote paths for HTTP transports are calculated differently to other
398
        transports.  They are just relative to the client base, not the root
399
        directory of the host.
400
        """
401
        for scheme in ['http:', 'https:', 'bzr+http:', 'bzr+https:']:
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
402
            self.assertRemotePathHTTP(
403
                '../xyz/', scheme + '//host/path', '../xyz/')
404
            self.assertRemotePathHTTP(
405
                'xyz/', scheme + '//host/path', 'xyz/')
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
406
407
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
408
class Test_ClientMedium_remote_is_at_least(tests.TestCase):
409
    """Tests for the behaviour of client_medium.remote_is_at_least."""
410
411
    def test_initially_unlimited(self):
412
        """A fresh medium assumes that the remote side supports all
413
        versions.
414
        """
415
        client_medium = medium.SmartClientMedium('dummy base')
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
416
        self.assertFalse(client_medium._is_remote_before((99, 99)))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
417
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
418
    def test__remember_remote_is_before(self):
419
        """Calling _remember_remote_is_before ratchets down the known remote
420
        version.
421
        """
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
422
        client_medium = medium.SmartClientMedium('dummy base')
423
        # Mark the remote side as being less than 1.6.  The remote side may
424
        # still be 1.5.
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
425
        client_medium._remember_remote_is_before((1, 6))
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
426
        self.assertTrue(client_medium._is_remote_before((1, 6)))
427
        self.assertFalse(client_medium._is_remote_before((1, 5)))
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
428
        # Calling _remember_remote_is_before again with a lower value works.
429
        client_medium._remember_remote_is_before((1, 5))
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
430
        self.assertTrue(client_medium._is_remote_before((1, 5)))
4797.49.4 by Andrew Bennetts
Expand test a little further, and use consistent terminology in its comments.
431
        # If you call _remember_remote_is_before with a higher value it logs a
432
        # warning, and continues to remember the lower value.
4797.49.1 by Andrew Bennetts
First, fix _remember_remote_is_before to never raise AssertionError for what is a very minor bug.
433
        self.assertNotContainsRe(self.get_log(), '_remember_remote_is_before')
434
        client_medium._remember_remote_is_before((1, 9))
435
        self.assertContainsRe(self.get_log(), '_remember_remote_is_before')
4797.49.4 by Andrew Bennetts
Expand test a little further, and use consistent terminology in its comments.
436
        self.assertTrue(client_medium._is_remote_before((1, 5)))
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
437
438
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
439
class TestBzrDirCloningMetaDir(TestRemote):
440
441
    def test_backwards_compat(self):
442
        self.setup_smart_server_with_call_log()
443
        a_dir = self.make_bzrdir('.')
444
        self.reset_smart_call_log()
445
        verb = 'BzrDir.cloning_metadir'
446
        self.disable_verb(verb)
447
        format = a_dir.cloning_metadir()
448
        call_count = len([call for call in self.hpss_calls if
4070.3.1 by Robert Collins
Alter branch sprouting with an alternate fix for stacked branches that does not require multiple copy_content_into and set_parent calls, reducing IO and round trips.
449
            call.call.method == verb])
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
450
        self.assertEqual(1, call_count)
451
4160.2.9 by Andrew Bennetts
Fix BzrDir.cloning_metadir RPC to fail on branch references, and make
452
    def test_branch_reference(self):
453
        transport = self.get_transport('quack')
454
        referenced = self.make_branch('referenced')
455
        expected = referenced.bzrdir.cloning_metadir()
456
        client = FakeClient(transport.base)
457
        client.add_expected_call(
458
            'BzrDir.cloning_metadir', ('quack/', 'False'),
459
            'error', ('BranchReference',)),
460
        client.add_expected_call(
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
461
            'BzrDir.open_branchV3', ('quack/',),
4160.2.9 by Andrew Bennetts
Fix BzrDir.cloning_metadir RPC to fail on branch references, and make
462
            'success', ('ref', self.get_url('referenced'))),
5712.3.17 by Jelmer Vernooij
more fixes.
463
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4160.2.9 by Andrew Bennetts
Fix BzrDir.cloning_metadir RPC to fail on branch references, and make
464
            _client=client)
465
        result = a_bzrdir.cloning_metadir()
466
        # We should have got a control dir matching the referenced branch.
467
        self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
468
        self.assertEqual(expected._repository_format, result._repository_format)
469
        self.assertEqual(expected._branch_format, result._branch_format)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
470
        self.assertFinished(client)
4160.2.9 by Andrew Bennetts
Fix BzrDir.cloning_metadir RPC to fail on branch references, and make
471
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
472
    def test_current_server(self):
473
        transport = self.get_transport('.')
474
        transport = transport.clone('quack')
475
        self.make_bzrdir('quack')
476
        client = FakeClient(transport.base)
477
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
478
        control_name = reference_bzrdir_format.network_name()
479
        client.add_expected_call(
480
            'BzrDir.cloning_metadir', ('quack/', 'False'),
4084.2.2 by Robert Collins
Review feedback.
481
            'success', (control_name, '', ('branch', ''))),
5712.3.17 by Jelmer Vernooij
more fixes.
482
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
483
            _client=client)
484
        result = a_bzrdir.cloning_metadir()
485
        # We should have got a reference control dir with default branch and
486
        # repository formats.
487
        # This pokes a little, just to be sure.
488
        self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
4070.2.8 by Robert Collins
Really test the current BzrDir.cloning_metadir contract.
489
        self.assertEqual(None, result._repository_format)
490
        self.assertEqual(None, result._branch_format)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
491
        self.assertFinished(client)
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
492
6305.4.1 by Jelmer Vernooij
Print sensible error message when remote format is unknown.
493
    def test_unknown(self):
494
        transport = self.get_transport('quack')
495
        referenced = self.make_branch('referenced')
496
        expected = referenced.bzrdir.cloning_metadir()
497
        client = FakeClient(transport.base)
498
        client.add_expected_call(
499
            'BzrDir.cloning_metadir', ('quack/', 'False'),
500
            'success', ('unknown', 'unknown', ('branch', ''))),
501
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
502
            _client=client)
503
        self.assertRaises(errors.UnknownFormatError, a_bzrdir.cloning_metadir)
504
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
505
6266.4.1 by Jelmer Vernooij
HPSS call 'BzrDir.destroy_branch'.
506
class TestBzrDirDestroyBranch(TestRemote):
507
508
    def test_destroy_default(self):
509
        transport = self.get_transport('quack')
510
        referenced = self.make_branch('referenced')
511
        client = FakeClient(transport.base)
512
        client.add_expected_call(
6266.4.5 by Jelmer Vernooij
Don't serialize None.
513
            'BzrDir.destroy_branch', ('quack/', ),
6266.4.1 by Jelmer Vernooij
HPSS call 'BzrDir.destroy_branch'.
514
            'success', ('ok',)),
515
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
516
            _client=client)
517
        a_bzrdir.destroy_branch()
518
        self.assertFinished(client)
519
520
    def test_destroy_named(self):
521
        transport = self.get_transport('quack')
522
        referenced = self.make_branch('referenced')
523
        client = FakeClient(transport.base)
524
        client.add_expected_call(
525
            'BzrDir.destroy_branch', ('quack/', "foo"),
526
            'success', ('ok',)),
527
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
528
            _client=client)
529
        a_bzrdir.destroy_branch("foo")
530
        self.assertFinished(client)
531
532
6266.3.1 by Jelmer Vernooij
Add HPSS call for BzrDir.has_workingtree.
533
class TestBzrDirHasWorkingTree(TestRemote):
534
535
    def test_has_workingtree(self):
536
        transport = self.get_transport('quack')
537
        client = FakeClient(transport.base)
538
        client.add_expected_call(
539
            'BzrDir.has_workingtree', ('quack/',),
540
            'success', ('yes',)),
541
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
542
            _client=client)
543
        self.assertTrue(a_bzrdir.has_workingtree())
544
        self.assertFinished(client)
545
546
    def test_no_workingtree(self):
547
        transport = self.get_transport('quack')
548
        client = FakeClient(transport.base)
549
        client.add_expected_call(
550
            'BzrDir.has_workingtree', ('quack/',),
551
            'success', ('no',)),
552
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
553
            _client=client)
554
        self.assertFalse(a_bzrdir.has_workingtree())
555
        self.assertFinished(client)
556
557
6266.2.1 by Jelmer Vernooij
New HPSS call BzrDir.destroy_repository.
558
class TestBzrDirDestroyRepository(TestRemote):
559
560
    def test_destroy_repository(self):
561
        transport = self.get_transport('quack')
562
        client = FakeClient(transport.base)
563
        client.add_expected_call(
6266.2.2 by Jelmer Vernooij
Fix tests.
564
            'BzrDir.destroy_repository', ('quack/',),
6266.2.1 by Jelmer Vernooij
New HPSS call BzrDir.destroy_repository.
565
            'success', ('ok',)),
566
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
567
            _client=client)
568
        a_bzrdir.destroy_repository()
569
        self.assertFinished(client)
570
571
4634.47.5 by Andrew Bennetts
Add tests, and fix BzrDirMeta1.has_workingtree which was failing if the local transport is decorated with a ChrootTransport or similar.
572
class TestBzrDirOpen(TestRemote):
573
574
    def make_fake_client_and_transport(self, path='quack'):
575
        transport = MemoryTransport()
576
        transport.mkdir(path)
577
        transport = transport.clone(path)
578
        client = FakeClient(transport.base)
579
        return client, transport
580
581
    def test_absent(self):
582
        client, transport = self.make_fake_client_and_transport()
583
        client.add_expected_call(
584
            'BzrDir.open_2.1', ('quack/',), 'success', ('no',))
585
        self.assertRaises(errors.NotBranchError, RemoteBzrDir, transport,
5712.3.17 by Jelmer Vernooij
more fixes.
586
                RemoteBzrDirFormat(), _client=client, _force_probe=True)
4634.47.5 by Andrew Bennetts
Add tests, and fix BzrDirMeta1.has_workingtree which was failing if the local transport is decorated with a ChrootTransport or similar.
587
        self.assertFinished(client)
588
589
    def test_present_without_workingtree(self):
590
        client, transport = self.make_fake_client_and_transport()
591
        client.add_expected_call(
592
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'no'))
5712.3.17 by Jelmer Vernooij
more fixes.
593
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4634.47.5 by Andrew Bennetts
Add tests, and fix BzrDirMeta1.has_workingtree which was failing if the local transport is decorated with a ChrootTransport or similar.
594
            _client=client, _force_probe=True)
595
        self.assertIsInstance(bd, RemoteBzrDir)
596
        self.assertFalse(bd.has_workingtree())
597
        self.assertRaises(errors.NoWorkingTree, bd.open_workingtree)
598
        self.assertFinished(client)
599
600
    def test_present_with_workingtree(self):
601
        client, transport = self.make_fake_client_and_transport()
602
        client.add_expected_call(
603
            'BzrDir.open_2.1', ('quack/',), 'success', ('yes', 'yes'))
5712.3.17 by Jelmer Vernooij
more fixes.
604
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4634.47.5 by Andrew Bennetts
Add tests, and fix BzrDirMeta1.has_workingtree which was failing if the local transport is decorated with a ChrootTransport or similar.
605
            _client=client, _force_probe=True)
606
        self.assertIsInstance(bd, RemoteBzrDir)
607
        self.assertTrue(bd.has_workingtree())
608
        self.assertRaises(errors.NotLocalUrl, bd.open_workingtree)
609
        self.assertFinished(client)
610
611
    def test_backwards_compat(self):
612
        client, transport = self.make_fake_client_and_transport()
613
        client.add_expected_call(
614
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
615
        client.add_expected_call(
616
            'BzrDir.open', ('quack/',), 'success', ('yes',))
5712.3.17 by Jelmer Vernooij
more fixes.
617
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4634.47.5 by Andrew Bennetts
Add tests, and fix BzrDirMeta1.has_workingtree which was failing if the local transport is decorated with a ChrootTransport or similar.
618
            _client=client, _force_probe=True)
619
        self.assertIsInstance(bd, RemoteBzrDir)
620
        self.assertFinished(client)
621
4797.49.2 by Andrew Bennetts
Add test that demonstrates bug #528041.
622
    def test_backwards_compat_hpss_v2(self):
623
        client, transport = self.make_fake_client_and_transport()
624
        # Monkey-patch fake client to simulate real-world behaviour with v2
625
        # server: upon first RPC call detect the protocol version, and because
626
        # the version is 2 also do _remember_remote_is_before((1, 6)) before
627
        # continuing with the RPC.
628
        orig_check_call = client._check_call
629
        def check_call(method, args):
630
            client._medium._protocol_version = 2
631
            client._medium._remember_remote_is_before((1, 6))
632
            client._check_call = orig_check_call
633
            client._check_call(method, args)
634
        client._check_call = check_call
635
        client.add_expected_call(
636
            'BzrDir.open_2.1', ('quack/',), 'unknown', ('BzrDir.open_2.1',))
637
        client.add_expected_call(
638
            'BzrDir.open', ('quack/',), 'success', ('yes',))
5712.3.17 by Jelmer Vernooij
more fixes.
639
        bd = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4797.49.2 by Andrew Bennetts
Add test that demonstrates bug #528041.
640
            _client=client, _force_probe=True)
641
        self.assertIsInstance(bd, RemoteBzrDir)
642
        self.assertFinished(client)
643
4634.47.5 by Andrew Bennetts
Add tests, and fix BzrDirMeta1.has_workingtree which was failing if the local transport is decorated with a ChrootTransport or similar.
644
4053.1.2 by Robert Collins
Actually make this branch work.
645
class TestBzrDirOpenBranch(TestRemote):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
646
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
647
    def test_backwards_compat(self):
648
        self.setup_smart_server_with_call_log()
649
        self.make_branch('.')
650
        a_dir = BzrDir.open(self.get_url('.'))
651
        self.reset_smart_call_log()
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
652
        verb = 'BzrDir.open_branchV3'
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
653
        self.disable_verb(verb)
654
        format = a_dir.open_branch()
655
        call_count = len([call for call in self.hpss_calls if
656
            call.call.method == verb])
657
        self.assertEqual(1, call_count)
658
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
659
    def test_branch_present(self):
4053.1.2 by Robert Collins
Actually make this branch work.
660
        reference_format = self.get_repo_format()
661
        network_name = reference_format.network_name()
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
662
        branch_network_name = self.get_branch_format().network_name()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
663
        transport = MemoryTransport()
664
        transport.mkdir('quack')
665
        transport = transport.clone('quack')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
666
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
667
        client.add_expected_call(
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
668
            'BzrDir.open_branchV3', ('quack/',),
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
669
            'success', ('branch', branch_network_name))
3691.2.10 by Martin Pool
Update more test_remote tests
670
        client.add_expected_call(
4053.1.2 by Robert Collins
Actually make this branch work.
671
            'BzrDir.find_repositoryV3', ('quack/',),
672
            'success', ('ok', '', 'no', 'no', 'no', network_name))
3691.2.10 by Martin Pool
Update more test_remote tests
673
        client.add_expected_call(
674
            'Branch.get_stacked_on_url', ('quack/',),
675
            'error', ('NotStacked',))
5712.3.17 by Jelmer Vernooij
more fixes.
676
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
677
            _client=client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
678
        result = bzrdir.open_branch()
679
        self.assertIsInstance(result, RemoteBranch)
680
        self.assertEqual(bzrdir, result.bzrdir)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
681
        self.assertFinished(client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
682
683
    def test_branch_missing(self):
684
        transport = MemoryTransport()
685
        transport.mkdir('quack')
686
        transport = transport.clone('quack')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
687
        client = FakeClient(transport.base)
688
        client.add_error_response('nobranch')
5712.3.17 by Jelmer Vernooij
more fixes.
689
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
690
            _client=client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
691
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
692
        self.assertEqual(
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
693
            [('call', 'BzrDir.open_branchV3', ('quack/',))],
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
694
            client._calls)
695
3211.4.1 by Robert Collins
* ``RemoteBzrDir._get_tree_branch`` no longer triggers ``_ensure_real``,
696
    def test__get_tree_branch(self):
697
        # _get_tree_branch is a form of open_branch, but it should only ask for
698
        # branch opening, not any other network requests.
699
        calls = []
6305.3.4 by Jelmer Vernooij
Add possible_transports in a couple more places.
700
        def open_branch(name=None, possible_transports=None):
3211.4.1 by Robert Collins
* ``RemoteBzrDir._get_tree_branch`` no longer triggers ``_ensure_real``,
701
            calls.append("Called")
702
            return "a-branch"
703
        transport = MemoryTransport()
704
        # no requests on the network - catches other api calls being made.
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
705
        client = FakeClient(transport.base)
5712.3.17 by Jelmer Vernooij
more fixes.
706
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
707
            _client=client)
3211.4.1 by Robert Collins
* ``RemoteBzrDir._get_tree_branch`` no longer triggers ``_ensure_real``,
708
        # patch the open_branch call to record that it was called.
709
        bzrdir.open_branch = open_branch
710
        self.assertEqual((None, "a-branch"), bzrdir._get_tree_branch())
711
        self.assertEqual(["Called"], calls)
712
        self.assertEqual([], client._calls)
713
3192.2.1 by Andrew Bennetts
Don't transmit URL-escaped relpaths in the smart protocol, which is back to how things worked in bzr 1.1 and earlier.
714
    def test_url_quoting_of_path(self):
715
        # Relpaths on the wire should not be URL-escaped.  So "~" should be
716
        # transmitted as "~", not "%7E".
3431.3.1 by Andrew Bennetts
First rough cut of a fix for bug #230550, by adding .base to SmartClientMedia rather than relying on other objects to track this accurately while reusing client media.
717
        transport = RemoteTCPTransport('bzr://localhost/~hello/')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
718
        client = FakeClient(transport.base)
4053.1.2 by Robert Collins
Actually make this branch work.
719
        reference_format = self.get_repo_format()
720
        network_name = reference_format.network_name()
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
721
        branch_network_name = self.get_branch_format().network_name()
3691.2.10 by Martin Pool
Update more test_remote tests
722
        client.add_expected_call(
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
723
            'BzrDir.open_branchV3', ('~hello/',),
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
724
            'success', ('branch', branch_network_name))
3691.2.10 by Martin Pool
Update more test_remote tests
725
        client.add_expected_call(
4053.1.2 by Robert Collins
Actually make this branch work.
726
            'BzrDir.find_repositoryV3', ('~hello/',),
727
            'success', ('ok', '', 'no', 'no', 'no', network_name))
3691.2.10 by Martin Pool
Update more test_remote tests
728
        client.add_expected_call(
729
            'Branch.get_stacked_on_url', ('~hello/',),
730
            'error', ('NotStacked',))
5712.3.17 by Jelmer Vernooij
more fixes.
731
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
732
            _client=client)
3192.2.1 by Andrew Bennetts
Don't transmit URL-escaped relpaths in the smart protocol, which is back to how things worked in bzr 1.1 and earlier.
733
        result = bzrdir.open_branch()
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
734
        self.assertFinished(client)
3192.2.1 by Andrew Bennetts
Don't transmit URL-escaped relpaths in the smart protocol, which is back to how things worked in bzr 1.1 and earlier.
735
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
736
    def check_open_repository(self, rich_root, subtrees, external_lookup='no'):
4053.1.2 by Robert Collins
Actually make this branch work.
737
        reference_format = self.get_repo_format()
738
        network_name = reference_format.network_name()
3104.4.2 by Andrew Bennetts
All tests passing.
739
        transport = MemoryTransport()
740
        transport.mkdir('quack')
741
        transport = transport.clone('quack')
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
742
        if rich_root:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
743
            rich_response = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
744
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
745
            rich_response = 'no'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
746
        if subtrees:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
747
            subtree_response = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
748
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
749
            subtree_response = 'no'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
750
        client = FakeClient(transport.base)
751
        client.add_success_response(
4053.1.2 by Robert Collins
Actually make this branch work.
752
            'ok', '', rich_response, subtree_response, external_lookup,
753
            network_name)
5712.3.17 by Jelmer Vernooij
more fixes.
754
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
755
            _client=client)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
756
        result = bzrdir.open_repository()
757
        self.assertEqual(
4053.1.2 by Robert Collins
Actually make this branch work.
758
            [('call', 'BzrDir.find_repositoryV3', ('quack/',))],
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
759
            client._calls)
760
        self.assertIsInstance(result, RemoteRepository)
761
        self.assertEqual(bzrdir, result.bzrdir)
762
        self.assertEqual(rich_root, result._format.rich_root_data)
2018.5.138 by Robert Collins
Merge bzr.dev.
763
        self.assertEqual(subtrees, result._format.supports_tree_reference)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
764
765
    def test_open_repository_sets_format_attributes(self):
766
        self.check_open_repository(True, True)
767
        self.check_open_repository(False, True)
768
        self.check_open_repository(True, False)
769
        self.check_open_repository(False, False)
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
770
        self.check_open_repository(False, False, 'yes')
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
771
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
772
    def test_old_server(self):
773
        """RemoteBzrDirFormat should fail to probe if the server version is too
774
        old.
775
        """
776
        self.assertRaises(errors.NotBranchError,
5363.2.9 by Jelmer Vernooij
Fix some tests.
777
            RemoteBzrProber.probe_transport, OldServerTransport())
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
778
779
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
780
class TestBzrDirCreateBranch(TestRemote):
781
782
    def test_backwards_compat(self):
783
        self.setup_smart_server_with_call_log()
784
        repo = self.make_repository('.')
785
        self.reset_smart_call_log()
786
        self.disable_verb('BzrDir.create_branch')
787
        branch = repo.bzrdir.create_branch()
788
        create_branch_call_count = len([call for call in self.hpss_calls if
4070.3.1 by Robert Collins
Alter branch sprouting with an alternate fix for stacked branches that does not require multiple copy_content_into and set_parent calls, reducing IO and round trips.
789
            call.call.method == 'BzrDir.create_branch'])
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
790
        self.assertEqual(1, create_branch_call_count)
791
792
    def test_current_server(self):
793
        transport = self.get_transport('.')
794
        transport = transport.clone('quack')
795
        self.make_repository('quack')
796
        client = FakeClient(transport.base)
797
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
798
        reference_format = reference_bzrdir_format.get_branch_format()
799
        network_name = reference_format.network_name()
800
        reference_repo_fmt = reference_bzrdir_format.repository_format
801
        reference_repo_name = reference_repo_fmt.network_name()
802
        client.add_expected_call(
803
            'BzrDir.create_branch', ('quack/', network_name),
804
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
805
            reference_repo_name))
5712.3.17 by Jelmer Vernooij
more fixes.
806
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
807
            _client=client)
808
        branch = a_bzrdir.create_branch()
809
        # We should have got a remote branch
810
        self.assertIsInstance(branch, remote.RemoteBranch)
811
        # its format should have the settings from the response
812
        format = branch._format
813
        self.assertEqual(network_name, format.network_name())
814
5609.21.2 by Andrew Bennetts
Add test.
815
    def test_already_open_repo_and_reused_medium(self):
816
        """Bug 726584: create_branch(..., repository=repo) should work
817
        regardless of what the smart medium's base URL is.
818
        """
819
        self.transport_server = test_server.SmartTCPServer_for_testing
820
        transport = self.get_transport('.')
821
        repo = self.make_repository('quack')
822
        # Client's medium rooted a transport root (not at the bzrdir)
823
        client = FakeClient(transport.base)
824
        transport = transport.clone('quack')
825
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
826
        reference_format = reference_bzrdir_format.get_branch_format()
827
        network_name = reference_format.network_name()
828
        reference_repo_fmt = reference_bzrdir_format.repository_format
829
        reference_repo_name = reference_repo_fmt.network_name()
830
        client.add_expected_call(
831
            'BzrDir.create_branch', ('extra/quack/', network_name),
832
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
833
            reference_repo_name))
5712.3.17 by Jelmer Vernooij
more fixes.
834
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
5609.21.2 by Andrew Bennetts
Add test.
835
            _client=client)
836
        branch = a_bzrdir.create_branch(repository=repo)
837
        # We should have got a remote branch
838
        self.assertIsInstance(branch, remote.RemoteBranch)
839
        # its format should have the settings from the response
840
        format = branch._format
841
        self.assertEqual(network_name, format.network_name())
842
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
843
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
844
class TestBzrDirCreateRepository(TestRemote):
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
845
846
    def test_backwards_compat(self):
847
        self.setup_smart_server_with_call_log()
848
        bzrdir = self.make_bzrdir('.')
849
        self.reset_smart_call_log()
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
850
        self.disable_verb('BzrDir.create_repository')
851
        repo = bzrdir.create_repository()
852
        create_repo_call_count = len([call for call in self.hpss_calls if
4070.3.1 by Robert Collins
Alter branch sprouting with an alternate fix for stacked branches that does not require multiple copy_content_into and set_parent calls, reducing IO and round trips.
853
            call.call.method == 'BzrDir.create_repository'])
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
854
        self.assertEqual(1, create_repo_call_count)
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
855
856
    def test_current_server(self):
857
        transport = self.get_transport('.')
858
        transport = transport.clone('quack')
859
        self.make_bzrdir('quack')
860
        client = FakeClient(transport.base)
861
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
862
        reference_format = reference_bzrdir_format.repository_format
863
        network_name = reference_format.network_name()
864
        client.add_expected_call(
865
            'BzrDir.create_repository', ('quack/',
4599.4.20 by Robert Collins
Prep test_remote for 2a as default.
866
                'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
867
                'False'),
868
            'success', ('ok', 'yes', 'yes', 'yes', network_name))
5712.3.17 by Jelmer Vernooij
more fixes.
869
        a_bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
870
            _client=client)
871
        repo = a_bzrdir.create_repository()
872
        # We should have got a remote repository
873
        self.assertIsInstance(repo, remote.RemoteRepository)
874
        # its format should have the settings from the response
875
        format = repo._format
4599.4.20 by Robert Collins
Prep test_remote for 2a as default.
876
        self.assertTrue(format.rich_root_data)
877
        self.assertTrue(format.supports_tree_reference)
878
        self.assertTrue(format.supports_external_lookups)
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
879
        self.assertEqual(network_name, format.network_name())
880
881
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
882
class TestBzrDirOpenRepository(TestRemote):
883
884
    def test_backwards_compat_1_2_3(self):
885
        # fallback all the way to the first version.
886
        reference_format = self.get_repo_format()
887
        network_name = reference_format.network_name()
4691.2.1 by Robert Collins
Add stronger test isolation by interception BzrDir.open and checking the thing being opened is known to the test suite.
888
        server_url = 'bzr://example.com/'
889
        self.permit_url(server_url)
890
        client = FakeClient(server_url)
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
891
        client.add_unknown_method_response('BzrDir.find_repositoryV3')
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
892
        client.add_unknown_method_response('BzrDir.find_repositoryV2')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
893
        client.add_success_response('ok', '', 'no', 'no')
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
894
        # A real repository instance will be created to determine the network
895
        # name.
896
        client.add_success_response_with_body(
897
            "Bazaar-NG meta directory, format 1\n", 'ok')
898
        client.add_success_response_with_body(
899
            reference_format.get_format_string(), 'ok')
900
        # PackRepository wants to do a stat
901
        client.add_success_response('stat', '0', '65535')
4691.2.1 by Robert Collins
Add stronger test isolation by interception BzrDir.open and checking the thing being opened is known to the test suite.
902
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
903
            _client=client)
5712.3.17 by Jelmer Vernooij
more fixes.
904
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
905
            _client=client)
906
        repo = bzrdir.open_repository()
907
        self.assertEqual(
908
            [('call', 'BzrDir.find_repositoryV3', ('quack/',)),
909
             ('call', 'BzrDir.find_repositoryV2', ('quack/',)),
910
             ('call', 'BzrDir.find_repository', ('quack/',)),
911
             ('call_expecting_body', 'get', ('/quack/.bzr/branch-format',)),
912
             ('call_expecting_body', 'get', ('/quack/.bzr/repository/format',)),
913
             ('call', 'stat', ('/quack/.bzr/repository',)),
914
             ],
915
            client._calls)
916
        self.assertEqual(network_name, repo._format.network_name())
917
918
    def test_backwards_compat_2(self):
919
        # fallback to find_repositoryV2
920
        reference_format = self.get_repo_format()
921
        network_name = reference_format.network_name()
4691.2.1 by Robert Collins
Add stronger test isolation by interception BzrDir.open and checking the thing being opened is known to the test suite.
922
        server_url = 'bzr://example.com/'
923
        self.permit_url(server_url)
924
        client = FakeClient(server_url)
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
925
        client.add_unknown_method_response('BzrDir.find_repositoryV3')
926
        client.add_success_response('ok', '', 'no', 'no', 'no')
927
        # A real repository instance will be created to determine the network
928
        # name.
929
        client.add_success_response_with_body(
930
            "Bazaar-NG meta directory, format 1\n", 'ok')
931
        client.add_success_response_with_body(
932
            reference_format.get_format_string(), 'ok')
933
        # PackRepository wants to do a stat
934
        client.add_success_response('stat', '0', '65535')
4691.2.1 by Robert Collins
Add stronger test isolation by interception BzrDir.open and checking the thing being opened is known to the test suite.
935
        remote_transport = RemoteTransport(server_url + 'quack/', medium=False,
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
936
            _client=client)
5712.3.17 by Jelmer Vernooij
more fixes.
937
        bzrdir = RemoteBzrDir(remote_transport, RemoteBzrDirFormat(),
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
938
            _client=client)
939
        repo = bzrdir.open_repository()
940
        self.assertEqual(
941
            [('call', 'BzrDir.find_repositoryV3', ('quack/',)),
942
             ('call', 'BzrDir.find_repositoryV2', ('quack/',)),
943
             ('call_expecting_body', 'get', ('/quack/.bzr/branch-format',)),
944
             ('call_expecting_body', 'get', ('/quack/.bzr/repository/format',)),
945
             ('call', 'stat', ('/quack/.bzr/repository',)),
946
             ],
947
            client._calls)
948
        self.assertEqual(network_name, repo._format.network_name())
949
950
    def test_current_server(self):
951
        reference_format = self.get_repo_format()
952
        network_name = reference_format.network_name()
953
        transport = MemoryTransport()
954
        transport.mkdir('quack')
955
        transport = transport.clone('quack')
956
        client = FakeClient(transport.base)
957
        client.add_success_response('ok', '', 'no', 'no', 'no', network_name)
5712.3.17 by Jelmer Vernooij
more fixes.
958
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
959
            _client=client)
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
960
        repo = bzrdir.open_repository()
961
        self.assertEqual(
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
962
            [('call', 'BzrDir.find_repositoryV3', ('quack/',))],
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
963
            client._calls)
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
964
        self.assertEqual(network_name, repo._format.network_name())
3297.3.3 by Andrew Bennetts
SmartClientRequestProtocol*.read_response_tuple can now raise UnknownSmartMethod. Callers no longer need to do their own ad hoc unknown smart method error detection.
965
966
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
967
class TestBzrDirFormatInitializeEx(TestRemote):
968
969
    def test_success(self):
970
        """Simple test for typical successful call."""
5712.3.17 by Jelmer Vernooij
more fixes.
971
        fmt = RemoteBzrDirFormat()
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
972
        default_format_name = BzrDirFormat.get_default_format().network_name()
973
        transport = self.get_transport()
974
        client = FakeClient(transport.base)
975
        client.add_expected_call(
4436.1.1 by Andrew Bennetts
Rename BzrDirFormat.initialize_ex verb to BzrDirFormat.initialize_ex_1.16.
976
            'BzrDirFormat.initialize_ex_1.16',
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
977
                (default_format_name, 'path', 'False', 'False', 'False', '',
978
                 '', '', '', 'False'),
979
            'success',
980
                ('.', 'no', 'no', 'yes', 'repo fmt', 'repo bzrdir fmt',
981
                 'bzrdir fmt', 'False', '', '', 'repo lock token'))
982
        # XXX: It would be better to call fmt.initialize_on_transport_ex, but
983
        # it's currently hard to test that without supplying a real remote
984
        # transport connected to a real server.
985
        result = fmt._initialize_on_transport_ex_rpc(client, 'path',
986
            transport, False, False, False, None, None, None, None, False)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
987
        self.assertFinished(client)
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
988
989
    def test_error(self):
990
        """Error responses are translated, e.g. 'PermissionDenied' raises the
991
        corresponding error from the client.
992
        """
5712.3.17 by Jelmer Vernooij
more fixes.
993
        fmt = RemoteBzrDirFormat()
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
994
        default_format_name = BzrDirFormat.get_default_format().network_name()
995
        transport = self.get_transport()
996
        client = FakeClient(transport.base)
997
        client.add_expected_call(
4436.1.1 by Andrew Bennetts
Rename BzrDirFormat.initialize_ex verb to BzrDirFormat.initialize_ex_1.16.
998
            'BzrDirFormat.initialize_ex_1.16',
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
999
                (default_format_name, 'path', 'False', 'False', 'False', '',
1000
                 '', '', '', 'False'),
1001
            'error',
1002
                ('PermissionDenied', 'path', 'extra info'))
1003
        # XXX: It would be better to call fmt.initialize_on_transport_ex, but
1004
        # it's currently hard to test that without supplying a real remote
1005
        # transport connected to a real server.
1006
        err = self.assertRaises(errors.PermissionDenied,
1007
            fmt._initialize_on_transport_ex_rpc, client, 'path', transport,
1008
            False, False, False, None, None, None, None, False)
1009
        self.assertEqual('path', err.path)
1010
        self.assertEqual(': extra info', err.extra)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1011
        self.assertFinished(client)
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
1012
4384.1.3 by Andrew Bennetts
Add test suggested by John.
1013
    def test_error_from_real_server(self):
1014
        """Integration test for error translation."""
1015
        transport = self.make_smart_server('foo')
1016
        transport = transport.clone('no-such-path')
5712.3.17 by Jelmer Vernooij
more fixes.
1017
        fmt = RemoteBzrDirFormat()
4384.1.3 by Andrew Bennetts
Add test suggested by John.
1018
        err = self.assertRaises(errors.NoSuchFile,
1019
            fmt.initialize_on_transport_ex, transport, create_prefix=False)
1020
4384.1.1 by Andrew Bennetts
Translate ErrorFromSmartServer in RemoteBzrDirFormat.
1021
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
1022
class OldSmartClient(object):
1023
    """A fake smart client for test_old_version that just returns a version one
1024
    response to the 'hello' (query version) command.
1025
    """
1026
1027
    def get_request(self):
1028
        input_file = StringIO('ok\x011\n')
1029
        output_file = StringIO()
1030
        client_medium = medium.SmartSimplePipesClientMedium(
1031
            input_file, output_file)
1032
        return medium.SmartClientStreamMediumRequest(client_medium)
1033
3241.1.1 by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium.
1034
    def protocol_version(self):
1035
        return 1
1036
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
1037
1038
class OldServerTransport(object):
1039
    """A fake transport for test_old_server that reports it's smart server
1040
    protocol version as version one.
1041
    """
1042
1043
    def __init__(self):
1044
        self.base = 'fake:'
1045
1046
    def get_smart_client(self):
1047
        return OldSmartClient()
1048
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1049
4288.1.1 by Robert Collins
Add support for a RemoteBzrDirConfig to support optimising push operations which need to look for default stacking locations.
1050
class RemoteBzrDirTestCase(TestRemote):
1051
1052
    def make_remote_bzrdir(self, transport, client):
1053
        """Make a RemotebzrDir using 'client' as the _client."""
5712.3.17 by Jelmer Vernooij
more fixes.
1054
        return RemoteBzrDir(transport, RemoteBzrDirFormat(),
4288.1.1 by Robert Collins
Add support for a RemoteBzrDirConfig to support optimising push operations which need to look for default stacking locations.
1055
            _client=client)
1056
1057
1058
class RemoteBranchTestCase(RemoteBzrDirTestCase):
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1059
4634.36.1 by Andrew Bennetts
Fix trivial bug in RemoteBranch._set_tags_bytes, and add some unit tests for it.
1060
    def lock_remote_branch(self, branch):
1061
        """Trick a RemoteBranch into thinking it is locked."""
1062
        branch._lock_mode = 'w'
1063
        branch._lock_count = 2
1064
        branch._lock_token = 'branch token'
1065
        branch._repo_lock_token = 'repo token'
1066
        branch.repository._lock_mode = 'w'
1067
        branch.repository._lock_count = 2
1068
        branch.repository._lock_token = 'repo token'
1069
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1070
    def make_remote_branch(self, transport, client):
1071
        """Make a RemoteBranch using 'client' as its _SmartClient.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1072
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1073
        A RemoteBzrDir and RemoteRepository will also be created to fill out
1074
        the RemoteBranch, albeit with stub values for some of their attributes.
1075
        """
1076
        # we do not want bzrdir to make any remote calls, so use False as its
1077
        # _client.  If it tries to make a remote call, this will fail
1078
        # immediately.
4288.1.1 by Robert Collins
Add support for a RemoteBzrDirConfig to support optimising push operations which need to look for default stacking locations.
1079
        bzrdir = self.make_remote_bzrdir(transport, False)
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1080
        repo = RemoteRepository(bzrdir, None, _client=client)
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
1081
        branch_format = self.get_branch_format()
1082
        format = RemoteBranchFormat(network_name=branch_format.network_name())
1083
        return RemoteBranch(bzrdir, repo, _client=client, format=format)
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1084
1085
6280.4.3 by Jelmer Vernooij
add Branch.break_lock.
1086
class TestBranchBreakLock(RemoteBranchTestCase):
1087
1088
    def test_break_lock(self):
1089
        transport_path = 'quack'
1090
        transport = MemoryTransport()
1091
        client = FakeClient(transport.base)
1092
        client.add_expected_call(
1093
            'Branch.get_stacked_on_url', ('quack/',),
1094
            'error', ('NotStacked',))
1095
        client.add_expected_call(
1096
            'Branch.break_lock', ('quack/',),
1097
            'success', ('ok',))
1098
        transport.mkdir('quack')
1099
        transport = transport.clone('quack')
1100
        branch = self.make_remote_branch(transport, client)
1101
        branch.break_lock()
1102
        self.assertFinished(client)
1103
1104
6280.6.1 by Jelmer Vernooij
Implement remote side of {Branch,Repository}.get_physical_lock_status.
1105
class TestBranchGetPhysicalLockStatus(RemoteBranchTestCase):
1106
1107
    def test_get_physical_lock_status_yes(self):
1108
        transport = MemoryTransport()
1109
        client = FakeClient(transport.base)
1110
        client.add_expected_call(
1111
            'Branch.get_stacked_on_url', ('quack/',),
1112
            'error', ('NotStacked',))
1113
        client.add_expected_call(
1114
            'Branch.get_physical_lock_status', ('quack/',),
1115
            'success', ('yes',))
1116
        transport.mkdir('quack')
1117
        transport = transport.clone('quack')
1118
        branch = self.make_remote_branch(transport, client)
1119
        result = branch.get_physical_lock_status()
1120
        self.assertFinished(client)
1121
        self.assertEqual(True, result)
1122
1123
    def test_get_physical_lock_status_no(self):
1124
        transport = MemoryTransport()
1125
        client = FakeClient(transport.base)
1126
        client.add_expected_call(
1127
            'Branch.get_stacked_on_url', ('quack/',),
1128
            'error', ('NotStacked',))
1129
        client.add_expected_call(
1130
            'Branch.get_physical_lock_status', ('quack/',),
1131
            'success', ('no',))
1132
        transport.mkdir('quack')
1133
        transport = transport.clone('quack')
1134
        branch = self.make_remote_branch(transport, client)
1135
        result = branch.get_physical_lock_status()
1136
        self.assertFinished(client)
1137
        self.assertEqual(False, result)
1138
1139
4078.2.1 by Robert Collins
Add a Branch.get_parent remote call for RemoteBranch.
1140
class TestBranchGetParent(RemoteBranchTestCase):
1141
1142
    def test_no_parent(self):
1143
        # in an empty branch we decode the response properly
1144
        transport = MemoryTransport()
1145
        client = FakeClient(transport.base)
1146
        client.add_expected_call(
1147
            'Branch.get_stacked_on_url', ('quack/',),
1148
            'error', ('NotStacked',))
1149
        client.add_expected_call(
1150
            'Branch.get_parent', ('quack/',),
4083.1.7 by Andrew Bennetts
Fix same trivial bug [(x) != (x,)] in test_remote and test_smart.
1151
            'success', ('',))
4078.2.1 by Robert Collins
Add a Branch.get_parent remote call for RemoteBranch.
1152
        transport.mkdir('quack')
1153
        transport = transport.clone('quack')
1154
        branch = self.make_remote_branch(transport, client)
1155
        result = branch.get_parent()
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1156
        self.assertFinished(client)
4078.2.1 by Robert Collins
Add a Branch.get_parent remote call for RemoteBranch.
1157
        self.assertEqual(None, result)
1158
1159
    def test_parent_relative(self):
1160
        transport = MemoryTransport()
1161
        client = FakeClient(transport.base)
1162
        client.add_expected_call(
1163
            'Branch.get_stacked_on_url', ('kwaak/',),
1164
            'error', ('NotStacked',))
1165
        client.add_expected_call(
1166
            'Branch.get_parent', ('kwaak/',),
4083.1.7 by Andrew Bennetts
Fix same trivial bug [(x) != (x,)] in test_remote and test_smart.
1167
            'success', ('../foo/',))
4078.2.1 by Robert Collins
Add a Branch.get_parent remote call for RemoteBranch.
1168
        transport.mkdir('kwaak')
1169
        transport = transport.clone('kwaak')
1170
        branch = self.make_remote_branch(transport, client)
1171
        result = branch.get_parent()
1172
        self.assertEqual(transport.clone('../foo').base, result)
1173
1174
    def test_parent_absolute(self):
1175
        transport = MemoryTransport()
1176
        client = FakeClient(transport.base)
1177
        client.add_expected_call(
1178
            'Branch.get_stacked_on_url', ('kwaak/',),
1179
            'error', ('NotStacked',))
1180
        client.add_expected_call(
1181
            'Branch.get_parent', ('kwaak/',),
4083.1.7 by Andrew Bennetts
Fix same trivial bug [(x) != (x,)] in test_remote and test_smart.
1182
            'success', ('http://foo/',))
4078.2.1 by Robert Collins
Add a Branch.get_parent remote call for RemoteBranch.
1183
        transport.mkdir('kwaak')
1184
        transport = transport.clone('kwaak')
1185
        branch = self.make_remote_branch(transport, client)
1186
        result = branch.get_parent()
1187
        self.assertEqual('http://foo/', result)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1188
        self.assertFinished(client)
4288.1.7 by Robert Collins
Add new remote server verb Branch.set_parent_location, dropping roundtrips further on push operations.
1189
1190
1191
class TestBranchSetParentLocation(RemoteBranchTestCase):
1192
1193
    def test_no_parent(self):
1194
        # We call the verb when setting parent to None
1195
        transport = MemoryTransport()
1196
        client = FakeClient(transport.base)
1197
        client.add_expected_call(
1198
            'Branch.get_stacked_on_url', ('quack/',),
1199
            'error', ('NotStacked',))
1200
        client.add_expected_call(
1201
            'Branch.set_parent_location', ('quack/', 'b', 'r', ''),
1202
            'success', ())
1203
        transport.mkdir('quack')
1204
        transport = transport.clone('quack')
1205
        branch = self.make_remote_branch(transport, client)
1206
        branch._lock_token = 'b'
1207
        branch._repo_lock_token = 'r'
1208
        branch._set_parent_location(None)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1209
        self.assertFinished(client)
4288.1.7 by Robert Collins
Add new remote server verb Branch.set_parent_location, dropping roundtrips further on push operations.
1210
1211
    def test_parent(self):
1212
        transport = MemoryTransport()
1213
        client = FakeClient(transport.base)
1214
        client.add_expected_call(
1215
            'Branch.get_stacked_on_url', ('kwaak/',),
1216
            'error', ('NotStacked',))
1217
        client.add_expected_call(
1218
            'Branch.set_parent_location', ('kwaak/', 'b', 'r', 'foo'),
1219
            'success', ())
1220
        transport.mkdir('kwaak')
1221
        transport = transport.clone('kwaak')
1222
        branch = self.make_remote_branch(transport, client)
1223
        branch._lock_token = 'b'
1224
        branch._repo_lock_token = 'r'
1225
        branch._set_parent_location('foo')
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1226
        self.assertFinished(client)
4288.1.7 by Robert Collins
Add new remote server verb Branch.set_parent_location, dropping roundtrips further on push operations.
1227
1228
    def test_backwards_compat(self):
1229
        self.setup_smart_server_with_call_log()
1230
        branch = self.make_branch('.')
1231
        self.reset_smart_call_log()
1232
        verb = 'Branch.set_parent_location'
1233
        self.disable_verb(verb)
1234
        branch.set_parent('http://foo/')
1235
        self.assertLength(12, self.hpss_calls)
4078.2.1 by Robert Collins
Add a Branch.get_parent remote call for RemoteBranch.
1236
1237
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
1238
class TestBranchGetTagsBytes(RemoteBranchTestCase):
1239
1240
    def test_backwards_compat(self):
1241
        self.setup_smart_server_with_call_log()
1242
        branch = self.make_branch('.')
1243
        self.reset_smart_call_log()
1244
        verb = 'Branch.get_tags_bytes'
1245
        self.disable_verb(verb)
1246
        branch.tags.get_tag_dict()
1247
        call_count = len([call for call in self.hpss_calls if
1248
            call.call.method == verb])
1249
        self.assertEqual(1, call_count)
1250
1251
    def test_trivial(self):
1252
        transport = MemoryTransport()
1253
        client = FakeClient(transport.base)
1254
        client.add_expected_call(
1255
            'Branch.get_stacked_on_url', ('quack/',),
1256
            'error', ('NotStacked',))
1257
        client.add_expected_call(
1258
            'Branch.get_tags_bytes', ('quack/',),
1259
            'success', ('',))
1260
        transport.mkdir('quack')
1261
        transport = transport.clone('quack')
1262
        branch = self.make_remote_branch(transport, client)
1263
        result = branch.tags.get_tag_dict()
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1264
        self.assertFinished(client)
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
1265
        self.assertEqual({}, result)
1266
1267
4634.36.1 by Andrew Bennetts
Fix trivial bug in RemoteBranch._set_tags_bytes, and add some unit tests for it.
1268
class TestBranchSetTagsBytes(RemoteBranchTestCase):
1269
1270
    def test_trivial(self):
1271
        transport = MemoryTransport()
1272
        client = FakeClient(transport.base)
1273
        client.add_expected_call(
1274
            'Branch.get_stacked_on_url', ('quack/',),
1275
            'error', ('NotStacked',))
1276
        client.add_expected_call(
1277
            'Branch.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
1278
            'success', ('',))
1279
        transport.mkdir('quack')
1280
        transport = transport.clone('quack')
1281
        branch = self.make_remote_branch(transport, client)
1282
        self.lock_remote_branch(branch)
1283
        branch._set_tags_bytes('tags bytes')
1284
        self.assertFinished(client)
1285
        self.assertEqual('tags bytes', client._calls[-1][-1])
1286
1287
    def test_backwards_compatible(self):
1288
        transport = MemoryTransport()
1289
        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.set_tags_bytes', ('quack/', 'branch token', 'repo token'),
1295
            'unknown', ('Branch.set_tags_bytes',))
1296
        transport.mkdir('quack')
1297
        transport = transport.clone('quack')
1298
        branch = self.make_remote_branch(transport, client)
1299
        self.lock_remote_branch(branch)
1300
        class StubRealBranch(object):
1301
            def __init__(self):
1302
                self.calls = []
1303
            def _set_tags_bytes(self, bytes):
1304
                self.calls.append(('set_tags_bytes', bytes))
1305
        real_branch = StubRealBranch()
1306
        branch._real_branch = real_branch
1307
        branch._set_tags_bytes('tags bytes')
1308
        # Call a second time, to exercise the 'remote version already inferred'
1309
        # code path.
1310
        branch._set_tags_bytes('tags bytes')
1311
        self.assertFinished(client)
1312
        self.assertEqual(
1313
            [('set_tags_bytes', 'tags bytes')] * 2, real_branch.calls)
1314
1315
5672.1.5 by Andrew Bennetts
Add some tests for RemoteBranch.heads_to_fetch, and add release-note.
1316
class TestBranchHeadsToFetch(RemoteBranchTestCase):
1317
1318
    def test_uses_last_revision_info_and_tags_by_default(self):
1319
        transport = MemoryTransport()
1320
        client = FakeClient(transport.base)
1321
        client.add_expected_call(
1322
            'Branch.get_stacked_on_url', ('quack/',),
1323
            'error', ('NotStacked',))
1324
        client.add_expected_call(
1325
            'Branch.last_revision_info', ('quack/',),
1326
            'success', ('ok', '1', 'rev-tip'))
6015.15.4 by John Arbash Meinel
Catch a couple more cases that test tag fetching.
1327
        client.add_expected_call(
1328
            'Branch.get_config_file', ('quack/',),
1329
            'success', ('ok',), '')
1330
        transport.mkdir('quack')
1331
        transport = transport.clone('quack')
1332
        branch = self.make_remote_branch(transport, client)
1333
        result = branch.heads_to_fetch()
1334
        self.assertFinished(client)
1335
        self.assertEqual((set(['rev-tip']), set()), result)
1336
1337
    def test_uses_last_revision_info_and_tags_when_set(self):
1338
        transport = MemoryTransport()
1339
        client = FakeClient(transport.base)
1340
        client.add_expected_call(
1341
            'Branch.get_stacked_on_url', ('quack/',),
1342
            'error', ('NotStacked',))
1343
        client.add_expected_call(
1344
            'Branch.last_revision_info', ('quack/',),
1345
            'success', ('ok', '1', 'rev-tip'))
1346
        client.add_expected_call(
1347
            'Branch.get_config_file', ('quack/',),
1348
            'success', ('ok',), 'branch.fetch_tags = True')
5672.1.5 by Andrew Bennetts
Add some tests for RemoteBranch.heads_to_fetch, and add release-note.
1349
        # XXX: this will break if the default format's serialization of tags
1350
        # changes, or if the RPC for fetching tags changes from get_tags_bytes.
1351
        client.add_expected_call(
1352
            'Branch.get_tags_bytes', ('quack/',),
1353
            'success', ('d5:tag-17:rev-foo5:tag-27:rev-bare',))
1354
        transport.mkdir('quack')
1355
        transport = transport.clone('quack')
1356
        branch = self.make_remote_branch(transport, client)
1357
        result = branch.heads_to_fetch()
1358
        self.assertFinished(client)
1359
        self.assertEqual(
1360
            (set(['rev-tip']), set(['rev-foo', 'rev-bar'])), result)
1361
1362
    def test_uses_rpc_for_formats_with_non_default_heads_to_fetch(self):
1363
        transport = MemoryTransport()
1364
        client = FakeClient(transport.base)
1365
        client.add_expected_call(
1366
            'Branch.get_stacked_on_url', ('quack/',),
1367
            'error', ('NotStacked',))
1368
        client.add_expected_call(
5741.1.11 by Jelmer Vernooij
Don't make heads_to_fetch() take a stop_revision.
1369
            'Branch.heads_to_fetch', ('quack/',),
5672.1.6 by Andrew Bennetts
Add another test.
1370
            'success', (['tip'], ['tagged-1', 'tagged-2']))
5672.1.5 by Andrew Bennetts
Add some tests for RemoteBranch.heads_to_fetch, and add release-note.
1371
        transport.mkdir('quack')
1372
        transport = transport.clone('quack')
1373
        branch = self.make_remote_branch(transport, client)
5672.1.7 by Andrew Bennetts
Use a more explicit method name.
1374
        branch._format._use_default_local_heads_to_fetch = lambda: False
5672.1.5 by Andrew Bennetts
Add some tests for RemoteBranch.heads_to_fetch, and add release-note.
1375
        result = branch.heads_to_fetch()
1376
        self.assertFinished(client)
5672.1.6 by Andrew Bennetts
Add another test.
1377
        self.assertEqual((set(['tip']), set(['tagged-1', 'tagged-2'])), result)
1378
6015.15.1 by John Arbash Meinel
Start working on a config entry for testing whether we should fetch tags or not.
1379
    def make_branch_with_tags(self):
5672.1.6 by Andrew Bennetts
Add another test.
1380
        self.setup_smart_server_with_call_log()
1381
        # Make a branch with a single revision.
1382
        builder = self.make_branch_builder('foo')
1383
        builder.start_series()
1384
        builder.build_snapshot('tip', None, [
1385
            ('add', ('', 'root-id', 'directory', ''))])
1386
        builder.finish_series()
1387
        branch = builder.get_branch()
1388
        # Add two tags to that branch
1389
        branch.tags.set_tag('tag-1', 'rev-1')
1390
        branch.tags.set_tag('tag-2', 'rev-2')
6015.15.1 by John Arbash Meinel
Start working on a config entry for testing whether we should fetch tags or not.
1391
        return branch
1392
1393
    def test_backwards_compatible(self):
1394
        branch = self.make_branch_with_tags()
1395
        c = branch.get_config()
1396
        c.set_user_option('branch.fetch_tags', 'True')
5672.1.6 by Andrew Bennetts
Add another test.
1397
        self.addCleanup(branch.lock_read().unlock)
1398
        # Disable the heads_to_fetch verb
1399
        verb = 'Branch.heads_to_fetch'
1400
        self.disable_verb(verb)
1401
        self.reset_smart_call_log()
1402
        result = branch.heads_to_fetch()
1403
        self.assertEqual((set(['tip']), set(['rev-1', 'rev-2'])), result)
1404
        self.assertEqual(
6015.15.1 by John Arbash Meinel
Start working on a config entry for testing whether we should fetch tags or not.
1405
            ['Branch.last_revision_info', 'Branch.get_config_file',
1406
             'Branch.get_tags_bytes'],
1407
            [call.call.method for call in self.hpss_calls])
1408
1409
    def test_backwards_compatible_no_tags(self):
1410
        branch = self.make_branch_with_tags()
1411
        c = branch.get_config()
1412
        c.set_user_option('branch.fetch_tags', 'False')
1413
        self.addCleanup(branch.lock_read().unlock)
1414
        # Disable the heads_to_fetch verb
1415
        verb = 'Branch.heads_to_fetch'
1416
        self.disable_verb(verb)
1417
        self.reset_smart_call_log()
1418
        result = branch.heads_to_fetch()
1419
        self.assertEqual((set(['tip']), set()), result)
1420
        self.assertEqual(
1421
            ['Branch.last_revision_info', 'Branch.get_config_file'],
5672.1.6 by Andrew Bennetts
Add another test.
1422
            [call.call.method for call in self.hpss_calls])
5672.1.5 by Andrew Bennetts
Add some tests for RemoteBranch.heads_to_fetch, and add release-note.
1423
1424
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1425
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1426
1427
    def test_empty_branch(self):
1428
        # in an empty branch we decode the response properly
1429
        transport = MemoryTransport()
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1430
        client = FakeClient(transport.base)
3691.2.8 by Martin Pool
Update some test_remote tests for Branch.get_stacked_on_url and with clearer assertions
1431
        client.add_expected_call(
1432
            'Branch.get_stacked_on_url', ('quack/',),
1433
            'error', ('NotStacked',))
1434
        client.add_expected_call(
1435
            'Branch.last_revision_info', ('quack/',),
1436
            'success', ('ok', '0', 'null:'))
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1437
        transport.mkdir('quack')
1438
        transport = transport.clone('quack')
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1439
        branch = self.make_remote_branch(transport, client)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1440
        result = branch.last_revision_info()
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1441
        self.assertFinished(client)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1442
        self.assertEqual((0, NULL_REVISION), result)
1443
1444
    def test_non_empty_branch(self):
1445
        # in a non-empty branch we also decode the response properly
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
1446
        revid = u'\xc8'.encode('utf8')
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1447
        transport = MemoryTransport()
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1448
        client = FakeClient(transport.base)
3691.2.8 by Martin Pool
Update some test_remote tests for Branch.get_stacked_on_url and with clearer assertions
1449
        client.add_expected_call(
1450
            'Branch.get_stacked_on_url', ('kwaak/',),
1451
            'error', ('NotStacked',))
1452
        client.add_expected_call(
1453
            'Branch.last_revision_info', ('kwaak/',),
1454
            'success', ('ok', '2', revid))
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1455
        transport.mkdir('kwaak')
1456
        transport = transport.clone('kwaak')
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1457
        branch = self.make_remote_branch(transport, client)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1458
        result = branch.last_revision_info()
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
1459
        self.assertEqual((2, revid), result)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1460
1461
4053.1.2 by Robert Collins
Actually make this branch work.
1462
class TestBranch_get_stacked_on_url(TestRemote):
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
1463
    """Test Branch._get_stacked_on_url rpc"""
1464
3691.2.10 by Martin Pool
Update more test_remote tests
1465
    def test_get_stacked_on_invalid_url(self):
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1466
        # test that asking for a stacked on url the server can't access works.
1467
        # This isn't perfect, but then as we're in the same process there
1468
        # really isn't anything we can do to be 100% sure that the server
1469
        # doesn't just open in - this test probably needs to be rewritten using
1470
        # a spawn()ed server.
1471
        stacked_branch = self.make_branch('stacked', format='1.9')
1472
        memory_branch = self.make_branch('base', format='1.9')
1473
        vfs_url = self.get_vfs_only_url('base')
1474
        stacked_branch.set_stacked_on_url(vfs_url)
1475
        transport = stacked_branch.bzrdir.root_transport
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
1476
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1477
        client.add_expected_call(
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1478
            'Branch.get_stacked_on_url', ('stacked/',),
1479
            'success', ('ok', vfs_url))
1480
        # XXX: Multiple calls are bad, this second call documents what is
1481
        # today.
1482
        client.add_expected_call(
1483
            'Branch.get_stacked_on_url', ('stacked/',),
1484
            'success', ('ok', vfs_url))
5712.3.17 by Jelmer Vernooij
more fixes.
1485
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1486
            _client=client)
4118.1.5 by Andrew Bennetts
Fix test_remote tests.
1487
        repo_fmt = remote.RemoteRepositoryFormat()
1488
        repo_fmt._custom_format = stacked_branch.repository._format
1489
        branch = RemoteBranch(bzrdir, RemoteRepository(bzrdir, repo_fmt),
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1490
            _client=client)
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
1491
        result = branch.get_stacked_on_url()
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1492
        self.assertEqual(vfs_url, result)
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
1493
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
1494
    def test_backwards_compatible(self):
1495
        # like with bzr1.6 with no Branch.get_stacked_on_url rpc
1496
        base_branch = self.make_branch('base', format='1.6')
1497
        stacked_branch = self.make_branch('stacked', format='1.6')
1498
        stacked_branch.set_stacked_on_url('../base')
1499
        client = FakeClient(self.get_url())
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
1500
        branch_network_name = self.get_branch_format().network_name()
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
1501
        client.add_expected_call(
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
1502
            'BzrDir.open_branchV3', ('stacked/',),
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
1503
            'success', ('branch', branch_network_name))
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
1504
        client.add_expected_call(
4053.1.2 by Robert Collins
Actually make this branch work.
1505
            'BzrDir.find_repositoryV3', ('stacked/',),
4118.1.5 by Andrew Bennetts
Fix test_remote tests.
1506
            'success', ('ok', '', 'no', 'no', 'yes',
4053.1.2 by Robert Collins
Actually make this branch work.
1507
                stacked_branch.repository._format.network_name()))
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
1508
        # called twice, once from constructor and then again by us
1509
        client.add_expected_call(
1510
            'Branch.get_stacked_on_url', ('stacked/',),
1511
            'unknown', ('Branch.get_stacked_on_url',))
1512
        client.add_expected_call(
1513
            'Branch.get_stacked_on_url', ('stacked/',),
1514
            'unknown', ('Branch.get_stacked_on_url',))
1515
        # this will also do vfs access, but that goes direct to the transport
1516
        # and isn't seen by the FakeClient.
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1517
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
5712.3.17 by Jelmer Vernooij
more fixes.
1518
            RemoteBzrDirFormat(), _client=client)
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
1519
        branch = bzrdir.open_branch()
1520
        result = branch.get_stacked_on_url()
1521
        self.assertEqual('../base', result)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1522
        self.assertFinished(client)
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
1523
        # it's in the fallback list both for the RemoteRepository and its vfs
1524
        # repository
1525
        self.assertEqual(1, len(branch.repository._fallback_repositories))
1526
        self.assertEqual(1,
1527
            len(branch.repository._real_repository._fallback_repositories))
1528
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
1529
    def test_get_stacked_on_real_branch(self):
5158.4.3 by Andrew Bennetts
Fix test_remote tests that accidentally assumed it was ok to stack mismatched formats.
1530
        base_branch = self.make_branch('base')
1531
        stacked_branch = self.make_branch('stacked')
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
1532
        stacked_branch.set_stacked_on_url('../base')
4053.1.2 by Robert Collins
Actually make this branch work.
1533
        reference_format = self.get_repo_format()
1534
        network_name = reference_format.network_name()
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
1535
        client = FakeClient(self.get_url())
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
1536
        branch_network_name = self.get_branch_format().network_name()
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
1537
        client.add_expected_call(
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
1538
            'BzrDir.open_branchV3', ('stacked/',),
4084.2.1 by Robert Collins
Make accessing a branch.tags.get_tag_dict use a smart[er] method rather than VFS calls and real objects.
1539
            'success', ('branch', branch_network_name))
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
1540
        client.add_expected_call(
4053.1.2 by Robert Collins
Actually make this branch work.
1541
            'BzrDir.find_repositoryV3', ('stacked/',),
5158.4.3 by Andrew Bennetts
Fix test_remote tests that accidentally assumed it was ok to stack mismatched formats.
1542
            'success', ('ok', '', 'yes', 'no', 'yes', network_name))
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
1543
        # called twice, once from constructor and then again by us
1544
        client.add_expected_call(
1545
            'Branch.get_stacked_on_url', ('stacked/',),
1546
            'success', ('ok', '../base'))
1547
        client.add_expected_call(
1548
            'Branch.get_stacked_on_url', ('stacked/',),
1549
            'success', ('ok', '../base'))
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1550
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
5712.3.17 by Jelmer Vernooij
more fixes.
1551
            RemoteBzrDirFormat(), _client=client)
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
1552
        branch = bzrdir.open_branch()
1553
        result = branch.get_stacked_on_url()
1554
        self.assertEqual('../base', result)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1555
        self.assertFinished(client)
4226.1.2 by Robert Collins
Fix test_remote failing because of less _real_repository objects.
1556
        # it's in the fallback list both for the RemoteRepository.
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
1557
        self.assertEqual(1, len(branch.repository._fallback_repositories))
4226.1.2 by Robert Collins
Fix test_remote failing because of less _real_repository objects.
1558
        # And we haven't had to construct a real repository.
1559
        self.assertEqual(None, branch.repository._real_repository)
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
1560
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
1561
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1562
class TestBranchSetLastRevision(RemoteBranchTestCase):
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1563
1564
    def test_set_empty(self):
5718.7.12 by Jelmer Vernooij
Fix use of _set_last_revision.
1565
        # _set_last_revision_info('null:') is translated to calling
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1566
        # Branch.set_last_revision(path, '') on the wire.
3104.4.2 by Andrew Bennetts
All tests passing.
1567
        transport = MemoryTransport()
1568
        transport.mkdir('branch')
1569
        transport = transport.clone('branch')
1570
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1571
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1572
        client.add_expected_call(
1573
            'Branch.get_stacked_on_url', ('branch/',),
1574
            'error', ('NotStacked',))
1575
        client.add_expected_call(
1576
            'Branch.lock_write', ('branch/', '', ''),
1577
            'success', ('ok', 'branch token', 'repo token'))
1578
        client.add_expected_call(
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1579
            'Branch.last_revision_info',
1580
            ('branch/',),
1581
            'success', ('ok', '0', 'null:'))
1582
        client.add_expected_call(
3691.2.10 by Martin Pool
Update more test_remote tests
1583
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'null:',),
1584
            'success', ('ok',))
1585
        client.add_expected_call(
1586
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
1587
            'success', ('ok',))
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1588
        branch = self.make_remote_branch(transport, client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1589
        branch.lock_write()
5718.7.12 by Jelmer Vernooij
Fix use of _set_last_revision.
1590
        result = branch._set_last_revision(NULL_REVISION)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1591
        branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1592
        self.assertEqual(None, result)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1593
        self.assertFinished(client)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1594
1595
    def test_set_nonempty(self):
5718.7.4 by Jelmer Vernooij
Branch.set_revision_history.
1596
        # set_last_revision_info(N, rev-idN) is translated to calling
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1597
        # Branch.set_last_revision(path, rev-idN) on the wire.
3104.4.2 by Andrew Bennetts
All tests passing.
1598
        transport = MemoryTransport()
1599
        transport.mkdir('branch')
1600
        transport = transport.clone('branch')
1601
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1602
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1603
        client.add_expected_call(
1604
            'Branch.get_stacked_on_url', ('branch/',),
1605
            'error', ('NotStacked',))
1606
        client.add_expected_call(
1607
            'Branch.lock_write', ('branch/', '', ''),
1608
            'success', ('ok', 'branch token', 'repo token'))
1609
        client.add_expected_call(
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1610
            'Branch.last_revision_info',
1611
            ('branch/',),
1612
            'success', ('ok', '0', 'null:'))
1613
        lines = ['rev-id2']
1614
        encoded_body = bz2.compress('\n'.join(lines))
1615
        client.add_success_response_with_body(encoded_body, 'ok')
1616
        client.add_expected_call(
3691.2.10 by Martin Pool
Update more test_remote tests
1617
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id2',),
1618
            'success', ('ok',))
1619
        client.add_expected_call(
1620
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
1621
            'success', ('ok',))
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1622
        branch = self.make_remote_branch(transport, client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1623
        # Lock the branch, reset the record of remote calls.
1624
        branch.lock_write()
5718.7.12 by Jelmer Vernooij
Fix use of _set_last_revision.
1625
        result = branch._set_last_revision('rev-id2')
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1626
        branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1627
        self.assertEqual(None, result)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1628
        self.assertFinished(client)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1629
1630
    def test_no_such_revision(self):
1631
        transport = MemoryTransport()
1632
        transport.mkdir('branch')
1633
        transport = transport.clone('branch')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1634
        # A response of 'NoSuchRevision' is translated into an exception.
1635
        client = FakeClient(transport.base)
3691.2.9 by Martin Pool
Convert and update more test_remote tests
1636
        client.add_expected_call(
1637
            'Branch.get_stacked_on_url', ('branch/',),
1638
            'error', ('NotStacked',))
1639
        client.add_expected_call(
1640
            'Branch.lock_write', ('branch/', '', ''),
1641
            'success', ('ok', 'branch token', 'repo token'))
1642
        client.add_expected_call(
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1643
            'Branch.last_revision_info',
1644
            ('branch/',),
1645
            'success', ('ok', '0', 'null:'))
1646
        # get_graph calls to construct the revision history, for the set_rh
1647
        # hook
1648
        lines = ['rev-id']
1649
        encoded_body = bz2.compress('\n'.join(lines))
1650
        client.add_success_response_with_body(encoded_body, 'ok')
1651
        client.add_expected_call(
3691.2.9 by Martin Pool
Convert and update more test_remote tests
1652
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id',),
1653
            'error', ('NoSuchRevision', 'rev-id'))
1654
        client.add_expected_call(
1655
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
1656
            'success', ('ok',))
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1657
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1658
        branch = self.make_remote_branch(transport, client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1659
        branch.lock_write()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1660
        self.assertRaises(
5718.7.12 by Jelmer Vernooij
Fix use of _set_last_revision.
1661
            errors.NoSuchRevision, branch._set_last_revision, 'rev-id')
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1662
        branch.unlock()
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1663
        self.assertFinished(client)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1664
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1665
    def test_tip_change_rejected(self):
1666
        """TipChangeRejected responses cause a TipChangeRejected exception to
1667
        be raised.
1668
        """
1669
        transport = MemoryTransport()
1670
        transport.mkdir('branch')
1671
        transport = transport.clone('branch')
1672
        client = FakeClient(transport.base)
1673
        rejection_msg_unicode = u'rejection message\N{INTERROBANG}'
1674
        rejection_msg_utf8 = rejection_msg_unicode.encode('utf8')
3691.2.10 by Martin Pool
Update more test_remote tests
1675
        client.add_expected_call(
1676
            'Branch.get_stacked_on_url', ('branch/',),
1677
            'error', ('NotStacked',))
1678
        client.add_expected_call(
1679
            'Branch.lock_write', ('branch/', '', ''),
1680
            'success', ('ok', 'branch token', 'repo token'))
1681
        client.add_expected_call(
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1682
            'Branch.last_revision_info',
1683
            ('branch/',),
1684
            'success', ('ok', '0', 'null:'))
1685
        lines = ['rev-id']
1686
        encoded_body = bz2.compress('\n'.join(lines))
1687
        client.add_success_response_with_body(encoded_body, 'ok')
1688
        client.add_expected_call(
3691.2.10 by Martin Pool
Update more test_remote tests
1689
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id',),
1690
            'error', ('TipChangeRejected', rejection_msg_utf8))
1691
        client.add_expected_call(
1692
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
1693
            'success', ('ok',))
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1694
        branch = self.make_remote_branch(transport, client)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1695
        branch.lock_write()
1696
        # The 'TipChangeRejected' error response triggered by calling
5718.7.4 by Jelmer Vernooij
Branch.set_revision_history.
1697
        # set_last_revision_info causes a TipChangeRejected exception.
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1698
        err = self.assertRaises(
5718.7.4 by Jelmer Vernooij
Branch.set_revision_history.
1699
            errors.TipChangeRejected,
5718.7.12 by Jelmer Vernooij
Fix use of _set_last_revision.
1700
            branch._set_last_revision, 'rev-id')
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1701
        # The UTF-8 message from the response has been decoded into a unicode
1702
        # object.
1703
        self.assertIsInstance(err.msg, unicode)
1704
        self.assertEqual(rejection_msg_unicode, err.msg)
3691.2.10 by Martin Pool
Update more test_remote tests
1705
        branch.unlock()
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1706
        self.assertFinished(client)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1707
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
1708
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1709
class TestBranchSetLastRevisionInfo(RemoteBranchTestCase):
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1710
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1711
    def test_set_last_revision_info(self):
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1712
        # set_last_revision_info(num, 'rev-id') is translated to calling
1713
        # Branch.set_last_revision_info(num, 'rev-id') on the wire.
3297.4.1 by Andrew Bennetts
Merge 'Add Branch.set_last_revision_info smart method'.
1714
        transport = MemoryTransport()
1715
        transport.mkdir('branch')
1716
        transport = transport.clone('branch')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1717
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1718
        # get_stacked_on_url
1719
        client.add_error_response('NotStacked')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1720
        # lock_write
1721
        client.add_success_response('ok', 'branch token', 'repo token')
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1722
        # query the current revision
1723
        client.add_success_response('ok', '0', 'null:')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1724
        # set_last_revision
1725
        client.add_success_response('ok')
1726
        # unlock
1727
        client.add_success_response('ok')
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1728
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1729
        branch = self.make_remote_branch(transport, client)
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1730
        # Lock the branch, reset the record of remote calls.
1731
        branch.lock_write()
1732
        client._calls = []
1733
        result = branch.set_last_revision_info(1234, 'a-revision-id')
1734
        self.assertEqual(
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1735
            [('call', 'Branch.last_revision_info', ('branch/',)),
1736
             ('call', 'Branch.set_last_revision_info',
3297.4.1 by Andrew Bennetts
Merge 'Add Branch.set_last_revision_info smart method'.
1737
                ('branch/', 'branch token', 'repo token',
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1738
                 '1234', 'a-revision-id'))],
1739
            client._calls)
1740
        self.assertEqual(None, result)
1741
1742
    def test_no_such_revision(self):
1743
        # A response of 'NoSuchRevision' is translated into an exception.
1744
        transport = MemoryTransport()
1745
        transport.mkdir('branch')
1746
        transport = transport.clone('branch')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1747
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1748
        # get_stacked_on_url
1749
        client.add_error_response('NotStacked')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1750
        # lock_write
1751
        client.add_success_response('ok', 'branch token', 'repo token')
1752
        # set_last_revision
1753
        client.add_error_response('NoSuchRevision', 'revid')
1754
        # unlock
1755
        client.add_success_response('ok')
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1756
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1757
        branch = self.make_remote_branch(transport, client)
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1758
        # Lock the branch, reset the record of remote calls.
1759
        branch.lock_write()
1760
        client._calls = []
1761
1762
        self.assertRaises(
1763
            errors.NoSuchRevision, branch.set_last_revision_info, 123, 'revid')
1764
        branch.unlock()
1765
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1766
    def test_backwards_compatibility(self):
1767
        """If the server does not support the Branch.set_last_revision_info
1768
        verb (which is new in 1.4), then the client falls back to VFS methods.
1769
        """
1770
        # This test is a little messy.  Unlike most tests in this file, it
1771
        # doesn't purely test what a Remote* object sends over the wire, and
1772
        # how it reacts to responses from the wire.  It instead relies partly
1773
        # on asserting that the RemoteBranch will call
1774
        # self._real_branch.set_last_revision_info(...).
1775
1776
        # First, set up our RemoteBranch with a FakeClient that raises
1777
        # UnknownSmartMethod, and a StubRealBranch that logs how it is called.
1778
        transport = MemoryTransport()
1779
        transport.mkdir('branch')
1780
        transport = transport.clone('branch')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1781
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1782
        client.add_expected_call(
1783
            'Branch.get_stacked_on_url', ('branch/',),
1784
            'error', ('NotStacked',))
1785
        client.add_expected_call(
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
1786
            'Branch.last_revision_info',
1787
            ('branch/',),
1788
            'success', ('ok', '0', 'null:'))
1789
        client.add_expected_call(
3691.2.10 by Martin Pool
Update more test_remote tests
1790
            'Branch.set_last_revision_info',
1791
            ('branch/', 'branch token', 'repo token', '1234', 'a-revision-id',),
1792
            'unknown', 'Branch.set_last_revision_info')
1793
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1794
        branch = self.make_remote_branch(transport, client)
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1795
        class StubRealBranch(object):
1796
            def __init__(self):
1797
                self.calls = []
1798
            def set_last_revision_info(self, revno, revision_id):
1799
                self.calls.append(
1800
                    ('set_last_revision_info', revno, revision_id))
3441.5.5 by Andrew Bennetts
Some small tweaks and comments.
1801
            def _clear_cached_state(self):
1802
                pass
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1803
        real_branch = StubRealBranch()
1804
        branch._real_branch = real_branch
1805
        self.lock_remote_branch(branch)
1806
1807
        # Call set_last_revision_info, and verify it behaved as expected.
1808
        result = branch.set_last_revision_info(1234, 'a-revision-id')
1809
        self.assertEqual(
1810
            [('set_last_revision_info', 1234, 'a-revision-id')],
1811
            real_branch.calls)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1812
        self.assertFinished(client)
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1813
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1814
    def test_unexpected_error(self):
3697.2.6 by Martin Pool
Merge 261315 fix into 1.7 branch
1815
        # If the server sends an error the client doesn't understand, it gets
1816
        # turned into an UnknownErrorFromSmartServer, which is presented as a
1817
        # non-internal error to the user.
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1818
        transport = MemoryTransport()
1819
        transport.mkdir('branch')
1820
        transport = transport.clone('branch')
1821
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1822
        # get_stacked_on_url
1823
        client.add_error_response('NotStacked')
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1824
        # lock_write
1825
        client.add_success_response('ok', 'branch token', 'repo token')
1826
        # set_last_revision
1827
        client.add_error_response('UnexpectedError')
1828
        # unlock
1829
        client.add_success_response('ok')
1830
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1831
        branch = self.make_remote_branch(transport, client)
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1832
        # Lock the branch, reset the record of remote calls.
1833
        branch.lock_write()
1834
        client._calls = []
1835
1836
        err = self.assertRaises(
3690.1.2 by Andrew Bennetts
Rename UntranslateableErrorFromSmartServer -> UnknownErrorFromSmartServer.
1837
            errors.UnknownErrorFromSmartServer,
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1838
            branch.set_last_revision_info, 123, 'revid')
1839
        self.assertEqual(('UnexpectedError',), err.error_tuple)
1840
        branch.unlock()
1841
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1842
    def test_tip_change_rejected(self):
1843
        """TipChangeRejected responses cause a TipChangeRejected exception to
1844
        be raised.
1845
        """
1846
        transport = MemoryTransport()
1847
        transport.mkdir('branch')
1848
        transport = transport.clone('branch')
1849
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1850
        # get_stacked_on_url
1851
        client.add_error_response('NotStacked')
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1852
        # lock_write
1853
        client.add_success_response('ok', 'branch token', 'repo token')
1854
        # set_last_revision
1855
        client.add_error_response('TipChangeRejected', 'rejection message')
1856
        # unlock
1857
        client.add_success_response('ok')
1858
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1859
        branch = self.make_remote_branch(transport, client)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1860
        # Lock the branch, reset the record of remote calls.
1861
        branch.lock_write()
1862
        self.addCleanup(branch.unlock)
1863
        client._calls = []
1864
1865
        # The 'TipChangeRejected' error response triggered by calling
1866
        # set_last_revision_info causes a TipChangeRejected exception.
1867
        err = self.assertRaises(
1868
            errors.TipChangeRejected,
1869
            branch.set_last_revision_info, 123, 'revid')
1870
        self.assertEqual('rejection message', err.msg)
1871
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1872
4226.2.1 by Robert Collins
Set branch config options via a smart method.
1873
class TestBranchGetSetConfig(RemoteBranchTestCase):
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1874
1875
    def test_get_branch_conf(self):
4226.1.5 by Robert Collins
Reinstate the use of the Branch.get_config_file verb.
1876
        # in an empty branch we decode the response properly
1877
        client = FakeClient()
1878
        client.add_expected_call(
1879
            'Branch.get_stacked_on_url', ('memory:///',),
1880
            'error', ('NotStacked',),)
1881
        client.add_success_response_with_body('# config file body', 'ok')
1882
        transport = MemoryTransport()
1883
        branch = self.make_remote_branch(transport, client)
1884
        config = branch.get_config()
1885
        config.has_explicit_nickname()
1886
        self.assertEqual(
1887
            [('call', 'Branch.get_stacked_on_url', ('memory:///',)),
1888
             ('call_expecting_body', 'Branch.get_config_file', ('memory:///',))],
1889
            client._calls)
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1890
4241.5.2 by Matt Nordhoff
Add a test
1891
    def test_get_multi_line_branch_conf(self):
1892
        # Make sure that multiple-line branch.conf files are supported
1893
        #
5243.1.2 by Martin
Point launchpad links in comments at production server rather than edge
1894
        # https://bugs.launchpad.net/bzr/+bug/354075
4241.5.2 by Matt Nordhoff
Add a test
1895
        client = FakeClient()
1896
        client.add_expected_call(
1897
            'Branch.get_stacked_on_url', ('memory:///',),
1898
            'error', ('NotStacked',),)
1899
        client.add_success_response_with_body('a = 1\nb = 2\nc = 3\n', 'ok')
1900
        transport = MemoryTransport()
1901
        branch = self.make_remote_branch(transport, client)
1902
        config = branch.get_config()
1903
        self.assertEqual(u'2', config.get_user_option('b'))
1904
4226.2.1 by Robert Collins
Set branch config options via a smart method.
1905
    def test_set_option(self):
1906
        client = FakeClient()
1907
        client.add_expected_call(
1908
            'Branch.get_stacked_on_url', ('memory:///',),
1909
            'error', ('NotStacked',),)
1910
        client.add_expected_call(
1911
            'Branch.lock_write', ('memory:///', '', ''),
1912
            'success', ('ok', 'branch token', 'repo token'))
1913
        client.add_expected_call(
1914
            'Branch.set_config_option', ('memory:///', 'branch token',
1915
            'repo token', 'foo', 'bar', ''),
1916
            'success', ())
1917
        client.add_expected_call(
1918
            'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
1919
            'success', ('ok',))
1920
        transport = MemoryTransport()
1921
        branch = self.make_remote_branch(transport, client)
1922
        branch.lock_write()
1923
        config = branch._get_config()
1924
        config.set_option('foo', 'bar')
1925
        branch.unlock()
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
1926
        self.assertFinished(client)
4226.2.1 by Robert Collins
Set branch config options via a smart method.
1927
5227.1.2 by Andrew Bennetts
Add Branch.set_config_option_dict RPC (and VFS fallback), fixes #430382.
1928
    def test_set_option_with_dict(self):
1929
        client = FakeClient()
1930
        client.add_expected_call(
1931
            'Branch.get_stacked_on_url', ('memory:///',),
1932
            'error', ('NotStacked',),)
1933
        client.add_expected_call(
1934
            'Branch.lock_write', ('memory:///', '', ''),
1935
            'success', ('ok', 'branch token', 'repo token'))
1936
        encoded_dict_value = 'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde'
1937
        client.add_expected_call(
1938
            'Branch.set_config_option_dict', ('memory:///', 'branch token',
1939
            'repo token', encoded_dict_value, 'foo', ''),
1940
            'success', ())
1941
        client.add_expected_call(
1942
            'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
1943
            'success', ('ok',))
1944
        transport = MemoryTransport()
1945
        branch = self.make_remote_branch(transport, client)
1946
        branch.lock_write()
1947
        config = branch._get_config()
1948
        config.set_option(
1949
            {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'},
1950
            'foo')
1951
        branch.unlock()
1952
        self.assertFinished(client)
1953
4226.2.1 by Robert Collins
Set branch config options via a smart method.
1954
    def test_backwards_compat_set_option(self):
1955
        self.setup_smart_server_with_call_log()
1956
        branch = self.make_branch('.')
1957
        verb = 'Branch.set_config_option'
1958
        self.disable_verb(verb)
1959
        branch.lock_write()
1960
        self.addCleanup(branch.unlock)
1961
        self.reset_smart_call_log()
1962
        branch._get_config().set_option('value', 'name')
1963
        self.assertLength(10, self.hpss_calls)
1964
        self.assertEqual('value', branch._get_config().get_option('name'))
1965
5227.1.2 by Andrew Bennetts
Add Branch.set_config_option_dict RPC (and VFS fallback), fixes #430382.
1966
    def test_backwards_compat_set_option_with_dict(self):
1967
        self.setup_smart_server_with_call_log()
1968
        branch = self.make_branch('.')
1969
        verb = 'Branch.set_config_option_dict'
1970
        self.disable_verb(verb)
1971
        branch.lock_write()
1972
        self.addCleanup(branch.unlock)
1973
        self.reset_smart_call_log()
1974
        config = branch._get_config()
1975
        value_dict = {'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
1976
        config.set_option(value_dict, 'name')
1977
        self.assertLength(10, self.hpss_calls)
1978
        self.assertEqual(value_dict, branch._get_config().get_option('name'))
1979
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1980
6270.1.23 by Jelmer Vernooij
Add notes about Remote{Control,Branch}Store.
1981
class TestBranchGetPutConfigStore(RemoteBranchTestCase):
6270.1.10 by Jelmer Vernooij
Fix testing Branch.set_config_file.
1982
1983
    def test_get_branch_conf(self):
1984
        # in an empty branch we decode the response properly
1985
        client = FakeClient()
1986
        client.add_expected_call(
1987
            'Branch.get_stacked_on_url', ('memory:///',),
1988
            'error', ('NotStacked',),)
1989
        client.add_success_response_with_body('# config file body', 'ok')
1990
        transport = MemoryTransport()
1991
        branch = self.make_remote_branch(transport, client)
1992
        config = branch.get_config_stack()
1993
        config.get("email")
1994
        config.get("log_format")
1995
        self.assertEqual(
1996
            [('call', 'Branch.get_stacked_on_url', ('memory:///',)),
1997
             ('call_expecting_body', 'Branch.get_config_file', ('memory:///',))],
1998
            client._calls)
1999
2000
    def test_set_branch_conf(self):
2001
        client = FakeClient()
2002
        client.add_expected_call(
2003
            'Branch.get_stacked_on_url', ('memory:///',),
2004
            'error', ('NotStacked',),)
2005
        client.add_expected_call(
2006
            'Branch.lock_write', ('memory:///', '', ''),
2007
            'success', ('ok', 'branch token', 'repo token'))
2008
        client.add_expected_call(
2009
            'Branch.get_config_file', ('memory:///', ),
2010
            'success', ('ok', ), "# line 1\n")
2011
        client.add_expected_call(
6270.1.17 by Jelmer Vernooij
s/set_config_file/put_config_file.
2012
            'Branch.put_config_file', ('memory:///', 'branch token',
6270.1.10 by Jelmer Vernooij
Fix testing Branch.set_config_file.
2013
            'repo token'),
6270.1.16 by Jelmer Vernooij
Expect 'ok' response from set_config_file.
2014
            'success', ('ok',))
6270.1.10 by Jelmer Vernooij
Fix testing Branch.set_config_file.
2015
        client.add_expected_call(
2016
            'Branch.unlock', ('memory:///', 'branch token', 'repo token'),
2017
            'success', ('ok',))
2018
        transport = MemoryTransport()
2019
        branch = self.make_remote_branch(transport, client)
2020
        branch.lock_write()
2021
        config = branch.get_config_stack()
2022
        config.set('email', 'The Dude <lebowski@example.com>')
2023
        branch.unlock()
2024
        self.assertFinished(client)
2025
        self.assertEqual(
2026
            [('call', 'Branch.get_stacked_on_url', ('memory:///',)),
2027
             ('call', 'Branch.lock_write', ('memory:///', '', '')),
2028
             ('call_expecting_body', 'Branch.get_config_file', ('memory:///',)),
6270.1.18 by Jelmer Vernooij
Fix a test.
2029
             ('call_with_body_bytes_expecting_body', 'Branch.put_config_file',
6270.1.10 by Jelmer Vernooij
Fix testing Branch.set_config_file.
2030
                 ('memory:///', 'branch token', 'repo token'),
2031
                 '# line 1\nemail = The Dude <lebowski@example.com>\n'),
2032
             ('call', 'Branch.unlock', ('memory:///', 'branch token', 'repo token'))],
2033
            client._calls)
2034
2035
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
2036
class TestBranchLockWrite(RemoteBranchTestCase):
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
2037
2038
    def test_lock_write_unlockable(self):
2039
        transport = MemoryTransport()
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2040
        client = FakeClient(transport.base)
3691.2.9 by Martin Pool
Convert and update more test_remote tests
2041
        client.add_expected_call(
2042
            'Branch.get_stacked_on_url', ('quack/',),
2043
            'error', ('NotStacked',),)
2044
        client.add_expected_call(
2045
            'Branch.lock_write', ('quack/', '', ''),
2046
            'error', ('UnlockableTransport',))
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
2047
        transport.mkdir('quack')
2048
        transport = transport.clone('quack')
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
2049
        branch = self.make_remote_branch(transport, client)
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
2050
        self.assertRaises(errors.UnlockableTransport, branch.lock_write)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
2051
        self.assertFinished(client)
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
2052
2053
6263.1.3 by Jelmer Vernooij
Add dotted revno test.
2054
class TestBranchRevisionIdToRevno(RemoteBranchTestCase):
2055
2056
    def test_simple(self):
2057
        transport = MemoryTransport()
2058
        client = FakeClient(transport.base)
2059
        client.add_expected_call(
2060
            'Branch.get_stacked_on_url', ('quack/',),
2061
            'error', ('NotStacked',),)
2062
        client.add_expected_call(
2063
            'Branch.revision_id_to_revno', ('quack/', 'null:'),
2064
            'success', ('ok', '0',),)
2065
        client.add_expected_call(
2066
            'Branch.revision_id_to_revno', ('quack/', 'unknown'),
2067
            'error', ('NoSuchRevision', 'unknown',),)
2068
        transport.mkdir('quack')
2069
        transport = transport.clone('quack')
2070
        branch = self.make_remote_branch(transport, client)
2071
        self.assertEquals(0, branch.revision_id_to_revno('null:'))
2072
        self.assertRaises(errors.NoSuchRevision,
2073
            branch.revision_id_to_revno, 'unknown')
2074
        self.assertFinished(client)
2075
2076
    def test_dotted(self):
2077
        transport = MemoryTransport()
2078
        client = FakeClient(transport.base)
2079
        client.add_expected_call(
2080
            'Branch.get_stacked_on_url', ('quack/',),
2081
            'error', ('NotStacked',),)
2082
        client.add_expected_call(
2083
            'Branch.revision_id_to_revno', ('quack/', 'null:'),
2084
            'success', ('ok', '0',),)
2085
        client.add_expected_call(
2086
            'Branch.revision_id_to_revno', ('quack/', 'unknown'),
2087
            'error', ('NoSuchRevision', 'unknown',),)
2088
        transport.mkdir('quack')
2089
        transport = transport.clone('quack')
2090
        branch = self.make_remote_branch(transport, client)
2091
        self.assertEquals((0, ), branch.revision_id_to_dotted_revno('null:'))
2092
        self.assertRaises(errors.NoSuchRevision,
2093
            branch.revision_id_to_dotted_revno, 'unknown')
2094
        self.assertFinished(client)
2095
6305.1.1 by Jelmer Vernooij
Add test for Branch.revision_id_to_dotted_revno fallback.
2096
    def test_dotted_no_smart_verb(self):
2097
        self.setup_smart_server_with_call_log()
2098
        branch = self.make_branch('.')
2099
        self.disable_verb('Branch.revision_id_to_revno')
2100
        self.reset_smart_call_log()
2101
        self.assertEquals((0, ),
2102
            branch.revision_id_to_dotted_revno('null:'))
2103
        self.assertLength(7, self.hpss_calls)
2104
6263.1.3 by Jelmer Vernooij
Add dotted revno test.
2105
4288.1.1 by Robert Collins
Add support for a RemoteBzrDirConfig to support optimising push operations which need to look for default stacking locations.
2106
class TestBzrDirGetSetConfig(RemoteBzrDirTestCase):
2107
2108
    def test__get_config(self):
2109
        client = FakeClient()
2110
        client.add_success_response_with_body('default_stack_on = /\n', 'ok')
2111
        transport = MemoryTransport()
2112
        bzrdir = self.make_remote_bzrdir(transport, client)
2113
        config = bzrdir.get_config()
2114
        self.assertEqual('/', config.get_default_stack_on())
2115
        self.assertEqual(
2116
            [('call_expecting_body', 'BzrDir.get_config_file', ('memory:///',))],
2117
            client._calls)
2118
2119
    def test_set_option_uses_vfs(self):
2120
        self.setup_smart_server_with_call_log()
2121
        bzrdir = self.make_bzrdir('.')
2122
        self.reset_smart_call_log()
2123
        config = bzrdir.get_config()
2124
        config.set_default_stack_on('/')
2125
        self.assertLength(3, self.hpss_calls)
2126
2127
    def test_backwards_compat_get_option(self):
2128
        self.setup_smart_server_with_call_log()
2129
        bzrdir = self.make_bzrdir('.')
2130
        verb = 'BzrDir.get_config_file'
4288.1.2 by Robert Collins
Create a server verb for doing BzrDir.get_config()
2131
        self.disable_verb(verb)
4288.1.1 by Robert Collins
Add support for a RemoteBzrDirConfig to support optimising push operations which need to look for default stacking locations.
2132
        self.reset_smart_call_log()
2133
        self.assertEqual(None,
2134
            bzrdir._get_config().get_option('default_stack_on'))
2135
        self.assertLength(3, self.hpss_calls)
2136
2137
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
2138
class TestTransportIsReadonly(tests.TestCase):
2139
2140
    def test_true(self):
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2141
        client = FakeClient()
2142
        client.add_success_response('yes')
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
2143
        transport = RemoteTransport('bzr://example.com/', medium=False,
2144
                                    _client=client)
2145
        self.assertEqual(True, transport.is_readonly())
2146
        self.assertEqual(
2147
            [('call', 'Transport.is_readonly', ())],
2148
            client._calls)
2149
2150
    def test_false(self):
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2151
        client = FakeClient()
2152
        client.add_success_response('no')
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
2153
        transport = RemoteTransport('bzr://example.com/', medium=False,
2154
                                    _client=client)
2155
        self.assertEqual(False, transport.is_readonly())
2156
        self.assertEqual(
2157
            [('call', 'Transport.is_readonly', ())],
2158
            client._calls)
2159
2160
    def test_error_from_old_server(self):
2161
        """bzr 0.15 and earlier servers don't recognise the is_readonly verb.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2162
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
2163
        Clients should treat it as a "no" response, because is_readonly is only
2164
        advisory anyway (a transport could be read-write, but then the
2165
        underlying filesystem could be readonly anyway).
2166
        """
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2167
        client = FakeClient()
2168
        client.add_unknown_method_response('Transport.is_readonly')
2471.2.1 by Andrew Bennetts
Fix trivial incompatibility with bzr 0.11 servers, which give a slightly different error to bzr 0.15 servers.
2169
        transport = RemoteTransport('bzr://example.com/', medium=False,
2170
                                    _client=client)
2171
        self.assertEqual(False, transport.is_readonly())
2172
        self.assertEqual(
2173
            [('call', 'Transport.is_readonly', ())],
2174
            client._calls)
2175
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
2176
3840.1.1 by Andrew Bennetts
Fix RemoteTransport's translation of errors involving paths; it wasn't passing orig_path to _translate_error.
2177
class TestTransportMkdir(tests.TestCase):
2178
2179
    def test_permissiondenied(self):
2180
        client = FakeClient()
2181
        client.add_error_response('PermissionDenied', 'remote path', 'extra')
2182
        transport = RemoteTransport('bzr://example.com/', medium=False,
2183
                                    _client=client)
2184
        exc = self.assertRaises(
2185
            errors.PermissionDenied, transport.mkdir, 'client path')
2186
        expected_error = errors.PermissionDenied('/client path', 'extra')
2187
        self.assertEqual(expected_error, exc)
2188
2189
3777.1.3 by Aaron Bentley
Use SSH default username from authentication.conf
2190
class TestRemoteSSHTransportAuthentication(tests.TestCaseInTempDir):
2191
4304.2.1 by Vincent Ladeuil
Fix bug #367726 by reverting some default user handling introduced
2192
    def test_defaults_to_none(self):
3777.1.3 by Aaron Bentley
Use SSH default username from authentication.conf
2193
        t = RemoteSSHTransport('bzr+ssh://example.com')
4304.2.1 by Vincent Ladeuil
Fix bug #367726 by reverting some default user handling introduced
2194
        self.assertIs(None, t._get_credentials()[0])
3777.1.3 by Aaron Bentley
Use SSH default username from authentication.conf
2195
2196
    def test_uses_authentication_config(self):
2197
        conf = config.AuthenticationConfig()
2198
        conf._get_config().update(
2199
            {'bzr+sshtest': {'scheme': 'ssh', 'user': 'bar', 'host':
2200
            'example.com'}})
2201
        conf._save()
2202
        t = RemoteSSHTransport('bzr+ssh://example.com')
2203
        self.assertEqual('bar', t._get_credentials()[0])
2204
2205
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
2206
class TestRemoteRepository(TestRemote):
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
2207
    """Base for testing RemoteRepository protocol usage.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2208
2209
    These tests contain frozen requests and responses.  We want any changes to
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
2210
    what is sent or expected to be require a thoughtful update to these tests
2211
    because they might break compatibility with different-versioned servers.
2212
    """
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
2213
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2214
    def setup_fake_client_and_repository(self, transport_path):
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
2215
        """Create the fake client and repository for testing with.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2216
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
2217
        There's no real server here; we just have canned responses sent
2218
        back one by one.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2219
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
2220
        :param transport_path: Path below the root of the MemoryTransport
2221
            where the repository will be created.
2222
        """
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
2223
        transport = MemoryTransport()
2224
        transport.mkdir(transport_path)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2225
        client = FakeClient(transport.base)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
2226
        transport = transport.clone(transport_path)
2227
        # we do not want bzrdir to make any remote calls
5712.3.17 by Jelmer Vernooij
more fixes.
2228
        bzrdir = RemoteBzrDir(transport, RemoteBzrDirFormat(),
4005.2.1 by Robert Collins
Fix RemoteBranch to be used correctly in tests using bzr+ssh, to fire off Branch hooks correctly, and improve the branch_implementations tests to check that making a branch gets the right format under test.
2229
            _client=False)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
2230
        repo = RemoteRepository(bzrdir, None, _client=client)
2231
        return repo, client
2232
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
2233
4792.1.1 by Andrew Bennetts
Show real branch/repo format description in 'info -v' over HPSS.
2234
def remoted_description(format):
2235
    return 'Remote: ' + format.get_format_description()
2236
2237
2238
class TestBranchFormat(tests.TestCase):
2239
2240
    def test_get_format_description(self):
2241
        remote_format = RemoteBranchFormat()
5662.2.2 by Jelmer Vernooij
Move most format registration functions to BranchFormatRegistry.
2242
        real_format = branch.format_registry.get_default()
4792.1.1 by Andrew Bennetts
Show real branch/repo format description in 'info -v' over HPSS.
2243
        remote_format._network_name = real_format.network_name()
2244
        self.assertEqual(remoted_description(real_format),
2245
            remote_format.get_format_description())
2246
2247
4183.5.1 by Robert Collins
Add RepositoryFormat.fast_deltas to signal fast delta creation.
2248
class TestRepositoryFormat(TestRemoteRepository):
2249
2250
    def test_fast_delta(self):
5546.1.1 by Andrew Bennetts
Remove RepositoryFormatCHK1 and RepositoryFormatCHK2.
2251
        true_name = groupcompress_repo.RepositoryFormat2a().network_name()
4183.5.1 by Robert Collins
Add RepositoryFormat.fast_deltas to signal fast delta creation.
2252
        true_format = RemoteRepositoryFormat()
2253
        true_format._network_name = true_name
2254
        self.assertEqual(True, true_format.fast_deltas)
5757.1.7 by Jelmer Vernooij
Fix more imports.
2255
        false_name = knitpack_repo.RepositoryFormatKnitPack1().network_name()
4183.5.1 by Robert Collins
Add RepositoryFormat.fast_deltas to signal fast delta creation.
2256
        false_format = RemoteRepositoryFormat()
2257
        false_format._network_name = false_name
2258
        self.assertEqual(False, false_format.fast_deltas)
2259
4792.1.1 by Andrew Bennetts
Show real branch/repo format description in 'info -v' over HPSS.
2260
    def test_get_format_description(self):
2261
        remote_repo_format = RemoteRepositoryFormat()
5651.3.9 by Jelmer Vernooij
Avoid using deprecated functions.
2262
        real_format = repository.format_registry.get_default()
4792.1.1 by Andrew Bennetts
Show real branch/repo format description in 'info -v' over HPSS.
2263
        remote_repo_format._network_name = real_format.network_name()
2264
        self.assertEqual(remoted_description(real_format),
2265
            remote_repo_format.get_format_description())
2266
4183.5.1 by Robert Collins
Add RepositoryFormat.fast_deltas to signal fast delta creation.
2267
6280.3.1 by Jelmer Vernooij
Add remote side of Repository.all_revision_ids.
2268
class TestRepositoryAllRevisionIds(TestRemoteRepository):
2269
2270
    def test_empty(self):
2271
        transport_path = 'quack'
2272
        repo, client = self.setup_fake_client_and_repository(transport_path)
2273
        client.add_success_response_with_body('', 'ok')
2274
        self.assertEquals([], repo.all_revision_ids())
2275
        self.assertEqual(
2276
            [('call_expecting_body', 'Repository.all_revision_ids',
2277
             ('quack/',))],
2278
            client._calls)
2279
2280
    def test_with_some_content(self):
2281
        transport_path = 'quack'
2282
        repo, client = self.setup_fake_client_and_repository(transport_path)
2283
        client.add_success_response_with_body(
2284
            'rev1\nrev2\nanotherrev\n', 'ok')
2285
        self.assertEquals(["rev1", "rev2", "anotherrev"],
2286
            repo.all_revision_ids())
2287
        self.assertEqual(
2288
            [('call_expecting_body', 'Repository.all_revision_ids',
2289
             ('quack/',))],
2290
            client._calls)
2291
2292
2018.12.2 by Andrew Bennetts
Remove some duplicate code in test_remote
2293
class TestRepositoryGatherStats(TestRemoteRepository):
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
2294
2295
    def test_revid_none(self):
2296
        # ('ok',), body with revisions and size
2297
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2298
        repo, client = self.setup_fake_client_and_repository(transport_path)
2299
        client.add_success_response_with_body(
2300
            'revisions: 2\nsize: 18\n', 'ok')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
2301
        result = repo.gather_stats(None)
2302
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
2303
            [('call_expecting_body', 'Repository.gather_stats',
3104.4.2 by Andrew Bennetts
All tests passing.
2304
             ('quack/','','no'))],
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
2305
            client._calls)
2306
        self.assertEqual({'revisions': 2, 'size': 18}, result)
2307
2308
    def test_revid_no_committers(self):
2309
        # ('ok',), body without committers
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2310
        body = ('firstrev: 123456.300 3600\n'
2311
                'latestrev: 654231.400 0\n'
2312
                'revisions: 2\n'
2313
                'size: 18\n')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
2314
        transport_path = 'quick'
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
2315
        revid = u'\xc8'.encode('utf8')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2316
        repo, client = self.setup_fake_client_and_repository(transport_path)
2317
        client.add_success_response_with_body(body, 'ok')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
2318
        result = repo.gather_stats(revid)
2319
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
2320
            [('call_expecting_body', 'Repository.gather_stats',
3104.4.2 by Andrew Bennetts
All tests passing.
2321
              ('quick/', revid, 'no'))],
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
2322
            client._calls)
2323
        self.assertEqual({'revisions': 2, 'size': 18,
2324
                          'firstrev': (123456.300, 3600),
2325
                          'latestrev': (654231.400, 0),},
2326
                         result)
2327
2328
    def test_revid_with_committers(self):
2329
        # ('ok',), body with committers
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2330
        body = ('committers: 128\n'
2331
                'firstrev: 123456.300 3600\n'
2332
                'latestrev: 654231.400 0\n'
2333
                'revisions: 2\n'
2334
                'size: 18\n')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
2335
        transport_path = 'buick'
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
2336
        revid = u'\xc8'.encode('utf8')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2337
        repo, client = self.setup_fake_client_and_repository(transport_path)
2338
        client.add_success_response_with_body(body, 'ok')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
2339
        result = repo.gather_stats(revid, True)
2340
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
2341
            [('call_expecting_body', 'Repository.gather_stats',
3104.4.2 by Andrew Bennetts
All tests passing.
2342
              ('buick/', revid, 'yes'))],
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
2343
            client._calls)
2344
        self.assertEqual({'revisions': 2, 'size': 18,
2345
                          'committers': 128,
2346
                          'firstrev': (123456.300, 3600),
2347
                          'latestrev': (654231.400, 0),},
2348
                         result)
2349
2350
6280.4.1 by Jelmer Vernooij
Add remote side of Repository.break_lock.
2351
class TestRepositoryBreakLock(TestRemoteRepository):
2352
2353
    def test_break_lock(self):
2354
        transport_path = 'quack'
2355
        repo, client = self.setup_fake_client_and_repository(transport_path)
2356
        client.add_success_response('ok')
2357
        repo.break_lock()
2358
        self.assertEqual(
6280.4.6 by Jelmer Vernooij
Fix test.
2359
            [('call', 'Repository.break_lock', ('quack/',))],
6280.4.1 by Jelmer Vernooij
Add remote side of Repository.break_lock.
2360
            client._calls)
2361
2362
6280.5.1 by Jelmer Vernooij
Add client side of Repository.get_serializer_format.
2363
class TestRepositoryGetSerializerFormat(TestRemoteRepository):
2364
2365
    def test_get_serializer_format(self):
2366
        transport_path = 'hill'
2367
        repo, client = self.setup_fake_client_and_repository(transport_path)
2368
        client.add_success_response('ok', '7')
6280.5.2 by Jelmer Vernooij
New HPSS call VersionedFileRepository.get_serializer_format.
2369
        self.assertEquals('7', repo.get_serializer_format())
6280.5.1 by Jelmer Vernooij
Add client side of Repository.get_serializer_format.
2370
        self.assertEqual(
2371
            [('call', 'VersionedFileRepository.get_serializer_format',
2372
              ('hill/', ))],
2373
            client._calls)
2374
2375
6300.1.1 by Jelmer Vernooij
Add remote implementation of RemoteRepository.reconcile.
2376
class TestRepositoryReconcile(TestRemoteRepository):
2377
2378
    def test_reconcile(self):
2379
        transport_path = 'hill'
2380
        repo, client = self.setup_fake_client_and_repository(transport_path)
6300.1.4 by Jelmer Vernooij
Add reconcile results.
2381
        body = ("garbage_inventories: 2\n"
2382
                "inconsistent_parents: 3\n")
6300.1.7 by Jelmer Vernooij
Fix test.
2383
        client.add_expected_call(
2384
            'Repository.lock_write', ('hill/', ''),
2385
            'success', ('ok', 'a token'))
6300.1.4 by Jelmer Vernooij
Add reconcile results.
2386
        client.add_success_response_with_body(body, 'ok')
2387
        reconciler = repo.reconcile()
6300.1.1 by Jelmer Vernooij
Add remote implementation of RemoteRepository.reconcile.
2388
        self.assertEqual(
6300.1.7 by Jelmer Vernooij
Fix test.
2389
            [('call', 'Repository.lock_write', ('hill/', '')),
2390
             ('call_expecting_body', 'Repository.reconcile',
2391
                ('hill/', 'a token'))],
6300.1.1 by Jelmer Vernooij
Add remote implementation of RemoteRepository.reconcile.
2392
            client._calls)
6300.1.4 by Jelmer Vernooij
Add reconcile results.
2393
        self.assertEquals(2, reconciler.garbage_inventories)
2394
        self.assertEquals(3, reconciler.inconsistent_parents)
6300.1.1 by Jelmer Vernooij
Add remote implementation of RemoteRepository.reconcile.
2395
2396
6263.3.2 by Jelmer Vernooij
Add new HPSS call 'Repository.get_revision_signature_text'.
2397
class TestRepositoryGetRevisionSignatureText(TestRemoteRepository):
2398
2399
    def test_text(self):
2400
        # ('ok',), body with signature text
2401
        transport_path = 'quack'
2402
        repo, client = self.setup_fake_client_and_repository(transport_path)
2403
        client.add_success_response_with_body(
2404
            'THETEXT', 'ok')
2405
        self.assertEquals("THETEXT", repo.get_signature_text("revid"))
2406
        self.assertEqual(
2407
            [('call_expecting_body', 'Repository.get_revision_signature_text',
2408
             ('quack/', 'revid'))],
2409
            client._calls)
2410
2411
    def test_no_signature(self):
2412
        transport_path = 'quick'
2413
        repo, client = self.setup_fake_client_and_repository(transport_path)
2414
        client.add_error_response('nosuchrevision', 'unknown')
2415
        self.assertRaises(errors.NoSuchRevision, repo.get_signature_text,
2416
                "unknown")
2417
        self.assertEqual(
2418
            [('call_expecting_body', 'Repository.get_revision_signature_text',
2419
              ('quick/', 'unknown'))],
2420
            client._calls)
2421
2422
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2423
class TestRepositoryGetGraph(TestRemoteRepository):
2424
2425
    def test_get_graph(self):
3835.1.6 by Aaron Bentley
Reduce inefficiency when doing make_parents_provider frequently
2426
        # get_graph returns a graph with a custom parents provider.
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2427
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2428
        repo, client = self.setup_fake_client_and_repository(transport_path)
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2429
        graph = repo.get_graph()
3835.1.6 by Aaron Bentley
Reduce inefficiency when doing make_parents_provider frequently
2430
        self.assertNotEqual(graph._parents_provider, repo)
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2431
2432
6268.1.2 by Jelmer Vernooij
Initial work on Repository.add_signature_text.
2433
class TestRepositoryAddSignatureText(TestRemoteRepository):
2434
2435
    def test_add_signature_text(self):
2436
        transport_path = 'quack'
2437
        repo, client = self.setup_fake_client_and_repository(transport_path)
6280.10.32 by Jelmer Vernooij
Merge bzr.dev.
2438
        client.add_expected_call(
2439
            'Repository.lock_write', ('quack/', ''),
2440
            'success', ('ok', 'a token'))
2441
        client.add_expected_call(
2442
            'Repository.start_write_group', ('quack/', 'a token'),
2443
            'success', ('ok', ('token1', )))
2444
        client.add_expected_call(
2445
            'Repository.add_signature_text', ('quack/', 'a token', 'rev1',
2446
                'token1'),
2447
            'success', ('ok', ), None)
2448
        repo.lock_write()
2449
        repo.start_write_group()
6268.1.2 by Jelmer Vernooij
Initial work on Repository.add_signature_text.
2450
        self.assertIs(None,
2451
            repo.add_signature_text("rev1", "every bloody emperor"))
2452
        self.assertEqual(
6280.10.39 by Jelmer Vernooij
Merge bzr.dev.
2453
            ('call_with_body_bytes_expecting_body',
6280.10.32 by Jelmer Vernooij
Merge bzr.dev.
2454
              'Repository.add_signature_text',
2455
                ('quack/', 'a token', 'rev1', 'token1'),
2456
              'every bloody emperor'),
2457
            client._calls[-1])
6268.1.2 by Jelmer Vernooij
Initial work on Repository.add_signature_text.
2458
2459
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2460
class TestRepositoryGetParentMap(TestRemoteRepository):
2461
2462
    def test_get_parent_map_caching(self):
2463
        # get_parent_map returns from cache until unlock()
2464
        # setup a reponse with two revisions
2465
        r1 = u'\u0e33'.encode('utf8')
2466
        r2 = u'\u0dab'.encode('utf8')
2467
        lines = [' '.join([r2, r1]), r1]
3211.5.2 by Robert Collins
Change RemoteRepository.get_parent_map to use bz2 not gzip for compression.
2468
        encoded_body = bz2.compress('\n'.join(lines))
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2469
2470
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2471
        repo, client = self.setup_fake_client_and_repository(transport_path)
2472
        client.add_success_response_with_body(encoded_body, 'ok')
2473
        client.add_success_response_with_body(encoded_body, 'ok')
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2474
        repo.lock_read()
2475
        graph = repo.get_graph()
2476
        parents = graph.get_parent_map([r2])
2477
        self.assertEqual({r2: (r1,)}, parents)
2478
        # locking and unlocking deeper should not reset
2479
        repo.lock_read()
2480
        repo.unlock()
2481
        parents = graph.get_parent_map([r1])
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
2482
        self.assertEqual({r1: (NULL_REVISION,)}, parents)
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2483
        self.assertEqual(
3211.5.1 by Robert Collins
Change the smart server get_parents method to take a graph search to exclude already recieved parents from. This prevents history shortcuts causing huge numbers of duplicates.
2484
            [('call_with_body_bytes_expecting_body',
4190.1.6 by Robert Collins
Missed some unit tests.
2485
              'Repository.get_parent_map', ('quack/', 'include-missing:', r2),
2486
              '\n\n0')],
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2487
            client._calls)
2488
        repo.unlock()
2489
        # now we call again, and it should use the second response.
2490
        repo.lock_read()
2491
        graph = repo.get_graph()
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
2492
        parents = graph.get_parent_map([r1])
2493
        self.assertEqual({r1: (NULL_REVISION,)}, parents)
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2494
        self.assertEqual(
3211.5.1 by Robert Collins
Change the smart server get_parents method to take a graph search to exclude already recieved parents from. This prevents history shortcuts causing huge numbers of duplicates.
2495
            [('call_with_body_bytes_expecting_body',
4190.1.6 by Robert Collins
Missed some unit tests.
2496
              'Repository.get_parent_map', ('quack/', 'include-missing:', r2),
2497
              '\n\n0'),
3211.5.1 by Robert Collins
Change the smart server get_parents method to take a graph search to exclude already recieved parents from. This prevents history shortcuts causing huge numbers of duplicates.
2498
             ('call_with_body_bytes_expecting_body',
4190.1.6 by Robert Collins
Missed some unit tests.
2499
              'Repository.get_parent_map', ('quack/', 'include-missing:', r1),
2500
              '\n\n0'),
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2501
            ],
2502
            client._calls)
2503
        repo.unlock()
2504
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
2505
    def test_get_parent_map_reconnects_if_unknown_method(self):
2506
        transport_path = 'quack'
3948.3.7 by Martin Pool
Updated tests for RemoteRepository.get_parent_map on old servers.
2507
        rev_id = 'revision-id'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2508
        repo, client = self.setup_fake_client_and_repository(transport_path)
3948.3.7 by Martin Pool
Updated tests for RemoteRepository.get_parent_map on old servers.
2509
        client.add_unknown_method_response('Repository.get_parent_map')
2510
        client.add_success_response_with_body(rev_id, 'ok')
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
2511
        self.assertFalse(client._medium._is_remote_before((1, 2)))
3948.3.7 by Martin Pool
Updated tests for RemoteRepository.get_parent_map on old servers.
2512
        parents = repo.get_parent_map([rev_id])
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
2513
        self.assertEqual(
3213.1.8 by Andrew Bennetts
Merge from bzr.dev.
2514
            [('call_with_body_bytes_expecting_body',
6015.23.17 by John Arbash Meinel
Code was relying on an empty parent map to yield an empty search.
2515
              'Repository.get_parent_map',
2516
              ('quack/', 'include-missing:', rev_id), '\n\n0'),
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
2517
             ('disconnect medium',),
2518
             ('call_expecting_body', 'Repository.get_revision_graph',
2519
              ('quack/', ''))],
2520
            client._calls)
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
2521
        # The medium is now marked as being connected to an older server
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
2522
        self.assertTrue(client._medium._is_remote_before((1, 2)))
3948.3.7 by Martin Pool
Updated tests for RemoteRepository.get_parent_map on old servers.
2523
        self.assertEqual({rev_id: ('null:',)}, parents)
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
2524
2525
    def test_get_parent_map_fallback_parentless_node(self):
2526
        """get_parent_map falls back to get_revision_graph on old servers.  The
2527
        results from get_revision_graph are tweaked to match the get_parent_map
2528
        API.
2529
3389.1.3 by Andrew Bennetts
Remove XXX from test description.
2530
        Specifically, a {key: ()} result from get_revision_graph means "no
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
2531
        parents" for that key, which in get_parent_map results should be
3389.1.3 by Andrew Bennetts
Remove XXX from test description.
2532
        represented as {key: ('null:',)}.
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
2533
2534
        This is the test for https://bugs.launchpad.net/bzr/+bug/214894
2535
        """
2536
        rev_id = 'revision-id'
2537
        transport_path = 'quack'
3245.4.40 by Andrew Bennetts
Merge from bzr.dev.
2538
        repo, client = self.setup_fake_client_and_repository(transport_path)
2539
        client.add_success_response_with_body(rev_id, 'ok')
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
2540
        client._medium._remember_remote_is_before((1, 2))
3948.3.7 by Martin Pool
Updated tests for RemoteRepository.get_parent_map on old servers.
2541
        parents = repo.get_parent_map([rev_id])
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
2542
        self.assertEqual(
2543
            [('call_expecting_body', 'Repository.get_revision_graph',
2544
             ('quack/', ''))],
2545
            client._calls)
2546
        self.assertEqual({rev_id: ('null:',)}, parents)
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
2547
3297.2.3 by Andrew Bennetts
Test the code path that the typo is on.
2548
    def test_get_parent_map_unexpected_response(self):
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2549
        repo, client = self.setup_fake_client_and_repository('path')
2550
        client.add_success_response('something unexpected!')
3297.2.3 by Andrew Bennetts
Test the code path that the typo is on.
2551
        self.assertRaises(
2552
            errors.UnexpectedSmartServerResponse,
2553
            repo.get_parent_map, ['a-revision-id'])
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
2554
4190.1.1 by Robert Collins
Negatively cache misses during read-locks in RemoteRepository.
2555
    def test_get_parent_map_negative_caches_missing_keys(self):
2556
        self.setup_smart_server_with_call_log()
2557
        repo = self.make_repository('foo')
2558
        self.assertIsInstance(repo, RemoteRepository)
2559
        repo.lock_read()
2560
        self.addCleanup(repo.unlock)
2561
        self.reset_smart_call_log()
2562
        graph = repo.get_graph()
2563
        self.assertEqual({},
2564
            graph.get_parent_map(['some-missing', 'other-missing']))
2565
        self.assertLength(1, self.hpss_calls)
2566
        # No call if we repeat this
2567
        self.reset_smart_call_log()
2568
        graph = repo.get_graph()
2569
        self.assertEqual({},
2570
            graph.get_parent_map(['some-missing', 'other-missing']))
2571
        self.assertLength(0, self.hpss_calls)
2572
        # Asking for more unknown keys makes a request.
2573
        self.reset_smart_call_log()
2574
        graph = repo.get_graph()
2575
        self.assertEqual({},
2576
            graph.get_parent_map(['some-missing', 'other-missing',
2577
                'more-missing']))
2578
        self.assertLength(1, self.hpss_calls)
2579
4214.2.1 by Andrew Bennetts
A long but failing test for the get_parent_map RPC bug.
2580
    def disableExtraResults(self):
4985.1.5 by Vincent Ladeuil
Deploying the new overrideAttr facility further reduces the complexity
2581
        self.overrideAttr(SmartServerRepositoryGetParentMap,
2582
                          'no_extra_results', True)
4214.2.1 by Andrew Bennetts
A long but failing test for the get_parent_map RPC bug.
2583
4214.2.5 by Andrew Bennetts
Fix the bug.
2584
    def test_null_cached_missing_and_stop_key(self):
4214.2.1 by Andrew Bennetts
A long but failing test for the get_parent_map RPC bug.
2585
        self.setup_smart_server_with_call_log()
4214.2.4 by Andrew Bennetts
Further simplify the test to reproduce the bug.
2586
        # Make a branch with a single revision.
4214.2.1 by Andrew Bennetts
A long but failing test for the get_parent_map RPC bug.
2587
        builder = self.make_branch_builder('foo')
2588
        builder.start_series()
2589
        builder.build_snapshot('first', None, [
2590
            ('add', ('', 'root-id', 'directory', ''))])
2591
        builder.finish_series()
2592
        branch = builder.get_branch()
2593
        repo = branch.repository
2594
        self.assertIsInstance(repo, RemoteRepository)
4214.2.3 by Andrew Bennetts
Further simplify test case, and add more comments.
2595
        # Stop the server from sending extra results.
2596
        self.disableExtraResults()
4214.2.1 by Andrew Bennetts
A long but failing test for the get_parent_map RPC bug.
2597
        repo.lock_read()
2598
        self.addCleanup(repo.unlock)
2599
        self.reset_smart_call_log()
2600
        graph = repo.get_graph()
4214.2.4 by Andrew Bennetts
Further simplify the test to reproduce the bug.
2601
        # Query for 'first' and 'null:'.  Because 'null:' is a parent of
4214.2.5 by Andrew Bennetts
Fix the bug.
2602
        # 'first' it will be a candidate for the stop_keys of subsequent
2603
        # requests, and because 'null:' was queried but not returned it will be
2604
        # cached as missing.
4214.2.1 by Andrew Bennetts
A long but failing test for the get_parent_map RPC bug.
2605
        self.assertEqual({'first': ('null:',)},
4214.2.4 by Andrew Bennetts
Further simplify the test to reproduce the bug.
2606
            graph.get_parent_map(['first', 'null:']))
2607
        # Now query for another key.  This request will pass along a recipe of
2608
        # start and stop keys describing the already cached results, and this
4214.2.5 by Andrew Bennetts
Fix the bug.
2609
        # recipe's revision count must be correct (or else it will trigger an
4214.2.4 by Andrew Bennetts
Further simplify the test to reproduce the bug.
2610
        # error from the server).
4214.2.5 by Andrew Bennetts
Fix the bug.
2611
        self.assertEqual({}, graph.get_parent_map(['another-key']))
4214.2.3 by Andrew Bennetts
Further simplify test case, and add more comments.
2612
        # This assertion guards against disableExtraResults silently failing to
2613
        # work, thus invalidating the test.
4214.2.4 by Andrew Bennetts
Further simplify the test to reproduce the bug.
2614
        self.assertLength(2, self.hpss_calls)
4214.2.1 by Andrew Bennetts
A long but failing test for the get_parent_map RPC bug.
2615
4190.1.4 by Robert Collins
Cache ghosts when we can get them from a RemoteRepository in get_parent_map.
2616
    def test_get_parent_map_gets_ghosts_from_result(self):
2617
        # asking for a revision should negatively cache close ghosts in its
2618
        # ancestry.
2619
        self.setup_smart_server_with_call_log()
2620
        tree = self.make_branch_and_memory_tree('foo')
2621
        tree.lock_write()
2622
        try:
2623
            builder = treebuilder.TreeBuilder()
2624
            builder.start_tree(tree)
2625
            builder.build([])
2626
            builder.finish_tree()
2627
            tree.set_parent_ids(['non-existant'], allow_leftmost_as_ghost=True)
2628
            rev_id = tree.commit('')
2629
        finally:
2630
            tree.unlock()
2631
        tree.lock_read()
2632
        self.addCleanup(tree.unlock)
2633
        repo = tree.branch.repository
2634
        self.assertIsInstance(repo, RemoteRepository)
2635
        # ask for rev_id
2636
        repo.get_parent_map([rev_id])
2637
        self.reset_smart_call_log()
2638
        # Now asking for rev_id's ghost parent should not make calls
2639
        self.assertEqual({}, repo.get_parent_map(['non-existant']))
2640
        self.assertLength(0, self.hpss_calls)
2641
6015.24.4 by John Arbash Meinel
For it to all work properly, we have to expose get_parent_map_cached on RemoteRepository.
2642
    def test_exposes_get_cached_parent_map(self):
2643
        """RemoteRepository exposes get_cached_parent_map from
2644
        _unstacked_provider
2645
        """
2646
        r1 = u'\u0e33'.encode('utf8')
2647
        r2 = u'\u0dab'.encode('utf8')
2648
        lines = [' '.join([r2, r1]), r1]
2649
        encoded_body = bz2.compress('\n'.join(lines))
2650
2651
        transport_path = 'quack'
2652
        repo, client = self.setup_fake_client_and_repository(transport_path)
2653
        client.add_success_response_with_body(encoded_body, 'ok')
2654
        repo.lock_read()
6015.24.5 by John Arbash Meinel
Bug #388269.
2655
        # get_cached_parent_map should *not* trigger an RPC
2656
        self.assertEqual({}, repo.get_cached_parent_map([r1]))
2657
        self.assertEqual([], client._calls)
6015.24.4 by John Arbash Meinel
For it to all work properly, we have to expose get_parent_map_cached on RemoteRepository.
2658
        self.assertEqual({r2: (r1,)}, repo.get_parent_map([r2]))
2659
        self.assertEqual({r1: (NULL_REVISION,)},
2660
            repo.get_cached_parent_map([r1]))
2661
        self.assertEqual(
2662
            [('call_with_body_bytes_expecting_body',
2663
              'Repository.get_parent_map', ('quack/', 'include-missing:', r2),
2664
              '\n\n0')],
2665
            client._calls)
2666
        repo.unlock()
2667
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
2668
3835.1.15 by Aaron Bentley
Allow miss caching to be disabled.
2669
class TestGetParentMapAllowsNew(tests.TestCaseWithTransport):
2670
2671
    def test_allows_new_revisions(self):
2672
        """get_parent_map's results can be updated by commit."""
5017.3.28 by Vincent Ladeuil
selftest -s bt.test_remote passing
2673
        smart_server = test_server.SmartTCPServer_for_testing()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
2674
        self.start_server(smart_server)
3835.1.15 by Aaron Bentley
Allow miss caching to be disabled.
2675
        self.make_branch('branch')
2676
        branch = Branch.open(smart_server.get_url() + '/branch')
2677
        tree = branch.create_checkout('tree', lightweight=True)
2678
        tree.lock_write()
2679
        self.addCleanup(tree.unlock)
2680
        graph = tree.branch.repository.get_graph()
2681
        # This provides an opportunity for the missing rev-id to be cached.
2682
        self.assertEqual({}, graph.get_parent_map(['rev1']))
2683
        tree.commit('message', rev_id='rev1')
2684
        graph = tree.branch.repository.get_graph()
2685
        self.assertEqual({'rev1': ('null:',)}, graph.get_parent_map(['rev1']))
2686
2687
6280.9.1 by Jelmer Vernooij
Add remote side of Repository.iter_revisions.
2688
class TestRepositoryGetRevisions(TestRemoteRepository):
2689
2690
    def test_hpss_missing_revision(self):
2691
        transport_path = 'quack'
2692
        repo, client = self.setup_fake_client_and_repository(transport_path)
2693
        client.add_success_response_with_body(
2694
            '', 'ok', '10')
2695
        self.assertRaises(errors.NoSuchRevision, repo.get_revisions,
2696
            ['somerev1', 'anotherrev2'])
2697
        self.assertEqual(
2698
            [('call_with_body_bytes_expecting_body', 'Repository.iter_revisions',
2699
             ('quack/', ), "somerev1\nanotherrev2")],
2700
            client._calls)
2701
2702
    def test_hpss_get_single_revision(self):
2703
        transport_path = 'quack'
2704
        repo, client = self.setup_fake_client_and_repository(transport_path)
2705
        somerev1 = Revision("somerev1")
2706
        somerev1.committer = "Joe Committer <joe@example.com>"
2707
        somerev1.timestamp = 1321828927
2708
        somerev1.timezone = -60
2709
        somerev1.inventory_sha1 = "691b39be74c67b1212a75fcb19c433aaed903c2b"
2710
        somerev1.message = "Message"
6280.9.4 by Jelmer Vernooij
use zlib instead.
2711
        body = zlib.compress(chk_bencode_serializer.write_revision_to_string(
6280.9.1 by Jelmer Vernooij
Add remote side of Repository.iter_revisions.
2712
            somerev1))
6280.9.8 by Jelmer Vernooij
Try to make two calls to zlib.decompressobj.decompress.
2713
        # Split up body into two bits to make sure the zlib compression object
2714
        # gets data fed twice.
6280.9.1 by Jelmer Vernooij
Add remote side of Repository.iter_revisions.
2715
        client.add_success_response_with_body(
6280.9.8 by Jelmer Vernooij
Try to make two calls to zlib.decompressobj.decompress.
2716
                [body[:10], body[10:]], 'ok', '10')
6280.9.1 by Jelmer Vernooij
Add remote side of Repository.iter_revisions.
2717
        revs = repo.get_revisions(['somerev1'])
2718
        self.assertEquals(revs, [somerev1])
2719
        self.assertEqual(
2720
            [('call_with_body_bytes_expecting_body', 'Repository.iter_revisions',
2721
             ('quack/', ), "somerev1")],
2722
            client._calls)
2723
2724
3948.3.9 by Martin Pool
Undelete TestRepositoryGetRevisionGraph but make it use private client methods to simulate old clients
2725
class TestRepositoryGetRevisionGraph(TestRemoteRepository):
2726
2727
    def test_null_revision(self):
2728
        # a null revision has the predictable result {}, we should have no wire
2729
        # traffic when calling it with this argument
2730
        transport_path = 'empty'
2731
        repo, client = self.setup_fake_client_and_repository(transport_path)
2732
        client.add_success_response('notused')
2733
        # actual RemoteRepository.get_revision_graph is gone, but there's an
2734
        # equivalent private method for testing
2735
        result = repo._get_revision_graph(NULL_REVISION)
2736
        self.assertEqual([], client._calls)
2737
        self.assertEqual({}, result)
2738
2739
    def test_none_revision(self):
2740
        # with none we want the entire graph
2741
        r1 = u'\u0e33'.encode('utf8')
2742
        r2 = u'\u0dab'.encode('utf8')
2743
        lines = [' '.join([r2, r1]), r1]
2744
        encoded_body = '\n'.join(lines)
2745
2746
        transport_path = 'sinhala'
2747
        repo, client = self.setup_fake_client_and_repository(transport_path)
2748
        client.add_success_response_with_body(encoded_body, 'ok')
2749
        # actual RemoteRepository.get_revision_graph is gone, but there's an
2750
        # equivalent private method for testing
2751
        result = repo._get_revision_graph(None)
2752
        self.assertEqual(
2753
            [('call_expecting_body', 'Repository.get_revision_graph',
2754
             ('sinhala/', ''))],
2755
            client._calls)
2756
        self.assertEqual({r1: (), r2: (r1, )}, result)
2757
2758
    def test_specific_revision(self):
2759
        # with a specific revision we want the graph for that
2760
        # with none we want the entire graph
2761
        r11 = u'\u0e33'.encode('utf8')
2762
        r12 = u'\xc9'.encode('utf8')
2763
        r2 = u'\u0dab'.encode('utf8')
2764
        lines = [' '.join([r2, r11, r12]), r11, r12]
2765
        encoded_body = '\n'.join(lines)
2766
2767
        transport_path = 'sinhala'
2768
        repo, client = self.setup_fake_client_and_repository(transport_path)
2769
        client.add_success_response_with_body(encoded_body, 'ok')
2770
        result = repo._get_revision_graph(r2)
2771
        self.assertEqual(
2772
            [('call_expecting_body', 'Repository.get_revision_graph',
2773
             ('sinhala/', r2))],
2774
            client._calls)
2775
        self.assertEqual({r11: (), r12: (), r2: (r11, r12), }, result)
2776
2777
    def test_no_such_revision(self):
2778
        revid = '123'
2779
        transport_path = 'sinhala'
2780
        repo, client = self.setup_fake_client_and_repository(transport_path)
2781
        client.add_error_response('nosuchrevision', revid)
2782
        # also check that the right revision is reported in the error
2783
        self.assertRaises(errors.NoSuchRevision,
2784
            repo._get_revision_graph, revid)
2785
        self.assertEqual(
2786
            [('call_expecting_body', 'Repository.get_revision_graph',
2787
             ('sinhala/', revid))],
2788
            client._calls)
2789
2790
    def test_unexpected_error(self):
2791
        revid = '123'
2792
        transport_path = 'sinhala'
2793
        repo, client = self.setup_fake_client_and_repository(transport_path)
2794
        client.add_error_response('AnUnexpectedError')
2795
        e = self.assertRaises(errors.UnknownErrorFromSmartServer,
2796
            repo._get_revision_graph, revid)
2797
        self.assertEqual(('AnUnexpectedError',), e.error_tuple)
2798
2799
4419.2.7 by Andrew Bennetts
Add unit tests for RemoteRepository.get_rev_id_for_revno.
2800
class TestRepositoryGetRevIdForRevno(TestRemoteRepository):
2801
2802
    def test_ok(self):
4419.2.8 by Andrew Bennetts
Add unit test for RemoteRepository.get_rev_id_for_revno using fallbacks if it gets a history-incomplete response.
2803
        repo, client = self.setup_fake_client_and_repository('quack')
4419.2.7 by Andrew Bennetts
Add unit tests for RemoteRepository.get_rev_id_for_revno.
2804
        client.add_expected_call(
2805
            'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
2806
            'success', ('ok', 'rev-five'))
2807
        result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
2808
        self.assertEqual((True, 'rev-five'), result)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
2809
        self.assertFinished(client)
4419.2.7 by Andrew Bennetts
Add unit tests for RemoteRepository.get_rev_id_for_revno.
2810
2811
    def test_history_incomplete(self):
4419.2.8 by Andrew Bennetts
Add unit test for RemoteRepository.get_rev_id_for_revno using fallbacks if it gets a history-incomplete response.
2812
        repo, client = self.setup_fake_client_and_repository('quack')
4419.2.7 by Andrew Bennetts
Add unit tests for RemoteRepository.get_rev_id_for_revno.
2813
        client.add_expected_call(
2814
            'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
2815
            'success', ('history-incomplete', 10, 'rev-ten'))
2816
        result = repo.get_rev_id_for_revno(5, (42, 'rev-foo'))
2817
        self.assertEqual((False, (10, 'rev-ten')), result)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
2818
        self.assertFinished(client)
4419.2.7 by Andrew Bennetts
Add unit tests for RemoteRepository.get_rev_id_for_revno.
2819
4419.2.8 by Andrew Bennetts
Add unit test for RemoteRepository.get_rev_id_for_revno using fallbacks if it gets a history-incomplete response.
2820
    def test_history_incomplete_with_fallback(self):
2821
        """A 'history-incomplete' response causes the fallback repository to be
2822
        queried too, if one is set.
2823
        """
2824
        # Make a repo with a fallback repo, both using a FakeClient.
2825
        format = remote.response_tuple_to_repo_format(
5158.4.3 by Andrew Bennetts
Fix test_remote tests that accidentally assumed it was ok to stack mismatched formats.
2826
            ('yes', 'no', 'yes', self.get_repo_format().network_name()))
4419.2.8 by Andrew Bennetts
Add unit test for RemoteRepository.get_rev_id_for_revno using fallbacks if it gets a history-incomplete response.
2827
        repo, client = self.setup_fake_client_and_repository('quack')
2828
        repo._format = format
2829
        fallback_repo, ignored = self.setup_fake_client_and_repository(
2830
            'fallback')
2831
        fallback_repo._client = client
5158.4.3 by Andrew Bennetts
Fix test_remote tests that accidentally assumed it was ok to stack mismatched formats.
2832
        fallback_repo._format = format
4419.2.8 by Andrew Bennetts
Add unit test for RemoteRepository.get_rev_id_for_revno using fallbacks if it gets a history-incomplete response.
2833
        repo.add_fallback_repository(fallback_repo)
2834
        # First the client should ask the primary repo
2835
        client.add_expected_call(
2836
            'Repository.get_rev_id_for_revno', ('quack/', 1, (42, 'rev-foo')),
2837
            'success', ('history-incomplete', 2, 'rev-two'))
2838
        # Then it should ask the fallback, using revno/revid from the
2839
        # history-incomplete response as the known revno/revid.
2840
        client.add_expected_call(
2841
            'Repository.get_rev_id_for_revno',('fallback/', 1, (2, 'rev-two')),
2842
            'success', ('ok', 'rev-one'))
2843
        result = repo.get_rev_id_for_revno(1, (42, 'rev-foo'))
2844
        self.assertEqual((True, 'rev-one'), result)
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
2845
        self.assertFinished(client)
4419.2.8 by Andrew Bennetts
Add unit test for RemoteRepository.get_rev_id_for_revno using fallbacks if it gets a history-incomplete response.
2846
4419.2.7 by Andrew Bennetts
Add unit tests for RemoteRepository.get_rev_id_for_revno.
2847
    def test_nosuchrevision(self):
2848
        # 'nosuchrevision' is returned when the known-revid is not found in the
2849
        # remote repo.  The client translates that response to NoSuchRevision.
4419.2.8 by Andrew Bennetts
Add unit test for RemoteRepository.get_rev_id_for_revno using fallbacks if it gets a history-incomplete response.
2850
        repo, client = self.setup_fake_client_and_repository('quack')
4419.2.7 by Andrew Bennetts
Add unit tests for RemoteRepository.get_rev_id_for_revno.
2851
        client.add_expected_call(
2852
            'Repository.get_rev_id_for_revno', ('quack/', 5, (42, 'rev-foo')),
2853
            'error', ('nosuchrevision', 'rev-foo'))
2854
        self.assertRaises(
2855
            errors.NoSuchRevision,
2856
            repo.get_rev_id_for_revno, 5, (42, 'rev-foo'))
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
2857
        self.assertFinished(client)
4419.2.7 by Andrew Bennetts
Add unit tests for RemoteRepository.get_rev_id_for_revno.
2858
4634.69.1 by Andrew Bennetts
Apply @needs_read_lock to RemoteBranch.get_rev_id.
2859
    def test_branch_fallback_locking(self):
2860
        """RemoteBranch.get_rev_id takes a read lock, and tries to call the
2861
        get_rev_id_for_revno verb.  If the verb is unknown the VFS fallback
2862
        will be invoked, which will fail if the repo is unlocked.
2863
        """
2864
        self.setup_smart_server_with_call_log()
2865
        tree = self.make_branch_and_memory_tree('.')
2866
        tree.lock_write()
5222.1.1 by Aaron Bentley
Refuse to commit trees with no root.
2867
        tree.add('')
4634.69.1 by Andrew Bennetts
Apply @needs_read_lock to RemoteBranch.get_rev_id.
2868
        rev1 = tree.commit('First')
2869
        rev2 = tree.commit('Second')
2870
        tree.unlock()
2871
        branch = tree.branch
2872
        self.assertFalse(branch.is_locked())
2873
        self.reset_smart_call_log()
2874
        verb = 'Repository.get_rev_id_for_revno'
2875
        self.disable_verb(verb)
2876
        self.assertEqual(rev1, branch.get_rev_id(1))
2877
        self.assertLength(1, [call for call in self.hpss_calls if
2878
                              call.call.method == verb])
2879
4419.2.7 by Andrew Bennetts
Add unit tests for RemoteRepository.get_rev_id_for_revno.
2880
6265.1.1 by Jelmer Vernooij
Add new HPSS call ``Repository.has_signature_for_revision_id``.
2881
class TestRepositoryHasSignatureForRevisionId(TestRemoteRepository):
2882
2883
    def test_has_signature_for_revision_id(self):
2884
        # ('yes', ) for Repository.has_signature_for_revision_id -> 'True'.
2885
        transport_path = 'quack'
2886
        repo, client = self.setup_fake_client_and_repository(transport_path)
2887
        client.add_success_response('yes')
2888
        result = repo.has_signature_for_revision_id('A')
2889
        self.assertEqual(
2890
            [('call', 'Repository.has_signature_for_revision_id',
2891
              ('quack/', 'A'))],
2892
            client._calls)
2893
        self.assertEqual(True, result)
2894
2895
    def test_is_not_shared(self):
2896
        # ('no', ) for Repository.has_signature_for_revision_id -> 'False'.
2897
        transport_path = 'qwack'
2898
        repo, client = self.setup_fake_client_and_repository(transport_path)
2899
        client.add_success_response('no')
2900
        result = repo.has_signature_for_revision_id('A')
2901
        self.assertEqual(
2902
            [('call', 'Repository.has_signature_for_revision_id',
2903
              ('qwack/', 'A'))],
2904
            client._calls)
2905
        self.assertEqual(False, result)
2906
2907
6280.6.1 by Jelmer Vernooij
Implement remote side of {Branch,Repository}.get_physical_lock_status.
2908
class TestRepositoryPhysicalLockStatus(TestRemoteRepository):
2909
2910
    def test_get_physical_lock_status_yes(self):
2911
        transport_path = 'qwack'
2912
        repo, client = self.setup_fake_client_and_repository(transport_path)
2913
        client.add_success_response('yes')
2914
        result = repo.get_physical_lock_status()
2915
        self.assertEqual(
2916
            [('call', 'Repository.get_physical_lock_status',
2917
              ('qwack/', ))],
2918
            client._calls)
2919
        self.assertEqual(True, result)
2920
2921
    def test_get_physical_lock_status_no(self):
2922
        transport_path = 'qwack'
2923
        repo, client = self.setup_fake_client_and_repository(transport_path)
2924
        client.add_success_response('no')
2925
        result = repo.get_physical_lock_status()
2926
        self.assertEqual(
2927
            [('call', 'Repository.get_physical_lock_status',
2928
              ('qwack/', ))],
2929
            client._calls)
2930
        self.assertEqual(False, result)
2931
2932
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
2933
class TestRepositoryIsShared(TestRemoteRepository):
2934
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
2935
    def test_is_shared(self):
2936
        # ('yes', ) for Repository.is_shared -> 'True'.
2937
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2938
        repo, client = self.setup_fake_client_and_repository(transport_path)
2939
        client.add_success_response('yes')
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
2940
        result = repo.is_shared()
2941
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
2942
            [('call', 'Repository.is_shared', ('quack/',))],
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
2943
            client._calls)
2944
        self.assertEqual(True, result)
2945
2946
    def test_is_not_shared(self):
2947
        # ('no', ) for Repository.is_shared -> 'False'.
2948
        transport_path = 'qwack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2949
        repo, client = self.setup_fake_client_and_repository(transport_path)
2950
        client.add_success_response('no')
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
2951
        result = repo.is_shared()
2952
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
2953
            [('call', 'Repository.is_shared', ('qwack/',))],
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
2954
            client._calls)
2955
        self.assertEqual(False, result)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
2956
2957
6263.2.1 by Jelmer Vernooij
Add hpss call ``Repository.make_working_trees``
2958
class TestRepositoryMakeWorkingTrees(TestRemoteRepository):
2959
2960
    def test_make_working_trees(self):
2961
        # ('yes', ) for Repository.make_working_trees -> 'True'.
2962
        transport_path = 'quack'
2963
        repo, client = self.setup_fake_client_and_repository(transport_path)
2964
        client.add_success_response('yes')
2965
        result = repo.make_working_trees()
2966
        self.assertEqual(
2967
            [('call', 'Repository.make_working_trees', ('quack/',))],
2968
            client._calls)
2969
        self.assertEqual(True, result)
2970
2971
    def test_no_working_trees(self):
2972
        # ('no', ) for Repository.make_working_trees -> 'False'.
2973
        transport_path = 'qwack'
2974
        repo, client = self.setup_fake_client_and_repository(transport_path)
2975
        client.add_success_response('no')
2976
        result = repo.make_working_trees()
2977
        self.assertEqual(
2978
            [('call', 'Repository.make_working_trees', ('qwack/',))],
2979
            client._calls)
2980
        self.assertEqual(False, result)
2981
2982
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
2983
class TestRepositoryLockWrite(TestRemoteRepository):
2984
2985
    def test_lock_write(self):
2986
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2987
        repo, client = self.setup_fake_client_and_repository(transport_path)
2988
        client.add_success_response('ok', 'a token')
5200.3.3 by Robert Collins
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
2989
        token = repo.lock_write().repository_token
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
2990
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
2991
            [('call', 'Repository.lock_write', ('quack/', ''))],
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
2992
            client._calls)
5200.3.3 by Robert Collins
Lock methods on ``Tree``, ``Branch`` and ``Repository`` are now
2993
        self.assertEqual('a token', token)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
2994
2995
    def test_lock_write_already_locked(self):
2996
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
2997
        repo, client = self.setup_fake_client_and_repository(transport_path)
2998
        client.add_error_response('LockContention')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
2999
        self.assertRaises(errors.LockContention, repo.lock_write)
3000
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
3001
            [('call', 'Repository.lock_write', ('quack/', ''))],
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
3002
            client._calls)
3003
3004
    def test_lock_write_unlockable(self):
3005
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
3006
        repo, client = self.setup_fake_client_and_repository(transport_path)
3007
        client.add_error_response('UnlockableTransport')
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
3008
        self.assertRaises(errors.UnlockableTransport, repo.lock_write)
3009
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
3010
            [('call', 'Repository.lock_write', ('quack/', ''))],
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
3011
            client._calls)
3012
3013
6280.7.1 by Jelmer Vernooij
Implement RemoteRepository side of write group HPSS methods.
3014
class TestRepositoryWriteGroups(TestRemoteRepository):
3015
3016
    def test_start_write_group(self):
3017
        transport_path = 'quack'
3018
        repo, client = self.setup_fake_client_and_repository(transport_path)
3019
        client.add_expected_call(
6280.7.2 by Jelmer Vernooij
Add HPSS calls ``Repository.start_write_group``, ``Repository.abort_write_group`` and ``Repository.commit_write_group``.
3020
            'Repository.lock_write', ('quack/', ''),
3021
            'success', ('ok', 'a token'))
3022
        client.add_expected_call(
3023
            'Repository.start_write_group', ('quack/', 'a token'),
6280.10.32 by Jelmer Vernooij
Merge bzr.dev.
3024
            'success', ('ok', ('token1', )))
6280.7.2 by Jelmer Vernooij
Add HPSS calls ``Repository.start_write_group``, ``Repository.abort_write_group`` and ``Repository.commit_write_group``.
3025
        repo.lock_write()
6280.7.1 by Jelmer Vernooij
Implement RemoteRepository side of write group HPSS methods.
3026
        repo.start_write_group()
3027
6280.7.8 by Jelmer Vernooij
make sure start_write_group falls back to real_repository if write groups aren't suspendable.
3028
    def test_start_write_group_unsuspendable(self):
3029
        # Some repositories do not support suspending write
3030
        # groups. For those, fall back to the "real" repository.
3031
        transport_path = 'quack'
3032
        repo, client = self.setup_fake_client_and_repository(transport_path)
3033
        def stub_ensure_real():
3034
            client._calls.append(('_ensure_real',))
3035
            repo._real_repository = _StubRealPackRepository(client._calls)
3036
        repo._ensure_real = stub_ensure_real
3037
        client.add_expected_call(
3038
            'Repository.lock_write', ('quack/', ''),
3039
            'success', ('ok', 'a token'))
3040
        client.add_expected_call(
3041
            'Repository.start_write_group', ('quack/', 'a token'),
3042
            'error', ('UnsuspendableWriteGroup',))
3043
        repo.lock_write()
3044
        repo.start_write_group()
3045
        self.assertEquals(client._calls[-2:], [ 
3046
            ('_ensure_real',),
3047
            ('start_write_group',)])
3048
6280.7.1 by Jelmer Vernooij
Implement RemoteRepository side of write group HPSS methods.
3049
    def test_commit_write_group(self):
3050
        transport_path = 'quack'
3051
        repo, client = self.setup_fake_client_and_repository(transport_path)
3052
        client.add_expected_call(
6280.7.2 by Jelmer Vernooij
Add HPSS calls ``Repository.start_write_group``, ``Repository.abort_write_group`` and ``Repository.commit_write_group``.
3053
            'Repository.lock_write', ('quack/', ''),
3054
            'success', ('ok', 'a token'))
3055
        client.add_expected_call(
3056
            'Repository.start_write_group', ('quack/', 'a token'),
6280.7.6 by Jelmer Vernooij
Fix remaining tests.
3057
            'success', ('ok', ['token1']))
6280.7.2 by Jelmer Vernooij
Add HPSS calls ``Repository.start_write_group``, ``Repository.abort_write_group`` and ``Repository.commit_write_group``.
3058
        client.add_expected_call(
6280.7.6 by Jelmer Vernooij
Fix remaining tests.
3059
            'Repository.commit_write_group', ('quack/', 'a token', ['token1']),
6280.7.1 by Jelmer Vernooij
Implement RemoteRepository side of write group HPSS methods.
3060
            'success', ('ok',))
6280.7.2 by Jelmer Vernooij
Add HPSS calls ``Repository.start_write_group``, ``Repository.abort_write_group`` and ``Repository.commit_write_group``.
3061
        repo.lock_write()
3062
        repo.start_write_group()
6280.7.1 by Jelmer Vernooij
Implement RemoteRepository side of write group HPSS methods.
3063
        repo.commit_write_group()
3064
3065
    def test_abort_write_group(self):
3066
        transport_path = 'quack'
3067
        repo, client = self.setup_fake_client_and_repository(transport_path)
3068
        client.add_expected_call(
6280.7.2 by Jelmer Vernooij
Add HPSS calls ``Repository.start_write_group``, ``Repository.abort_write_group`` and ``Repository.commit_write_group``.
3069
            'Repository.lock_write', ('quack/', ''),
3070
            'success', ('ok', 'a token'))
3071
        client.add_expected_call(
3072
            'Repository.start_write_group', ('quack/', 'a token'),
6280.7.6 by Jelmer Vernooij
Fix remaining tests.
3073
            'success', ('ok', ['token1']))
6280.7.2 by Jelmer Vernooij
Add HPSS calls ``Repository.start_write_group``, ``Repository.abort_write_group`` and ``Repository.commit_write_group``.
3074
        client.add_expected_call(
6280.7.6 by Jelmer Vernooij
Fix remaining tests.
3075
            'Repository.abort_write_group', ('quack/', 'a token', ['token1']),
6280.7.1 by Jelmer Vernooij
Implement RemoteRepository side of write group HPSS methods.
3076
            'success', ('ok',))
6280.7.2 by Jelmer Vernooij
Add HPSS calls ``Repository.start_write_group``, ``Repository.abort_write_group`` and ``Repository.commit_write_group``.
3077
        repo.lock_write()
3078
        repo.start_write_group()
6280.7.1 by Jelmer Vernooij
Implement RemoteRepository side of write group HPSS methods.
3079
        repo.abort_write_group(False)
3080
3081
    def test_suspend_write_group(self):
3082
        transport_path = 'quack'
3083
        repo, client = self.setup_fake_client_and_repository(transport_path)
6280.7.2 by Jelmer Vernooij
Add HPSS calls ``Repository.start_write_group``, ``Repository.abort_write_group`` and ``Repository.commit_write_group``.
3084
        self.assertEquals([], repo.suspend_write_group())
6280.7.1 by Jelmer Vernooij
Implement RemoteRepository side of write group HPSS methods.
3085
3086
    def test_resume_write_group(self):
3087
        transport_path = 'quack'
3088
        repo, client = self.setup_fake_client_and_repository(transport_path)
6280.7.6 by Jelmer Vernooij
Fix remaining tests.
3089
        client.add_expected_call(
3090
            'Repository.lock_write', ('quack/', ''),
3091
            'success', ('ok', 'a token'))
3092
        client.add_expected_call(
3093
            'Repository.check_write_group', ('quack/', 'a token', ['token1']),
3094
            'success', ('ok',))
3095
        repo.lock_write()
3096
        repo.resume_write_group(['token1'])
6280.7.1 by Jelmer Vernooij
Implement RemoteRepository side of write group HPSS methods.
3097
3098
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
3099
class TestRepositorySetMakeWorkingTrees(TestRemoteRepository):
3100
3101
    def test_backwards_compat(self):
3102
        self.setup_smart_server_with_call_log()
3103
        repo = self.make_repository('.')
3104
        self.reset_smart_call_log()
3105
        verb = 'Repository.set_make_working_trees'
3106
        self.disable_verb(verb)
3107
        repo.set_make_working_trees(True)
3108
        call_count = len([call for call in self.hpss_calls if
4070.3.1 by Robert Collins
Alter branch sprouting with an alternate fix for stacked branches that does not require multiple copy_content_into and set_parent calls, reducing IO and round trips.
3109
            call.call.method == verb])
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
3110
        self.assertEqual(1, call_count)
3111
3112
    def test_current(self):
3113
        transport_path = 'quack'
3114
        repo, client = self.setup_fake_client_and_repository(transport_path)
3115
        client.add_expected_call(
3116
            'Repository.set_make_working_trees', ('quack/', 'True'),
3117
            'success', ('ok',))
3118
        client.add_expected_call(
3119
            'Repository.set_make_working_trees', ('quack/', 'False'),
3120
            'success', ('ok',))
3121
        repo.set_make_working_trees(True)
3122
        repo.set_make_working_trees(False)
3123
3124
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
3125
class TestRepositoryUnlock(TestRemoteRepository):
3126
3127
    def test_unlock(self):
3128
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
3129
        repo, client = self.setup_fake_client_and_repository(transport_path)
3130
        client.add_success_response('ok', 'a token')
3131
        client.add_success_response('ok')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
3132
        repo.lock_write()
3133
        repo.unlock()
3134
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
3135
            [('call', 'Repository.lock_write', ('quack/', '')),
3136
             ('call', 'Repository.unlock', ('quack/', 'a token'))],
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
3137
            client._calls)
3138
3139
    def test_unlock_wrong_token(self):
3140
        # If somehow the token is wrong, unlock will raise TokenMismatch.
3141
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
3142
        repo, client = self.setup_fake_client_and_repository(transport_path)
3143
        client.add_success_response('ok', 'a token')
3144
        client.add_error_response('TokenMismatch')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
3145
        repo.lock_write()
3146
        self.assertRaises(errors.TokenMismatch, repo.unlock)
3147
3148
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
3149
class TestRepositoryHasRevision(TestRemoteRepository):
3150
3151
    def test_none(self):
3152
        # repo.has_revision(None) should not cause any traffic.
3153
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
3154
        repo, client = self.setup_fake_client_and_repository(transport_path)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
3155
3156
        # The null revision is always there, so has_revision(None) == True.
3172.3.3 by Robert Collins
Missed one occurence of None -> NULL_REVISION.
3157
        self.assertEqual(True, repo.has_revision(NULL_REVISION))
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
3158
3159
        # The remote repo shouldn't be accessed.
3160
        self.assertEqual([], client._calls)
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
3161
3162
6280.10.7 by Jelmer Vernooij
Fix remote.
3163
class TestRepositoryIterFilesBytes(TestRemoteRepository):
6280.10.1 by Jelmer Vernooij
Add remote side of Repository.iter_file_bytes.
3164
    """Test Repository.iter_file_bytes."""
3165
3166
    def test_single(self):
3167
        transport_path = 'quack'
3168
        repo, client = self.setup_fake_client_and_repository(transport_path)
3169
        client.add_expected_call(
6280.10.19 by Jelmer Vernooij
Convert remote side to zlib.
3170
            'Repository.iter_files_bytes', ('quack/', ),
3171
            'success', ('ok',), iter(["ok\x000", "\n", zlib.compress("mydata" * 10)]))
6280.10.1 by Jelmer Vernooij
Add remote side of Repository.iter_file_bytes.
3172
        for (identifier, byte_stream) in repo.iter_files_bytes([("somefile",
3173
                "somerev", "myid")]):
3174
            self.assertEquals("myid", identifier)
6280.10.7 by Jelmer Vernooij
Fix remote.
3175
            self.assertEquals("".join(byte_stream), "mydata" * 10)
6280.10.1 by Jelmer Vernooij
Add remote side of Repository.iter_file_bytes.
3176
6280.10.3 by Jelmer Vernooij
Fix iter_file_bytes.
3177
    def test_missing(self):
3178
        transport_path = 'quack'
3179
        repo, client = self.setup_fake_client_and_repository(transport_path)
3180
        client.add_expected_call(
6280.10.19 by Jelmer Vernooij
Convert remote side to zlib.
3181
            'Repository.iter_files_bytes',
6280.10.7 by Jelmer Vernooij
Fix remote.
3182
                ('quack/', ),
3183
            'error', ('RevisionNotPresent', 'somefile', 'somerev'),
6280.10.11 by Jelmer Vernooij
mark absent entries.
3184
            iter(["absent\0somefile\0somerev\n"]))
6280.10.3 by Jelmer Vernooij
Fix iter_file_bytes.
3185
        self.assertRaises(errors.RevisionNotPresent, list,
3186
                repo.iter_files_bytes(
3187
                [("somefile", "somerev", "myid")]))
3188
6280.10.1 by Jelmer Vernooij
Add remote side of Repository.iter_file_bytes.
3189
4476.3.79 by Andrew Bennetts
Remove a bit of duplication from Repository.insert_stream* tests.
3190
class TestRepositoryInsertStreamBase(TestRemoteRepository):
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3191
    """Base class for Repository.insert_stream and .insert_stream_1.19
4476.3.79 by Andrew Bennetts
Remove a bit of duplication from Repository.insert_stream* tests.
3192
    tests.
3193
    """
3194
    
3195
    def checkInsertEmptyStream(self, repo, client):
3196
        """Insert an empty stream, checking the result.
3197
3198
        This checks that there are no resume_tokens or missing_keys, and that
3199
        the client is finished.
3200
        """
3201
        sink = repo._get_sink()
5651.3.9 by Jelmer Vernooij
Avoid using deprecated functions.
3202
        fmt = repository.format_registry.get_default()
4476.3.79 by Andrew Bennetts
Remove a bit of duplication from Repository.insert_stream* tests.
3203
        resume_tokens, missing_keys = sink.insert_stream([], fmt, [])
3204
        self.assertEqual([], resume_tokens)
3205
        self.assertEqual(set(), missing_keys)
3206
        self.assertFinished(client)
3207
3208
3209
class TestRepositoryInsertStream(TestRepositoryInsertStreamBase):
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3210
    """Tests for using Repository.insert_stream verb when the _1.19 variant is
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3211
    not available.
3212
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3213
    This test case is very similar to TestRepositoryInsertStream_1_19.
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3214
    """
3215
3216
    def setUp(self):
3217
        TestRemoteRepository.setUp(self)
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3218
        self.disable_verb('Repository.insert_stream_1.19')
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3219
3220
    def test_unlocked_repo(self):
3221
        transport_path = 'quack'
3222
        repo, client = self.setup_fake_client_and_repository(transport_path)
3223
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3224
            'Repository.insert_stream_1.19', ('quack/', ''),
3225
            'unknown', ('Repository.insert_stream_1.19',))
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3226
        client.add_expected_call(
3227
            'Repository.insert_stream', ('quack/', ''),
3228
            'success', ('ok',))
3229
        client.add_expected_call(
3230
            'Repository.insert_stream', ('quack/', ''),
3231
            'success', ('ok',))
4476.3.79 by Andrew Bennetts
Remove a bit of duplication from Repository.insert_stream* tests.
3232
        self.checkInsertEmptyStream(repo, client)
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3233
3234
    def test_locked_repo_with_no_lock_token(self):
3235
        transport_path = 'quack'
3236
        repo, client = self.setup_fake_client_and_repository(transport_path)
3237
        client.add_expected_call(
3238
            'Repository.lock_write', ('quack/', ''),
3239
            'success', ('ok', ''))
3240
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3241
            'Repository.insert_stream_1.19', ('quack/', ''),
3242
            'unknown', ('Repository.insert_stream_1.19',))
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3243
        client.add_expected_call(
3244
            'Repository.insert_stream', ('quack/', ''),
3245
            'success', ('ok',))
3246
        client.add_expected_call(
3247
            'Repository.insert_stream', ('quack/', ''),
3248
            'success', ('ok',))
3249
        repo.lock_write()
4476.3.79 by Andrew Bennetts
Remove a bit of duplication from Repository.insert_stream* tests.
3250
        self.checkInsertEmptyStream(repo, client)
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3251
3252
    def test_locked_repo_with_lock_token(self):
3253
        transport_path = 'quack'
3254
        repo, client = self.setup_fake_client_and_repository(transport_path)
3255
        client.add_expected_call(
3256
            'Repository.lock_write', ('quack/', ''),
3257
            'success', ('ok', 'a token'))
3258
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3259
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
3260
            'unknown', ('Repository.insert_stream_1.19',))
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3261
        client.add_expected_call(
3262
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
3263
            'success', ('ok',))
3264
        client.add_expected_call(
3265
            'Repository.insert_stream_locked', ('quack/', '', 'a token'),
3266
            'success', ('ok',))
3267
        repo.lock_write()
4476.3.79 by Andrew Bennetts
Remove a bit of duplication from Repository.insert_stream* tests.
3268
        self.checkInsertEmptyStream(repo, client)
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3269
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
3270
    def test_stream_with_inventory_deltas(self):
4476.3.71 by Andrew Bennetts
Clearer comments prompted by Robert's review.
3271
        """'inventory-deltas' substreams cannot be sent to the
3272
        Repository.insert_stream verb, because not all servers that implement
3273
        that verb will accept them.  So when one is encountered the RemoteSink
3274
        immediately stops using that verb and falls back to VFS insert_stream.
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3275
        """
3276
        transport_path = 'quack'
3277
        repo, client = self.setup_fake_client_and_repository(transport_path)
3278
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3279
            'Repository.insert_stream_1.19', ('quack/', ''),
3280
            'unknown', ('Repository.insert_stream_1.19',))
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3281
        client.add_expected_call(
3282
            'Repository.insert_stream', ('quack/', ''),
3283
            'success', ('ok',))
3284
        client.add_expected_call(
3285
            'Repository.insert_stream', ('quack/', ''),
3286
            'success', ('ok',))
3287
        # Create a fake real repository for insert_stream to fall back on, so
3288
        # that we can directly see the records the RemoteSink passes to the
3289
        # real sink.
3290
        class FakeRealSink:
3291
            def __init__(self):
3292
                self.records = []
3293
            def insert_stream(self, stream, src_format, resume_tokens):
3294
                for substream_kind, substream in stream:
3295
                    self.records.append(
3296
                        (substream_kind, [record.key for record in substream]))
3297
                return ['fake tokens'], ['fake missing keys']
3298
        fake_real_sink = FakeRealSink()
3299
        class FakeRealRepository:
3300
            def _get_sink(self):
3301
                return fake_real_sink
4634.35.20 by Andrew Bennetts
Fix test_remote.
3302
            def is_in_write_group(self):
3303
                return False
3304
            def refresh_data(self):
3305
                return True
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3306
        repo._real_repository = FakeRealRepository()
3307
        sink = repo._get_sink()
5651.3.9 by Jelmer Vernooij
Avoid using deprecated functions.
3308
        fmt = repository.format_registry.get_default()
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3309
        stream = self.make_stream_with_inv_deltas(fmt)
3310
        resume_tokens, missing_keys = sink.insert_stream(stream, fmt, [])
3311
        # Every record from the first inventory delta should have been sent to
3312
        # the VFS sink.
3313
        expected_records = [
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
3314
            ('inventory-deltas', [('rev2',), ('rev3',)]),
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3315
            ('texts', [('some-rev', 'some-file')])]
3316
        self.assertEqual(expected_records, fake_real_sink.records)
3317
        # The return values from the real sink's insert_stream are propagated
3318
        # back to the original caller.
3319
        self.assertEqual(['fake tokens'], resume_tokens)
3320
        self.assertEqual(['fake missing keys'], missing_keys)
4476.3.40 by Andrew Bennetts
Merge bzr.dev.
3321
        self.assertFinished(client)
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3322
3323
    def make_stream_with_inv_deltas(self, fmt):
3324
        """Make a simple stream with an inventory delta followed by more
3325
        records and more substreams to test that all records and substreams
3326
        from that point on are used.
3327
3328
        This sends, in order:
3329
           * inventories substream: rev1, rev2, rev3.  rev2 and rev3 are
3330
             inventory-deltas.
3331
           * texts substream: (some-rev, some-file)
3332
        """
3333
        # Define a stream using generators so that it isn't rewindable.
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
3334
        inv = inventory.Inventory(revision_id='rev1')
4599.4.39 by Robert Collins
Use a valid for storage inventory in test_remote's new inventory streaming test.
3335
        inv.root.revision = 'rev1'
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3336
        def stream_with_inv_delta():
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
3337
            yield ('inventories', inventories_substream())
3338
            yield ('inventory-deltas', inventory_delta_substream())
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3339
            yield ('texts', [
3340
                versionedfile.FulltextContentFactory(
3341
                    ('some-rev', 'some-file'), (), None, 'content')])
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
3342
        def inventories_substream():
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3343
            # An empty inventory fulltext.  This will be streamed normally.
3344
            text = fmt._serializer.write_inventory_to_string(inv)
3345
            yield versionedfile.FulltextContentFactory(
3346
                ('rev1',), (), None, text)
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
3347
        def inventory_delta_substream():
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3348
            # An inventory delta.  This can't be streamed via this verb, so it
3349
            # will trigger a fallback to VFS insert_stream.
3350
            entry = inv.make_entry(
3351
                'directory', 'newdir', inv.root.file_id, 'newdir-id')
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
3352
            entry.revision = 'ghost'
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3353
            delta = [(None, 'newdir', 'newdir-id', entry)]
4476.3.76 by Andrew Bennetts
Split out InventoryDeltaDeserializer from InventoryDeltaSerializer.
3354
            serializer = inventory_delta.InventoryDeltaSerializer(
3355
                versioned_root=True, tree_references=False)
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
3356
            lines = serializer.delta_to_lines('rev1', 'rev2', delta)
3357
            yield versionedfile.ChunkedContentFactory(
3358
                ('rev2',), (('rev1',)), None, lines)
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3359
            # Another delta.
4476.3.56 by Andrew Bennetts
Update test_stream_with_inventory_deltas for inventory-deltas substream.
3360
            lines = serializer.delta_to_lines('rev1', 'rev3', delta)
3361
            yield versionedfile.ChunkedContentFactory(
3362
                ('rev3',), (('rev1',)), None, lines)
4476.3.36 by Andrew Bennetts
Add a somewhat complex test to exercise the fallback-to-vfs logic in RemoteSink when an inventory-delta is encountered and the 1.18 verb isn't accepted.
3363
        return stream_with_inv_delta()
3364
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3365
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3366
class TestRepositoryInsertStream_1_19(TestRepositoryInsertStreamBase):
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3367
3368
    def test_unlocked_repo(self):
3369
        transport_path = 'quack'
3370
        repo, client = self.setup_fake_client_and_repository(transport_path)
3371
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3372
            'Repository.insert_stream_1.19', ('quack/', ''),
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3373
            'success', ('ok',))
3374
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3375
            'Repository.insert_stream_1.19', ('quack/', ''),
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3376
            'success', ('ok',))
4476.3.79 by Andrew Bennetts
Remove a bit of duplication from Repository.insert_stream* tests.
3377
        self.checkInsertEmptyStream(repo, client)
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3378
3379
    def test_locked_repo_with_no_lock_token(self):
3380
        transport_path = 'quack'
3381
        repo, client = self.setup_fake_client_and_repository(transport_path)
3382
        client.add_expected_call(
3383
            'Repository.lock_write', ('quack/', ''),
3384
            'success', ('ok', ''))
3385
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3386
            'Repository.insert_stream_1.19', ('quack/', ''),
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3387
            'success', ('ok',))
3388
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3389
            'Repository.insert_stream_1.19', ('quack/', ''),
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3390
            'success', ('ok',))
3391
        repo.lock_write()
4476.3.79 by Andrew Bennetts
Remove a bit of duplication from Repository.insert_stream* tests.
3392
        self.checkInsertEmptyStream(repo, client)
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3393
3394
    def test_locked_repo_with_lock_token(self):
3395
        transport_path = 'quack'
3396
        repo, client = self.setup_fake_client_and_repository(transport_path)
3397
        client.add_expected_call(
3398
            'Repository.lock_write', ('quack/', ''),
3399
            'success', ('ok', 'a token'))
3400
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3401
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
4476.3.32 by Andrew Bennetts
Move disable_verb into base TestCase to remove duplication, fix trivial test failures due to new insert_stream verb in test_remote (and also add some trivial tests for the new verb).
3402
            'success', ('ok',))
3403
        client.add_expected_call(
4476.3.82 by Andrew Bennetts
Mention another bug fix in NEWS, and update verb name, comments, and NEWS additions for landing on 1.19 rather than 1.18.
3404
            'Repository.insert_stream_1.19', ('quack/', '', 'a token'),
4144.3.2 by Andrew Bennetts
Use Repository.insert_stream_locked if there is a lock_token for the remote repo.
3405
            'success', ('ok',))
3406
        repo.lock_write()
4476.3.79 by Andrew Bennetts
Remove a bit of duplication from Repository.insert_stream* tests.
3407
        self.checkInsertEmptyStream(repo, client)
4144.3.2 by Andrew Bennetts
Use Repository.insert_stream_locked if there is a lock_token for the remote repo.
3408
3409
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
3410
class TestRepositoryTarball(TestRemoteRepository):
3411
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
3412
    # This is a canned tarball reponse we can validate against
2018.18.18 by Martin Pool
reformat
3413
    tarball_content = (
2018.18.23 by Martin Pool
review cleanups
3414
        'QlpoOTFBWSZTWdGkj3wAAWF/k8aQACBIB//A9+8cIX/v33AACEAYABAECEACNz'
3415
        'JqsgJJFPTSnk1A3qh6mTQAAAANPUHkagkSTEkaA09QaNAAAGgAAAcwCYCZGAEY'
3416
        'mJhMJghpiaYBUkKammSHqNMZQ0NABkNAeo0AGneAevnlwQoGzEzNVzaYxp/1Uk'
3417
        'xXzA1CQX0BJMZZLcPBrluJir5SQyijWHYZ6ZUtVqqlYDdB2QoCwa9GyWwGYDMA'
3418
        'OQYhkpLt/OKFnnlT8E0PmO8+ZNSo2WWqeCzGB5fBXZ3IvV7uNJVE7DYnWj6qwB'
3419
        'k5DJDIrQ5OQHHIjkS9KqwG3mc3t+F1+iujb89ufyBNIKCgeZBWrl5cXxbMGoMs'
3420
        'c9JuUkg5YsiVcaZJurc6KLi6yKOkgCUOlIlOpOoXyrTJjK8ZgbklReDdwGmFgt'
3421
        'dkVsAIslSVCd4AtACSLbyhLHryfb14PKegrVDba+U8OL6KQtzdM5HLjAc8/p6n'
3422
        '0lgaWU8skgO7xupPTkyuwheSckejFLK5T4ZOo0Gda9viaIhpD1Qn7JqqlKAJqC'
3423
        'QplPKp2nqBWAfwBGaOwVrz3y1T+UZZNismXHsb2Jq18T+VaD9k4P8DqE3g70qV'
3424
        'JLurpnDI6VS5oqDDPVbtVjMxMxMg4rzQVipn2Bv1fVNK0iq3Gl0hhnnHKm/egy'
3425
        'nWQ7QH/F3JFOFCQ0aSPfA='
3426
        ).decode('base64')
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
3427
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
3428
    def test_repository_tarball(self):
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
3429
        # Test that Repository.tarball generates the right operations
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
3430
        transport_path = 'repo'
2018.18.14 by Martin Pool
merge hpss again; restore incorrectly removed RemoteRepository.break_lock
3431
        expected_calls = [('call_expecting_body', 'Repository.tarball',
3104.4.2 by Andrew Bennetts
All tests passing.
3432
                           ('repo/', 'bz2',),),
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
3433
            ]
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
3434
        repo, client = self.setup_fake_client_and_repository(transport_path)
3435
        client.add_success_response_with_body(self.tarball_content, 'ok')
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
3436
        # Now actually ask for the tarball
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
3437
        tarball_file = repo._get_tarball('bz2')
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
3438
        try:
3439
            self.assertEqual(expected_calls, client._calls)
3440
            self.assertEqual(self.tarball_content, tarball_file.read())
3441
        finally:
3442
            tarball_file.close()
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
3443
3444
3445
class TestRemoteRepositoryCopyContent(tests.TestCaseWithTransport):
3446
    """RemoteRepository.copy_content_into optimizations"""
3447
2018.18.10 by Martin Pool
copy_content_into from Remote repositories by using temporary directories on both ends.
3448
    def test_copy_content_remote_to_local(self):
5017.3.28 by Vincent Ladeuil
selftest -s bt.test_remote passing
3449
        self.transport_server = test_server.SmartTCPServer_for_testing
2018.18.10 by Martin Pool
copy_content_into from Remote repositories by using temporary directories on both ends.
3450
        src_repo = self.make_repository('repo1')
3451
        src_repo = repository.Repository.open(self.get_url('repo1'))
3452
        # At the moment the tarball-based copy_content_into can't write back
3453
        # into a smart server.  It would be good if it could upload the
3454
        # tarball; once that works we'd have to create repositories of
3455
        # different formats. -- mbp 20070410
3456
        dest_url = self.get_vfs_only_url('repo2')
3457
        dest_bzrdir = BzrDir.create(dest_url)
3458
        dest_repo = dest_bzrdir.create_repository()
3459
        self.assertFalse(isinstance(dest_repo, RemoteRepository))
3460
        self.assertTrue(isinstance(src_repo, RemoteRepository))
3461
        src_repo.copy_content_into(dest_repo)
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3462
3463
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3464
class _StubRealPackRepository(object):
3465
3466
    def __init__(self, calls):
4145.1.6 by Robert Collins
More test fallout, but all caught now.
3467
        self.calls = calls
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3468
        self._pack_collection = _StubPackCollection(calls)
3469
6280.7.8 by Jelmer Vernooij
make sure start_write_group falls back to real_repository if write groups aren't suspendable.
3470
    def start_write_group(self):
3471
        self.calls.append(('start_write_group',))
3472
4145.1.6 by Robert Collins
More test fallout, but all caught now.
3473
    def is_in_write_group(self):
3474
        return False
3475
3476
    def refresh_data(self):
3477
        self.calls.append(('pack collection reload_pack_names',))
3478
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3479
3480
class _StubPackCollection(object):
3481
3482
    def __init__(self, calls):
3483
        self.calls = calls
3484
3485
    def autopack(self):
3486
        self.calls.append(('pack collection autopack',))
3487
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
3488
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3489
class TestRemotePackRepositoryAutoPack(TestRemoteRepository):
3490
    """Tests for RemoteRepository.autopack implementation."""
3491
3492
    def test_ok(self):
3493
        """When the server returns 'ok' and there's no _real_repository, then
3494
        nothing else happens: the autopack method is done.
3495
        """
3496
        transport_path = 'quack'
3497
        repo, client = self.setup_fake_client_and_repository(transport_path)
3498
        client.add_expected_call(
3801.1.13 by Andrew Bennetts
Revert returning of pack-names from the RPC.
3499
            'PackRepository.autopack', ('quack/',), 'success', ('ok',))
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3500
        repo.autopack()
4523.3.1 by Andrew Bennetts
Change FakeClient.finished_test into a more typical assertion method on TestRemote.
3501
        self.assertFinished(client)
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3502
3503
    def test_ok_with_real_repo(self):
3504
        """When the server returns 'ok' and there is a _real_repository, then
3505
        the _real_repository's reload_pack_name's method will be called.
3506
        """
3507
        transport_path = 'quack'
3508
        repo, client = self.setup_fake_client_and_repository(transport_path)
3509
        client.add_expected_call(
3510
            'PackRepository.autopack', ('quack/',),
3801.1.13 by Andrew Bennetts
Revert returning of pack-names from the RPC.
3511
            'success', ('ok',))
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3512
        repo._real_repository = _StubRealPackRepository(client._calls)
3513
        repo.autopack()
3514
        self.assertEqual(
3515
            [('call', 'PackRepository.autopack', ('quack/',)),
3801.1.13 by Andrew Bennetts
Revert returning of pack-names from the RPC.
3516
             ('pack collection reload_pack_names',)],
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3517
            client._calls)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
3518
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3519
    def test_backwards_compatibility(self):
3520
        """If the server does not recognise the PackRepository.autopack verb,
3521
        fallback to the real_repository's implementation.
3522
        """
3523
        transport_path = 'quack'
3524
        repo, client = self.setup_fake_client_and_repository(transport_path)
3525
        client.add_unknown_method_response('PackRepository.autopack')
3526
        def stub_ensure_real():
3527
            client._calls.append(('_ensure_real',))
3528
            repo._real_repository = _StubRealPackRepository(client._calls)
3529
        repo._ensure_real = stub_ensure_real
3530
        repo.autopack()
3531
        self.assertEqual(
3532
            [('call', 'PackRepository.autopack', ('quack/',)),
3533
             ('_ensure_real',),
3534
             ('pack collection autopack',)],
3535
            client._calls)
3536
5677.2.2 by Martin
Give clearer message when remote server reports a MemoryError
3537
    def test_oom_error_reporting(self):
3538
        """An out-of-memory condition on the server is reported clearly"""
3539
        transport_path = 'quack'
3540
        repo, client = self.setup_fake_client_and_repository(transport_path)
3541
        client.add_expected_call(
3542
            'PackRepository.autopack', ('quack/',),
3543
            'error', ('MemoryError',))
3544
        err = self.assertRaises(errors.BzrError, repo.autopack)
3545
        self.assertContainsRe(str(err), "^remote server out of mem")
3546
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3547
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3548
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
3549
    """Base class for unit tests for bzrlib.remote._translate_error."""
3550
3551
    def translateTuple(self, error_tuple, **context):
3552
        """Call _translate_error with an ErrorFromSmartServer built from the
3553
        given error_tuple.
3554
3555
        :param error_tuple: A tuple of a smart server response, as would be
3556
            passed to an ErrorFromSmartServer.
3557
        :kwargs context: context items to call _translate_error with.
3558
3559
        :returns: The error raised by _translate_error.
3560
        """
3561
        # Raise the ErrorFromSmartServer before passing it as an argument,
3562
        # because _translate_error may need to re-raise it with a bare 'raise'
3563
        # statement.
3564
        server_error = errors.ErrorFromSmartServer(error_tuple)
3565
        translated_error = self.translateErrorFromSmartServer(
3566
            server_error, **context)
3567
        return translated_error
3568
3569
    def translateErrorFromSmartServer(self, error_object, **context):
3570
        """Like translateTuple, but takes an already constructed
3571
        ErrorFromSmartServer rather than a tuple.
3572
        """
3573
        try:
3574
            raise error_object
3575
        except errors.ErrorFromSmartServer, server_error:
3576
            translated_error = self.assertRaises(
3577
                errors.BzrError, remote._translate_error, server_error,
3578
                **context)
3579
        return translated_error
3580
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
3581
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3582
class TestErrorTranslationSuccess(TestErrorTranslationBase):
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3583
    """Unit tests for bzrlib.remote._translate_error.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
3584
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3585
    Given an ErrorFromSmartServer (which has an error tuple from a smart
3586
    server) and some context, _translate_error raises more specific errors from
3587
    bzrlib.errors.
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3588
3589
    This test case covers the cases where _translate_error succeeds in
3590
    translating an ErrorFromSmartServer to something better.  See
3591
    TestErrorTranslationRobustness for other cases.
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3592
    """
3593
3594
    def test_NoSuchRevision(self):
3595
        branch = self.make_branch('')
3596
        revid = 'revid'
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3597
        translated_error = self.translateTuple(
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3598
            ('NoSuchRevision', revid), branch=branch)
3599
        expected_error = errors.NoSuchRevision(branch, revid)
3600
        self.assertEqual(expected_error, translated_error)
3601
3602
    def test_nosuchrevision(self):
3603
        repository = self.make_repository('')
3604
        revid = 'revid'
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3605
        translated_error = self.translateTuple(
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3606
            ('nosuchrevision', revid), repository=repository)
3607
        expected_error = errors.NoSuchRevision(repository, revid)
3608
        self.assertEqual(expected_error, translated_error)
3609
3610
    def test_nobranch(self):
3611
        bzrdir = self.make_bzrdir('')
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3612
        translated_error = self.translateTuple(('nobranch',), bzrdir=bzrdir)
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3613
        expected_error = errors.NotBranchError(path=bzrdir.root_transport.base)
3614
        self.assertEqual(expected_error, translated_error)
3615
4734.4.8 by Andrew Bennetts
Fix HPSS tests; pass 'location is a repository' message via smart server when possible (adds BzrDir.open_branchV3 verb).
3616
    def test_nobranch_one_arg(self):
3617
        bzrdir = self.make_bzrdir('')
3618
        translated_error = self.translateTuple(
3619
            ('nobranch', 'extra detail'), bzrdir=bzrdir)
3620
        expected_error = errors.NotBranchError(
3621
            path=bzrdir.root_transport.base,
3622
            detail='extra detail')
3623
        self.assertEqual(expected_error, translated_error)
3624
5677.2.5 by Martin
Add more tests for remote._translate_error including for MemoryError handling
3625
    def test_norepository(self):
3626
        bzrdir = self.make_bzrdir('')
3627
        translated_error = self.translateTuple(('norepository',),
3628
            bzrdir=bzrdir)
3629
        expected_error = errors.NoRepositoryPresent(bzrdir)
3630
        self.assertEqual(expected_error, translated_error)
3631
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3632
    def test_LockContention(self):
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3633
        translated_error = self.translateTuple(('LockContention',))
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3634
        expected_error = errors.LockContention('(remote lock)')
3635
        self.assertEqual(expected_error, translated_error)
3636
3637
    def test_UnlockableTransport(self):
3638
        bzrdir = self.make_bzrdir('')
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3639
        translated_error = self.translateTuple(
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3640
            ('UnlockableTransport',), bzrdir=bzrdir)
3641
        expected_error = errors.UnlockableTransport(bzrdir.root_transport)
3642
        self.assertEqual(expected_error, translated_error)
3643
3644
    def test_LockFailed(self):
3645
        lock = 'str() of a server lock'
3646
        why = 'str() of why'
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3647
        translated_error = self.translateTuple(('LockFailed', lock, why))
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3648
        expected_error = errors.LockFailed(lock, why)
3649
        self.assertEqual(expected_error, translated_error)
3650
3651
    def test_TokenMismatch(self):
3652
        token = 'a lock token'
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3653
        translated_error = self.translateTuple(('TokenMismatch',), token=token)
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3654
        expected_error = errors.TokenMismatch(token, '(remote token)')
3655
        self.assertEqual(expected_error, translated_error)
3656
3657
    def test_Diverged(self):
3658
        branch = self.make_branch('a')
3659
        other_branch = self.make_branch('b')
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3660
        translated_error = self.translateTuple(
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
3661
            ('Diverged',), branch=branch, other_branch=other_branch)
3662
        expected_error = errors.DivergedBranches(branch, other_branch)
3663
        self.assertEqual(expected_error, translated_error)
3664
5677.2.5 by Martin
Add more tests for remote._translate_error including for MemoryError handling
3665
    def test_NotStacked(self):
3666
        branch = self.make_branch('')
3667
        translated_error = self.translateTuple(('NotStacked',), branch=branch)
3668
        expected_error = errors.NotStacked(branch)
3669
        self.assertEqual(expected_error, translated_error)
3670
3786.4.2 by Andrew Bennetts
Add tests and fix code to make sure ReadError and PermissionDenied are robustly handled by _translate_error.
3671
    def test_ReadError_no_args(self):
3672
        path = 'a path'
3673
        translated_error = self.translateTuple(('ReadError',), path=path)
3674
        expected_error = errors.ReadError(path)
3675
        self.assertEqual(expected_error, translated_error)
3676
3677
    def test_ReadError(self):
3678
        path = 'a path'
3679
        translated_error = self.translateTuple(('ReadError', path))
3680
        expected_error = errors.ReadError(path)
3681
        self.assertEqual(expected_error, translated_error)
3682
4650.2.1 by Robert Collins
Deserialise IncompatibleRepositories errors in the client, generating
3683
    def test_IncompatibleRepositories(self):
3684
        translated_error = self.translateTuple(('IncompatibleRepositories',
3685
            "repo1", "repo2", "details here"))
3686
        expected_error = errors.IncompatibleRepositories("repo1", "repo2",
3687
            "details here")
3688
        self.assertEqual(expected_error, translated_error)
3689
3786.4.2 by Andrew Bennetts
Add tests and fix code to make sure ReadError and PermissionDenied are robustly handled by _translate_error.
3690
    def test_PermissionDenied_no_args(self):
3691
        path = 'a path'
5677.2.5 by Martin
Add more tests for remote._translate_error including for MemoryError handling
3692
        translated_error = self.translateTuple(('PermissionDenied',),
3693
            path=path)
3786.4.2 by Andrew Bennetts
Add tests and fix code to make sure ReadError and PermissionDenied are robustly handled by _translate_error.
3694
        expected_error = errors.PermissionDenied(path)
3695
        self.assertEqual(expected_error, translated_error)
3696
3697
    def test_PermissionDenied_one_arg(self):
3698
        path = 'a path'
3699
        translated_error = self.translateTuple(('PermissionDenied', path))
3700
        expected_error = errors.PermissionDenied(path)
3701
        self.assertEqual(expected_error, translated_error)
3702
3703
    def test_PermissionDenied_one_arg_and_context(self):
3704
        """Given a choice between a path from the local context and a path on
3705
        the wire, _translate_error prefers the path from the local context.
3706
        """
3707
        local_path = 'local path'
3708
        remote_path = 'remote path'
3709
        translated_error = self.translateTuple(
3710
            ('PermissionDenied', remote_path), path=local_path)
3711
        expected_error = errors.PermissionDenied(local_path)
3712
        self.assertEqual(expected_error, translated_error)
3713
3714
    def test_PermissionDenied_two_args(self):
3715
        path = 'a path'
3716
        extra = 'a string with extra info'
3717
        translated_error = self.translateTuple(
3718
            ('PermissionDenied', path, extra))
3719
        expected_error = errors.PermissionDenied(path, extra)
3720
        self.assertEqual(expected_error, translated_error)
3721
5677.2.5 by Martin
Add more tests for remote._translate_error including for MemoryError handling
3722
    # GZ 2011-03-02: TODO test for PermissionDenied with non-ascii 'extra'
3723
3724
    def test_NoSuchFile_context_path(self):
3725
        local_path = "local path"
3726
        translated_error = self.translateTuple(('ReadError', "remote path"),
3727
            path=local_path)
3728
        expected_error = errors.ReadError(local_path)
3729
        self.assertEqual(expected_error, translated_error)
3730
3731
    def test_NoSuchFile_without_context(self):
3732
        remote_path = "remote path"
3733
        translated_error = self.translateTuple(('ReadError', remote_path))
3734
        expected_error = errors.ReadError(remote_path)
3735
        self.assertEqual(expected_error, translated_error)
3736
3737
    def test_ReadOnlyError(self):
3738
        translated_error = self.translateTuple(('ReadOnlyError',))
3739
        expected_error = errors.TransportNotPossible("readonly transport")
3740
        self.assertEqual(expected_error, translated_error)
3741
3742
    def test_MemoryError(self):
3743
        translated_error = self.translateTuple(('MemoryError',))
5677.2.9 by Martin
Add hint to possible ways forward for user in remote MemoryError message
3744
        self.assertStartsWith(str(translated_error),
3745
            "remote server out of memory")
5677.2.5 by Martin
Add more tests for remote._translate_error including for MemoryError handling
3746
5677.2.8 by Martin
More tests for handling of unexpected remote errors
3747
    def test_generic_IndexError_no_classname(self):
5677.2.5 by Martin
Add more tests for remote._translate_error including for MemoryError handling
3748
        err = errors.ErrorFromSmartServer(('error', "list index out of range"))
3749
        translated_error = self.translateErrorFromSmartServer(err)
3750
        expected_error = errors.UnknownErrorFromSmartServer(err)
3751
        self.assertEqual(expected_error, translated_error)
3752
3753
    # GZ 2011-03-02: TODO test generic non-ascii error string
3754
5677.2.8 by Martin
More tests for handling of unexpected remote errors
3755
    def test_generic_KeyError(self):
3756
        err = errors.ErrorFromSmartServer(('error', 'KeyError', "1"))
3757
        translated_error = self.translateErrorFromSmartServer(err)
3758
        expected_error = errors.UnknownErrorFromSmartServer(err)
3759
        self.assertEqual(expected_error, translated_error)
3760
3786.4.2 by Andrew Bennetts
Add tests and fix code to make sure ReadError and PermissionDenied are robustly handled by _translate_error.
3761
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3762
class TestErrorTranslationRobustness(TestErrorTranslationBase):
3763
    """Unit tests for bzrlib.remote._translate_error's robustness.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
3764
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3765
    TestErrorTranslationSuccess is for cases where _translate_error can
3766
    translate successfully.  This class about how _translate_err behaves when
3767
    it fails to translate: it re-raises the original error.
3768
    """
3769
3770
    def test_unrecognised_server_error(self):
3771
        """If the error code from the server is not recognised, the original
3772
        ErrorFromSmartServer is propagated unmodified.
3773
        """
3774
        error_tuple = ('An unknown error tuple',)
3690.1.2 by Andrew Bennetts
Rename UntranslateableErrorFromSmartServer -> UnknownErrorFromSmartServer.
3775
        server_error = errors.ErrorFromSmartServer(error_tuple)
3776
        translated_error = self.translateErrorFromSmartServer(server_error)
3777
        expected_error = errors.UnknownErrorFromSmartServer(server_error)
3690.1.1 by Andrew Bennetts
Unexpected error responses from a smart server no longer cause the client to traceback.
3778
        self.assertEqual(expected_error, translated_error)
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3779
3780
    def test_context_missing_a_key(self):
3781
        """In case of a bug in the client, or perhaps an unexpected response
3782
        from a server, _translate_error returns the original error tuple from
3783
        the server and mutters a warning.
3784
        """
3785
        # To translate a NoSuchRevision error _translate_error needs a 'branch'
3786
        # in the context dict.  So let's give it an empty context dict instead
3787
        # to exercise its error recovery.
3788
        empty_context = {}
3789
        error_tuple = ('NoSuchRevision', 'revid')
3790
        server_error = errors.ErrorFromSmartServer(error_tuple)
3791
        translated_error = self.translateErrorFromSmartServer(server_error)
3792
        self.assertEqual(server_error, translated_error)
3793
        # In addition to re-raising ErrorFromSmartServer, some debug info has
3794
        # been muttered to the log file for developer to look at.
3795
        self.assertContainsRe(
4794.1.15 by Robert Collins
Review feedback.
3796
            self.get_log(),
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
3797
            "Missing key 'branch' in context")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
3798
3786.4.2 by Andrew Bennetts
Add tests and fix code to make sure ReadError and PermissionDenied are robustly handled by _translate_error.
3799
    def test_path_missing(self):
3800
        """Some translations (PermissionDenied, ReadError) can determine the
3801
        'path' variable from either the wire or the local context.  If neither
3802
        has it, then an error is raised.
3803
        """
3804
        error_tuple = ('ReadError',)
3805
        server_error = errors.ErrorFromSmartServer(error_tuple)
3806
        translated_error = self.translateErrorFromSmartServer(server_error)
3807
        self.assertEqual(server_error, translated_error)
3808
        # In addition to re-raising ErrorFromSmartServer, some debug info has
3809
        # been muttered to the log file for developer to look at.
4794.1.15 by Robert Collins
Review feedback.
3810
        self.assertContainsRe(self.get_log(), "Missing key 'path' in context")
3786.4.2 by Andrew Bennetts
Add tests and fix code to make sure ReadError and PermissionDenied are robustly handled by _translate_error.
3811
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
3812
3813
class TestStacking(tests.TestCaseWithTransport):
3814
    """Tests for operations on stacked remote repositories.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
3815
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
3816
    The underlying format type must support stacking.
3817
    """
3818
3819
    def test_access_stacked_remote(self):
3820
        # based on <http://launchpad.net/bugs/261315>
3821
        # make a branch stacked on another repository containing an empty
3822
        # revision, then open it over hpss - we should be able to see that
3823
        # revision.
3824
        base_transport = self.get_transport()
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3825
        base_builder = self.make_branch_builder('base', format='1.9')
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
3826
        base_builder.start_series()
3827
        base_revid = base_builder.build_snapshot('rev-id', None,
3828
            [('add', ('', None, 'directory', None))],
3829
            'message')
3830
        base_builder.finish_series()
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3831
        stacked_branch = self.make_branch('stacked', format='1.9')
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
3832
        stacked_branch.set_stacked_on_url('../base')
3833
        # start a server looking at this
5017.3.28 by Vincent Ladeuil
selftest -s bt.test_remote passing
3834
        smart_server = test_server.SmartTCPServer_for_testing()
4659.1.3 by Robert Collins
Review feedback.
3835
        self.start_server(smart_server)
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
3836
        remote_bzrdir = BzrDir.open(smart_server.get_url() + '/stacked')
3837
        # can get its branch and repository
3838
        remote_branch = remote_bzrdir.open_branch()
3839
        remote_repo = remote_branch.repository
3691.2.6 by Martin Pool
Disable RemoteBranch stacking, but get get_stacked_on_url working, and passing back exceptions
3840
        remote_repo.lock_read()
3841
        try:
3842
            # it should have an appropriate fallback repository, which should also
3843
            # be a RemoteRepository
4288.1.2 by Robert Collins
Create a server verb for doing BzrDir.get_config()
3844
            self.assertLength(1, remote_repo._fallback_repositories)
3691.2.6 by Martin Pool
Disable RemoteBranch stacking, but get get_stacked_on_url working, and passing back exceptions
3845
            self.assertIsInstance(remote_repo._fallback_repositories[0],
3846
                RemoteRepository)
3847
            # and it has the revision committed to the underlying repository;
3848
            # these have varying implementations so we try several of them
3849
            self.assertTrue(remote_repo.has_revisions([base_revid]))
3850
            self.assertTrue(remote_repo.has_revision(base_revid))
3851
            self.assertEqual(remote_repo.get_revision(base_revid).message,
3852
                'message')
3853
        finally:
3854
            remote_repo.unlock()
3835.1.2 by Aaron Bentley
Add tests for get_parent_map
3855
3835.1.7 by Aaron Bentley
Updates from review
3856
    def prepare_stacked_remote_branch(self):
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3857
        """Get stacked_upon and stacked branches with content in each."""
4152.1.2 by Robert Collins
Add streaming from a stacked branch when the sort order is compatible with doing so.
3858
        self.setup_smart_server_with_call_log()
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3859
        tree1 = self.make_branch_and_tree('tree1', format='1.9')
3835.1.2 by Aaron Bentley
Add tests for get_parent_map
3860
        tree1.commit('rev1', rev_id='rev1')
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3861
        tree2 = tree1.branch.bzrdir.sprout('tree2', stacked=True
3862
            ).open_workingtree()
4595.4.4 by Robert Collins
Disable committing directly to stacked branches from lightweight checkouts.
3863
        local_tree = tree2.branch.create_checkout('local')
3864
        local_tree.commit('local changes make me feel good.')
4152.1.2 by Robert Collins
Add streaming from a stacked branch when the sort order is compatible with doing so.
3865
        branch2 = Branch.open(self.get_url('tree2'))
3835.1.2 by Aaron Bentley
Add tests for get_parent_map
3866
        branch2.lock_read()
3867
        self.addCleanup(branch2.unlock)
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3868
        return tree1.branch, branch2
3835.1.7 by Aaron Bentley
Updates from review
3869
3870
    def test_stacked_get_parent_map(self):
3871
        # the public implementation of get_parent_map obeys stacking
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3872
        _, branch = self.prepare_stacked_remote_branch()
3835.1.7 by Aaron Bentley
Updates from review
3873
        repo = branch.repository
3835.1.2 by Aaron Bentley
Add tests for get_parent_map
3874
        self.assertEqual(['rev1'], repo.get_parent_map(['rev1']).keys())
3835.1.7 by Aaron Bentley
Updates from review
3875
3835.1.10 by Aaron Bentley
Move CachingExtraParentsProvider to Graph
3876
    def test_unstacked_get_parent_map(self):
3877
        # _unstacked_provider.get_parent_map ignores stacking
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3878
        _, branch = self.prepare_stacked_remote_branch()
3835.1.10 by Aaron Bentley
Move CachingExtraParentsProvider to Graph
3879
        provider = branch.repository._unstacked_provider
3835.1.8 by Aaron Bentley
Make UnstackedParentsProvider manage the cache
3880
        self.assertEqual([], provider.get_parent_map(['rev1']).keys())
3834.3.3 by John Arbash Meinel
Merge bzr.dev, resolve conflict in tests.
3881
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3882
    def fetch_stream_to_rev_order(self, stream):
3883
        result = []
3884
        for kind, substream in stream:
3885
            if not kind == 'revisions':
3886
                list(substream)
3887
            else:
3888
                for content in substream:
3889
                    result.append(content.key[-1])
3890
        return result
3891
4577.1.1 by Robert Collins
Fix fetching from smart servers where there is a chain of stacked repositories rather than a single stacking point. (Robert Collins, bug #406597)
3892
    def get_ordered_revs(self, format, order, branch_factory=None):
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3893
        """Get a list of the revisions in a stream to format format.
3894
3895
        :param format: The format of the target.
3896
        :param order: the order that target should have requested.
4577.1.1 by Robert Collins
Fix fetching from smart servers where there is a chain of stacked repositories rather than a single stacking point. (Robert Collins, bug #406597)
3897
        :param branch_factory: A callable to create a trunk and stacked branch
3898
            to fetch from. If none, self.prepare_stacked_remote_branch is used.
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3899
        :result: The revision ids in the stream, in the order seen,
3900
            the topological order of revisions in the source.
3901
        """
3902
        unordered_format = bzrdir.format_registry.get(format)()
3903
        target_repository_format = unordered_format.repository_format
3904
        # Cross check
3905
        self.assertEqual(order, target_repository_format._fetch_order)
4577.1.1 by Robert Collins
Fix fetching from smart servers where there is a chain of stacked repositories rather than a single stacking point. (Robert Collins, bug #406597)
3906
        if branch_factory is None:
3907
            branch_factory = self.prepare_stacked_remote_branch
3908
        _, stacked = branch_factory()
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3909
        source = stacked.repository._get_source(target_repository_format)
3910
        tip = stacked.last_revision()
5972.3.25 by Jelmer Vernooij
Fix another use of get_ancestry.
3911
        stacked.repository._ensure_real()
3912
        graph = stacked.repository.get_graph()
3913
        revs = [r for (r,ps) in graph.iter_ancestry([tip])
3914
                if r != NULL_REVISION]
3915
        revs.reverse()
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
3916
        search = vf_search.PendingAncestryResult([tip], stacked.repository)
4152.1.2 by Robert Collins
Add streaming from a stacked branch when the sort order is compatible with doing so.
3917
        self.reset_smart_call_log()
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3918
        stream = source.get_stream(search)
3919
        # We trust that if a revision is in the stream the rest of the new
3920
        # content for it is too, as per our main fetch tests; here we are
3921
        # checking that the revisions are actually included at all, and their
3922
        # order.
3923
        return self.fetch_stream_to_rev_order(stream), revs
3924
3925
    def test_stacked_get_stream_unordered(self):
3926
        # Repository._get_source.get_stream() from a stacked repository with
3927
        # unordered yields the full data from both stacked and stacked upon
3928
        # sources.
3929
        rev_ord, expected_revs = self.get_ordered_revs('1.9', 'unordered')
4152.1.2 by Robert Collins
Add streaming from a stacked branch when the sort order is compatible with doing so.
3930
        self.assertEqual(set(expected_revs), set(rev_ord))
3931
        # Getting unordered results should have made a streaming data request
3932
        # from the server, then one from the backing branch.
3933
        self.assertLength(2, self.hpss_calls)
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3934
4577.1.1 by Robert Collins
Fix fetching from smart servers where there is a chain of stacked repositories rather than a single stacking point. (Robert Collins, bug #406597)
3935
    def test_stacked_on_stacked_get_stream_unordered(self):
3936
        # Repository._get_source.get_stream() from a stacked repository which
3937
        # is itself stacked yields the full data from all three sources.
3938
        def make_stacked_stacked():
3939
            _, stacked = self.prepare_stacked_remote_branch()
3940
            tree = stacked.bzrdir.sprout('tree3', stacked=True
3941
                ).open_workingtree()
4595.4.4 by Robert Collins
Disable committing directly to stacked branches from lightweight checkouts.
3942
            local_tree = tree.branch.create_checkout('local-tree3')
3943
            local_tree.commit('more local changes are better')
4577.1.1 by Robert Collins
Fix fetching from smart servers where there is a chain of stacked repositories rather than a single stacking point. (Robert Collins, bug #406597)
3944
            branch = Branch.open(self.get_url('tree3'))
3945
            branch.lock_read()
4857.2.3 by John Arbash Meinel
Found the failed-to-unlocked branches in test_remote.
3946
            self.addCleanup(branch.unlock)
4577.1.1 by Robert Collins
Fix fetching from smart servers where there is a chain of stacked repositories rather than a single stacking point. (Robert Collins, bug #406597)
3947
            return None, branch
3948
        rev_ord, expected_revs = self.get_ordered_revs('1.9', 'unordered',
3949
            branch_factory=make_stacked_stacked)
3950
        self.assertEqual(set(expected_revs), set(rev_ord))
3951
        # Getting unordered results should have made a streaming data request
3952
        # from the server, and one from each backing repo
3953
        self.assertLength(3, self.hpss_calls)
3954
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3955
    def test_stacked_get_stream_topological(self):
3956
        # Repository._get_source.get_stream() from a stacked repository with
3957
        # topological sorting yields the full data from both stacked and
3958
        # stacked upon sources in topological order.
3959
        rev_ord, expected_revs = self.get_ordered_revs('knit', 'topological')
4152.1.2 by Robert Collins
Add streaming from a stacked branch when the sort order is compatible with doing so.
3960
        self.assertEqual(expected_revs, rev_ord)
4595.4.4 by Robert Collins
Disable committing directly to stacked branches from lightweight checkouts.
3961
        # Getting topological sort requires VFS calls still - one of which is
3962
        # pushing up from the bound branch.
5972.3.25 by Jelmer Vernooij
Fix another use of get_ancestry.
3963
        self.assertLength(14, self.hpss_calls)
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3964
3965
    def test_stacked_get_stream_groupcompress(self):
3966
        # Repository._get_source.get_stream() from a stacked repository with
3967
        # groupcompress sorting yields the full data from both stacked and
3968
        # stacked upon sources in groupcompress order.
3969
        raise tests.TestSkipped('No groupcompress ordered format available')
3970
        rev_ord, expected_revs = self.get_ordered_revs('dev5', 'groupcompress')
4152.1.2 by Robert Collins
Add streaming from a stacked branch when the sort order is compatible with doing so.
3971
        self.assertEqual(expected_revs, reversed(rev_ord))
3972
        # Getting unordered results should have made a streaming data request
3973
        # from the backing branch, and one from the stacked on branch.
3974
        self.assertLength(2, self.hpss_calls)
4152.1.1 by Robert Collins
Add specific tests for fetch streaming in the bzr protocol client.
3975
4332.2.1 by Robert Collins
Fix bug 360791 by not raising an error when a smart server is asked for more content than it has locally; the client is assumed to be monitoring what it gets.
3976
    def test_stacked_pull_more_than_stacking_has_bug_360791(self):
3977
        # When pulling some fixed amount of content that is more than the
3978
        # source has (because some is coming from a fallback branch, no error
3979
        # should be received. This was reported as bug 360791.
3980
        # Need three branches: a trunk, a stacked branch, and a preexisting
3981
        # branch pulling content from stacked and trunk.
3982
        self.setup_smart_server_with_call_log()
3983
        trunk = self.make_branch_and_tree('trunk', format="1.9-rich-root")
3984
        r1 = trunk.commit('start')
3985
        stacked_branch = trunk.branch.create_clone_on_transport(
3986
            self.get_transport('stacked'), stacked_on=trunk.branch.base)
3987
        local = self.make_branch('local', format='1.9-rich-root')
3988
        local.repository.fetch(stacked_branch.repository,
3989
            stacked_branch.last_revision())
3990
3834.3.2 by Andrew Bennetts
Preserve BzrBranch5's _synchronize_history code without affecting Branch or BzrBranch7; add effort test for RemoteBranch.copy_content_into.
3991
3992
class TestRemoteBranchEffort(tests.TestCaseWithTransport):
3993
3994
    def setUp(self):
3995
        super(TestRemoteBranchEffort, self).setUp()
3996
        # Create a smart server that publishes whatever the backing VFS server
3997
        # does.
5017.3.28 by Vincent Ladeuil
selftest -s bt.test_remote passing
3998
        self.smart_server = test_server.SmartTCPServer_for_testing()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
3999
        self.start_server(self.smart_server, self.get_server())
3834.3.2 by Andrew Bennetts
Preserve BzrBranch5's _synchronize_history code without affecting Branch or BzrBranch7; add effort test for RemoteBranch.copy_content_into.
4000
        # Log all HPSS calls into self.hpss_calls.
4001
        _SmartClient.hooks.install_named_hook(
4002
            'call', self.capture_hpss_call, None)
4003
        self.hpss_calls = []
4004
4005
    def capture_hpss_call(self, params):
4006
        self.hpss_calls.append(params.method)
4007
4008
    def test_copy_content_into_avoids_revision_history(self):
4009
        local = self.make_branch('local')
5539.2.5 by Andrew Bennetts
Add test to test_remote, fix another shallow bug.
4010
        builder = self.make_branch_builder('remote')
4011
        builder.build_commit(message="Commit.")
3834.3.2 by Andrew Bennetts
Preserve BzrBranch5's _synchronize_history code without affecting Branch or BzrBranch7; add effort test for RemoteBranch.copy_content_into.
4012
        remote_branch_url = self.smart_server.get_url() + 'remote'
4013
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
4014
        local.repository.fetch(remote_branch.repository)
4015
        self.hpss_calls = []
4016
        remote_branch.copy_content_into(local)
3834.3.3 by John Arbash Meinel
Merge bzr.dev, resolve conflict in tests.
4017
        self.assertFalse('Branch.revision_history' in self.hpss_calls)
5539.2.5 by Andrew Bennetts
Add test to test_remote, fix another shallow bug.
4018
5539.2.6 by Andrew Bennetts
Better test name.
4019
    def test_fetch_everything_needs_just_one_call(self):
5539.2.5 by Andrew Bennetts
Add test to test_remote, fix another shallow bug.
4020
        local = self.make_branch('local')
4021
        builder = self.make_branch_builder('remote')
4022
        builder.build_commit(message="Commit.")
4023
        remote_branch_url = self.smart_server.get_url() + 'remote'
4024
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
4025
        self.hpss_calls = []
6015.5.1 by Vincent Ladeuil
Merge 2.3 into 2.4
4026
        local.repository.fetch(
4027
            remote_branch.repository,
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
4028
            fetch_spec=vf_search.EverythingResult(remote_branch.repository))
5539.2.14 by Andrew Bennetts
Don't add a new verb; instead just teach the client to fallback if it gets a BadSearch error.
4029
        self.assertEqual(['Repository.get_stream_1.19'], self.hpss_calls)
5539.2.13 by Andrew Bennetts
Add a test for compatibility with pre-2.3 servers.
4030
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4031
    def override_verb(self, verb_name, verb):
4032
        request_handlers = request.request_handlers
4033
        orig_verb = request_handlers.get(verb_name)
6206.1.11 by Jelmer Vernooij
Preserve info when restoring verbs after disabling/overriding.
4034
        orig_info = request_handlers.get_info(verb_name)
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4035
        request_handlers.register(verb_name, verb, override_existing=True)
4036
        self.addCleanup(request_handlers.register, verb_name, orig_verb,
6206.1.11 by Jelmer Vernooij
Preserve info when restoring verbs after disabling/overriding.
4037
                override_existing=True, info=orig_info)
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4038
5539.2.13 by Andrew Bennetts
Add a test for compatibility with pre-2.3 servers.
4039
    def test_fetch_everything_backwards_compat(self):
5536.3.3 by Andrew Bennetts
Merge lp:bzr.
4040
        """Can fetch with EverythingResult even with pre 2.4 servers.
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4041
        
5536.3.3 by Andrew Bennetts
Merge lp:bzr.
4042
        Pre-2.4 do not support 'everything' searches with the
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4043
        Repository.get_stream_1.19 verb.
5539.2.13 by Andrew Bennetts
Add a test for compatibility with pre-2.3 servers.
4044
        """
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4045
        verb_log = []
4046
        class OldGetStreamVerb(SmartServerRepositoryGetStream_1_19):
4047
            """A version of the Repository.get_stream_1.19 verb patched to
5536.3.3 by Andrew Bennetts
Merge lp:bzr.
4048
            reject 'everything' searches the way 2.3 and earlier do.
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4049
            """
6015.5.1 by Vincent Ladeuil
Merge 2.3 into 2.4
4050
            def recreate_search(self, repository, search_bytes,
4051
                                discard_excess=False):
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4052
                verb_log.append(search_bytes.split('\n', 1)[0])
4053
                if search_bytes == 'everything':
6015.5.1 by Vincent Ladeuil
Merge 2.3 into 2.4
4054
                    return (None,
4055
                            request.FailedSmartServerResponse(('BadSearch',)))
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4056
                return super(OldGetStreamVerb,
4057
                        self).recreate_search(repository, search_bytes,
4058
                            discard_excess=discard_excess)
4059
        self.override_verb('Repository.get_stream_1.19', OldGetStreamVerb)
5539.2.13 by Andrew Bennetts
Add a test for compatibility with pre-2.3 servers.
4060
        local = self.make_branch('local')
4061
        builder = self.make_branch_builder('remote')
4062
        builder.build_commit(message="Commit.")
4063
        remote_branch_url = self.smart_server.get_url() + 'remote'
4064
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
4065
        self.hpss_calls = []
6015.5.1 by Vincent Ladeuil
Merge 2.3 into 2.4
4066
        local.repository.fetch(
4067
            remote_branch.repository,
6341.1.4 by Jelmer Vernooij
Move more functionality to vf_search.
4068
            fetch_spec=vf_search.EverythingResult(remote_branch.repository))
5536.2.7 by Andrew Bennetts
Fix test_fetch_everything_backwards_compat to actually test what it is intended to test.
4069
        # make sure the overridden verb was used
4070
        self.assertLength(1, verb_log)
4071
        # more than one HPSS call is needed, but because it's a VFS callback
4072
        # its hard to predict exactly how many.
4073
        self.assertTrue(len(self.hpss_calls) > 1)
5539.2.13 by Andrew Bennetts
Add a test for compatibility with pre-2.3 servers.
4074
5609.50.1 by Vincent Ladeuil
Be more tolerant about ``bound_location`` from config files
4075
5609.50.4 by Vincent Ladeuil
Add more tests for accepted bound_location variations.
4076
class TestUpdateBoundBranchWithModifiedBoundLocation(
4077
    tests.TestCaseWithTransport):
4078
    """Ensure correct handling of bound_location modifications.
4079
4080
    This is tested against a smart server as http://pad.lv/786980 was about a
4081
    ReadOnlyError (write attempt during a read-only transaction) which can only
4082
    happen in this context.
4083
    """
4084
4085
    def setUp(self):
4086
        super(TestUpdateBoundBranchWithModifiedBoundLocation, self).setUp()
5609.50.1 by Vincent Ladeuil
Be more tolerant about ``bound_location`` from config files
4087
        self.transport_server = test_server.SmartTCPServer_for_testing
5609.50.4 by Vincent Ladeuil
Add more tests for accepted bound_location variations.
4088
4089
    def make_master_and_checkout(self, master_name, checkout_name):
4090
        # Create the master branch and its associated checkout
4091
        self.master = self.make_branch_and_tree(master_name)
4092
        self.checkout = self.master.branch.create_checkout(checkout_name)
4093
        # Modify the master branch so there is something to update
4094
        self.master.commit('add stuff')
4095
        self.last_revid = self.master.commit('even more stuff')
4096
        self.bound_location = self.checkout.branch.get_bound_location()
4097
4098
    def assertUpdateSucceeds(self, new_location):
4099
        self.checkout.branch.set_bound_location(new_location)
4100
        self.checkout.update()
4101
        self.assertEquals(self.last_revid, self.checkout.last_revision())
4102
4103
    def test_without_final_slash(self):
4104
        self.make_master_and_checkout('master', 'checkout')
5609.50.1 by Vincent Ladeuil
Be more tolerant about ``bound_location`` from config files
4105
        # For unclear reasons some users have a bound_location without a final
4106
        # '/', simulate that by forcing such a value
5609.50.4 by Vincent Ladeuil
Add more tests for accepted bound_location variations.
4107
        self.assertEndsWith(self.bound_location, '/')
4108
        self.assertUpdateSucceeds(self.bound_location.rstrip('/'))
4109
4110
    def test_plus_sign(self):
4111
        self.make_master_and_checkout('+master', 'checkout')
4112
        self.assertUpdateSucceeds(self.bound_location.replace('%2B', '+', 1))
4113
4114
    def test_tilda(self):
4115
        # Embed ~ in the middle of the path just to avoid any $HOME
4116
        # interpretation
4117
        self.make_master_and_checkout('mas~ter', 'checkout')
4118
        self.assertUpdateSucceeds(self.bound_location.replace('%2E', '~', 1))
6284.1.1 by Jelmer Vernooij
Allow registering custom error handlers in the HPSS client.
4119
4120
4121
class TestWithCustomErrorHandler(RemoteBranchTestCase):
4122
4123
    def test_no_context(self):
4124
        class OutOfCoffee(errors.BzrError):
4125
            """A dummy exception for testing."""
4126
4127
            def __init__(self, urgency):
4128
                self.urgency = urgency
4129
        remote.no_context_error_translators.register("OutOfCoffee",
4130
            lambda err: OutOfCoffee(err.error_args[0]))
4131
        transport = MemoryTransport()
4132
        client = FakeClient(transport.base)
4133
        client.add_expected_call(
4134
            'Branch.get_stacked_on_url', ('quack/',),
4135
            'error', ('NotStacked',))
4136
        client.add_expected_call(
4137
            'Branch.last_revision_info',
4138
            ('quack/',),
4139
            'error', ('OutOfCoffee', 'low'))
4140
        transport.mkdir('quack')
4141
        transport = transport.clone('quack')
4142
        branch = self.make_remote_branch(transport, client)
4143
        self.assertRaises(OutOfCoffee, branch.last_revision_info)
4144
        self.assertFinished(client)
4145
4146
    def test_with_context(self):
4147
        class OutOfTea(errors.BzrError):
4148
            def __init__(self, branch, urgency):
4149
                self.branch = branch
4150
                self.urgency = urgency
4151
        remote.error_translators.register("OutOfTea",
4152
            lambda err, find, path: OutOfTea(err.error_args[0],
4153
                find("branch")))
4154
        transport = MemoryTransport()
4155
        client = FakeClient(transport.base)
4156
        client.add_expected_call(
4157
            'Branch.get_stacked_on_url', ('quack/',),
4158
            'error', ('NotStacked',))
4159
        client.add_expected_call(
4160
            'Branch.last_revision_info',
4161
            ('quack/',),
4162
            'error', ('OutOfTea', 'low'))
4163
        transport.mkdir('quack')
4164
        transport = transport.clone('quack')
4165
        branch = self.make_remote_branch(transport, client)
4166
        self.assertRaises(OutOfTea, branch.last_revision_info)
4167
        self.assertFinished(client)
6305.2.1 by Jelmer Vernooij
add remote call for Repository.pack.
4168
4169
4170
class TestRepositoryPack(TestRemoteRepository):
4171
4172
    def test_pack(self):
4173
        transport_path = 'quack'
4174
        repo, client = self.setup_fake_client_and_repository(transport_path)
4175
        client.add_expected_call(
4176
            'Repository.lock_write', ('quack/', ''),
4177
            'success', ('ok', 'token'))
4178
        client.add_expected_call(
6305.2.4 by Jelmer Vernooij
Fix tests.
4179
            'Repository.pack', ('quack/', 'token', 'False'),
6305.2.3 by Jelmer Vernooij
Store hint in body.
4180
            'success', ('ok',), )
6305.2.4 by Jelmer Vernooij
Fix tests.
4181
        client.add_expected_call(
4182
            'Repository.unlock', ('quack/', 'token'),
4183
            'success', ('ok', ))
6305.2.1 by Jelmer Vernooij
add remote call for Repository.pack.
4184
        repo.pack()
4185
4186
    def test_pack_with_hint(self):
4187
        transport_path = 'quack'
4188
        repo, client = self.setup_fake_client_and_repository(transport_path)
4189
        client.add_expected_call(
4190
            'Repository.lock_write', ('quack/', ''),
4191
            'success', ('ok', 'token'))
4192
        client.add_expected_call(
6305.2.4 by Jelmer Vernooij
Fix tests.
4193
            'Repository.pack', ('quack/', 'token', 'False'),
6305.2.3 by Jelmer Vernooij
Store hint in body.
4194
            'success', ('ok',), )
6305.2.4 by Jelmer Vernooij
Fix tests.
4195
        client.add_expected_call(
4196
            'Repository.unlock', ('quack/', 'token', 'False'),
4197
            'success', ('ok', ))
6305.2.1 by Jelmer Vernooij
add remote call for Repository.pack.
4198
        repo.pack(['hinta', 'hintb'])