/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3407.2.2 by Martin Pool
Remove special case in RemoteBranchLockableFiles for branch.conf
1
# Copyright (C) 2006, 2007, 2008 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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
28
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
29
from bzrlib import (
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.
30
    bzrdir,
3777.1.3 by Aaron Bentley
Use SSH default username from authentication.conf
31
    config,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
32
    errors,
3184.1.9 by Robert Collins
* ``Repository.get_data_stream`` is now deprecated in favour of
33
    graph,
2535.3.39 by Andrew Bennetts
Tidy some XXXs.
34
    pack,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
35
    remote,
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
36
    repository,
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
37
    smart,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
38
    tests,
3691.2.4 by Martin Pool
Add FakeRemoteTransport to clarify test_remote
39
    urlutils,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
40
    )
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
41
from bzrlib.branch import Branch
42
from bzrlib.bzrdir import BzrDir, BzrDirFormat
43
from bzrlib.remote import (
44
    RemoteBranch,
45
    RemoteBzrDir,
46
    RemoteBzrDirFormat,
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
47
    RemoteRepository,
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
48
    )
49
from bzrlib.revision import NULL_REVISION
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
50
from bzrlib.smart import server, medium
2018.5.159 by Andrew Bennetts
Rename SmartClient to _SmartClient.
51
from bzrlib.smart.client import _SmartClient
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
52
from bzrlib.symbol_versioning import one_four
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
53
from bzrlib.transport import get_transport, http
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
54
from bzrlib.transport.memory import MemoryTransport
3777.1.3 by Aaron Bentley
Use SSH default username from authentication.conf
55
from bzrlib.transport.remote import (
56
    RemoteTransport,
57
    RemoteSSHTransport,
58
    RemoteTCPTransport,
59
)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
60
2018.5.24 by Andrew Bennetts
Setting NO_SMART_VFS in environment will disable VFS methods in the smart server. (Robert Collins, John Arbash Meinel, Andrew Bennetts)
61
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
62
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
63
64
    def setUp(self):
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.
65
        self.transport_server = server.SmartTCPServer_for_testing
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
66
        super(BasicRemoteObjectTests, self).setUp()
67
        self.transport = self.get_transport()
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
68
        # 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
69
        self.local_wt = BzrDir.create_standalone_workingtree('.')
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
70
2018.5.171 by Andrew Bennetts
Disconnect RemoteTransports in some tests to avoid tripping up test_strace with leftover threads from previous tests.
71
    def tearDown(self):
72
        self.transport.disconnect()
73
        tests.TestCaseWithTransport.tearDown(self)
74
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
75
    def test_create_remote_bzrdir(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.
76
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
77
        self.assertIsInstance(b, BzrDir)
78
79
    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.
80
        # open a standalone branch in the working directory
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.
81
        b = remote.RemoteBzrDir(self.transport, remote.RemoteBzrDirFormat())
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
82
        branch = b.open_branch()
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
83
        self.assertIsInstance(branch, Branch)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
84
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
85
    def test_remote_repository(self):
86
        b = BzrDir.open_from_transport(self.transport)
87
        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.
88
        revid = u'\xc823123123'.encode('utf8')
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
89
        self.assertFalse(repo.has_revision(revid))
90
        self.local_wt.commit(message='test commit', rev_id=revid)
91
        self.assertTrue(repo.has_revision(revid))
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
92
93
    def test_remote_branch_revision_history(self):
94
        b = BzrDir.open_from_transport(self.transport).open_branch()
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
95
        self.assertEqual([], b.revision_history())
96
        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.
97
        r2 = self.local_wt.commit('1st commit', rev_id=u'\xc8'.encode('utf8'))
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
98
        self.assertEqual([r1, r2], b.revision_history())
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
99
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
100
    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)
101
        """Should open a RemoteBzrDir over a RemoteTransport"""
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
102
        fmt = BzrDirFormat.find_format(self.transport)
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
103
        self.assertTrue(RemoteBzrDirFormat
104
                        in BzrDirFormat._control_server_formats)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
105
        self.assertIsInstance(fmt, remote.RemoteBzrDirFormat)
106
107
    def test_open_detected_smart_format(self):
108
        fmt = BzrDirFormat.find_format(self.transport)
109
        d = fmt.open(self.transport)
110
        self.assertIsInstance(d, BzrDir)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
111
2477.1.1 by Martin Pool
Add RemoteBranch repr
112
    def test_remote_branch_repr(self):
113
        b = BzrDir.open_from_transport(self.transport).open_branch()
114
        self.assertStartsWith(str(b), 'RemoteBranch(')
115
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
116
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
117
class FakeProtocol(object):
118
    """Lookalike SmartClientRequestProtocolOne allowing body reading tests."""
119
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
120
    def __init__(self, body, fake_client):
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
121
        self.body = body
122
        self._body_buffer = None
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
123
        self._fake_client = fake_client
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
124
125
    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.
126
        if self._body_buffer is None:
127
            self._body_buffer = StringIO(self.body)
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
128
        bytes = self._body_buffer.read(count)
129
        if self._body_buffer.tell() == len(self._body_buffer.getvalue()):
130
            self._fake_client.expecting_body = False
131
        return bytes
132
133
    def cancel_read_body(self):
134
        self._fake_client.expecting_body = False
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
135
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
136
    def read_streamed_body(self):
137
        return self.body
138
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
139
2018.5.159 by Andrew Bennetts
Rename SmartClient to _SmartClient.
140
class FakeClient(_SmartClient):
141
    """Lookalike for _SmartClient allowing testing."""
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
142
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
143
    def __init__(self, fake_medium_base='fake base'):
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
144
        """Create a FakeClient."""
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
145
        self.responses = []
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
146
        self._calls = []
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
147
        self.expecting_body = False
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
148
        # if non-None, this is the list of expected calls, with only the
149
        # 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.
150
        # compute so is not included. If a call is None, that call can
151
        # be anything.
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
152
        self._expected_calls = None
3431.3.2 by Andrew Bennetts
Remove 'base' from _SmartClient entirely, now that the medium has it.
153
        _SmartClient.__init__(self, FakeMedium(self._calls, fake_medium_base))
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
154
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
155
    def add_expected_call(self, call_name, call_args, response_type,
156
        response_args, response_body=None):
157
        if self._expected_calls is None:
158
            self._expected_calls = []
159
        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
160
        self.responses.append((response_type, response_args, response_body))
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
161
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
162
    def add_success_response(self, *args):
163
        self.responses.append(('success', args, None))
164
165
    def add_success_response_with_body(self, body, *args):
166
        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.
167
        if self._expected_calls is not None:
168
            self._expected_calls.append(None)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
169
170
    def add_error_response(self, *args):
171
        self.responses.append(('error', args))
172
173
    def add_unknown_method_response(self, verb):
174
        self.responses.append(('unknown', verb))
175
3691.2.8 by Martin Pool
Update some test_remote tests for Branch.get_stacked_on_url and with clearer assertions
176
    def finished_test(self):
177
        if self._expected_calls:
178
            raise AssertionError("%r finished but was still expecting %r"
179
                % (self, self._expected_calls[0]))
180
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.
181
    def _get_next_response(self):
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
182
        try:
183
            response_tuple = self.responses.pop(0)
184
        except IndexError, e:
185
            raise AssertionError("%r didn't expect any more calls"
186
                % (self,))
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
187
        if response_tuple[0] == 'unknown':
188
            raise errors.UnknownSmartMethod(response_tuple[1])
189
        elif response_tuple[0] == 'error':
190
            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.
191
        return response_tuple
192
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
193
    def _check_call(self, method, args):
194
        if self._expected_calls is None:
195
            # the test should be updated to say what it expects
196
            return
197
        try:
198
            next_call = self._expected_calls.pop(0)
199
        except IndexError:
200
            raise AssertionError("%r didn't expect any more calls "
201
                "but got %r%r"
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
202
                % (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.
203
        if next_call is None:
204
            return
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
205
        if method != next_call[0] or args != next_call[1]:
206
            raise AssertionError("%r expected %r%r "
207
                "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
208
                % (self, next_call[0], next_call[1], method, args,))
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
209
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
210
    def call(self, method, *args):
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
211
        self._check_call(method, args)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
212
        self._calls.append(('call', method, args))
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
213
        return self._get_next_response()[1]
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
214
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
215
    def call_expecting_body(self, method, *args):
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
216
        self._check_call(method, args)
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
217
        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.
218
        result = self._get_next_response()
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
219
        self.expecting_body = True
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
220
        return result[1], FakeProtocol(result[2], self)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
221
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.
222
    def call_with_body_bytes_expecting_body(self, method, args, body):
3691.2.7 by Martin Pool
FakeClient can know what calls to expect
223
        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.
224
        self._calls.append(('call_with_body_bytes_expecting_body', method,
225
            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.
226
        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.
227
        self.expecting_body = True
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
228
        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.
229
3842.3.9 by Andrew Bennetts
Backing up the stream so that we can fallback correctly.
230
    def call_with_body_stream(self, args, stream):
231
        # Explicitly consume the stream before checking for an error, because
232
        # that's what happens a real medium.
233
        stream = list(stream)
234
        self._check_call(args[0], args[1:])
235
        self._calls.append(('call_with_body_stream', args[0], args[1:], stream))
236
        return self._get_next_response()[1]
237
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
238
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
239
class FakeMedium(medium.SmartClientMedium):
3104.4.2 by Andrew Bennetts
All tests passing.
240
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.
241
    def __init__(self, client_calls, base):
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
242
        medium.SmartClientMedium.__init__(self, base)
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
243
        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.
244
245
    def disconnect(self):
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
246
        self._client_calls.append(('disconnect medium',))
3104.4.2 by Andrew Bennetts
All tests passing.
247
248
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.
249
class TestVfsHas(tests.TestCase):
250
251
    def test_unicode_path(self):
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
252
        client = FakeClient('/')
253
        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.
254
        transport = RemoteTransport('bzr://localhost/', _client=client)
255
        filename = u'/hell\u00d8'.encode('utf8')
256
        result = transport.has(filename)
257
        self.assertEqual(
258
            [('call', 'has', (filename,))],
259
            client._calls)
260
        self.assertTrue(result)
261
262
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
263
class TestRemote(tests.TestCaseWithMemoryTransport):
4032.1.1 by John Arbash Meinel
Merge the removal of all trailing whitespace, and resolve conflicts.
264
4053.1.2 by Robert Collins
Actually make this branch work.
265
    def get_repo_format(self):
266
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
267
        return reference_bzrdir_format.repository_format
268
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
269
    def disable_verb(self, verb):
270
        """Disable a verb for one test."""
271
        request_handlers = smart.request.request_handlers
272
        orig_method = request_handlers.get(verb)
273
        request_handlers.remove(verb)
274
        def restoreVerb():
275
            request_handlers.register(verb, orig_method)
276
        self.addCleanup(restoreVerb)
277
278
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
279
class Test_ClientMedium_remote_path_from_transport(tests.TestCase):
280
    """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.
281
282
    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.
283
        """Assert that the result of
284
        SmartClientMedium.remote_path_from_transport is the expected value for
285
        a given client_base and transport_base.
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
286
        """
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
287
        client_medium = medium.SmartClientMedium(client_base)
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
288
        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.
289
        result = client_medium.remote_path_from_transport(transport)
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
290
        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.
291
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
292
    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.
293
        """SmartClientMedium.remote_path_from_transport calculates a URL for
294
        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.
295
        """
296
        self.assertRemotePath('xyz/', 'bzr://host/path', 'bzr://host/xyz')
297
        self.assertRemotePath(
298
            'path/xyz/', 'bzr://host/path', 'bzr://host/path/xyz')
299
3431.3.11 by Andrew Bennetts
Push remote_path_from_transport logic into SmartClientMedium, removing special-casing of bzr+http from _SmartClient.
300
    def assertRemotePathHTTP(self, expected, transport_base, relpath):
301
        """Assert that the result of
302
        HttpTransportBase.remote_path_from_transport is the expected value for
303
        a given transport_base and relpath of that transport.  (Note that
304
        HttpTransportBase is a subclass of SmartClientMedium)
305
        """
306
        base_transport = get_transport(transport_base)
307
        client_medium = base_transport.get_smart_medium()
308
        cloned_transport = base_transport.clone(relpath)
309
        result = client_medium.remote_path_from_transport(cloned_transport)
310
        self.assertEqual(expected, result)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
311
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
312
    def test_remote_path_from_transport_http(self):
313
        """Remote paths for HTTP transports are calculated differently to other
314
        transports.  They are just relative to the client base, not the root
315
        directory of the host.
316
        """
317
        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.
318
            self.assertRemotePathHTTP(
319
                '../xyz/', scheme + '//host/path', '../xyz/')
320
            self.assertRemotePathHTTP(
321
                'xyz/', scheme + '//host/path', 'xyz/')
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
322
323
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
324
class Test_ClientMedium_remote_is_at_least(tests.TestCase):
325
    """Tests for the behaviour of client_medium.remote_is_at_least."""
326
327
    def test_initially_unlimited(self):
328
        """A fresh medium assumes that the remote side supports all
329
        versions.
330
        """
331
        client_medium = medium.SmartClientMedium('dummy base')
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
332
        self.assertFalse(client_medium._is_remote_before((99, 99)))
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
333
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
334
    def test__remember_remote_is_before(self):
335
        """Calling _remember_remote_is_before ratchets down the known remote
336
        version.
337
        """
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
338
        client_medium = medium.SmartClientMedium('dummy base')
339
        # Mark the remote side as being less than 1.6.  The remote side may
340
        # still be 1.5.
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
341
        client_medium._remember_remote_is_before((1, 6))
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
342
        self.assertTrue(client_medium._is_remote_before((1, 6)))
343
        self.assertFalse(client_medium._is_remote_before((1, 5)))
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
344
        # Calling _remember_remote_is_before again with a lower value works.
345
        client_medium._remember_remote_is_before((1, 5))
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
346
        self.assertTrue(client_medium._is_remote_before((1, 5)))
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
347
        # You cannot call _remember_remote_is_before with a larger value.
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
348
        self.assertRaises(
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
349
            AssertionError, client_medium._remember_remote_is_before, (1, 9))
3453.4.1 by Andrew Bennetts
Better infrastructure on SmartClientMedium for tracking the remote version.
350
351
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
352
class TestBzrDirCloningMetaDir(TestRemote):
353
354
    def test_backwards_compat(self):
355
        self.setup_smart_server_with_call_log()
356
        a_dir = self.make_bzrdir('.')
357
        self.reset_smart_call_log()
358
        verb = 'BzrDir.cloning_metadir'
359
        self.disable_verb(verb)
360
        format = a_dir.cloning_metadir()
361
        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.
362
            call.call.method == verb])
4070.2.3 by Robert Collins
Get BzrDir.cloning_metadir working.
363
        self.assertEqual(1, call_count)
364
365
    def test_current_server(self):
366
        transport = self.get_transport('.')
367
        transport = transport.clone('quack')
368
        self.make_bzrdir('quack')
369
        client = FakeClient(transport.base)
370
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
371
        control_name = reference_bzrdir_format.network_name()
372
        client.add_expected_call(
373
            'BzrDir.cloning_metadir', ('quack/', 'False'),
374
            'success', (control_name, '', '')),
375
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
376
            _client=client)
377
        result = a_bzrdir.cloning_metadir()
378
        # We should have got a reference control dir with default branch and
379
        # repository formats.
380
        # This pokes a little, just to be sure.
381
        self.assertEqual(bzrdir.BzrDirMetaFormat1, type(result))
382
        self.assertEqual(reference_bzrdir_format.repository_format,
383
            result._repository_format)
384
        self.assertEqual(reference_bzrdir_format.get_branch_format(),
385
            result._branch_format)
386
        client.finished_test()
387
388
4053.1.2 by Robert Collins
Actually make this branch work.
389
class TestBzrDirOpenBranch(TestRemote):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
390
391
    def test_branch_present(self):
4053.1.2 by Robert Collins
Actually make this branch work.
392
        reference_format = self.get_repo_format()
393
        network_name = reference_format.network_name()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
394
        transport = MemoryTransport()
395
        transport.mkdir('quack')
396
        transport = transport.clone('quack')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
397
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
398
        client.add_expected_call(
399
            'BzrDir.open_branch', ('quack/',),
400
            'success', ('ok', ''))
401
        client.add_expected_call(
4053.1.2 by Robert Collins
Actually make this branch work.
402
            'BzrDir.find_repositoryV3', ('quack/',),
403
            'success', ('ok', '', 'no', 'no', 'no', network_name))
3691.2.10 by Martin Pool
Update more test_remote tests
404
        client.add_expected_call(
405
            'Branch.get_stacked_on_url', ('quack/',),
406
            'error', ('NotStacked',))
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.
407
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
408
            _client=client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
409
        result = bzrdir.open_branch()
410
        self.assertIsInstance(result, RemoteBranch)
411
        self.assertEqual(bzrdir, result.bzrdir)
3691.2.10 by Martin Pool
Update more test_remote tests
412
        client.finished_test()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
413
414
    def test_branch_missing(self):
415
        transport = MemoryTransport()
416
        transport.mkdir('quack')
417
        transport = transport.clone('quack')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
418
        client = FakeClient(transport.base)
419
        client.add_error_response('nobranch')
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.
420
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
421
            _client=client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
422
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
423
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
424
            [('call', 'BzrDir.open_branch', ('quack/',))],
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
425
            client._calls)
426
3211.4.1 by Robert Collins
* ``RemoteBzrDir._get_tree_branch`` no longer triggers ``_ensure_real``,
427
    def test__get_tree_branch(self):
428
        # _get_tree_branch is a form of open_branch, but it should only ask for
429
        # branch opening, not any other network requests.
430
        calls = []
431
        def open_branch():
432
            calls.append("Called")
433
            return "a-branch"
434
        transport = MemoryTransport()
435
        # 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.
436
        client = FakeClient(transport.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.
437
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
438
            _client=client)
3211.4.1 by Robert Collins
* ``RemoteBzrDir._get_tree_branch`` no longer triggers ``_ensure_real``,
439
        # patch the open_branch call to record that it was called.
440
        bzrdir.open_branch = open_branch
441
        self.assertEqual((None, "a-branch"), bzrdir._get_tree_branch())
442
        self.assertEqual(["Called"], calls)
443
        self.assertEqual([], client._calls)
444
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.
445
    def test_url_quoting_of_path(self):
446
        # Relpaths on the wire should not be URL-escaped.  So "~" should be
447
        # 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.
448
        transport = RemoteTCPTransport('bzr://localhost/~hello/')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
449
        client = FakeClient(transport.base)
4053.1.2 by Robert Collins
Actually make this branch work.
450
        reference_format = self.get_repo_format()
451
        network_name = reference_format.network_name()
3691.2.10 by Martin Pool
Update more test_remote tests
452
        client.add_expected_call(
453
            'BzrDir.open_branch', ('~hello/',),
454
            'success', ('ok', ''))
455
        client.add_expected_call(
4053.1.2 by Robert Collins
Actually make this branch work.
456
            'BzrDir.find_repositoryV3', ('~hello/',),
457
            'success', ('ok', '', 'no', 'no', 'no', network_name))
3691.2.10 by Martin Pool
Update more test_remote tests
458
        client.add_expected_call(
459
            'Branch.get_stacked_on_url', ('~hello/',),
460
            'error', ('NotStacked',))
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.
461
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
462
            _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.
463
        result = bzrdir.open_branch()
3691.2.10 by Martin Pool
Update more test_remote tests
464
        client.finished_test()
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.
465
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
466
    def check_open_repository(self, rich_root, subtrees, external_lookup='no'):
4053.1.2 by Robert Collins
Actually make this branch work.
467
        reference_format = self.get_repo_format()
468
        network_name = reference_format.network_name()
3104.4.2 by Andrew Bennetts
All tests passing.
469
        transport = MemoryTransport()
470
        transport.mkdir('quack')
471
        transport = transport.clone('quack')
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
472
        if rich_root:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
473
            rich_response = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
474
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
475
            rich_response = 'no'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
476
        if subtrees:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
477
            subtree_response = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
478
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
479
            subtree_response = 'no'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
480
        client = FakeClient(transport.base)
481
        client.add_success_response(
4053.1.2 by Robert Collins
Actually make this branch work.
482
            'ok', '', rich_response, subtree_response, external_lookup,
483
            network_name)
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.
484
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
485
            _client=client)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
486
        result = bzrdir.open_repository()
487
        self.assertEqual(
4053.1.2 by Robert Collins
Actually make this branch work.
488
            [('call', 'BzrDir.find_repositoryV3', ('quack/',))],
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
489
            client._calls)
490
        self.assertIsInstance(result, RemoteRepository)
491
        self.assertEqual(bzrdir, result.bzrdir)
492
        self.assertEqual(rich_root, result._format.rich_root_data)
2018.5.138 by Robert Collins
Merge bzr.dev.
493
        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.
494
495
    def test_open_repository_sets_format_attributes(self):
496
        self.check_open_repository(True, True)
497
        self.check_open_repository(False, True)
498
        self.check_open_repository(True, False)
499
        self.check_open_repository(False, False)
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
500
        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.
501
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
502
    def test_old_server(self):
503
        """RemoteBzrDirFormat should fail to probe if the server version is too
504
        old.
505
        """
506
        self.assertRaises(errors.NotBranchError,
507
            RemoteBzrDirFormat.probe_transport, OldServerTransport())
508
509
4032.3.2 by Robert Collins
Create and use a RPC call to create branches on bzr servers rather than using VFS calls.
510
class TestBzrDirCreateBranch(TestRemote):
511
512
    def test_backwards_compat(self):
513
        self.setup_smart_server_with_call_log()
514
        repo = self.make_repository('.')
515
        self.reset_smart_call_log()
516
        self.disable_verb('BzrDir.create_branch')
517
        branch = repo.bzrdir.create_branch()
518
        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.
519
            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.
520
        self.assertEqual(1, create_branch_call_count)
521
522
    def test_current_server(self):
523
        transport = self.get_transport('.')
524
        transport = transport.clone('quack')
525
        self.make_repository('quack')
526
        client = FakeClient(transport.base)
527
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
528
        reference_format = reference_bzrdir_format.get_branch_format()
529
        network_name = reference_format.network_name()
530
        reference_repo_fmt = reference_bzrdir_format.repository_format
531
        reference_repo_name = reference_repo_fmt.network_name()
532
        client.add_expected_call(
533
            'BzrDir.create_branch', ('quack/', network_name),
534
            'success', ('ok', network_name, '', 'no', 'no', 'yes',
535
            reference_repo_name))
536
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
537
            _client=client)
538
        branch = a_bzrdir.create_branch()
539
        # We should have got a remote branch
540
        self.assertIsInstance(branch, remote.RemoteBranch)
541
        # its format should have the settings from the response
542
        format = branch._format
543
        self.assertEqual(network_name, format.network_name())
544
545
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
546
class TestBzrDirCreateRepository(TestRemote):
4017.3.2 by Robert Collins
Reduce the number of round trips required to create a repository over the network.
547
548
    def test_backwards_compat(self):
549
        self.setup_smart_server_with_call_log()
550
        bzrdir = self.make_bzrdir('.')
551
        self.reset_smart_call_log()
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
552
        self.disable_verb('BzrDir.create_repository')
553
        repo = bzrdir.create_repository()
554
        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.
555
            call.call.method == 'BzrDir.create_repository'])
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
556
        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.
557
558
    def test_current_server(self):
559
        transport = self.get_transport('.')
560
        transport = transport.clone('quack')
561
        self.make_bzrdir('quack')
562
        client = FakeClient(transport.base)
563
        reference_bzrdir_format = bzrdir.format_registry.get('default')()
564
        reference_format = reference_bzrdir_format.repository_format
565
        network_name = reference_format.network_name()
566
        client.add_expected_call(
567
            'BzrDir.create_repository', ('quack/',
568
                'Bazaar pack repository format 1 (needs bzr 0.92)\n', 'False'),
569
            'success', ('ok', 'no', 'no', 'no', network_name))
570
        a_bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
571
            _client=client)
572
        repo = a_bzrdir.create_repository()
573
        # We should have got a remote repository
574
        self.assertIsInstance(repo, remote.RemoteRepository)
575
        # its format should have the settings from the response
576
        format = repo._format
577
        self.assertFalse(format.rich_root_data)
578
        self.assertFalse(format.supports_tree_reference)
579
        self.assertFalse(format.supports_external_lookups)
580
        self.assertEqual(network_name, format.network_name())
581
582
4053.1.1 by Robert Collins
New version of the BzrDir.find_repository verb supporting _network_name to support removing more _ensure_real calls.
583
class TestBzrDirOpenRepository(TestRemote):
584
585
    def test_backwards_compat_1_2_3(self):
586
        # fallback all the way to the first version.
587
        reference_format = self.get_repo_format()
588
        network_name = reference_format.network_name()
589
        client = FakeClient('bzr://example.com/')
590
        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.
591
        client.add_unknown_method_response('BzrDir.find_repositoryV2')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
592
        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.
593
        # A real repository instance will be created to determine the network
594
        # name.
595
        client.add_success_response_with_body(
596
            "Bazaar-NG meta directory, format 1\n", 'ok')
597
        client.add_success_response_with_body(
598
            reference_format.get_format_string(), 'ok')
599
        # PackRepository wants to do a stat
600
        client.add_success_response('stat', '0', '65535')
601
        remote_transport = RemoteTransport('bzr://example.com/quack/', medium=False,
602
            _client=client)
603
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
604
            _client=client)
605
        repo = bzrdir.open_repository()
606
        self.assertEqual(
607
            [('call', 'BzrDir.find_repositoryV3', ('quack/',)),
608
             ('call', 'BzrDir.find_repositoryV2', ('quack/',)),
609
             ('call', 'BzrDir.find_repository', ('quack/',)),
610
             ('call_expecting_body', 'get', ('/quack/.bzr/branch-format',)),
611
             ('call_expecting_body', 'get', ('/quack/.bzr/repository/format',)),
612
             ('call', 'stat', ('/quack/.bzr/repository',)),
613
             ],
614
            client._calls)
615
        self.assertEqual(network_name, repo._format.network_name())
616
617
    def test_backwards_compat_2(self):
618
        # fallback to find_repositoryV2
619
        reference_format = self.get_repo_format()
620
        network_name = reference_format.network_name()
621
        client = FakeClient('bzr://example.com/')
622
        client.add_unknown_method_response('BzrDir.find_repositoryV3')
623
        client.add_success_response('ok', '', 'no', 'no', 'no')
624
        # A real repository instance will be created to determine the network
625
        # name.
626
        client.add_success_response_with_body(
627
            "Bazaar-NG meta directory, format 1\n", 'ok')
628
        client.add_success_response_with_body(
629
            reference_format.get_format_string(), 'ok')
630
        # PackRepository wants to do a stat
631
        client.add_success_response('stat', '0', '65535')
632
        remote_transport = RemoteTransport('bzr://example.com/quack/', medium=False,
633
            _client=client)
634
        bzrdir = RemoteBzrDir(remote_transport, remote.RemoteBzrDirFormat(),
635
            _client=client)
636
        repo = bzrdir.open_repository()
637
        self.assertEqual(
638
            [('call', 'BzrDir.find_repositoryV3', ('quack/',)),
639
             ('call', 'BzrDir.find_repositoryV2', ('quack/',)),
640
             ('call_expecting_body', 'get', ('/quack/.bzr/branch-format',)),
641
             ('call_expecting_body', 'get', ('/quack/.bzr/repository/format',)),
642
             ('call', 'stat', ('/quack/.bzr/repository',)),
643
             ],
644
            client._calls)
645
        self.assertEqual(network_name, repo._format.network_name())
646
647
    def test_current_server(self):
648
        reference_format = self.get_repo_format()
649
        network_name = reference_format.network_name()
650
        transport = MemoryTransport()
651
        transport.mkdir('quack')
652
        transport = transport.clone('quack')
653
        client = FakeClient(transport.base)
654
        client.add_success_response('ok', '', 'no', 'no', 'no', network_name)
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.
655
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
656
            _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.
657
        repo = bzrdir.open_repository()
658
        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.
659
            [('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.
660
            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.
661
        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.
662
663
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
664
class OldSmartClient(object):
665
    """A fake smart client for test_old_version that just returns a version one
666
    response to the 'hello' (query version) command.
667
    """
668
669
    def get_request(self):
670
        input_file = StringIO('ok\x011\n')
671
        output_file = StringIO()
672
        client_medium = medium.SmartSimplePipesClientMedium(
673
            input_file, output_file)
674
        return medium.SmartClientStreamMediumRequest(client_medium)
675
3241.1.1 by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium.
676
    def protocol_version(self):
677
        return 1
678
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
679
680
class OldServerTransport(object):
681
    """A fake transport for test_old_server that reports it's smart server
682
    protocol version as version one.
683
    """
684
685
    def __init__(self):
686
        self.base = 'fake:'
687
688
    def get_smart_client(self):
689
        return OldSmartClient()
690
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
691
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
692
class RemoteBranchTestCase(tests.TestCase):
693
694
    def make_remote_branch(self, transport, client):
695
        """Make a RemoteBranch using 'client' as its _SmartClient.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
696
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
697
        A RemoteBzrDir and RemoteRepository will also be created to fill out
698
        the RemoteBranch, albeit with stub values for some of their attributes.
699
        """
700
        # we do not want bzrdir to make any remote calls, so use False as its
701
        # _client.  If it tries to make a remote call, this will fail
702
        # immediately.
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.
703
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
704
            _client=False)
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
705
        repo = RemoteRepository(bzrdir, None, _client=client)
706
        return RemoteBranch(bzrdir, repo, _client=client)
707
708
709
class TestBranchLastRevisionInfo(RemoteBranchTestCase):
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
710
711
    def test_empty_branch(self):
712
        # in an empty branch we decode the response properly
713
        transport = MemoryTransport()
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
714
        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
715
        client.add_expected_call(
716
            'Branch.get_stacked_on_url', ('quack/',),
717
            'error', ('NotStacked',))
718
        client.add_expected_call(
719
            'Branch.last_revision_info', ('quack/',),
720
            'success', ('ok', '0', 'null:'))
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
721
        transport.mkdir('quack')
722
        transport = transport.clone('quack')
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
723
        branch = self.make_remote_branch(transport, client)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
724
        result = branch.last_revision_info()
3691.2.8 by Martin Pool
Update some test_remote tests for Branch.get_stacked_on_url and with clearer assertions
725
        client.finished_test()
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
726
        self.assertEqual((0, NULL_REVISION), result)
727
728
    def test_non_empty_branch(self):
729
        # 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.
730
        revid = u'\xc8'.encode('utf8')
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
731
        transport = MemoryTransport()
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
732
        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
733
        client.add_expected_call(
734
            'Branch.get_stacked_on_url', ('kwaak/',),
735
            'error', ('NotStacked',))
736
        client.add_expected_call(
737
            'Branch.last_revision_info', ('kwaak/',),
738
            'success', ('ok', '2', revid))
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
739
        transport.mkdir('kwaak')
740
        transport = transport.clone('kwaak')
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
741
        branch = self.make_remote_branch(transport, client)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
742
        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.
743
        self.assertEqual((2, revid), result)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
744
745
4053.1.2 by Robert Collins
Actually make this branch work.
746
class TestBranch_get_stacked_on_url(TestRemote):
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
747
    """Test Branch._get_stacked_on_url rpc"""
748
3691.2.10 by Martin Pool
Update more test_remote tests
749
    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.
750
        # test that asking for a stacked on url the server can't access works.
751
        # This isn't perfect, but then as we're in the same process there
752
        # really isn't anything we can do to be 100% sure that the server
753
        # doesn't just open in - this test probably needs to be rewritten using
754
        # a spawn()ed server.
755
        stacked_branch = self.make_branch('stacked', format='1.9')
756
        memory_branch = self.make_branch('base', format='1.9')
757
        vfs_url = self.get_vfs_only_url('base')
758
        stacked_branch.set_stacked_on_url(vfs_url)
759
        transport = stacked_branch.bzrdir.root_transport
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
760
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
761
        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.
762
            'Branch.get_stacked_on_url', ('stacked/',),
763
            'success', ('ok', vfs_url))
764
        # XXX: Multiple calls are bad, this second call documents what is
765
        # today.
766
        client.add_expected_call(
767
            'Branch.get_stacked_on_url', ('stacked/',),
768
            'success', ('ok', vfs_url))
769
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
770
            _client=client)
771
        branch = RemoteBranch(bzrdir, RemoteRepository(bzrdir, None),
772
            _client=client)
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
773
        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.
774
        self.assertEqual(vfs_url, result)
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
775
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
776
    def test_backwards_compatible(self):
777
        # like with bzr1.6 with no Branch.get_stacked_on_url rpc
778
        base_branch = self.make_branch('base', format='1.6')
779
        stacked_branch = self.make_branch('stacked', format='1.6')
780
        stacked_branch.set_stacked_on_url('../base')
781
        client = FakeClient(self.get_url())
782
        client.add_expected_call(
783
            'BzrDir.open_branch', ('stacked/',),
784
            'success', ('ok', ''))
785
        client.add_expected_call(
4053.1.2 by Robert Collins
Actually make this branch work.
786
            'BzrDir.find_repositoryV3', ('stacked/',),
787
            'success', ('ok', '', 'no', 'no', 'no',
788
                stacked_branch.repository._format.network_name()))
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
789
        # called twice, once from constructor and then again by us
790
        client.add_expected_call(
791
            'Branch.get_stacked_on_url', ('stacked/',),
792
            'unknown', ('Branch.get_stacked_on_url',))
793
        client.add_expected_call(
794
            'Branch.get_stacked_on_url', ('stacked/',),
795
            'unknown', ('Branch.get_stacked_on_url',))
796
        # this will also do vfs access, but that goes direct to the transport
797
        # 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.
798
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
799
            remote.RemoteBzrDirFormat(), _client=client)
3691.2.12 by Martin Pool
Add test for coping without Branch.get_stacked_on_url
800
        branch = bzrdir.open_branch()
801
        result = branch.get_stacked_on_url()
802
        self.assertEqual('../base', result)
803
        client.finished_test()
804
        # it's in the fallback list both for the RemoteRepository and its vfs
805
        # repository
806
        self.assertEqual(1, len(branch.repository._fallback_repositories))
807
        self.assertEqual(1,
808
            len(branch.repository._real_repository._fallback_repositories))
809
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
810
    def test_get_stacked_on_real_branch(self):
811
        base_branch = self.make_branch('base', format='1.6')
812
        stacked_branch = self.make_branch('stacked', format='1.6')
813
        stacked_branch.set_stacked_on_url('../base')
4053.1.2 by Robert Collins
Actually make this branch work.
814
        reference_format = self.get_repo_format()
815
        network_name = reference_format.network_name()
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
816
        client = FakeClient(self.get_url())
817
        client.add_expected_call(
818
            'BzrDir.open_branch', ('stacked/',),
819
            'success', ('ok', ''))
820
        client.add_expected_call(
4053.1.2 by Robert Collins
Actually make this branch work.
821
            'BzrDir.find_repositoryV3', ('stacked/',),
822
            'success', ('ok', '', 'no', 'no', 'no', network_name))
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
823
        # called twice, once from constructor and then again by us
824
        client.add_expected_call(
825
            'Branch.get_stacked_on_url', ('stacked/',),
826
            'success', ('ok', '../base'))
827
        client.add_expected_call(
828
            'Branch.get_stacked_on_url', ('stacked/',),
829
            '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.
830
        bzrdir = RemoteBzrDir(self.get_transport('stacked'),
831
            remote.RemoteBzrDirFormat(), _client=client)
3691.2.11 by Martin Pool
More tests around RemoteBranch stacking.
832
        branch = bzrdir.open_branch()
833
        result = branch.get_stacked_on_url()
834
        self.assertEqual('../base', result)
835
        client.finished_test()
836
        # it's in the fallback list both for the RemoteRepository and its vfs
837
        # repository
838
        self.assertEqual(1, len(branch.repository._fallback_repositories))
839
        self.assertEqual(1,
840
            len(branch.repository._real_repository._fallback_repositories))
841
3691.2.5 by Martin Pool
Add Branch.get_stacked_on_url rpc and tests for same
842
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
843
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.
844
845
    def test_set_empty(self):
846
        # set_revision_history([]) is translated to calling
847
        # Branch.set_last_revision(path, '') on the wire.
3104.4.2 by Andrew Bennetts
All tests passing.
848
        transport = MemoryTransport()
849
        transport.mkdir('branch')
850
        transport = transport.clone('branch')
851
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
852
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
853
        client.add_expected_call(
854
            'Branch.get_stacked_on_url', ('branch/',),
855
            'error', ('NotStacked',))
856
        client.add_expected_call(
857
            'Branch.lock_write', ('branch/', '', ''),
858
            'success', ('ok', 'branch token', 'repo token'))
859
        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.
860
            'Branch.last_revision_info',
861
            ('branch/',),
862
            'success', ('ok', '0', 'null:'))
863
        client.add_expected_call(
3691.2.10 by Martin Pool
Update more test_remote tests
864
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'null:',),
865
            'success', ('ok',))
866
        client.add_expected_call(
867
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
868
            'success', ('ok',))
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
869
        branch = self.make_remote_branch(transport, client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
870
        # This is a hack to work around the problem that RemoteBranch currently
871
        # unnecessarily invokes _ensure_real upon a call to lock_write.
872
        branch._ensure_real = lambda: None
873
        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.
874
        result = branch.set_revision_history([])
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
875
        branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
876
        self.assertEqual(None, result)
3691.2.10 by Martin Pool
Update more test_remote tests
877
        client.finished_test()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
878
879
    def test_set_nonempty(self):
880
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
881
        # Branch.set_last_revision(path, rev-idN) on the wire.
3104.4.2 by Andrew Bennetts
All tests passing.
882
        transport = MemoryTransport()
883
        transport.mkdir('branch')
884
        transport = transport.clone('branch')
885
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
886
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
887
        client.add_expected_call(
888
            'Branch.get_stacked_on_url', ('branch/',),
889
            'error', ('NotStacked',))
890
        client.add_expected_call(
891
            'Branch.lock_write', ('branch/', '', ''),
892
            'success', ('ok', 'branch token', 'repo token'))
893
        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.
894
            'Branch.last_revision_info',
895
            ('branch/',),
896
            'success', ('ok', '0', 'null:'))
897
        lines = ['rev-id2']
898
        encoded_body = bz2.compress('\n'.join(lines))
899
        client.add_success_response_with_body(encoded_body, 'ok')
900
        client.add_expected_call(
3691.2.10 by Martin Pool
Update more test_remote tests
901
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id2',),
902
            'success', ('ok',))
903
        client.add_expected_call(
904
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
905
            'success', ('ok',))
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
906
        branch = self.make_remote_branch(transport, client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
907
        # This is a hack to work around the problem that RemoteBranch currently
908
        # unnecessarily invokes _ensure_real upon a call to lock_write.
909
        branch._ensure_real = lambda: None
910
        # Lock the branch, reset the record of remote calls.
911
        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.
912
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
913
        branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
914
        self.assertEqual(None, result)
3691.2.10 by Martin Pool
Update more test_remote tests
915
        client.finished_test()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
916
917
    def test_no_such_revision(self):
918
        transport = MemoryTransport()
919
        transport.mkdir('branch')
920
        transport = transport.clone('branch')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
921
        # A response of 'NoSuchRevision' is translated into an exception.
922
        client = FakeClient(transport.base)
3691.2.9 by Martin Pool
Convert and update more test_remote tests
923
        client.add_expected_call(
924
            'Branch.get_stacked_on_url', ('branch/',),
925
            'error', ('NotStacked',))
926
        client.add_expected_call(
927
            'Branch.lock_write', ('branch/', '', ''),
928
            'success', ('ok', 'branch token', 'repo token'))
929
        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.
930
            'Branch.last_revision_info',
931
            ('branch/',),
932
            'success', ('ok', '0', 'null:'))
933
        # get_graph calls to construct the revision history, for the set_rh
934
        # hook
935
        lines = ['rev-id']
936
        encoded_body = bz2.compress('\n'.join(lines))
937
        client.add_success_response_with_body(encoded_body, 'ok')
938
        client.add_expected_call(
3691.2.9 by Martin Pool
Convert and update more test_remote tests
939
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id',),
940
            'error', ('NoSuchRevision', 'rev-id'))
941
        client.add_expected_call(
942
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
943
            'success', ('ok',))
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
944
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
945
        branch = self.make_remote_branch(transport, client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
946
        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.
947
        self.assertRaises(
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
948
            errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
949
        branch.unlock()
3691.2.9 by Martin Pool
Convert and update more test_remote tests
950
        client.finished_test()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
951
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
952
    def test_tip_change_rejected(self):
953
        """TipChangeRejected responses cause a TipChangeRejected exception to
954
        be raised.
955
        """
956
        transport = MemoryTransport()
957
        transport.mkdir('branch')
958
        transport = transport.clone('branch')
959
        client = FakeClient(transport.base)
960
        rejection_msg_unicode = u'rejection message\N{INTERROBANG}'
961
        rejection_msg_utf8 = rejection_msg_unicode.encode('utf8')
3691.2.10 by Martin Pool
Update more test_remote tests
962
        client.add_expected_call(
963
            'Branch.get_stacked_on_url', ('branch/',),
964
            'error', ('NotStacked',))
965
        client.add_expected_call(
966
            'Branch.lock_write', ('branch/', '', ''),
967
            'success', ('ok', 'branch token', 'repo token'))
968
        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.
969
            'Branch.last_revision_info',
970
            ('branch/',),
971
            'success', ('ok', '0', 'null:'))
972
        lines = ['rev-id']
973
        encoded_body = bz2.compress('\n'.join(lines))
974
        client.add_success_response_with_body(encoded_body, 'ok')
975
        client.add_expected_call(
3691.2.10 by Martin Pool
Update more test_remote tests
976
            'Branch.set_last_revision', ('branch/', 'branch token', 'repo token', 'rev-id',),
977
            'error', ('TipChangeRejected', rejection_msg_utf8))
978
        client.add_expected_call(
979
            'Branch.unlock', ('branch/', 'branch token', 'repo token'),
980
            'success', ('ok',))
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
981
        branch = self.make_remote_branch(transport, client)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
982
        branch._ensure_real = lambda: None
983
        branch.lock_write()
984
        # The 'TipChangeRejected' error response triggered by calling
985
        # set_revision_history causes a TipChangeRejected exception.
986
        err = self.assertRaises(
987
            errors.TipChangeRejected, branch.set_revision_history, ['rev-id'])
988
        # The UTF-8 message from the response has been decoded into a unicode
989
        # object.
990
        self.assertIsInstance(err.msg, unicode)
991
        self.assertEqual(rejection_msg_unicode, err.msg)
3691.2.10 by Martin Pool
Update more test_remote tests
992
        branch.unlock()
993
        client.finished_test()
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
994
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
995
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
996
class TestBranchSetLastRevisionInfo(RemoteBranchTestCase):
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
997
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
998
    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.
999
        # set_last_revision_info(num, 'rev-id') is translated to calling
1000
        # 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'.
1001
        transport = MemoryTransport()
1002
        transport.mkdir('branch')
1003
        transport = transport.clone('branch')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1004
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1005
        # get_stacked_on_url
1006
        client.add_error_response('NotStacked')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1007
        # lock_write
1008
        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.
1009
        # query the current revision
1010
        client.add_success_response('ok', '0', 'null:')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1011
        # set_last_revision
1012
        client.add_success_response('ok')
1013
        # unlock
1014
        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.
1015
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1016
        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.
1017
        # Lock the branch, reset the record of remote calls.
1018
        branch.lock_write()
1019
        client._calls = []
1020
        result = branch.set_last_revision_info(1234, 'a-revision-id')
1021
        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.
1022
            [('call', 'Branch.last_revision_info', ('branch/',)),
1023
             ('call', 'Branch.set_last_revision_info',
3297.4.1 by Andrew Bennetts
Merge 'Add Branch.set_last_revision_info smart method'.
1024
                ('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.
1025
                 '1234', 'a-revision-id'))],
1026
            client._calls)
1027
        self.assertEqual(None, result)
1028
1029
    def test_no_such_revision(self):
1030
        # A response of 'NoSuchRevision' is translated into an exception.
1031
        transport = MemoryTransport()
1032
        transport.mkdir('branch')
1033
        transport = transport.clone('branch')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1034
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1035
        # get_stacked_on_url
1036
        client.add_error_response('NotStacked')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1037
        # lock_write
1038
        client.add_success_response('ok', 'branch token', 'repo token')
1039
        # set_last_revision
1040
        client.add_error_response('NoSuchRevision', 'revid')
1041
        # unlock
1042
        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.
1043
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1044
        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.
1045
        # Lock the branch, reset the record of remote calls.
1046
        branch.lock_write()
1047
        client._calls = []
1048
1049
        self.assertRaises(
1050
            errors.NoSuchRevision, branch.set_last_revision_info, 123, 'revid')
1051
        branch.unlock()
1052
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1053
    def lock_remote_branch(self, branch):
1054
        """Trick a RemoteBranch into thinking it is locked."""
1055
        branch._lock_mode = 'w'
1056
        branch._lock_count = 2
1057
        branch._lock_token = 'branch token'
1058
        branch._repo_lock_token = 'repo token'
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1059
        branch.repository._lock_mode = 'w'
1060
        branch.repository._lock_count = 2
1061
        branch.repository._lock_token = 'repo token'
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1062
1063
    def test_backwards_compatibility(self):
1064
        """If the server does not support the Branch.set_last_revision_info
1065
        verb (which is new in 1.4), then the client falls back to VFS methods.
1066
        """
1067
        # This test is a little messy.  Unlike most tests in this file, it
1068
        # doesn't purely test what a Remote* object sends over the wire, and
1069
        # how it reacts to responses from the wire.  It instead relies partly
1070
        # on asserting that the RemoteBranch will call
1071
        # self._real_branch.set_last_revision_info(...).
1072
1073
        # First, set up our RemoteBranch with a FakeClient that raises
1074
        # UnknownSmartMethod, and a StubRealBranch that logs how it is called.
1075
        transport = MemoryTransport()
1076
        transport.mkdir('branch')
1077
        transport = transport.clone('branch')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1078
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1079
        client.add_expected_call(
1080
            'Branch.get_stacked_on_url', ('branch/',),
1081
            'error', ('NotStacked',))
1082
        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.
1083
            'Branch.last_revision_info',
1084
            ('branch/',),
1085
            'success', ('ok', '0', 'null:'))
1086
        client.add_expected_call(
3691.2.10 by Martin Pool
Update more test_remote tests
1087
            'Branch.set_last_revision_info',
1088
            ('branch/', 'branch token', 'repo token', '1234', 'a-revision-id',),
1089
            'unknown', 'Branch.set_last_revision_info')
1090
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1091
        branch = self.make_remote_branch(transport, client)
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1092
        class StubRealBranch(object):
1093
            def __init__(self):
1094
                self.calls = []
1095
            def set_last_revision_info(self, revno, revision_id):
1096
                self.calls.append(
1097
                    ('set_last_revision_info', revno, revision_id))
3441.5.5 by Andrew Bennetts
Some small tweaks and comments.
1098
            def _clear_cached_state(self):
1099
                pass
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1100
        real_branch = StubRealBranch()
1101
        branch._real_branch = real_branch
1102
        self.lock_remote_branch(branch)
1103
1104
        # Call set_last_revision_info, and verify it behaved as expected.
1105
        result = branch.set_last_revision_info(1234, 'a-revision-id')
1106
        self.assertEqual(
1107
            [('set_last_revision_info', 1234, 'a-revision-id')],
1108
            real_branch.calls)
3691.2.10 by Martin Pool
Update more test_remote tests
1109
        client.finished_test()
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
1110
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1111
    def test_unexpected_error(self):
3697.2.6 by Martin Pool
Merge 261315 fix into 1.7 branch
1112
        # If the server sends an error the client doesn't understand, it gets
1113
        # turned into an UnknownErrorFromSmartServer, which is presented as a
1114
        # non-internal error to the user.
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1115
        transport = MemoryTransport()
1116
        transport.mkdir('branch')
1117
        transport = transport.clone('branch')
1118
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1119
        # get_stacked_on_url
1120
        client.add_error_response('NotStacked')
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1121
        # lock_write
1122
        client.add_success_response('ok', 'branch token', 'repo token')
1123
        # set_last_revision
1124
        client.add_error_response('UnexpectedError')
1125
        # unlock
1126
        client.add_success_response('ok')
1127
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1128
        branch = self.make_remote_branch(transport, client)
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1129
        # Lock the branch, reset the record of remote calls.
1130
        branch.lock_write()
1131
        client._calls = []
1132
1133
        err = self.assertRaises(
3690.1.2 by Andrew Bennetts
Rename UntranslateableErrorFromSmartServer -> UnknownErrorFromSmartServer.
1134
            errors.UnknownErrorFromSmartServer,
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1135
            branch.set_last_revision_info, 123, 'revid')
1136
        self.assertEqual(('UnexpectedError',), err.error_tuple)
1137
        branch.unlock()
1138
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1139
    def test_tip_change_rejected(self):
1140
        """TipChangeRejected responses cause a TipChangeRejected exception to
1141
        be raised.
1142
        """
1143
        transport = MemoryTransport()
1144
        transport.mkdir('branch')
1145
        transport = transport.clone('branch')
1146
        client = FakeClient(transport.base)
3691.2.10 by Martin Pool
Update more test_remote tests
1147
        # get_stacked_on_url
1148
        client.add_error_response('NotStacked')
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1149
        # lock_write
1150
        client.add_success_response('ok', 'branch token', 'repo token')
1151
        # set_last_revision
1152
        client.add_error_response('TipChangeRejected', 'rejection message')
1153
        # unlock
1154
        client.add_success_response('ok')
1155
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1156
        branch = self.make_remote_branch(transport, client)
3577.1.1 by Andrew Bennetts
Cherry-pick TipChangeRejected changes from pre-branch-tip-changed-hook loom.
1157
        # Lock the branch, reset the record of remote calls.
1158
        branch.lock_write()
1159
        self.addCleanup(branch.unlock)
1160
        client._calls = []
1161
1162
        # The 'TipChangeRejected' error response triggered by calling
1163
        # set_last_revision_info causes a TipChangeRejected exception.
1164
        err = self.assertRaises(
1165
            errors.TipChangeRejected,
1166
            branch.set_last_revision_info, 123, 'revid')
1167
        self.assertEqual('rejection message', err.msg)
1168
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1169
2018.5.169 by Andrew Bennetts
Add a _server_formats flag to BzrDir.open_from_transport and BzrDirFormat.find_format, make RemoteBranch.control_files into a property.
1170
class TestBranchControlGetBranchConf(tests.TestCaseWithMemoryTransport):
3408.3.1 by Martin Pool
Remove erroneous handling of branch.conf for RemoteBranch
1171
    """Getting the branch configuration should use an abstract method not vfs.
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1172
    """
1173
1174
    def test_get_branch_conf(self):
3408.3.1 by Martin Pool
Remove erroneous handling of branch.conf for RemoteBranch
1175
        raise tests.KnownFailure('branch.conf is not retrieved by get_config_file')
3407.2.10 by Martin Pool
Merge trunk
1176
        ## # We should see that branch.get_config() does a single rpc to get the
1177
        ## # remote configuration file, abstracting away where that is stored on
1178
        ## # the server.  However at the moment it always falls back to using the
1179
        ## # vfs, and this would need some changes in config.py.
3408.3.1 by Martin Pool
Remove erroneous handling of branch.conf for RemoteBranch
1180
3407.2.10 by Martin Pool
Merge trunk
1181
        ## # in an empty branch we decode the response properly
1182
        ## client = FakeClient([(('ok', ), '# config file body')], self.get_url())
1183
        ## # we need to make a real branch because the remote_branch.control_files
1184
        ## # will trigger _ensure_real.
1185
        ## branch = self.make_branch('quack')
1186
        ## transport = branch.bzrdir.root_transport
1187
        ## # we do not want bzrdir to make any remote calls
1188
        ## bzrdir = RemoteBzrDir(transport, _client=False)
1189
        ## branch = RemoteBranch(bzrdir, None, _client=client)
1190
        ## config = branch.get_config()
1191
        ## self.assertEqual(
1192
        ##     [('call_expecting_body', 'Branch.get_config_file', ('quack/',))],
1193
        ##     client._calls)
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1194
1195
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1196
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.
1197
1198
    def test_lock_write_unlockable(self):
1199
        transport = MemoryTransport()
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1200
        client = FakeClient(transport.base)
3691.2.9 by Martin Pool
Convert and update more test_remote tests
1201
        client.add_expected_call(
1202
            'Branch.get_stacked_on_url', ('quack/',),
1203
            'error', ('NotStacked',),)
1204
        client.add_expected_call(
1205
            'Branch.lock_write', ('quack/', '', ''),
1206
            '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.
1207
        transport.mkdir('quack')
1208
        transport = transport.clone('quack')
3692.1.1 by Andrew Bennetts
Make RemoteBranch.lock_write lock the repository too.
1209
        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.
1210
        self.assertRaises(errors.UnlockableTransport, branch.lock_write)
3691.2.9 by Martin Pool
Convert and update more test_remote tests
1211
        client.finished_test()
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.
1212
1213
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
1214
class TestTransportIsReadonly(tests.TestCase):
1215
1216
    def test_true(self):
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1217
        client = FakeClient()
1218
        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.
1219
        transport = RemoteTransport('bzr://example.com/', medium=False,
1220
                                    _client=client)
1221
        self.assertEqual(True, transport.is_readonly())
1222
        self.assertEqual(
1223
            [('call', 'Transport.is_readonly', ())],
1224
            client._calls)
1225
1226
    def test_false(self):
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1227
        client = FakeClient()
1228
        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.
1229
        transport = RemoteTransport('bzr://example.com/', medium=False,
1230
                                    _client=client)
1231
        self.assertEqual(False, transport.is_readonly())
1232
        self.assertEqual(
1233
            [('call', 'Transport.is_readonly', ())],
1234
            client._calls)
1235
1236
    def test_error_from_old_server(self):
1237
        """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
1238
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
1239
        Clients should treat it as a "no" response, because is_readonly is only
1240
        advisory anyway (a transport could be read-write, but then the
1241
        underlying filesystem could be readonly anyway).
1242
        """
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1243
        client = FakeClient()
1244
        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.
1245
        transport = RemoteTransport('bzr://example.com/', medium=False,
1246
                                    _client=client)
1247
        self.assertEqual(False, transport.is_readonly())
1248
        self.assertEqual(
1249
            [('call', 'Transport.is_readonly', ())],
1250
            client._calls)
1251
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
1252
3840.1.1 by Andrew Bennetts
Fix RemoteTransport's translation of errors involving paths; it wasn't passing orig_path to _translate_error.
1253
class TestTransportMkdir(tests.TestCase):
1254
1255
    def test_permissiondenied(self):
1256
        client = FakeClient()
1257
        client.add_error_response('PermissionDenied', 'remote path', 'extra')
1258
        transport = RemoteTransport('bzr://example.com/', medium=False,
1259
                                    _client=client)
1260
        exc = self.assertRaises(
1261
            errors.PermissionDenied, transport.mkdir, 'client path')
1262
        expected_error = errors.PermissionDenied('/client path', 'extra')
1263
        self.assertEqual(expected_error, exc)
1264
1265
3777.1.3 by Aaron Bentley
Use SSH default username from authentication.conf
1266
class TestRemoteSSHTransportAuthentication(tests.TestCaseInTempDir):
1267
1268
    def test_defaults_to_none(self):
1269
        t = RemoteSSHTransport('bzr+ssh://example.com')
1270
        self.assertIs(None, t._get_credentials()[0])
1271
1272
    def test_uses_authentication_config(self):
1273
        conf = config.AuthenticationConfig()
1274
        conf._get_config().update(
1275
            {'bzr+sshtest': {'scheme': 'ssh', 'user': 'bar', 'host':
1276
            'example.com'}})
1277
        conf._save()
1278
        t = RemoteSSHTransport('bzr+ssh://example.com')
1279
        self.assertEqual('bar', t._get_credentials()[0])
1280
1281
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
1282
class TestRemoteRepository(TestRemote):
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1283
    """Base for testing RemoteRepository protocol usage.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1284
1285
    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
1286
    what is sent or expected to be require a thoughtful update to these tests
1287
    because they might break compatibility with different-versioned servers.
1288
    """
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1289
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1290
    def setup_fake_client_and_repository(self, transport_path):
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1291
        """Create the fake client and repository for testing with.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1292
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1293
        There's no real server here; we just have canned responses sent
1294
        back one by one.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1295
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1296
        :param transport_path: Path below the root of the MemoryTransport
1297
            where the repository will be created.
1298
        """
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1299
        transport = MemoryTransport()
1300
        transport.mkdir(transport_path)
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1301
        client = FakeClient(transport.base)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1302
        transport = transport.clone(transport_path)
1303
        # we do not want bzrdir to make any remote calls
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.
1304
        bzrdir = RemoteBzrDir(transport, remote.RemoteBzrDirFormat(),
1305
            _client=False)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1306
        repo = RemoteRepository(bzrdir, None, _client=client)
1307
        return repo, client
1308
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1309
2018.12.2 by Andrew Bennetts
Remove some duplicate code in test_remote
1310
class TestRepositoryGatherStats(TestRemoteRepository):
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
1311
1312
    def test_revid_none(self):
1313
        # ('ok',), body with revisions and size
1314
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1315
        repo, client = self.setup_fake_client_and_repository(transport_path)
1316
        client.add_success_response_with_body(
1317
            'revisions: 2\nsize: 18\n', 'ok')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
1318
        result = repo.gather_stats(None)
1319
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
1320
            [('call_expecting_body', 'Repository.gather_stats',
3104.4.2 by Andrew Bennetts
All tests passing.
1321
             ('quack/','','no'))],
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
1322
            client._calls)
1323
        self.assertEqual({'revisions': 2, 'size': 18}, result)
1324
1325
    def test_revid_no_committers(self):
1326
        # ('ok',), body without committers
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1327
        body = ('firstrev: 123456.300 3600\n'
1328
                'latestrev: 654231.400 0\n'
1329
                'revisions: 2\n'
1330
                'size: 18\n')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
1331
        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.
1332
        revid = u'\xc8'.encode('utf8')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1333
        repo, client = self.setup_fake_client_and_repository(transport_path)
1334
        client.add_success_response_with_body(body, 'ok')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
1335
        result = repo.gather_stats(revid)
1336
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
1337
            [('call_expecting_body', 'Repository.gather_stats',
3104.4.2 by Andrew Bennetts
All tests passing.
1338
              ('quick/', revid, 'no'))],
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
1339
            client._calls)
1340
        self.assertEqual({'revisions': 2, 'size': 18,
1341
                          'firstrev': (123456.300, 3600),
1342
                          'latestrev': (654231.400, 0),},
1343
                         result)
1344
1345
    def test_revid_with_committers(self):
1346
        # ('ok',), body with committers
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1347
        body = ('committers: 128\n'
1348
                'firstrev: 123456.300 3600\n'
1349
                'latestrev: 654231.400 0\n'
1350
                'revisions: 2\n'
1351
                'size: 18\n')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
1352
        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.
1353
        revid = u'\xc8'.encode('utf8')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1354
        repo, client = self.setup_fake_client_and_repository(transport_path)
1355
        client.add_success_response_with_body(body, 'ok')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
1356
        result = repo.gather_stats(revid, True)
1357
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
1358
            [('call_expecting_body', 'Repository.gather_stats',
3104.4.2 by Andrew Bennetts
All tests passing.
1359
              ('buick/', revid, 'yes'))],
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
1360
            client._calls)
1361
        self.assertEqual({'revisions': 2, 'size': 18,
1362
                          'committers': 128,
1363
                          'firstrev': (123456.300, 3600),
1364
                          'latestrev': (654231.400, 0),},
1365
                         result)
1366
1367
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
1368
class TestRepositoryGetGraph(TestRemoteRepository):
1369
1370
    def test_get_graph(self):
3835.1.6 by Aaron Bentley
Reduce inefficiency when doing make_parents_provider frequently
1371
        # 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.
1372
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1373
        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.
1374
        graph = repo.get_graph()
3835.1.6 by Aaron Bentley
Reduce inefficiency when doing make_parents_provider frequently
1375
        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.
1376
1377
1378
class TestRepositoryGetParentMap(TestRemoteRepository):
1379
1380
    def test_get_parent_map_caching(self):
1381
        # get_parent_map returns from cache until unlock()
1382
        # setup a reponse with two revisions
1383
        r1 = u'\u0e33'.encode('utf8')
1384
        r2 = u'\u0dab'.encode('utf8')
1385
        lines = [' '.join([r2, r1]), r1]
3211.5.2 by Robert Collins
Change RemoteRepository.get_parent_map to use bz2 not gzip for compression.
1386
        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.
1387
1388
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1389
        repo, client = self.setup_fake_client_and_repository(transport_path)
1390
        client.add_success_response_with_body(encoded_body, 'ok')
1391
        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.
1392
        repo.lock_read()
1393
        graph = repo.get_graph()
1394
        parents = graph.get_parent_map([r2])
1395
        self.assertEqual({r2: (r1,)}, parents)
1396
        # locking and unlocking deeper should not reset
1397
        repo.lock_read()
1398
        repo.unlock()
1399
        parents = graph.get_parent_map([r1])
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
1400
        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.
1401
        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.
1402
            [('call_with_body_bytes_expecting_body',
1403
              'Repository.get_parent_map', ('quack/', r2), '\n\n0')],
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
1404
            client._calls)
1405
        repo.unlock()
1406
        # now we call again, and it should use the second response.
1407
        repo.lock_read()
1408
        graph = repo.get_graph()
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
1409
        parents = graph.get_parent_map([r1])
1410
        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.
1411
        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.
1412
            [('call_with_body_bytes_expecting_body',
1413
              'Repository.get_parent_map', ('quack/', r2), '\n\n0'),
1414
             ('call_with_body_bytes_expecting_body',
1415
              'Repository.get_parent_map', ('quack/', r1), '\n\n0'),
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
1416
            ],
1417
            client._calls)
1418
        repo.unlock()
1419
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
1420
    def test_get_parent_map_reconnects_if_unknown_method(self):
1421
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1422
        repo, client = self.setup_fake_client_and_repository(transport_path)
3842.3.20 by Andrew Bennetts
Re-revert changes from another thread that accidentally got reinstated here.
1423
        client.add_unknown_method_response('Repository,get_parent_map')
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1424
        client.add_success_response_with_body('', 'ok')
3453.4.10 by Andrew Bennetts
Change _is_remote_at_least to _is_remote_before.
1425
        self.assertFalse(client._medium._is_remote_before((1, 2)))
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
1426
        rev_id = 'revision-id'
3297.3.5 by Andrew Bennetts
Suppress a deprecation warning.
1427
        expected_deprecations = [
1428
            'bzrlib.remote.RemoteRepository.get_revision_graph was deprecated '
1429
            'in version 1.4.']
1430
        parents = self.callDeprecated(
1431
            expected_deprecations, 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.
1432
        self.assertEqual(
3213.1.8 by Andrew Bennetts
Merge from bzr.dev.
1433
            [('call_with_body_bytes_expecting_body',
1434
              'Repository.get_parent_map', ('quack/', rev_id), '\n\n0'),
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
1435
             ('disconnect medium',),
1436
             ('call_expecting_body', 'Repository.get_revision_graph',
1437
              ('quack/', ''))],
1438
            client._calls)
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
1439
        # 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.
1440
        self.assertTrue(client._medium._is_remote_before((1, 2)))
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
1441
1442
    def test_get_parent_map_fallback_parentless_node(self):
1443
        """get_parent_map falls back to get_revision_graph on old servers.  The
1444
        results from get_revision_graph are tweaked to match the get_parent_map
1445
        API.
1446
3389.1.3 by Andrew Bennetts
Remove XXX from test description.
1447
        Specifically, a {key: ()} result from get_revision_graph means "no
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
1448
        parents" for that key, which in get_parent_map results should be
3389.1.3 by Andrew Bennetts
Remove XXX from test description.
1449
        represented as {key: ('null:',)}.
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
1450
1451
        This is the test for https://bugs.launchpad.net/bzr/+bug/214894
1452
        """
1453
        rev_id = 'revision-id'
1454
        transport_path = 'quack'
3245.4.40 by Andrew Bennetts
Merge from bzr.dev.
1455
        repo, client = self.setup_fake_client_and_repository(transport_path)
1456
        client.add_success_response_with_body(rev_id, 'ok')
3453.4.9 by Andrew Bennetts
Rename _remote_is_not to _remember_remote_is_before.
1457
        client._medium._remember_remote_is_before((1, 2))
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
1458
        expected_deprecations = [
1459
            'bzrlib.remote.RemoteRepository.get_revision_graph was deprecated '
1460
            'in version 1.4.']
1461
        parents = self.callDeprecated(
1462
            expected_deprecations, repo.get_parent_map, [rev_id])
1463
        self.assertEqual(
1464
            [('call_expecting_body', 'Repository.get_revision_graph',
1465
             ('quack/', ''))],
1466
            client._calls)
1467
        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.
1468
3297.2.3 by Andrew Bennetts
Test the code path that the typo is on.
1469
    def test_get_parent_map_unexpected_response(self):
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1470
        repo, client = self.setup_fake_client_and_repository('path')
1471
        client.add_success_response('something unexpected!')
3297.2.3 by Andrew Bennetts
Test the code path that the typo is on.
1472
        self.assertRaises(
1473
            errors.UnexpectedSmartServerResponse,
1474
            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.
1475
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
1476
3835.1.15 by Aaron Bentley
Allow miss caching to be disabled.
1477
class TestGetParentMapAllowsNew(tests.TestCaseWithTransport):
1478
1479
    def test_allows_new_revisions(self):
1480
        """get_parent_map's results can be updated by commit."""
1481
        smart_server = server.SmartTCPServer_for_testing()
1482
        smart_server.setUp()
1483
        self.addCleanup(smart_server.tearDown)
1484
        self.make_branch('branch')
1485
        branch = Branch.open(smart_server.get_url() + '/branch')
1486
        tree = branch.create_checkout('tree', lightweight=True)
1487
        tree.lock_write()
1488
        self.addCleanup(tree.unlock)
1489
        graph = tree.branch.repository.get_graph()
1490
        # This provides an opportunity for the missing rev-id to be cached.
1491
        self.assertEqual({}, graph.get_parent_map(['rev1']))
1492
        tree.commit('message', rev_id='rev1')
1493
        graph = tree.branch.repository.get_graph()
1494
        self.assertEqual({'rev1': ('null:',)}, graph.get_parent_map(['rev1']))
1495
1496
2018.5.68 by Wouter van Heyst
Merge RemoteRepository.gather_stats.
1497
class TestRepositoryGetRevisionGraph(TestRemoteRepository):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1498
2018.5.68 by Wouter van Heyst
Merge RemoteRepository.gather_stats.
1499
    def test_null_revision(self):
1500
        # a null revision has the predictable result {}, we should have no wire
1501
        # traffic when calling it with this argument
1502
        transport_path = 'empty'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1503
        repo, client = self.setup_fake_client_and_repository(transport_path)
1504
        client.add_success_response('notused')
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
1505
        result = self.applyDeprecated(one_four, repo.get_revision_graph,
1506
            NULL_REVISION)
2018.5.68 by Wouter van Heyst
Merge RemoteRepository.gather_stats.
1507
        self.assertEqual([], client._calls)
1508
        self.assertEqual({}, result)
1509
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1510
    def test_none_revision(self):
1511
        # with none we want the entire graph
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
1512
        r1 = u'\u0e33'.encode('utf8')
1513
        r2 = u'\u0dab'.encode('utf8')
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1514
        lines = [' '.join([r2, r1]), r1]
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
1515
        encoded_body = '\n'.join(lines)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1516
1517
        transport_path = 'sinhala'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1518
        repo, client = self.setup_fake_client_and_repository(transport_path)
1519
        client.add_success_response_with_body(encoded_body, 'ok')
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
1520
        result = self.applyDeprecated(one_four, repo.get_revision_graph)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1521
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
1522
            [('call_expecting_body', 'Repository.get_revision_graph',
3104.4.2 by Andrew Bennetts
All tests passing.
1523
             ('sinhala/', ''))],
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1524
            client._calls)
2625.8.1 by Robert Collins
LIBRARY API BREAKS:
1525
        self.assertEqual({r1: (), r2: (r1, )}, result)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1526
1527
    def test_specific_revision(self):
1528
        # with a specific revision we want the graph for that
1529
        # with none we want the entire graph
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
1530
        r11 = u'\u0e33'.encode('utf8')
1531
        r12 = u'\xc9'.encode('utf8')
1532
        r2 = u'\u0dab'.encode('utf8')
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1533
        lines = [' '.join([r2, r11, r12]), r11, r12]
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
1534
        encoded_body = '\n'.join(lines)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1535
1536
        transport_path = 'sinhala'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1537
        repo, client = self.setup_fake_client_and_repository(transport_path)
1538
        client.add_success_response_with_body(encoded_body, 'ok')
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
1539
        result = self.applyDeprecated(one_four, repo.get_revision_graph, r2)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1540
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
1541
            [('call_expecting_body', 'Repository.get_revision_graph',
3104.4.2 by Andrew Bennetts
All tests passing.
1542
             ('sinhala/', r2))],
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1543
            client._calls)
2625.8.1 by Robert Collins
LIBRARY API BREAKS:
1544
        self.assertEqual({r11: (), r12: (), r2: (r11, r12), }, result)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1545
1546
    def test_no_such_revision(self):
1547
        revid = '123'
1548
        transport_path = 'sinhala'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1549
        repo, client = self.setup_fake_client_and_repository(transport_path)
1550
        client.add_error_response('nosuchrevision', revid)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1551
        # also check that the right revision is reported in the error
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1552
        self.assertRaises(errors.NoSuchRevision,
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
1553
            self.applyDeprecated, one_four, repo.get_revision_graph, revid)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1554
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
1555
            [('call_expecting_body', 'Repository.get_revision_graph',
3104.4.2 by Andrew Bennetts
All tests passing.
1556
             ('sinhala/', revid))],
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1557
            client._calls)
1558
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1559
    def test_unexpected_error(self):
1560
        revid = '123'
1561
        transport_path = 'sinhala'
1562
        repo, client = self.setup_fake_client_and_repository(transport_path)
1563
        client.add_error_response('AnUnexpectedError')
3690.1.2 by Andrew Bennetts
Rename UntranslateableErrorFromSmartServer -> UnknownErrorFromSmartServer.
1564
        e = self.assertRaises(errors.UnknownErrorFromSmartServer,
3245.4.53 by Andrew Bennetts
Add some missing 'raise' statements to test_remote.
1565
            self.applyDeprecated, one_four, repo.get_revision_graph, revid)
1566
        self.assertEqual(('AnUnexpectedError',), e.error_tuple)
1567
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1568
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1569
class TestRepositoryIsShared(TestRemoteRepository):
1570
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1571
    def test_is_shared(self):
1572
        # ('yes', ) for Repository.is_shared -> 'True'.
1573
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1574
        repo, client = self.setup_fake_client_and_repository(transport_path)
1575
        client.add_success_response('yes')
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1576
        result = repo.is_shared()
1577
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
1578
            [('call', 'Repository.is_shared', ('quack/',))],
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1579
            client._calls)
1580
        self.assertEqual(True, result)
1581
1582
    def test_is_not_shared(self):
1583
        # ('no', ) for Repository.is_shared -> 'False'.
1584
        transport_path = 'qwack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1585
        repo, client = self.setup_fake_client_and_repository(transport_path)
1586
        client.add_success_response('no')
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1587
        result = repo.is_shared()
1588
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
1589
            [('call', 'Repository.is_shared', ('qwack/',))],
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1590
            client._calls)
1591
        self.assertEqual(False, result)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1592
1593
1594
class TestRepositoryLockWrite(TestRemoteRepository):
1595
1596
    def test_lock_write(self):
1597
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1598
        repo, client = self.setup_fake_client_and_repository(transport_path)
1599
        client.add_success_response('ok', 'a token')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1600
        result = repo.lock_write()
1601
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
1602
            [('call', 'Repository.lock_write', ('quack/', ''))],
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1603
            client._calls)
1604
        self.assertEqual('a token', result)
1605
1606
    def test_lock_write_already_locked(self):
1607
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1608
        repo, client = self.setup_fake_client_and_repository(transport_path)
1609
        client.add_error_response('LockContention')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1610
        self.assertRaises(errors.LockContention, repo.lock_write)
1611
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
1612
            [('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.
1613
            client._calls)
1614
1615
    def test_lock_write_unlockable(self):
1616
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1617
        repo, client = self.setup_fake_client_and_repository(transport_path)
1618
        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.
1619
        self.assertRaises(errors.UnlockableTransport, repo.lock_write)
1620
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
1621
            [('call', 'Repository.lock_write', ('quack/', ''))],
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1622
            client._calls)
1623
1624
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
1625
class TestRepositorySetMakeWorkingTrees(TestRemoteRepository):
1626
1627
    def test_backwards_compat(self):
1628
        self.setup_smart_server_with_call_log()
1629
        repo = self.make_repository('.')
1630
        self.reset_smart_call_log()
1631
        verb = 'Repository.set_make_working_trees'
1632
        self.disable_verb(verb)
1633
        repo.set_make_working_trees(True)
1634
        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.
1635
            call.call.method == verb])
4017.3.4 by Robert Collins
Create a verb for Repository.set_make_working_trees.
1636
        self.assertEqual(1, call_count)
1637
1638
    def test_current(self):
1639
        transport_path = 'quack'
1640
        repo, client = self.setup_fake_client_and_repository(transport_path)
1641
        client.add_expected_call(
1642
            'Repository.set_make_working_trees', ('quack/', 'True'),
1643
            'success', ('ok',))
1644
        client.add_expected_call(
1645
            'Repository.set_make_working_trees', ('quack/', 'False'),
1646
            'success', ('ok',))
1647
        repo.set_make_working_trees(True)
1648
        repo.set_make_working_trees(False)
1649
1650
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1651
class TestRepositoryUnlock(TestRemoteRepository):
1652
1653
    def test_unlock(self):
1654
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1655
        repo, client = self.setup_fake_client_and_repository(transport_path)
1656
        client.add_success_response('ok', 'a token')
1657
        client.add_success_response('ok')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1658
        repo.lock_write()
1659
        repo.unlock()
1660
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
1661
            [('call', 'Repository.lock_write', ('quack/', '')),
1662
             ('call', 'Repository.unlock', ('quack/', 'a token'))],
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1663
            client._calls)
1664
1665
    def test_unlock_wrong_token(self):
1666
        # If somehow the token is wrong, unlock will raise TokenMismatch.
1667
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1668
        repo, client = self.setup_fake_client_and_repository(transport_path)
1669
        client.add_success_response('ok', 'a token')
1670
        client.add_error_response('TokenMismatch')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1671
        repo.lock_write()
1672
        self.assertRaises(errors.TokenMismatch, repo.unlock)
1673
1674
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1675
class TestRepositoryHasRevision(TestRemoteRepository):
1676
1677
    def test_none(self):
1678
        # repo.has_revision(None) should not cause any traffic.
1679
        transport_path = 'quack'
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1680
        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.
1681
1682
        # The null revision is always there, so has_revision(None) == True.
3172.3.3 by Robert Collins
Missed one occurence of None -> NULL_REVISION.
1683
        self.assertEqual(True, repo.has_revision(NULL_REVISION))
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1684
1685
        # The remote repo shouldn't be accessed.
1686
        self.assertEqual([], client._calls)
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1687
1688
1689
class TestRepositoryTarball(TestRemoteRepository):
1690
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1691
    # This is a canned tarball reponse we can validate against
2018.18.18 by Martin Pool
reformat
1692
    tarball_content = (
2018.18.23 by Martin Pool
review cleanups
1693
        'QlpoOTFBWSZTWdGkj3wAAWF/k8aQACBIB//A9+8cIX/v33AACEAYABAECEACNz'
1694
        'JqsgJJFPTSnk1A3qh6mTQAAAANPUHkagkSTEkaA09QaNAAAGgAAAcwCYCZGAEY'
1695
        'mJhMJghpiaYBUkKammSHqNMZQ0NABkNAeo0AGneAevnlwQoGzEzNVzaYxp/1Uk'
1696
        'xXzA1CQX0BJMZZLcPBrluJir5SQyijWHYZ6ZUtVqqlYDdB2QoCwa9GyWwGYDMA'
1697
        'OQYhkpLt/OKFnnlT8E0PmO8+ZNSo2WWqeCzGB5fBXZ3IvV7uNJVE7DYnWj6qwB'
1698
        'k5DJDIrQ5OQHHIjkS9KqwG3mc3t+F1+iujb89ufyBNIKCgeZBWrl5cXxbMGoMs'
1699
        'c9JuUkg5YsiVcaZJurc6KLi6yKOkgCUOlIlOpOoXyrTJjK8ZgbklReDdwGmFgt'
1700
        'dkVsAIslSVCd4AtACSLbyhLHryfb14PKegrVDba+U8OL6KQtzdM5HLjAc8/p6n'
1701
        '0lgaWU8skgO7xupPTkyuwheSckejFLK5T4ZOo0Gda9viaIhpD1Qn7JqqlKAJqC'
1702
        'QplPKp2nqBWAfwBGaOwVrz3y1T+UZZNismXHsb2Jq18T+VaD9k4P8DqE3g70qV'
1703
        'JLurpnDI6VS5oqDDPVbtVjMxMxMg4rzQVipn2Bv1fVNK0iq3Gl0hhnnHKm/egy'
1704
        'nWQ7QH/F3JFOFCQ0aSPfA='
1705
        ).decode('base64')
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1706
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1707
    def test_repository_tarball(self):
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1708
        # Test that Repository.tarball generates the right operations
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1709
        transport_path = 'repo'
2018.18.14 by Martin Pool
merge hpss again; restore incorrectly removed RemoteRepository.break_lock
1710
        expected_calls = [('call_expecting_body', 'Repository.tarball',
3104.4.2 by Andrew Bennetts
All tests passing.
1711
                           ('repo/', 'bz2',),),
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1712
            ]
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1713
        repo, client = self.setup_fake_client_and_repository(transport_path)
1714
        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
1715
        # Now actually ask for the tarball
3245.4.24 by Andrew Bennetts
Consistently raise errors from the server as ErrorFromSmartServer exceptions.
1716
        tarball_file = repo._get_tarball('bz2')
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
1717
        try:
1718
            self.assertEqual(expected_calls, client._calls)
1719
            self.assertEqual(self.tarball_content, tarball_file.read())
1720
        finally:
1721
            tarball_file.close()
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1722
1723
1724
class TestRemoteRepositoryCopyContent(tests.TestCaseWithTransport):
1725
    """RemoteRepository.copy_content_into optimizations"""
1726
2018.18.10 by Martin Pool
copy_content_into from Remote repositories by using temporary directories on both ends.
1727
    def test_copy_content_remote_to_local(self):
1728
        self.transport_server = server.SmartTCPServer_for_testing
1729
        src_repo = self.make_repository('repo1')
1730
        src_repo = repository.Repository.open(self.get_url('repo1'))
1731
        # At the moment the tarball-based copy_content_into can't write back
1732
        # into a smart server.  It would be good if it could upload the
1733
        # tarball; once that works we'd have to create repositories of
1734
        # different formats. -- mbp 20070410
1735
        dest_url = self.get_vfs_only_url('repo2')
1736
        dest_bzrdir = BzrDir.create(dest_url)
1737
        dest_repo = dest_bzrdir.create_repository()
1738
        self.assertFalse(isinstance(dest_repo, RemoteRepository))
1739
        self.assertTrue(isinstance(src_repo, RemoteRepository))
1740
        src_repo.copy_content_into(dest_repo)
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1741
1742
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
1743
class _StubRealPackRepository(object):
1744
1745
    def __init__(self, calls):
1746
        self._pack_collection = _StubPackCollection(calls)
1747
1748
1749
class _StubPackCollection(object):
1750
1751
    def __init__(self, calls):
1752
        self.calls = calls
1753
1754
    def autopack(self):
1755
        self.calls.append(('pack collection autopack',))
1756
3801.1.13 by Andrew Bennetts
Revert returning of pack-names from the RPC.
1757
    def reload_pack_names(self):
1758
        self.calls.append(('pack collection reload_pack_names',))
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
1759
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1760
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
1761
class TestRemotePackRepositoryAutoPack(TestRemoteRepository):
1762
    """Tests for RemoteRepository.autopack implementation."""
1763
1764
    def test_ok(self):
1765
        """When the server returns 'ok' and there's no _real_repository, then
1766
        nothing else happens: the autopack method is done.
1767
        """
1768
        transport_path = 'quack'
1769
        repo, client = self.setup_fake_client_and_repository(transport_path)
1770
        client.add_expected_call(
3801.1.13 by Andrew Bennetts
Revert returning of pack-names from the RPC.
1771
            'PackRepository.autopack', ('quack/',), 'success', ('ok',))
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
1772
        repo.autopack()
1773
        client.finished_test()
1774
1775
    def test_ok_with_real_repo(self):
1776
        """When the server returns 'ok' and there is a _real_repository, then
1777
        the _real_repository's reload_pack_name's method will be called.
1778
        """
1779
        transport_path = 'quack'
1780
        repo, client = self.setup_fake_client_and_repository(transport_path)
1781
        client.add_expected_call(
1782
            'PackRepository.autopack', ('quack/',),
3801.1.13 by Andrew Bennetts
Revert returning of pack-names from the RPC.
1783
            'success', ('ok',))
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
1784
        repo._real_repository = _StubRealPackRepository(client._calls)
1785
        repo.autopack()
1786
        self.assertEqual(
1787
            [('call', 'PackRepository.autopack', ('quack/',)),
3801.1.13 by Andrew Bennetts
Revert returning of pack-names from the RPC.
1788
             ('pack collection reload_pack_names',)],
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
1789
            client._calls)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1790
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
1791
    def test_backwards_compatibility(self):
1792
        """If the server does not recognise the PackRepository.autopack verb,
1793
        fallback to the real_repository's implementation.
1794
        """
1795
        transport_path = 'quack'
1796
        repo, client = self.setup_fake_client_and_repository(transport_path)
1797
        client.add_unknown_method_response('PackRepository.autopack')
1798
        def stub_ensure_real():
1799
            client._calls.append(('_ensure_real',))
1800
            repo._real_repository = _StubRealPackRepository(client._calls)
1801
        repo._ensure_real = stub_ensure_real
1802
        repo.autopack()
1803
        self.assertEqual(
1804
            [('call', 'PackRepository.autopack', ('quack/',)),
1805
             ('_ensure_real',),
1806
             ('pack collection autopack',)],
1807
            client._calls)
1808
1809
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1810
class TestErrorTranslationBase(tests.TestCaseWithMemoryTransport):
1811
    """Base class for unit tests for bzrlib.remote._translate_error."""
1812
1813
    def translateTuple(self, error_tuple, **context):
1814
        """Call _translate_error with an ErrorFromSmartServer built from the
1815
        given error_tuple.
1816
1817
        :param error_tuple: A tuple of a smart server response, as would be
1818
            passed to an ErrorFromSmartServer.
1819
        :kwargs context: context items to call _translate_error with.
1820
1821
        :returns: The error raised by _translate_error.
1822
        """
1823
        # Raise the ErrorFromSmartServer before passing it as an argument,
1824
        # because _translate_error may need to re-raise it with a bare 'raise'
1825
        # statement.
1826
        server_error = errors.ErrorFromSmartServer(error_tuple)
1827
        translated_error = self.translateErrorFromSmartServer(
1828
            server_error, **context)
1829
        return translated_error
1830
1831
    def translateErrorFromSmartServer(self, error_object, **context):
1832
        """Like translateTuple, but takes an already constructed
1833
        ErrorFromSmartServer rather than a tuple.
1834
        """
1835
        try:
1836
            raise error_object
1837
        except errors.ErrorFromSmartServer, server_error:
1838
            translated_error = self.assertRaises(
1839
                errors.BzrError, remote._translate_error, server_error,
1840
                **context)
1841
        return translated_error
1842
3801.1.4 by Andrew Bennetts
Add tests for autopack RPC.
1843
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1844
class TestErrorTranslationSuccess(TestErrorTranslationBase):
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1845
    """Unit tests for bzrlib.remote._translate_error.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1846
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1847
    Given an ErrorFromSmartServer (which has an error tuple from a smart
1848
    server) and some context, _translate_error raises more specific errors from
1849
    bzrlib.errors.
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1850
1851
    This test case covers the cases where _translate_error succeeds in
1852
    translating an ErrorFromSmartServer to something better.  See
1853
    TestErrorTranslationRobustness for other cases.
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1854
    """
1855
1856
    def test_NoSuchRevision(self):
1857
        branch = self.make_branch('')
1858
        revid = 'revid'
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1859
        translated_error = self.translateTuple(
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1860
            ('NoSuchRevision', revid), branch=branch)
1861
        expected_error = errors.NoSuchRevision(branch, revid)
1862
        self.assertEqual(expected_error, translated_error)
1863
1864
    def test_nosuchrevision(self):
1865
        repository = self.make_repository('')
1866
        revid = 'revid'
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1867
        translated_error = self.translateTuple(
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1868
            ('nosuchrevision', revid), repository=repository)
1869
        expected_error = errors.NoSuchRevision(repository, revid)
1870
        self.assertEqual(expected_error, translated_error)
1871
1872
    def test_nobranch(self):
1873
        bzrdir = self.make_bzrdir('')
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1874
        translated_error = self.translateTuple(('nobranch',), bzrdir=bzrdir)
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1875
        expected_error = errors.NotBranchError(path=bzrdir.root_transport.base)
1876
        self.assertEqual(expected_error, translated_error)
1877
1878
    def test_LockContention(self):
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1879
        translated_error = self.translateTuple(('LockContention',))
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1880
        expected_error = errors.LockContention('(remote lock)')
1881
        self.assertEqual(expected_error, translated_error)
1882
1883
    def test_UnlockableTransport(self):
1884
        bzrdir = self.make_bzrdir('')
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1885
        translated_error = self.translateTuple(
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1886
            ('UnlockableTransport',), bzrdir=bzrdir)
1887
        expected_error = errors.UnlockableTransport(bzrdir.root_transport)
1888
        self.assertEqual(expected_error, translated_error)
1889
1890
    def test_LockFailed(self):
1891
        lock = 'str() of a server lock'
1892
        why = 'str() of why'
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1893
        translated_error = self.translateTuple(('LockFailed', lock, why))
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1894
        expected_error = errors.LockFailed(lock, why)
1895
        self.assertEqual(expected_error, translated_error)
1896
1897
    def test_TokenMismatch(self):
1898
        token = 'a lock token'
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1899
        translated_error = self.translateTuple(('TokenMismatch',), token=token)
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1900
        expected_error = errors.TokenMismatch(token, '(remote token)')
1901
        self.assertEqual(expected_error, translated_error)
1902
1903
    def test_Diverged(self):
1904
        branch = self.make_branch('a')
1905
        other_branch = self.make_branch('b')
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1906
        translated_error = self.translateTuple(
3533.3.3 by Andrew Bennetts
Add unit tests for bzrlib.remote._translate_error.
1907
            ('Diverged',), branch=branch, other_branch=other_branch)
1908
        expected_error = errors.DivergedBranches(branch, other_branch)
1909
        self.assertEqual(expected_error, translated_error)
1910
3786.4.2 by Andrew Bennetts
Add tests and fix code to make sure ReadError and PermissionDenied are robustly handled by _translate_error.
1911
    def test_ReadError_no_args(self):
1912
        path = 'a path'
1913
        translated_error = self.translateTuple(('ReadError',), path=path)
1914
        expected_error = errors.ReadError(path)
1915
        self.assertEqual(expected_error, translated_error)
1916
1917
    def test_ReadError(self):
1918
        path = 'a path'
1919
        translated_error = self.translateTuple(('ReadError', path))
1920
        expected_error = errors.ReadError(path)
1921
        self.assertEqual(expected_error, translated_error)
1922
1923
    def test_PermissionDenied_no_args(self):
1924
        path = 'a path'
1925
        translated_error = self.translateTuple(('PermissionDenied',), path=path)
1926
        expected_error = errors.PermissionDenied(path)
1927
        self.assertEqual(expected_error, translated_error)
1928
1929
    def test_PermissionDenied_one_arg(self):
1930
        path = 'a path'
1931
        translated_error = self.translateTuple(('PermissionDenied', path))
1932
        expected_error = errors.PermissionDenied(path)
1933
        self.assertEqual(expected_error, translated_error)
1934
1935
    def test_PermissionDenied_one_arg_and_context(self):
1936
        """Given a choice between a path from the local context and a path on
1937
        the wire, _translate_error prefers the path from the local context.
1938
        """
1939
        local_path = 'local path'
1940
        remote_path = 'remote path'
1941
        translated_error = self.translateTuple(
1942
            ('PermissionDenied', remote_path), path=local_path)
1943
        expected_error = errors.PermissionDenied(local_path)
1944
        self.assertEqual(expected_error, translated_error)
1945
1946
    def test_PermissionDenied_two_args(self):
1947
        path = 'a path'
1948
        extra = 'a string with extra info'
1949
        translated_error = self.translateTuple(
1950
            ('PermissionDenied', path, extra))
1951
        expected_error = errors.PermissionDenied(path, extra)
1952
        self.assertEqual(expected_error, translated_error)
1953
1954
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1955
class TestErrorTranslationRobustness(TestErrorTranslationBase):
1956
    """Unit tests for bzrlib.remote._translate_error's robustness.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1957
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1958
    TestErrorTranslationSuccess is for cases where _translate_error can
1959
    translate successfully.  This class about how _translate_err behaves when
1960
    it fails to translate: it re-raises the original error.
1961
    """
1962
1963
    def test_unrecognised_server_error(self):
1964
        """If the error code from the server is not recognised, the original
1965
        ErrorFromSmartServer is propagated unmodified.
1966
        """
1967
        error_tuple = ('An unknown error tuple',)
3690.1.2 by Andrew Bennetts
Rename UntranslateableErrorFromSmartServer -> UnknownErrorFromSmartServer.
1968
        server_error = errors.ErrorFromSmartServer(error_tuple)
1969
        translated_error = self.translateErrorFromSmartServer(server_error)
1970
        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.
1971
        self.assertEqual(expected_error, translated_error)
3533.3.4 by Andrew Bennetts
Add tests for _translate_error's robustness.
1972
1973
    def test_context_missing_a_key(self):
1974
        """In case of a bug in the client, or perhaps an unexpected response
1975
        from a server, _translate_error returns the original error tuple from
1976
        the server and mutters a warning.
1977
        """
1978
        # To translate a NoSuchRevision error _translate_error needs a 'branch'
1979
        # in the context dict.  So let's give it an empty context dict instead
1980
        # to exercise its error recovery.
1981
        empty_context = {}
1982
        error_tuple = ('NoSuchRevision', 'revid')
1983
        server_error = errors.ErrorFromSmartServer(error_tuple)
1984
        translated_error = self.translateErrorFromSmartServer(server_error)
1985
        self.assertEqual(server_error, translated_error)
1986
        # In addition to re-raising ErrorFromSmartServer, some debug info has
1987
        # been muttered to the log file for developer to look at.
1988
        self.assertContainsRe(
1989
            self._get_log(keep_log_file=True),
1990
            "Missing key 'branch' in context")
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1991
3786.4.2 by Andrew Bennetts
Add tests and fix code to make sure ReadError and PermissionDenied are robustly handled by _translate_error.
1992
    def test_path_missing(self):
1993
        """Some translations (PermissionDenied, ReadError) can determine the
1994
        'path' variable from either the wire or the local context.  If neither
1995
        has it, then an error is raised.
1996
        """
1997
        error_tuple = ('ReadError',)
1998
        server_error = errors.ErrorFromSmartServer(error_tuple)
1999
        translated_error = self.translateErrorFromSmartServer(server_error)
2000
        self.assertEqual(server_error, translated_error)
2001
        # In addition to re-raising ErrorFromSmartServer, some debug info has
2002
        # been muttered to the log file for developer to look at.
2003
        self.assertContainsRe(
2004
            self._get_log(keep_log_file=True), "Missing key 'path' in context")
2005
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
2006
2007
class TestStacking(tests.TestCaseWithTransport):
2008
    """Tests for operations on stacked remote repositories.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
2009
3691.2.2 by Martin Pool
Fix some problems in access to stacked repositories over hpss (#261315)
2010
    The underlying format type must support stacking.
2011
    """
2012
2013
    def test_access_stacked_remote(self):
2014
        # based on <http://launchpad.net/bugs/261315>
2015
        # make a branch stacked on another repository containing an empty
2016
        # revision, then open it over hpss - we should be able to see that
2017
        # revision.
2018
        base_transport = self.get_transport()
2019
        base_builder = self.make_branch_builder('base', format='1.6')
2020
        base_builder.start_series()
2021
        base_revid = base_builder.build_snapshot('rev-id', None,
2022
            [('add', ('', None, 'directory', None))],
2023
            'message')
2024
        base_builder.finish_series()
2025
        stacked_branch = self.make_branch('stacked', format='1.6')
2026
        stacked_branch.set_stacked_on_url('../base')
2027
        # start a server looking at this
2028
        smart_server = server.SmartTCPServer_for_testing()
2029
        smart_server.setUp()
2030
        self.addCleanup(smart_server.tearDown)
2031
        remote_bzrdir = BzrDir.open(smart_server.get_url() + '/stacked')
2032
        # can get its branch and repository
2033
        remote_branch = remote_bzrdir.open_branch()
2034
        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
2035
        remote_repo.lock_read()
2036
        try:
2037
            # it should have an appropriate fallback repository, which should also
2038
            # be a RemoteRepository
2039
            self.assertEquals(len(remote_repo._fallback_repositories), 1)
2040
            self.assertIsInstance(remote_repo._fallback_repositories[0],
2041
                RemoteRepository)
2042
            # and it has the revision committed to the underlying repository;
2043
            # these have varying implementations so we try several of them
2044
            self.assertTrue(remote_repo.has_revisions([base_revid]))
2045
            self.assertTrue(remote_repo.has_revision(base_revid))
2046
            self.assertEqual(remote_repo.get_revision(base_revid).message,
2047
                'message')
2048
        finally:
2049
            remote_repo.unlock()
3835.1.2 by Aaron Bentley
Add tests for get_parent_map
2050
3835.1.7 by Aaron Bentley
Updates from review
2051
    def prepare_stacked_remote_branch(self):
3835.1.2 by Aaron Bentley
Add tests for get_parent_map
2052
        smart_server = server.SmartTCPServer_for_testing()
2053
        smart_server.setUp()
2054
        self.addCleanup(smart_server.tearDown)
2055
        tree1 = self.make_branch_and_tree('tree1')
2056
        tree1.commit('rev1', rev_id='rev1')
2057
        tree2 = self.make_branch_and_tree('tree2', format='1.6')
2058
        tree2.branch.set_stacked_on_url(tree1.branch.base)
2059
        branch2 = Branch.open(smart_server.get_url() + '/tree2')
2060
        branch2.lock_read()
2061
        self.addCleanup(branch2.unlock)
3835.1.7 by Aaron Bentley
Updates from review
2062
        return branch2
2063
2064
    def test_stacked_get_parent_map(self):
2065
        # the public implementation of get_parent_map obeys stacking
2066
        branch = self.prepare_stacked_remote_branch()
2067
        repo = branch.repository
3835.1.2 by Aaron Bentley
Add tests for get_parent_map
2068
        self.assertEqual(['rev1'], repo.get_parent_map(['rev1']).keys())
3835.1.7 by Aaron Bentley
Updates from review
2069
3835.1.10 by Aaron Bentley
Move CachingExtraParentsProvider to Graph
2070
    def test_unstacked_get_parent_map(self):
2071
        # _unstacked_provider.get_parent_map ignores stacking
3835.1.7 by Aaron Bentley
Updates from review
2072
        branch = self.prepare_stacked_remote_branch()
3835.1.10 by Aaron Bentley
Move CachingExtraParentsProvider to Graph
2073
        provider = branch.repository._unstacked_provider
3835.1.8 by Aaron Bentley
Make UnstackedParentsProvider manage the cache
2074
        self.assertEqual([], provider.get_parent_map(['rev1']).keys())
3834.3.3 by John Arbash Meinel
Merge bzr.dev, resolve conflict in tests.
2075
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.
2076
2077
class TestRemoteBranchEffort(tests.TestCaseWithTransport):
2078
2079
    def setUp(self):
2080
        super(TestRemoteBranchEffort, self).setUp()
2081
        # Create a smart server that publishes whatever the backing VFS server
2082
        # does.
2083
        self.smart_server = server.SmartTCPServer_for_testing()
2084
        self.smart_server.setUp(self.get_server())
2085
        self.addCleanup(self.smart_server.tearDown)
2086
        # Log all HPSS calls into self.hpss_calls.
2087
        _SmartClient.hooks.install_named_hook(
2088
            'call', self.capture_hpss_call, None)
2089
        self.hpss_calls = []
2090
2091
    def capture_hpss_call(self, params):
2092
        self.hpss_calls.append(params.method)
2093
2094
    def test_copy_content_into_avoids_revision_history(self):
2095
        local = self.make_branch('local')
2096
        remote_backing_tree = self.make_branch_and_tree('remote')
2097
        remote_backing_tree.commit("Commit.")
2098
        remote_branch_url = self.smart_server.get_url() + 'remote'
2099
        remote_branch = bzrdir.BzrDir.open(remote_branch_url).open_branch()
2100
        local.repository.fetch(remote_branch.repository)
2101
        self.hpss_calls = []
2102
        remote_branch.copy_content_into(local)
3834.3.3 by John Arbash Meinel
Merge bzr.dev, resolve conflict in tests.
2103
        self.assertFalse('Branch.revision_history' in self.hpss_calls)