~bzr-pqm/bzr/bzr.dev

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