/brz/remove-bazaar

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