/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3408.3.1 by Martin Pool
Remove erroneous handling of branch.conf for RemoteBranch
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for remote bzrdir/branch/repo/etc
18
19
These are proxy objects which act on remote objects by sending messages
20
through a smart client.  The proxies are to be created when attempting to open
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 (
30
    errors,
3184.1.9 by Robert Collins
* ``Repository.get_data_stream`` is now deprecated in favour of
31
    graph,
2535.3.39 by Andrew Bennetts
Tidy some XXXs.
32
    pack,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
33
    remote,
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
34
    repository,
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
35
    tests,
36
    )
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
37
from bzrlib.branch import Branch
38
from bzrlib.bzrdir import BzrDir, BzrDirFormat
39
from bzrlib.remote import (
40
    RemoteBranch,
41
    RemoteBzrDir,
42
    RemoteBzrDirFormat,
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
43
    RemoteRepository,
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
44
    )
45
from bzrlib.revision import NULL_REVISION
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
46
from bzrlib.smart import server, medium
2018.5.159 by Andrew Bennetts
Rename SmartClient to _SmartClient.
47
from bzrlib.smart.client import _SmartClient
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
48
from bzrlib.symbol_versioning import one_four
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
49
from bzrlib.transport import get_transport
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
50
from bzrlib.transport.memory import MemoryTransport
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
51
from bzrlib.transport.remote import RemoteTransport
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
52
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)
53
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
54
class BasicRemoteObjectTests(tests.TestCaseWithTransport):
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
55
56
    def setUp(self):
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
57
        self.transport_server = server.SmartTCPServer_for_testing
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
58
        super(BasicRemoteObjectTests, self).setUp()
59
        self.transport = self.get_transport()
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
60
        # 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
61
        self.local_wt = BzrDir.create_standalone_workingtree('.')
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
62
2018.5.171 by Andrew Bennetts
Disconnect RemoteTransports in some tests to avoid tripping up test_strace with leftover threads from previous tests.
63
    def tearDown(self):
64
        self.transport.disconnect()
65
        tests.TestCaseWithTransport.tearDown(self)
66
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
67
    def test_create_remote_bzrdir(self):
68
        b = remote.RemoteBzrDir(self.transport)
69
        self.assertIsInstance(b, BzrDir)
70
71
    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.
72
        # open a standalone branch in the working directory
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
73
        b = remote.RemoteBzrDir(self.transport)
74
        branch = b.open_branch()
2018.5.163 by Andrew Bennetts
Deal with various review comments from Robert.
75
        self.assertIsInstance(branch, Branch)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
76
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
77
    def test_remote_repository(self):
78
        b = BzrDir.open_from_transport(self.transport)
79
        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.
80
        revid = u'\xc823123123'.encode('utf8')
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
81
        self.assertFalse(repo.has_revision(revid))
82
        self.local_wt.commit(message='test commit', rev_id=revid)
83
        self.assertTrue(repo.has_revision(revid))
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
84
85
    def test_remote_branch_revision_history(self):
86
        b = BzrDir.open_from_transport(self.transport).open_branch()
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
87
        self.assertEqual([], b.revision_history())
88
        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.
89
        r2 = self.local_wt.commit('1st commit', rev_id=u'\xc8'.encode('utf8'))
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
90
        self.assertEqual([r1, r2], b.revision_history())
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
91
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
92
    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)
93
        """Should open a RemoteBzrDir over a RemoteTransport"""
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
94
        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.
95
        self.assertTrue(RemoteBzrDirFormat
96
                        in BzrDirFormat._control_server_formats)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
97
        self.assertIsInstance(fmt, remote.RemoteBzrDirFormat)
98
99
    def test_open_detected_smart_format(self):
100
        fmt = BzrDirFormat.find_format(self.transport)
101
        d = fmt.open(self.transport)
102
        self.assertIsInstance(d, BzrDir)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
103
2477.1.1 by Martin Pool
Add RemoteBranch repr
104
    def test_remote_branch_repr(self):
105
        b = BzrDir.open_from_transport(self.transport).open_branch()
106
        self.assertStartsWith(str(b), 'RemoteBranch(')
107
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
108
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
109
class FakeProtocol(object):
110
    """Lookalike SmartClientRequestProtocolOne allowing body reading tests."""
111
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
112
    def __init__(self, body, fake_client):
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
113
        self.body = body
114
        self._body_buffer = None
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
115
        self._fake_client = fake_client
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
116
117
    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.
118
        if self._body_buffer is None:
119
            self._body_buffer = StringIO(self.body)
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
120
        bytes = self._body_buffer.read(count)
121
        if self._body_buffer.tell() == len(self._body_buffer.getvalue()):
122
            self._fake_client.expecting_body = False
123
        return bytes
124
125
    def cancel_read_body(self):
126
        self._fake_client.expecting_body = False
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
127
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
128
    def read_streamed_body(self):
129
        return self.body
130
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
131
2018.5.159 by Andrew Bennetts
Rename SmartClient to _SmartClient.
132
class FakeClient(_SmartClient):
133
    """Lookalike for _SmartClient allowing testing."""
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
134
    
3104.4.2 by Andrew Bennetts
All tests passing.
135
    def __init__(self, responses, fake_medium_base='fake base'):
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
136
        """Create a FakeClient.
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
137
3104.4.2 by Andrew Bennetts
All tests passing.
138
        :param responses: A list of response-tuple, body-data pairs to be sent
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.
139
            back to callers.  A special case is if the response-tuple is
140
            'unknown verb', then a UnknownSmartMethod will be raised for that
141
            call, using the second element of the tuple as the verb in the
142
            exception.
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
143
        """
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
144
        self.responses = responses
145
        self._calls = []
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
146
        self.expecting_body = False
3313.2.1 by Andrew Bennetts
Change _SmartClient's API to accept a medium and a base, rather than a _SharedConnection.
147
        _SmartClient.__init__(self, FakeMedium(self._calls), fake_medium_base)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
148
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.
149
    def _get_next_response(self):
150
        response_tuple = self.responses.pop(0)
151
        if response_tuple[0][0] == 'unknown verb':
152
            raise errors.UnknownSmartMethod(response_tuple[0][1])
153
        return response_tuple
154
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
155
    def call(self, method, *args):
156
        self._calls.append(('call', 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.
157
        return self._get_next_response()[0]
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
158
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
159
    def call_expecting_body(self, method, *args):
160
        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.
161
        result = self._get_next_response()
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
162
        self.expecting_body = True
163
        return result[0], FakeProtocol(result[1], self)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
164
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.
165
    def call_with_body_bytes_expecting_body(self, method, args, body):
166
        self._calls.append(('call_with_body_bytes_expecting_body', method,
167
            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.
168
        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.
169
        self.expecting_body = True
170
        return result[0], FakeProtocol(result[1], self)
171
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
172
3104.4.2 by Andrew Bennetts
All tests passing.
173
class FakeMedium(object):
174
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
175
    def __init__(self, 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.
176
        self._remote_is_at_least_1_2 = True
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
177
        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.
178
179
    def disconnect(self):
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
180
        self._client_calls.append(('disconnect medium',))
3104.4.2 by Andrew Bennetts
All tests passing.
181
182
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.
183
class TestVfsHas(tests.TestCase):
184
185
    def test_unicode_path(self):
186
        client = FakeClient([(('yes',), )], '/')
187
        transport = RemoteTransport('bzr://localhost/', _client=client)
188
        filename = u'/hell\u00d8'.encode('utf8')
189
        result = transport.has(filename)
190
        self.assertEqual(
191
            [('call', 'has', (filename,))],
192
            client._calls)
193
        self.assertTrue(result)
194
195
3313.3.3 by Andrew Bennetts
Add tests for _SmartClient.remote_path_for_transport.
196
class Test_SmartClient_remote_path_from_transport(tests.TestCase):
197
    """Tests for the behaviour of _SmartClient.remote_path_from_transport."""
198
199
    def assertRemotePath(self, expected, client_base, transport_base):
200
        """Assert that the result of _SmartClient.remote_path_from_transport
201
        is the expected value for a given client_base and transport_base.
202
        """
203
        dummy_medium = 'dummy medium'
204
        client = _SmartClient(dummy_medium, client_base)
205
        transport = get_transport(transport_base)
206
        result = client.remote_path_from_transport(transport)
207
        self.assertEqual(expected, result)
208
        
209
    def test_remote_path_from_transport(self):
210
        """_SmartClient.remote_path_from_transport calculates a URL for the
211
        given transport relative to the root of the client base URL.
212
        """
213
        self.assertRemotePath('xyz/', 'bzr://host/path', 'bzr://host/xyz')
214
        self.assertRemotePath(
215
            'path/xyz/', 'bzr://host/path', 'bzr://host/path/xyz')
216
217
    def test_remote_path_from_transport_http(self):
218
        """Remote paths for HTTP transports are calculated differently to other
219
        transports.  They are just relative to the client base, not the root
220
        directory of the host.
221
        """
222
        for scheme in ['http:', 'https:', 'bzr+http:', 'bzr+https:']:
223
            self.assertRemotePath(
224
                '../xyz/', scheme + '//host/path', scheme + '//host/xyz')
225
            self.assertRemotePath(
226
                'xyz/', scheme + '//host/path', scheme + '//host/path/xyz')
227
228
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
229
class TestBzrDirOpenBranch(tests.TestCase):
230
231
    def test_branch_present(self):
232
        transport = MemoryTransport()
233
        transport.mkdir('quack')
234
        transport = transport.clone('quack')
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
235
        client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no', 'no'), )],
3104.4.2 by Andrew Bennetts
All tests passing.
236
                            transport.base)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
237
        bzrdir = RemoteBzrDir(transport, _client=client)
238
        result = bzrdir.open_branch()
239
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
240
            [('call', 'BzrDir.open_branch', ('quack/',)),
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
241
             ('call', 'BzrDir.find_repositoryV2', ('quack/',))],
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
242
            client._calls)
243
        self.assertIsInstance(result, RemoteBranch)
244
        self.assertEqual(bzrdir, result.bzrdir)
245
246
    def test_branch_missing(self):
247
        transport = MemoryTransport()
248
        transport.mkdir('quack')
249
        transport = transport.clone('quack')
3104.4.2 by Andrew Bennetts
All tests passing.
250
        client = FakeClient([(('nobranch',), )], transport.base)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
251
        bzrdir = RemoteBzrDir(transport, _client=client)
252
        self.assertRaises(errors.NotBranchError, bzrdir.open_branch)
253
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
254
            [('call', 'BzrDir.open_branch', ('quack/',))],
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
255
            client._calls)
256
3211.4.1 by Robert Collins
* ``RemoteBzrDir._get_tree_branch`` no longer triggers ``_ensure_real``,
257
    def test__get_tree_branch(self):
258
        # _get_tree_branch is a form of open_branch, but it should only ask for
259
        # branch opening, not any other network requests.
260
        calls = []
261
        def open_branch():
262
            calls.append("Called")
263
            return "a-branch"
264
        transport = MemoryTransport()
265
        # no requests on the network - catches other api calls being made.
266
        client = FakeClient([], transport.base)
267
        bzrdir = RemoteBzrDir(transport, _client=client)
268
        # patch the open_branch call to record that it was called.
269
        bzrdir.open_branch = open_branch
270
        self.assertEqual((None, "a-branch"), bzrdir._get_tree_branch())
271
        self.assertEqual(["Called"], calls)
272
        self.assertEqual([], client._calls)
273
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.
274
    def test_url_quoting_of_path(self):
275
        # Relpaths on the wire should not be URL-escaped.  So "~" should be
276
        # transmitted as "~", not "%7E".
277
        transport = RemoteTransport('bzr://localhost/~hello/')
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
278
        client = FakeClient([(('ok', ''), ), (('ok', '', 'no', 'no', 'no'), )],
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.
279
                            transport.base)
280
        bzrdir = RemoteBzrDir(transport, _client=client)
281
        result = bzrdir.open_branch()
282
        self.assertEqual(
283
            [('call', 'BzrDir.open_branch', ('~hello/',)),
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
284
             ('call', 'BzrDir.find_repositoryV2', ('~hello/',))],
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.
285
            client._calls)
286
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
287
    def check_open_repository(self, rich_root, subtrees, external_lookup='no'):
3104.4.2 by Andrew Bennetts
All tests passing.
288
        transport = MemoryTransport()
289
        transport.mkdir('quack')
290
        transport = transport.clone('quack')
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
291
        if rich_root:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
292
            rich_response = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
293
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
294
            rich_response = 'no'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
295
        if subtrees:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
296
            subtree_response = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
297
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
298
            subtree_response = 'no'
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
299
        client = FakeClient(
300
            [(('ok', '', rich_response, subtree_response, external_lookup), ),],
301
            transport.base)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
302
        bzrdir = RemoteBzrDir(transport, _client=client)
303
        result = bzrdir.open_repository()
304
        self.assertEqual(
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
305
            [('call', 'BzrDir.find_repositoryV2', ('quack/',))],
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
306
            client._calls)
307
        self.assertIsInstance(result, RemoteRepository)
308
        self.assertEqual(bzrdir, result.bzrdir)
309
        self.assertEqual(rich_root, result._format.rich_root_data)
2018.5.138 by Robert Collins
Merge bzr.dev.
310
        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.
311
312
    def test_open_repository_sets_format_attributes(self):
313
        self.check_open_repository(True, True)
314
        self.check_open_repository(False, True)
315
        self.check_open_repository(True, False)
316
        self.check_open_repository(False, False)
3221.3.3 by Robert Collins
* Hook up the new remote method ``RemoteBzrDir.find_repositoryV2`` so
317
        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.
318
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
319
    def test_old_server(self):
320
        """RemoteBzrDirFormat should fail to probe if the server version is too
321
        old.
322
        """
323
        self.assertRaises(errors.NotBranchError,
324
            RemoteBzrDirFormat.probe_transport, OldServerTransport())
325
326
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.
327
class TestBzrDirOpenRepository(tests.TestCase):
328
329
    def test_backwards_compat_1_2(self):
330
        transport = MemoryTransport()
331
        transport.mkdir('quack')
332
        transport = transport.clone('quack')
333
        client = FakeClient([
334
            (('unknown verb', 'RemoteRepository.find_repositoryV2'), ''),
335
            (('ok', '', 'no', 'no'), ''),],
336
            transport.base)
337
        bzrdir = RemoteBzrDir(transport, _client=client)
338
        repo = bzrdir.open_repository()
339
        self.assertEqual(
340
            [('call', 'BzrDir.find_repositoryV2', ('quack/',)),
341
             ('call', 'BzrDir.find_repository', ('quack/',))],
342
            client._calls)
343
344
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
345
class OldSmartClient(object):
346
    """A fake smart client for test_old_version that just returns a version one
347
    response to the 'hello' (query version) command.
348
    """
349
350
    def get_request(self):
351
        input_file = StringIO('ok\x011\n')
352
        output_file = StringIO()
353
        client_medium = medium.SmartSimplePipesClientMedium(
354
            input_file, output_file)
355
        return medium.SmartClientStreamMediumRequest(client_medium)
356
3241.1.1 by Andrew Bennetts
Shift protocol version querying from RemoteBzrDirFormat into SmartClientMedium.
357
    def protocol_version(self):
358
        return 1
359
2432.3.2 by Andrew Bennetts
Add test, and tidy implementation.
360
361
class OldServerTransport(object):
362
    """A fake transport for test_old_server that reports it's smart server
363
    protocol version as version one.
364
    """
365
366
    def __init__(self):
367
        self.base = 'fake:'
368
369
    def get_smart_client(self):
370
        return OldSmartClient()
371
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
372
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
373
class TestBranchLastRevisionInfo(tests.TestCase):
374
375
    def test_empty_branch(self):
376
        # in an empty branch we decode the response properly
377
        transport = MemoryTransport()
3104.4.2 by Andrew Bennetts
All tests passing.
378
        client = FakeClient([(('ok', '0', 'null:'), )], transport.base)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
379
        transport.mkdir('quack')
380
        transport = transport.clone('quack')
381
        # we do not want bzrdir to make any remote calls
382
        bzrdir = RemoteBzrDir(transport, _client=False)
383
        branch = RemoteBranch(bzrdir, None, _client=client)
384
        result = branch.last_revision_info()
385
386
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
387
            [('call', 'Branch.last_revision_info', ('quack/',))],
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
388
            client._calls)
389
        self.assertEqual((0, NULL_REVISION), result)
390
391
    def test_non_empty_branch(self):
392
        # 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.
393
        revid = u'\xc8'.encode('utf8')
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
394
        transport = MemoryTransport()
3104.4.2 by Andrew Bennetts
All tests passing.
395
        client = FakeClient([(('ok', '2', revid), )], transport.base)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
396
        transport.mkdir('kwaak')
397
        transport = transport.clone('kwaak')
398
        # we do not want bzrdir to make any remote calls
399
        bzrdir = RemoteBzrDir(transport, _client=False)
400
        branch = RemoteBranch(bzrdir, None, _client=client)
401
        result = branch.last_revision_info()
402
403
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
404
            [('call', 'Branch.last_revision_info', ('kwaak/',))],
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
405
            client._calls)
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
406
        self.assertEqual((2, revid), result)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
407
408
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
409
class TestBranchSetLastRevision(tests.TestCase):
410
411
    def test_set_empty(self):
412
        # set_revision_history([]) is translated to calling
413
        # Branch.set_last_revision(path, '') on the wire.
3104.4.2 by Andrew Bennetts
All tests passing.
414
        transport = MemoryTransport()
415
        transport.mkdir('branch')
416
        transport = transport.clone('branch')
417
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
418
        client = FakeClient([
419
            # lock_write
420
            (('ok', 'branch token', 'repo token'), ),
421
            # set_last_revision
422
            (('ok',), ),
423
            # unlock
3104.4.2 by Andrew Bennetts
All tests passing.
424
            (('ok',), )],
425
            transport.base)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
426
        bzrdir = RemoteBzrDir(transport, _client=False)
427
        branch = RemoteBranch(bzrdir, None, _client=client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
428
        # This is a hack to work around the problem that RemoteBranch currently
429
        # unnecessarily invokes _ensure_real upon a call to lock_write.
430
        branch._ensure_real = lambda: None
431
        branch.lock_write()
432
        client._calls = []
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
433
        result = branch.set_revision_history([])
434
        self.assertEqual(
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
435
            [('call', 'Branch.set_last_revision',
3104.4.2 by Andrew Bennetts
All tests passing.
436
                ('branch/', 'branch token', 'repo token', 'null:'))],
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
437
            client._calls)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
438
        branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
439
        self.assertEqual(None, result)
440
441
    def test_set_nonempty(self):
442
        # set_revision_history([rev-id1, ..., rev-idN]) is translated to calling
443
        # Branch.set_last_revision(path, rev-idN) on the wire.
3104.4.2 by Andrew Bennetts
All tests passing.
444
        transport = MemoryTransport()
445
        transport.mkdir('branch')
446
        transport = transport.clone('branch')
447
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
448
        client = FakeClient([
449
            # lock_write
450
            (('ok', 'branch token', 'repo token'), ),
451
            # set_last_revision
452
            (('ok',), ),
453
            # unlock
3104.4.2 by Andrew Bennetts
All tests passing.
454
            (('ok',), )],
455
            transport.base)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
456
        bzrdir = RemoteBzrDir(transport, _client=False)
457
        branch = RemoteBranch(bzrdir, None, _client=client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
458
        # This is a hack to work around the problem that RemoteBranch currently
459
        # unnecessarily invokes _ensure_real upon a call to lock_write.
460
        branch._ensure_real = lambda: None
461
        # Lock the branch, reset the record of remote calls.
462
        branch.lock_write()
463
        client._calls = []
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
464
465
        result = branch.set_revision_history(['rev-id1', 'rev-id2'])
466
        self.assertEqual(
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
467
            [('call', 'Branch.set_last_revision',
3104.4.2 by Andrew Bennetts
All tests passing.
468
                ('branch/', 'branch token', 'repo token', 'rev-id2'))],
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
469
            client._calls)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
470
        branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
471
        self.assertEqual(None, result)
472
473
    def test_no_such_revision(self):
474
        # A response of 'NoSuchRevision' is translated into an exception.
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
475
        client = FakeClient([
476
            # lock_write
477
            (('ok', 'branch token', 'repo token'), ),
478
            # set_last_revision
479
            (('NoSuchRevision', 'rev-id'), ),
480
            # unlock
481
            (('ok',), )])
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
482
        transport = MemoryTransport()
483
        transport.mkdir('branch')
484
        transport = transport.clone('branch')
485
486
        bzrdir = RemoteBzrDir(transport, _client=False)
487
        branch = RemoteBranch(bzrdir, None, _client=client)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
488
        branch._ensure_real = lambda: None
489
        branch.lock_write()
490
        client._calls = []
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
491
492
        self.assertRaises(
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
493
            errors.NoSuchRevision, branch.set_revision_history, ['rev-id'])
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
494
        branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
495
496
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
497
class TestBranchSetLastRevisionInfo(tests.TestCase):
498
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
499
    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.
500
        # set_last_revision_info(num, 'rev-id') is translated to calling
501
        # 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'.
502
        transport = MemoryTransport()
503
        transport.mkdir('branch')
504
        transport = transport.clone('branch')
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
505
        client = FakeClient([
506
            # lock_write
507
            (('ok', 'branch token', 'repo token'), ),
508
            # set_last_revision_info
509
            (('ok',), ),
510
            # unlock
3297.4.1 by Andrew Bennetts
Merge 'Add Branch.set_last_revision_info smart method'.
511
            (('ok',), )], transport.base)
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
512
513
        bzrdir = RemoteBzrDir(transport, _client=False)
514
        branch = RemoteBranch(bzrdir, None, _client=client)
515
        # This is a hack to work around the problem that RemoteBranch currently
516
        # unnecessarily invokes _ensure_real upon a call to lock_write.
517
        branch._ensure_real = lambda: None
518
        # Lock the branch, reset the record of remote calls.
519
        branch.lock_write()
520
        client._calls = []
521
        result = branch.set_last_revision_info(1234, 'a-revision-id')
522
        self.assertEqual(
523
            [('call', 'Branch.set_last_revision_info',
3297.4.1 by Andrew Bennetts
Merge 'Add Branch.set_last_revision_info smart method'.
524
                ('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.
525
                 '1234', 'a-revision-id'))],
526
            client._calls)
527
        self.assertEqual(None, result)
528
529
    def test_no_such_revision(self):
530
        # A response of 'NoSuchRevision' is translated into an exception.
531
        client = FakeClient([
532
            # lock_write
533
            (('ok', 'branch token', 'repo token'), ),
534
            # set_last_revision_info
535
            (('NoSuchRevision', 'revid'), ),
536
            # unlock
537
            (('ok',), ),
538
            ])
539
        transport = MemoryTransport()
540
        transport.mkdir('branch')
541
        transport = transport.clone('branch')
542
543
        bzrdir = RemoteBzrDir(transport, _client=False)
544
        branch = RemoteBranch(bzrdir, None, _client=client)
545
        # This is a hack to work around the problem that RemoteBranch currently
546
        # unnecessarily invokes _ensure_real upon a call to lock_write.
547
        branch._ensure_real = lambda: None
548
        # Lock the branch, reset the record of remote calls.
549
        branch.lock_write()
550
        client._calls = []
551
552
        self.assertRaises(
553
            errors.NoSuchRevision, branch.set_last_revision_info, 123, 'revid')
554
        branch.unlock()
555
3297.4.2 by Andrew Bennetts
Add backwards compatibility for servers older than 1.4.
556
    def lock_remote_branch(self, branch):
557
        """Trick a RemoteBranch into thinking it is locked."""
558
        branch._lock_mode = 'w'
559
        branch._lock_count = 2
560
        branch._lock_token = 'branch token'
561
        branch._repo_lock_token = 'repo token'
562
563
    def test_backwards_compatibility(self):
564
        """If the server does not support the Branch.set_last_revision_info
565
        verb (which is new in 1.4), then the client falls back to VFS methods.
566
        """
567
        # This test is a little messy.  Unlike most tests in this file, it
568
        # doesn't purely test what a Remote* object sends over the wire, and
569
        # how it reacts to responses from the wire.  It instead relies partly
570
        # on asserting that the RemoteBranch will call
571
        # self._real_branch.set_last_revision_info(...).
572
573
        # First, set up our RemoteBranch with a FakeClient that raises
574
        # UnknownSmartMethod, and a StubRealBranch that logs how it is called.
575
        transport = MemoryTransport()
576
        transport.mkdir('branch')
577
        transport = transport.clone('branch')
578
        client = FakeClient(
579
            [(('unknown verb', 'Branch.set_last_revision_info',), ),],
580
            transport.base)
581
        bzrdir = RemoteBzrDir(transport, _client=False)
582
        branch = RemoteBranch(bzrdir, None, _client=client)
583
        class StubRealBranch(object):
584
            def __init__(self):
585
                self.calls = []
586
            def set_last_revision_info(self, revno, revision_id):
587
                self.calls.append(
588
                    ('set_last_revision_info', revno, revision_id))
589
        real_branch = StubRealBranch()
590
        branch._real_branch = real_branch
591
        self.lock_remote_branch(branch)
592
593
        # Call set_last_revision_info, and verify it behaved as expected.
594
        result = branch.set_last_revision_info(1234, 'a-revision-id')
595
        self.assertEqual(
596
            [('call', 'Branch.set_last_revision_info',
597
                ('branch/', 'branch token', 'repo token',
598
                 '1234', 'a-revision-id')),],
599
            client._calls)
600
        self.assertEqual(
601
            [('set_last_revision_info', 1234, 'a-revision-id')],
602
            real_branch.calls)
603
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
604
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.
605
class TestBranchControlGetBranchConf(tests.TestCaseWithMemoryTransport):
3408.3.1 by Martin Pool
Remove erroneous handling of branch.conf for RemoteBranch
606
    """Getting the branch configuration should use an abstract method not vfs.
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
607
    """
608
609
    def test_get_branch_conf(self):
3408.3.1 by Martin Pool
Remove erroneous handling of branch.conf for RemoteBranch
610
        raise tests.KnownFailure('branch.conf is not retrieved by get_config_file')
611
        # We should see that branch.get_config() does a single rpc to get the
612
        # remote configuration file, abstracting away where that is stored on
613
        # the server.  However at the moment it always falls back to using the
614
        # vfs, and this would need some changes in config.py.
615
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
616
        # in an empty branch we decode the response properly
3408.3.1 by Martin Pool
Remove erroneous handling of branch.conf for RemoteBranch
617
        client = FakeClient([(('ok', ), '# config file body')], self.get_url())
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.
618
        # we need to make a real branch because the remote_branch.control_files
619
        # will trigger _ensure_real.
620
        branch = self.make_branch('quack')
621
        transport = branch.bzrdir.root_transport
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
622
        # we do not want bzrdir to make any remote calls
623
        bzrdir = RemoteBzrDir(transport, _client=False)
624
        branch = RemoteBranch(bzrdir, None, _client=client)
3408.3.1 by Martin Pool
Remove erroneous handling of branch.conf for RemoteBranch
625
        config = branch.get_config()
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
626
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
627
            [('call_expecting_body', 'Branch.get_config_file', ('quack/',))],
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
628
            client._calls)
629
630
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.
631
class TestBranchLockWrite(tests.TestCase):
632
633
    def test_lock_write_unlockable(self):
634
        transport = MemoryTransport()
3104.4.2 by Andrew Bennetts
All tests passing.
635
        client = FakeClient([(('UnlockableTransport', ), '')], transport.base)
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.
636
        transport.mkdir('quack')
637
        transport = transport.clone('quack')
638
        # we do not want bzrdir to make any remote calls
639
        bzrdir = RemoteBzrDir(transport, _client=False)
640
        branch = RemoteBranch(bzrdir, None, _client=client)
641
        self.assertRaises(errors.UnlockableTransport, branch.lock_write)
642
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
643
            [('call', 'Branch.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.
644
            client._calls)
645
646
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
647
class TestTransportIsReadonly(tests.TestCase):
648
649
    def test_true(self):
650
        client = FakeClient([(('yes',), '')])
651
        transport = RemoteTransport('bzr://example.com/', medium=False,
652
                                    _client=client)
653
        self.assertEqual(True, transport.is_readonly())
654
        self.assertEqual(
655
            [('call', 'Transport.is_readonly', ())],
656
            client._calls)
657
658
    def test_false(self):
659
        client = FakeClient([(('no',), '')])
660
        transport = RemoteTransport('bzr://example.com/', medium=False,
661
                                    _client=client)
662
        self.assertEqual(False, transport.is_readonly())
663
        self.assertEqual(
664
            [('call', 'Transport.is_readonly', ())],
665
            client._calls)
666
667
    def test_error_from_old_server(self):
668
        """bzr 0.15 and earlier servers don't recognise the is_readonly verb.
669
        
670
        Clients should treat it as a "no" response, because is_readonly is only
671
        advisory anyway (a transport could be read-write, but then the
672
        underlying filesystem could be readonly anyway).
673
        """
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.
674
        client = FakeClient([(('unknown verb', '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.
675
        transport = RemoteTransport('bzr://example.com/', medium=False,
676
                                    _client=client)
677
        self.assertEqual(False, transport.is_readonly())
678
        self.assertEqual(
679
            [('call', 'Transport.is_readonly', ())],
680
            client._calls)
681
2466.2.2 by Andrew Bennetts
Add tests for RemoteTransport.is_readonly in the style of the other remote object tests.
682
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
683
class TestRemoteRepository(tests.TestCase):
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
684
    """Base for testing RemoteRepository protocol usage.
685
    
686
    These tests contain frozen requests and responses.  We want any changes to 
687
    what is sent or expected to be require a thoughtful update to these tests
688
    because they might break compatibility with different-versioned servers.
689
    """
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
690
691
    def setup_fake_client_and_repository(self, responses, transport_path):
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
692
        """Create the fake client and repository for testing with.
693
        
694
        There's no real server here; we just have canned responses sent
695
        back one by one.
696
        
697
        :param transport_path: Path below the root of the MemoryTransport
698
            where the repository will be created.
699
        """
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
700
        transport = MemoryTransport()
701
        transport.mkdir(transport_path)
3104.4.2 by Andrew Bennetts
All tests passing.
702
        client = FakeClient(responses, transport.base)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
703
        transport = transport.clone(transport_path)
704
        # we do not want bzrdir to make any remote calls
705
        bzrdir = RemoteBzrDir(transport, _client=False)
706
        repo = RemoteRepository(bzrdir, None, _client=client)
707
        return repo, client
708
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
709
2018.12.2 by Andrew Bennetts
Remove some duplicate code in test_remote
710
class TestRepositoryGatherStats(TestRemoteRepository):
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
711
712
    def test_revid_none(self):
713
        # ('ok',), body with revisions and size
714
        responses = [(('ok', ), 'revisions: 2\nsize: 18\n')]
715
        transport_path = 'quack'
716
        repo, client = self.setup_fake_client_and_repository(
717
            responses, transport_path)
718
        result = repo.gather_stats(None)
719
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
720
            [('call_expecting_body', 'Repository.gather_stats',
3104.4.2 by Andrew Bennetts
All tests passing.
721
             ('quack/','','no'))],
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
722
            client._calls)
723
        self.assertEqual({'revisions': 2, 'size': 18}, result)
724
725
    def test_revid_no_committers(self):
726
        # ('ok',), body without committers
727
        responses = [(('ok', ),
728
                      'firstrev: 123456.300 3600\n'
729
                      'latestrev: 654231.400 0\n'
730
                      'revisions: 2\n'
731
                      'size: 18\n')]
732
        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.
733
        revid = u'\xc8'.encode('utf8')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
734
        repo, client = self.setup_fake_client_and_repository(
735
            responses, transport_path)
736
        result = repo.gather_stats(revid)
737
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
738
            [('call_expecting_body', 'Repository.gather_stats',
3104.4.2 by Andrew Bennetts
All tests passing.
739
              ('quick/', revid, 'no'))],
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
740
            client._calls)
741
        self.assertEqual({'revisions': 2, 'size': 18,
742
                          'firstrev': (123456.300, 3600),
743
                          'latestrev': (654231.400, 0),},
744
                         result)
745
746
    def test_revid_with_committers(self):
747
        # ('ok',), body with committers
748
        responses = [(('ok', ),
749
                      'committers: 128\n'
750
                      'firstrev: 123456.300 3600\n'
751
                      'latestrev: 654231.400 0\n'
752
                      'revisions: 2\n'
753
                      'size: 18\n')]
754
        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.
755
        revid = u'\xc8'.encode('utf8')
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
756
        repo, client = self.setup_fake_client_and_repository(
757
            responses, transport_path)
758
        result = repo.gather_stats(revid, True)
759
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
760
            [('call_expecting_body', 'Repository.gather_stats',
3104.4.2 by Andrew Bennetts
All tests passing.
761
              ('buick/', revid, 'yes'))],
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
762
            client._calls)
763
        self.assertEqual({'revisions': 2, 'size': 18,
764
                          'committers': 128,
765
                          'firstrev': (123456.300, 3600),
766
                          'latestrev': (654231.400, 0),},
767
                         result)
768
769
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
770
class TestRepositoryGetGraph(TestRemoteRepository):
771
772
    def test_get_graph(self):
3172.5.8 by Robert Collins
Review feedback.
773
        # get_graph returns a graph with the repository as the
774
        # parents_provider.
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
775
        responses = []
776
        transport_path = 'quack'
777
        repo, client = self.setup_fake_client_and_repository(
778
            responses, transport_path)
779
        graph = repo.get_graph()
780
        self.assertEqual(graph._parents_provider, repo)
781
782
783
class TestRepositoryGetParentMap(TestRemoteRepository):
784
785
    def test_get_parent_map_caching(self):
786
        # get_parent_map returns from cache until unlock()
787
        # setup a reponse with two revisions
788
        r1 = u'\u0e33'.encode('utf8')
789
        r2 = u'\u0dab'.encode('utf8')
790
        lines = [' '.join([r2, r1]), r1]
3211.5.2 by Robert Collins
Change RemoteRepository.get_parent_map to use bz2 not gzip for compression.
791
        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.
792
        responses = [(('ok', ), encoded_body), (('ok', ), encoded_body)]
793
794
        transport_path = 'quack'
795
        repo, client = self.setup_fake_client_and_repository(
796
            responses, transport_path)
797
        repo.lock_read()
798
        graph = repo.get_graph()
799
        parents = graph.get_parent_map([r2])
800
        self.assertEqual({r2: (r1,)}, parents)
801
        # locking and unlocking deeper should not reset
802
        repo.lock_read()
803
        repo.unlock()
804
        parents = graph.get_parent_map([r1])
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
805
        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.
806
        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.
807
            [('call_with_body_bytes_expecting_body',
808
              'Repository.get_parent_map', ('quack/', r2), '\n\n0')],
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
809
            client._calls)
810
        repo.unlock()
811
        # now we call again, and it should use the second response.
812
        repo.lock_read()
813
        graph = repo.get_graph()
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
814
        parents = graph.get_parent_map([r1])
815
        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.
816
        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.
817
            [('call_with_body_bytes_expecting_body',
818
              'Repository.get_parent_map', ('quack/', r2), '\n\n0'),
819
             ('call_with_body_bytes_expecting_body',
820
              'Repository.get_parent_map', ('quack/', r1), '\n\n0'),
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
821
            ],
822
            client._calls)
823
        repo.unlock()
824
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
825
    def test_get_parent_map_reconnects_if_unknown_method(self):
826
        responses = [
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.
827
            (('unknown verb', 'Repository.get_parent_map'), ''),
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
828
            (('ok',), '')]
829
        transport_path = 'quack'
830
        repo, client = self.setup_fake_client_and_repository(
831
            responses, transport_path)
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
832
        self.assertTrue(client._medium._remote_is_at_least_1_2)
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
833
        rev_id = 'revision-id'
3297.3.5 by Andrew Bennetts
Suppress a deprecation warning.
834
        expected_deprecations = [
835
            'bzrlib.remote.RemoteRepository.get_revision_graph was deprecated '
836
            'in version 1.4.']
837
        parents = self.callDeprecated(
838
            expected_deprecations, repo.get_parent_map, [rev_id])
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
839
        self.assertEqual(
3213.1.8 by Andrew Bennetts
Merge from bzr.dev.
840
            [('call_with_body_bytes_expecting_body',
841
              'Repository.get_parent_map', ('quack/', rev_id), '\n\n0'),
3213.1.2 by Andrew Bennetts
Add test for reconnection if get_parent_map is unknown by the server.
842
             ('disconnect medium',),
843
             ('call_expecting_body', 'Repository.get_revision_graph',
844
              ('quack/', ''))],
845
            client._calls)
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
846
        # The medium is now marked as being connected to an older server
847
        self.assertFalse(client._medium._remote_is_at_least_1_2)
848
849
    def test_get_parent_map_fallback_parentless_node(self):
850
        """get_parent_map falls back to get_revision_graph on old servers.  The
851
        results from get_revision_graph are tweaked to match the get_parent_map
852
        API.
853
3389.1.3 by Andrew Bennetts
Remove XXX from test description.
854
        Specifically, a {key: ()} result from get_revision_graph means "no
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
855
        parents" for that key, which in get_parent_map results should be
3389.1.3 by Andrew Bennetts
Remove XXX from test description.
856
        represented as {key: ('null:',)}.
3389.1.2 by Andrew Bennetts
Add test for the bug John found.
857
858
        This is the test for https://bugs.launchpad.net/bzr/+bug/214894
859
        """
860
        rev_id = 'revision-id'
861
        responses = [(('ok',), rev_id)]
862
        transport_path = 'quack'
863
        repo, client = self.setup_fake_client_and_repository(
864
            responses, transport_path)
865
        client._medium._remote_is_at_least_1_2 = False
866
        expected_deprecations = [
867
            'bzrlib.remote.RemoteRepository.get_revision_graph was deprecated '
868
            'in version 1.4.']
869
        parents = self.callDeprecated(
870
            expected_deprecations, repo.get_parent_map, [rev_id])
871
        self.assertEqual(
872
            [('call_expecting_body', 'Repository.get_revision_graph',
873
             ('quack/', ''))],
874
            client._calls)
875
        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.
876
3297.2.3 by Andrew Bennetts
Test the code path that the typo is on.
877
    def test_get_parent_map_unexpected_response(self):
878
        responses = [
879
            (('something unexpected!',), '')]
880
        repo, client = self.setup_fake_client_and_repository(responses, 'path')
881
        self.assertRaises(
882
            errors.UnexpectedSmartServerResponse,
883
            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.
884
3172.5.4 by Robert Collins
Implement get_parent_map for RemoteRepository with caching, based on get_revision_graph.
885
2018.5.68 by Wouter van Heyst
Merge RemoteRepository.gather_stats.
886
class TestRepositoryGetRevisionGraph(TestRemoteRepository):
887
    
888
    def test_null_revision(self):
889
        # a null revision has the predictable result {}, we should have no wire
890
        # traffic when calling it with this argument
891
        responses = [(('notused', ), '')]
892
        transport_path = 'empty'
893
        repo, client = self.setup_fake_client_and_repository(
894
            responses, transport_path)
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
895
        result = self.applyDeprecated(one_four, repo.get_revision_graph,
896
            NULL_REVISION)
2018.5.68 by Wouter van Heyst
Merge RemoteRepository.gather_stats.
897
        self.assertEqual([], client._calls)
898
        self.assertEqual({}, result)
899
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
900
    def test_none_revision(self):
901
        # with none we want the entire graph
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
902
        r1 = u'\u0e33'.encode('utf8')
903
        r2 = u'\u0dab'.encode('utf8')
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
904
        lines = [' '.join([r2, r1]), r1]
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
905
        encoded_body = '\n'.join(lines)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
906
907
        responses = [(('ok', ), encoded_body)]
908
        transport_path = 'sinhala'
909
        repo, client = self.setup_fake_client_and_repository(
910
            responses, transport_path)
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
911
        result = self.applyDeprecated(one_four, repo.get_revision_graph)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
912
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
913
            [('call_expecting_body', 'Repository.get_revision_graph',
3104.4.2 by Andrew Bennetts
All tests passing.
914
             ('sinhala/', ''))],
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
915
            client._calls)
2625.8.1 by Robert Collins
LIBRARY API BREAKS:
916
        self.assertEqual({r1: (), r2: (r1, )}, result)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
917
918
    def test_specific_revision(self):
919
        # with a specific revision we want the graph for that
920
        # with none we want the entire graph
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
921
        r11 = u'\u0e33'.encode('utf8')
922
        r12 = u'\xc9'.encode('utf8')
923
        r2 = u'\u0dab'.encode('utf8')
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
924
        lines = [' '.join([r2, r11, r12]), r11, r12]
2018.5.106 by Andrew Bennetts
Update tests in test_remote to use utf-8 byte strings for revision IDs, rather than unicode strings.
925
        encoded_body = '\n'.join(lines)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
926
927
        responses = [(('ok', ), encoded_body)]
928
        transport_path = 'sinhala'
929
        repo, client = self.setup_fake_client_and_repository(
930
            responses, transport_path)
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
931
        result = self.applyDeprecated(one_four, repo.get_revision_graph, r2)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
932
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
933
            [('call_expecting_body', 'Repository.get_revision_graph',
3104.4.2 by Andrew Bennetts
All tests passing.
934
             ('sinhala/', r2))],
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
935
            client._calls)
2625.8.1 by Robert Collins
LIBRARY API BREAKS:
936
        self.assertEqual({r11: (), r12: (), r2: (r11, r12), }, result)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
937
938
    def test_no_such_revision(self):
939
        revid = '123'
940
        responses = [(('nosuchrevision', revid), '')]
941
        transport_path = 'sinhala'
942
        repo, client = self.setup_fake_client_and_repository(
943
            responses, transport_path)
944
        # also check that the right revision is reported in the error
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
945
        self.assertRaises(errors.NoSuchRevision,
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
946
            self.applyDeprecated, one_four, repo.get_revision_graph, revid)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
947
        self.assertEqual(
2018.5.153 by Andrew Bennetts
Rename call2 to call_expecting_body, and other small changes prompted by review.
948
            [('call_expecting_body', 'Repository.get_revision_graph',
3104.4.2 by Andrew Bennetts
All tests passing.
949
             ('sinhala/', revid))],
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
950
            client._calls)
951
952
        
953
class TestRepositoryIsShared(TestRemoteRepository):
954
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
955
    def test_is_shared(self):
956
        # ('yes', ) for Repository.is_shared -> 'True'.
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
957
        responses = [(('yes', ), )]
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
958
        transport_path = 'quack'
959
        repo, client = self.setup_fake_client_and_repository(
960
            responses, transport_path)
961
        result = repo.is_shared()
962
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
963
            [('call', 'Repository.is_shared', ('quack/',))],
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
964
            client._calls)
965
        self.assertEqual(True, result)
966
967
    def test_is_not_shared(self):
968
        # ('no', ) for Repository.is_shared -> 'False'.
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
969
        responses = [(('no', ), )]
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
970
        transport_path = 'qwack'
971
        repo, client = self.setup_fake_client_and_repository(
972
            responses, transport_path)
973
        result = repo.is_shared()
974
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
975
            [('call', 'Repository.is_shared', ('qwack/',))],
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
976
            client._calls)
977
        self.assertEqual(False, result)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
978
979
980
class TestRepositoryLockWrite(TestRemoteRepository):
981
982
    def test_lock_write(self):
983
        responses = [(('ok', 'a token'), '')]
984
        transport_path = 'quack'
985
        repo, client = self.setup_fake_client_and_repository(
986
            responses, transport_path)
987
        result = repo.lock_write()
988
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
989
            [('call', 'Repository.lock_write', ('quack/', ''))],
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
990
            client._calls)
991
        self.assertEqual('a token', result)
992
993
    def test_lock_write_already_locked(self):
994
        responses = [(('LockContention', ), '')]
995
        transport_path = 'quack'
996
        repo, client = self.setup_fake_client_and_repository(
997
            responses, transport_path)
998
        self.assertRaises(errors.LockContention, repo.lock_write)
999
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
1000
            [('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.
1001
            client._calls)
1002
1003
    def test_lock_write_unlockable(self):
1004
        responses = [(('UnlockableTransport', ), '')]
1005
        transport_path = 'quack'
1006
        repo, client = self.setup_fake_client_and_repository(
1007
            responses, transport_path)
1008
        self.assertRaises(errors.UnlockableTransport, repo.lock_write)
1009
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
1010
            [('call', 'Repository.lock_write', ('quack/', ''))],
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1011
            client._calls)
1012
1013
1014
class TestRepositoryUnlock(TestRemoteRepository):
1015
1016
    def test_unlock(self):
1017
        responses = [(('ok', 'a token'), ''),
1018
                     (('ok',), '')]
1019
        transport_path = 'quack'
1020
        repo, client = self.setup_fake_client_and_repository(
1021
            responses, transport_path)
1022
        repo.lock_write()
1023
        repo.unlock()
1024
        self.assertEqual(
3104.4.2 by Andrew Bennetts
All tests passing.
1025
            [('call', 'Repository.lock_write', ('quack/', '')),
1026
             ('call', 'Repository.unlock', ('quack/', 'a token'))],
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1027
            client._calls)
1028
1029
    def test_unlock_wrong_token(self):
1030
        # If somehow the token is wrong, unlock will raise TokenMismatch.
1031
        responses = [(('ok', 'a token'), ''),
1032
                     (('TokenMismatch',), '')]
1033
        transport_path = 'quack'
1034
        repo, client = self.setup_fake_client_and_repository(
1035
            responses, transport_path)
1036
        repo.lock_write()
1037
        self.assertRaises(errors.TokenMismatch, repo.unlock)
1038
1039
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1040
class TestRepositoryHasRevision(TestRemoteRepository):
1041
1042
    def test_none(self):
1043
        # repo.has_revision(None) should not cause any traffic.
1044
        transport_path = 'quack'
1045
        responses = None
1046
        repo, client = self.setup_fake_client_and_repository(
1047
            responses, transport_path)
1048
1049
        # The null revision is always there, so has_revision(None) == True.
3172.3.3 by Robert Collins
Missed one occurence of None -> NULL_REVISION.
1050
        self.assertEqual(True, repo.has_revision(NULL_REVISION))
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
1051
1052
        # The remote repo shouldn't be accessed.
1053
        self.assertEqual([], client._calls)
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1054
1055
1056
class TestRepositoryTarball(TestRemoteRepository):
1057
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1058
    # This is a canned tarball reponse we can validate against
2018.18.18 by Martin Pool
reformat
1059
    tarball_content = (
2018.18.23 by Martin Pool
review cleanups
1060
        'QlpoOTFBWSZTWdGkj3wAAWF/k8aQACBIB//A9+8cIX/v33AACEAYABAECEACNz'
1061
        'JqsgJJFPTSnk1A3qh6mTQAAAANPUHkagkSTEkaA09QaNAAAGgAAAcwCYCZGAEY'
1062
        'mJhMJghpiaYBUkKammSHqNMZQ0NABkNAeo0AGneAevnlwQoGzEzNVzaYxp/1Uk'
1063
        'xXzA1CQX0BJMZZLcPBrluJir5SQyijWHYZ6ZUtVqqlYDdB2QoCwa9GyWwGYDMA'
1064
        'OQYhkpLt/OKFnnlT8E0PmO8+ZNSo2WWqeCzGB5fBXZ3IvV7uNJVE7DYnWj6qwB'
1065
        'k5DJDIrQ5OQHHIjkS9KqwG3mc3t+F1+iujb89ufyBNIKCgeZBWrl5cXxbMGoMs'
1066
        'c9JuUkg5YsiVcaZJurc6KLi6yKOkgCUOlIlOpOoXyrTJjK8ZgbklReDdwGmFgt'
1067
        'dkVsAIslSVCd4AtACSLbyhLHryfb14PKegrVDba+U8OL6KQtzdM5HLjAc8/p6n'
1068
        '0lgaWU8skgO7xupPTkyuwheSckejFLK5T4ZOo0Gda9viaIhpD1Qn7JqqlKAJqC'
1069
        'QplPKp2nqBWAfwBGaOwVrz3y1T+UZZNismXHsb2Jq18T+VaD9k4P8DqE3g70qV'
1070
        'JLurpnDI6VS5oqDDPVbtVjMxMxMg4rzQVipn2Bv1fVNK0iq3Gl0hhnnHKm/egy'
1071
        'nWQ7QH/F3JFOFCQ0aSPfA='
1072
        ).decode('base64')
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1073
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1074
    def test_repository_tarball(self):
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1075
        # Test that Repository.tarball generates the right operations
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1076
        transport_path = 'repo'
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1077
        expected_responses = [(('ok',), self.tarball_content),
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1078
            ]
2018.18.14 by Martin Pool
merge hpss again; restore incorrectly removed RemoteRepository.break_lock
1079
        expected_calls = [('call_expecting_body', 'Repository.tarball',
3104.4.2 by Andrew Bennetts
All tests passing.
1080
                           ('repo/', 'bz2',),),
2018.18.7 by Martin Pool
(broken) Start addng client proxy test for Repository.tarball
1081
            ]
1082
        remote_repo, client = self.setup_fake_client_and_repository(
1083
            expected_responses, transport_path)
1084
        # Now actually ask for the tarball
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
1085
        tarball_file = remote_repo._get_tarball('bz2')
1086
        try:
1087
            self.assertEqual(expected_calls, client._calls)
1088
            self.assertEqual(self.tarball_content, tarball_file.read())
1089
        finally:
1090
            tarball_file.close()
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1091
1092
1093
class TestRemoteRepositoryCopyContent(tests.TestCaseWithTransport):
1094
    """RemoteRepository.copy_content_into optimizations"""
1095
2018.18.10 by Martin Pool
copy_content_into from Remote repositories by using temporary directories on both ends.
1096
    def test_copy_content_remote_to_local(self):
1097
        self.transport_server = server.SmartTCPServer_for_testing
1098
        src_repo = self.make_repository('repo1')
1099
        src_repo = repository.Repository.open(self.get_url('repo1'))
1100
        # At the moment the tarball-based copy_content_into can't write back
1101
        # into a smart server.  It would be good if it could upload the
1102
        # tarball; once that works we'd have to create repositories of
1103
        # different formats. -- mbp 20070410
1104
        dest_url = self.get_vfs_only_url('repo2')
1105
        dest_bzrdir = BzrDir.create(dest_url)
1106
        dest_repo = dest_bzrdir.create_repository()
1107
        self.assertFalse(isinstance(dest_repo, RemoteRepository))
1108
        self.assertTrue(isinstance(src_repo, RemoteRepository))
1109
        src_repo.copy_content_into(dest_repo)
2535.3.39 by Andrew Bennetts
Tidy some XXXs.
1110
1111
2535.3.49 by Andrew Bennetts
Rename 'Repository.fetch_revisions' smart request to 'Repository.stream_knit_data_for_revisions'.
1112
class TestRepositoryStreamKnitData(TestRemoteRepository):
2535.3.39 by Andrew Bennetts
Tidy some XXXs.
1113
1114
    def make_pack_file(self, records):
1115
        pack_file = StringIO()
1116
        pack_writer = pack.ContainerWriter(pack_file.write)
1117
        pack_writer.begin()
1118
        for bytes, names in records:
1119
            pack_writer.add_bytes_record(bytes, names)
1120
        pack_writer.end()
1121
        pack_file.seek(0)
1122
        return pack_file
1123
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
1124
    def make_pack_stream(self, records):
2535.4.18 by Andrew Bennetts
Use pack.ContainerSerialiser to remove some nasty cruft.
1125
        pack_serialiser = pack.ContainerSerialiser()
1126
        yield pack_serialiser.begin()
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
1127
        for bytes, names in records:
2535.4.18 by Andrew Bennetts
Use pack.ContainerSerialiser to remove some nasty cruft.
1128
            yield pack_serialiser.bytes_record(bytes, names)
1129
        yield pack_serialiser.end()
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
1130
2535.3.39 by Andrew Bennetts
Tidy some XXXs.
1131
    def test_bad_pack_from_server(self):
2535.3.50 by Andrew Bennetts
Use tuple names in data streams rather than concatenated strings.
1132
        """A response with invalid data (e.g. it has a record with multiple
1133
        names) triggers an exception.
1134
        
1135
        Not all possible errors will be caught at this stage, but obviously
1136
        malformed data should be.
1137
        """
1138
        record = ('bytes', [('name1',), ('name2',)])
2535.4.2 by Andrew Bennetts
Nasty hackery to make stream_knit_data_for_revisions response use streaming.
1139
        pack_stream = self.make_pack_stream([record])
1140
        responses = [(('ok',), pack_stream), ]
2535.3.39 by Andrew Bennetts
Tidy some XXXs.
1141
        transport_path = 'quack'
1142
        repo, client = self.setup_fake_client_and_repository(
1143
            responses, transport_path)
3184.1.9 by Robert Collins
* ``Repository.get_data_stream`` is now deprecated in favour of
1144
        search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
1145
        stream = repo.get_data_stream_for_search(search)
2535.3.39 by Andrew Bennetts
Tidy some XXXs.
1146
        self.assertRaises(errors.SmartProtocolError, list, stream)
1147
    
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
1148
    def test_backwards_compatibility(self):
1149
        """If the server doesn't recognise this request, fallback to VFS."""
1150
        responses = [
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.
1151
            (('unknown verb', 'Repository.stream_revisions_chunked'), '')]
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
1152
        repo, client = self.setup_fake_client_and_repository(
1153
            responses, 'path')
1154
        self.mock_called = False
1155
        repo._real_repository = MockRealRepository(self)
3184.1.9 by Robert Collins
* ``Repository.get_data_stream`` is now deprecated in favour of
1156
        search = graph.SearchResult(set(['revid']), set(), 1, set(['revid']))
1157
        repo.get_data_stream_for_search(search)
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
1158
        self.assertTrue(self.mock_called)
1159
        self.failIf(client.expecting_body,
1160
            "The protocol has been left in an unclean state that will cause "
1161
            "TooManyConcurrentRequests errors.")
1162
1163
1164
class MockRealRepository(object):
1165
    """Helper class for TestRepositoryStreamKnitData.test_unknown_method."""
1166
1167
    def __init__(self, test):
1168
        self.test = test
1169
3184.1.9 by Robert Collins
* ``Repository.get_data_stream`` is now deprecated in favour of
1170
    def get_data_stream_for_search(self, search):
1171
        self.test.assertEqual(set(['revid']), search.get_keys())
2535.3.68 by Andrew Bennetts
Backwards compatibility for new smart method.
1172
        self.test.mock_called = True
1173
1174