/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/tests/test_smart.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
"""
26
26
 
27
27
import bz2
28
 
from io import BytesIO
29
 
import tarfile
30
28
import zlib
31
29
 
32
 
from breezy import (
 
30
from brzlib import (
33
31
    bencode,
34
32
    branch as _mod_branch,
35
33
    controldir,
36
34
    errors,
37
35
    gpg,
 
36
    inventory_delta,
38
37
    tests,
39
38
    transport,
40
39
    urlutils,
41
 
    )
42
 
from breezy.bzr import (
43
 
    branch as _mod_bzrbranch,
44
 
    inventory_delta,
45
40
    versionedfile,
46
41
    )
47
 
from breezy.bzr.smart import (
 
42
from brzlib.smart import (
48
43
    branch as smart_branch,
49
44
    bzrdir as smart_dir,
50
45
    repository as smart_repo,
53
48
    server,
54
49
    vfs,
55
50
    )
56
 
from breezy.bzr.testament import Testament
57
 
from breezy.tests import test_server
58
 
from breezy.transport import (
 
51
from brzlib.testament import Testament
 
52
from brzlib.tests import test_server
 
53
from brzlib.transport import (
59
54
    chroot,
60
55
    memory,
61
56
    )
62
57
 
63
58
 
64
 
def load_tests(loader, standard_tests, pattern):
 
59
def load_tests(standard_tests, module, loader):
65
60
    """Multiply tests version and protocol consistency."""
66
61
    # FindRepository tests.
67
62
    scenarios = [
72
67
        ("find_repositoryV3", {
73
68
            "_request_class": smart_dir.SmartServerRequestFindRepositoryV3}),
74
69
        ]
75
 
    to_adapt, result = tests.split_suite_by_re(
76
 
        standard_tests, "TestSmartServerRequestFindRepository")
77
 
    v2_only, v1_and_2 = tests.split_suite_by_re(to_adapt, "_v2")
 
70
    to_adapt, result = tests.split_suite_by_re(standard_tests,
 
71
        "TestSmartServerRequestFindRepository")
 
72
    v2_only, v1_and_2 = tests.split_suite_by_re(to_adapt,
 
73
        "_v2")
78
74
    tests.multiply_tests(v1_and_2, scenarios, result)
79
75
    # The first scenario is only applicable to v1 protocols, it is deleted
80
76
    # since.
122
118
 
123
119
    def test_repeated_substreams_same_kind_are_one_stream(self):
124
120
        # Make a stream - an iterable of bytestrings.
125
 
        stream = [
126
 
            ('text', [versionedfile.FulltextContentFactory((b'k1',), None,
127
 
             None, b'foo')]),
128
 
            ('text', [versionedfile.FulltextContentFactory((b'k2',), None,
129
 
             None, b'bar')])]
 
121
        stream = [('text', [versionedfile.FulltextContentFactory(('k1',), None,
 
122
            None, 'foo')]),('text', [
 
123
            versionedfile.FulltextContentFactory(('k2',), None, None, 'bar')])]
130
124
        fmt = controldir.format_registry.get('pack-0.92')().repository_format
131
125
        bytes = smart_repo._stream_to_byte_stream(stream, fmt)
132
126
        streams = []
142
136
class TestSmartServerResponse(tests.TestCase):
143
137
 
144
138
    def test__eq__(self):
145
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
146
 
                         smart_req.SmartServerResponse((b'ok', )))
147
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'body'),
148
 
                         smart_req.SmartServerResponse((b'ok', ), b'body'))
149
 
        self.assertNotEqual(smart_req.SmartServerResponse((b'ok', )),
150
 
                            smart_req.SmartServerResponse((b'notok', )))
151
 
        self.assertNotEqual(smart_req.SmartServerResponse((b'ok', ), b'body'),
152
 
                            smart_req.SmartServerResponse((b'ok', )))
 
139
        self.assertEqual(smart_req.SmartServerResponse(('ok', )),
 
140
            smart_req.SmartServerResponse(('ok', )))
 
141
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), 'body'),
 
142
            smart_req.SmartServerResponse(('ok', ), 'body'))
 
143
        self.assertNotEqual(smart_req.SmartServerResponse(('ok', )),
 
144
            smart_req.SmartServerResponse(('notok', )))
 
145
        self.assertNotEqual(smart_req.SmartServerResponse(('ok', ), 'body'),
 
146
            smart_req.SmartServerResponse(('ok', )))
153
147
        self.assertNotEqual(None,
154
 
                            smart_req.SmartServerResponse((b'ok', )))
 
148
            smart_req.SmartServerResponse(('ok', )))
155
149
 
156
150
    def test__str__(self):
157
151
        """SmartServerResponses can be stringified."""
158
 
        self.assertIn(
159
 
            str(smart_req.SuccessfulSmartServerResponse((b'args',), b'body')),
160
 
            ("<SuccessfulSmartServerResponse args=(b'args',) body=b'body'>",
161
 
             "<SuccessfulSmartServerResponse args=('args',) body='body'>"))
162
 
        self.assertIn(
163
 
            str(smart_req.FailedSmartServerResponse((b'args',), b'body')),
164
 
            ("<FailedSmartServerResponse args=(b'args',) body=b'body'>",
165
 
             "<FailedSmartServerResponse args=('args',) body='body'>"))
 
152
        self.assertEqual(
 
153
            "<SuccessfulSmartServerResponse args=('args',) body='body'>",
 
154
            str(smart_req.SuccessfulSmartServerResponse(('args',), 'body')))
 
155
        self.assertEqual(
 
156
            "<FailedSmartServerResponse args=('args',) body='body'>",
 
157
            str(smart_req.FailedSmartServerResponse(('args',), 'body')))
166
158
 
167
159
 
168
160
class TestSmartServerRequest(tests.TestCaseWithMemoryTransport):
170
162
    def test_translate_client_path(self):
171
163
        transport = self.get_transport()
172
164
        request = smart_req.SmartServerRequest(transport, 'foo/')
173
 
        self.assertEqual('./', request.translate_client_path(b'foo/'))
174
 
        self.assertRaises(
175
 
            urlutils.InvalidURLJoin, request.translate_client_path, b'foo/..')
176
 
        self.assertRaises(
177
 
            errors.PathNotChild, request.translate_client_path, b'/')
178
 
        self.assertRaises(
179
 
            errors.PathNotChild, request.translate_client_path, b'bar/')
180
 
        self.assertEqual('./baz', request.translate_client_path(b'foo/baz'))
181
 
        e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'
182
 
        self.assertEqual(
183
 
            u'./' + urlutils.escape(e_acute),
184
 
            request.translate_client_path(b'foo/' + e_acute.encode('utf-8')))
 
165
        self.assertEqual('./', request.translate_client_path('foo/'))
 
166
        self.assertRaises(
 
167
            errors.InvalidURLJoin, request.translate_client_path, 'foo/..')
 
168
        self.assertRaises(
 
169
            errors.PathNotChild, request.translate_client_path, '/')
 
170
        self.assertRaises(
 
171
            errors.PathNotChild, request.translate_client_path, 'bar/')
 
172
        self.assertEqual('./baz', request.translate_client_path('foo/baz'))
 
173
        e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'.encode('utf-8')
 
174
        self.assertEqual('./' + urlutils.escape(e_acute),
 
175
                         request.translate_client_path('foo/' + e_acute))
185
176
 
186
177
    def test_translate_client_path_vfs(self):
187
178
        """VfsRequests receive escaped paths rather than raw UTF-8."""
188
179
        transport = self.get_transport()
189
180
        request = vfs.VfsRequest(transport, 'foo/')
190
 
        e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'
191
 
        escaped = urlutils.escape(u'foo/' + e_acute)
192
 
        self.assertEqual(
193
 
            './' + urlutils.escape(e_acute),
194
 
            request.translate_client_path(escaped.encode('ascii')))
 
181
        e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'.encode('utf-8')
 
182
        escaped = urlutils.escape('foo/' + e_acute)
 
183
        self.assertEqual('./' + urlutils.escape(e_acute),
 
184
                         request.translate_client_path(escaped))
195
185
 
196
186
    def test_transport_from_client_path(self):
197
187
        transport = self.get_transport()
198
188
        request = smart_req.SmartServerRequest(transport, 'foo/')
199
189
        self.assertEqual(
200
190
            transport.base,
201
 
            request.transport_from_client_path(b'foo/').base)
 
191
            request.transport_from_client_path('foo/').base)
202
192
 
203
193
 
204
194
class TestSmartServerBzrDirRequestCloningMetaDir(
205
 
        tests.TestCaseWithMemoryTransport):
 
195
    tests.TestCaseWithMemoryTransport):
206
196
    """Tests for BzrDir.cloning_metadir."""
207
197
 
208
198
    def test_cloning_metadir(self):
209
199
        """When there is a bzrdir present, the call succeeds."""
210
200
        backing = self.get_transport()
211
 
        dir = self.make_controldir('.')
 
201
        dir = self.make_bzrdir('.')
212
202
        local_result = dir.cloning_metadir()
213
203
        request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
214
204
        request = request_class(backing)
215
205
        expected = smart_req.SuccessfulSmartServerResponse(
216
206
            (local_result.network_name(),
217
 
             local_result.repository_format.network_name(),
218
 
             (b'branch', local_result.get_branch_format().network_name())))
219
 
        self.assertEqual(expected, request.execute(b'', b'False'))
 
207
            local_result.repository_format.network_name(),
 
208
            ('branch', local_result.get_branch_format().network_name())))
 
209
        self.assertEqual(expected, request.execute('', 'False'))
220
210
 
221
211
    def test_cloning_metadir_reference(self):
222
212
        """The request fails when bzrdir contains a branch reference."""
223
213
        backing = self.get_transport()
224
214
        referenced_branch = self.make_branch('referenced')
225
 
        dir = self.make_controldir('.')
226
 
        dir.cloning_metadir()
227
 
        _mod_bzrbranch.BranchReferenceFormat().initialize(
 
215
        dir = self.make_bzrdir('.')
 
216
        local_result = dir.cloning_metadir()
 
217
        reference = _mod_branch.BranchReferenceFormat().initialize(
228
218
            dir, target_branch=referenced_branch)
229
 
        _mod_bzrbranch.BranchReferenceFormat().get_reference(dir)
 
219
        reference_url = _mod_branch.BranchReferenceFormat().get_reference(dir)
230
220
        # The server shouldn't try to follow the branch reference, so it's fine
231
221
        # if the referenced branch isn't reachable.
232
222
        backing.rename('referenced', 'moved')
233
223
        request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
234
224
        request = request_class(backing)
235
 
        expected = smart_req.FailedSmartServerResponse((b'BranchReference',))
236
 
        self.assertEqual(expected, request.execute(b'', b'False'))
237
 
 
238
 
 
239
 
class TestSmartServerBzrDirRequestCheckoutMetaDir(
240
 
        tests.TestCaseWithMemoryTransport):
 
225
        expected = smart_req.FailedSmartServerResponse(('BranchReference',))
 
226
        self.assertEqual(expected, request.execute('', 'False'))
 
227
 
 
228
 
 
229
class TestSmartServerBzrDirRequestCloningMetaDir(
 
230
    tests.TestCaseWithMemoryTransport):
241
231
    """Tests for BzrDir.checkout_metadir."""
242
232
 
243
233
    def test_checkout_metadir(self):
244
234
        backing = self.get_transport()
245
235
        request = smart_dir.SmartServerBzrDirRequestCheckoutMetaDir(
246
236
            backing)
247
 
        self.make_branch('.', format='2a')
248
 
        response = request.execute(b'')
 
237
        branch = self.make_branch('.', format='2a')
 
238
        response = request.execute('')
249
239
        self.assertEqual(
250
240
            smart_req.SmartServerResponse(
251
 
                (b'Bazaar-NG meta directory, format 1\n',
252
 
                 b'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
253
 
                 b'Bazaar Branch Format 7 (needs bzr 1.6)\n')),
 
241
                ('Bazaar-NG meta directory, format 1\n',
 
242
                 'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
 
243
                 'Bazaar Branch Format 7 (needs bzr 1.6)\n')),
254
244
            response)
255
245
 
256
246
 
257
247
class TestSmartServerBzrDirRequestDestroyBranch(
258
 
        tests.TestCaseWithMemoryTransport):
 
248
    tests.TestCaseWithMemoryTransport):
259
249
    """Tests for BzrDir.destroy_branch."""
260
250
 
261
251
    def test_destroy_branch_default(self):
262
252
        """The default branch can be removed."""
263
253
        backing = self.get_transport()
264
 
        self.make_branch('.')
 
254
        dir = self.make_branch('.').bzrdir
265
255
        request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
266
256
        request = request_class(backing)
267
 
        expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
268
 
        self.assertEqual(expected, request.execute(b'', None))
 
257
        expected = smart_req.SuccessfulSmartServerResponse(('ok',))
 
258
        self.assertEqual(expected, request.execute('', None))
269
259
 
270
260
    def test_destroy_branch_named(self):
271
261
        """A named branch can be removed."""
272
262
        backing = self.get_transport()
273
 
        dir = self.make_repository('.', format="development-colo").controldir
 
263
        dir = self.make_repository('.', format="development-colo").bzrdir
274
264
        dir.create_branch(name="branchname")
275
265
        request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
276
266
        request = request_class(backing)
277
 
        expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
278
 
        self.assertEqual(expected, request.execute(b'', b"branchname"))
 
267
        expected = smart_req.SuccessfulSmartServerResponse(('ok',))
 
268
        self.assertEqual(expected, request.execute('', "branchname"))
279
269
 
280
270
    def test_destroy_branch_missing(self):
281
271
        """An error is raised if the branch didn't exist."""
282
272
        backing = self.get_transport()
283
 
        self.make_controldir('.', format="development-colo")
 
273
        dir = self.make_bzrdir('.', format="development-colo")
284
274
        request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
285
275
        request = request_class(backing)
286
 
        expected = smart_req.FailedSmartServerResponse((b'nobranch',), None)
287
 
        self.assertEqual(expected, request.execute(b'', b"branchname"))
 
276
        expected = smart_req.FailedSmartServerResponse(('nobranch',), None)
 
277
        self.assertEqual(expected, request.execute('', "branchname"))
288
278
 
289
279
 
290
280
class TestSmartServerBzrDirRequestHasWorkingTree(
291
 
        tests.TestCaseWithTransport):
 
281
    tests.TestCaseWithTransport):
292
282
    """Tests for BzrDir.has_workingtree."""
293
283
 
294
284
    def test_has_workingtree_yes(self):
295
285
        """A working tree is present."""
296
286
        backing = self.get_transport()
297
 
        self.make_branch_and_tree('.')
 
287
        dir = self.make_branch_and_tree('.').bzrdir
298
288
        request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
299
289
        request = request_class(backing)
300
 
        expected = smart_req.SuccessfulSmartServerResponse((b'yes',))
301
 
        self.assertEqual(expected, request.execute(b''))
 
290
        expected = smart_req.SuccessfulSmartServerResponse(('yes',))
 
291
        self.assertEqual(expected, request.execute(''))
302
292
 
303
293
    def test_has_workingtree_no(self):
304
294
        """A working tree is missing."""
305
295
        backing = self.get_transport()
306
 
        self.make_controldir('.')
 
296
        dir = self.make_bzrdir('.')
307
297
        request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
308
298
        request = request_class(backing)
309
 
        expected = smart_req.SuccessfulSmartServerResponse((b'no',))
310
 
        self.assertEqual(expected, request.execute(b''))
 
299
        expected = smart_req.SuccessfulSmartServerResponse(('no',))
 
300
        self.assertEqual(expected, request.execute(''))
311
301
 
312
302
 
313
303
class TestSmartServerBzrDirRequestDestroyRepository(
314
 
        tests.TestCaseWithMemoryTransport):
 
304
    tests.TestCaseWithMemoryTransport):
315
305
    """Tests for BzrDir.destroy_repository."""
316
306
 
317
307
    def test_destroy_repository_default(self):
318
308
        """The repository can be removed."""
319
309
        backing = self.get_transport()
320
 
        self.make_repository('.')
 
310
        dir = self.make_repository('.').bzrdir
321
311
        request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
322
312
        request = request_class(backing)
323
 
        expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
324
 
        self.assertEqual(expected, request.execute(b''))
 
313
        expected = smart_req.SuccessfulSmartServerResponse(('ok',))
 
314
        self.assertEqual(expected, request.execute(''))
325
315
 
326
316
    def test_destroy_repository_missing(self):
327
317
        """An error is raised if the repository didn't exist."""
328
318
        backing = self.get_transport()
329
 
        self.make_controldir('.')
 
319
        dir = self.make_bzrdir('.')
330
320
        request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
331
321
        request = request_class(backing)
332
322
        expected = smart_req.FailedSmartServerResponse(
333
 
            (b'norepository',), None)
334
 
        self.assertEqual(expected, request.execute(b''))
335
 
 
336
 
 
337
 
class TestSmartServerRequestCreateRepository(
338
 
        tests.TestCaseWithMemoryTransport):
 
323
            ('norepository',), None)
 
324
        self.assertEqual(expected, request.execute(''))
 
325
 
 
326
 
 
327
class TestSmartServerRequestCreateRepository(tests.TestCaseWithMemoryTransport):
339
328
    """Tests for BzrDir.create_repository."""
340
329
 
341
330
    def test_makes_repository(self):
342
331
        """When there is a bzrdir present, the call succeeds."""
343
332
        backing = self.get_transport()
344
 
        self.make_controldir('.')
 
333
        self.make_bzrdir('.')
345
334
        request_class = smart_dir.SmartServerRequestCreateRepository
346
335
        request = request_class(backing)
347
336
        reference_bzrdir_format = controldir.format_registry.get('pack-0.92')()
348
337
        reference_format = reference_bzrdir_format.repository_format
349
338
        network_name = reference_format.network_name()
350
339
        expected = smart_req.SuccessfulSmartServerResponse(
351
 
            (b'ok', b'no', b'no', b'no', network_name))
352
 
        self.assertEqual(expected, request.execute(b'', network_name, b'True'))
 
340
            ('ok', 'no', 'no', 'no', network_name))
 
341
        self.assertEqual(expected, request.execute('', network_name, 'True'))
353
342
 
354
343
 
355
344
class TestSmartServerRequestFindRepository(tests.TestCaseWithMemoryTransport):
356
345
    """Tests for BzrDir.find_repository."""
357
346
 
358
347
    def test_no_repository(self):
359
 
        """If no repository is found, ('norepository', ) is returned."""
 
348
        """When there is no repository to be found, ('norepository', ) is returned."""
360
349
        backing = self.get_transport()
361
350
        request = self._request_class(backing)
362
 
        self.make_controldir('.')
363
 
        self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
364
 
                         request.execute(b''))
 
351
        self.make_bzrdir('.')
 
352
        self.assertEqual(smart_req.SmartServerResponse(('norepository', )),
 
353
            request.execute(''))
365
354
 
366
355
    def test_nonshared_repository(self):
367
356
        # nonshared repositorys only allow 'find' to return a handle when the
370
359
        backing = self.get_transport()
371
360
        request = self._request_class(backing)
372
361
        result = self._make_repository_and_result()
373
 
        self.assertEqual(result, request.execute(b''))
374
 
        self.make_controldir('subdir')
375
 
        self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
376
 
                         request.execute(b'subdir'))
 
362
        self.assertEqual(result, request.execute(''))
 
363
        self.make_bzrdir('subdir')
 
364
        self.assertEqual(smart_req.SmartServerResponse(('norepository', )),
 
365
            request.execute('subdir'))
377
366
 
378
367
    def _make_repository_and_result(self, shared=False, format=None):
379
368
        """Convenience function to setup a repository.
382
371
        """
383
372
        repo = self.make_repository('.', shared=shared, format=format)
384
373
        if repo.supports_rich_root():
385
 
            rich_root = b'yes'
 
374
            rich_root = 'yes'
386
375
        else:
387
 
            rich_root = b'no'
 
376
            rich_root = 'no'
388
377
        if repo._format.supports_tree_reference:
389
 
            subtrees = b'yes'
 
378
            subtrees = 'yes'
390
379
        else:
391
 
            subtrees = b'no'
 
380
            subtrees = 'no'
392
381
        if repo._format.supports_external_lookups:
393
 
            external = b'yes'
 
382
            external = 'yes'
394
383
        else:
395
 
            external = b'no'
 
384
            external = 'no'
396
385
        if (smart_dir.SmartServerRequestFindRepositoryV3 ==
397
 
                self._request_class):
 
386
            self._request_class):
398
387
            return smart_req.SuccessfulSmartServerResponse(
399
 
                (b'ok', b'', rich_root, subtrees, external,
 
388
                ('ok', '', rich_root, subtrees, external,
400
389
                 repo._format.network_name()))
401
390
        elif (smart_dir.SmartServerRequestFindRepositoryV2 ==
402
 
              self._request_class):
 
391
            self._request_class):
403
392
            # All tests so far are on formats, and for non-external
404
393
            # repositories.
405
394
            return smart_req.SuccessfulSmartServerResponse(
406
 
                (b'ok', b'', rich_root, subtrees, external))
 
395
                ('ok', '', rich_root, subtrees, external))
407
396
        else:
408
397
            return smart_req.SuccessfulSmartServerResponse(
409
 
                (b'ok', b'', rich_root, subtrees))
 
398
                ('ok', '', rich_root, subtrees))
410
399
 
411
400
    def test_shared_repository(self):
412
 
        """for a shared repository, we get 'ok', 'relpath-to-repo'."""
 
401
        """When there is a shared repository, we get 'ok', 'relpath-to-repo'."""
413
402
        backing = self.get_transport()
414
403
        request = self._request_class(backing)
415
404
        result = self._make_repository_and_result(shared=True)
416
 
        self.assertEqual(result, request.execute(b''))
417
 
        self.make_controldir('subdir')
 
405
        self.assertEqual(result, request.execute(''))
 
406
        self.make_bzrdir('subdir')
418
407
        result2 = smart_req.SmartServerResponse(
419
 
            result.args[0:1] + (b'..', ) + result.args[2:])
 
408
            result.args[0:1] + ('..', ) + result.args[2:])
420
409
        self.assertEqual(result2,
421
 
                         request.execute(b'subdir'))
422
 
        self.make_controldir('subdir/deeper')
 
410
            request.execute('subdir'))
 
411
        self.make_bzrdir('subdir/deeper')
423
412
        result3 = smart_req.SmartServerResponse(
424
 
            result.args[0:1] + (b'../..', ) + result.args[2:])
 
413
            result.args[0:1] + ('../..', ) + result.args[2:])
425
414
        self.assertEqual(result3,
426
 
                         request.execute(b'subdir/deeper'))
 
415
            request.execute('subdir/deeper'))
427
416
 
428
417
    def test_rich_root_and_subtree_encoding(self):
429
418
        """Test for the format attributes for rich root and subtree support."""
432
421
        result = self._make_repository_and_result(
433
422
            format='development-subtree')
434
423
        # check the test will be valid
435
 
        self.assertEqual(b'yes', result.args[2])
436
 
        self.assertEqual(b'yes', result.args[3])
437
 
        self.assertEqual(result, request.execute(b''))
 
424
        self.assertEqual('yes', result.args[2])
 
425
        self.assertEqual('yes', result.args[3])
 
426
        self.assertEqual(result, request.execute(''))
438
427
 
439
428
    def test_supports_external_lookups_no_v2(self):
440
429
        """Test for the supports_external_lookups attribute."""
443
432
        result = self._make_repository_and_result(
444
433
            format='development-subtree')
445
434
        # check the test will be valid
446
 
        self.assertEqual(b'yes', result.args[4])
447
 
        self.assertEqual(result, request.execute(b''))
 
435
        self.assertEqual('yes', result.args[4])
 
436
        self.assertEqual(result, request.execute(''))
448
437
 
449
438
 
450
439
class TestSmartServerBzrDirRequestGetConfigFile(
451
 
        tests.TestCaseWithMemoryTransport):
 
440
    tests.TestCaseWithMemoryTransport):
452
441
    """Tests for BzrDir.get_config_file."""
453
442
 
454
443
    def test_present(self):
455
444
        backing = self.get_transport()
456
 
        dir = self.make_controldir('.')
 
445
        dir = self.make_bzrdir('.')
457
446
        dir.get_config().set_default_stack_on("/")
458
447
        local_result = dir._get_config()._get_config_file().read()
459
448
        request_class = smart_dir.SmartServerBzrDirRequestConfigFile
460
449
        request = request_class(backing)
461
450
        expected = smart_req.SuccessfulSmartServerResponse((), local_result)
462
 
        self.assertEqual(expected, request.execute(b''))
 
451
        self.assertEqual(expected, request.execute(''))
463
452
 
464
453
    def test_missing(self):
465
454
        backing = self.get_transport()
466
 
        self.make_controldir('.')
 
455
        dir = self.make_bzrdir('.')
467
456
        request_class = smart_dir.SmartServerBzrDirRequestConfigFile
468
457
        request = request_class(backing)
469
 
        expected = smart_req.SuccessfulSmartServerResponse((), b'')
470
 
        self.assertEqual(expected, request.execute(b''))
 
458
        expected = smart_req.SuccessfulSmartServerResponse((), '')
 
459
        self.assertEqual(expected, request.execute(''))
471
460
 
472
461
 
473
462
class TestSmartServerBzrDirRequestGetBranches(
474
 
        tests.TestCaseWithMemoryTransport):
 
463
    tests.TestCaseWithMemoryTransport):
475
464
    """Tests for BzrDir.get_branches."""
476
465
 
477
466
    def test_simple(self):
480
469
        request_class = smart_dir.SmartServerBzrDirRequestGetBranches
481
470
        request = request_class(backing)
482
471
        local_result = bencode.bencode(
483
 
            {b"": (b"branch", branch._format.network_name())})
 
472
            {"": ("branch", branch._format.network_name())})
484
473
        expected = smart_req.SuccessfulSmartServerResponse(
485
 
            (b"success", ), local_result)
486
 
        self.assertEqual(expected, request.execute(b''))
 
474
            ("success", ), local_result)
 
475
        self.assertEqual(expected, request.execute(''))
487
476
 
488
477
    def test_empty(self):
489
478
        backing = self.get_transport()
490
 
        self.make_controldir('.')
 
479
        dir = self.make_bzrdir('.')
491
480
        request_class = smart_dir.SmartServerBzrDirRequestGetBranches
492
481
        request = request_class(backing)
493
482
        local_result = bencode.bencode({})
494
483
        expected = smart_req.SuccessfulSmartServerResponse(
495
 
            (b'success',), local_result)
496
 
        self.assertEqual(expected, request.execute(b''))
497
 
 
498
 
 
499
 
class TestSmartServerRequestInitializeBzrDir(
500
 
        tests.TestCaseWithMemoryTransport):
 
484
            ('success',), local_result)
 
485
        self.assertEqual(expected, request.execute(''))
 
486
 
 
487
 
 
488
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithMemoryTransport):
501
489
 
502
490
    def test_empty_dir(self):
503
491
        """Initializing an empty dir should succeed and do it."""
504
492
        backing = self.get_transport()
505
493
        request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
506
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
507
 
                         request.execute(b''))
 
494
        self.assertEqual(smart_req.SmartServerResponse(('ok', )),
 
495
            request.execute(''))
508
496
        made_dir = controldir.ControlDir.open_from_transport(backing)
509
497
        # no branch, tree or repository is expected with the current
510
498
        # default formart.
517
505
        backing = self.get_transport()
518
506
        request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
519
507
        self.assertRaises(errors.NoSuchFile,
520
 
                          request.execute, b'subdir')
 
508
            request.execute, 'subdir')
521
509
 
522
510
    def test_initialized_dir(self):
523
511
        """Initializing an extant bzrdir should fail like the bzrdir api."""
524
512
        backing = self.get_transport()
525
513
        request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
526
 
        self.make_controldir('subdir')
 
514
        self.make_bzrdir('subdir')
527
515
        self.assertRaises(errors.AlreadyControlDirError,
528
 
                          request.execute, b'subdir')
 
516
            request.execute, 'subdir')
529
517
 
530
518
 
531
519
class TestSmartServerRequestBzrDirInitializeEx(
532
 
        tests.TestCaseWithMemoryTransport):
 
520
    tests.TestCaseWithMemoryTransport):
533
521
    """Basic tests for BzrDir.initialize_ex_1.16 in the smart server.
534
522
 
535
523
    The main unit tests in test_bzrdir exercise the API comprehensively.
538
526
    def test_empty_dir(self):
539
527
        """Initializing an empty dir should succeed and do it."""
540
528
        backing = self.get_transport()
541
 
        name = self.make_controldir('reference')._format.network_name()
 
529
        name = self.make_bzrdir('reference')._format.network_name()
542
530
        request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
543
531
        self.assertEqual(
544
 
            smart_req.SmartServerResponse((b'', b'', b'', b'', b'', b'', name,
545
 
                                           b'False', b'', b'', b'')),
546
 
            request.execute(name, b'', b'True', b'False', b'False', b'', b'',
547
 
                            b'', b'', b'False'))
 
532
            smart_req.SmartServerResponse(('', '', '', '', '', '', name,
 
533
                                           'False', '', '', '')),
 
534
            request.execute(name, '', 'True', 'False', 'False', '', '', '', '',
 
535
                            'False'))
548
536
        made_dir = controldir.ControlDir.open_from_transport(backing)
549
537
        # no branch, tree or repository is expected with the current
550
538
        # default format.
555
543
    def test_missing_dir(self):
556
544
        """Initializing a missing directory should fail like the bzrdir api."""
557
545
        backing = self.get_transport()
558
 
        name = self.make_controldir('reference')._format.network_name()
 
546
        name = self.make_bzrdir('reference')._format.network_name()
559
547
        request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
560
548
        self.assertRaises(errors.NoSuchFile, request.execute, name,
561
 
                          b'subdir/dir', b'False', b'False', b'False', b'',
562
 
                          b'', b'', b'', b'False')
 
549
            'subdir/dir', 'False', 'False', 'False', '', '', '', '', 'False')
563
550
 
564
551
    def test_initialized_dir(self):
565
552
        """Initializing an extant directory should fail like the bzrdir api."""
566
553
        backing = self.get_transport()
567
 
        name = self.make_controldir('reference')._format.network_name()
 
554
        name = self.make_bzrdir('reference')._format.network_name()
568
555
        request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
569
 
        self.make_controldir('subdir')
570
 
        self.assertRaises(errors.FileExists, request.execute, name, b'subdir',
571
 
                          b'False', b'False', b'False', b'', b'', b'', b'',
572
 
                          b'False')
 
556
        self.make_bzrdir('subdir')
 
557
        self.assertRaises(errors.FileExists, request.execute, name, 'subdir',
 
558
            'False', 'False', 'False', '', '', '', '', 'False')
573
559
 
574
560
 
575
561
class TestSmartServerRequestOpenBzrDir(tests.TestCaseWithMemoryTransport):
577
563
    def test_no_directory(self):
578
564
        backing = self.get_transport()
579
565
        request = smart_dir.SmartServerRequestOpenBzrDir(backing)
580
 
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
581
 
                         request.execute(b'does-not-exist'))
 
566
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
567
            request.execute('does-not-exist'))
582
568
 
583
569
    def test_empty_directory(self):
584
570
        backing = self.get_transport()
585
571
        backing.mkdir('empty')
586
572
        request = smart_dir.SmartServerRequestOpenBzrDir(backing)
587
 
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
588
 
                         request.execute(b'empty'))
 
573
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
574
            request.execute('empty'))
589
575
 
590
576
    def test_outside_root_client_path(self):
591
577
        backing = self.get_transport()
592
 
        request = smart_dir.SmartServerRequestOpenBzrDir(
593
 
            backing, root_client_path='root')
594
 
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
595
 
                         request.execute(b'not-root'))
 
578
        request = smart_dir.SmartServerRequestOpenBzrDir(backing,
 
579
            root_client_path='root')
 
580
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
581
            request.execute('not-root'))
596
582
 
597
583
 
598
584
class TestSmartServerRequestOpenBzrDir_2_1(tests.TestCaseWithMemoryTransport):
600
586
    def test_no_directory(self):
601
587
        backing = self.get_transport()
602
588
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
603
 
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
604
 
                         request.execute(b'does-not-exist'))
 
589
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
590
            request.execute('does-not-exist'))
605
591
 
606
592
    def test_empty_directory(self):
607
593
        backing = self.get_transport()
608
594
        backing.mkdir('empty')
609
595
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
610
 
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
611
 
                         request.execute(b'empty'))
 
596
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
597
            request.execute('empty'))
612
598
 
613
599
    def test_present_without_workingtree(self):
614
600
        backing = self.get_transport()
615
601
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
616
 
        self.make_controldir('.')
617
 
        self.assertEqual(smart_req.SmartServerResponse((b'yes', b'no')),
618
 
                         request.execute(b''))
 
602
        self.make_bzrdir('.')
 
603
        self.assertEqual(smart_req.SmartServerResponse(('yes', 'no')),
 
604
            request.execute(''))
619
605
 
620
606
    def test_outside_root_client_path(self):
621
607
        backing = self.get_transport()
622
 
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(
623
 
            backing, root_client_path='root')
624
 
        self.assertEqual(smart_req.SmartServerResponse((b'no',)),
625
 
                         request.execute(b'not-root'))
 
608
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing,
 
609
            root_client_path='root')
 
610
        self.assertEqual(smart_req.SmartServerResponse(('no',)),
 
611
            request.execute('not-root'))
626
612
 
627
613
 
628
614
class TestSmartServerRequestOpenBzrDir_2_1_disk(TestCaseWithChrootedTransport):
631
617
        self.vfs_transport_factory = test_server.LocalURLServer
632
618
        backing = self.get_transport()
633
619
        request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
634
 
        bd = self.make_controldir('.')
 
620
        bd = self.make_bzrdir('.')
635
621
        bd.create_repository()
636
622
        bd.create_branch()
637
623
        bd.create_workingtree()
638
 
        self.assertEqual(smart_req.SmartServerResponse((b'yes', b'yes')),
639
 
                         request.execute(b''))
 
624
        self.assertEqual(smart_req.SmartServerResponse(('yes', 'yes')),
 
625
            request.execute(''))
640
626
 
641
627
 
642
628
class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
645
631
        """When there is no branch, ('nobranch', ) is returned."""
646
632
        backing = self.get_transport()
647
633
        request = smart_dir.SmartServerRequestOpenBranch(backing)
648
 
        self.make_controldir('.')
649
 
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
650
 
                         request.execute(b''))
 
634
        self.make_bzrdir('.')
 
635
        self.assertEqual(smart_req.SmartServerResponse(('nobranch', )),
 
636
            request.execute(''))
651
637
 
652
638
    def test_branch(self):
653
639
        """When there is a branch, 'ok' is returned."""
654
640
        backing = self.get_transport()
655
641
        request = smart_dir.SmartServerRequestOpenBranch(backing)
656
642
        self.make_branch('.')
657
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', b'')),
658
 
                         request.execute(b''))
 
643
        self.assertEqual(smart_req.SmartServerResponse(('ok', '')),
 
644
            request.execute(''))
659
645
 
660
646
    def test_branch_reference(self):
661
647
        """When there is a branch reference, the reference URL is returned."""
663
649
        backing = self.get_transport()
664
650
        request = smart_dir.SmartServerRequestOpenBranch(backing)
665
651
        branch = self.make_branch('branch')
666
 
        checkout = branch.create_checkout('reference', lightweight=True)
667
 
        reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
668
 
            checkout.controldir).encode('utf-8')
 
652
        checkout = branch.create_checkout('reference',lightweight=True)
 
653
        reference_url = _mod_branch.BranchReferenceFormat().get_reference(
 
654
            checkout.bzrdir)
669
655
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
670
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', reference_url)),
671
 
                         request.execute(b'reference'))
 
656
        self.assertEqual(smart_req.SmartServerResponse(('ok', reference_url)),
 
657
            request.execute('reference'))
672
658
 
673
659
    def test_notification_on_branch_from_repository(self):
674
660
        """When there is a repository, the error should return details."""
675
661
        backing = self.get_transport()
676
662
        request = smart_dir.SmartServerRequestOpenBranch(backing)
677
 
        self.make_repository('.')
678
 
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
679
 
                         request.execute(b''))
 
663
        repo = self.make_repository('.')
 
664
        self.assertEqual(smart_req.SmartServerResponse(('nobranch',)),
 
665
            request.execute(''))
680
666
 
681
667
 
682
668
class TestSmartServerRequestOpenBranchV2(TestCaseWithChrootedTransport):
684
670
    def test_no_branch(self):
685
671
        """When there is no branch, ('nobranch', ) is returned."""
686
672
        backing = self.get_transport()
687
 
        self.make_controldir('.')
 
673
        self.make_bzrdir('.')
688
674
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
689
 
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
690
 
                         request.execute(b''))
 
675
        self.assertEqual(smart_req.SmartServerResponse(('nobranch', )),
 
676
            request.execute(''))
691
677
 
692
678
    def test_branch(self):
693
679
        """When there is a branch, 'ok' is returned."""
695
681
        expected = self.make_branch('.')._format.network_name()
696
682
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
697
683
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
698
 
            (b'branch', expected)),
699
 
            request.execute(b''))
 
684
                ('branch', expected)),
 
685
                         request.execute(''))
700
686
 
701
687
    def test_branch_reference(self):
702
688
        """When there is a branch reference, the reference URL is returned."""
704
690
        backing = self.get_transport()
705
691
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
706
692
        branch = self.make_branch('branch')
707
 
        checkout = branch.create_checkout('reference', lightweight=True)
708
 
        reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
709
 
            checkout.controldir).encode('utf-8')
 
693
        checkout = branch.create_checkout('reference',lightweight=True)
 
694
        reference_url = _mod_branch.BranchReferenceFormat().get_reference(
 
695
            checkout.bzrdir)
710
696
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
711
697
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
712
 
            (b'ref', reference_url)),
713
 
            request.execute(b'reference'))
 
698
                ('ref', reference_url)),
 
699
                         request.execute('reference'))
714
700
 
715
701
    def test_stacked_branch(self):
716
702
        """Opening a stacked branch does not open the stacked-on branch."""
724
710
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
725
711
        request.setup_jail()
726
712
        try:
727
 
            response = request.execute(b'feature')
 
713
            response = request.execute('feature')
728
714
        finally:
729
715
            request.teardown_jail()
730
716
        expected_format = feature._format.network_name()
731
717
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
732
 
            (b'branch', expected_format)),
733
 
            response)
 
718
                ('branch', expected_format)),
 
719
                         response)
734
720
        self.assertLength(1, opened_branches)
735
721
 
736
722
    def test_notification_on_branch_from_repository(self):
737
723
        """When there is a repository, the error should return details."""
738
724
        backing = self.get_transport()
739
725
        request = smart_dir.SmartServerRequestOpenBranchV2(backing)
740
 
        self.make_repository('.')
741
 
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
742
 
                         request.execute(b''))
 
726
        repo = self.make_repository('.')
 
727
        self.assertEqual(smart_req.SmartServerResponse(('nobranch',)),
 
728
            request.execute(''))
743
729
 
744
730
 
745
731
class TestSmartServerRequestOpenBranchV3(TestCaseWithChrootedTransport):
747
733
    def test_no_branch(self):
748
734
        """When there is no branch, ('nobranch', ) is returned."""
749
735
        backing = self.get_transport()
750
 
        self.make_controldir('.')
 
736
        self.make_bzrdir('.')
751
737
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
752
 
        self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
753
 
                         request.execute(b''))
 
738
        self.assertEqual(smart_req.SmartServerResponse(('nobranch',)),
 
739
            request.execute(''))
754
740
 
755
741
    def test_branch(self):
756
742
        """When there is a branch, 'ok' is returned."""
758
744
        expected = self.make_branch('.')._format.network_name()
759
745
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
760
746
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
761
 
            (b'branch', expected)),
762
 
            request.execute(b''))
 
747
                ('branch', expected)),
 
748
                         request.execute(''))
763
749
 
764
750
    def test_branch_reference(self):
765
751
        """When there is a branch reference, the reference URL is returned."""
767
753
        backing = self.get_transport()
768
754
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
769
755
        branch = self.make_branch('branch')
770
 
        checkout = branch.create_checkout('reference', lightweight=True)
771
 
        reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
772
 
            checkout.controldir).encode('utf-8')
 
756
        checkout = branch.create_checkout('reference',lightweight=True)
 
757
        reference_url = _mod_branch.BranchReferenceFormat().get_reference(
 
758
            checkout.bzrdir)
773
759
        self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
774
760
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
775
 
            (b'ref', reference_url)),
776
 
            request.execute(b'reference'))
 
761
                ('ref', reference_url)),
 
762
                         request.execute('reference'))
777
763
 
778
764
    def test_stacked_branch(self):
779
765
        """Opening a stacked branch does not open the stacked-on branch."""
787
773
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
788
774
        request.setup_jail()
789
775
        try:
790
 
            response = request.execute(b'feature')
 
776
            response = request.execute('feature')
791
777
        finally:
792
778
            request.teardown_jail()
793
779
        expected_format = feature._format.network_name()
794
780
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
795
 
            (b'branch', expected_format)),
796
 
            response)
 
781
                ('branch', expected_format)),
 
782
                         response)
797
783
        self.assertLength(1, opened_branches)
798
784
 
799
785
    def test_notification_on_branch_from_repository(self):
800
786
        """When there is a repository, the error should return details."""
801
787
        backing = self.get_transport()
802
788
        request = smart_dir.SmartServerRequestOpenBranchV3(backing)
803
 
        self.make_repository('.')
 
789
        repo = self.make_repository('.')
804
790
        self.assertEqual(smart_req.SmartServerResponse(
805
 
            (b'nobranch', b'location is a repository')),
806
 
            request.execute(b''))
 
791
                ('nobranch', 'location is a repository')),
 
792
                         request.execute(''))
807
793
 
808
794
 
809
795
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
813
799
        backing = self.get_transport()
814
800
        request = smart_branch.SmartServerRequestRevisionHistory(backing)
815
801
        self.make_branch('.')
816
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b''),
817
 
                         request.execute(b''))
 
802
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), ''),
 
803
            request.execute(''))
818
804
 
819
805
    def test_not_empty(self):
820
806
        """For a non-empty branch, the body is empty."""
827
813
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
828
814
        tree.unlock()
829
815
        self.assertEqual(
830
 
            smart_req.SmartServerResponse((b'ok', ), (b'\x00'.join([r1, r2]))),
831
 
            request.execute(b''))
 
816
            smart_req.SmartServerResponse(('ok', ), ('\x00'.join([r1, r2]))),
 
817
            request.execute(''))
832
818
 
833
819
 
834
820
class TestSmartServerBranchRequest(tests.TestCaseWithMemoryTransport):
837
823
        """When there is a bzrdir and no branch, NotBranchError is raised."""
838
824
        backing = self.get_transport()
839
825
        request = smart_branch.SmartServerBranchRequest(backing)
840
 
        self.make_controldir('.')
 
826
        self.make_bzrdir('.')
841
827
        self.assertRaises(errors.NotBranchError,
842
 
                          request.execute, b'')
 
828
            request.execute, '')
843
829
 
844
830
    def test_branch_reference(self):
845
831
        """When there is a branch reference, NotBranchError is raised."""
846
832
        backing = self.get_transport()
847
833
        request = smart_branch.SmartServerBranchRequest(backing)
848
834
        branch = self.make_branch('branch')
849
 
        branch.create_checkout('reference', lightweight=True)
 
835
        checkout = branch.create_checkout('reference',lightweight=True)
850
836
        self.assertRaises(errors.NotBranchError,
851
 
                          request.execute, b'checkout')
 
837
            request.execute, 'checkout')
852
838
 
853
839
 
854
840
class TestSmartServerBranchRequestLastRevisionInfo(
855
 
        tests.TestCaseWithMemoryTransport):
 
841
    tests.TestCaseWithMemoryTransport):
856
842
 
857
843
    def test_empty(self):
858
 
        """For an empty branch, the result is ('ok', '0', b'null:')."""
 
844
        """For an empty branch, the result is ('ok', '0', 'null:')."""
859
845
        backing = self.get_transport()
860
 
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(
861
 
            backing)
 
846
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
862
847
        self.make_branch('.')
863
 
        self.assertEqual(
864
 
            smart_req.SmartServerResponse((b'ok', b'0', b'null:')),
865
 
            request.execute(b''))
866
 
 
867
 
    def test_ghost(self):
868
 
        """For an empty branch, the result is ('ok', '0', b'null:')."""
869
 
        backing = self.get_transport()
870
 
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(
871
 
            backing)
872
 
        branch = self.make_branch('.')
873
 
 
874
 
        def last_revision_info():
875
 
            raise errors.GhostRevisionsHaveNoRevno(b'revid1', b'revid2')
876
 
        self.overrideAttr(branch, 'last_revision_info', last_revision_info)
877
 
        self.assertRaises(errors.GhostRevisionsHaveNoRevno,
878
 
                          request.do_with_branch, branch)
 
848
        self.assertEqual(smart_req.SmartServerResponse(('ok', '0', 'null:')),
 
849
            request.execute(''))
879
850
 
880
851
    def test_not_empty(self):
881
852
        """For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
882
853
        backing = self.get_transport()
883
 
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(
884
 
            backing)
 
854
        request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
885
855
        tree = self.make_branch_and_memory_tree('.')
886
856
        tree.lock_write()
887
857
        tree.add('')
888
858
        rev_id_utf8 = u'\xc8'.encode('utf-8')
889
 
        tree.commit('1st commit')
890
 
        tree.commit('2nd commit', rev_id=rev_id_utf8)
 
859
        r1 = tree.commit('1st commit')
 
860
        r2 = tree.commit('2nd commit', rev_id=rev_id_utf8)
891
861
        tree.unlock()
892
862
        self.assertEqual(
893
 
            smart_req.SmartServerResponse((b'ok', b'2', rev_id_utf8)),
894
 
            request.execute(b''))
 
863
            smart_req.SmartServerResponse(('ok', '2', rev_id_utf8)),
 
864
            request.execute(''))
895
865
 
896
866
 
897
867
class TestSmartServerBranchRequestRevisionIdToRevno(
898
 
        tests.TestCaseWithMemoryTransport):
 
868
    tests.TestCaseWithMemoryTransport):
899
869
 
900
870
    def test_null(self):
901
871
        backing = self.get_transport()
902
872
        request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
903
873
            backing)
904
874
        self.make_branch('.')
905
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', b'0')),
906
 
                         request.execute(b'', b'null:'))
907
 
 
908
 
    def test_ghost_revision(self):
909
 
        backing = self.get_transport()
910
 
        request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
911
 
            backing)
912
 
        branch = self.make_branch('.')
913
 
        def revision_id_to_dotted_revno(revid):
914
 
            raise errors.GhostRevisionsHaveNoRevno(revid, b'ghost-revid')
915
 
        self.overrideAttr(branch, 'revision_id_to_dotted_revno', revision_id_to_dotted_revno)
916
 
        self.assertEqual(
917
 
            smart_req.FailedSmartServerResponse(
918
 
                (b'GhostRevisionsHaveNoRevno', b'revid', b'ghost-revid')),
919
 
            request.do_with_branch(branch, b'revid'))
 
875
        self.assertEqual(smart_req.SmartServerResponse(('ok', '0')),
 
876
            request.execute('', 'null:'))
920
877
 
921
878
    def test_simple(self):
922
879
        backing = self.get_transport()
928
885
        r1 = tree.commit('1st commit')
929
886
        tree.unlock()
930
887
        self.assertEqual(
931
 
            smart_req.SmartServerResponse((b'ok', b'1')),
932
 
            request.execute(b'', r1))
 
888
            smart_req.SmartServerResponse(('ok', '1')),
 
889
            request.execute('', r1))
933
890
 
934
891
    def test_not_found(self):
935
892
        backing = self.get_transport()
936
893
        request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
937
894
            backing)
938
 
        self.make_branch('.')
 
895
        branch = self.make_branch('.')
939
896
        self.assertEqual(
940
897
            smart_req.FailedSmartServerResponse(
941
 
                (b'NoSuchRevision', b'idontexist')),
942
 
            request.execute(b'', b'idontexist'))
 
898
                ('NoSuchRevision', 'idontexist')),
 
899
            request.execute('', 'idontexist'))
943
900
 
944
901
 
945
902
class TestSmartServerBranchRequestGetConfigFile(
946
 
        tests.TestCaseWithMemoryTransport):
 
903
    tests.TestCaseWithMemoryTransport):
947
904
 
948
905
    def test_default(self):
949
906
        """With no file, we get empty content."""
950
907
        backing = self.get_transport()
951
908
        request = smart_branch.SmartServerBranchGetConfigFile(backing)
952
 
        self.make_branch('.')
 
909
        branch = self.make_branch('.')
953
910
        # there should be no file by default
954
 
        content = b''
955
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), content),
956
 
                         request.execute(b''))
 
911
        content = ''
 
912
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), content),
 
913
            request.execute(''))
957
914
 
958
915
    def test_with_content(self):
959
916
        # SmartServerBranchGetConfigFile should return the content from
960
 
        # branch.control_files.get('branch.conf') for now - in the future it
961
 
        # may perform more complex processing.
 
917
        # branch.control_files.get('branch.conf') for now - in the future it may
 
918
        # perform more complex processing.
962
919
        backing = self.get_transport()
963
920
        request = smart_branch.SmartServerBranchGetConfigFile(backing)
964
921
        branch = self.make_branch('.')
965
 
        branch._transport.put_bytes('branch.conf', b'foo bar baz')
966
 
        self.assertEqual(
967
 
            smart_req.SmartServerResponse((b'ok', ), b'foo bar baz'),
968
 
            request.execute(b''))
 
922
        branch._transport.put_bytes('branch.conf', 'foo bar baz')
 
923
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), 'foo bar baz'),
 
924
            request.execute(''))
969
925
 
970
926
 
971
927
class TestLockedBranch(tests.TestCaseWithMemoryTransport):
972
928
 
973
929
    def get_lock_tokens(self, branch):
974
 
        branch_token = branch.lock_write().token
 
930
        branch_token = branch.lock_write().branch_token
975
931
        repo_token = branch.repository.lock_write().repository_token
976
932
        branch.repository.unlock()
977
933
        return branch_token, repo_token
984
940
        request = smart_branch.SmartServerBranchPutConfigFile(backing)
985
941
        branch = self.make_branch('.')
986
942
        branch_token, repo_token = self.get_lock_tokens(branch)
987
 
        self.assertIs(None, request.execute(b'', branch_token, repo_token))
 
943
        self.assertIs(None, request.execute('', branch_token, repo_token))
988
944
        self.assertEqual(
989
 
            smart_req.SmartServerResponse((b'ok', )),
990
 
            request.do_body(b'foo bar baz'))
 
945
            smart_req.SmartServerResponse(('ok', )),
 
946
            request.do_body('foo bar baz'))
991
947
        self.assertEqual(
992
948
            branch.control_transport.get_bytes('branch.conf'),
993
 
            b'foo bar baz')
 
949
            'foo bar baz')
994
950
        branch.unlock()
995
951
 
996
952
 
999
955
    def test_value_name(self):
1000
956
        branch = self.make_branch('.')
1001
957
        request = smart_branch.SmartServerBranchRequestSetConfigOption(
1002
 
            branch.controldir.root_transport)
 
958
            branch.bzrdir.root_transport)
1003
959
        branch_token, repo_token = self.get_lock_tokens(branch)
1004
960
        config = branch._get_config()
1005
 
        result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
1006
 
                                 b'')
 
961
        result = request.execute('', branch_token, repo_token, 'bar', 'foo',
 
962
            '')
1007
963
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1008
964
        self.assertEqual('bar', config.get_option('foo'))
1009
965
        # Cleanup
1012
968
    def test_value_name_section(self):
1013
969
        branch = self.make_branch('.')
1014
970
        request = smart_branch.SmartServerBranchRequestSetConfigOption(
1015
 
            branch.controldir.root_transport)
 
971
            branch.bzrdir.root_transport)
1016
972
        branch_token, repo_token = self.get_lock_tokens(branch)
1017
973
        config = branch._get_config()
1018
 
        result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
1019
 
                                 b'gam')
 
974
        result = request.execute('', branch_token, repo_token, 'bar', 'foo',
 
975
            'gam')
1020
976
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1021
977
        self.assertEqual('bar', config.get_option('foo', 'gam'))
1022
978
        # Cleanup
1030
986
        # A dict with non-ascii keys and values to exercise unicode
1031
987
        # roundtripping.
1032
988
        self.encoded_value_dict = (
1033
 
            b'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde')
 
989
            'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde')
1034
990
        self.value_dict = {
1035
991
            'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
1036
992
 
1037
993
    def test_value_name(self):
1038
994
        branch = self.make_branch('.')
1039
995
        request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
1040
 
            branch.controldir.root_transport)
 
996
            branch.bzrdir.root_transport)
1041
997
        branch_token, repo_token = self.get_lock_tokens(branch)
1042
998
        config = branch._get_config()
1043
 
        result = request.execute(b'', branch_token, repo_token,
1044
 
                                 self.encoded_value_dict, b'foo', b'')
 
999
        result = request.execute('', branch_token, repo_token,
 
1000
            self.encoded_value_dict, 'foo', '')
1045
1001
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1046
1002
        self.assertEqual(self.value_dict, config.get_option('foo'))
1047
1003
        # Cleanup
1050
1006
    def test_value_name_section(self):
1051
1007
        branch = self.make_branch('.')
1052
1008
        request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
1053
 
            branch.controldir.root_transport)
 
1009
            branch.bzrdir.root_transport)
1054
1010
        branch_token, repo_token = self.get_lock_tokens(branch)
1055
1011
        config = branch._get_config()
1056
 
        result = request.execute(b'', branch_token, repo_token,
1057
 
                                 self.encoded_value_dict, b'foo', b'gam')
 
1012
        result = request.execute('', branch_token, repo_token,
 
1013
            self.encoded_value_dict, 'foo', 'gam')
1058
1014
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1059
1015
        self.assertEqual(self.value_dict, config.get_option('foo', 'gam'))
1060
1016
        # Cleanup
1072
1028
        branch_token, repo_token = self.get_lock_tokens(base_branch)
1073
1029
        request = smart_branch.SmartServerBranchSetTagsBytes(
1074
1030
            self.get_transport())
1075
 
        response = request.execute(b'base', branch_token, repo_token)
 
1031
        response = request.execute('base', branch_token, repo_token)
1076
1032
        self.assertEqual(None, response)
1077
1033
        response = request.do_chunk(tag_bytes)
1078
1034
        self.assertEqual(None, response)
1088
1044
        request = smart_branch.SmartServerBranchSetTagsBytes(
1089
1045
            self.get_transport())
1090
1046
        self.assertRaises(errors.TokenMismatch, request.execute,
1091
 
                          b'base', b'wrong token', b'wrong token')
 
1047
            'base', 'wrong token', 'wrong token')
1092
1048
        # The request handler will keep processing the message parts, so even
1093
1049
        # if the request fails immediately do_chunk and do_end are still
1094
1050
        # called.
1097
1053
        base_branch.unlock()
1098
1054
 
1099
1055
 
 
1056
 
1100
1057
class SetLastRevisionTestBase(TestLockedBranch):
1101
1058
    """Base test case for verbs that implement set_last_revision."""
1102
1059
 
1121
1078
 
1122
1079
    def assertRequestSucceeds(self, revision_id, revno):
1123
1080
        response = self.set_last_revision(revision_id, revno)
1124
 
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
 
1081
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
1125
1082
                         response)
1126
1083
 
1127
1084
 
1129
1086
    """Mixin test case for verbs that implement set_last_revision."""
1130
1087
 
1131
1088
    def test_set_null_to_null(self):
1132
 
        """An empty branch can have its last revision set to b'null:'."""
1133
 
        self.assertRequestSucceeds(b'null:', 0)
 
1089
        """An empty branch can have its last revision set to 'null:'."""
 
1090
        self.assertRequestSucceeds('null:', 0)
1134
1091
 
1135
1092
    def test_NoSuchRevision(self):
1136
1093
        """If the revision_id is not present, the verb returns NoSuchRevision.
1137
1094
        """
1138
 
        revision_id = b'non-existent revision'
1139
 
        self.assertEqual(
1140
 
            smart_req.FailedSmartServerResponse(
1141
 
                (b'NoSuchRevision', revision_id)),
1142
 
            self.set_last_revision(revision_id, 1))
 
1095
        revision_id = 'non-existent revision'
 
1096
        self.assertEqual(smart_req.FailedSmartServerResponse(('NoSuchRevision',
 
1097
                                                              revision_id)),
 
1098
                         self.set_last_revision(revision_id, 1))
1143
1099
 
1144
1100
    def make_tree_with_two_commits(self):
1145
1101
        self.tree.lock_write()
1146
1102
        self.tree.add('')
1147
1103
        rev_id_utf8 = u'\xc8'.encode('utf-8')
1148
 
        self.tree.commit('1st commit', rev_id=rev_id_utf8)
1149
 
        self.tree.commit('2nd commit', rev_id=b'rev-2')
 
1104
        r1 = self.tree.commit('1st commit', rev_id=rev_id_utf8)
 
1105
        r2 = self.tree.commit('2nd commit', rev_id='rev-2')
1150
1106
        self.tree.unlock()
1151
1107
 
1152
1108
    def test_branch_last_revision_info_is_updated(self):
1157
1113
        # its repository.
1158
1114
        self.make_tree_with_two_commits()
1159
1115
        rev_id_utf8 = u'\xc8'.encode('utf-8')
1160
 
        self.tree.branch.set_last_revision_info(0, b'null:')
 
1116
        self.tree.branch.set_last_revision_info(0, 'null:')
1161
1117
        self.assertEqual(
1162
 
            (0, b'null:'), self.tree.branch.last_revision_info())
 
1118
            (0, 'null:'), self.tree.branch.last_revision_info())
1163
1119
        # We can update the branch to a revision that is present in the
1164
1120
        # repository.
1165
1121
        self.assertRequestSucceeds(rev_id_utf8, 1)
1173
1129
        self.make_tree_with_two_commits()
1174
1130
        rev_id_utf8 = u'\xc8'.encode('utf-8')
1175
1131
        self.assertEqual(
1176
 
            (2, b'rev-2'), self.tree.branch.last_revision_info())
 
1132
            (2, 'rev-2'), self.tree.branch.last_revision_info())
1177
1133
        self.assertRequestSucceeds(rev_id_utf8, 1)
1178
1134
        self.assertEqual(
1179
1135
            (1, rev_id_utf8), self.tree.branch.last_revision_info())
1183
1139
        returns TipChangeRejected.
1184
1140
        """
1185
1141
        rejection_message = u'rejection message\N{INTERROBANG}'
1186
 
 
1187
1142
        def hook_that_rejects(params):
1188
1143
            raise errors.TipChangeRejected(rejection_message)
1189
1144
        _mod_branch.Branch.hooks.install_named_hook(
1190
1145
            'pre_change_branch_tip', hook_that_rejects, None)
1191
1146
        self.assertEqual(
1192
1147
            smart_req.FailedSmartServerResponse(
1193
 
                (b'TipChangeRejected', rejection_message.encode('utf-8'))),
1194
 
            self.set_last_revision(b'null:', 0))
 
1148
                ('TipChangeRejected', rejection_message.encode('utf-8'))),
 
1149
            self.set_last_revision('null:', 0))
1195
1150
 
1196
1151
 
1197
1152
class TestSmartServerBranchRequestSetLastRevision(
1202
1157
 
1203
1158
    def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1204
1159
        return self.request.execute(
1205
 
            b'', branch_token, repo_token, revision_id)
 
1160
            '', branch_token, repo_token, revision_id)
1206
1161
 
1207
1162
 
1208
1163
class TestSmartServerBranchRequestSetLastRevisionInfo(
1213
1168
 
1214
1169
    def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1215
1170
        return self.request.execute(
1216
 
            b'', branch_token, repo_token, revno, revision_id)
 
1171
            '', branch_token, repo_token, revno, revision_id)
1217
1172
 
1218
1173
    def test_NoSuchRevision(self):
1219
1174
        """Branch.set_last_revision_info does not have to return
1230
1185
 
1231
1186
    def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1232
1187
        return self.request.execute(
1233
 
            b'', branch_token, repo_token, revision_id, 0, 0)
 
1188
            '', branch_token, repo_token, revision_id, 0, 0)
1234
1189
 
1235
1190
    def assertRequestSucceeds(self, revision_id, revno):
1236
1191
        response = self.set_last_revision(revision_id, revno)
1237
1192
        self.assertEqual(
1238
 
            smart_req.SuccessfulSmartServerResponse(
1239
 
                (b'ok', revno, revision_id)),
 
1193
            smart_req.SuccessfulSmartServerResponse(('ok', revno, revision_id)),
1240
1194
            response)
1241
1195
 
1242
1196
    def test_branch_last_revision_info_rewind(self):
1246
1200
        self.make_tree_with_two_commits()
1247
1201
        rev_id_utf8 = u'\xc8'.encode('utf-8')
1248
1202
        self.assertEqual(
1249
 
            (2, b'rev-2'), self.tree.branch.last_revision_info())
 
1203
            (2, 'rev-2'), self.tree.branch.last_revision_info())
1250
1204
        # If allow_overwrite_descendant flag is 0, then trying to set the tip
1251
1205
        # to an older revision ID has no effect.
1252
1206
        branch_token, repo_token = self.lock_branch()
1253
1207
        response = self.request.execute(
1254
 
            b'', branch_token, repo_token, rev_id_utf8, 0, 0)
 
1208
            '', branch_token, repo_token, rev_id_utf8, 0, 0)
1255
1209
        self.assertEqual(
1256
 
            smart_req.SuccessfulSmartServerResponse((b'ok', 2, b'rev-2')),
 
1210
            smart_req.SuccessfulSmartServerResponse(('ok', 2, 'rev-2')),
1257
1211
            response)
1258
1212
        self.assertEqual(
1259
 
            (2, b'rev-2'), self.tree.branch.last_revision_info())
 
1213
            (2, 'rev-2'), self.tree.branch.last_revision_info())
1260
1214
 
1261
1215
        # If allow_overwrite_descendant flag is 1, then setting the tip to an
1262
1216
        # ancestor works.
1263
1217
        response = self.request.execute(
1264
 
            b'', branch_token, repo_token, rev_id_utf8, 0, 1)
 
1218
            '', branch_token, repo_token, rev_id_utf8, 0, 1)
1265
1219
        self.assertEqual(
1266
 
            smart_req.SuccessfulSmartServerResponse((b'ok', 1, rev_id_utf8)),
 
1220
            smart_req.SuccessfulSmartServerResponse(('ok', 1, rev_id_utf8)),
1267
1221
            response)
1268
1222
        self.unlock_branch()
1269
1223
        self.assertEqual(
1277
1231
        """
1278
1232
        self.tree.lock_write()
1279
1233
        self.tree.add('')
1280
 
        self.tree.commit('1st commit')
 
1234
        r1 = self.tree.commit('1st commit')
1281
1235
        revno_1, revid_1 = self.tree.branch.last_revision_info()
1282
 
        self.tree.commit('2nd commit', rev_id=b'child-1')
 
1236
        r2 = self.tree.commit('2nd commit', rev_id='child-1')
1283
1237
        # Undo the second commit
1284
1238
        self.tree.branch.set_last_revision_info(revno_1, revid_1)
1285
1239
        self.tree.set_parent_ids([revid_1])
1286
1240
        # Make a new second commit, child-2.  child-2 has diverged from
1287
1241
        # child-1.
1288
 
        self.tree.commit('2nd commit', rev_id=b'child-2')
 
1242
        new_r2 = self.tree.commit('2nd commit', rev_id='child-2')
1289
1243
        self.tree.unlock()
1290
1244
 
1291
1245
    def test_not_allow_diverged(self):
1294
1248
        """
1295
1249
        self.make_branch_with_divergent_history()
1296
1250
        self.assertEqual(
1297
 
            smart_req.FailedSmartServerResponse((b'Diverged',)),
1298
 
            self.set_last_revision(b'child-1', 2))
 
1251
            smart_req.FailedSmartServerResponse(('Diverged',)),
 
1252
            self.set_last_revision('child-1', 2))
1299
1253
        # The branch tip was not changed.
1300
 
        self.assertEqual(b'child-2', self.tree.branch.last_revision())
 
1254
        self.assertEqual('child-2', self.tree.branch.last_revision())
1301
1255
 
1302
1256
    def test_allow_diverged(self):
1303
1257
        """If allow_diverged is passed, then setting a divergent history
1306
1260
        self.make_branch_with_divergent_history()
1307
1261
        branch_token, repo_token = self.lock_branch()
1308
1262
        response = self.request.execute(
1309
 
            b'', branch_token, repo_token, b'child-1', 1, 0)
 
1263
            '', branch_token, repo_token, 'child-1', 1, 0)
1310
1264
        self.assertEqual(
1311
 
            smart_req.SuccessfulSmartServerResponse((b'ok', 2, b'child-1')),
 
1265
            smart_req.SuccessfulSmartServerResponse(('ok', 2, 'child-1')),
1312
1266
            response)
1313
1267
        self.unlock_branch()
1314
1268
        # The branch tip was changed.
1315
 
        self.assertEqual(b'child-1', self.tree.branch.last_revision())
 
1269
        self.assertEqual('child-1', self.tree.branch.last_revision())
1316
1270
 
1317
1271
 
1318
1272
class TestSmartServerBranchBreakLock(tests.TestCaseWithMemoryTransport):
1323
1277
            self.get_transport())
1324
1278
        base_branch.lock_write()
1325
1279
        self.assertEqual(
1326
 
            smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1327
 
            request.execute(b'base'))
 
1280
            smart_req.SuccessfulSmartServerResponse(('ok', ), None),
 
1281
            request.execute('base'))
1328
1282
 
1329
1283
    def test_nothing_to_break(self):
1330
 
        self.make_branch('base')
 
1284
        base_branch = self.make_branch('base')
1331
1285
        request = smart_branch.SmartServerBranchBreakLock(
1332
1286
            self.get_transport())
1333
1287
        self.assertEqual(
1334
 
            smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1335
 
            request.execute(b'base'))
 
1288
            smart_req.SuccessfulSmartServerResponse(('ok', ), None),
 
1289
            request.execute('base'))
1336
1290
 
1337
1291
 
1338
1292
class TestSmartServerBranchRequestGetParent(tests.TestCaseWithMemoryTransport):
1339
1293
 
1340
1294
    def test_get_parent_none(self):
1341
 
        self.make_branch('base')
 
1295
        base_branch = self.make_branch('base')
1342
1296
        request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1343
 
        response = request.execute(b'base')
 
1297
        response = request.execute('base')
1344
1298
        self.assertEqual(
1345
 
            smart_req.SuccessfulSmartServerResponse((b'',)), response)
 
1299
            smart_req.SuccessfulSmartServerResponse(('',)), response)
1346
1300
 
1347
1301
    def test_get_parent_something(self):
1348
1302
        base_branch = self.make_branch('base')
1349
1303
        base_branch.set_parent(self.get_url('foo'))
1350
1304
        request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1351
 
        response = request.execute(b'base')
 
1305
        response = request.execute('base')
1352
1306
        self.assertEqual(
1353
 
            smart_req.SuccessfulSmartServerResponse((b"../foo",)),
 
1307
            smart_req.SuccessfulSmartServerResponse(("../foo",)),
1354
1308
            response)
1355
1309
 
1356
1310
 
1365
1319
            self.get_transport())
1366
1320
        branch_token, repo_token = self.get_lock_tokens(branch)
1367
1321
        try:
1368
 
            response = request.execute(b'base', branch_token, repo_token, b'')
 
1322
            response = request.execute('base', branch_token, repo_token, '')
1369
1323
        finally:
1370
1324
            branch.unlock()
1371
1325
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1372
1326
        # Refresh branch as SetParentLocation modified it
1373
 
        branch = branch.controldir.open_branch()
 
1327
        branch = branch.bzrdir.open_branch()
1374
1328
        self.assertEqual(None, branch.get_parent())
1375
1329
 
1376
1330
    def test_set_parent_something(self):
1379
1333
            self.get_transport())
1380
1334
        branch_token, repo_token = self.get_lock_tokens(branch)
1381
1335
        try:
1382
 
            response = request.execute(b'base', branch_token, repo_token,
1383
 
                                       b'http://bar/')
 
1336
            response = request.execute('base', branch_token, repo_token,
 
1337
                                       'http://bar/')
1384
1338
        finally:
1385
1339
            branch.unlock()
1386
1340
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1389
1343
 
1390
1344
 
1391
1345
class TestSmartServerBranchRequestGetTagsBytes(
1392
 
        tests.TestCaseWithMemoryTransport):
 
1346
    tests.TestCaseWithMemoryTransport):
1393
1347
    # Only called when the branch format and tags match [yay factory
1394
1348
    # methods] so only need to test straight forward cases.
1395
1349
 
1396
1350
    def test_get_bytes(self):
1397
 
        self.make_branch('base')
 
1351
        base_branch = self.make_branch('base')
1398
1352
        request = smart_branch.SmartServerBranchGetTagsBytes(
1399
1353
            self.get_transport())
1400
 
        response = request.execute(b'base')
 
1354
        response = request.execute('base')
1401
1355
        self.assertEqual(
1402
 
            smart_req.SuccessfulSmartServerResponse((b'',)), response)
1403
 
 
1404
 
 
1405
 
class TestSmartServerBranchRequestGetStackedOnURL(
1406
 
        tests.TestCaseWithMemoryTransport):
 
1356
            smart_req.SuccessfulSmartServerResponse(('',)), response)
 
1357
 
 
1358
 
 
1359
class TestSmartServerBranchRequestGetStackedOnURL(tests.TestCaseWithMemoryTransport):
1407
1360
 
1408
1361
    def test_get_stacked_on_url(self):
1409
 
        self.make_branch('base', format='1.6')
 
1362
        base_branch = self.make_branch('base', format='1.6')
1410
1363
        stacked_branch = self.make_branch('stacked', format='1.6')
1411
1364
        # typically should be relative
1412
1365
        stacked_branch.set_stacked_on_url('../base')
1413
1366
        request = smart_branch.SmartServerBranchRequestGetStackedOnURL(
1414
1367
            self.get_transport())
1415
 
        response = request.execute(b'stacked')
 
1368
        response = request.execute('stacked')
1416
1369
        self.assertEqual(
1417
 
            smart_req.SmartServerResponse((b'ok', b'../base')),
 
1370
            smart_req.SmartServerResponse(('ok', '../base')),
1418
1371
            response)
1419
1372
 
1420
1373
 
1425
1378
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1426
1379
        branch = self.make_branch('.', format='knit')
1427
1380
        repository = branch.repository
1428
 
        response = request.execute(b'')
 
1381
        response = request.execute('')
1429
1382
        branch_nonce = branch.control_files._lock.peek().get('nonce')
1430
1383
        repository_nonce = repository.control_files._lock.peek().get('nonce')
1431
1384
        self.assertEqual(smart_req.SmartServerResponse(
1432
 
            (b'ok', branch_nonce, repository_nonce)),
1433
 
            response)
 
1385
                ('ok', branch_nonce, repository_nonce)),
 
1386
                         response)
1434
1387
        # The branch (and associated repository) is now locked.  Verify that
1435
1388
        # with a new branch object.
1436
 
        new_branch = repository.controldir.open_branch()
 
1389
        new_branch = repository.bzrdir.open_branch()
1437
1390
        self.assertRaises(errors.LockContention, new_branch.lock_write)
1438
1391
        # Cleanup
1439
1392
        request = smart_branch.SmartServerBranchRequestUnlock(backing)
1440
 
        response = request.execute(b'', branch_nonce, repository_nonce)
 
1393
        response = request.execute('', branch_nonce, repository_nonce)
1441
1394
 
1442
1395
    def test_lock_write_on_locked_branch(self):
1443
1396
        backing = self.get_transport()
1444
1397
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1445
1398
        branch = self.make_branch('.')
1446
 
        branch_token = branch.lock_write().token
 
1399
        branch_token = branch.lock_write().branch_token
1447
1400
        branch.leave_lock_in_place()
1448
1401
        branch.unlock()
1449
 
        response = request.execute(b'')
 
1402
        response = request.execute('')
1450
1403
        self.assertEqual(
1451
 
            smart_req.SmartServerResponse((b'LockContention',)), response)
 
1404
            smart_req.SmartServerResponse(('LockContention',)), response)
1452
1405
        # Cleanup
1453
1406
        branch.lock_write(branch_token)
1454
1407
        branch.dont_leave_lock_in_place()
1462
1415
        branch.leave_lock_in_place()
1463
1416
        branch.repository.leave_lock_in_place()
1464
1417
        branch.unlock()
1465
 
        response = request.execute(b'',
 
1418
        response = request.execute('',
1466
1419
                                   branch_token, repo_token)
1467
1420
        self.assertEqual(
1468
 
            smart_req.SmartServerResponse((b'ok', branch_token, repo_token)),
 
1421
            smart_req.SmartServerResponse(('ok', branch_token, repo_token)),
1469
1422
            response)
1470
1423
        # Cleanup
1471
1424
        branch.repository.lock_write(repo_token)
1483
1436
        branch.leave_lock_in_place()
1484
1437
        branch.repository.leave_lock_in_place()
1485
1438
        branch.unlock()
1486
 
        response = request.execute(b'',
1487
 
                                   branch_token + b'xxx', repo_token)
 
1439
        response = request.execute('',
 
1440
                                   branch_token+'xxx', repo_token)
1488
1441
        self.assertEqual(
1489
 
            smart_req.SmartServerResponse((b'TokenMismatch',)), response)
 
1442
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
1490
1443
        # Cleanup
1491
1444
        branch.repository.lock_write(repo_token)
1492
1445
        branch.repository.dont_leave_lock_in_place()
1503
1456
        repo_token = repo.lock_write().repository_token
1504
1457
        repo.leave_lock_in_place()
1505
1458
        repo.unlock()
1506
 
        response = request.execute(b'')
 
1459
        response = request.execute('')
1507
1460
        self.assertEqual(
1508
 
            smart_req.SmartServerResponse((b'LockContention',)), response)
 
1461
            smart_req.SmartServerResponse(('LockContention',)), response)
1509
1462
        # Cleanup
1510
1463
        repo.lock_write(repo_token)
1511
1464
        repo.dont_leave_lock_in_place()
1514
1467
    def test_lock_write_on_readonly_transport(self):
1515
1468
        backing = self.get_readonly_transport()
1516
1469
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1517
 
        self.make_branch('.')
 
1470
        branch = self.make_branch('.')
1518
1471
        root = self.get_transport().clone('/')
1519
1472
        path = urlutils.relative_url(root.base, self.get_transport().base)
1520
 
        response = request.execute(path.encode('utf-8'))
 
1473
        response = request.execute(path)
1521
1474
        error_name, lock_str, why_str = response.args
1522
1475
        self.assertFalse(response.is_successful())
1523
 
        self.assertEqual(b'LockFailed', error_name)
 
1476
        self.assertEqual('LockFailed', error_name)
1524
1477
 
1525
1478
 
1526
1479
class TestSmartServerBranchRequestGetPhysicalLockStatus(TestLockedBranch):
1532
1485
        branch = self.make_branch('.')
1533
1486
        branch_token, repo_token = self.get_lock_tokens(branch)
1534
1487
        self.assertEqual(True, branch.get_physical_lock_status())
1535
 
        response = request.execute(b'')
 
1488
        response = request.execute('')
1536
1489
        self.assertEqual(
1537
 
            smart_req.SmartServerResponse((b'yes',)), response)
 
1490
            smart_req.SmartServerResponse(('yes',)), response)
1538
1491
        branch.unlock()
1539
1492
 
1540
1493
    def test_false(self):
1543
1496
            backing)
1544
1497
        branch = self.make_branch('.')
1545
1498
        self.assertEqual(False, branch.get_physical_lock_status())
1546
 
        response = request.execute(b'')
 
1499
        response = request.execute('')
1547
1500
        self.assertEqual(
1548
 
            smart_req.SmartServerResponse((b'no',)), response)
 
1501
            smart_req.SmartServerResponse(('no',)), response)
1549
1502
 
1550
1503
 
1551
1504
class TestSmartServerBranchRequestUnlock(TestLockedBranch):
1561
1514
        branch.leave_lock_in_place()
1562
1515
        branch.repository.leave_lock_in_place()
1563
1516
        branch.unlock()
1564
 
        response = request.execute(b'',
 
1517
        response = request.execute('',
1565
1518
                                   branch_token, repo_token)
1566
1519
        self.assertEqual(
1567
 
            smart_req.SmartServerResponse((b'ok',)), response)
 
1520
            smart_req.SmartServerResponse(('ok',)), response)
1568
1521
        # The branch is now unlocked.  Verify that with a new branch
1569
1522
        # object.
1570
 
        new_branch = branch.controldir.open_branch()
 
1523
        new_branch = branch.bzrdir.open_branch()
1571
1524
        new_branch.lock_write()
1572
1525
        new_branch.unlock()
1573
1526
 
1574
1527
    def test_unlock_on_unlocked_branch_unlocked_repo(self):
1575
1528
        backing = self.get_transport()
1576
1529
        request = smart_branch.SmartServerBranchRequestUnlock(backing)
1577
 
        self.make_branch('.', format='knit')
 
1530
        branch = self.make_branch('.', format='knit')
1578
1531
        response = request.execute(
1579
 
            b'', b'branch token', b'repo token')
 
1532
            '', 'branch token', 'repo token')
1580
1533
        self.assertEqual(
1581
 
            smart_req.SmartServerResponse((b'TokenMismatch',)), response)
 
1534
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
1582
1535
 
1583
1536
    def test_unlock_on_unlocked_branch_locked_repo(self):
1584
1537
        backing = self.get_transport()
1590
1543
        branch.repository.unlock()
1591
1544
        # Issue branch lock_write request on the unlocked branch (with locked
1592
1545
        # repo).
1593
 
        response = request.execute(b'', b'branch token', repo_token)
 
1546
        response = request.execute('', 'branch token', repo_token)
1594
1547
        self.assertEqual(
1595
 
            smart_req.SmartServerResponse((b'TokenMismatch',)), response)
 
1548
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
1596
1549
        # Cleanup
1597
1550
        branch.repository.lock_write(repo_token)
1598
1551
        branch.repository.dont_leave_lock_in_place()
1610
1563
        backing = self.get_transport()
1611
1564
        request = smart_repo.SmartServerRepositoryRequest(backing)
1612
1565
        self.make_repository('.', shared=True)
1613
 
        self.make_controldir('subdir')
 
1566
        self.make_bzrdir('subdir')
1614
1567
        self.assertRaises(errors.NoRepositoryPresent,
1615
 
                          request.execute, b'subdir')
1616
 
 
1617
 
 
1618
 
class TestSmartServerRepositoryAddSignatureText(
1619
 
        tests.TestCaseWithMemoryTransport):
 
1568
            request.execute, 'subdir')
 
1569
 
 
1570
 
 
1571
class TestSmartServerRepositoryAddSignatureText(tests.TestCaseWithMemoryTransport):
1620
1572
 
1621
1573
    def test_add_text(self):
1622
1574
        backing = self.get_transport()
1625
1577
        write_token = tree.lock_write()
1626
1578
        self.addCleanup(tree.unlock)
1627
1579
        tree.add('')
1628
 
        tree.commit("Message", rev_id=b'rev1')
 
1580
        tree.commit("Message", rev_id='rev1')
1629
1581
        tree.branch.repository.start_write_group()
1630
1582
        write_group_tokens = tree.branch.repository.suspend_write_group()
1631
 
        self.assertEqual(
1632
 
            None, request.execute(
1633
 
                b'', write_token, b'rev1',
1634
 
                *[token.encode('utf-8') for token in write_group_tokens]))
1635
 
        response = request.do_body(b'somesignature')
 
1583
        self.assertEqual(None, request.execute('', write_token,
 
1584
            'rev1', *write_group_tokens))
 
1585
        response = request.do_body('somesignature')
1636
1586
        self.assertTrue(response.is_successful())
1637
 
        self.assertEqual(response.args[0], b'ok')
1638
 
        write_group_tokens = [token.decode('utf-8')
1639
 
                              for token in response.args[1:]]
 
1587
        self.assertEqual(response.args[0], 'ok')
 
1588
        write_group_tokens = response.args[1:]
1640
1589
        tree.branch.repository.resume_write_group(write_group_tokens)
1641
1590
        tree.branch.repository.commit_write_group()
1642
1591
        tree.unlock()
1643
 
        self.assertEqual(b"somesignature",
1644
 
                         tree.branch.repository.get_signature_text(b"rev1"))
 
1592
        self.assertEqual("somesignature",
 
1593
            tree.branch.repository.get_signature_text("rev1"))
1645
1594
 
1646
1595
 
1647
1596
class TestSmartServerRepositoryAllRevisionIds(
1648
 
        tests.TestCaseWithMemoryTransport):
 
1597
    tests.TestCaseWithMemoryTransport):
1649
1598
 
1650
1599
    def test_empty(self):
1651
1600
        """An empty body should be returned for an empty repository."""
1653
1602
        request = smart_repo.SmartServerRepositoryAllRevisionIds(backing)
1654
1603
        self.make_repository('.')
1655
1604
        self.assertEqual(
1656
 
            smart_req.SuccessfulSmartServerResponse((b"ok", ), b""),
1657
 
            request.execute(b''))
 
1605
            smart_req.SuccessfulSmartServerResponse(("ok", ), ""),
 
1606
            request.execute(''))
1658
1607
 
1659
1608
    def test_some_revisions(self):
1660
1609
        """An empty body should be returned for an empty repository."""
1663
1612
        tree = self.make_branch_and_memory_tree('.')
1664
1613
        tree.lock_write()
1665
1614
        tree.add('')
1666
 
        tree.commit(rev_id=b'origineel', message="message")
1667
 
        tree.commit(rev_id=b'nog-een-revisie', message="message")
 
1615
        tree.commit(rev_id='origineel', message="message")
 
1616
        tree.commit(rev_id='nog-een-revisie', message="message")
1668
1617
        tree.unlock()
1669
 
        self.assertIn(
1670
 
            request.execute(b''),
1671
 
            [smart_req.SuccessfulSmartServerResponse(
1672
 
                (b"ok", ), b"origineel\nnog-een-revisie"),
1673
 
             smart_req.SuccessfulSmartServerResponse(
1674
 
                 (b"ok", ), b"nog-een-revisie\norigineel")])
 
1618
        self.assertEqual(
 
1619
            smart_req.SuccessfulSmartServerResponse(("ok", ),
 
1620
                "origineel\nnog-een-revisie"),
 
1621
            request.execute(''))
1675
1622
 
1676
1623
 
1677
1624
class TestSmartServerRepositoryBreakLock(tests.TestCaseWithMemoryTransport):
1682
1629
        tree = self.make_branch_and_memory_tree('.')
1683
1630
        tree.branch.repository.lock_write()
1684
1631
        self.assertEqual(
1685
 
            smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1686
 
            request.execute(b''))
 
1632
            smart_req.SuccessfulSmartServerResponse(('ok', ), None),
 
1633
            request.execute(''))
1687
1634
 
1688
1635
    def test_nothing_to_break(self):
1689
1636
        backing = self.get_transport()
1690
1637
        request = smart_repo.SmartServerRepositoryBreakLock(backing)
1691
 
        self.make_branch_and_memory_tree('.')
 
1638
        tree = self.make_branch_and_memory_tree('.')
1692
1639
        self.assertEqual(
1693
 
            smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1694
 
            request.execute(b''))
 
1640
            smart_req.SuccessfulSmartServerResponse(('ok', ), None),
 
1641
            request.execute(''))
1695
1642
 
1696
1643
 
1697
1644
class TestSmartServerRepositoryGetParentMap(tests.TestCaseWithMemoryTransport):
1700
1647
        # This tests that the wire encoding is actually bzipped
1701
1648
        backing = self.get_transport()
1702
1649
        request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1703
 
        self.make_branch_and_memory_tree('.')
 
1650
        tree = self.make_branch_and_memory_tree('.')
1704
1651
 
1705
1652
        self.assertEqual(None,
1706
 
                         request.execute(b'', b'missing-id'))
 
1653
            request.execute('', 'missing-id'))
1707
1654
        # Note that it returns a body that is bzipped.
1708
1655
        self.assertEqual(
1709
 
            smart_req.SuccessfulSmartServerResponse(
1710
 
                (b'ok', ), bz2.compress(b'')),
1711
 
            request.do_body(b'\n\n0\n'))
 
1656
            smart_req.SuccessfulSmartServerResponse(('ok', ), bz2.compress('')),
 
1657
            request.do_body('\n\n0\n'))
1712
1658
 
1713
1659
    def test_trivial_include_missing(self):
1714
1660
        backing = self.get_transport()
1715
1661
        request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1716
 
        self.make_branch_and_memory_tree('.')
 
1662
        tree = self.make_branch_and_memory_tree('.')
1717
1663
 
1718
 
        self.assertEqual(
1719
 
            None, request.execute(b'', b'missing-id', b'include-missing:'))
1720
 
        self.assertEqual(
1721
 
            smart_req.SuccessfulSmartServerResponse(
1722
 
                (b'ok', ), bz2.compress(b'missing:missing-id')),
1723
 
            request.do_body(b'\n\n0\n'))
 
1664
        self.assertEqual(None,
 
1665
            request.execute('', 'missing-id', 'include-missing:'))
 
1666
        self.assertEqual(
 
1667
            smart_req.SuccessfulSmartServerResponse(('ok', ),
 
1668
                bz2.compress('missing:missing-id')),
 
1669
            request.do_body('\n\n0\n'))
1724
1670
 
1725
1671
 
1726
1672
class TestSmartServerRepositoryGetRevisionGraph(
1727
 
        tests.TestCaseWithMemoryTransport):
 
1673
    tests.TestCaseWithMemoryTransport):
1728
1674
 
1729
1675
    def test_none_argument(self):
1730
1676
        backing = self.get_transport()
1738
1684
 
1739
1685
        # the lines of revision_id->revision_parent_list has no guaranteed
1740
1686
        # order coming out of a dict, so sort both our test and response
1741
 
        lines = sorted([b' '.join([r2, r1]), r1])
1742
 
        response = request.execute(b'', b'')
1743
 
        response.body = b'\n'.join(sorted(response.body.split(b'\n')))
 
1687
        lines = sorted([' '.join([r2, r1]), r1])
 
1688
        response = request.execute('', '')
 
1689
        response.body = '\n'.join(sorted(response.body.split('\n')))
1744
1690
 
1745
1691
        self.assertEqual(
1746
 
            smart_req.SmartServerResponse((b'ok', ), b'\n'.join(lines)),
1747
 
            response)
 
1692
            smart_req.SmartServerResponse(('ok', ), '\n'.join(lines)), response)
1748
1693
 
1749
1694
    def test_specific_revision_argument(self):
1750
1695
        backing = self.get_transport()
1753
1698
        tree.lock_write()
1754
1699
        tree.add('')
1755
1700
        rev_id_utf8 = u'\xc9'.encode('utf-8')
1756
 
        tree.commit('1st commit', rev_id=rev_id_utf8)
1757
 
        tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
 
1701
        r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
 
1702
        r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1758
1703
        tree.unlock()
1759
1704
 
1760
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), rev_id_utf8),
1761
 
                         request.execute(b'', rev_id_utf8))
 
1705
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), rev_id_utf8),
 
1706
            request.execute('', rev_id_utf8))
1762
1707
 
1763
1708
    def test_no_such_revision(self):
1764
1709
        backing = self.get_transport()
1766
1711
        tree = self.make_branch_and_memory_tree('.')
1767
1712
        tree.lock_write()
1768
1713
        tree.add('')
1769
 
        tree.commit('1st commit')
 
1714
        r1 = tree.commit('1st commit')
1770
1715
        tree.unlock()
1771
1716
 
1772
1717
        # Note that it still returns body (of zero bytes).
1773
1718
        self.assertEqual(smart_req.SmartServerResponse(
1774
 
            (b'nosuchrevision', b'missingrevision', ), b''),
1775
 
            request.execute(b'', b'missingrevision'))
 
1719
                ('nosuchrevision', 'missingrevision', ), ''),
 
1720
                         request.execute('', 'missingrevision'))
1776
1721
 
1777
1722
 
1778
1723
class TestSmartServerRepositoryGetRevIdForRevno(
1779
 
        tests.TestCaseWithMemoryTransport):
 
1724
    tests.TestCaseWithMemoryTransport):
1780
1725
 
1781
1726
    def test_revno_found(self):
1782
1727
        backing = self.get_transport()
1790
1735
        tree.commit('2nd commit', rev_id=rev2_id_utf8)
1791
1736
        tree.unlock()
1792
1737
 
1793
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', rev1_id_utf8)),
1794
 
                         request.execute(b'', 1, (2, rev2_id_utf8)))
 
1738
        self.assertEqual(smart_req.SmartServerResponse(('ok', rev1_id_utf8)),
 
1739
            request.execute('', 1, (2, rev2_id_utf8)))
1795
1740
 
1796
1741
    def test_known_revid_missing(self):
1797
1742
        backing = self.get_transport()
1798
1743
        request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1799
 
        self.make_repository('.')
 
1744
        repo = self.make_repository('.')
1800
1745
        self.assertEqual(
1801
 
            smart_req.FailedSmartServerResponse((b'nosuchrevision', b'ghost')),
1802
 
            request.execute(b'', 1, (2, b'ghost')))
 
1746
            smart_req.FailedSmartServerResponse(('nosuchrevision', 'ghost')),
 
1747
            request.execute('', 1, (2, 'ghost')))
1803
1748
 
1804
1749
    def test_history_incomplete(self):
1805
1750
        backing = self.get_transport()
1806
1751
        request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1807
1752
        parent = self.make_branch_and_memory_tree('parent', format='1.9')
1808
1753
        parent.lock_write()
1809
 
        parent.add([''], [b'TREE_ROOT'])
1810
 
        parent.commit(message='first commit')
 
1754
        parent.add([''], ['TREE_ROOT'])
 
1755
        r1 = parent.commit(message='first commit')
1811
1756
        r2 = parent.commit(message='second commit')
1812
1757
        parent.unlock()
1813
1758
        local = self.make_branch_and_memory_tree('local', format='1.9')
1817
1762
        local.branch.create_clone_on_transport(
1818
1763
            self.get_transport('stacked'), stacked_on=self.get_url('parent'))
1819
1764
        self.assertEqual(
1820
 
            smart_req.SmartServerResponse((b'history-incomplete', 2, r2)),
1821
 
            request.execute(b'stacked', 1, (3, r3)))
 
1765
            smart_req.SmartServerResponse(('history-incomplete', 2, r2)),
 
1766
            request.execute('stacked', 1, (3, r3)))
1822
1767
 
1823
1768
 
1824
1769
class TestSmartServerRepositoryIterRevisions(
1825
 
        tests.TestCaseWithMemoryTransport):
 
1770
    tests.TestCaseWithMemoryTransport):
1826
1771
 
1827
1772
    def test_basic(self):
1828
1773
        backing = self.get_transport()
1830
1775
        tree = self.make_branch_and_memory_tree('.', format='2a')
1831
1776
        tree.lock_write()
1832
1777
        tree.add('')
1833
 
        tree.commit('1st commit', rev_id=b"rev1")
1834
 
        tree.commit('2nd commit', rev_id=b"rev2")
 
1778
        tree.commit('1st commit', rev_id="rev1")
 
1779
        tree.commit('2nd commit', rev_id="rev2")
1835
1780
        tree.unlock()
1836
1781
 
1837
 
        self.assertIs(None, request.execute(b''))
1838
 
        response = request.do_body(b"rev1\nrev2")
 
1782
        self.assertIs(None, request.execute(''))
 
1783
        response = request.do_body("rev1\nrev2")
1839
1784
        self.assertTrue(response.is_successful())
1840
1785
        # Format 2a uses serializer format 10
1841
 
        self.assertEqual(response.args, (b"ok", b"10"))
 
1786
        self.assertEqual(response.args, ("ok", "10"))
1842
1787
 
1843
1788
        self.addCleanup(tree.branch.lock_read().unlock)
1844
1789
        entries = [zlib.compress(record.get_bytes_as("fulltext")) for record in
1845
 
                   tree.branch.repository.revisions.get_record_stream(
1846
 
            [(b"rev1", ), (b"rev2", )], "unordered", True)]
 
1790
            tree.branch.repository.revisions.get_record_stream(
 
1791
            [("rev1", ), ("rev2", )], "unordered", True)]
1847
1792
 
1848
 
        contents = b"".join(response.body_stream)
 
1793
        contents = "".join(response.body_stream)
1849
1794
        self.assertTrue(contents in (
1850
 
            b"".join([entries[0], entries[1]]),
1851
 
            b"".join([entries[1], entries[0]])))
 
1795
            "".join([entries[0], entries[1]]),
 
1796
            "".join([entries[1], entries[0]])))
1852
1797
 
1853
1798
    def test_missing(self):
1854
1799
        backing = self.get_transport()
1855
1800
        request = smart_repo.SmartServerRepositoryIterRevisions(backing)
1856
 
        self.make_branch_and_memory_tree('.', format='2a')
 
1801
        tree = self.make_branch_and_memory_tree('.', format='2a')
1857
1802
 
1858
 
        self.assertIs(None, request.execute(b''))
1859
 
        response = request.do_body(b"rev1\nrev2")
 
1803
        self.assertIs(None, request.execute(''))
 
1804
        response = request.do_body("rev1\nrev2")
1860
1805
        self.assertTrue(response.is_successful())
1861
1806
        # Format 2a uses serializer format 10
1862
 
        self.assertEqual(response.args, (b"ok", b"10"))
 
1807
        self.assertEqual(response.args, ("ok", "10"))
1863
1808
 
1864
 
        contents = b"".join(response.body_stream)
1865
 
        self.assertEqual(contents, b"")
 
1809
        contents = "".join(response.body_stream)
 
1810
        self.assertEqual(contents, "")
1866
1811
 
1867
1812
 
1868
1813
class GetStreamTestBase(tests.TestCaseWithMemoryTransport):
1885
1830
        backing = self.get_transport()
1886
1831
        request = smart_repo.SmartServerRepositoryGetStream(backing)
1887
1832
        repo, r1, r2 = self.make_two_commit_repo()
1888
 
        fetch_spec = [b'ancestry-of', r2]
1889
 
        lines = b'\n'.join(fetch_spec)
1890
 
        request.execute(b'', repo._format.network_name())
 
1833
        fetch_spec = ['ancestry-of', r2]
 
1834
        lines = '\n'.join(fetch_spec)
 
1835
        request.execute('', repo._format.network_name())
1891
1836
        response = request.do_body(lines)
1892
 
        self.assertEqual((b'ok',), response.args)
1893
 
        stream_bytes = b''.join(response.body_stream)
1894
 
        self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
 
1837
        self.assertEqual(('ok',), response.args)
 
1838
        stream_bytes = ''.join(response.body_stream)
 
1839
        self.assertStartsWith(stream_bytes, 'Bazaar pack format 1')
1895
1840
 
1896
1841
    def test_search(self):
1897
1842
        """The search argument may be a 'search' of some explicit keys."""
1898
1843
        backing = self.get_transport()
1899
1844
        request = smart_repo.SmartServerRepositoryGetStream(backing)
1900
1845
        repo, r1, r2 = self.make_two_commit_repo()
1901
 
        fetch_spec = [b'search', r1 + b' ' + r2, b'null:', b'2']
1902
 
        lines = b'\n'.join(fetch_spec)
1903
 
        request.execute(b'', repo._format.network_name())
 
1846
        fetch_spec = ['search', '%s %s' % (r1, r2), 'null:', '2']
 
1847
        lines = '\n'.join(fetch_spec)
 
1848
        request.execute('', repo._format.network_name())
1904
1849
        response = request.do_body(lines)
1905
 
        self.assertEqual((b'ok',), response.args)
1906
 
        stream_bytes = b''.join(response.body_stream)
1907
 
        self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
 
1850
        self.assertEqual(('ok',), response.args)
 
1851
        stream_bytes = ''.join(response.body_stream)
 
1852
        self.assertStartsWith(stream_bytes, 'Bazaar pack format 1')
1908
1853
 
1909
1854
    def test_search_everything(self):
1910
1855
        """A search of 'everything' returns a stream."""
1911
1856
        backing = self.get_transport()
1912
1857
        request = smart_repo.SmartServerRepositoryGetStream_1_19(backing)
1913
1858
        repo, r1, r2 = self.make_two_commit_repo()
1914
 
        serialised_fetch_spec = b'everything'
1915
 
        request.execute(b'', repo._format.network_name())
 
1859
        serialised_fetch_spec = 'everything'
 
1860
        request.execute('', repo._format.network_name())
1916
1861
        response = request.do_body(serialised_fetch_spec)
1917
 
        self.assertEqual((b'ok',), response.args)
1918
 
        stream_bytes = b''.join(response.body_stream)
1919
 
        self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
 
1862
        self.assertEqual(('ok',), response.args)
 
1863
        stream_bytes = ''.join(response.body_stream)
 
1864
        self.assertStartsWith(stream_bytes, 'Bazaar pack format 1')
1920
1865
 
1921
1866
 
1922
1867
class TestSmartServerRequestHasRevision(tests.TestCaseWithMemoryTransport):
1926
1871
        backing = self.get_transport()
1927
1872
        request = smart_repo.SmartServerRequestHasRevision(backing)
1928
1873
        self.make_repository('.')
1929
 
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
1930
 
                         request.execute(b'', b'revid'))
 
1874
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
1875
            request.execute('', 'revid'))
1931
1876
 
1932
1877
    def test_present_revision(self):
1933
1878
        """For a present revision, ('yes', ) is returned."""
1937
1882
        tree.lock_write()
1938
1883
        tree.add('')
1939
1884
        rev_id_utf8 = u'\xc8abc'.encode('utf-8')
1940
 
        tree.commit('a commit', rev_id=rev_id_utf8)
 
1885
        r1 = tree.commit('a commit', rev_id=rev_id_utf8)
1941
1886
        tree.unlock()
1942
1887
        self.assertTrue(tree.branch.repository.has_revision(rev_id_utf8))
1943
 
        self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
1944
 
                         request.execute(b'', rev_id_utf8))
 
1888
        self.assertEqual(smart_req.SmartServerResponse(('yes', )),
 
1889
            request.execute('', rev_id_utf8))
1945
1890
 
1946
1891
 
1947
1892
class TestSmartServerRepositoryIterFilesBytes(tests.TestCaseWithTransport):
1951
1896
        request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
1952
1897
        t = self.make_branch_and_tree('.')
1953
1898
        self.addCleanup(t.lock_write().unlock)
1954
 
        self.build_tree_contents([("file", b"somecontents")])
1955
 
        t.add(["file"], [b"thefileid"])
1956
 
        t.commit(rev_id=b'somerev', message="add file")
1957
 
        self.assertIs(None, request.execute(b''))
1958
 
        response = request.do_body(b"thefileid\0somerev\n")
 
1899
        self.build_tree_contents([("file", "somecontents")])
 
1900
        t.add(["file"], ["thefileid"])
 
1901
        t.commit(rev_id='somerev', message="add file")
 
1902
        self.assertIs(None, request.execute(''))
 
1903
        response = request.do_body("thefileid\0somerev\n")
1959
1904
        self.assertTrue(response.is_successful())
1960
 
        self.assertEqual(response.args, (b"ok", ))
1961
 
        self.assertEqual(b"".join(response.body_stream),
1962
 
                         b"ok\x000\n" + zlib.compress(b"somecontents"))
 
1905
        self.assertEqual(response.args, ("ok", ))
 
1906
        self.assertEqual("".join(response.body_stream),
 
1907
            "ok\x000\n" + zlib.compress("somecontents"))
1963
1908
 
1964
1909
    def test_missing(self):
1965
1910
        backing = self.get_transport()
1966
1911
        request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
1967
1912
        t = self.make_branch_and_tree('.')
1968
1913
        self.addCleanup(t.lock_write().unlock)
1969
 
        self.assertIs(None, request.execute(b''))
1970
 
        response = request.do_body(b"thefileid\0revision\n")
 
1914
        self.assertIs(None, request.execute(''))
 
1915
        response = request.do_body("thefileid\0revision\n")
1971
1916
        self.assertTrue(response.is_successful())
1972
 
        self.assertEqual(response.args, (b"ok", ))
1973
 
        self.assertEqual(b"".join(response.body_stream),
1974
 
                         b"absent\x00thefileid\x00revision\x000\n")
 
1917
        self.assertEqual(response.args, ("ok", ))
 
1918
        self.assertEqual("".join(response.body_stream),
 
1919
            "absent\x00thefileid\x00revision\x000\n")
1975
1920
 
1976
1921
 
1977
1922
class TestSmartServerRequestHasSignatureForRevisionId(
1985
1930
        self.make_repository('.')
1986
1931
        self.assertEqual(
1987
1932
            smart_req.FailedSmartServerResponse(
1988
 
                (b'nosuchrevision', b'revid'), None),
1989
 
            request.execute(b'', b'revid'))
 
1933
                ('nosuchrevision', 'revid'), None),
 
1934
            request.execute('', 'revid'))
1990
1935
 
1991
1936
    def test_missing_signature(self):
1992
1937
        """For a missing signature, ('no', ) is returned."""
1996
1941
        tree = self.make_branch_and_memory_tree('.')
1997
1942
        tree.lock_write()
1998
1943
        tree.add('')
1999
 
        tree.commit('a commit', rev_id=b'A')
 
1944
        r1 = tree.commit('a commit', rev_id='A')
2000
1945
        tree.unlock()
2001
 
        self.assertTrue(tree.branch.repository.has_revision(b'A'))
2002
 
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2003
 
                         request.execute(b'', b'A'))
 
1946
        self.assertTrue(tree.branch.repository.has_revision('A'))
 
1947
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
1948
            request.execute('', 'A'))
2004
1949
 
2005
1950
    def test_present_signature(self):
2006
1951
        """For a present signature, ('yes', ) is returned."""
2011
1956
        tree = self.make_branch_and_memory_tree('.')
2012
1957
        tree.lock_write()
2013
1958
        tree.add('')
2014
 
        tree.commit('a commit', rev_id=b'A')
 
1959
        r1 = tree.commit('a commit', rev_id='A')
2015
1960
        tree.branch.repository.start_write_group()
2016
 
        tree.branch.repository.sign_revision(b'A', strategy)
 
1961
        tree.branch.repository.sign_revision('A', strategy)
2017
1962
        tree.branch.repository.commit_write_group()
2018
1963
        tree.unlock()
2019
 
        self.assertTrue(tree.branch.repository.has_revision(b'A'))
2020
 
        self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2021
 
                         request.execute(b'', b'A'))
 
1964
        self.assertTrue(tree.branch.repository.has_revision('A'))
 
1965
        self.assertEqual(smart_req.SmartServerResponse(('yes', )),
 
1966
            request.execute('', 'A'))
2022
1967
 
2023
1968
 
2024
1969
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
2028
1973
        backing = self.get_transport()
2029
1974
        request = smart_repo.SmartServerRepositoryGatherStats(backing)
2030
1975
        repository = self.make_repository('.')
2031
 
        repository.gather_stats()
2032
 
        expected_body = b'revisions: 0\n'
2033
 
        self.assertEqual(
2034
 
            smart_req.SmartServerResponse((b'ok', ), expected_body),
2035
 
            request.execute(b'', b'', b'no'))
 
1976
        stats = repository.gather_stats()
 
1977
        expected_body = 'revisions: 0\n'
 
1978
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), expected_body),
 
1979
                         request.execute('', '', 'no'))
2036
1980
 
2037
1981
    def test_revid_with_committers(self):
2038
1982
        """For a revid we get more infos."""
2048
1992
                    rev_id=rev_id_utf8)
2049
1993
        tree.unlock()
2050
1994
 
2051
 
        tree.branch.repository.gather_stats()
2052
 
        expected_body = (b'firstrev: 123456.200 3600\n'
2053
 
                         b'latestrev: 654321.400 0\n'
2054
 
                         b'revisions: 2\n')
2055
 
        self.assertEqual(
2056
 
            smart_req.SmartServerResponse((b'ok', ), expected_body),
2057
 
            request.execute(b'', rev_id_utf8, b'no'))
 
1995
        stats = tree.branch.repository.gather_stats()
 
1996
        expected_body = ('firstrev: 123456.200 3600\n'
 
1997
                         'latestrev: 654321.400 0\n'
 
1998
                         'revisions: 2\n')
 
1999
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), expected_body),
 
2000
                         request.execute('',
 
2001
                                         rev_id_utf8, 'no'))
2058
2002
 
2059
2003
    def test_not_empty_repository_with_committers(self):
2060
2004
        """For a revid and requesting committers we get the whole thing."""
2070
2014
        tree.commit('a commit', timestamp=654321.4, timezone=0,
2071
2015
                    committer='bar', rev_id=rev_id_utf8)
2072
2016
        tree.unlock()
2073
 
        tree.branch.repository.gather_stats()
 
2017
        stats = tree.branch.repository.gather_stats()
2074
2018
 
2075
 
        expected_body = (b'committers: 2\n'
2076
 
                         b'firstrev: 123456.200 3600\n'
2077
 
                         b'latestrev: 654321.400 0\n'
2078
 
                         b'revisions: 2\n')
2079
 
        self.assertEqual(
2080
 
            smart_req.SmartServerResponse((b'ok', ), expected_body),
2081
 
            request.execute(b'', rev_id_utf8, b'yes'))
 
2019
        expected_body = ('committers: 2\n'
 
2020
                         'firstrev: 123456.200 3600\n'
 
2021
                         'latestrev: 654321.400 0\n'
 
2022
                         'revisions: 2\n')
 
2023
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), expected_body),
 
2024
                         request.execute('',
 
2025
                                         rev_id_utf8, 'yes'))
2082
2026
 
2083
2027
    def test_unknown_revid(self):
2084
2028
        """An unknown revision id causes a 'nosuchrevision' error."""
2085
2029
        backing = self.get_transport()
2086
2030
        request = smart_repo.SmartServerRepositoryGatherStats(backing)
2087
 
        self.make_repository('.')
 
2031
        repository = self.make_repository('.')
 
2032
        expected_body = 'revisions: 0\n'
2088
2033
        self.assertEqual(
2089
2034
            smart_req.FailedSmartServerResponse(
2090
 
                (b'nosuchrevision', b'mia'), None),
2091
 
            request.execute(b'', b'mia', b'yes'))
 
2035
                ('nosuchrevision', 'mia'), None),
 
2036
            request.execute('', 'mia', 'yes'))
2092
2037
 
2093
2038
 
2094
2039
class TestSmartServerRepositoryIsShared(tests.TestCaseWithMemoryTransport):
2098
2043
        backing = self.get_transport()
2099
2044
        request = smart_repo.SmartServerRepositoryIsShared(backing)
2100
2045
        self.make_repository('.', shared=True)
2101
 
        self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2102
 
                         request.execute(b'', ))
 
2046
        self.assertEqual(smart_req.SmartServerResponse(('yes', )),
 
2047
            request.execute('', ))
2103
2048
 
2104
2049
    def test_is_not_shared(self):
2105
2050
        """For a shared repository, ('no', ) is returned."""
2106
2051
        backing = self.get_transport()
2107
2052
        request = smart_repo.SmartServerRepositoryIsShared(backing)
2108
2053
        self.make_repository('.', shared=False)
2109
 
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2110
 
                         request.execute(b'', ))
 
2054
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
2055
            request.execute('', ))
2111
2056
 
2112
2057
 
2113
2058
class TestSmartServerRepositoryGetRevisionSignatureText(
2118
2063
        request = smart_repo.SmartServerRepositoryGetRevisionSignatureText(
2119
2064
            backing)
2120
2065
        bb = self.make_branch_builder('.')
2121
 
        bb.build_commit(rev_id=b'A')
 
2066
        bb.build_commit(rev_id='A')
2122
2067
        repo = bb.get_branch().repository
2123
2068
        strategy = gpg.LoopbackGPGStrategy(None)
2124
2069
        self.addCleanup(repo.lock_write().unlock)
2125
2070
        repo.start_write_group()
2126
 
        repo.sign_revision(b'A', strategy)
 
2071
        repo.sign_revision('A', strategy)
2127
2072
        repo.commit_write_group()
2128
2073
        expected_body = (
2129
 
            b'-----BEGIN PSEUDO-SIGNED CONTENT-----\n' +
2130
 
            Testament.from_revision(repo, b'A').as_short_text() +
2131
 
            b'-----END PSEUDO-SIGNED CONTENT-----\n')
 
2074
            '-----BEGIN PSEUDO-SIGNED CONTENT-----\n' +
 
2075
            Testament.from_revision(repo, 'A').as_short_text() +
 
2076
            '-----END PSEUDO-SIGNED CONTENT-----\n')
2132
2077
        self.assertEqual(
2133
 
            smart_req.SmartServerResponse((b'ok', ), expected_body),
2134
 
            request.execute(b'', b'A'))
 
2078
            smart_req.SmartServerResponse(('ok', ), expected_body),
 
2079
            request.execute('', 'A'))
2135
2080
 
2136
2081
 
2137
2082
class TestSmartServerRepositoryMakeWorkingTrees(
2143
2088
        request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
2144
2089
        r = self.make_repository('.')
2145
2090
        r.set_make_working_trees(True)
2146
 
        self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2147
 
                         request.execute(b'', ))
 
2091
        self.assertEqual(smart_req.SmartServerResponse(('yes', )),
 
2092
            request.execute('', ))
2148
2093
 
2149
2094
    def test_is_not_shared(self):
2150
2095
        """For a repository with working trees, ('no', ) is returned."""
2152
2097
        request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
2153
2098
        r = self.make_repository('.')
2154
2099
        r.set_make_working_trees(False)
2155
 
        self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2156
 
                         request.execute(b'', ))
 
2100
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
2101
            request.execute('', ))
2157
2102
 
2158
2103
 
2159
2104
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
2162
2107
        backing = self.get_transport()
2163
2108
        request = smart_repo.SmartServerRepositoryLockWrite(backing)
2164
2109
        repository = self.make_repository('.', format='knit')
2165
 
        response = request.execute(b'')
 
2110
        response = request.execute('')
2166
2111
        nonce = repository.control_files._lock.peek().get('nonce')
2167
 
        self.assertEqual(smart_req.SmartServerResponse(
2168
 
            (b'ok', nonce)), response)
 
2112
        self.assertEqual(smart_req.SmartServerResponse(('ok', nonce)), response)
2169
2113
        # The repository is now locked.  Verify that with a new repository
2170
2114
        # object.
2171
 
        new_repo = repository.controldir.open_repository()
 
2115
        new_repo = repository.bzrdir.open_repository()
2172
2116
        self.assertRaises(errors.LockContention, new_repo.lock_write)
2173
2117
        # Cleanup
2174
2118
        request = smart_repo.SmartServerRepositoryUnlock(backing)
2175
 
        response = request.execute(b'', nonce)
 
2119
        response = request.execute('', nonce)
2176
2120
 
2177
2121
    def test_lock_write_on_locked_repo(self):
2178
2122
        backing = self.get_transport()
2181
2125
        repo_token = repository.lock_write().repository_token
2182
2126
        repository.leave_lock_in_place()
2183
2127
        repository.unlock()
2184
 
        response = request.execute(b'')
 
2128
        response = request.execute('')
2185
2129
        self.assertEqual(
2186
 
            smart_req.SmartServerResponse((b'LockContention',)), response)
 
2130
            smart_req.SmartServerResponse(('LockContention',)), response)
2187
2131
        # Cleanup
2188
2132
        repository.lock_write(repo_token)
2189
2133
        repository.dont_leave_lock_in_place()
2192
2136
    def test_lock_write_on_readonly_transport(self):
2193
2137
        backing = self.get_readonly_transport()
2194
2138
        request = smart_repo.SmartServerRepositoryLockWrite(backing)
2195
 
        self.make_repository('.', format='knit')
2196
 
        response = request.execute(b'')
 
2139
        repository = self.make_repository('.', format='knit')
 
2140
        response = request.execute('')
2197
2141
        self.assertFalse(response.is_successful())
2198
 
        self.assertEqual(b'LockFailed', response.args[0])
 
2142
        self.assertEqual('LockFailed', response.args[0])
2199
2143
 
2200
2144
 
2201
2145
class TestInsertStreamBase(tests.TestCaseWithMemoryTransport):
2202
2146
 
2203
2147
    def make_empty_byte_stream(self, repo):
2204
2148
        byte_stream = smart_repo._stream_to_byte_stream([], repo._format)
2205
 
        return b''.join(byte_stream)
 
2149
        return ''.join(byte_stream)
2206
2150
 
2207
2151
 
2208
2152
class TestSmartServerRepositoryInsertStream(TestInsertStreamBase):
2211
2155
        backing = self.get_transport()
2212
2156
        request = smart_repo.SmartServerRepositoryInsertStream(backing)
2213
2157
        repository = self.make_repository('.')
2214
 
        response = request.execute(b'', b'')
 
2158
        response = request.execute('', '')
2215
2159
        self.assertEqual(None, response)
2216
2160
        response = request.do_chunk(self.make_empty_byte_stream(repository))
2217
2161
        self.assertEqual(None, response)
2218
2162
        response = request.do_end()
2219
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', )), response)
 
2163
        self.assertEqual(smart_req.SmartServerResponse(('ok', )), response)
2220
2164
 
2221
2165
 
2222
2166
class TestSmartServerRepositoryInsertStreamLocked(TestInsertStreamBase):
2227
2171
            backing)
2228
2172
        repository = self.make_repository('.', format='knit')
2229
2173
        lock_token = repository.lock_write().repository_token
2230
 
        response = request.execute(b'', b'', lock_token)
 
2174
        response = request.execute('', '', lock_token)
2231
2175
        self.assertEqual(None, response)
2232
2176
        response = request.do_chunk(self.make_empty_byte_stream(repository))
2233
2177
        self.assertEqual(None, response)
2234
2178
        response = request.do_end()
2235
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', )), response)
 
2179
        self.assertEqual(smart_req.SmartServerResponse(('ok', )), response)
2236
2180
        repository.unlock()
2237
2181
 
2238
2182
    def test_insert_stream_with_wrong_lock_token(self):
2240
2184
        request = smart_repo.SmartServerRepositoryInsertStreamLocked(
2241
2185
            backing)
2242
2186
        repository = self.make_repository('.', format='knit')
2243
 
        with repository.lock_write():
2244
 
            self.assertRaises(
2245
 
                errors.TokenMismatch, request.execute, b'', b'',
2246
 
                b'wrong-token')
 
2187
        lock_token = repository.lock_write().repository_token
 
2188
        self.assertRaises(
 
2189
            errors.TokenMismatch, request.execute, '', '', 'wrong-token')
 
2190
        repository.unlock()
2247
2191
 
2248
2192
 
2249
2193
class TestSmartServerRepositoryUnlock(tests.TestCaseWithMemoryTransport):
2255
2199
        token = repository.lock_write().repository_token
2256
2200
        repository.leave_lock_in_place()
2257
2201
        repository.unlock()
2258
 
        response = request.execute(b'', token)
 
2202
        response = request.execute('', token)
2259
2203
        self.assertEqual(
2260
 
            smart_req.SmartServerResponse((b'ok',)), response)
 
2204
            smart_req.SmartServerResponse(('ok',)), response)
2261
2205
        # The repository is now unlocked.  Verify that with a new repository
2262
2206
        # object.
2263
 
        new_repo = repository.controldir.open_repository()
 
2207
        new_repo = repository.bzrdir.open_repository()
2264
2208
        new_repo.lock_write()
2265
2209
        new_repo.unlock()
2266
2210
 
2267
2211
    def test_unlock_on_unlocked_repo(self):
2268
2212
        backing = self.get_transport()
2269
2213
        request = smart_repo.SmartServerRepositoryUnlock(backing)
2270
 
        self.make_repository('.', format='knit')
2271
 
        response = request.execute(b'', b'some token')
 
2214
        repository = self.make_repository('.', format='knit')
 
2215
        response = request.execute('', 'some token')
2272
2216
        self.assertEqual(
2273
 
            smart_req.SmartServerResponse((b'TokenMismatch',)), response)
 
2217
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
2274
2218
 
2275
2219
 
2276
2220
class TestSmartServerRepositoryGetPhysicalLockStatus(
2277
 
        tests.TestCaseWithTransport):
 
2221
    tests.TestCaseWithTransport):
2278
2222
 
2279
2223
    def test_with_write_lock(self):
2280
2224
        backing = self.get_transport()
2283
2227
        # lock_write() doesn't necessarily actually take a physical
2284
2228
        # lock out.
2285
2229
        if repo.get_physical_lock_status():
2286
 
            expected = b'yes'
 
2230
            expected = 'yes'
2287
2231
        else:
2288
 
            expected = b'no'
 
2232
            expected = 'no'
2289
2233
        request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2290
2234
        request = request_class(backing)
2291
2235
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((expected,)),
2292
 
                         request.execute(b'', ))
 
2236
            request.execute('', ))
2293
2237
 
2294
2238
    def test_without_write_lock(self):
2295
2239
        backing = self.get_transport()
2297
2241
        self.assertEqual(False, repo.get_physical_lock_status())
2298
2242
        request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2299
2243
        request = request_class(backing)
2300
 
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'no',)),
2301
 
                         request.execute(b'', ))
 
2244
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('no',)),
 
2245
            request.execute('', ))
2302
2246
 
2303
2247
 
2304
2248
class TestSmartServerRepositoryReconcile(tests.TestCaseWithTransport):
2311
2255
        request_class = smart_repo.SmartServerRepositoryReconcile
2312
2256
        request = request_class(backing)
2313
2257
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
2314
 
            (b'ok', ),
2315
 
            b'garbage_inventories: 0\n'
2316
 
            b'inconsistent_parents: 0\n'),
2317
 
            request.execute(b'', token))
 
2258
            ('ok', ),
 
2259
             'garbage_inventories: 0\n'
 
2260
             'inconsistent_parents: 0\n'),
 
2261
            request.execute('', token))
2318
2262
 
2319
2263
 
2320
2264
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
2324
2268
        request = smart_req.SmartServerIsReadonly(backing)
2325
2269
        response = request.execute()
2326
2270
        self.assertEqual(
2327
 
            smart_req.SmartServerResponse((b'no',)), response)
 
2271
            smart_req.SmartServerResponse(('no',)), response)
2328
2272
 
2329
2273
    def test_is_readonly_yes(self):
2330
2274
        backing = self.get_readonly_transport()
2331
2275
        request = smart_req.SmartServerIsReadonly(backing)
2332
2276
        response = request.execute()
2333
2277
        self.assertEqual(
2334
 
            smart_req.SmartServerResponse((b'yes',)), response)
 
2278
            smart_req.SmartServerResponse(('yes',)), response)
2335
2279
 
2336
2280
 
2337
2281
class TestSmartServerRepositorySetMakeWorkingTrees(
2338
 
        tests.TestCaseWithMemoryTransport):
 
2282
    tests.TestCaseWithMemoryTransport):
2339
2283
 
2340
2284
    def test_set_false(self):
2341
2285
        backing = self.get_transport()
2343
2287
        repo.set_make_working_trees(True)
2344
2288
        request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2345
2289
        request = request_class(backing)
2346
 
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2347
 
                         request.execute(b'', b'False'))
2348
 
        repo = repo.controldir.open_repository()
 
2290
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
 
2291
            request.execute('', 'False'))
 
2292
        repo = repo.bzrdir.open_repository()
2349
2293
        self.assertFalse(repo.make_working_trees())
2350
2294
 
2351
2295
    def test_set_true(self):
2354
2298
        repo.set_make_working_trees(False)
2355
2299
        request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2356
2300
        request = request_class(backing)
2357
 
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2358
 
                         request.execute(b'', b'True'))
2359
 
        repo = repo.controldir.open_repository()
 
2301
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
 
2302
            request.execute('', 'True'))
 
2303
        repo = repo.bzrdir.open_repository()
2360
2304
        self.assertTrue(repo.make_working_trees())
2361
2305
 
2362
2306
 
2363
2307
class TestSmartServerRepositoryGetSerializerFormat(
2364
 
        tests.TestCaseWithMemoryTransport):
 
2308
    tests.TestCaseWithMemoryTransport):
2365
2309
 
2366
2310
    def test_get_serializer_format(self):
2367
2311
        backing = self.get_transport()
2369
2313
        request_class = smart_repo.SmartServerRepositoryGetSerializerFormat
2370
2314
        request = request_class(backing)
2371
2315
        self.assertEqual(
2372
 
            smart_req.SuccessfulSmartServerResponse((b'ok', b'10')),
2373
 
            request.execute(b''))
 
2316
            smart_req.SuccessfulSmartServerResponse(('ok', '10')),
 
2317
            request.execute(''))
2374
2318
 
2375
2319
 
2376
2320
class TestSmartServerRepositoryWriteGroup(
2377
 
        tests.TestCaseWithMemoryTransport):
 
2321
    tests.TestCaseWithMemoryTransport):
2378
2322
 
2379
2323
    def test_start_write_group(self):
2380
2324
        backing = self.get_transport()
2383
2327
        self.addCleanup(repo.unlock)
2384
2328
        request_class = smart_repo.SmartServerRepositoryStartWriteGroup
2385
2329
        request = request_class(backing)
2386
 
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok', [])),
2387
 
                         request.execute(b'', lock_token))
 
2330
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok', [])),
 
2331
            request.execute('', lock_token))
2388
2332
 
2389
2333
    def test_start_write_group_unsuspendable(self):
2390
2334
        backing = self.get_transport()
2394
2338
        request_class = smart_repo.SmartServerRepositoryStartWriteGroup
2395
2339
        request = request_class(backing)
2396
2340
        self.assertEqual(
2397
 
            smart_req.FailedSmartServerResponse((b'UnsuspendableWriteGroup',)),
2398
 
            request.execute(b'', lock_token))
 
2341
            smart_req.FailedSmartServerResponse(('UnsuspendableWriteGroup',)),
 
2342
            request.execute('', lock_token))
2399
2343
 
2400
2344
    def test_commit_write_group(self):
2401
2345
        backing = self.get_transport()
2406
2350
        tokens = repo.suspend_write_group()
2407
2351
        request_class = smart_repo.SmartServerRepositoryCommitWriteGroup
2408
2352
        request = request_class(backing)
2409
 
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2410
 
                         request.execute(b'', lock_token, tokens))
 
2353
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
 
2354
            request.execute('', lock_token, tokens))
2411
2355
 
2412
2356
    def test_abort_write_group(self):
2413
2357
        backing = self.get_transport()
2418
2362
        self.addCleanup(repo.unlock)
2419
2363
        request_class = smart_repo.SmartServerRepositoryAbortWriteGroup
2420
2364
        request = request_class(backing)
2421
 
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2422
 
                         request.execute(b'', lock_token, tokens))
 
2365
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
 
2366
            request.execute('', lock_token, tokens))
2423
2367
 
2424
2368
    def test_check_write_group(self):
2425
2369
        backing = self.get_transport()
2430
2374
        self.addCleanup(repo.unlock)
2431
2375
        request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
2432
2376
        request = request_class(backing)
2433
 
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2434
 
                         request.execute(b'', lock_token, tokens))
 
2377
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
 
2378
            request.execute('', lock_token, tokens))
2435
2379
 
2436
2380
    def test_check_write_group_invalid(self):
2437
2381
        backing = self.get_transport()
2441
2385
        request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
2442
2386
        request = request_class(backing)
2443
2387
        self.assertEqual(smart_req.FailedSmartServerResponse(
2444
 
            (b'UnresumableWriteGroup', [b'random'],
2445
 
                b'Malformed write group token')),
2446
 
            request.execute(b'', lock_token, [b"random"]))
 
2388
            ('UnresumableWriteGroup', ['random'],
 
2389
                'Malformed write group token')),
 
2390
            request.execute('', lock_token, ["random"]))
2447
2391
 
2448
2392
 
2449
2393
class TestSmartServerPackRepositoryAutopack(tests.TestCaseWithTransport):
2467
2411
        backing = self.get_transport()
2468
2412
        request = smart_packrepo.SmartServerPackRepositoryAutopack(
2469
2413
            backing)
2470
 
        response = request.execute(b'')
2471
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
 
2414
        response = request.execute('')
 
2415
        self.assertEqual(smart_req.SmartServerResponse(('ok',)), response)
2472
2416
        repo._pack_collection.reload_pack_names()
2473
2417
        self.assertEqual(1, len(repo._pack_collection.names()))
2474
2418
 
2482
2426
        backing = self.get_transport()
2483
2427
        request = smart_packrepo.SmartServerPackRepositoryAutopack(
2484
2428
            backing)
2485
 
        response = request.execute(b'')
2486
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
 
2429
        response = request.execute('')
 
2430
        self.assertEqual(smart_req.SmartServerResponse(('ok',)), response)
2487
2431
        repo._pack_collection.reload_pack_names()
2488
2432
        self.assertEqual(9, len(repo._pack_collection.names()))
2489
2433
 
2493
2437
        backing = self.get_transport()
2494
2438
        request = smart_packrepo.SmartServerPackRepositoryAutopack(
2495
2439
            backing)
2496
 
        response = request.execute(b'')
2497
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
 
2440
        response = request.execute('')
 
2441
        self.assertEqual(smart_req.SmartServerResponse(('ok',)), response)
2498
2442
 
2499
2443
 
2500
2444
class TestSmartServerVfsGet(tests.TestCaseWithMemoryTransport):
2505
2449
        filename_escaped = urlutils.escape(filename)
2506
2450
        backing = self.get_transport()
2507
2451
        request = vfs.GetRequest(backing)
2508
 
        backing.put_bytes_non_atomic(filename_escaped, b'contents')
2509
 
        self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'contents'),
2510
 
                         request.execute(filename_escaped.encode('ascii')))
 
2452
        backing.put_bytes_non_atomic(filename_escaped, 'contents')
 
2453
        self.assertEqual(smart_req.SmartServerResponse(('ok', ), 'contents'),
 
2454
            request.execute(filename_escaped))
2511
2455
 
2512
2456
 
2513
2457
class TestHandlers(tests.TestCase):
2520
2464
        for key in smart_req.request_handlers.keys():
2521
2465
            try:
2522
2466
                item = smart_req.request_handlers.get(key)
2523
 
            except AttributeError as e:
 
2467
            except AttributeError, e:
2524
2468
                raise AttributeError('failed to get %s: %s' % (key, e))
2525
2469
 
2526
2470
    def assertHandlerEqual(self, verb, handler):
2528
2472
 
2529
2473
    def test_registered_methods(self):
2530
2474
        """Test that known methods are registered to the correct object."""
2531
 
        self.assertHandlerEqual(b'Branch.break_lock',
2532
 
                                smart_branch.SmartServerBranchBreakLock)
2533
 
        self.assertHandlerEqual(b'Branch.get_config_file',
2534
 
                                smart_branch.SmartServerBranchGetConfigFile)
2535
 
        self.assertHandlerEqual(b'Branch.put_config_file',
2536
 
                                smart_branch.SmartServerBranchPutConfigFile)
2537
 
        self.assertHandlerEqual(b'Branch.get_parent',
2538
 
                                smart_branch.SmartServerBranchGetParent)
2539
 
        self.assertHandlerEqual(b'Branch.get_physical_lock_status',
2540
 
                                smart_branch.SmartServerBranchRequestGetPhysicalLockStatus)
2541
 
        self.assertHandlerEqual(b'Branch.get_tags_bytes',
2542
 
                                smart_branch.SmartServerBranchGetTagsBytes)
2543
 
        self.assertHandlerEqual(b'Branch.lock_write',
2544
 
                                smart_branch.SmartServerBranchRequestLockWrite)
2545
 
        self.assertHandlerEqual(b'Branch.last_revision_info',
2546
 
                                smart_branch.SmartServerBranchRequestLastRevisionInfo)
2547
 
        self.assertHandlerEqual(b'Branch.revision_history',
2548
 
                                smart_branch.SmartServerRequestRevisionHistory)
2549
 
        self.assertHandlerEqual(b'Branch.revision_id_to_revno',
2550
 
                                smart_branch.SmartServerBranchRequestRevisionIdToRevno)
2551
 
        self.assertHandlerEqual(b'Branch.set_config_option',
2552
 
                                smart_branch.SmartServerBranchRequestSetConfigOption)
2553
 
        self.assertHandlerEqual(b'Branch.set_last_revision',
2554
 
                                smart_branch.SmartServerBranchRequestSetLastRevision)
2555
 
        self.assertHandlerEqual(b'Branch.set_last_revision_info',
2556
 
                                smart_branch.SmartServerBranchRequestSetLastRevisionInfo)
2557
 
        self.assertHandlerEqual(b'Branch.set_last_revision_ex',
2558
 
                                smart_branch.SmartServerBranchRequestSetLastRevisionEx)
2559
 
        self.assertHandlerEqual(b'Branch.set_parent_location',
2560
 
                                smart_branch.SmartServerBranchRequestSetParentLocation)
2561
 
        self.assertHandlerEqual(b'Branch.unlock',
2562
 
                                smart_branch.SmartServerBranchRequestUnlock)
2563
 
        self.assertHandlerEqual(b'BzrDir.destroy_branch',
2564
 
                                smart_dir.SmartServerBzrDirRequestDestroyBranch)
2565
 
        self.assertHandlerEqual(b'BzrDir.find_repository',
2566
 
                                smart_dir.SmartServerRequestFindRepositoryV1)
2567
 
        self.assertHandlerEqual(b'BzrDir.find_repositoryV2',
2568
 
                                smart_dir.SmartServerRequestFindRepositoryV2)
2569
 
        self.assertHandlerEqual(b'BzrDirFormat.initialize',
2570
 
                                smart_dir.SmartServerRequestInitializeBzrDir)
2571
 
        self.assertHandlerEqual(b'BzrDirFormat.initialize_ex_1.16',
2572
 
                                smart_dir.SmartServerRequestBzrDirInitializeEx)
2573
 
        self.assertHandlerEqual(b'BzrDir.checkout_metadir',
2574
 
                                smart_dir.SmartServerBzrDirRequestCheckoutMetaDir)
2575
 
        self.assertHandlerEqual(b'BzrDir.cloning_metadir',
2576
 
                                smart_dir.SmartServerBzrDirRequestCloningMetaDir)
2577
 
        self.assertHandlerEqual(b'BzrDir.get_branches',
2578
 
                                smart_dir.SmartServerBzrDirRequestGetBranches)
2579
 
        self.assertHandlerEqual(b'BzrDir.get_config_file',
2580
 
                                smart_dir.SmartServerBzrDirRequestConfigFile)
2581
 
        self.assertHandlerEqual(b'BzrDir.open_branch',
2582
 
                                smart_dir.SmartServerRequestOpenBranch)
2583
 
        self.assertHandlerEqual(b'BzrDir.open_branchV2',
2584
 
                                smart_dir.SmartServerRequestOpenBranchV2)
2585
 
        self.assertHandlerEqual(b'BzrDir.open_branchV3',
2586
 
                                smart_dir.SmartServerRequestOpenBranchV3)
2587
 
        self.assertHandlerEqual(b'PackRepository.autopack',
2588
 
                                smart_packrepo.SmartServerPackRepositoryAutopack)
2589
 
        self.assertHandlerEqual(b'Repository.add_signature_text',
2590
 
                                smart_repo.SmartServerRepositoryAddSignatureText)
2591
 
        self.assertHandlerEqual(b'Repository.all_revision_ids',
2592
 
                                smart_repo.SmartServerRepositoryAllRevisionIds)
2593
 
        self.assertHandlerEqual(b'Repository.break_lock',
2594
 
                                smart_repo.SmartServerRepositoryBreakLock)
2595
 
        self.assertHandlerEqual(b'Repository.gather_stats',
2596
 
                                smart_repo.SmartServerRepositoryGatherStats)
2597
 
        self.assertHandlerEqual(b'Repository.get_parent_map',
2598
 
                                smart_repo.SmartServerRepositoryGetParentMap)
2599
 
        self.assertHandlerEqual(b'Repository.get_physical_lock_status',
2600
 
                                smart_repo.SmartServerRepositoryGetPhysicalLockStatus)
2601
 
        self.assertHandlerEqual(b'Repository.get_rev_id_for_revno',
2602
 
                                smart_repo.SmartServerRepositoryGetRevIdForRevno)
2603
 
        self.assertHandlerEqual(b'Repository.get_revision_graph',
2604
 
                                smart_repo.SmartServerRepositoryGetRevisionGraph)
2605
 
        self.assertHandlerEqual(b'Repository.get_revision_signature_text',
2606
 
                                smart_repo.SmartServerRepositoryGetRevisionSignatureText)
2607
 
        self.assertHandlerEqual(b'Repository.get_stream',
2608
 
                                smart_repo.SmartServerRepositoryGetStream)
2609
 
        self.assertHandlerEqual(b'Repository.get_stream_1.19',
2610
 
                                smart_repo.SmartServerRepositoryGetStream_1_19)
2611
 
        self.assertHandlerEqual(b'Repository.iter_revisions',
2612
 
                                smart_repo.SmartServerRepositoryIterRevisions)
2613
 
        self.assertHandlerEqual(b'Repository.has_revision',
2614
 
                                smart_repo.SmartServerRequestHasRevision)
2615
 
        self.assertHandlerEqual(b'Repository.insert_stream',
2616
 
                                smart_repo.SmartServerRepositoryInsertStream)
2617
 
        self.assertHandlerEqual(b'Repository.insert_stream_locked',
2618
 
                                smart_repo.SmartServerRepositoryInsertStreamLocked)
2619
 
        self.assertHandlerEqual(b'Repository.is_shared',
2620
 
                                smart_repo.SmartServerRepositoryIsShared)
2621
 
        self.assertHandlerEqual(b'Repository.iter_files_bytes',
2622
 
                                smart_repo.SmartServerRepositoryIterFilesBytes)
2623
 
        self.assertHandlerEqual(b'Repository.lock_write',
2624
 
                                smart_repo.SmartServerRepositoryLockWrite)
2625
 
        self.assertHandlerEqual(b'Repository.make_working_trees',
2626
 
                                smart_repo.SmartServerRepositoryMakeWorkingTrees)
2627
 
        self.assertHandlerEqual(b'Repository.pack',
2628
 
                                smart_repo.SmartServerRepositoryPack)
2629
 
        self.assertHandlerEqual(b'Repository.reconcile',
2630
 
                                smart_repo.SmartServerRepositoryReconcile)
2631
 
        self.assertHandlerEqual(b'Repository.tarball',
2632
 
                                smart_repo.SmartServerRepositoryTarball)
2633
 
        self.assertHandlerEqual(b'Repository.unlock',
2634
 
                                smart_repo.SmartServerRepositoryUnlock)
2635
 
        self.assertHandlerEqual(b'Repository.start_write_group',
2636
 
                                smart_repo.SmartServerRepositoryStartWriteGroup)
2637
 
        self.assertHandlerEqual(b'Repository.check_write_group',
2638
 
                                smart_repo.SmartServerRepositoryCheckWriteGroup)
2639
 
        self.assertHandlerEqual(b'Repository.commit_write_group',
2640
 
                                smart_repo.SmartServerRepositoryCommitWriteGroup)
2641
 
        self.assertHandlerEqual(b'Repository.abort_write_group',
2642
 
                                smart_repo.SmartServerRepositoryAbortWriteGroup)
2643
 
        self.assertHandlerEqual(b'VersionedFileRepository.get_serializer_format',
2644
 
                                smart_repo.SmartServerRepositoryGetSerializerFormat)
2645
 
        self.assertHandlerEqual(b'VersionedFileRepository.get_inventories',
2646
 
                                smart_repo.SmartServerRepositoryGetInventories)
2647
 
        self.assertHandlerEqual(b'Transport.is_readonly',
2648
 
                                smart_req.SmartServerIsReadonly)
 
2475
        self.assertHandlerEqual('Branch.break_lock',
 
2476
            smart_branch.SmartServerBranchBreakLock)
 
2477
        self.assertHandlerEqual('Branch.get_config_file',
 
2478
            smart_branch.SmartServerBranchGetConfigFile)
 
2479
        self.assertHandlerEqual('Branch.put_config_file',
 
2480
            smart_branch.SmartServerBranchPutConfigFile)
 
2481
        self.assertHandlerEqual('Branch.get_parent',
 
2482
            smart_branch.SmartServerBranchGetParent)
 
2483
        self.assertHandlerEqual('Branch.get_physical_lock_status',
 
2484
            smart_branch.SmartServerBranchRequestGetPhysicalLockStatus)
 
2485
        self.assertHandlerEqual('Branch.get_tags_bytes',
 
2486
            smart_branch.SmartServerBranchGetTagsBytes)
 
2487
        self.assertHandlerEqual('Branch.lock_write',
 
2488
            smart_branch.SmartServerBranchRequestLockWrite)
 
2489
        self.assertHandlerEqual('Branch.last_revision_info',
 
2490
            smart_branch.SmartServerBranchRequestLastRevisionInfo)
 
2491
        self.assertHandlerEqual('Branch.revision_history',
 
2492
            smart_branch.SmartServerRequestRevisionHistory)
 
2493
        self.assertHandlerEqual('Branch.revision_id_to_revno',
 
2494
            smart_branch.SmartServerBranchRequestRevisionIdToRevno)
 
2495
        self.assertHandlerEqual('Branch.set_config_option',
 
2496
            smart_branch.SmartServerBranchRequestSetConfigOption)
 
2497
        self.assertHandlerEqual('Branch.set_last_revision',
 
2498
            smart_branch.SmartServerBranchRequestSetLastRevision)
 
2499
        self.assertHandlerEqual('Branch.set_last_revision_info',
 
2500
            smart_branch.SmartServerBranchRequestSetLastRevisionInfo)
 
2501
        self.assertHandlerEqual('Branch.set_last_revision_ex',
 
2502
            smart_branch.SmartServerBranchRequestSetLastRevisionEx)
 
2503
        self.assertHandlerEqual('Branch.set_parent_location',
 
2504
            smart_branch.SmartServerBranchRequestSetParentLocation)
 
2505
        self.assertHandlerEqual('Branch.unlock',
 
2506
            smart_branch.SmartServerBranchRequestUnlock)
 
2507
        self.assertHandlerEqual('BzrDir.destroy_branch',
 
2508
            smart_dir.SmartServerBzrDirRequestDestroyBranch)
 
2509
        self.assertHandlerEqual('BzrDir.find_repository',
 
2510
            smart_dir.SmartServerRequestFindRepositoryV1)
 
2511
        self.assertHandlerEqual('BzrDir.find_repositoryV2',
 
2512
            smart_dir.SmartServerRequestFindRepositoryV2)
 
2513
        self.assertHandlerEqual('BzrDirFormat.initialize',
 
2514
            smart_dir.SmartServerRequestInitializeBzrDir)
 
2515
        self.assertHandlerEqual('BzrDirFormat.initialize_ex_1.16',
 
2516
            smart_dir.SmartServerRequestBzrDirInitializeEx)
 
2517
        self.assertHandlerEqual('BzrDir.checkout_metadir',
 
2518
            smart_dir.SmartServerBzrDirRequestCheckoutMetaDir)
 
2519
        self.assertHandlerEqual('BzrDir.cloning_metadir',
 
2520
            smart_dir.SmartServerBzrDirRequestCloningMetaDir)
 
2521
        self.assertHandlerEqual('BzrDir.get_branches',
 
2522
            smart_dir.SmartServerBzrDirRequestGetBranches)
 
2523
        self.assertHandlerEqual('BzrDir.get_config_file',
 
2524
            smart_dir.SmartServerBzrDirRequestConfigFile)
 
2525
        self.assertHandlerEqual('BzrDir.open_branch',
 
2526
            smart_dir.SmartServerRequestOpenBranch)
 
2527
        self.assertHandlerEqual('BzrDir.open_branchV2',
 
2528
            smart_dir.SmartServerRequestOpenBranchV2)
 
2529
        self.assertHandlerEqual('BzrDir.open_branchV3',
 
2530
            smart_dir.SmartServerRequestOpenBranchV3)
 
2531
        self.assertHandlerEqual('PackRepository.autopack',
 
2532
            smart_packrepo.SmartServerPackRepositoryAutopack)
 
2533
        self.assertHandlerEqual('Repository.add_signature_text',
 
2534
            smart_repo.SmartServerRepositoryAddSignatureText)
 
2535
        self.assertHandlerEqual('Repository.all_revision_ids',
 
2536
            smart_repo.SmartServerRepositoryAllRevisionIds)
 
2537
        self.assertHandlerEqual('Repository.break_lock',
 
2538
            smart_repo.SmartServerRepositoryBreakLock)
 
2539
        self.assertHandlerEqual('Repository.gather_stats',
 
2540
            smart_repo.SmartServerRepositoryGatherStats)
 
2541
        self.assertHandlerEqual('Repository.get_parent_map',
 
2542
            smart_repo.SmartServerRepositoryGetParentMap)
 
2543
        self.assertHandlerEqual('Repository.get_physical_lock_status',
 
2544
            smart_repo.SmartServerRepositoryGetPhysicalLockStatus)
 
2545
        self.assertHandlerEqual('Repository.get_rev_id_for_revno',
 
2546
            smart_repo.SmartServerRepositoryGetRevIdForRevno)
 
2547
        self.assertHandlerEqual('Repository.get_revision_graph',
 
2548
            smart_repo.SmartServerRepositoryGetRevisionGraph)
 
2549
        self.assertHandlerEqual('Repository.get_revision_signature_text',
 
2550
            smart_repo.SmartServerRepositoryGetRevisionSignatureText)
 
2551
        self.assertHandlerEqual('Repository.get_stream',
 
2552
            smart_repo.SmartServerRepositoryGetStream)
 
2553
        self.assertHandlerEqual('Repository.get_stream_1.19',
 
2554
            smart_repo.SmartServerRepositoryGetStream_1_19)
 
2555
        self.assertHandlerEqual('Repository.iter_revisions',
 
2556
            smart_repo.SmartServerRepositoryIterRevisions)
 
2557
        self.assertHandlerEqual('Repository.has_revision',
 
2558
            smart_repo.SmartServerRequestHasRevision)
 
2559
        self.assertHandlerEqual('Repository.insert_stream',
 
2560
            smart_repo.SmartServerRepositoryInsertStream)
 
2561
        self.assertHandlerEqual('Repository.insert_stream_locked',
 
2562
            smart_repo.SmartServerRepositoryInsertStreamLocked)
 
2563
        self.assertHandlerEqual('Repository.is_shared',
 
2564
            smart_repo.SmartServerRepositoryIsShared)
 
2565
        self.assertHandlerEqual('Repository.iter_files_bytes',
 
2566
            smart_repo.SmartServerRepositoryIterFilesBytes)
 
2567
        self.assertHandlerEqual('Repository.lock_write',
 
2568
            smart_repo.SmartServerRepositoryLockWrite)
 
2569
        self.assertHandlerEqual('Repository.make_working_trees',
 
2570
            smart_repo.SmartServerRepositoryMakeWorkingTrees)
 
2571
        self.assertHandlerEqual('Repository.pack',
 
2572
            smart_repo.SmartServerRepositoryPack)
 
2573
        self.assertHandlerEqual('Repository.reconcile',
 
2574
            smart_repo.SmartServerRepositoryReconcile)
 
2575
        self.assertHandlerEqual('Repository.tarball',
 
2576
            smart_repo.SmartServerRepositoryTarball)
 
2577
        self.assertHandlerEqual('Repository.unlock',
 
2578
            smart_repo.SmartServerRepositoryUnlock)
 
2579
        self.assertHandlerEqual('Repository.start_write_group',
 
2580
            smart_repo.SmartServerRepositoryStartWriteGroup)
 
2581
        self.assertHandlerEqual('Repository.check_write_group',
 
2582
            smart_repo.SmartServerRepositoryCheckWriteGroup)
 
2583
        self.assertHandlerEqual('Repository.commit_write_group',
 
2584
            smart_repo.SmartServerRepositoryCommitWriteGroup)
 
2585
        self.assertHandlerEqual('Repository.abort_write_group',
 
2586
            smart_repo.SmartServerRepositoryAbortWriteGroup)
 
2587
        self.assertHandlerEqual('VersionedFileRepository.get_serializer_format',
 
2588
            smart_repo.SmartServerRepositoryGetSerializerFormat)
 
2589
        self.assertHandlerEqual('VersionedFileRepository.get_inventories',
 
2590
            smart_repo.SmartServerRepositoryGetInventories)
 
2591
        self.assertHandlerEqual('Transport.is_readonly',
 
2592
            smart_req.SmartServerIsReadonly)
2649
2593
 
2650
2594
 
2651
2595
class SmartTCPServerHookTests(tests.TestCaseWithMemoryTransport):
2659
2603
        """Test the server started hooks get fired properly."""
2660
2604
        started_calls = []
2661
2605
        server.SmartTCPServer.hooks.install_named_hook('server_started',
2662
 
                                                       lambda backing_urls, url: started_calls.append(
2663
 
                                                           (backing_urls, url)),
2664
 
                                                       None)
 
2606
            lambda backing_urls, url: started_calls.append((backing_urls, url)),
 
2607
            None)
2665
2608
        started_ex_calls = []
2666
2609
        server.SmartTCPServer.hooks.install_named_hook('server_started_ex',
2667
 
                                                       lambda backing_urls, url: started_ex_calls.append(
2668
 
                                                           (backing_urls, url)),
2669
 
                                                       None)
 
2610
            lambda backing_urls, url: started_ex_calls.append((backing_urls, url)),
 
2611
            None)
2670
2612
        self.server._sockname = ('example.com', 42)
2671
2613
        self.server.run_server_started_hooks()
2672
2614
        self.assertEqual(started_calls,
2673
 
                         [([self.get_transport().base], 'bzr://example.com:42/')])
 
2615
            [([self.get_transport().base], 'bzr://example.com:42/')])
2674
2616
        self.assertEqual(started_ex_calls,
2675
 
                         [([self.get_transport().base], self.server)])
 
2617
            [([self.get_transport().base], self.server)])
2676
2618
 
2677
2619
    def test_run_server_started_hooks_ipv6(self):
2678
2620
        """Test that socknames can contain 4-tuples."""
2679
2621
        self.server._sockname = ('::', 42, 0, 0)
2680
2622
        started_calls = []
2681
2623
        server.SmartTCPServer.hooks.install_named_hook('server_started',
2682
 
                                                       lambda backing_urls, url: started_calls.append(
2683
 
                                                           (backing_urls, url)),
2684
 
                                                       None)
 
2624
            lambda backing_urls, url: started_calls.append((backing_urls, url)),
 
2625
            None)
2685
2626
        self.server.run_server_started_hooks()
2686
2627
        self.assertEqual(started_calls,
2687
 
                         [([self.get_transport().base], 'bzr://:::42/')])
 
2628
                [([self.get_transport().base], 'bzr://:::42/')])
2688
2629
 
2689
2630
    def test_run_server_stopped_hooks(self):
2690
2631
        """Test the server stopped hooks."""
2691
2632
        self.server._sockname = ('example.com', 42)
2692
2633
        stopped_calls = []
2693
2634
        server.SmartTCPServer.hooks.install_named_hook('server_stopped',
2694
 
                                                       lambda backing_urls, url: stopped_calls.append(
2695
 
                                                           (backing_urls, url)),
2696
 
                                                       None)
 
2635
            lambda backing_urls, url: stopped_calls.append((backing_urls, url)),
 
2636
            None)
2697
2637
        self.server.run_server_stopped_hooks()
2698
2638
        self.assertEqual(stopped_calls,
2699
 
                         [([self.get_transport().base], 'bzr://example.com:42/')])
 
2639
            [([self.get_transport().base], 'bzr://example.com:42/')])
2700
2640
 
2701
2641
 
2702
2642
class TestSmartServerRepositoryPack(tests.TestCaseWithMemoryTransport):
2707
2647
        tree = self.make_branch_and_memory_tree('.')
2708
2648
        repo_token = tree.branch.repository.lock_write().repository_token
2709
2649
 
2710
 
        self.assertIs(None, request.execute(b'', repo_token, False))
 
2650
        self.assertIs(None, request.execute('', repo_token, False))
2711
2651
 
2712
2652
        self.assertEqual(
2713
 
            smart_req.SuccessfulSmartServerResponse((b'ok', ), ),
2714
 
            request.do_body(b''))
 
2653
            smart_req.SuccessfulSmartServerResponse(('ok', ), ),
 
2654
            request.do_body(''))
2715
2655
 
2716
2656
 
2717
2657
class TestSmartServerRepositoryGetInventories(tests.TestCaseWithTransport):
2720
2660
        base_inv = repository.revision_tree(base_revid).root_inventory
2721
2661
        inv = repository.revision_tree(revid).root_inventory
2722
2662
        inv_delta = inv._make_delta(base_inv)
2723
 
        serializer = inventory_delta.InventoryDeltaSerializer(True, True)
2724
 
        return b"".join(serializer.delta_to_lines(base_revid, revid, inv_delta))
 
2663
        serializer = inventory_delta.InventoryDeltaSerializer(True, False)
 
2664
        return "".join(serializer.delta_to_lines(base_revid, revid, inv_delta))
2725
2665
 
2726
2666
    def test_single(self):
2727
2667
        backing = self.get_transport()
2728
2668
        request = smart_repo.SmartServerRepositoryGetInventories(backing)
2729
2669
        t = self.make_branch_and_tree('.', format='2a')
2730
2670
        self.addCleanup(t.lock_write().unlock)
2731
 
        self.build_tree_contents([("file", b"somecontents")])
2732
 
        t.add(["file"], [b"thefileid"])
2733
 
        t.commit(rev_id=b'somerev', message="add file")
2734
 
        self.assertIs(None, request.execute(b'', b'unordered'))
2735
 
        response = request.do_body(b"somerev\n")
 
2671
        self.build_tree_contents([("file", "somecontents")])
 
2672
        t.add(["file"], ["thefileid"])
 
2673
        t.commit(rev_id='somerev', message="add file")
 
2674
        self.assertIs(None, request.execute('', 'unordered'))
 
2675
        response = request.do_body("somerev\n")
2736
2676
        self.assertTrue(response.is_successful())
2737
 
        self.assertEqual(response.args, (b"ok", ))
 
2677
        self.assertEqual(response.args, ("ok", ))
2738
2678
        stream = [('inventory-deltas', [
2739
 
            versionedfile.FulltextContentFactory(b'somerev', None, None,
2740
 
                                                 self._get_serialized_inventory_delta(
2741
 
                                                     t.branch.repository, b'null:', b'somerev'))])]
 
2679
            versionedfile.FulltextContentFactory('somerev', None, None,
 
2680
                self._get_serialized_inventory_delta(
 
2681
                    t.branch.repository, 'null:', 'somerev'))])]
2742
2682
        fmt = controldir.format_registry.get('2a')().repository_format
2743
2683
        self.assertEqual(
2744
 
            b"".join(response.body_stream),
2745
 
            b"".join(smart_repo._stream_to_byte_stream(stream, fmt)))
 
2684
            "".join(response.body_stream),
 
2685
            "".join(smart_repo._stream_to_byte_stream(stream, fmt)))
2746
2686
 
2747
2687
    def test_empty(self):
2748
2688
        backing = self.get_transport()
2749
2689
        request = smart_repo.SmartServerRepositoryGetInventories(backing)
2750
2690
        t = self.make_branch_and_tree('.', format='2a')
2751
2691
        self.addCleanup(t.lock_write().unlock)
2752
 
        self.build_tree_contents([("file", b"somecontents")])
2753
 
        t.add(["file"], [b"thefileid"])
2754
 
        t.commit(rev_id=b'somerev', message="add file")
2755
 
        self.assertIs(None, request.execute(b'', b'unordered'))
2756
 
        response = request.do_body(b"")
2757
 
        self.assertTrue(response.is_successful())
2758
 
        self.assertEqual(response.args, (b"ok", ))
2759
 
        self.assertEqual(b"".join(response.body_stream),
2760
 
                         b"Bazaar pack format 1 (introduced in 0.18)\nB54\n\nBazaar repository format 2a (needs bzr 1.16 or later)\nE")
2761
 
 
2762
 
 
2763
 
class TestSmartServerRepositoryGetStreamForMissingKeys(GetStreamTestBase):
2764
 
 
2765
 
    def test_missing(self):
2766
 
        """The search argument may be a 'ancestry-of' some heads'."""
2767
 
        backing = self.get_transport()
2768
 
        request = smart_repo.SmartServerRepositoryGetStreamForMissingKeys(
2769
 
            backing)
2770
 
        repo, r1, r2 = self.make_two_commit_repo()
2771
 
        request.execute(b'', repo._format.network_name())
2772
 
        lines = b'inventories\t' + r1
2773
 
        response = request.do_body(lines)
2774
 
        self.assertEqual((b'ok',), response.args)
2775
 
        stream_bytes = b''.join(response.body_stream)
2776
 
        self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
2777
 
 
2778
 
    def test_unknown_format(self):
2779
 
        """The format may not be known by the remote server."""
2780
 
        backing = self.get_transport()
2781
 
        request = smart_repo.SmartServerRepositoryGetStreamForMissingKeys(
2782
 
            backing)
2783
 
        repo, r1, r2 = self.make_two_commit_repo()
2784
 
        request.execute(b'', b'yada yada yada')
2785
 
        expected = smart_req.FailedSmartServerResponse(
2786
 
            (b'UnknownFormat', b'repository', b'yada yada yada'))
2787
 
 
2788
 
 
2789
 
class TestSmartServerRepositoryRevisionArchive(tests.TestCaseWithTransport):
2790
 
    def test_get(self):
2791
 
        backing = self.get_transport()
2792
 
        request = smart_repo.SmartServerRepositoryRevisionArchive(backing)
2793
 
        t = self.make_branch_and_tree('.')
2794
 
        self.addCleanup(t.lock_write().unlock)
2795
 
        self.build_tree_contents([("file", b"somecontents")])
2796
 
        t.add(["file"], [b"thefileid"])
2797
 
        t.commit(rev_id=b'somerev', message="add file")
2798
 
        response = request.execute(b'', b"somerev", b"tar", b"foo.tar", b"foo")
2799
 
        self.assertTrue(response.is_successful())
2800
 
        self.assertEqual(response.args, (b"ok", ))
2801
 
        b = BytesIO(b"".join(response.body_stream))
2802
 
        with tarfile.open(mode='r', fileobj=b) as tf:
2803
 
            self.assertEqual(['foo/file'], tf.getnames())
2804
 
 
2805
 
 
2806
 
class TestSmartServerRepositoryAnnotateFileRevision(tests.TestCaseWithTransport):
2807
 
 
2808
 
    def test_get(self):
2809
 
        backing = self.get_transport()
2810
 
        request = smart_repo.SmartServerRepositoryAnnotateFileRevision(backing)
2811
 
        t = self.make_branch_and_tree('.')
2812
 
        self.addCleanup(t.lock_write().unlock)
2813
 
        self.build_tree_contents([("file", b"somecontents\nmorecontents\n")])
2814
 
        t.add(["file"], [b"thefileid"])
2815
 
        t.commit(rev_id=b'somerev', message="add file")
2816
 
        response = request.execute(b'', b"somerev", b"file")
2817
 
        self.assertTrue(response.is_successful())
2818
 
        self.assertEqual(response.args, (b"ok", ))
2819
 
        self.assertEqual(
2820
 
            [[b'somerev', b'somecontents\n'], [b'somerev', b'morecontents\n']],
2821
 
            bencode.bdecode(response.body))
2822
 
 
2823
 
 
2824
 
class TestSmartServerBranchRequestGetAllReferenceInfo(TestLockedBranch):
2825
 
 
2826
 
    def test_get_some(self):
2827
 
        backing = self.get_transport()
2828
 
        request = smart_branch.SmartServerBranchRequestGetAllReferenceInfo(backing)
2829
 
        branch = self.make_branch('.')
2830
 
        branch.set_reference_info('some/path', 'http://www.example.com/')
2831
 
        response = request.execute(b'')
2832
 
        self.assertTrue(response.is_successful())
2833
 
        self.assertEqual(response.args, (b"ok", ))
2834
 
        self.assertEqual(
2835
 
            [[b'some/path', b'http://www.example.com/', b'']],
2836
 
            bencode.bdecode(response.body))
 
2692
        self.build_tree_contents([("file", "somecontents")])
 
2693
        t.add(["file"], ["thefileid"])
 
2694
        t.commit(rev_id='somerev', message="add file")
 
2695
        self.assertIs(None, request.execute('', 'unordered'))
 
2696
        response = request.do_body("")
 
2697
        self.assertTrue(response.is_successful())
 
2698
        self.assertEqual(response.args, ("ok", ))
 
2699
        self.assertEqual("".join(response.body_stream),
 
2700
            "Bazaar pack format 1 (introduced in 0.18)\nB54\n\nBazaar repository format 2a (needs bzr 1.16 or later)\nE")