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