/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
2018.18.2 by Martin Pool
smart method Repository.tarball actually returns the tarball
27
from StringIO import StringIO
28
import tempfile
29
import tarfile
30
2535.3.31 by Andrew Bennetts
Fix imports broken by reverting the container-format merge. I didn't notice them earlier because of .pyc files :(
31
from bzrlib import bzrdir, errors, pack, smart, tests
2535.4.5 by Andrew Bennetts
Merge latest chunking protocol, including support for errors, fixing a test failure.
32
from bzrlib.smart.request import (
33
    FailedSmartServerResponse,
34
    SmartServerResponse,
35
    SuccessfulSmartServerResponse,
36
    )
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
37
import bzrlib.smart.bzrdir
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
38
import bzrlib.smart.branch
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
39
import bzrlib.smart.repository
2535.3.15 by Andrew Bennetts
Add KnitVersionedFile.get_stream_as_bytes, start smart implementation of RemoteRepository.get_data_stream.
40
from bzrlib.util import bencode
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
41
42
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
43
class TestCaseWithSmartMedium(tests.TestCaseWithTransport):
44
45
    def setUp(self):
46
        super(TestCaseWithSmartMedium, self).setUp()
47
        # We're allowed to set  the transport class here, so that we don't use
48
        # the default or a parameterized class, but rather use the
49
        # TestCaseWithTransport infrastructure to set up a smart server and
50
        # transport.
51
        self.transport_server = smart.server.SmartTCPServer_for_testing
52
53
    def get_smart_medium(self):
54
        """Get a smart medium to use in tests."""
55
        return self.get_transport().get_smart_medium()
56
57
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
58
class TestSmartServerResponse(tests.TestCase):
59
60
    def test__eq__(self):
61
        self.assertEqual(SmartServerResponse(('ok', )),
62
            SmartServerResponse(('ok', )))
63
        self.assertEqual(SmartServerResponse(('ok', ), 'body'),
64
            SmartServerResponse(('ok', ), 'body'))
65
        self.assertNotEqual(SmartServerResponse(('ok', )),
66
            SmartServerResponse(('notok', )))
67
        self.assertNotEqual(SmartServerResponse(('ok', ), 'body'),
68
            SmartServerResponse(('ok', )))
2018.5.41 by Robert Collins
Fix SmartServerResponse.__eq__ to handle None.
69
        self.assertNotEqual(None,
70
            SmartServerResponse(('ok', )))
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
71
72
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
73
class TestSmartServerRequestFindRepository(tests.TestCaseWithTransport):
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
74
    """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.
75
76
    def test_no_repository(self):
77
        """When there is no repository to be found, ('norepository', ) is returned."""
78
        backing = self.get_transport()
79
        request = smart.bzrdir.SmartServerRequestFindRepository(backing)
80
        self.make_bzrdir('.')
81
        self.assertEqual(SmartServerResponse(('norepository', )),
82
            request.execute(backing.local_abspath('')))
83
84
    def test_nonshared_repository(self):
85
        # nonshared repositorys only allow 'find' to return a handle when the 
86
        # path the repository is being searched on is the same as that that 
87
        # the repository is at.
88
        backing = self.get_transport()
89
        request = smart.bzrdir.SmartServerRequestFindRepository(backing)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
90
        result = self._make_repository_and_result()
91
        self.assertEqual(result, request.execute(backing.local_abspath('')))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
92
        self.make_bzrdir('subdir')
93
        self.assertEqual(SmartServerResponse(('norepository', )),
94
            request.execute(backing.local_abspath('subdir')))
95
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
96
    def _make_repository_and_result(self, shared=False, format=None):
97
        """Convenience function to setup a repository.
98
99
        :result: The SmartServerResponse to expect when opening it.
100
        """
101
        repo = self.make_repository('.', shared=shared, format=format)
102
        if repo.supports_rich_root():
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
103
            rich_root = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
104
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
105
            rich_root = 'no'
2018.5.138 by Robert Collins
Merge bzr.dev.
106
        if repo._format.supports_tree_reference:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
107
            subtrees = 'yes'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
108
        else:
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
109
            subtrees = 'no'
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
110
        return SmartServerResponse(('ok', '', rich_root, subtrees))
111
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
112
    def test_shared_repository(self):
113
        """When there is a shared repository, we get 'ok', 'relpath-to-repo'."""
114
        backing = self.get_transport()
115
        request = smart.bzrdir.SmartServerRequestFindRepository(backing)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
116
        result = self._make_repository_and_result(shared=True)
117
        self.assertEqual(result, request.execute(backing.local_abspath('')))
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
118
        self.make_bzrdir('subdir')
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
119
        result2 = SmartServerResponse(result.args[0:1] + ('..', ) + result.args[2:])
120
        self.assertEqual(result2,
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
121
            request.execute(backing.local_abspath('subdir')))
122
        self.make_bzrdir('subdir/deeper')
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
123
        result3 = SmartServerResponse(result.args[0:1] + ('../..', ) + result.args[2:])
124
        self.assertEqual(result3,
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
125
            request.execute(backing.local_abspath('subdir/deeper')))
126
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
127
    def test_rich_root_and_subtree_encoding(self):
128
        """Test for the format attributes for rich root and subtree support."""
129
        backing = self.get_transport()
130
        request = smart.bzrdir.SmartServerRequestFindRepository(backing)
131
        result = self._make_repository_and_result(format='dirstate-with-subtree')
132
        # check the test will be valid
2018.5.166 by Andrew Bennetts
Small changes in response to Aaron's review.
133
        self.assertEqual('yes', result.args[2])
134
        self.assertEqual('yes', result.args[3])
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
135
        self.assertEqual(result, request.execute(backing.local_abspath('')))
136
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
137
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
138
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithTransport):
139
140
    def test_empty_dir(self):
141
        """Initializing an empty dir should succeed and do it."""
142
        backing = self.get_transport()
143
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
144
        self.assertEqual(SmartServerResponse(('ok', )),
145
            request.execute(backing.local_abspath('.')))
146
        made_dir = bzrdir.BzrDir.open_from_transport(backing)
147
        # no branch, tree or repository is expected with the current 
148
        # default formart.
149
        self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
150
        self.assertRaises(errors.NotBranchError, made_dir.open_branch)
151
        self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
152
153
    def test_missing_dir(self):
154
        """Initializing a missing directory should fail like the bzrdir api."""
155
        backing = self.get_transport()
156
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
157
        self.assertRaises(errors.NoSuchFile,
158
            request.execute, backing.local_abspath('subdir'))
159
160
    def test_initialized_dir(self):
161
        """Initializing an extant bzrdir should fail like the bzrdir api."""
162
        backing = self.get_transport()
163
        request = smart.bzrdir.SmartServerRequestInitializeBzrDir(backing)
164
        self.make_bzrdir('subdir')
165
        self.assertRaises(errors.FileExists,
166
            request.execute, backing.local_abspath('subdir'))
167
168
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
169
class TestSmartServerRequestOpenBranch(tests.TestCaseWithTransport):
170
171
    def test_no_branch(self):
172
        """When there is no branch, ('nobranch', ) is returned."""
173
        backing = self.get_transport()
174
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
175
        self.make_bzrdir('.')
176
        self.assertEqual(SmartServerResponse(('nobranch', )),
177
            request.execute(backing.local_abspath('')))
178
179
    def test_branch(self):
180
        """When there is a branch, 'ok' is returned."""
181
        backing = self.get_transport()
182
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
183
        self.make_branch('.')
184
        self.assertEqual(SmartServerResponse(('ok', '')),
185
            request.execute(backing.local_abspath('')))
186
187
    def test_branch_reference(self):
188
        """When there is a branch reference, the reference URL is returned."""
189
        backing = self.get_transport()
190
        request = smart.bzrdir.SmartServerRequestOpenBranch(backing)
191
        branch = self.make_branch('branch')
192
        checkout = branch.create_checkout('reference',lightweight=True)
193
        # TODO: once we have an API to probe for references of any sort, we
194
        # can use it here.
195
        reference_url = backing.abspath('branch') + '/'
196
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
197
        self.assertEqual(SmartServerResponse(('ok', reference_url)),
198
            request.execute(backing.local_abspath('reference')))
199
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
200
201
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithTransport):
202
203
    def test_empty(self):
204
        """For an empty branch, the body is empty."""
205
        backing = self.get_transport()
206
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
207
        self.make_branch('.')
208
        self.assertEqual(SmartServerResponse(('ok', ), ''),
209
            request.execute(backing.local_abspath('')))
210
211
    def test_not_empty(self):
212
        """For a non-empty branch, the body is empty."""
213
        backing = self.get_transport()
214
        request = smart.branch.SmartServerRequestRevisionHistory(backing)
215
        tree = self.make_branch_and_memory_tree('.')
216
        tree.lock_write()
217
        tree.add('')
218
        r1 = tree.commit('1st commit')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
219
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
220
        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.
221
        self.assertEqual(
222
            SmartServerResponse(('ok', ), ('\x00'.join([r1, r2]))),
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
223
            request.execute(backing.local_abspath('')))
224
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
225
226
class TestSmartServerBranchRequest(tests.TestCaseWithTransport):
227
228
    def test_no_branch(self):
229
        """When there is a bzrdir and no branch, NotBranchError is raised."""
230
        backing = self.get_transport()
231
        request = smart.branch.SmartServerBranchRequest(backing)
232
        self.make_bzrdir('.')
233
        self.assertRaises(errors.NotBranchError,
234
            request.execute, backing.local_abspath(''))
235
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
236
    def test_branch_reference(self):
237
        """When there is a branch reference, NotBranchError is raised."""
238
        backing = self.get_transport()
2018.5.49 by Wouter van Heyst
Refactor SmartServerBranchRequest out from SmartServerRequestRevisionHistory to
239
        request = smart.branch.SmartServerBranchRequest(backing)
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
240
        branch = self.make_branch('branch')
241
        checkout = branch.create_checkout('reference',lightweight=True)
242
        self.assertRaises(errors.NotBranchError,
243
            request.execute, backing.local_abspath('checkout'))
244
245
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
246
class TestSmartServerBranchRequestLastRevisionInfo(tests.TestCaseWithTransport):
247
248
    def test_empty(self):
2018.5.170 by Andrew Bennetts
Use 'null:' instead of '' to mean NULL_REVISION on the wire.
249
        """For an empty branch, the result is ('ok', '0', 'null:')."""
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
250
        backing = self.get_transport()
251
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
252
        self.make_branch('.')
2018.5.170 by Andrew Bennetts
Use 'null:' instead of '' to mean NULL_REVISION on the wire.
253
        self.assertEqual(SmartServerResponse(('ok', '0', 'null:')),
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
254
            request.execute(backing.local_abspath('')))
255
256
    def test_not_empty(self):
257
        """For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
258
        backing = self.get_transport()
259
        request = smart.branch.SmartServerBranchRequestLastRevisionInfo(backing)
260
        tree = self.make_branch_and_memory_tree('.')
261
        tree.lock_write()
262
        tree.add('')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
263
        rev_id_utf8 = u'\xc8'.encode('utf-8')
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
264
        r1 = tree.commit('1st commit')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
265
        r2 = tree.commit('2nd commit', rev_id=rev_id_utf8)
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
266
        tree.unlock()
267
        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.
268
            SmartServerResponse(('ok', '2', rev_id_utf8)),
2018.5.50 by Wouter van Heyst
Add SmartServerBranchRequestLastRevisionInfo method.
269
            request.execute(backing.local_abspath('')))
270
271
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
272
class TestSmartServerBranchRequestGetConfigFile(tests.TestCaseWithTransport):
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
273
274
    def test_default(self):
275
        """With no file, we get empty content."""
276
        backing = self.get_transport()
277
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
278
        branch = self.make_branch('.')
279
        # there should be no file by default
280
        content = ''
281
        self.assertEqual(SmartServerResponse(('ok', ), content),
282
            request.execute(backing.local_abspath('')))
283
284
    def test_with_content(self):
285
        # SmartServerBranchGetConfigFile should return the content from
286
        # branch.control_files.get('branch.conf') for now - in the future it may
287
        # perform more complex processing. 
288
        backing = self.get_transport()
289
        request = smart.branch.SmartServerBranchGetConfigFile(backing)
290
        branch = self.make_branch('.')
291
        branch.control_files.put_utf8('branch.conf', 'foo bar baz')
292
        self.assertEqual(SmartServerResponse(('ok', ), 'foo bar baz'),
293
            request.execute(backing.local_abspath('')))
294
295
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
296
class TestSmartServerBranchRequestSetLastRevision(tests.TestCaseWithTransport):
297
298
    def test_empty(self):
299
        backing = self.get_transport()
300
        request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
301
        b = self.make_branch('.')
2018.5.144 by Andrew Bennetts
Fix four tests I broke with the Branch.lock_write changes.
302
        branch_token = b.lock_write()
303
        repo_token = b.repository.lock_write()
304
        b.repository.unlock()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
305
        try:
306
            self.assertEqual(SmartServerResponse(('ok',)),
307
                request.execute(
2018.5.170 by Andrew Bennetts
Use 'null:' instead of '' to mean NULL_REVISION on the wire.
308
                    backing.local_abspath(''), branch_token, repo_token,
309
                    'null:'))
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
310
        finally:
311
            b.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
312
313
    def test_not_present_revision_id(self):
314
        backing = self.get_transport()
315
        request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
316
        b = self.make_branch('.')
2018.5.144 by Andrew Bennetts
Fix four tests I broke with the Branch.lock_write changes.
317
        branch_token = b.lock_write()
318
        repo_token = b.repository.lock_write()
319
        b.repository.unlock()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
320
        try:
321
            revision_id = 'non-existent revision'
322
            self.assertEqual(
323
                SmartServerResponse(('NoSuchRevision', revision_id)),
324
                request.execute(
325
                    backing.local_abspath(''), branch_token, repo_token,
326
                    revision_id))
327
        finally:
328
            b.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
329
330
    def test_revision_id_present(self):
331
        backing = self.get_transport()
332
        request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
333
        tree = self.make_branch_and_memory_tree('.')
334
        tree.lock_write()
335
        tree.add('')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
336
        rev_id_utf8 = u'\xc8'.encode('utf-8')
337
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
338
        r2 = tree.commit('2nd commit')
339
        tree.unlock()
2018.5.144 by Andrew Bennetts
Fix four tests I broke with the Branch.lock_write changes.
340
        branch_token = tree.branch.lock_write()
341
        repo_token = tree.branch.repository.lock_write()
342
        tree.branch.repository.unlock()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
343
        try:
344
            self.assertEqual(
345
                SmartServerResponse(('ok',)),
346
                request.execute(
347
                    backing.local_abspath(''), branch_token, repo_token,
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
348
                    rev_id_utf8))
349
            self.assertEqual([rev_id_utf8], tree.branch.revision_history())
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
350
        finally:
351
            tree.branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
352
353
    def test_revision_id_present2(self):
354
        backing = self.get_transport()
355
        request = smart.branch.SmartServerBranchRequestSetLastRevision(backing)
356
        tree = self.make_branch_and_memory_tree('.')
357
        tree.lock_write()
358
        tree.add('')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
359
        rev_id_utf8 = u'\xc8'.encode('utf-8')
360
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
361
        r2 = tree.commit('2nd commit')
362
        tree.unlock()
363
        tree.branch.set_revision_history([])
2018.5.144 by Andrew Bennetts
Fix four tests I broke with the Branch.lock_write changes.
364
        branch_token = tree.branch.lock_write()
365
        repo_token = tree.branch.repository.lock_write()
366
        tree.branch.repository.unlock()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
367
        try:
368
            self.assertEqual(
369
                SmartServerResponse(('ok',)),
370
                request.execute(
371
                    backing.local_abspath(''), branch_token, repo_token,
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
372
                    rev_id_utf8))
373
            self.assertEqual([rev_id_utf8], tree.branch.revision_history())
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
374
        finally:
375
            tree.branch.unlock()
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
376
377
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
378
class TestSmartServerBranchRequestLockWrite(tests.TestCaseWithTransport):
379
380
    def setUp(self):
381
        tests.TestCaseWithTransport.setUp(self)
382
        self.reduceLockdirTimeout()
383
384
    def test_lock_write_on_unlocked_branch(self):
385
        backing = self.get_transport()
386
        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.
387
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
388
        repository = branch.repository
389
        response = request.execute(backing.local_abspath(''))
390
        branch_nonce = branch.control_files._lock.peek().get('nonce')
391
        repository_nonce = repository.control_files._lock.peek().get('nonce')
392
        self.assertEqual(
393
            SmartServerResponse(('ok', branch_nonce, repository_nonce)),
394
            response)
395
        # The branch (and associated repository) is now locked.  Verify that
396
        # with a new branch object.
397
        new_branch = repository.bzrdir.open_branch()
398
        self.assertRaises(errors.LockContention, new_branch.lock_write)
399
400
    def test_lock_write_on_locked_branch(self):
401
        backing = self.get_transport()
402
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
403
        branch = self.make_branch('.')
404
        branch.lock_write()
405
        branch.leave_lock_in_place()
406
        branch.unlock()
407
        response = request.execute(backing.local_abspath(''))
408
        self.assertEqual(
409
            SmartServerResponse(('LockContention',)), response)
410
411
    def test_lock_write_with_tokens_on_locked_branch(self):
412
        backing = self.get_transport()
413
        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.
414
        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).
415
        branch_token = branch.lock_write()
416
        repo_token = branch.repository.lock_write()
417
        branch.repository.unlock()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
418
        branch.leave_lock_in_place()
419
        branch.repository.leave_lock_in_place()
420
        branch.unlock()
421
        response = request.execute(backing.local_abspath(''),
422
                                   branch_token, repo_token)
423
        self.assertEqual(
424
            SmartServerResponse(('ok', branch_token, repo_token)), response)
425
426
    def test_lock_write_with_mismatched_tokens_on_locked_branch(self):
427
        backing = self.get_transport()
428
        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.
429
        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).
430
        branch_token = branch.lock_write()
431
        repo_token = branch.repository.lock_write()
432
        branch.repository.unlock()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
433
        branch.leave_lock_in_place()
434
        branch.repository.leave_lock_in_place()
435
        branch.unlock()
436
        response = request.execute(backing.local_abspath(''),
437
                                   branch_token+'xxx', repo_token)
438
        self.assertEqual(
439
            SmartServerResponse(('TokenMismatch',)), response)
440
441
    def test_lock_write_on_locked_repo(self):
442
        backing = self.get_transport()
443
        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.
444
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
445
        branch.repository.lock_write()
446
        branch.repository.leave_lock_in_place()
447
        branch.repository.unlock()
448
        response = request.execute(backing.local_abspath(''))
449
        self.assertEqual(
450
            SmartServerResponse(('LockContention',)), response)
451
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.
452
    def test_lock_write_on_readonly_transport(self):
453
        backing = self.get_readonly_transport()
454
        request = smart.branch.SmartServerBranchRequestLockWrite(backing)
455
        branch = self.make_branch('.')
456
        response = request.execute('')
2872.5.3 by Martin Pool
Pass back LockFailed from smart server lock methods
457
        error_name, lock_str, why_str = response.args
458
        self.assertFalse(response.is_successful())
459
        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.
460
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
461
462
class TestSmartServerBranchRequestUnlock(tests.TestCaseWithTransport):
463
464
    def setUp(self):
465
        tests.TestCaseWithTransport.setUp(self)
466
        self.reduceLockdirTimeout()
467
468
    def test_unlock_on_locked_branch_and_repo(self):
469
        backing = self.get_transport()
470
        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.
471
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
472
        # 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).
473
        branch_token = branch.lock_write()
474
        repo_token = branch.repository.lock_write()
475
        branch.repository.unlock()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
476
        # Unlock the branch (and repo) object, leaving the physical locks
477
        # in place.
478
        branch.leave_lock_in_place()
479
        branch.repository.leave_lock_in_place()
480
        branch.unlock()
481
        response = request.execute(backing.local_abspath(''),
482
                                   branch_token, repo_token)
483
        self.assertEqual(
484
            SmartServerResponse(('ok',)), response)
485
        # The branch is now unlocked.  Verify that with a new branch
486
        # object.
487
        new_branch = branch.bzrdir.open_branch()
488
        new_branch.lock_write()
489
        new_branch.unlock()
490
491
    def test_unlock_on_unlocked_branch_unlocked_repo(self):
492
        backing = self.get_transport()
493
        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.
494
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
495
        response = request.execute(
496
            backing.local_abspath(''), 'branch token', 'repo token')
497
        self.assertEqual(
498
            SmartServerResponse(('TokenMismatch',)), response)
499
500
    def test_unlock_on_unlocked_branch_locked_repo(self):
501
        backing = self.get_transport()
502
        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.
503
        branch = self.make_branch('.', format='knit')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
504
        # Lock the repository.
505
        repo_token = branch.repository.lock_write()
506
        branch.repository.leave_lock_in_place()
507
        branch.repository.unlock()
508
        # Issue branch lock_write request on the unlocked branch (with locked
509
        # repo).
510
        response = request.execute(
511
            backing.local_abspath(''), 'branch token', repo_token)
512
        self.assertEqual(
513
            SmartServerResponse(('TokenMismatch',)), response)
514
515
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
516
class TestSmartServerRepositoryRequest(tests.TestCaseWithTransport):
517
518
    def test_no_repository(self):
519
        """Raise NoRepositoryPresent when there is a bzrdir and no repo."""
520
        # we test this using a shared repository above the named path,
521
        # thus checking the right search logic is used - that is, that
522
        # its the exact path being looked at and the server is not
523
        # searching.
524
        backing = self.get_transport()
2018.5.58 by Wouter van Heyst
Small test fixes to reflect naming and documentation
525
        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).
526
        self.make_repository('.', shared=True)
527
        self.make_bzrdir('subdir')
528
        self.assertRaises(errors.NoRepositoryPresent,
2018.5.58 by Wouter van Heyst
Small test fixes to reflect naming and documentation
529
            request.execute, backing.local_abspath('subdir'))
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
530
531
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
532
class TestSmartServerRepositoryGetRevisionGraph(tests.TestCaseWithTransport):
533
534
    def test_none_argument(self):
535
        backing = self.get_transport()
536
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
537
        tree = self.make_branch_and_memory_tree('.')
538
        tree.lock_write()
539
        tree.add('')
540
        r1 = tree.commit('1st commit')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
541
        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)
542
        tree.unlock()
543
544
        # the lines of revision_id->revision_parent_list has no guaranteed
545
        # order coming out of a dict, so sort both our test and response
546
        lines = sorted([' '.join([r2, r1]), r1])
547
        response = request.execute(backing.local_abspath(''), '')
548
        response.body = '\n'.join(sorted(response.body.split('\n')))
549
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
550
        self.assertEqual(
551
            SmartServerResponse(('ok', ), '\n'.join(lines)), response)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
552
553
    def test_specific_revision_argument(self):
554
        backing = self.get_transport()
555
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
556
        tree = self.make_branch_and_memory_tree('.')
557
        tree.lock_write()
558
        tree.add('')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
559
        rev_id_utf8 = u'\xc9'.encode('utf-8')
560
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
561
        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)
562
        tree.unlock()
563
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
564
        self.assertEqual(SmartServerResponse(('ok', ), rev_id_utf8),
565
            request.execute(backing.local_abspath(''), rev_id_utf8))
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
566
    
567
    def test_no_such_revision(self):
568
        backing = self.get_transport()
569
        request = smart.repository.SmartServerRepositoryGetRevisionGraph(backing)
570
        tree = self.make_branch_and_memory_tree('.')
571
        tree.lock_write()
572
        tree.add('')
573
        r1 = tree.commit('1st commit')
574
        tree.unlock()
575
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
576
        # Note that it still returns body (of zero bytes).
577
        self.assertEqual(
578
            SmartServerResponse(('nosuchrevision', 'missingrevision', ), ''),
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
579
            request.execute(backing.local_abspath(''), 'missingrevision'))
580
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
581
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
582
class TestSmartServerRequestHasRevision(tests.TestCaseWithTransport):
583
584
    def test_missing_revision(self):
585
        """For a missing revision, ('no', ) is returned."""
586
        backing = self.get_transport()
587
        request = smart.repository.SmartServerRequestHasRevision(backing)
588
        self.make_repository('.')
589
        self.assertEqual(SmartServerResponse(('no', )),
590
            request.execute(backing.local_abspath(''), 'revid'))
591
592
    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.
593
        """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).
594
        backing = self.get_transport()
595
        request = smart.repository.SmartServerRequestHasRevision(backing)
596
        tree = self.make_branch_and_memory_tree('.')
597
        tree.lock_write()
598
        tree.add('')
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
599
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
600
        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).
601
        tree.unlock()
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
602
        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.
603
        self.assertEqual(SmartServerResponse(('yes', )),
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
604
            request.execute(backing.local_abspath(''), 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).
605
606
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
607
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithTransport):
608
609
    def test_empty_revid(self):
610
        """With an empty revid, we get only size an number and revisions"""
611
        backing = self.get_transport()
612
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
613
        repository = self.make_repository('.')
614
        stats = repository.gather_stats()
615
        size = stats['size']
616
        expected_body = 'revisions: 0\nsize: %d\n' % size
617
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
618
                         request.execute(backing.local_abspath(''), '', 'no'))
619
620
    def test_revid_with_committers(self):
621
        """For a revid we get more infos."""
622
        backing = self.get_transport()
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
623
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
624
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
625
        tree = self.make_branch_and_memory_tree('.')
626
        tree.lock_write()
627
        tree.add('')
628
        # Let's build a predictable result
629
        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.
630
        tree.commit('a commit', timestamp=654321.4, timezone=0,
631
                    rev_id=rev_id_utf8)
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
632
        tree.unlock()
633
634
        stats = tree.branch.repository.gather_stats()
635
        size = stats['size']
636
        expected_body = ('firstrev: 123456.200 3600\n'
637
                         'latestrev: 654321.400 0\n'
638
                         'revisions: 2\n'
639
                         'size: %d\n' % size)
640
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
641
                         request.execute(backing.local_abspath(''),
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
642
                                         rev_id_utf8, 'no'))
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
643
644
    def test_not_empty_repository_with_committers(self):
645
        """For a revid and requesting committers we get the whole thing."""
646
        backing = self.get_transport()
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
647
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
648
        request = smart.repository.SmartServerRepositoryGatherStats(backing)
649
        tree = self.make_branch_and_memory_tree('.')
650
        tree.lock_write()
651
        tree.add('')
652
        # Let's build a predictable result
653
        tree.commit('a commit', timestamp=123456.2, timezone=3600,
654
                    committer='foo')
655
        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.
656
                    committer='bar', rev_id=rev_id_utf8)
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
657
        tree.unlock()
658
        stats = tree.branch.repository.gather_stats()
659
660
        size = stats['size']
661
        expected_body = ('committers: 2\n'
662
                         'firstrev: 123456.200 3600\n'
663
                         'latestrev: 654321.400 0\n'
664
                         'revisions: 2\n'
665
                         'size: %d\n' % size)
666
        self.assertEqual(SmartServerResponse(('ok', ), expected_body),
667
                         request.execute(backing.local_abspath(''),
2018.5.148 by Andrew Bennetts
Fix all the DeprecationWarnings in test_smart caused by unicode revision IDs.
668
                                         rev_id_utf8, 'yes'))
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
669
670
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
671
class TestSmartServerRepositoryIsShared(tests.TestCaseWithTransport):
672
673
    def test_is_shared(self):
674
        """For a shared repository, ('yes', ) is returned."""
675
        backing = self.get_transport()
676
        request = smart.repository.SmartServerRepositoryIsShared(backing)
677
        self.make_repository('.', shared=True)
678
        self.assertEqual(SmartServerResponse(('yes', )),
679
            request.execute(backing.local_abspath(''), ))
680
681
    def test_is_not_shared(self):
2018.5.58 by Wouter van Heyst
Small test fixes to reflect naming and documentation
682
        """For a shared repository, ('no', ) is returned."""
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
683
        backing = self.get_transport()
684
        request = smart.repository.SmartServerRepositoryIsShared(backing)
685
        self.make_repository('.', shared=False)
686
        self.assertEqual(SmartServerResponse(('no', )),
687
            request.execute(backing.local_abspath(''), ))
688
2018.5.56 by Robert Collins
Factor out code we expect to be common in SmartServerRequestHasRevision to SmartServerRepositoryRequest (Robert Collins, Vincent Ladeuil).
689
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
690
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithTransport):
691
692
    def setUp(self):
693
        tests.TestCaseWithTransport.setUp(self)
694
        self.reduceLockdirTimeout()
695
696
    def test_lock_write_on_unlocked_repo(self):
697
        backing = self.get_transport()
698
        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.
699
        repository = self.make_repository('.', format='knit')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
700
        response = request.execute(backing.local_abspath(''))
701
        nonce = repository.control_files._lock.peek().get('nonce')
702
        self.assertEqual(SmartServerResponse(('ok', nonce)), response)
703
        # The repository is now locked.  Verify that with a new repository
704
        # object.
705
        new_repo = repository.bzrdir.open_repository()
706
        self.assertRaises(errors.LockContention, new_repo.lock_write)
707
708
    def test_lock_write_on_locked_repo(self):
709
        backing = self.get_transport()
710
        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.
711
        repository = self.make_repository('.', format='knit')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
712
        repository.lock_write()
713
        repository.leave_lock_in_place()
714
        repository.unlock()
715
        response = request.execute(backing.local_abspath(''))
716
        self.assertEqual(
717
            SmartServerResponse(('LockContention',)), response)
718
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.
719
    def test_lock_write_on_readonly_transport(self):
720
        backing = self.get_readonly_transport()
721
        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.
722
        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.
723
        response = request.execute('')
2872.5.3 by Martin Pool
Pass back LockFailed from smart server lock methods
724
        self.assertFalse(response.is_successful())
725
        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.
726
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
727
728
class TestSmartServerRepositoryUnlock(tests.TestCaseWithTransport):
729
730
    def setUp(self):
731
        tests.TestCaseWithTransport.setUp(self)
732
        self.reduceLockdirTimeout()
733
734
    def test_unlock_on_locked_repo(self):
735
        backing = self.get_transport()
736
        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.
737
        repository = self.make_repository('.', format='knit')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
738
        token = repository.lock_write()
739
        repository.leave_lock_in_place()
740
        repository.unlock()
741
        response = request.execute(backing.local_abspath(''), token)
742
        self.assertEqual(
743
            SmartServerResponse(('ok',)), response)
744
        # The repository is now unlocked.  Verify that with a new repository
745
        # object.
746
        new_repo = repository.bzrdir.open_repository()
747
        new_repo.lock_write()
748
        new_repo.unlock()
749
750
    def test_unlock_on_unlocked_repo(self):
751
        backing = self.get_transport()
752
        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.
753
        repository = self.make_repository('.', format='knit')
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
754
        response = request.execute(backing.local_abspath(''), 'some token')
755
        self.assertEqual(
756
            SmartServerResponse(('TokenMismatch',)), response)
757
758
2018.18.1 by Martin Pool
Add stub Repository.tarball smart method
759
class TestSmartServerRepositoryTarball(tests.TestCaseWithTransport):
760
761
    def test_repository_tarball(self):
762
        backing = self.get_transport()
763
        request = smart.repository.SmartServerRepositoryTarball(backing)
764
        repository = self.make_repository('.')
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
765
        # make some extraneous junk in the repository directory which should
766
        # not be copied
767
        self.build_tree(['.bzr/repository/extra-junk'])
2018.18.2 by Martin Pool
smart method Repository.tarball actually returns the tarball
768
        response = request.execute(backing.local_abspath(''), 'bz2')
2018.18.1 by Martin Pool
Add stub Repository.tarball smart method
769
        self.assertEqual(('ok',), response.args)
770
        # body should be a tbz2
2018.18.2 by Martin Pool
smart method Repository.tarball actually returns the tarball
771
        body_file = StringIO(response.body)
772
        body_tar = tarfile.open('body_tar.tbz2', fileobj=body_file,
2018.18.25 by Martin Pool
Repository.tarball fixes for python2.4
773
            mode='r|bz2')
2018.18.2 by Martin Pool
smart method Repository.tarball actually returns the tarball
774
        # let's make sure there are some key repository components inside it.
775
        # the tarfile returns directories with trailing slashes...
776
        names = set([n.rstrip('/') for n in body_tar.getnames()])
2018.18.11 by Martin Pool
merge hpss changes
777
        self.assertTrue('.bzr/repository/lock' in names)
778
        self.assertTrue('.bzr/repository/format' in names)
779
        self.assertTrue('.bzr/repository/extra-junk' not in names,
2018.18.9 by Martin Pool
remote Repository.tarball builds a temporary directory and tars that
780
            "extraneous file present in tar file")
2018.18.1 by Martin Pool
Add stub Repository.tarball smart method
781
782
2535.3.49 by Andrew Bennetts
Rename 'Repository.fetch_revisions' smart request to 'Repository.stream_knit_data_for_revisions'.
783
class TestSmartServerRepositoryStreamKnitData(tests.TestCaseWithTransport):
2535.3.14 by Andrew Bennetts
Move serialising repo data stream to bytes into smart protocol.
784
785
    def test_fetch_revisions(self):
786
        backing = self.get_transport()
2535.3.49 by Andrew Bennetts
Rename 'Repository.fetch_revisions' smart request to 'Repository.stream_knit_data_for_revisions'.
787
        request = smart.repository.SmartServerRepositoryStreamKnitDataForRevisions(backing)
2535.3.14 by Andrew Bennetts
Move serialising repo data stream to bytes into smart protocol.
788
        tree = self.make_branch_and_memory_tree('.')
789
        tree.lock_write()
790
        tree.add('')
791
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
792
        rev_id2_utf8 = u'\xc9'.encode('utf-8')
793
        r1 = tree.commit('1st commit', rev_id=rev_id1_utf8)
794
        r1 = tree.commit('2nd commit', rev_id=rev_id2_utf8)
795
        tree.unlock()
796
797
        response = request.execute(backing.local_abspath(''), rev_id2_utf8)
798
        self.assertEqual(('ok',), response.args)
799
        from cStringIO import StringIO
2535.4.29 by Andrew Bennetts
Add a new smart method, Repository.stream_revisions_chunked, rather than changing the behaviour of an existing method.
800
        unpacker = pack.ContainerReader(StringIO(response.body))
801
        names = []
802
        for [name], read_bytes in unpacker.iter_records():
803
            names.append(name)
804
            bytes = read_bytes(None)
805
            # The bytes should be a valid bencoded string.
806
            bencode.bdecode(bytes)
807
            # XXX: assert that the bencoded knit records have the right
808
            # contents?
809
        
810
    def test_no_such_revision_error(self):
811
        backing = self.get_transport()
812
        request = smart.repository.SmartServerRepositoryStreamKnitDataForRevisions(backing)
813
        repo = self.make_repository('.')
814
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
815
        response = request.execute(backing.local_abspath(''), rev_id1_utf8)
816
        self.assertEqual(
817
            SmartServerResponse(('NoSuchRevision', rev_id1_utf8)),
818
            response)
819
820
821
class TestSmartServerRepositoryStreamRevisionsChunked(tests.TestCaseWithTransport):
822
823
    def test_fetch_revisions(self):
824
        backing = self.get_transport()
825
        request = smart.repository.SmartServerRepositoryStreamRevisionsChunked(
826
            backing)
827
        tree = self.make_branch_and_memory_tree('.')
828
        tree.lock_write()
829
        tree.add('')
830
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
831
        rev_id2_utf8 = u'\xc9'.encode('utf-8')
832
        r1 = tree.commit('1st commit', rev_id=rev_id1_utf8)
833
        r1 = tree.commit('2nd commit', rev_id=rev_id2_utf8)
834
        tree.unlock()
835
836
        response = request.execute(backing.local_abspath(''), rev_id2_utf8)
837
        self.assertEqual(('ok',), response.args)
838
        from cStringIO import StringIO
2535.4.22 by Andrew Bennetts
Merge from streamable-containers.
839
        parser = pack.ContainerPushParser()
2535.3.14 by Andrew Bennetts
Move serialising repo data stream to bytes into smart protocol.
840
        names = []
2535.4.22 by Andrew Bennetts
Merge from streamable-containers.
841
        for stream_bytes in response.body_stream:
842
            parser.accept_bytes(stream_bytes)
843
            for [name], record_bytes in parser.read_pending_records():
844
                names.append(name)
845
                # The bytes should be a valid bencoded string.
846
                bencode.bdecode(record_bytes)
847
                # XXX: assert that the bencoded knit records have the right
848
                # contents?
2535.3.14 by Andrew Bennetts
Move serialising repo data stream to bytes into smart protocol.
849
        
2535.3.40 by Andrew Bennetts
Tidy up more XXXs.
850
    def test_no_such_revision_error(self):
851
        backing = self.get_transport()
2535.4.29 by Andrew Bennetts
Add a new smart method, Repository.stream_revisions_chunked, rather than changing the behaviour of an existing method.
852
        request = smart.repository.SmartServerRepositoryStreamRevisionsChunked(
853
            backing)
2535.3.40 by Andrew Bennetts
Tidy up more XXXs.
854
        repo = self.make_repository('.')
855
        rev_id1_utf8 = u'\xc8'.encode('utf-8')
856
        response = request.execute(backing.local_abspath(''), rev_id1_utf8)
2535.4.5 by Andrew Bennetts
Merge latest chunking protocol, including support for errors, fixing a test failure.
857
        # There's no error initially.
858
        self.assertTrue(response.is_successful())
859
        self.assertEqual(('ok',), response.args)
860
        # We only get an error while streaming the body.
861
        body = list(response.body_stream)
862
        last_chunk = body[-1]
863
        self.assertIsInstance(last_chunk, FailedSmartServerResponse)
2535.3.40 by Andrew Bennetts
Tidy up more XXXs.
864
        self.assertEqual(
2535.4.5 by Andrew Bennetts
Merge latest chunking protocol, including support for errors, fixing a test failure.
865
            last_chunk,
866
            FailedSmartServerResponse(('NoSuchRevision', rev_id1_utf8)))
2535.3.12 by Andrew Bennetts
Add a first cut of a get_data_stream method to Repository.
867
868
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.
869
class TestSmartServerIsReadonly(tests.TestCaseWithTransport):
870
871
    def test_is_readonly_no(self):
872
        backing = self.get_transport()
873
        request = smart.request.SmartServerIsReadonly(backing)
874
        response = request.execute()
875
        self.assertEqual(
876
            SmartServerResponse(('no',)), response)
877
878
    def test_is_readonly_yes(self):
879
        backing = self.get_readonly_transport()
880
        request = smart.request.SmartServerIsReadonly(backing)
881
        response = request.execute()
882
        self.assertEqual(
883
            SmartServerResponse(('yes',)), response)
884
885
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
886
class TestHandlers(tests.TestCase):
887
    """Tests for the request.request_handlers object."""
888
889
    def test_registered_methods(self):
890
        """Test that known methods are registered to the correct object."""
891
        self.assertEqual(
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
892
            smart.request.request_handlers.get('Branch.get_config_file'),
893
            smart.branch.SmartServerBranchGetConfigFile)
894
        self.assertEqual(
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
895
            smart.request.request_handlers.get('Branch.lock_write'),
896
            smart.branch.SmartServerBranchRequestLockWrite)
897
        self.assertEqual(
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
898
            smart.request.request_handlers.get('Branch.last_revision_info'),
899
            smart.branch.SmartServerBranchRequestLastRevisionInfo)
900
        self.assertEqual(
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
901
            smart.request.request_handlers.get('Branch.revision_history'),
902
            smart.branch.SmartServerRequestRevisionHistory)
903
        self.assertEqual(
2018.5.77 by Wouter van Heyst
Fix typo in request_handlers registration of Branch.set_last_revision, and test that registration
904
            smart.request.request_handlers.get('Branch.set_last_revision'),
905
            smart.branch.SmartServerBranchRequestSetLastRevision)
906
        self.assertEqual(
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
907
            smart.request.request_handlers.get('Branch.unlock'),
908
            smart.branch.SmartServerBranchRequestUnlock)
909
        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.
910
            smart.request.request_handlers.get('BzrDir.find_repository'),
911
            smart.bzrdir.SmartServerRequestFindRepository)
912
        self.assertEqual(
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
913
            smart.request.request_handlers.get('BzrDirFormat.initialize'),
914
            smart.bzrdir.SmartServerRequestInitializeBzrDir)
915
        self.assertEqual(
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
916
            smart.request.request_handlers.get('BzrDir.open_branch'),
917
            smart.bzrdir.SmartServerRequestOpenBranch)
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
918
        self.assertEqual(
2018.10.2 by v.ladeuil+lp at free
gather_stats server side and request registration
919
            smart.request.request_handlers.get('Repository.gather_stats'),
920
            smart.repository.SmartServerRepositoryGatherStats)
921
        self.assertEqual(
2535.3.69 by Andrew Bennetts
Add check for Repository.stream_knit_data_for_revisions to TestHandlers.test_registered_methods.
922
            smart.request.request_handlers.get(
923
                'Repository.get_revision_graph'),
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
924
            smart.repository.SmartServerRepositoryGetRevisionGraph)
925
        self.assertEqual(
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
926
            smart.request.request_handlers.get('Repository.has_revision'),
927
            smart.repository.SmartServerRequestHasRevision)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
928
        self.assertEqual(
929
            smart.request.request_handlers.get('Repository.is_shared'),
930
            smart.repository.SmartServerRepositoryIsShared)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
931
        self.assertEqual(
932
            smart.request.request_handlers.get('Repository.lock_write'),
933
            smart.repository.SmartServerRepositoryLockWrite)
934
        self.assertEqual(
2535.3.69 by Andrew Bennetts
Add check for Repository.stream_knit_data_for_revisions to TestHandlers.test_registered_methods.
935
            smart.request.request_handlers.get(
2535.4.21 by Andrew Bennetts
Rename smart request, to avoid confusion with non-chunked version merged into bzr.dev.
936
                'Repository.chunked_stream_knit_data_for_revisions'),
2535.3.69 by Andrew Bennetts
Add check for Repository.stream_knit_data_for_revisions to TestHandlers.test_registered_methods.
937
            smart.repository.SmartServerRepositoryStreamKnitDataForRevisions)
938
        self.assertEqual(
939
            smart.request.request_handlers.get('Repository.tarball'),
940
            smart.repository.SmartServerRepositoryTarball)
941
        self.assertEqual(
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
942
            smart.request.request_handlers.get('Repository.unlock'),
943
            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.
944
        self.assertEqual(
945
            smart.request.request_handlers.get('Transport.is_readonly'),
946
            smart.request.SmartServerIsReadonly)