/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2018.18.1 by Martin Pool
Add stub Repository.tarball smart method
1
# Copyright (C) 2006, 2007 Canonical Ltd
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
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
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
17
"""Tests for the smart wire/domain protocol.
18
19
This module contains tests for the domain-level smart requests and responses,
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
20
such as the 'Branch.lock_write' request. Many of these use specific disk
21
formats to exercise calls that only make sense for formats with specific
22
properties.
2748.4.1 by Andrew Bennetts
Implement a ChunkedBodyDecoder.
23
24
Tests for low-level protocol encoding are found in test_smart_transport.
25
"""
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
26
3211.5.2 by Robert Collins
Change RemoteRepository.get_parent_map to use bz2 not gzip for compression.
27
import bz2
2692.1.18 by Andrew Bennetts
Merge from bzr.dev.
28
from cStringIO import StringIO
2018.18.2 by Martin Pool
smart method Repository.tarball actually returns the tarball
29
import tarfile
30
2692.1.2 by Andrew Bennetts
Merge from bzr.dev.
31
from bzrlib import (
32
    bzrdir,
33
    errors,
34
    pack,
35
    smart,
36
    tests,
37
    urlutils,
38
    )
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
39
from bzrlib.branch import BranchReferenceFormat
2692.1.22 by Andrew Bennetts
Reinstate imports needed to run test_smart alone.
40
import bzrlib.smart.branch
41
import bzrlib.smart.bzrdir
42
import bzrlib.smart.repository
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
43
from bzrlib.smart.request import (
44
    FailedSmartServerResponse,
45
    SmartServerRequest,
46
    SmartServerResponse,
47
    SuccessfulSmartServerResponse,
48
    )
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
49
from bzrlib.tests import (
50
    iter_suite_tests,
51
    split_suite_by_re,
52
    TestScenarioApplier,
53
    )
2692.1.3 by Andrew Bennetts
Fix imports so that tests in test_smart.py can be run alone.
54
from bzrlib.transport import chroot, get_transport
2535.3.15 by Andrew Bennetts
Add KnitVersionedFile.get_stream_as_bytes, start smart implementation of RemoteRepository.get_data_stream.
55
from bzrlib.util import bencode
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
56
57
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
58
def load_tests(standard_tests, module, loader):
59
    """Multiply tests version and protocol consistency."""
60
    # FindRepository tests.
61
    bzrdir_mod = bzrlib.smart.bzrdir
62
    applier = TestScenarioApplier()
63
    applier.scenarios = [
64
        ("find_repository", {
65
            "_request_class":bzrdir_mod.SmartServerRequestFindRepositoryV1}),
66
        ("find_repositoryV2", {
67
            "_request_class":bzrdir_mod.SmartServerRequestFindRepositoryV2}),
68
        ]
69
    to_adapt, result = split_suite_by_re(standard_tests,
70
        "TestSmartServerRequestFindRepository")
71
    v2_only, v1_and_2 = split_suite_by_re(to_adapt,
72
        "_v2")
73
    for test in iter_suite_tests(v1_and_2):
74
        result.addTests(applier.adapt(test))
75
    del applier.scenarios[0]
76
    for test in iter_suite_tests(v2_only):
77
        result.addTests(applier.adapt(test))
78
    return result
79
80
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
81
class TestCaseWithChrootedTransport(tests.TestCaseWithTransport):
82
83
    def setUp(self):
84
        tests.TestCaseWithTransport.setUp(self)
85
        self._chroot_server = None
86
87
    def get_transport(self, relpath=None):
88
        if self._chroot_server is None:
89
            backing_transport = tests.TestCaseWithTransport.get_transport(self)
90
            self._chroot_server = chroot.ChrootServer(backing_transport)
91
            self._chroot_server.setUp()
92
            self.addCleanup(self._chroot_server.tearDown)
93
        t = get_transport(self._chroot_server.get_url())
94
        if relpath is not None:
95
            t = t.clone(relpath)
96
        return t
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
97
98
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
99
class TestCaseWithSmartMedium(tests.TestCaseWithTransport):
100
101
    def setUp(self):
102
        super(TestCaseWithSmartMedium, self).setUp()
103
        # We're allowed to set  the transport class here, so that we don't use
104
        # the default or a parameterized class, but rather use the
105
        # TestCaseWithTransport infrastructure to set up a smart server and
106
        # transport.
3245.4.28 by Andrew Bennetts
Remove another XXX, and include test ID in smart server thread names.
107
        self.transport_server = self.make_transport_server
108
109
    def make_transport_server(self):
110
        return smart.server.SmartTCPServer_for_testing('-' + self.id())
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
111
112
    def get_smart_medium(self):
113
        """Get a smart medium to use in tests."""
114
        return self.get_transport().get_smart_medium()
115
116
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
117
class TestSmartServerResponse(tests.TestCase):
118
119
    def test__eq__(self):
120
        self.assertEqual(SmartServerResponse(('ok', )),
121
            SmartServerResponse(('ok', )))
122
        self.assertEqual(SmartServerResponse(('ok', ), 'body'),
123
            SmartServerResponse(('ok', ), 'body'))
124
        self.assertNotEqual(SmartServerResponse(('ok', )),
125
            SmartServerResponse(('notok', )))
126
        self.assertNotEqual(SmartServerResponse(('ok', ), 'body'),
127
            SmartServerResponse(('ok', )))
2018.5.41 by Robert Collins
Fix SmartServerResponse.__eq__ to handle None.
128
        self.assertNotEqual(None,
129
            SmartServerResponse(('ok', )))
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
130
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
131
    def test__str__(self):
132
        """SmartServerResponses can be stringified."""
133
        self.assertEqual(
134
            "<SmartServerResponse status=OK args=('args',) body='body'>",
135
            str(SuccessfulSmartServerResponse(('args',), 'body')))
136
        self.assertEqual(
137
            "<SmartServerResponse status=ERR args=('args',) body='body'>",
138
            str(FailedSmartServerResponse(('args',), 'body')))
139
140
141
class TestSmartServerRequest(tests.TestCaseWithMemoryTransport):
142
143
    def test_translate_client_path(self):
144
        transport = self.get_transport()
145
        request = SmartServerRequest(transport, 'foo/')
146
        self.assertEqual('./', request.translate_client_path('foo/'))
147
        self.assertRaises(
148
            errors.InvalidURLJoin, request.translate_client_path, 'foo/..')
149
        self.assertRaises(
150
            errors.PathNotChild, request.translate_client_path, '/')
151
        self.assertRaises(
152
            errors.PathNotChild, request.translate_client_path, 'bar/')
153
        self.assertEqual('./baz', request.translate_client_path('foo/baz'))
154
155
    def test_transport_from_client_path(self):
156
        transport = self.get_transport()
157
        request = SmartServerRequest(transport, 'foo/')
158
        self.assertEqual(
159
            transport.base,
160
            request.transport_from_client_path('foo/').base)
161
162
163
class TestSmartServerRequestFindRepository(tests.TestCaseWithMemoryTransport):
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
164
    """Tests for BzrDir.find_repository."""
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
165
166
    def test_no_repository(self):
167
        """When there is no repository to be found, ('norepository', ) is returned."""
168
        backing = self.get_transport()
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
169
        request = self._request_class(backing)
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
170
        self.make_bzrdir('.')
171
        self.assertEqual(SmartServerResponse(('norepository', )),
2692.1.19 by Andrew Bennetts
Tweak for consistency suggested by John's review.
172
            request.execute(''))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
173
174
    def test_nonshared_repository(self):
175
        # nonshared repositorys only allow 'find' to return a handle when the 
176
        # path the repository is being searched on is the same as that that 
177
        # the repository is at.
178
        backing = self.get_transport()
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
179
        request = self._request_class(backing)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
180
        result = self._make_repository_and_result()
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
181
        self.assertEqual(result, request.execute(''))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
182
        self.make_bzrdir('subdir')
183
        self.assertEqual(SmartServerResponse(('norepository', )),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
184
            request.execute('subdir'))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
185
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
186
    def _make_repository_and_result(self, shared=False, format=None):
187
        """Convenience function to setup a repository.
188
189
        :result: The SmartServerResponse to expect when opening it.
190
        """
191
        repo = self.make_repository('.', shared=shared, format=format)
192
        if repo.supports_rich_root():
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
193
            rich_root = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
194
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
195
            rich_root = 'no'
2018.5.138 by Robert Collins
Merge bzr.dev.
196
        if repo._format.supports_tree_reference:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
197
            subtrees = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
198
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
199
            subtrees = 'no'
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
200
        if (smart.bzrdir.SmartServerRequestFindRepositoryV2 ==
201
            self._request_class):
202
            # All tests so far are on formats, and for non-external
203
            # repositories.
204
            return SuccessfulSmartServerResponse(
205
                ('ok', '', rich_root, subtrees, 'no'))
206
        else:
207
            return SuccessfulSmartServerResponse(('ok', '', rich_root, subtrees))
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
208
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
209
    def test_shared_repository(self):
210
        """When there is a shared repository, we get 'ok', 'relpath-to-repo'."""
211
        backing = self.get_transport()
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
212
        request = self._request_class(backing)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
213
        result = self._make_repository_and_result(shared=True)
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
214
        self.assertEqual(result, request.execute(''))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
215
        self.make_bzrdir('subdir')
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
216
        result2 = SmartServerResponse(result.args[0:1] + ('..', ) + result.args[2:])
217
        self.assertEqual(result2,
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
218
            request.execute('subdir'))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
219
        self.make_bzrdir('subdir/deeper')
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
220
        result3 = SmartServerResponse(result.args[0:1] + ('../..', ) + result.args[2:])
221
        self.assertEqual(result3,
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
222
            request.execute('subdir/deeper'))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
223
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
224
    def test_rich_root_and_subtree_encoding(self):
225
        """Test for the format attributes for rich root and subtree support."""
226
        backing = self.get_transport()
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
227
        request = self._request_class(backing)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
228
        result = self._make_repository_and_result(format='dirstate-with-subtree')
229
        # check the test will be valid
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
230
        self.assertEqual('yes', result.args[2])
231
        self.assertEqual('yes', result.args[3])
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
232
        self.assertEqual(result, request.execute(''))
233
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
234
    def test_supports_external_lookups_no_v2(self):
235
        """Test for the supports_external_lookups attribute."""
236
        backing = self.get_transport()
237
        request = self._request_class(backing)
238
        result = self._make_repository_and_result(format='dirstate-with-subtree')
239
        # check the test will be valid
240
        self.assertEqual('no', result.args[4])
2692.1.24 by Andrew Bennetts
Merge from bzr.dev.
241
        self.assertEqual(result, request.execute(''))
242
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
243
244
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithMemoryTransport):
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
245
246
    def test_empty_dir(self):
247
        """Initializing an empty dir should succeed and do it."""
248
        backing = self.get_transport()
249
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
250
        self.assertEqual(SmartServerResponse(('ok', )),
2692.1.20 by Andrew Bennetts
Tweak for consistency suggested by John's review.
251
            request.execute(''))
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
252
        made_dir = bzrdir.BzrDir.open_from_transport(backing)
253
        # no branch, tree or repository is expected with the current 
254
        # default formart.
255
        self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
256
        self.assertRaises(errors.NotBranchError, made_dir.open_branch)
257
        self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
258
259
    def test_missing_dir(self):
260
        """Initializing a missing directory should fail like the bzrdir api."""
261
        backing = self.get_transport()
262
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
263
        self.assertRaises(errors.NoSuchFile,
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
264
            request.execute, 'subdir')
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
265
266
    def test_initialized_dir(self):
267
        """Initializing an extant bzrdir should fail like the bzrdir api."""
268
        backing = self.get_transport()
269
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
270
        self.make_bzrdir('subdir')
271
        self.assertRaises(errors.FileExists,
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
272
            request.execute, 'subdir')
273
274
275
class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
276
277
    def test_no_branch(self):
278
        """When there is no branch, ('nobranch', ) is returned."""
279
        backing = self.get_transport()
280
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
281
        self.make_bzrdir('.')
282
        self.assertEqual(SmartServerResponse(('nobranch', )),
2692.1.20 by Andrew Bennetts
Tweak for consistency suggested by John's review.
283
            request.execute(''))
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
284
285
    def test_branch(self):
286
        """When there is a branch, 'ok' is returned."""
287
        backing = self.get_transport()
288
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
289
        self.make_branch('.')
290
        self.assertEqual(SmartServerResponse(('ok', '')),
2692.1.20 by Andrew Bennetts
Tweak for consistency suggested by John's review.
291
            request.execute(''))
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
292
293
    def test_branch_reference(self):
294
        """When there is a branch reference, the reference URL is returned."""
295
        backing = self.get_transport()
296
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
297
        branch = self.make_branch('branch')
298
        checkout = branch.create_checkout('reference',lightweight=True)
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
299
        reference_url = BranchReferenceFormat().get_reference(checkout.bzrdir)
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
300
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
301
        self.assertEqual(SmartServerResponse(('ok', reference_url)),
2692.1.20 by Andrew Bennetts
Tweak for consistency suggested by John's review.
302
            request.execute('reference'))
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
303
304
305
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
306
307
    def test_empty(self):
308
        """For an empty branch, the body is empty."""
309
        backing = self.get_transport()
310
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
311
        self.make_branch('.')
312
        self.assertEqual(SmartServerResponse(('ok', ), ''),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
313
            request.execute(''))
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
314
315
    def test_not_empty(self):
316
        """For a non-empty branch, the body is empty."""
317
        backing = self.get_transport()
318
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
319
        tree = self.make_branch_and_memory_tree('.')
320
        tree.lock_write()
321
        tree.add('')
322
        r1 = tree.commit('1st commit')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
323
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
324
        tree.unlock()
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
325
        self.assertEqual(
326
            SmartServerResponse(('ok', ), ('\x00'.join([r1, r2]))),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
327
            request.execute(''))
328
329
330
class TestSmartServerBranchRequest(tests.TestCaseWithMemoryTransport):
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
331
332
    def test_no_branch(self):
333
        """When there is a bzrdir and no branch, NotBranchError is raised."""
334
        backing = self.get_transport()
335
        request = smart.branch.SmartServerBranchRequest(backing)
336
        self.make_bzrdir('.')
337
        self.assertRaises(errors.NotBranchError,
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
338
            request.execute, '')
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
339
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
340
    def test_branch_reference(self):
341
        """When there is a branch reference, NotBranchError is raised."""
342
        backing = self.get_transport()
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
343
        request = smart.branch.SmartServerBranchRequest(backing)
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
344
        branch = self.make_branch('branch')
345
        checkout = branch.create_checkout('reference',lightweight=True)
346
        self.assertRaises(errors.NotBranchError,
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
347
            request.execute, 'checkout')
348
349
350
class TestSmartServerBranchRequestLastRevisionInfo(tests.TestCaseWithMemoryTransport):
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
351
352
    def test_empty(self):
2018.5.170 by Andrew Bennetts
Use 'null:' instead of '' to mean NULL_REVISION on the wire.
353
        """For an empty branch, the result is ('ok', '0', 'null:')."""
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
354
        backing = self.get_transport()
355
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
356
        self.make_branch('.')
2018.5.170 by Andrew Bennetts
Use 'null:' instead of '' to mean NULL_REVISION on the wire.
357
        self.assertEqual(SmartServerResponse(('ok', '0', 'null:')),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
358
            request.execute(''))
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
359
360
    def test_not_empty(self):
361
        """For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
362
        backing = self.get_transport()
363
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
364
        tree = self.make_branch_and_memory_tree('.')
365
        tree.lock_write()
366
        tree.add('')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
367
        rev_id_utf8 = u'\xc8'.encode('utf-8')
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
368
        r1 = tree.commit('1st commit')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
369
        r2 = tree.commit('2nd commit', rev_id=rev_id_utf8)
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
370
        tree.unlock()
371
        self.assertEqual(
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
372
            SmartServerResponse(('ok', '2', rev_id_utf8)),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
373
            request.execute(''))
374
375
376
class TestSmartServerBranchRequestGetConfigFile(tests.TestCaseWithMemoryTransport):
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
377
378
    def test_default(self):
379
        """With no file, we get empty content."""
380
        backing = self.get_transport()
381
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
382
        branch = self.make_branch('.')
383
        # there should be no file by default
384
        content = ''
385
        self.assertEqual(SmartServerResponse(('ok', ), content),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
386
            request.execute(''))
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
387
388
    def test_with_content(self):
389
        # SmartServerBranchGetConfigFile should return the content from
390
        # branch.control_files.get('branch.conf') for now - in the future it may
391
        # perform more complex processing. 
392
        backing = self.get_transport()
393
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
394
        branch = self.make_branch('.')
395
        branch.control_files.put_utf8('branch.conf', 'foo bar baz')
396
        self.assertEqual(SmartServerResponse(('ok', ), 'foo bar baz'),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
397
            request.execute(''))
398
399
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
400
class SetLastRevisionCommonTests(object):
401
    """Mixin class for tests common to Branch.set_last_revision* variants."""
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
402
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
403
    request_class = None
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
404
405
    def test_not_present_revision_id(self):
406
        backing = self.get_transport()
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
407
        request = self.request_class(backing)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
408
        b = self.make_branch('.')
2018.5.144 by Andrew Bennetts
Fix four tests I broke with the Branch.lock_write changes.
409
        branch_token = b.lock_write()
410
        repo_token = b.repository.lock_write()
411
        b.repository.unlock()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
412
        try:
413
            revision_id = 'non-existent revision'
414
            self.assertEqual(
415
                SmartServerResponse(('NoSuchRevision', revision_id)),
416
                request.execute(
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
417
                    '', branch_token, repo_token,
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
418
                    revision_id))
419
        finally:
420
            b.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
421
3441.5.6 by Andrew Bennetts
Greatly simplify RemoteBranch.update_revisions. Still needs more tests.
422
423
class TestSmartServerBranchRequestSetLastRevision(
424
    tests.TestCaseWithMemoryTransport, SetLastRevisionCommonTests):
425
426
    request_class = smart.branch.SmartServerBranchRequestSetLastRevision
427
428
    def test_revision_id_present2(self):
429
        backing = self.get_transport()
430
        request = self.request_class(backing)
431
        tree = self.make_branch_and_memory_tree('.')
432
        tree.lock_write()
433
        tree.add('')
434
        rev_id_utf8 = u'\xc8'.encode('utf-8')
435
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
436
        r2 = tree.commit('2nd commit')
437
        tree.unlock()
438
        tree.branch.set_revision_history([])
439
        branch_token = tree.branch.lock_write()
440
        repo_token = tree.branch.repository.lock_write()
441
        tree.branch.repository.unlock()
442
        try:
443
            self.assertEqual(
444
                SuccessfulSmartServerResponse(('ok',)),
445
                request.execute(
446
                    '', branch_token, repo_token,
447
                    rev_id_utf8))
448
            self.assertEqual([rev_id_utf8], tree.branch.revision_history())
449
        finally:
450
            tree.branch.unlock()
451
452
453
    def test_revision_id_previous(self):
454
        backing = self.get_transport()
455
        request = self.request_class(backing)
456
        tree = self.make_branch_and_memory_tree('.')
457
        tree.lock_write()
458
        tree.add('')
459
        rev_id_utf8 = u'\xc8'.encode('utf-8')
460
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
461
        r2 = tree.commit('2nd commit')
462
        tree.unlock()
463
        branch_token = tree.branch.lock_write()
464
        repo_token = tree.branch.repository.lock_write()
465
        tree.branch.repository.unlock()
466
        try:
467
            self.assertEqual(
468
                SuccessfulSmartServerResponse(('ok',)),
469
                request.execute(
470
                    '', branch_token, repo_token,
471
                    rev_id_utf8))
472
            self.assertEqual([rev_id_utf8], tree.branch.revision_history())
473
        finally:
474
            tree.branch.unlock()
475
476
477
class TestSmartServerBranchRequestSetLastRevisionDescendant(
478
    tests.TestCaseWithMemoryTransport, SetLastRevisionCommonTests):
479
480
    request_class = \
481
        smart.branch.SmartServerBranchRequestSetLastRevisionDescendant
482
483
    def test_empty(self):
484
        backing = self.get_transport()
485
        request = self.request_class(backing)
486
        b = self.make_branch('.')
487
        branch_token = b.lock_write()
488
        repo_token = b.repository.lock_write()
489
        b.repository.unlock()
490
        try:
491
            self.assertEqual(SmartServerResponse(('ok',)),
492
                request.execute(
493
                    '', branch_token, repo_token,
494
                    'null:'))
495
        finally:
496
            b.unlock()
497
498
    def test_empty(self):
499
        backing = self.get_transport()
500
        request = self.request_class(backing)
501
        b = self.make_branch('.')
502
        branch_token = b.lock_write()
503
        repo_token = b.repository.lock_write()
504
        b.repository.unlock()
505
        try:
506
            self.assertEqual(SmartServerResponse(('ok', 0)),
507
                request.execute(
508
                    '', branch_token, repo_token,
509
                    'null:'))
510
        finally:
511
            b.unlock()
512
513
    def test_revision_id_present2(self):
514
        backing = self.get_transport()
515
        request = self.request_class(backing)
516
        tree = self.make_branch_and_memory_tree('.')
517
        tree.lock_write()
518
        tree.add('')
519
        rev_id_utf8 = u'\xc8'.encode('utf-8')
520
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
521
        r2 = tree.commit('2nd commit')
522
        tree.unlock()
523
        tree.branch.set_revision_history([])
524
        branch_token = tree.branch.lock_write()
525
        repo_token = tree.branch.repository.lock_write()
526
        tree.branch.repository.unlock()
527
        try:
528
            self.assertEqual(
529
                SuccessfulSmartServerResponse(('ok', 1)),
530
                request.execute(
531
                    '', branch_token, repo_token,
532
                    rev_id_utf8))
533
            self.assertEqual([rev_id_utf8], tree.branch.revision_history())
534
        finally:
535
            tree.branch.unlock()
536
537
    def test_revision_id_previous(self):
538
        backing = self.get_transport()
539
        request = self.request_class(backing)
540
        tree = self.make_branch_and_memory_tree('.')
541
        tree.lock_write()
542
        tree.add('')
543
        rev_id_utf8 = u'\xc8'.encode('utf-8')
544
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
545
        r2 = tree.commit('2nd commit', rev_id='second-rev')
546
        tree.unlock()
547
        branch_token = tree.branch.lock_write()
548
        repo_token = tree.branch.repository.lock_write()
549
        tree.branch.repository.unlock()
550
        try:
551
            self.assertEqual(
552
                FailedSmartServerResponse(('NotDescendant',)),
553
                request.execute(
554
                    '', branch_token, repo_token,
555
                    rev_id_utf8))
556
            self.assertEqual('second-rev', tree.branch.last_revision())
557
        finally:
558
            tree.branch.unlock()
559
560
    def test_branch_diverged(self):
561
        backing = self.get_transport()
562
        request = self.request_class(backing)
563
        tree = self.make_branch_and_memory_tree('.')
564
        tree.lock_write()
565
        tree.add('')
566
        r1 = tree.commit('1st commit')
567
        revno_1, revid_1 = tree.branch.last_revision_info()
568
        r2 = tree.commit('2nd commit', rev_id='child-1')
569
        # Undo the second commit
570
        tree.branch.set_last_revision_info(revno_1, revid_1)
571
        tree.set_parent_ids([revid_1])
572
        # Make a new second commit, child-2.  child-2 has diverged from
573
        # child-1.
574
        new_r2 = tree.commit('2nd commit', rev_id='child-2')
575
        tree.unlock()
576
        branch_token = tree.branch.lock_write()
577
        repo_token = tree.branch.repository.lock_write()
578
        tree.branch.repository.unlock()
579
        try:
580
            self.assertEqual(
581
                FailedSmartServerResponse(('NotDescendant',)),
582
                request.execute(
583
                    '', branch_token, repo_token,
584
                    'child-1'))
585
            self.assertEqual('child-2', tree.branch.last_revision())
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
586
        finally:
587
            tree.branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
588
589
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
590
class TestSmartServerBranchRequestSetLastRevisionInfo(tests.TestCaseWithTransport):
591
3297.4.3 by Andrew Bennetts
Add more tests, handle NoSuchRevision in case the remote branch's format can raise it.
592
    def lock_branch(self, branch):
593
        branch_token = branch.lock_write()
594
        repo_token = branch.repository.lock_write()
595
        branch.repository.unlock()
596
        self.addCleanup(branch.unlock)
597
        return branch_token, repo_token
598
599
    def make_locked_branch(self, format=None):
600
        branch = self.make_branch('.', format=format)
601
        branch_token, repo_token = self.lock_branch(branch)
602
        return branch, branch_token, repo_token
603
604
    def test_empty(self):
605
        """An empty branch can have its last revision set to 'null:'."""
606
        b, branch_token, repo_token = self.make_locked_branch()
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
607
        backing = self.get_transport()
3297.4.3 by Andrew Bennetts
Add more tests, handle NoSuchRevision in case the remote branch's format can raise it.
608
        request = smart.branch.SmartServerBranchRequestSetLastRevisionInfo(
609
            backing)
610
        response = request.execute('', branch_token, repo_token, '0', 'null:')
611
        self.assertEqual(SmartServerResponse(('ok',)), response)
612
613
    def assertBranchLastRevisionInfo(self, expected_info, branch_relpath):
614
        branch = bzrdir.BzrDir.open(branch_relpath).open_branch()
615
        self.assertEqual(expected_info, branch.last_revision_info())
616
617
    def test_branch_revision_info_is_updated(self):
618
        """This method really does update the branch last revision info."""
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
619
        tree = self.make_branch_and_memory_tree('.')
620
        tree.lock_write()
621
        tree.add('')
3297.4.3 by Andrew Bennetts
Add more tests, handle NoSuchRevision in case the remote branch's format can raise it.
622
        tree.commit('First commit', rev_id='revision-1')
623
        tree.commit('Second commit', rev_id='revision-2')
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
624
        tree.unlock()
3297.4.3 by Andrew Bennetts
Add more tests, handle NoSuchRevision in case the remote branch's format can raise it.
625
        branch = tree.branch
626
627
        branch_token, repo_token = self.lock_branch(branch)
628
        backing = self.get_transport()
629
        request = smart.branch.SmartServerBranchRequestSetLastRevisionInfo(
630
            backing)
631
        self.assertBranchLastRevisionInfo((2, 'revision-2'), '.')
632
        response = request.execute(
633
            '', branch_token, repo_token, '1', 'revision-1')
634
        self.assertEqual(SmartServerResponse(('ok',)), response)
635
        self.assertBranchLastRevisionInfo((1, 'revision-1'), '.')
636
637
    def test_not_present_revid(self):
638
        """Some branch formats will check that the revision is present in the
639
        repository.  When that check fails, a NoSuchRevision error is returned
640
        to the client.
641
        """
642
        # Make a knit format branch, because that format checks the values
643
        # given to set_last_revision_info.
644
        b, branch_token, repo_token = self.make_locked_branch(format='knit')
645
        backing = self.get_transport()
646
        request = smart.branch.SmartServerBranchRequestSetLastRevisionInfo(
647
            backing)
648
        response = request.execute(
649
            '', branch_token, repo_token, '1', 'not-present')
650
        self.assertEqual(
651
            SmartServerResponse(('NoSuchRevision', 'not-present')), response)
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
652
653
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
654
class TestSmartServerBranchRequestLockWrite(tests.TestCaseWithMemoryTransport):
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
655
656
    def setUp(self):
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
657
        tests.TestCaseWithMemoryTransport.setUp(self)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
658
659
    def test_lock_write_on_unlocked_branch(self):
660
        backing = self.get_transport()
661
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
662
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
663
        repository = branch.repository
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
664
        response = request.execute('')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
665
        branch_nonce = branch.control_files._lock.peek().get('nonce')
666
        repository_nonce = repository.control_files._lock.peek().get('nonce')
667
        self.assertEqual(
668
            SmartServerResponse(('ok', branch_nonce, repository_nonce)),
669
            response)
670
        # The branch (and associated repository) is now locked.  Verify that
671
        # with a new branch object.
672
        new_branch = repository.bzrdir.open_branch()
673
        self.assertRaises(errors.LockContention, new_branch.lock_write)
674
675
    def test_lock_write_on_locked_branch(self):
676
        backing = self.get_transport()
677
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
678
        branch = self.make_branch('.')
679
        branch.lock_write()
680
        branch.leave_lock_in_place()
681
        branch.unlock()
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
682
        response = request.execute('')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
683
        self.assertEqual(
684
            SmartServerResponse(('LockContention',)), response)
685
686
    def test_lock_write_with_tokens_on_locked_branch(self):
687
        backing = self.get_transport()
688
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
689
        branch = self.make_branch('.', format='knit')
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
690
        branch_token = branch.lock_write()
691
        repo_token = branch.repository.lock_write()
692
        branch.repository.unlock()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
693
        branch.leave_lock_in_place()
694
        branch.repository.leave_lock_in_place()
695
        branch.unlock()
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
696
        response = request.execute('',
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
697
                                   branch_token, repo_token)
698
        self.assertEqual(
699
            SmartServerResponse(('ok', branch_token, repo_token)), response)
700
701
    def test_lock_write_with_mismatched_tokens_on_locked_branch(self):
702
        backing = self.get_transport()
703
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
704
        branch = self.make_branch('.', format='knit')
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
705
        branch_token = branch.lock_write()
706
        repo_token = branch.repository.lock_write()
707
        branch.repository.unlock()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
708
        branch.leave_lock_in_place()
709
        branch.repository.leave_lock_in_place()
710
        branch.unlock()
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
711
        response = request.execute('',
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
712
                                   branch_token+'xxx', repo_token)
713
        self.assertEqual(
714
            SmartServerResponse(('TokenMismatch',)), response)
715
716
    def test_lock_write_on_locked_repo(self):
717
        backing = self.get_transport()
718
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
719
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
720
        branch.repository.lock_write()
721
        branch.repository.leave_lock_in_place()
722
        branch.repository.unlock()
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
723
        response = request.execute('')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
724
        self.assertEqual(
725
            SmartServerResponse(('LockContention',)), response)
726
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.
727
    def test_lock_write_on_readonly_transport(self):
728
        backing = self.get_readonly_transport()
729
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
730
        branch = self.make_branch('.')
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
731
        root = self.get_transport().clone('/')
732
        path = urlutils.relative_url(root.base, self.get_transport().base)
733
        response = request.execute(path)
2872.5.3 by Martin Pool
Pass back LockFailed from smart server lock methods
734
        error_name, lock_str, why_str = response.args
735
        self.assertFalse(response.is_successful())
736
        self.assertEqual('LockFailed', error_name)
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.
737
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
738
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
739
class TestSmartServerBranchRequestUnlock(tests.TestCaseWithMemoryTransport):
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
740
741
    def setUp(self):
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
742
        tests.TestCaseWithMemoryTransport.setUp(self)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
743
744
    def test_unlock_on_locked_branch_and_repo(self):
745
        backing = self.get_transport()
746
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
747
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
748
        # Lock the branch
2018.5.142 by Andrew Bennetts
Change Branch.lock_token to only accept and receive the branch lock token (rather than the branch and repo lock tokens).
749
        branch_token = branch.lock_write()
750
        repo_token = branch.repository.lock_write()
751
        branch.repository.unlock()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
752
        # Unlock the branch (and repo) object, leaving the physical locks
753
        # in place.
754
        branch.leave_lock_in_place()
755
        branch.repository.leave_lock_in_place()
756
        branch.unlock()
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
757
        response = request.execute('',
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
758
                                   branch_token, repo_token)
759
        self.assertEqual(
760
            SmartServerResponse(('ok',)), response)
761
        # The branch is now unlocked.  Verify that with a new branch
762
        # object.
763
        new_branch = branch.bzrdir.open_branch()
764
        new_branch.lock_write()
765
        new_branch.unlock()
766
767
    def test_unlock_on_unlocked_branch_unlocked_repo(self):
768
        backing = self.get_transport()
769
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
770
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
771
        response = request.execute(
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
772
            '', 'branch token', 'repo token')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
773
        self.assertEqual(
774
            SmartServerResponse(('TokenMismatch',)), response)
775
776
    def test_unlock_on_unlocked_branch_locked_repo(self):
777
        backing = self.get_transport()
778
        request = smart.branch.SmartServerBranchRequestUnlock(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
779
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
780
        # Lock the repository.
781
        repo_token = branch.repository.lock_write()
782
        branch.repository.leave_lock_in_place()
783
        branch.repository.unlock()
784
        # Issue branch lock_write request on the unlocked branch (with locked
785
        # repo).
786
        response = request.execute(
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
787
            '', 'branch token', repo_token)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
788
        self.assertEqual(
789
            SmartServerResponse(('TokenMismatch',)), response)
790
791
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
792
class TestSmartServerRepositoryRequest(tests.TestCaseWithMemoryTransport):
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
793
794
    def test_no_repository(self):
795
        """Raise NoRepositoryPresent when there is a bzrdir and no repo."""
796
        # we test this using a shared repository above the named path,
797
        # thus checking the right search logic is used - that is, that
798
        # its the exact path being looked at and the server is not
799
        # searching.
800
        backing = self.get_transport()
2018.5.58 by Wouter van Heyst
Small test fixes to reflect naming and documentation
801
        request = smart.repository.SmartServerRepositoryRequest(backing)
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
802
        self.make_repository('.', shared=True)
803
        self.make_bzrdir('subdir')
804
        self.assertRaises(errors.NoRepositoryPresent,
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
805
            request.execute, 'subdir')
806
807
3441.5.4 by Andrew Bennetts
Fix test failures, and add some tests for the remote graph heads RPC.
808
class TestSmartServerRepositoryGetParentMap(tests.TestCaseWithMemoryTransport):
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.
809
3211.5.3 by Robert Collins
Adjust size of batch and change gzip comments to bzip2.
810
    def test_trivial_bzipped(self):
811
        # This tests that the wire encoding is actually bzipped
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.
812
        backing = self.get_transport()
813
        request = smart.repository.SmartServerRepositoryGetParentMap(backing)
814
        tree = self.make_branch_and_memory_tree('.')
815
816
        self.assertEqual(None,
2692.1.24 by Andrew Bennetts
Merge from bzr.dev.
817
            request.execute('', 'missing-id'))
3211.5.3 by Robert Collins
Adjust size of batch and change gzip comments to bzip2.
818
        # Note that it returns a body (of '' bzipped).
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.
819
        self.assertEqual(
3211.5.2 by Robert Collins
Change RemoteRepository.get_parent_map to use bz2 not gzip for compression.
820
            SuccessfulSmartServerResponse(('ok', ), bz2.compress('')),
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.
821
            request.do_body('\n\n0\n'))
822
823
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
824
class TestSmartServerRepositoryGetRevisionGraph(tests.TestCaseWithMemoryTransport):
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
825
826
    def test_none_argument(self):
827
        backing = self.get_transport()
828
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
829
        tree = self.make_branch_and_memory_tree('.')
830
        tree.lock_write()
831
        tree.add('')
832
        r1 = tree.commit('1st commit')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
833
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
834
        tree.unlock()
835
836
        # the lines of revision_id->revision_parent_list has no guaranteed
837
        # order coming out of a dict, so sort both our test and response
838
        lines = sorted([' '.join([r2, r1]), r1])
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
839
        response = request.execute('', '')
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
840
        response.body = '\n'.join(sorted(response.body.split('\n')))
841
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
842
        self.assertEqual(
843
            SmartServerResponse(('ok', ), '\n'.join(lines)), response)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
844
845
    def test_specific_revision_argument(self):
846
        backing = self.get_transport()
847
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
848
        tree = self.make_branch_and_memory_tree('.')
849
        tree.lock_write()
850
        tree.add('')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
851
        rev_id_utf8 = u'\xc9'.encode('utf-8')
852
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
853
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
854
        tree.unlock()
855
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
856
        self.assertEqual(SmartServerResponse(('ok', ), rev_id_utf8),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
857
            request.execute('', rev_id_utf8))
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
858
    
859
    def test_no_such_revision(self):
860
        backing = self.get_transport()
861
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
862
        tree = self.make_branch_and_memory_tree('.')
863
        tree.lock_write()
864
        tree.add('')
865
        r1 = tree.commit('1st commit')
866
        tree.unlock()
867
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
868
        # Note that it still returns body (of zero bytes).
869
        self.assertEqual(
870
            SmartServerResponse(('nosuchrevision', 'missingrevision', ), ''),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
871
            request.execute('', 'missingrevision'))
872
873
874
class TestSmartServerRequestHasRevision(tests.TestCaseWithMemoryTransport):
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
875
876
    def test_missing_revision(self):
877
        """For a missing revision, ('no', ) is returned."""
878
        backing = self.get_transport()
879
        request = smart.repository.SmartServerRequestHasRevision(backing)
880
        self.make_repository('.')
881
        self.assertEqual(SmartServerResponse(('no', )),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
882
            request.execute('', 'revid'))
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
883
884
    def test_present_revision(self):
2018.5.158 by Andrew Bennetts
Return 'yes'/'no' rather than 'ok'/'no' from the Repository.has_revision smart command.
885
        """For a present revision, ('yes', ) is returned."""
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
886
        backing = self.get_transport()
887
        request = smart.repository.SmartServerRequestHasRevision(backing)
888
        tree = self.make_branch_and_memory_tree('.')
889
        tree.lock_write()
890
        tree.add('')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
891
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
892
        r1 = tree.commit('a commit', rev_id=rev_id_utf8)
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
893
        tree.unlock()
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
894
        self.assertTrue(tree.branch.repository.has_revision(rev_id_utf8))
2018.5.158 by Andrew Bennetts
Return 'yes'/'no' rather than 'ok'/'no' from the Repository.has_revision smart command.
895
        self.assertEqual(SmartServerResponse(('yes', )),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
896
            request.execute('', rev_id_utf8))
897
898
899
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
900
901
    def test_empty_revid(self):
902
        """With an empty revid, we get only size an number and revisions"""
903
        backing = self.get_transport()
904
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
905
        repository = self.make_repository('.')
906
        stats = repository.gather_stats()
907
        size = stats['size']
908
        expected_body = 'revisions: 0\nsize: %d\n' % size
909
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
910
                         request.execute('', '', 'no'))
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
911
912
    def test_revid_with_committers(self):
913
        """For a revid we get more infos."""
914
        backing = self.get_transport()
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
915
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
916
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
917
        tree = self.make_branch_and_memory_tree('.')
918
        tree.lock_write()
919
        tree.add('')
920
        # Let's build a predictable result
921
        tree.commit('a commit', timestamp=123456.2, timezone=3600)
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
922
        tree.commit('a commit', timestamp=654321.4, timezone=0,
923
                    rev_id=rev_id_utf8)
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
924
        tree.unlock()
925
926
        stats = tree.branch.repository.gather_stats()
927
        size = stats['size']
928
        expected_body = ('firstrev: 123456.200 3600\n'
929
                         'latestrev: 654321.400 0\n'
930
                         'revisions: 2\n'
931
                         'size: %d\n' % size)
932
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
933
                         request.execute('',
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
934
                                         rev_id_utf8, 'no'))
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
935
936
    def test_not_empty_repository_with_committers(self):
937
        """For a revid and requesting committers we get the whole thing."""
938
        backing = self.get_transport()
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
939
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
940
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
941
        tree = self.make_branch_and_memory_tree('.')
942
        tree.lock_write()
943
        tree.add('')
944
        # Let's build a predictable result
945
        tree.commit('a commit', timestamp=123456.2, timezone=3600,
946
                    committer='foo')
947
        tree.commit('a commit', timestamp=654321.4, timezone=0,
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
948
                    committer='bar', rev_id=rev_id_utf8)
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
949
        tree.unlock()
950
        stats = tree.branch.repository.gather_stats()
951
952
        size = stats['size']
953
        expected_body = ('committers: 2\n'
954
                         'firstrev: 123456.200 3600\n'
955
                         'latestrev: 654321.400 0\n'
956
                         'revisions: 2\n'
957
                         'size: %d\n' % size)
958
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
959
                         request.execute('',
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
960
                                         rev_id_utf8, 'yes'))
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
961
962
3441.5.4 by Andrew Bennetts
Fix test failures, and add some tests for the remote graph heads RPC.
963
class TestSmartServerRepositoryGraphHeads(tests.TestCaseWithMemoryTransport):
964
965
    def test_null_revision(self):
966
        backing = self.get_transport()
967
        request = smart.repository.SmartServerRepositoryGraphHeads(backing)
968
        repo = self.make_repository('.')
969
970
        response = request.execute('', 'null:')
971
        self.assertEqual(SuccessfulSmartServerResponse(('null:', )), response)
972
973
    def test_single_head_result(self):
974
        backing = self.get_transport()
975
        request = smart.repository.SmartServerRepositoryGraphHeads(backing)
976
        # Make a branch with two revisions in it.
977
        tree = self.make_branch_and_memory_tree('.')
978
        tree.lock_write()
979
        tree.add('')
980
        tree.commit('1st commit', rev_id='rev-1')
981
        tree.commit('2nd commit', rev_id='rev-2')
982
        tree.unlock()
983
984
        response = request.execute('', 'rev-1', 'rev-2')
985
        self.assertEqual(
986
            SuccessfulSmartServerResponse(('rev-2',)), response)
987
988
    def test_multiple_head_result(self):
989
        backing = self.get_transport()
990
        request = smart.repository.SmartServerRepositoryGraphHeads(backing)
991
        # Make a repository with two unrelated revisions in it.
992
        self.make_repository('.', shared=True)
993
        for tree_num in [1, 2]:
994
            tree = self.make_branch_and_memory_tree('tree-%d' % tree_num)
995
            tree.lock_write()
996
            tree.add('')
997
            tree.commit('1st commit', rev_id='tree-%d-rev' % tree_num)
998
            tree.unlock()
999
1000
        response = request.execute('', 'tree-1-rev', 'tree-2-rev')
1001
        args = response.args
1002
        self.assertEqual(set(args), set(['tree-1-rev', 'tree-2-rev']))
1003
1004
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1005
class TestSmartServerRepositoryIsShared(tests.TestCaseWithMemoryTransport):
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1006
1007
    def test_is_shared(self):
1008
        """For a shared repository, ('yes', ) is returned."""
1009
        backing = self.get_transport()
1010
        request = smart.repository.SmartServerRepositoryIsShared(backing)
1011
        self.make_repository('.', shared=True)
1012
        self.assertEqual(SmartServerResponse(('yes', )),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1013
            request.execute('', ))
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1014
1015
    def test_is_not_shared(self):
2018.5.58 by Wouter van Heyst
Small test fixes to reflect naming and documentation
1016
        """For a shared repository, ('no', ) is returned."""
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1017
        backing = self.get_transport()
1018
        request = smart.repository.SmartServerRepositoryIsShared(backing)
1019
        self.make_repository('.', shared=False)
1020
        self.assertEqual(SmartServerResponse(('no', )),
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1021
            request.execute('', ))
1022
1023
1024
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1025
1026
    def setUp(self):
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1027
        tests.TestCaseWithMemoryTransport.setUp(self)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1028
1029
    def test_lock_write_on_unlocked_repo(self):
1030
        backing = self.get_transport()
1031
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
1032
        repository = self.make_repository('.', format='knit')
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1033
        response = request.execute('')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1034
        nonce = repository.control_files._lock.peek().get('nonce')
1035
        self.assertEqual(SmartServerResponse(('ok', nonce)), response)
1036
        # The repository is now locked.  Verify that with a new repository
1037
        # object.
1038
        new_repo = repository.bzrdir.open_repository()
1039
        self.assertRaises(errors.LockContention, new_repo.lock_write)
1040
1041
    def test_lock_write_on_locked_repo(self):
1042
        backing = self.get_transport()
1043
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
1044
        repository = self.make_repository('.', format='knit')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1045
        repository.lock_write()
1046
        repository.leave_lock_in_place()
1047
        repository.unlock()
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1048
        response = request.execute('')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1049
        self.assertEqual(
1050
            SmartServerResponse(('LockContention',)), response)
1051
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.
1052
    def test_lock_write_on_readonly_transport(self):
1053
        backing = self.get_readonly_transport()
1054
        request = smart.repository.SmartServerRepositoryLockWrite(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
1055
        repository = self.make_repository('.', format='knit')
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.
1056
        response = request.execute('')
2872.5.3 by Martin Pool
Pass back LockFailed from smart server lock methods
1057
        self.assertFalse(response.is_successful())
1058
        self.assertEqual('LockFailed', response.args[0])
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.
1059
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1060
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1061
class TestSmartServerRepositoryUnlock(tests.TestCaseWithMemoryTransport):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1062
1063
    def setUp(self):
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1064
        tests.TestCaseWithMemoryTransport.setUp(self)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1065
1066
    def test_unlock_on_locked_repo(self):
1067
        backing = self.get_transport()
1068
        request = smart.repository.SmartServerRepositoryUnlock(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
1069
        repository = self.make_repository('.', format='knit')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1070
        token = repository.lock_write()
1071
        repository.leave_lock_in_place()
1072
        repository.unlock()
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1073
        response = request.execute('', token)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1074
        self.assertEqual(
1075
            SmartServerResponse(('ok',)), response)
1076
        # The repository is now unlocked.  Verify that with a new repository
1077
        # object.
1078
        new_repo = repository.bzrdir.open_repository()
1079
        new_repo.lock_write()
1080
        new_repo.unlock()
1081
1082
    def test_unlock_on_unlocked_repo(self):
1083
        backing = self.get_transport()
1084
        request = smart.repository.SmartServerRepositoryUnlock(backing)
3015.2.12 by Robert Collins
Make test_smart use specific formats as needed to exercise locked and unlocked repositories.
1085
        repository = self.make_repository('.', format='knit')
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1086
        response = request.execute('', 'some token')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1087
        self.assertEqual(
1088
            SmartServerResponse(('TokenMismatch',)), response)
1089
1090
2018.18.1 by Martin Pool
Add stub Repository.tarball smart method
1091
class TestSmartServerRepositoryTarball(tests.TestCaseWithTransport):
1092
1093
    def test_repository_tarball(self):
1094
        backing = self.get_transport()
1095
        request = smart.repository.SmartServerRepositoryTarball(backing)
1096
        repository = self.make_repository('.')
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1097
        # make some extraneous junk in the repository directory which should
1098
        # not be copied
1099
        self.build_tree(['.bzr/repository/extra-junk'])
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1100
        response = request.execute('', 'bz2')
2018.18.1 by Martin Pool
Add stub Repository.tarball smart method
1101
        self.assertEqual(('ok',), response.args)
1102
        # body should be a tbz2
2018.18.2 by Martin Pool
smart method Repository.tarball actually returns the tarball
1103
        body_file = StringIO(response.body)
1104
        body_tar = tarfile.open('body_tar.tbz2', fileobj=body_file,
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
1105
            mode='r|bz2')
2018.18.2 by Martin Pool
smart method Repository.tarball actually returns the tarball
1106
        # let's make sure there are some key repository components inside it.
1107
        # the tarfile returns directories with trailing slashes...
1108
        names = set([n.rstrip('/') for n in body_tar.getnames()])
2018.18.11 by Martin Pool
merge hpss changes
1109
        self.assertTrue('.bzr/repository/lock' in names)
1110
        self.assertTrue('.bzr/repository/format' in names)
1111
        self.assertTrue('.bzr/repository/extra-junk' not in names,
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
1112
            "extraneous file present in tar file")
2018.18.1 by Martin Pool
Add stub Repository.tarball smart method
1113
1114
2692.1.2 by Andrew Bennetts
Merge from bzr.dev.
1115
class TestSmartServerRepositoryStreamKnitData(tests.TestCaseWithMemoryTransport):
2535.3.14 by Andrew Bennetts
Move serialising repo data stream to bytes into smart protocol.
1116
1117
    def test_fetch_revisions(self):
1118
        backing = self.get_transport()
2535.3.49 by Andrew Bennetts
Rename 'Repository.fetch_revisions' smart request to 'Repository.stream_knit_data_for_revisions'.
1119
        request = smart.repository.SmartServerRepositoryStreamKnitDataForRevisions(backing)
2535.3.14 by Andrew Bennetts
Move serialising repo data stream to bytes into smart protocol.
1120
        tree = self.make_branch_and_memory_tree('.')
1121
        tree.lock_write()
1122
        tree.add('')
1123
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
1124
        rev_id2_utf8 = u'\xc9'.encode('utf-8')
1125
        r1 = tree.commit('1st commit', rev_id=rev_id1_utf8)
1126
        r1 = tree.commit('2nd commit', rev_id=rev_id2_utf8)
1127
        tree.unlock()
1128
2692.1.2 by Andrew Bennetts
Merge from bzr.dev.
1129
        response = request.execute('', rev_id2_utf8)
2535.3.14 by Andrew Bennetts
Move serialising repo data stream to bytes into smart protocol.
1130
        self.assertEqual(('ok',), response.args)
2535.4.29 by Andrew Bennetts
Add a new smart method, Repository.stream_revisions_chunked, rather than changing the behaviour of an existing method.
1131
        unpacker = pack.ContainerReader(StringIO(response.body))
1132
        names = []
1133
        for [name], read_bytes in unpacker.iter_records():
1134
            names.append(name)
1135
            bytes = read_bytes(None)
1136
            # The bytes should be a valid bencoded string.
1137
            bencode.bdecode(bytes)
1138
            # XXX: assert that the bencoded knit records have the right
1139
            # contents?
1140
        
1141
    def test_no_such_revision_error(self):
1142
        backing = self.get_transport()
1143
        request = smart.repository.SmartServerRepositoryStreamKnitDataForRevisions(backing)
1144
        repo = self.make_repository('.')
1145
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
2692.1.2 by Andrew Bennetts
Merge from bzr.dev.
1146
        response = request.execute('', rev_id1_utf8)
2535.4.29 by Andrew Bennetts
Add a new smart method, Repository.stream_revisions_chunked, rather than changing the behaviour of an existing method.
1147
        self.assertEqual(
1148
            SmartServerResponse(('NoSuchRevision', rev_id1_utf8)),
1149
            response)
1150
1151
2692.1.18 by Andrew Bennetts
Merge from bzr.dev.
1152
class TestSmartServerRepositoryStreamRevisionsChunked(tests.TestCaseWithMemoryTransport):
2535.4.29 by Andrew Bennetts
Add a new smart method, Repository.stream_revisions_chunked, rather than changing the behaviour of an existing method.
1153
1154
    def test_fetch_revisions(self):
1155
        backing = self.get_transport()
1156
        request = smart.repository.SmartServerRepositoryStreamRevisionsChunked(
1157
            backing)
1158
        tree = self.make_branch_and_memory_tree('.')
1159
        tree.lock_write()
1160
        tree.add('')
1161
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
1162
        rev_id2_utf8 = u'\xc9'.encode('utf-8')
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.
1163
        tree.commit('1st commit', rev_id=rev_id1_utf8)
1164
        tree.commit('2nd commit', rev_id=rev_id2_utf8)
2535.4.29 by Andrew Bennetts
Add a new smart method, Repository.stream_revisions_chunked, rather than changing the behaviour of an existing method.
1165
        tree.unlock()
1166
2692.1.24 by Andrew Bennetts
Merge from bzr.dev.
1167
        response = request.execute('')
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.
1168
        self.assertEqual(None, response)
1169
        response = request.do_body("%s\n%s\n1" % (rev_id2_utf8, rev_id1_utf8))
2535.4.29 by Andrew Bennetts
Add a new smart method, Repository.stream_revisions_chunked, rather than changing the behaviour of an existing method.
1170
        self.assertEqual(('ok',), response.args)
2535.4.22 by Andrew Bennetts
Merge from streamable-containers.
1171
        parser = pack.ContainerPushParser()
2535.3.14 by Andrew Bennetts
Move serialising repo data stream to bytes into smart protocol.
1172
        names = []
2535.4.22 by Andrew Bennetts
Merge from streamable-containers.
1173
        for stream_bytes in response.body_stream:
1174
            parser.accept_bytes(stream_bytes)
1175
            for [name], record_bytes in parser.read_pending_records():
1176
                names.append(name)
1177
                # The bytes should be a valid bencoded string.
1178
                bencode.bdecode(record_bytes)
1179
                # XXX: assert that the bencoded knit records have the right
1180
                # contents?
2535.3.14 by Andrew Bennetts
Move serialising repo data stream to bytes into smart protocol.
1181
        
2535.3.40 by Andrew Bennetts
Tidy up more XXXs.
1182
    def test_no_such_revision_error(self):
1183
        backing = self.get_transport()
2535.4.29 by Andrew Bennetts
Add a new smart method, Repository.stream_revisions_chunked, rather than changing the behaviour of an existing method.
1184
        request = smart.repository.SmartServerRepositoryStreamRevisionsChunked(
1185
            backing)
2535.3.40 by Andrew Bennetts
Tidy up more XXXs.
1186
        repo = self.make_repository('.')
1187
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
2692.1.24 by Andrew Bennetts
Merge from bzr.dev.
1188
        response = request.execute('')
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.
1189
        self.assertEqual(None, response)
1190
        response = request.do_body("%s\n\n1" % (rev_id1_utf8,))
1191
        self.assertEqual(
1192
            FailedSmartServerResponse(('NoSuchRevision', )),
1193
            response)
1194
2535.3.12 by Andrew Bennetts
Add a first cut of a get_data_stream method to Repository.
1195
2692.1.1 by Andrew Bennetts
Add translate_client_path method to SmartServerRequest.
1196
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
1197
1198
    def test_is_readonly_no(self):
1199
        backing = self.get_transport()
1200
        request = smart.request.SmartServerIsReadonly(backing)
1201
        response = request.execute()
1202
        self.assertEqual(
1203
            SmartServerResponse(('no',)), response)
1204
1205
    def test_is_readonly_yes(self):
1206
        backing = self.get_readonly_transport()
1207
        request = smart.request.SmartServerIsReadonly(backing)
1208
        response = request.execute()
1209
        self.assertEqual(
1210
            SmartServerResponse(('yes',)), response)
1211
1212
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
1213
class TestHandlers(tests.TestCase):
1214
    """Tests for the request.request_handlers object."""
1215
1216
    def test_registered_methods(self):
1217
        """Test that known methods are registered to the correct object."""
1218
        self.assertEqual(
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
1219
            smart.request.request_handlers.get('Branch.get_config_file'),
1220
            smart.branch.SmartServerBranchGetConfigFile)
1221
        self.assertEqual(
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1222
            smart.request.request_handlers.get('Branch.lock_write'),
1223
            smart.branch.SmartServerBranchRequestLockWrite)
1224
        self.assertEqual(
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1225
            smart.request.request_handlers.get('Branch.last_revision_info'),
1226
            smart.branch.SmartServerBranchRequestLastRevisionInfo)
1227
        self.assertEqual(
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
1228
            smart.request.request_handlers.get('Branch.revision_history'),
1229
            smart.branch.SmartServerRequestRevisionHistory)
1230
        self.assertEqual(
2018.5.77 by Wouter van Heyst
Fix typo in request_handlers registration of Branch.set_last_revision, and test that registration
1231
            smart.request.request_handlers.get('Branch.set_last_revision'),
1232
            smart.branch.SmartServerBranchRequestSetLastRevision)
1233
        self.assertEqual(
2892.2.1 by Andrew Bennetts
Add Branch.set_last_revision_info smart method, and make the RemoteBranch client use it.
1234
            smart.request.request_handlers.get('Branch.set_last_revision_info'),
1235
            smart.branch.SmartServerBranchRequestSetLastRevisionInfo)
1236
        self.assertEqual(
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
1237
            smart.request.request_handlers.get('Branch.unlock'),
1238
            smart.branch.SmartServerBranchRequestUnlock)
1239
        self.assertEqual(
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
1240
            smart.request.request_handlers.get('BzrDir.find_repository'),
3221.3.2 by Robert Collins
* New remote method ``RemoteBzrDir.find_repositoryV2`` adding support for
1241
            smart.bzrdir.SmartServerRequestFindRepositoryV1)
1242
        self.assertEqual(
1243
            smart.request.request_handlers.get('BzrDir.find_repositoryV2'),
1244
            smart.bzrdir.SmartServerRequestFindRepositoryV2)
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
1245
        self.assertEqual(
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
1246
            smart.request.request_handlers.get('BzrDirFormat.initialize'),
1247
            smart.bzrdir.SmartServerRequestInitializeBzrDir)
1248
        self.assertEqual(
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
1249
            smart.request.request_handlers.get('BzrDir.open_branch'),
1250
            smart.bzrdir.SmartServerRequestOpenBranch)
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
1251
        self.assertEqual(
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
1252
            smart.request.request_handlers.get('Repository.gather_stats'),
1253
            smart.repository.SmartServerRepositoryGatherStats)
1254
        self.assertEqual(
3172.5.6 by Robert Collins
Create new smart server verb Repository.get_parent_map.
1255
            smart.request.request_handlers.get('Repository.get_parent_map'),
1256
            smart.repository.SmartServerRepositoryGetParentMap)
1257
        self.assertEqual(
2535.3.69 by Andrew Bennetts
Add check for Repository.stream_knit_data_for_revisions to TestHandlers.test_registered_methods.
1258
            smart.request.request_handlers.get(
1259
                'Repository.get_revision_graph'),
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
1260
            smart.repository.SmartServerRepositoryGetRevisionGraph)
1261
        self.assertEqual(
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
1262
            smart.request.request_handlers.get('Repository.has_revision'),
1263
            smart.repository.SmartServerRequestHasRevision)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
1264
        self.assertEqual(
1265
            smart.request.request_handlers.get('Repository.is_shared'),
1266
            smart.repository.SmartServerRepositoryIsShared)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1267
        self.assertEqual(
1268
            smart.request.request_handlers.get('Repository.lock_write'),
1269
            smart.repository.SmartServerRepositoryLockWrite)
1270
        self.assertEqual(
2535.3.69 by Andrew Bennetts
Add check for Repository.stream_knit_data_for_revisions to TestHandlers.test_registered_methods.
1271
            smart.request.request_handlers.get('Repository.tarball'),
1272
            smart.repository.SmartServerRepositoryTarball)
1273
        self.assertEqual(
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
1274
            smart.request.request_handlers.get('Repository.unlock'),
1275
            smart.repository.SmartServerRepositoryUnlock)
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.
1276
        self.assertEqual(
1277
            smart.request.request_handlers.get('Transport.is_readonly'),
1278
            smart.request.SmartServerIsReadonly)