/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 breezy/tests/test_smart.py

  • Committer: Jelmer Vernooij
  • Date: 2018-05-06 11:48:54 UTC
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@jelmer.uk-20180506114854-h4qd9ojaqy8wxjsd
Move .mailmap to root.

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