1
# Copyright (C) 2006-2012, 2016 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Tests for the smart wire/domain protocol.
19
This module contains tests for the domain-level smart requests and responses,
20
such as the 'Branch.lock_write' request. Many of these use specific disk
21
formats to exercise calls that only make sense for formats with specific
24
Tests for low-level protocol encoding are found in test_smart_transport.
32
branch as _mod_branch,
40
from breezy.bzr import (
41
branch as _mod_bzrbranch,
45
from breezy.bzr.smart import (
46
branch as smart_branch,
48
repository as smart_repo,
49
packrepository as smart_packrepo,
54
from breezy.testament import Testament
55
from breezy.tests import test_server
56
from breezy.transport import (
62
def load_tests(loader, standard_tests, pattern):
63
"""Multiply tests version and protocol consistency."""
64
# FindRepository tests.
67
"_request_class": smart_dir.SmartServerRequestFindRepositoryV1}),
68
("find_repositoryV2", {
69
"_request_class": smart_dir.SmartServerRequestFindRepositoryV2}),
70
("find_repositoryV3", {
71
"_request_class": smart_dir.SmartServerRequestFindRepositoryV3}),
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,
77
tests.multiply_tests(v1_and_2, scenarios, result)
78
# The first scenario is only applicable to v1 protocols, it is deleted
80
tests.multiply_tests(v2_only, scenarios[1:], result)
84
class TestCaseWithChrootedTransport(tests.TestCaseWithTransport):
87
self.vfs_transport_factory = memory.MemoryServer
88
super(TestCaseWithChrootedTransport, self).setUp()
89
self._chroot_server = None
91
def get_transport(self, relpath=None):
92
if self._chroot_server is None:
93
backing_transport = tests.TestCaseWithTransport.get_transport(self)
94
self._chroot_server = chroot.ChrootServer(backing_transport)
95
self.start_server(self._chroot_server)
96
t = transport.get_transport_from_url(self._chroot_server.get_url())
97
if relpath is not None:
102
class TestCaseWithSmartMedium(tests.TestCaseWithMemoryTransport):
105
super(TestCaseWithSmartMedium, self).setUp()
106
# We're allowed to set the transport class here, so that we don't use
107
# the default or a parameterized class, but rather use the
108
# TestCaseWithTransport infrastructure to set up a smart server and
110
self.overrideAttr(self, "transport_server", self.make_transport_server)
112
def make_transport_server(self):
113
return test_server.SmartTCPServer_for_testing('-' + self.id())
115
def get_smart_medium(self):
116
"""Get a smart medium to use in tests."""
117
return self.get_transport().get_smart_medium()
120
class TestByteStreamToStream(tests.TestCase):
122
def test_repeated_substreams_same_kind_are_one_stream(self):
123
# Make a stream - an iterable of bytestrings.
124
stream = [('text', [versionedfile.FulltextContentFactory(('k1',), None,
125
None, 'foo')]), ('text', [
126
versionedfile.FulltextContentFactory(('k2',), None, None, 'bar')])]
127
fmt = controldir.format_registry.get('pack-0.92')().repository_format
128
bytes = smart_repo._stream_to_byte_stream(stream, fmt)
130
# Iterate the resulting iterable; checking that we get only one stream
132
fmt, stream = smart_repo._byte_stream_to_stream(bytes)
133
for kind, substream in stream:
134
streams.append((kind, list(substream)))
135
self.assertLength(1, streams)
136
self.assertLength(2, streams[0][1])
139
class TestSmartServerResponse(tests.TestCase):
141
def test__eq__(self):
142
self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
143
smart_req.SmartServerResponse((b'ok', )))
144
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), 'body'),
145
smart_req.SmartServerResponse((b'ok', ), 'body'))
146
self.assertNotEqual(smart_req.SmartServerResponse((b'ok', )),
147
smart_req.SmartServerResponse((b'notok', )))
148
self.assertNotEqual(smart_req.SmartServerResponse((b'ok', ), 'body'),
149
smart_req.SmartServerResponse((b'ok', )))
150
self.assertNotEqual(None,
151
smart_req.SmartServerResponse((b'ok', )))
153
def test__str__(self):
154
"""SmartServerResponses can be stringified."""
156
str(smart_req.SuccessfulSmartServerResponse((b'args',), b'body')),
157
("<SuccessfulSmartServerResponse args=(b'args',) body=b'body'>",
158
"<SuccessfulSmartServerResponse args=('args',) body='body'>"))
160
str(smart_req.FailedSmartServerResponse((b'args',), b'body')),
161
("<FailedSmartServerResponse args=(b'args',) body=b'body'>",
162
"<FailedSmartServerResponse args=('args',) body='body'>"))
165
class TestSmartServerRequest(tests.TestCaseWithMemoryTransport):
167
def test_translate_client_path(self):
168
transport = self.get_transport()
169
request = smart_req.SmartServerRequest(transport, 'foo/')
170
self.assertEqual('./', request.translate_client_path('foo/'))
172
urlutils.InvalidURLJoin, request.translate_client_path, 'foo/..')
174
errors.PathNotChild, request.translate_client_path, '/')
176
errors.PathNotChild, request.translate_client_path, 'bar/')
177
self.assertEqual('./baz', request.translate_client_path('foo/baz'))
178
e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'.encode('utf-8')
179
self.assertEqual('./' + urlutils.escape(e_acute),
180
request.translate_client_path('foo/' + e_acute))
182
def test_translate_client_path_vfs(self):
183
"""VfsRequests receive escaped paths rather than raw UTF-8."""
184
transport = self.get_transport()
185
request = vfs.VfsRequest(transport, 'foo/')
186
e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'.encode('utf-8')
187
escaped = urlutils.escape('foo/' + e_acute)
188
self.assertEqual('./' + urlutils.escape(e_acute),
189
request.translate_client_path(escaped))
191
def test_transport_from_client_path(self):
192
transport = self.get_transport()
193
request = smart_req.SmartServerRequest(transport, 'foo/')
196
request.transport_from_client_path(b'foo/').base)
199
class TestSmartServerBzrDirRequestCloningMetaDir(
200
tests.TestCaseWithMemoryTransport):
201
"""Tests for BzrDir.cloning_metadir."""
203
def test_cloning_metadir(self):
204
"""When there is a bzrdir present, the call succeeds."""
205
backing = self.get_transport()
206
dir = self.make_controldir('.')
207
local_result = dir.cloning_metadir()
208
request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
209
request = request_class(backing)
210
expected = smart_req.SuccessfulSmartServerResponse(
211
(local_result.network_name(),
212
local_result.repository_format.network_name(),
213
('branch', local_result.get_branch_format().network_name())))
214
self.assertEqual(expected, request.execute('', 'False'))
216
def test_cloning_metadir_reference(self):
217
"""The request fails when bzrdir contains a branch reference."""
218
backing = self.get_transport()
219
referenced_branch = self.make_branch('referenced')
220
dir = self.make_controldir('.')
221
local_result = dir.cloning_metadir()
222
reference = _mod_bzrbranch.BranchReferenceFormat().initialize(
223
dir, target_branch=referenced_branch)
224
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(dir)
225
# The server shouldn't try to follow the branch reference, so it's fine
226
# if the referenced branch isn't reachable.
227
backing.rename('referenced', 'moved')
228
request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
229
request = request_class(backing)
230
expected = smart_req.FailedSmartServerResponse((b'BranchReference',))
231
self.assertEqual(expected, request.execute('', 'False'))
234
class TestSmartServerBzrDirRequestCloningMetaDir(
235
tests.TestCaseWithMemoryTransport):
236
"""Tests for BzrDir.checkout_metadir."""
238
def test_checkout_metadir(self):
239
backing = self.get_transport()
240
request = smart_dir.SmartServerBzrDirRequestCheckoutMetaDir(
242
branch = self.make_branch('.', format='2a')
243
response = request.execute('')
245
smart_req.SmartServerResponse(
246
('Bazaar-NG meta directory, format 1\n',
247
'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
248
'Bazaar Branch Format 7 (needs bzr 1.6)\n')),
252
class TestSmartServerBzrDirRequestDestroyBranch(
253
tests.TestCaseWithMemoryTransport):
254
"""Tests for BzrDir.destroy_branch."""
256
def test_destroy_branch_default(self):
257
"""The default branch can be removed."""
258
backing = self.get_transport()
259
dir = self.make_branch('.').controldir
260
request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
261
request = request_class(backing)
262
expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
263
self.assertEqual(expected, request.execute('', None))
265
def test_destroy_branch_named(self):
266
"""A named branch can be removed."""
267
backing = self.get_transport()
268
dir = self.make_repository('.', format="development-colo").controldir
269
dir.create_branch(name="branchname")
270
request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
271
request = request_class(backing)
272
expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
273
self.assertEqual(expected, request.execute('', "branchname"))
275
def test_destroy_branch_missing(self):
276
"""An error is raised if the branch didn't exist."""
277
backing = self.get_transport()
278
dir = self.make_controldir('.', format="development-colo")
279
request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
280
request = request_class(backing)
281
expected = smart_req.FailedSmartServerResponse((b'nobranch',), None)
282
self.assertEqual(expected, request.execute('', "branchname"))
285
class TestSmartServerBzrDirRequestHasWorkingTree(
286
tests.TestCaseWithTransport):
287
"""Tests for BzrDir.has_workingtree."""
289
def test_has_workingtree_yes(self):
290
"""A working tree is present."""
291
backing = self.get_transport()
292
dir = self.make_branch_and_tree('.').controldir
293
request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
294
request = request_class(backing)
295
expected = smart_req.SuccessfulSmartServerResponse((b'yes',))
296
self.assertEqual(expected, request.execute(b''))
298
def test_has_workingtree_no(self):
299
"""A working tree is missing."""
300
backing = self.get_transport()
301
dir = self.make_controldir('.')
302
request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
303
request = request_class(backing)
304
expected = smart_req.SuccessfulSmartServerResponse((b'no',))
305
self.assertEqual(expected, request.execute(b''))
308
class TestSmartServerBzrDirRequestDestroyRepository(
309
tests.TestCaseWithMemoryTransport):
310
"""Tests for BzrDir.destroy_repository."""
312
def test_destroy_repository_default(self):
313
"""The repository can be removed."""
314
backing = self.get_transport()
315
dir = self.make_repository('.').controldir
316
request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
317
request = request_class(backing)
318
expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
319
self.assertEqual(expected, request.execute(''))
321
def test_destroy_repository_missing(self):
322
"""An error is raised if the repository didn't exist."""
323
backing = self.get_transport()
324
dir = self.make_controldir('.')
325
request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
326
request = request_class(backing)
327
expected = smart_req.FailedSmartServerResponse(
328
('norepository',), None)
329
self.assertEqual(expected, request.execute(''))
332
class TestSmartServerRequestCreateRepository(tests.TestCaseWithMemoryTransport):
333
"""Tests for BzrDir.create_repository."""
335
def test_makes_repository(self):
336
"""When there is a bzrdir present, the call succeeds."""
337
backing = self.get_transport()
338
self.make_controldir('.')
339
request_class = smart_dir.SmartServerRequestCreateRepository
340
request = request_class(backing)
341
reference_bzrdir_format = controldir.format_registry.get('pack-0.92')()
342
reference_format = reference_bzrdir_format.repository_format
343
network_name = reference_format.network_name()
344
expected = smart_req.SuccessfulSmartServerResponse(
345
('ok', 'no', 'no', 'no', network_name))
346
self.assertEqual(expected, request.execute('', network_name, 'True'))
349
class TestSmartServerRequestFindRepository(tests.TestCaseWithMemoryTransport):
350
"""Tests for BzrDir.find_repository."""
352
def test_no_repository(self):
353
"""When there is no repository to be found, ('norepository', ) is returned."""
354
backing = self.get_transport()
355
request = self._request_class(backing)
356
self.make_controldir('.')
357
self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
360
def test_nonshared_repository(self):
361
# nonshared repositorys only allow 'find' to return a handle when the
362
# path the repository is being searched on is the same as that that
363
# the repository is at.
364
backing = self.get_transport()
365
request = self._request_class(backing)
366
result = self._make_repository_and_result()
367
self.assertEqual(result, request.execute(b''))
368
self.make_controldir('subdir')
369
self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
370
request.execute(b'subdir'))
372
def _make_repository_and_result(self, shared=False, format=None):
373
"""Convenience function to setup a repository.
375
:result: The SmartServerResponse to expect when opening it.
377
repo = self.make_repository('.', shared=shared, format=format)
378
if repo.supports_rich_root():
382
if repo._format.supports_tree_reference:
386
if repo._format.supports_external_lookups:
390
if (smart_dir.SmartServerRequestFindRepositoryV3 ==
391
self._request_class):
392
return smart_req.SuccessfulSmartServerResponse(
393
(b'ok', b'', rich_root, subtrees, external,
394
repo._format.network_name()))
395
elif (smart_dir.SmartServerRequestFindRepositoryV2 ==
396
self._request_class):
397
# All tests so far are on formats, and for non-external
399
return smart_req.SuccessfulSmartServerResponse(
400
(b'ok', b'', rich_root, subtrees, external))
402
return smart_req.SuccessfulSmartServerResponse(
403
(b'ok', b'', rich_root, subtrees))
405
def test_shared_repository(self):
406
"""When there is a shared repository, we get 'ok', 'relpath-to-repo'."""
407
backing = self.get_transport()
408
request = self._request_class(backing)
409
result = self._make_repository_and_result(shared=True)
410
self.assertEqual(result, request.execute(b''))
411
self.make_controldir('subdir')
412
result2 = smart_req.SmartServerResponse(
413
result.args[0:1] + ('..', ) + result.args[2:])
414
self.assertEqual(result2,
415
request.execute(b'subdir'))
416
self.make_controldir('subdir/deeper')
417
result3 = smart_req.SmartServerResponse(
418
result.args[0:1] + ('../..', ) + result.args[2:])
419
self.assertEqual(result3,
420
request.execute(b'subdir/deeper'))
422
def test_rich_root_and_subtree_encoding(self):
423
"""Test for the format attributes for rich root and subtree support."""
424
backing = self.get_transport()
425
request = self._request_class(backing)
426
result = self._make_repository_and_result(
427
format='development-subtree')
428
# check the test will be valid
429
self.assertEqual(b'yes', result.args[2])
430
self.assertEqual(b'yes', result.args[3])
431
self.assertEqual(result, request.execute(b''))
433
def test_supports_external_lookups_no_v2(self):
434
"""Test for the supports_external_lookups attribute."""
435
backing = self.get_transport()
436
request = self._request_class(backing)
437
result = self._make_repository_and_result(
438
format='development-subtree')
439
# check the test will be valid
440
self.assertEqual(b'yes', result.args[4])
441
self.assertEqual(result, request.execute(b''))
444
class TestSmartServerBzrDirRequestGetConfigFile(
445
tests.TestCaseWithMemoryTransport):
446
"""Tests for BzrDir.get_config_file."""
448
def test_present(self):
449
backing = self.get_transport()
450
dir = self.make_controldir('.')
451
dir.get_config().set_default_stack_on("/")
452
local_result = dir._get_config()._get_config_file().read()
453
request_class = smart_dir.SmartServerBzrDirRequestConfigFile
454
request = request_class(backing)
455
expected = smart_req.SuccessfulSmartServerResponse((), local_result)
456
self.assertEqual(expected, request.execute(b''))
458
def test_missing(self):
459
backing = self.get_transport()
460
dir = self.make_controldir('.')
461
request_class = smart_dir.SmartServerBzrDirRequestConfigFile
462
request = request_class(backing)
463
expected = smart_req.SuccessfulSmartServerResponse((), '')
464
self.assertEqual(expected, request.execute(b''))
467
class TestSmartServerBzrDirRequestGetBranches(
468
tests.TestCaseWithMemoryTransport):
469
"""Tests for BzrDir.get_branches."""
471
def test_simple(self):
472
backing = self.get_transport()
473
branch = self.make_branch('.')
474
request_class = smart_dir.SmartServerBzrDirRequestGetBranches
475
request = request_class(backing)
476
local_result = bencode.bencode(
477
{"": ("branch", branch._format.network_name())})
478
expected = smart_req.SuccessfulSmartServerResponse(
479
(b"success", ), local_result)
480
self.assertEqual(expected, request.execute(b''))
482
def test_empty(self):
483
backing = self.get_transport()
484
dir = self.make_controldir('.')
485
request_class = smart_dir.SmartServerBzrDirRequestGetBranches
486
request = request_class(backing)
487
local_result = bencode.bencode({})
488
expected = smart_req.SuccessfulSmartServerResponse(
489
(b'success',), local_result)
490
self.assertEqual(expected, request.execute(b''))
493
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithMemoryTransport):
495
def test_empty_dir(self):
496
"""Initializing an empty dir should succeed and do it."""
497
backing = self.get_transport()
498
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
499
self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
500
request.execute(b''))
501
made_dir = controldir.ControlDir.open_from_transport(backing)
502
# no branch, tree or repository is expected with the current
504
self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
505
self.assertRaises(errors.NotBranchError, made_dir.open_branch)
506
self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
508
def test_missing_dir(self):
509
"""Initializing a missing directory should fail like the bzrdir api."""
510
backing = self.get_transport()
511
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
512
self.assertRaises(errors.NoSuchFile,
513
request.execute, b'subdir')
515
def test_initialized_dir(self):
516
"""Initializing an extant bzrdir should fail like the bzrdir api."""
517
backing = self.get_transport()
518
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
519
self.make_controldir('subdir')
520
self.assertRaises(errors.AlreadyControlDirError,
521
request.execute, b'subdir')
524
class TestSmartServerRequestBzrDirInitializeEx(
525
tests.TestCaseWithMemoryTransport):
526
"""Basic tests for BzrDir.initialize_ex_1.16 in the smart server.
528
The main unit tests in test_bzrdir exercise the API comprehensively.
531
def test_empty_dir(self):
532
"""Initializing an empty dir should succeed and do it."""
533
backing = self.get_transport()
534
name = self.make_controldir('reference')._format.network_name()
535
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
537
smart_req.SmartServerResponse((b'', '', '', '', '', '', name,
538
'False', '', '', '')),
539
request.execute(name, b'', b'True', b'False', b'False', b'', b'', b'', b'',
541
made_dir = controldir.ControlDir.open_from_transport(backing)
542
# no branch, tree or repository is expected with the current
544
self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
545
self.assertRaises(errors.NotBranchError, made_dir.open_branch)
546
self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
548
def test_missing_dir(self):
549
"""Initializing a missing directory should fail like the bzrdir api."""
550
backing = self.get_transport()
551
name = self.make_controldir('reference')._format.network_name()
552
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
553
self.assertRaises(errors.NoSuchFile, request.execute, name,
554
b'subdir/dir', b'False', b'False', b'False', b'', b'', b'', b'', b'False')
556
def test_initialized_dir(self):
557
"""Initializing an extant directory should fail like the bzrdir api."""
558
backing = self.get_transport()
559
name = self.make_controldir('reference')._format.network_name()
560
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
561
self.make_controldir('subdir')
562
self.assertRaises(errors.FileExists, request.execute, name, b'subdir',
563
b'False', b'False', b'False', b'', b'', b'', b'', b'False')
566
class TestSmartServerRequestOpenBzrDir(tests.TestCaseWithMemoryTransport):
568
def test_no_directory(self):
569
backing = self.get_transport()
570
request = smart_dir.SmartServerRequestOpenBzrDir(backing)
571
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
572
request.execute(b'does-not-exist'))
574
def test_empty_directory(self):
575
backing = self.get_transport()
576
backing.mkdir('empty')
577
request = smart_dir.SmartServerRequestOpenBzrDir(backing)
578
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
579
request.execute(b'empty'))
581
def test_outside_root_client_path(self):
582
backing = self.get_transport()
583
request = smart_dir.SmartServerRequestOpenBzrDir(backing,
584
root_client_path='root')
585
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
586
request.execute(b'not-root'))
589
class TestSmartServerRequestOpenBzrDir_2_1(tests.TestCaseWithMemoryTransport):
591
def test_no_directory(self):
592
backing = self.get_transport()
593
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
594
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
595
request.execute(b'does-not-exist'))
597
def test_empty_directory(self):
598
backing = self.get_transport()
599
backing.mkdir('empty')
600
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
601
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
602
request.execute(b'empty'))
604
def test_present_without_workingtree(self):
605
backing = self.get_transport()
606
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
607
self.make_controldir('.')
608
self.assertEqual(smart_req.SmartServerResponse((b'yes', b'no')),
609
request.execute(b''))
611
def test_outside_root_client_path(self):
612
backing = self.get_transport()
613
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing,
614
root_client_path='root')
615
self.assertEqual(smart_req.SmartServerResponse((b'no',)),
616
request.execute(b'not-root'))
619
class TestSmartServerRequestOpenBzrDir_2_1_disk(TestCaseWithChrootedTransport):
621
def test_present_with_workingtree(self):
622
self.vfs_transport_factory = test_server.LocalURLServer
623
backing = self.get_transport()
624
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
625
bd = self.make_controldir('.')
626
bd.create_repository()
628
bd.create_workingtree()
629
self.assertEqual(smart_req.SmartServerResponse((b'yes', 'yes')),
630
request.execute(b''))
633
class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
635
def test_no_branch(self):
636
"""When there is no branch, ('nobranch', ) is returned."""
637
backing = self.get_transport()
638
request = smart_dir.SmartServerRequestOpenBranch(backing)
639
self.make_controldir('.')
640
self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
641
request.execute(b''))
643
def test_branch(self):
644
"""When there is a branch, 'ok' is returned."""
645
backing = self.get_transport()
646
request = smart_dir.SmartServerRequestOpenBranch(backing)
647
self.make_branch('.')
648
self.assertEqual(smart_req.SmartServerResponse((b'ok', '')),
649
request.execute(b''))
651
def test_branch_reference(self):
652
"""When there is a branch reference, the reference URL is returned."""
653
self.vfs_transport_factory = test_server.LocalURLServer
654
backing = self.get_transport()
655
request = smart_dir.SmartServerRequestOpenBranch(backing)
656
branch = self.make_branch('branch')
657
checkout = branch.create_checkout('reference', lightweight=True)
658
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
660
self.assertFileEqual(reference_url, b'reference/.bzr/branch/location')
661
self.assertEqual(smart_req.SmartServerResponse((b'ok', reference_url)),
662
request.execute(b'reference'))
664
def test_notification_on_branch_from_repository(self):
665
"""When there is a repository, the error should return details."""
666
backing = self.get_transport()
667
request = smart_dir.SmartServerRequestOpenBranch(backing)
668
repo = self.make_repository('.')
669
self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
670
request.execute(b''))
673
class TestSmartServerRequestOpenBranchV2(TestCaseWithChrootedTransport):
675
def test_no_branch(self):
676
"""When there is no branch, ('nobranch', ) is returned."""
677
backing = self.get_transport()
678
self.make_controldir('.')
679
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
680
self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
681
request.execute(b''))
683
def test_branch(self):
684
"""When there is a branch, 'ok' is returned."""
685
backing = self.get_transport()
686
expected = self.make_branch('.')._format.network_name()
687
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
688
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
689
(b'branch', expected)),
690
request.execute(b''))
692
def test_branch_reference(self):
693
"""When there is a branch reference, the reference URL is returned."""
694
self.vfs_transport_factory = test_server.LocalURLServer
695
backing = self.get_transport()
696
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
697
branch = self.make_branch('branch')
698
checkout = branch.create_checkout('reference', lightweight=True)
699
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
701
self.assertFileEqual(reference_url, b'reference/.bzr/branch/location')
702
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
703
(b'ref', reference_url)),
704
request.execute(b'reference'))
706
def test_stacked_branch(self):
707
"""Opening a stacked branch does not open the stacked-on branch."""
708
trunk = self.make_branch('trunk')
709
feature = self.make_branch('feature')
710
feature.set_stacked_on_url(trunk.base)
712
_mod_branch.Branch.hooks.install_named_hook(
713
'open', opened_branches.append, None)
714
backing = self.get_transport()
715
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
718
response = request.execute(b'feature')
720
request.teardown_jail()
721
expected_format = feature._format.network_name()
722
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
723
(b'branch', expected_format)),
725
self.assertLength(1, opened_branches)
727
def test_notification_on_branch_from_repository(self):
728
"""When there is a repository, the error should return details."""
729
backing = self.get_transport()
730
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
731
repo = self.make_repository('.')
732
self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
733
request.execute(b''))
736
class TestSmartServerRequestOpenBranchV3(TestCaseWithChrootedTransport):
738
def test_no_branch(self):
739
"""When there is no branch, ('nobranch', ) is returned."""
740
backing = self.get_transport()
741
self.make_controldir('.')
742
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
743
self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
744
request.execute(b''))
746
def test_branch(self):
747
"""When there is a branch, 'ok' is returned."""
748
backing = self.get_transport()
749
expected = self.make_branch('.')._format.network_name()
750
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
751
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
752
(b'branch', expected)),
753
request.execute(b''))
755
def test_branch_reference(self):
756
"""When there is a branch reference, the reference URL is returned."""
757
self.vfs_transport_factory = test_server.LocalURLServer
758
backing = self.get_transport()
759
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
760
branch = self.make_branch('branch')
761
checkout = branch.create_checkout('reference', lightweight=True)
762
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
764
self.assertFileEqual(reference_url, b'reference/.bzr/branch/location')
765
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
766
(b'ref', reference_url)),
767
request.execute(b'reference'))
769
def test_stacked_branch(self):
770
"""Opening a stacked branch does not open the stacked-on branch."""
771
trunk = self.make_branch('trunk')
772
feature = self.make_branch('feature')
773
feature.set_stacked_on_url(trunk.base)
775
_mod_branch.Branch.hooks.install_named_hook(
776
'open', opened_branches.append, None)
777
backing = self.get_transport()
778
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
781
response = request.execute(b'feature')
783
request.teardown_jail()
784
expected_format = feature._format.network_name()
785
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
786
('branch', expected_format)),
788
self.assertLength(1, opened_branches)
790
def test_notification_on_branch_from_repository(self):
791
"""When there is a repository, the error should return details."""
792
backing = self.get_transport()
793
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
794
repo = self.make_repository('.')
795
self.assertEqual(smart_req.SmartServerResponse(
796
(b'nobranch', b'location is a repository')),
797
request.execute(b''))
800
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
802
def test_empty(self):
803
"""For an empty branch, the body is empty."""
804
backing = self.get_transport()
805
request = smart_branch.SmartServerRequestRevisionHistory(backing)
806
self.make_branch('.')
807
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b''),
808
request.execute(b''))
810
def test_not_empty(self):
811
"""For a non-empty branch, the body is empty."""
812
backing = self.get_transport()
813
request = smart_branch.SmartServerRequestRevisionHistory(backing)
814
tree = self.make_branch_and_memory_tree('.')
817
r1 = tree.commit('1st commit')
818
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
821
smart_req.SmartServerResponse((b'ok', ), (b'\x00'.join([r1, r2]))),
822
request.execute(b''))
825
class TestSmartServerBranchRequest(tests.TestCaseWithMemoryTransport):
827
def test_no_branch(self):
828
"""When there is a bzrdir and no branch, NotBranchError is raised."""
829
backing = self.get_transport()
830
request = smart_branch.SmartServerBranchRequest(backing)
831
self.make_controldir('.')
832
self.assertRaises(errors.NotBranchError,
833
request.execute, b'')
835
def test_branch_reference(self):
836
"""When there is a branch reference, NotBranchError is raised."""
837
backing = self.get_transport()
838
request = smart_branch.SmartServerBranchRequest(backing)
839
branch = self.make_branch('branch')
840
checkout = branch.create_checkout('reference', lightweight=True)
841
self.assertRaises(errors.NotBranchError,
842
request.execute, 'checkout')
845
class TestSmartServerBranchRequestLastRevisionInfo(
846
tests.TestCaseWithMemoryTransport):
848
def test_empty(self):
849
"""For an empty branch, the result is ('ok', '0', 'null:')."""
850
backing = self.get_transport()
851
request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
852
self.make_branch('.')
853
self.assertEqual(smart_req.SmartServerResponse((b'ok', b'0', b'null:')),
854
request.execute(b''))
856
def test_not_empty(self):
857
"""For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
858
backing = self.get_transport()
859
request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
860
tree = self.make_branch_and_memory_tree('.')
863
rev_id_utf8 = u'\xc8'.encode('utf-8')
864
r1 = tree.commit('1st commit')
865
r2 = tree.commit('2nd commit', rev_id=rev_id_utf8)
868
smart_req.SmartServerResponse((b'ok', b'2', rev_id_utf8)),
869
request.execute(b''))
872
class TestSmartServerBranchRequestRevisionIdToRevno(
873
tests.TestCaseWithMemoryTransport):
876
backing = self.get_transport()
877
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
879
self.make_branch('.')
880
self.assertEqual(smart_req.SmartServerResponse((b'ok', b'0')),
881
request.execute(b'', b'null:'))
883
def test_simple(self):
884
backing = self.get_transport()
885
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
887
tree = self.make_branch_and_memory_tree('.')
890
r1 = tree.commit('1st commit')
893
smart_req.SmartServerResponse((b'ok', '1')),
894
request.execute(b'', r1))
896
def test_not_found(self):
897
backing = self.get_transport()
898
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
900
branch = self.make_branch('.')
902
smart_req.FailedSmartServerResponse(
903
(b'NoSuchRevision', b'idontexist')),
904
request.execute(b'', b'idontexist'))
907
class TestSmartServerBranchRequestGetConfigFile(
908
tests.TestCaseWithMemoryTransport):
910
def test_default(self):
911
"""With no file, we get empty content."""
912
backing = self.get_transport()
913
request = smart_branch.SmartServerBranchGetConfigFile(backing)
914
branch = self.make_branch('.')
915
# there should be no file by default
917
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), content),
918
request.execute(b''))
920
def test_with_content(self):
921
# SmartServerBranchGetConfigFile should return the content from
922
# branch.control_files.get('branch.conf') for now - in the future it may
923
# perform more complex processing.
924
backing = self.get_transport()
925
request = smart_branch.SmartServerBranchGetConfigFile(backing)
926
branch = self.make_branch('.')
927
branch._transport.put_bytes('branch.conf', b'foo bar baz')
928
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'foo bar baz'),
929
request.execute(b''))
932
class TestLockedBranch(tests.TestCaseWithMemoryTransport):
934
def get_lock_tokens(self, branch):
935
branch_token = branch.lock_write().token
936
repo_token = branch.repository.lock_write().repository_token
937
branch.repository.unlock()
938
return branch_token, repo_token
941
class TestSmartServerBranchRequestPutConfigFile(TestLockedBranch):
943
def test_with_content(self):
944
backing = self.get_transport()
945
request = smart_branch.SmartServerBranchPutConfigFile(backing)
946
branch = self.make_branch('.')
947
branch_token, repo_token = self.get_lock_tokens(branch)
948
self.assertIs(None, request.execute(b'', branch_token, repo_token))
950
smart_req.SmartServerResponse((b'ok', )),
951
request.do_body(b'foo bar baz'))
953
branch.control_transport.get_bytes(b'branch.conf'),
958
class TestSmartServerBranchRequestSetConfigOption(TestLockedBranch):
960
def test_value_name(self):
961
branch = self.make_branch('.')
962
request = smart_branch.SmartServerBranchRequestSetConfigOption(
963
branch.controldir.root_transport)
964
branch_token, repo_token = self.get_lock_tokens(branch)
965
config = branch._get_config()
966
result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
968
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
969
self.assertEqual('bar', config.get_option('foo'))
973
def test_value_name_section(self):
974
branch = self.make_branch('.')
975
request = smart_branch.SmartServerBranchRequestSetConfigOption(
976
branch.controldir.root_transport)
977
branch_token, repo_token = self.get_lock_tokens(branch)
978
config = branch._get_config()
979
result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
981
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
982
self.assertEqual('bar', config.get_option('foo', 'gam'))
987
class TestSmartServerBranchRequestSetConfigOptionDict(TestLockedBranch):
990
TestLockedBranch.setUp(self)
991
# A dict with non-ascii keys and values to exercise unicode
993
self.encoded_value_dict = (
994
'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde')
996
'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
998
def test_value_name(self):
999
branch = self.make_branch('.')
1000
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
1001
branch.controldir.root_transport)
1002
branch_token, repo_token = self.get_lock_tokens(branch)
1003
config = branch._get_config()
1004
result = request.execute(b'', branch_token, repo_token,
1005
self.encoded_value_dict, b'foo', b'')
1006
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1007
self.assertEqual(self.value_dict, config.get_option('foo'))
1011
def test_value_name_section(self):
1012
branch = self.make_branch('.')
1013
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
1014
branch.controldir.root_transport)
1015
branch_token, repo_token = self.get_lock_tokens(branch)
1016
config = branch._get_config()
1017
result = request.execute(b'', branch_token, repo_token,
1018
self.encoded_value_dict, b'foo', b'gam')
1019
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1020
self.assertEqual(self.value_dict, config.get_option('foo', 'gam'))
1025
class TestSmartServerBranchRequestSetTagsBytes(TestLockedBranch):
1026
# Only called when the branch format and tags match [yay factory
1027
# methods] so only need to test straight forward cases.
1029
def test_set_bytes(self):
1030
base_branch = self.make_branch('base')
1031
tag_bytes = base_branch._get_tags_bytes()
1032
# get_lock_tokens takes out a lock.
1033
branch_token, repo_token = self.get_lock_tokens(base_branch)
1034
request = smart_branch.SmartServerBranchSetTagsBytes(
1035
self.get_transport())
1036
response = request.execute(b'base', branch_token, repo_token)
1037
self.assertEqual(None, response)
1038
response = request.do_chunk(tag_bytes)
1039
self.assertEqual(None, response)
1040
response = request.do_end()
1042
smart_req.SuccessfulSmartServerResponse(()), response)
1043
base_branch.unlock()
1045
def test_lock_failed(self):
1046
base_branch = self.make_branch('base')
1047
base_branch.lock_write()
1048
tag_bytes = base_branch._get_tags_bytes()
1049
request = smart_branch.SmartServerBranchSetTagsBytes(
1050
self.get_transport())
1051
self.assertRaises(errors.TokenMismatch, request.execute,
1052
'base', 'wrong token', 'wrong token')
1053
# The request handler will keep processing the message parts, so even
1054
# if the request fails immediately do_chunk and do_end are still
1056
request.do_chunk(tag_bytes)
1058
base_branch.unlock()
1062
class SetLastRevisionTestBase(TestLockedBranch):
1063
"""Base test case for verbs that implement set_last_revision."""
1066
super(SetLastRevisionTestBase, self).setUp()
1067
backing_transport = self.get_transport()
1068
self.request = self.request_class(backing_transport)
1069
self.tree = self.make_branch_and_memory_tree('.')
1071
def lock_branch(self):
1072
return self.get_lock_tokens(self.tree.branch)
1074
def unlock_branch(self):
1075
self.tree.branch.unlock()
1077
def set_last_revision(self, revision_id, revno):
1078
branch_token, repo_token = self.lock_branch()
1079
response = self._set_last_revision(
1080
revision_id, revno, branch_token, repo_token)
1081
self.unlock_branch()
1084
def assertRequestSucceeds(self, revision_id, revno):
1085
response = self.set_last_revision(revision_id, revno)
1086
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
1090
class TestSetLastRevisionVerbMixin(object):
1091
"""Mixin test case for verbs that implement set_last_revision."""
1093
def test_set_null_to_null(self):
1094
"""An empty branch can have its last revision set to 'null:'."""
1095
self.assertRequestSucceeds('null:', 0)
1097
def test_NoSuchRevision(self):
1098
"""If the revision_id is not present, the verb returns NoSuchRevision.
1100
revision_id = 'non-existent revision'
1101
self.assertEqual(smart_req.FailedSmartServerResponse((b'NoSuchRevision',
1103
self.set_last_revision(revision_id, 1))
1105
def make_tree_with_two_commits(self):
1106
self.tree.lock_write()
1108
rev_id_utf8 = u'\xc8'.encode('utf-8')
1109
r1 = self.tree.commit('1st commit', rev_id=rev_id_utf8)
1110
r2 = self.tree.commit('2nd commit', rev_id=b'rev-2')
1113
def test_branch_last_revision_info_is_updated(self):
1114
"""A branch's tip can be set to a revision that is present in its
1117
# Make a branch with an empty revision history, but two revisions in
1119
self.make_tree_with_two_commits()
1120
rev_id_utf8 = u'\xc8'.encode('utf-8')
1121
self.tree.branch.set_last_revision_info(0, 'null:')
1123
(0, 'null:'), self.tree.branch.last_revision_info())
1124
# We can update the branch to a revision that is present in the
1126
self.assertRequestSucceeds(rev_id_utf8, 1)
1128
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1130
def test_branch_last_revision_info_rewind(self):
1131
"""A branch's tip can be set to a revision that is an ancestor of the
1134
self.make_tree_with_two_commits()
1135
rev_id_utf8 = u'\xc8'.encode('utf-8')
1137
(2, 'rev-2'), self.tree.branch.last_revision_info())
1138
self.assertRequestSucceeds(rev_id_utf8, 1)
1140
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1142
def test_TipChangeRejected(self):
1143
"""If a pre_change_branch_tip hook raises TipChangeRejected, the verb
1144
returns TipChangeRejected.
1146
rejection_message = u'rejection message\N{INTERROBANG}'
1147
def hook_that_rejects(params):
1148
raise errors.TipChangeRejected(rejection_message)
1149
_mod_branch.Branch.hooks.install_named_hook(
1150
'pre_change_branch_tip', hook_that_rejects, None)
1152
smart_req.FailedSmartServerResponse(
1153
('TipChangeRejected', rejection_message.encode('utf-8'))),
1154
self.set_last_revision('null:', 0))
1157
class TestSmartServerBranchRequestSetLastRevision(
1158
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1159
"""Tests for Branch.set_last_revision verb."""
1161
request_class = smart_branch.SmartServerBranchRequestSetLastRevision
1163
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1164
return self.request.execute(
1165
'', branch_token, repo_token, revision_id)
1168
class TestSmartServerBranchRequestSetLastRevisionInfo(
1169
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1170
"""Tests for Branch.set_last_revision_info verb."""
1172
request_class = smart_branch.SmartServerBranchRequestSetLastRevisionInfo
1174
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1175
return self.request.execute(
1176
'', branch_token, repo_token, revno, revision_id)
1178
def test_NoSuchRevision(self):
1179
"""Branch.set_last_revision_info does not have to return
1180
NoSuchRevision if the revision_id is absent.
1182
raise tests.TestNotApplicable()
1185
class TestSmartServerBranchRequestSetLastRevisionEx(
1186
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1187
"""Tests for Branch.set_last_revision_ex verb."""
1189
request_class = smart_branch.SmartServerBranchRequestSetLastRevisionEx
1191
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1192
return self.request.execute(
1193
'', branch_token, repo_token, revision_id, 0, 0)
1195
def assertRequestSucceeds(self, revision_id, revno):
1196
response = self.set_last_revision(revision_id, revno)
1198
smart_req.SuccessfulSmartServerResponse((b'ok', revno, revision_id)),
1201
def test_branch_last_revision_info_rewind(self):
1202
"""A branch's tip can be set to a revision that is an ancestor of the
1203
current tip, but only if allow_overwrite_descendant is passed.
1205
self.make_tree_with_two_commits()
1206
rev_id_utf8 = u'\xc8'.encode('utf-8')
1208
(2, 'rev-2'), self.tree.branch.last_revision_info())
1209
# If allow_overwrite_descendant flag is 0, then trying to set the tip
1210
# to an older revision ID has no effect.
1211
branch_token, repo_token = self.lock_branch()
1212
response = self.request.execute(
1213
'', branch_token, repo_token, rev_id_utf8, 0, 0)
1215
smart_req.SuccessfulSmartServerResponse((b'ok', 2, 'rev-2')),
1218
(2, 'rev-2'), self.tree.branch.last_revision_info())
1220
# If allow_overwrite_descendant flag is 1, then setting the tip to an
1222
response = self.request.execute(
1223
'', branch_token, repo_token, rev_id_utf8, 0, 1)
1225
smart_req.SuccessfulSmartServerResponse((b'ok', 1, rev_id_utf8)),
1227
self.unlock_branch()
1229
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1231
def make_branch_with_divergent_history(self):
1232
"""Make a branch with divergent history in its repo.
1234
The branch's tip will be 'child-2', and the repo will also contain
1235
'child-1', which diverges from a common base revision.
1237
self.tree.lock_write()
1239
r1 = self.tree.commit('1st commit')
1240
revno_1, revid_1 = self.tree.branch.last_revision_info()
1241
r2 = self.tree.commit('2nd commit', rev_id=b'child-1')
1242
# Undo the second commit
1243
self.tree.branch.set_last_revision_info(revno_1, revid_1)
1244
self.tree.set_parent_ids([revid_1])
1245
# Make a new second commit, child-2. child-2 has diverged from
1247
new_r2 = self.tree.commit('2nd commit', rev_id=b'child-2')
1250
def test_not_allow_diverged(self):
1251
"""If allow_diverged is not passed, then setting a divergent history
1252
returns a Diverged error.
1254
self.make_branch_with_divergent_history()
1256
smart_req.FailedSmartServerResponse((b'Diverged',)),
1257
self.set_last_revision('child-1', 2))
1258
# The branch tip was not changed.
1259
self.assertEqual('child-2', self.tree.branch.last_revision())
1261
def test_allow_diverged(self):
1262
"""If allow_diverged is passed, then setting a divergent history
1265
self.make_branch_with_divergent_history()
1266
branch_token, repo_token = self.lock_branch()
1267
response = self.request.execute(
1268
'', branch_token, repo_token, 'child-1', 1, 0)
1270
smart_req.SuccessfulSmartServerResponse((b'ok', 2, 'child-1')),
1272
self.unlock_branch()
1273
# The branch tip was changed.
1274
self.assertEqual('child-1', self.tree.branch.last_revision())
1277
class TestSmartServerBranchBreakLock(tests.TestCaseWithMemoryTransport):
1279
def test_lock_to_break(self):
1280
base_branch = self.make_branch('base')
1281
request = smart_branch.SmartServerBranchBreakLock(
1282
self.get_transport())
1283
base_branch.lock_write()
1285
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1286
request.execute(b'base'))
1288
def test_nothing_to_break(self):
1289
base_branch = self.make_branch('base')
1290
request = smart_branch.SmartServerBranchBreakLock(
1291
self.get_transport())
1293
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1294
request.execute(b'base'))
1297
class TestSmartServerBranchRequestGetParent(tests.TestCaseWithMemoryTransport):
1299
def test_get_parent_none(self):
1300
base_branch = self.make_branch('base')
1301
request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1302
response = request.execute(b'base')
1304
smart_req.SuccessfulSmartServerResponse((b'',)), response)
1306
def test_get_parent_something(self):
1307
base_branch = self.make_branch('base')
1308
base_branch.set_parent(self.get_url('foo'))
1309
request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1310
response = request.execute(b'base')
1312
smart_req.SuccessfulSmartServerResponse((b"../foo",)),
1316
class TestSmartServerBranchRequestSetParent(TestLockedBranch):
1318
def test_set_parent_none(self):
1319
branch = self.make_branch('base', format="1.9")
1321
branch._set_parent_location('foo')
1323
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1324
self.get_transport())
1325
branch_token, repo_token = self.get_lock_tokens(branch)
1327
response = request.execute(b'base', branch_token, repo_token, b'')
1330
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1331
# Refresh branch as SetParentLocation modified it
1332
branch = branch.controldir.open_branch()
1333
self.assertEqual(None, branch.get_parent())
1335
def test_set_parent_something(self):
1336
branch = self.make_branch('base', format="1.9")
1337
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1338
self.get_transport())
1339
branch_token, repo_token = self.get_lock_tokens(branch)
1341
response = request.execute(b'base', branch_token, repo_token,
1345
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1346
refreshed = _mod_branch.Branch.open(branch.base)
1347
self.assertEqual('http://bar/', refreshed.get_parent())
1350
class TestSmartServerBranchRequestGetTagsBytes(
1351
tests.TestCaseWithMemoryTransport):
1352
# Only called when the branch format and tags match [yay factory
1353
# methods] so only need to test straight forward cases.
1355
def test_get_bytes(self):
1356
base_branch = self.make_branch('base')
1357
request = smart_branch.SmartServerBranchGetTagsBytes(
1358
self.get_transport())
1359
response = request.execute(b'base')
1361
smart_req.SuccessfulSmartServerResponse((b'',)), response)
1364
class TestSmartServerBranchRequestGetStackedOnURL(tests.TestCaseWithMemoryTransport):
1366
def test_get_stacked_on_url(self):
1367
base_branch = self.make_branch('base', format='1.6')
1368
stacked_branch = self.make_branch('stacked', format='1.6')
1369
# typically should be relative
1370
stacked_branch.set_stacked_on_url('../base')
1371
request = smart_branch.SmartServerBranchRequestGetStackedOnURL(
1372
self.get_transport())
1373
response = request.execute(b'stacked')
1375
smart_req.SmartServerResponse((b'ok', b'../base')),
1379
class TestSmartServerBranchRequestLockWrite(TestLockedBranch):
1381
def test_lock_write_on_unlocked_branch(self):
1382
backing = self.get_transport()
1383
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1384
branch = self.make_branch('.', format='knit')
1385
repository = branch.repository
1386
response = request.execute(b'')
1387
branch_nonce = branch.control_files._lock.peek().get('nonce')
1388
repository_nonce = repository.control_files._lock.peek().get('nonce')
1389
self.assertEqual(smart_req.SmartServerResponse(
1390
(b'ok', branch_nonce, repository_nonce)),
1392
# The branch (and associated repository) is now locked. Verify that
1393
# with a new branch object.
1394
new_branch = repository.controldir.open_branch()
1395
self.assertRaises(errors.LockContention, new_branch.lock_write)
1397
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1398
response = request.execute(b'', branch_nonce, repository_nonce)
1400
def test_lock_write_on_locked_branch(self):
1401
backing = self.get_transport()
1402
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1403
branch = self.make_branch('.')
1404
branch_token = branch.lock_write().token
1405
branch.leave_lock_in_place()
1407
response = request.execute(b'')
1409
smart_req.SmartServerResponse((b'LockContention',)), response)
1411
branch.lock_write(branch_token)
1412
branch.dont_leave_lock_in_place()
1415
def test_lock_write_with_tokens_on_locked_branch(self):
1416
backing = self.get_transport()
1417
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1418
branch = self.make_branch('.', format='knit')
1419
branch_token, repo_token = self.get_lock_tokens(branch)
1420
branch.leave_lock_in_place()
1421
branch.repository.leave_lock_in_place()
1423
response = request.execute(b'',
1424
branch_token, repo_token)
1426
smart_req.SmartServerResponse((b'ok', branch_token, repo_token)),
1429
branch.repository.lock_write(repo_token)
1430
branch.repository.dont_leave_lock_in_place()
1431
branch.repository.unlock()
1432
branch.lock_write(branch_token)
1433
branch.dont_leave_lock_in_place()
1436
def test_lock_write_with_mismatched_tokens_on_locked_branch(self):
1437
backing = self.get_transport()
1438
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1439
branch = self.make_branch('.', format='knit')
1440
branch_token, repo_token = self.get_lock_tokens(branch)
1441
branch.leave_lock_in_place()
1442
branch.repository.leave_lock_in_place()
1444
response = request.execute(b'',
1445
branch_token+b'xxx', repo_token)
1447
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1449
branch.repository.lock_write(repo_token)
1450
branch.repository.dont_leave_lock_in_place()
1451
branch.repository.unlock()
1452
branch.lock_write(branch_token)
1453
branch.dont_leave_lock_in_place()
1456
def test_lock_write_on_locked_repo(self):
1457
backing = self.get_transport()
1458
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1459
branch = self.make_branch('.', format='knit')
1460
repo = branch.repository
1461
repo_token = repo.lock_write().repository_token
1462
repo.leave_lock_in_place()
1464
response = request.execute(b'')
1466
smart_req.SmartServerResponse((b'LockContention',)), response)
1468
repo.lock_write(repo_token)
1469
repo.dont_leave_lock_in_place()
1472
def test_lock_write_on_readonly_transport(self):
1473
backing = self.get_readonly_transport()
1474
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1475
branch = self.make_branch('.')
1476
root = self.get_transport().clone('/')
1477
path = urlutils.relative_url(root.base, self.get_transport().base)
1478
response = request.execute(path)
1479
error_name, lock_str, why_str = response.args
1480
self.assertFalse(response.is_successful())
1481
self.assertEqual('LockFailed', error_name)
1484
class TestSmartServerBranchRequestGetPhysicalLockStatus(TestLockedBranch):
1486
def test_true(self):
1487
backing = self.get_transport()
1488
request = smart_branch.SmartServerBranchRequestGetPhysicalLockStatus(
1490
branch = self.make_branch('.')
1491
branch_token, repo_token = self.get_lock_tokens(branch)
1492
self.assertEqual(True, branch.get_physical_lock_status())
1493
response = request.execute(b'')
1495
smart_req.SmartServerResponse((b'yes',)), response)
1498
def test_false(self):
1499
backing = self.get_transport()
1500
request = smart_branch.SmartServerBranchRequestGetPhysicalLockStatus(
1502
branch = self.make_branch('.')
1503
self.assertEqual(False, branch.get_physical_lock_status())
1504
response = request.execute(b'')
1506
smart_req.SmartServerResponse((b'no',)), response)
1509
class TestSmartServerBranchRequestUnlock(TestLockedBranch):
1511
def test_unlock_on_locked_branch_and_repo(self):
1512
backing = self.get_transport()
1513
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1514
branch = self.make_branch('.', format='knit')
1516
branch_token, repo_token = self.get_lock_tokens(branch)
1517
# Unlock the branch (and repo) object, leaving the physical locks
1519
branch.leave_lock_in_place()
1520
branch.repository.leave_lock_in_place()
1522
response = request.execute(b'',
1523
branch_token, repo_token)
1525
smart_req.SmartServerResponse((b'ok',)), response)
1526
# The branch is now unlocked. Verify that with a new branch
1528
new_branch = branch.controldir.open_branch()
1529
new_branch.lock_write()
1532
def test_unlock_on_unlocked_branch_unlocked_repo(self):
1533
backing = self.get_transport()
1534
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1535
branch = self.make_branch('.', format='knit')
1536
response = request.execute(
1537
'', 'branch token', 'repo token')
1539
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1541
def test_unlock_on_unlocked_branch_locked_repo(self):
1542
backing = self.get_transport()
1543
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1544
branch = self.make_branch('.', format='knit')
1545
# Lock the repository.
1546
repo_token = branch.repository.lock_write().repository_token
1547
branch.repository.leave_lock_in_place()
1548
branch.repository.unlock()
1549
# Issue branch lock_write request on the unlocked branch (with locked
1551
response = request.execute(b'', 'branch token', repo_token)
1553
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1555
branch.repository.lock_write(repo_token)
1556
branch.repository.dont_leave_lock_in_place()
1557
branch.repository.unlock()
1560
class TestSmartServerRepositoryRequest(tests.TestCaseWithMemoryTransport):
1562
def test_no_repository(self):
1563
"""Raise NoRepositoryPresent when there is a bzrdir and no repo."""
1564
# we test this using a shared repository above the named path,
1565
# thus checking the right search logic is used - that is, that
1566
# its the exact path being looked at and the server is not
1568
backing = self.get_transport()
1569
request = smart_repo.SmartServerRepositoryRequest(backing)
1570
self.make_repository('.', shared=True)
1571
self.make_controldir('subdir')
1572
self.assertRaises(errors.NoRepositoryPresent,
1573
request.execute, 'subdir')
1576
class TestSmartServerRepositoryAddSignatureText(tests.TestCaseWithMemoryTransport):
1578
def test_add_text(self):
1579
backing = self.get_transport()
1580
request = smart_repo.SmartServerRepositoryAddSignatureText(backing)
1581
tree = self.make_branch_and_memory_tree('.')
1582
write_token = tree.lock_write()
1583
self.addCleanup(tree.unlock)
1585
tree.commit("Message", rev_id=b'rev1')
1586
tree.branch.repository.start_write_group()
1587
write_group_tokens = tree.branch.repository.suspend_write_group()
1588
self.assertEqual(None, request.execute(b'', write_token,
1589
b'rev1', *write_group_tokens))
1590
response = request.do_body('somesignature')
1591
self.assertTrue(response.is_successful())
1592
self.assertEqual(response.args[0], 'ok')
1593
write_group_tokens = response.args[1:]
1594
tree.branch.repository.resume_write_group(write_group_tokens)
1595
tree.branch.repository.commit_write_group()
1597
self.assertEqual("somesignature",
1598
tree.branch.repository.get_signature_text("rev1"))
1601
class TestSmartServerRepositoryAllRevisionIds(
1602
tests.TestCaseWithMemoryTransport):
1604
def test_empty(self):
1605
"""An empty body should be returned for an empty repository."""
1606
backing = self.get_transport()
1607
request = smart_repo.SmartServerRepositoryAllRevisionIds(backing)
1608
self.make_repository('.')
1610
smart_req.SuccessfulSmartServerResponse((b"ok", ), b""),
1611
request.execute(b''))
1613
def test_some_revisions(self):
1614
"""An empty body should be returned for an empty repository."""
1615
backing = self.get_transport()
1616
request = smart_repo.SmartServerRepositoryAllRevisionIds(backing)
1617
tree = self.make_branch_and_memory_tree('.')
1620
tree.commit(rev_id=b'origineel', message="message")
1621
tree.commit(rev_id=b'nog-een-revisie', message="message")
1624
smart_req.SuccessfulSmartServerResponse((b"ok", ),
1625
b"origineel\nnog-een-revisie"),
1626
request.execute(b''))
1629
class TestSmartServerRepositoryBreakLock(tests.TestCaseWithMemoryTransport):
1631
def test_lock_to_break(self):
1632
backing = self.get_transport()
1633
request = smart_repo.SmartServerRepositoryBreakLock(backing)
1634
tree = self.make_branch_and_memory_tree('.')
1635
tree.branch.repository.lock_write()
1637
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1638
request.execute(b''))
1640
def test_nothing_to_break(self):
1641
backing = self.get_transport()
1642
request = smart_repo.SmartServerRepositoryBreakLock(backing)
1643
tree = self.make_branch_and_memory_tree('.')
1645
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1646
request.execute(b''))
1649
class TestSmartServerRepositoryGetParentMap(tests.TestCaseWithMemoryTransport):
1651
def test_trivial_bzipped(self):
1652
# This tests that the wire encoding is actually bzipped
1653
backing = self.get_transport()
1654
request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1655
tree = self.make_branch_and_memory_tree('.')
1657
self.assertEqual(None,
1658
request.execute(b'', b'missing-id'))
1659
# Note that it returns a body that is bzipped.
1661
smart_req.SuccessfulSmartServerResponse((b'ok', ), bz2.compress(b'')),
1662
request.do_body('\n\n0\n'))
1664
def test_trivial_include_missing(self):
1665
backing = self.get_transport()
1666
request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1667
tree = self.make_branch_and_memory_tree('.')
1669
self.assertEqual(None,
1670
request.execute(b'', b'missing-id', b'include-missing:'))
1672
smart_req.SuccessfulSmartServerResponse((b'ok', ),
1673
bz2.compress(b'missing:missing-id')),
1674
request.do_body(b'\n\n0\n'))
1677
class TestSmartServerRepositoryGetRevisionGraph(
1678
tests.TestCaseWithMemoryTransport):
1680
def test_none_argument(self):
1681
backing = self.get_transport()
1682
request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1683
tree = self.make_branch_and_memory_tree('.')
1686
r1 = tree.commit('1st commit')
1687
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1690
# the lines of revision_id->revision_parent_list has no guaranteed
1691
# order coming out of a dict, so sort both our test and response
1692
lines = sorted([b' '.join([r2, r1]), r1])
1693
response = request.execute(b'', b'')
1694
response.body = '\n'.join(sorted(response.body.split('\n')))
1697
smart_req.SmartServerResponse((b'ok', ), b'\n'.join(lines)), response)
1699
def test_specific_revision_argument(self):
1700
backing = self.get_transport()
1701
request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1702
tree = self.make_branch_and_memory_tree('.')
1705
rev_id_utf8 = u'\xc9'.encode('utf-8')
1706
r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
1707
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1710
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), rev_id_utf8),
1711
request.execute(b'', rev_id_utf8))
1713
def test_no_such_revision(self):
1714
backing = self.get_transport()
1715
request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1716
tree = self.make_branch_and_memory_tree('.')
1719
r1 = tree.commit('1st commit')
1722
# Note that it still returns body (of zero bytes).
1723
self.assertEqual(smart_req.SmartServerResponse(
1724
(b'nosuchrevision', b'missingrevision', ), b''),
1725
request.execute(b'', b'missingrevision'))
1728
class TestSmartServerRepositoryGetRevIdForRevno(
1729
tests.TestCaseWithMemoryTransport):
1731
def test_revno_found(self):
1732
backing = self.get_transport()
1733
request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1734
tree = self.make_branch_and_memory_tree('.')
1737
rev1_id_utf8 = u'\xc8'.encode('utf-8')
1738
rev2_id_utf8 = u'\xc9'.encode('utf-8')
1739
tree.commit('1st commit', rev_id=rev1_id_utf8)
1740
tree.commit('2nd commit', rev_id=rev2_id_utf8)
1743
self.assertEqual(smart_req.SmartServerResponse((b'ok', rev1_id_utf8)),
1744
request.execute(b'', 1, (2, rev2_id_utf8)))
1746
def test_known_revid_missing(self):
1747
backing = self.get_transport()
1748
request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1749
repo = self.make_repository('.')
1751
smart_req.FailedSmartServerResponse((b'nosuchrevision', b'ghost')),
1752
request.execute(b'', 1, (2, b'ghost')))
1754
def test_history_incomplete(self):
1755
backing = self.get_transport()
1756
request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1757
parent = self.make_branch_and_memory_tree('parent', format='1.9')
1759
parent.add([''], ['TREE_ROOT'])
1760
r1 = parent.commit(message='first commit')
1761
r2 = parent.commit(message='second commit')
1763
local = self.make_branch_and_memory_tree('local', format='1.9')
1764
local.branch.pull(parent.branch)
1765
local.set_parent_ids([r2])
1766
r3 = local.commit(message='local commit')
1767
local.branch.create_clone_on_transport(
1768
self.get_transport('stacked'), stacked_on=self.get_url('parent'))
1770
smart_req.SmartServerResponse((b'history-incomplete', 2, r2)),
1771
request.execute(b'stacked', 1, (3, r3)))
1774
class TestSmartServerRepositoryIterRevisions(
1775
tests.TestCaseWithMemoryTransport):
1777
def test_basic(self):
1778
backing = self.get_transport()
1779
request = smart_repo.SmartServerRepositoryIterRevisions(backing)
1780
tree = self.make_branch_and_memory_tree('.', format='2a')
1783
tree.commit('1st commit', rev_id="rev1")
1784
tree.commit('2nd commit', rev_id="rev2")
1787
self.assertIs(None, request.execute(b''))
1788
response = request.do_body("rev1\nrev2")
1789
self.assertTrue(response.is_successful())
1790
# Format 2a uses serializer format 10
1791
self.assertEqual(response.args, (b"ok", b"10"))
1793
self.addCleanup(tree.branch.lock_read().unlock)
1794
entries = [zlib.compress(record.get_bytes_as("fulltext")) for record in
1795
tree.branch.repository.revisions.get_record_stream(
1796
[(b"rev1", ), (b"rev2", )], "unordered", True)]
1798
contents = b"".join(response.body_stream)
1799
self.assertTrue(contents in (
1800
"".join([entries[0], entries[1]]),
1801
"".join([entries[1], entries[0]])))
1803
def test_missing(self):
1804
backing = self.get_transport()
1805
request = smart_repo.SmartServerRepositoryIterRevisions(backing)
1806
tree = self.make_branch_and_memory_tree('.', format='2a')
1808
self.assertIs(None, request.execute(b''))
1809
response = request.do_body(b"rev1\nrev2")
1810
self.assertTrue(response.is_successful())
1811
# Format 2a uses serializer format 10
1812
self.assertEqual(response.args, (b"ok", b"10"))
1814
contents = b"".join(response.body_stream)
1815
self.assertEqual(contents, b"")
1818
class GetStreamTestBase(tests.TestCaseWithMemoryTransport):
1820
def make_two_commit_repo(self):
1821
tree = self.make_branch_and_memory_tree('.')
1824
r1 = tree.commit('1st commit')
1825
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1827
repo = tree.branch.repository
1831
class TestSmartServerRepositoryGetStream(GetStreamTestBase):
1833
def test_ancestry_of(self):
1834
"""The search argument may be a 'ancestry-of' some heads'."""
1835
backing = self.get_transport()
1836
request = smart_repo.SmartServerRepositoryGetStream(backing)
1837
repo, r1, r2 = self.make_two_commit_repo()
1838
fetch_spec = [b'ancestry-of', r2]
1839
lines = b'\n'.join(fetch_spec)
1840
request.execute(b'', repo._format.network_name())
1841
response = request.do_body(lines)
1842
self.assertEqual((b'ok',), response.args)
1843
stream_bytes = b''.join(response.body_stream)
1844
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
1846
def test_search(self):
1847
"""The search argument may be a 'search' of some explicit keys."""
1848
backing = self.get_transport()
1849
request = smart_repo.SmartServerRepositoryGetStream(backing)
1850
repo, r1, r2 = self.make_two_commit_repo()
1851
fetch_spec = [b'search', r1 + b' ' + r2, b'null:', b'2']
1852
lines = b'\n'.join(fetch_spec)
1853
request.execute(b'', repo._format.network_name())
1854
response = request.do_body(lines)
1855
self.assertEqual((b'ok',), response.args)
1856
stream_bytes = b''.join(response.body_stream)
1857
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
1859
def test_search_everything(self):
1860
"""A search of 'everything' returns a stream."""
1861
backing = self.get_transport()
1862
request = smart_repo.SmartServerRepositoryGetStream_1_19(backing)
1863
repo, r1, r2 = self.make_two_commit_repo()
1864
serialised_fetch_spec = b'everything'
1865
request.execute(b'', repo._format.network_name())
1866
response = request.do_body(serialised_fetch_spec)
1867
self.assertEqual((b'ok',), response.args)
1868
stream_bytes = b''.join(response.body_stream)
1869
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
1872
class TestSmartServerRequestHasRevision(tests.TestCaseWithMemoryTransport):
1874
def test_missing_revision(self):
1875
"""For a missing revision, ('no', ) is returned."""
1876
backing = self.get_transport()
1877
request = smart_repo.SmartServerRequestHasRevision(backing)
1878
self.make_repository('.')
1879
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
1880
request.execute(b'', b'revid'))
1882
def test_present_revision(self):
1883
"""For a present revision, ('yes', ) is returned."""
1884
backing = self.get_transport()
1885
request = smart_repo.SmartServerRequestHasRevision(backing)
1886
tree = self.make_branch_and_memory_tree('.')
1889
rev_id_utf8 = u'\xc8abc'.encode('utf-8')
1890
r1 = tree.commit('a commit', rev_id=rev_id_utf8)
1892
self.assertTrue(tree.branch.repository.has_revision(rev_id_utf8))
1893
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
1894
request.execute(b'', rev_id_utf8))
1897
class TestSmartServerRepositoryIterFilesBytes(tests.TestCaseWithTransport):
1899
def test_single(self):
1900
backing = self.get_transport()
1901
request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
1902
t = self.make_branch_and_tree('.')
1903
self.addCleanup(t.lock_write().unlock)
1904
self.build_tree_contents([("file", b"somecontents")])
1905
t.add(["file"], [b"thefileid"])
1906
t.commit(rev_id=b'somerev', message="add file")
1907
self.assertIs(None, request.execute(b''))
1908
response = request.do_body(b"thefileid\0somerev\n")
1909
self.assertTrue(response.is_successful())
1910
self.assertEqual(response.args, (b"ok", ))
1911
self.assertEqual(b"".join(response.body_stream),
1912
b"ok\x000\n" + zlib.compress(b"somecontents"))
1914
def test_missing(self):
1915
backing = self.get_transport()
1916
request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
1917
t = self.make_branch_and_tree('.')
1918
self.addCleanup(t.lock_write().unlock)
1919
self.assertIs(None, request.execute(b''))
1920
response = request.do_body(b"thefileid\0revision\n")
1921
self.assertTrue(response.is_successful())
1922
self.assertEqual(response.args, (b"ok", ))
1923
self.assertEqual(b"".join(response.body_stream),
1924
b"absent\x00thefileid\x00revision\x000\n")
1927
class TestSmartServerRequestHasSignatureForRevisionId(
1928
tests.TestCaseWithMemoryTransport):
1930
def test_missing_revision(self):
1931
"""For a missing revision, NoSuchRevision is returned."""
1932
backing = self.get_transport()
1933
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1935
self.make_repository('.')
1937
smart_req.FailedSmartServerResponse(
1938
(b'nosuchrevision', b'revid'), None),
1939
request.execute(b'', b'revid'))
1941
def test_missing_signature(self):
1942
"""For a missing signature, ('no', ) is returned."""
1943
backing = self.get_transport()
1944
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1946
tree = self.make_branch_and_memory_tree('.')
1949
r1 = tree.commit('a commit', rev_id=b'A')
1951
self.assertTrue(tree.branch.repository.has_revision('A'))
1952
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
1953
request.execute(b'', b'A'))
1955
def test_present_signature(self):
1956
"""For a present signature, ('yes', ) is returned."""
1957
backing = self.get_transport()
1958
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1960
strategy = gpg.LoopbackGPGStrategy(None)
1961
tree = self.make_branch_and_memory_tree('.')
1964
r1 = tree.commit('a commit', rev_id=b'A')
1965
tree.branch.repository.start_write_group()
1966
tree.branch.repository.sign_revision(b'A', strategy)
1967
tree.branch.repository.commit_write_group()
1969
self.assertTrue(tree.branch.repository.has_revision(b'A'))
1970
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
1971
request.execute(b'', b'A'))
1974
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
1976
def test_empty_revid(self):
1977
"""With an empty revid, we get only size an number and revisions"""
1978
backing = self.get_transport()
1979
request = smart_repo.SmartServerRepositoryGatherStats(backing)
1980
repository = self.make_repository('.')
1981
stats = repository.gather_stats()
1982
expected_body = b'revisions: 0\n'
1983
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), expected_body),
1984
request.execute(b'', b'', b'no'))
1986
def test_revid_with_committers(self):
1987
"""For a revid we get more infos."""
1988
backing = self.get_transport()
1989
rev_id_utf8 = u'\xc8abc'.encode('utf-8')
1990
request = smart_repo.SmartServerRepositoryGatherStats(backing)
1991
tree = self.make_branch_and_memory_tree('.')
1994
# Let's build a predictable result
1995
tree.commit('a commit', timestamp=123456.2, timezone=3600)
1996
tree.commit('a commit', timestamp=654321.4, timezone=0,
2000
stats = tree.branch.repository.gather_stats()
2001
expected_body = (b'firstrev: 123456.200 3600\n'
2002
b'latestrev: 654321.400 0\n'
2004
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), expected_body),
2005
request.execute(b'', rev_id_utf8, b'no'))
2007
def test_not_empty_repository_with_committers(self):
2008
"""For a revid and requesting committers we get the whole thing."""
2009
backing = self.get_transport()
2010
rev_id_utf8 = u'\xc8abc'.encode('utf-8')
2011
request = smart_repo.SmartServerRepositoryGatherStats(backing)
2012
tree = self.make_branch_and_memory_tree('.')
2015
# Let's build a predictable result
2016
tree.commit('a commit', timestamp=123456.2, timezone=3600,
2018
tree.commit('a commit', timestamp=654321.4, timezone=0,
2019
committer='bar', rev_id=rev_id_utf8)
2021
stats = tree.branch.repository.gather_stats()
2023
expected_body = (b'committers: 2\n'
2024
b'firstrev: 123456.200 3600\n'
2025
b'latestrev: 654321.400 0\n'
2027
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), expected_body),
2028
request.execute(b'',
2029
rev_id_utf8, b'yes'))
2031
def test_unknown_revid(self):
2032
"""An unknown revision id causes a 'nosuchrevision' error."""
2033
backing = self.get_transport()
2034
request = smart_repo.SmartServerRepositoryGatherStats(backing)
2035
repository = self.make_repository('.')
2036
expected_body = b'revisions: 0\n'
2038
smart_req.FailedSmartServerResponse(
2039
(b'nosuchrevision', b'mia'), None),
2040
request.execute(b'', b'mia', b'yes'))
2043
class TestSmartServerRepositoryIsShared(tests.TestCaseWithMemoryTransport):
2045
def test_is_shared(self):
2046
"""For a shared repository, ('yes', ) is returned."""
2047
backing = self.get_transport()
2048
request = smart_repo.SmartServerRepositoryIsShared(backing)
2049
self.make_repository('.', shared=True)
2050
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2051
request.execute(b'', ))
2053
def test_is_not_shared(self):
2054
"""For a shared repository, ('no', ) is returned."""
2055
backing = self.get_transport()
2056
request = smart_repo.SmartServerRepositoryIsShared(backing)
2057
self.make_repository('.', shared=False)
2058
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2059
request.execute(b'', ))
2062
class TestSmartServerRepositoryGetRevisionSignatureText(
2063
tests.TestCaseWithMemoryTransport):
2065
def test_get_signature(self):
2066
backing = self.get_transport()
2067
request = smart_repo.SmartServerRepositoryGetRevisionSignatureText(
2069
bb = self.make_branch_builder('.')
2070
bb.build_commit(rev_id=b'A')
2071
repo = bb.get_branch().repository
2072
strategy = gpg.LoopbackGPGStrategy(None)
2073
self.addCleanup(repo.lock_write().unlock)
2074
repo.start_write_group()
2075
repo.sign_revision(b'A', strategy)
2076
repo.commit_write_group()
2078
b'-----BEGIN PSEUDO-SIGNED CONTENT-----\n' +
2079
Testament.from_revision(repo, 'A').as_short_text() +
2080
b'-----END PSEUDO-SIGNED CONTENT-----\n')
2082
smart_req.SmartServerResponse((b'ok', ), expected_body),
2083
request.execute(b'', b'A'))
2086
class TestSmartServerRepositoryMakeWorkingTrees(
2087
tests.TestCaseWithMemoryTransport):
2089
def test_make_working_trees(self):
2090
"""For a repository with working trees, ('yes', ) is returned."""
2091
backing = self.get_transport()
2092
request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
2093
r = self.make_repository('.')
2094
r.set_make_working_trees(True)
2095
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2096
request.execute(b'', ))
2098
def test_is_not_shared(self):
2099
"""For a repository with working trees, ('no', ) is returned."""
2100
backing = self.get_transport()
2101
request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
2102
r = self.make_repository('.')
2103
r.set_make_working_trees(False)
2104
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2105
request.execute(b'', ))
2108
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
2110
def test_lock_write_on_unlocked_repo(self):
2111
backing = self.get_transport()
2112
request = smart_repo.SmartServerRepositoryLockWrite(backing)
2113
repository = self.make_repository('.', format='knit')
2114
response = request.execute(b'')
2115
nonce = repository.control_files._lock.peek().get('nonce')
2116
self.assertEqual(smart_req.SmartServerResponse((b'ok', nonce)), response)
2117
# The repository is now locked. Verify that with a new repository
2119
new_repo = repository.controldir.open_repository()
2120
self.assertRaises(errors.LockContention, new_repo.lock_write)
2122
request = smart_repo.SmartServerRepositoryUnlock(backing)
2123
response = request.execute(b'', nonce)
2125
def test_lock_write_on_locked_repo(self):
2126
backing = self.get_transport()
2127
request = smart_repo.SmartServerRepositoryLockWrite(backing)
2128
repository = self.make_repository('.', format='knit')
2129
repo_token = repository.lock_write().repository_token
2130
repository.leave_lock_in_place()
2132
response = request.execute(b'')
2134
smart_req.SmartServerResponse((b'LockContention',)), response)
2136
repository.lock_write(repo_token)
2137
repository.dont_leave_lock_in_place()
2140
def test_lock_write_on_readonly_transport(self):
2141
backing = self.get_readonly_transport()
2142
request = smart_repo.SmartServerRepositoryLockWrite(backing)
2143
repository = self.make_repository('.', format='knit')
2144
response = request.execute(b'')
2145
self.assertFalse(response.is_successful())
2146
self.assertEqual(b'LockFailed', response.args[0])
2149
class TestInsertStreamBase(tests.TestCaseWithMemoryTransport):
2151
def make_empty_byte_stream(self, repo):
2152
byte_stream = smart_repo._stream_to_byte_stream([], repo._format)
2153
return b''.join(byte_stream)
2156
class TestSmartServerRepositoryInsertStream(TestInsertStreamBase):
2158
def test_insert_stream_empty(self):
2159
backing = self.get_transport()
2160
request = smart_repo.SmartServerRepositoryInsertStream(backing)
2161
repository = self.make_repository('.')
2162
response = request.execute(b'', b'')
2163
self.assertEqual(None, response)
2164
response = request.do_chunk(self.make_empty_byte_stream(repository))
2165
self.assertEqual(None, response)
2166
response = request.do_end()
2167
self.assertEqual(smart_req.SmartServerResponse((b'ok', )), response)
2170
class TestSmartServerRepositoryInsertStreamLocked(TestInsertStreamBase):
2172
def test_insert_stream_empty(self):
2173
backing = self.get_transport()
2174
request = smart_repo.SmartServerRepositoryInsertStreamLocked(
2176
repository = self.make_repository('.', format='knit')
2177
lock_token = repository.lock_write().repository_token
2178
response = request.execute(b'', b'', lock_token)
2179
self.assertEqual(None, response)
2180
response = request.do_chunk(self.make_empty_byte_stream(repository))
2181
self.assertEqual(None, response)
2182
response = request.do_end()
2183
self.assertEqual(smart_req.SmartServerResponse((b'ok', )), response)
2186
def test_insert_stream_with_wrong_lock_token(self):
2187
backing = self.get_transport()
2188
request = smart_repo.SmartServerRepositoryInsertStreamLocked(
2190
repository = self.make_repository('.', format='knit')
2191
lock_token = repository.lock_write().repository_token
2193
errors.TokenMismatch, request.execute, b'', b'', b'wrong-token')
2197
class TestSmartServerRepositoryUnlock(tests.TestCaseWithMemoryTransport):
2199
def test_unlock_on_locked_repo(self):
2200
backing = self.get_transport()
2201
request = smart_repo.SmartServerRepositoryUnlock(backing)
2202
repository = self.make_repository('.', format='knit')
2203
token = repository.lock_write().repository_token
2204
repository.leave_lock_in_place()
2206
response = request.execute(b'', token)
2208
smart_req.SmartServerResponse((b'ok',)), response)
2209
# The repository is now unlocked. Verify that with a new repository
2211
new_repo = repository.controldir.open_repository()
2212
new_repo.lock_write()
2215
def test_unlock_on_unlocked_repo(self):
2216
backing = self.get_transport()
2217
request = smart_repo.SmartServerRepositoryUnlock(backing)
2218
repository = self.make_repository('.', format='knit')
2219
response = request.execute(b'', b'some token')
2221
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
2224
class TestSmartServerRepositoryGetPhysicalLockStatus(
2225
tests.TestCaseWithTransport):
2227
def test_with_write_lock(self):
2228
backing = self.get_transport()
2229
repo = self.make_repository('.')
2230
self.addCleanup(repo.lock_write().unlock)
2231
# lock_write() doesn't necessarily actually take a physical
2233
if repo.get_physical_lock_status():
2237
request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2238
request = request_class(backing)
2239
self.assertEqual(smart_req.SuccessfulSmartServerResponse((expected,)),
2240
request.execute(b'', ))
2242
def test_without_write_lock(self):
2243
backing = self.get_transport()
2244
repo = self.make_repository('.')
2245
self.assertEqual(False, repo.get_physical_lock_status())
2246
request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2247
request = request_class(backing)
2248
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'no',)),
2249
request.execute(b'', ))
2252
class TestSmartServerRepositoryReconcile(tests.TestCaseWithTransport):
2254
def test_reconcile(self):
2255
backing = self.get_transport()
2256
repo = self.make_repository('.')
2257
token = repo.lock_write().repository_token
2258
self.addCleanup(repo.unlock)
2259
request_class = smart_repo.SmartServerRepositoryReconcile
2260
request = request_class(backing)
2261
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
2263
b'garbage_inventories: 0\n'
2264
b'inconsistent_parents: 0\n'),
2265
request.execute(b'', token))
2268
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
2270
def test_is_readonly_no(self):
2271
backing = self.get_transport()
2272
request = smart_req.SmartServerIsReadonly(backing)
2273
response = request.execute()
2275
smart_req.SmartServerResponse((b'no',)), response)
2277
def test_is_readonly_yes(self):
2278
backing = self.get_readonly_transport()
2279
request = smart_req.SmartServerIsReadonly(backing)
2280
response = request.execute()
2282
smart_req.SmartServerResponse((b'yes',)), response)
2285
class TestSmartServerRepositorySetMakeWorkingTrees(
2286
tests.TestCaseWithMemoryTransport):
2288
def test_set_false(self):
2289
backing = self.get_transport()
2290
repo = self.make_repository('.', shared=True)
2291
repo.set_make_working_trees(True)
2292
request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2293
request = request_class(backing)
2294
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2295
request.execute(b'', b'False'))
2296
repo = repo.controldir.open_repository()
2297
self.assertFalse(repo.make_working_trees())
2299
def test_set_true(self):
2300
backing = self.get_transport()
2301
repo = self.make_repository('.', shared=True)
2302
repo.set_make_working_trees(False)
2303
request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2304
request = request_class(backing)
2305
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2306
request.execute(b'', b'True'))
2307
repo = repo.controldir.open_repository()
2308
self.assertTrue(repo.make_working_trees())
2311
class TestSmartServerRepositoryGetSerializerFormat(
2312
tests.TestCaseWithMemoryTransport):
2314
def test_get_serializer_format(self):
2315
backing = self.get_transport()
2316
repo = self.make_repository('.', format='2a')
2317
request_class = smart_repo.SmartServerRepositoryGetSerializerFormat
2318
request = request_class(backing)
2320
smart_req.SuccessfulSmartServerResponse((b'ok', b'10')),
2321
request.execute(b''))
2324
class TestSmartServerRepositoryWriteGroup(
2325
tests.TestCaseWithMemoryTransport):
2327
def test_start_write_group(self):
2328
backing = self.get_transport()
2329
repo = self.make_repository('.')
2330
lock_token = repo.lock_write().repository_token
2331
self.addCleanup(repo.unlock)
2332
request_class = smart_repo.SmartServerRepositoryStartWriteGroup
2333
request = request_class(backing)
2334
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok', [])),
2335
request.execute(b'', lock_token))
2337
def test_start_write_group_unsuspendable(self):
2338
backing = self.get_transport()
2339
repo = self.make_repository('.', format='knit')
2340
lock_token = repo.lock_write().repository_token
2341
self.addCleanup(repo.unlock)
2342
request_class = smart_repo.SmartServerRepositoryStartWriteGroup
2343
request = request_class(backing)
2345
smart_req.FailedSmartServerResponse((b'UnsuspendableWriteGroup',)),
2346
request.execute(b'', lock_token))
2348
def test_commit_write_group(self):
2349
backing = self.get_transport()
2350
repo = self.make_repository('.')
2351
lock_token = repo.lock_write().repository_token
2352
self.addCleanup(repo.unlock)
2353
repo.start_write_group()
2354
tokens = repo.suspend_write_group()
2355
request_class = smart_repo.SmartServerRepositoryCommitWriteGroup
2356
request = request_class(backing)
2357
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2358
request.execute(b'', lock_token, tokens))
2360
def test_abort_write_group(self):
2361
backing = self.get_transport()
2362
repo = self.make_repository('.')
2363
lock_token = repo.lock_write().repository_token
2364
repo.start_write_group()
2365
tokens = repo.suspend_write_group()
2366
self.addCleanup(repo.unlock)
2367
request_class = smart_repo.SmartServerRepositoryAbortWriteGroup
2368
request = request_class(backing)
2369
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2370
request.execute(b'', lock_token, tokens))
2372
def test_check_write_group(self):
2373
backing = self.get_transport()
2374
repo = self.make_repository('.')
2375
lock_token = repo.lock_write().repository_token
2376
repo.start_write_group()
2377
tokens = repo.suspend_write_group()
2378
self.addCleanup(repo.unlock)
2379
request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
2380
request = request_class(backing)
2381
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2382
request.execute(b'', lock_token, tokens))
2384
def test_check_write_group_invalid(self):
2385
backing = self.get_transport()
2386
repo = self.make_repository('.')
2387
lock_token = repo.lock_write().repository_token
2388
self.addCleanup(repo.unlock)
2389
request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
2390
request = request_class(backing)
2391
self.assertEqual(smart_req.FailedSmartServerResponse(
2392
(b'UnresumableWriteGroup', [b'random'],
2393
b'Malformed write group token')),
2394
request.execute(b'', lock_token, [b"random"]))
2397
class TestSmartServerPackRepositoryAutopack(tests.TestCaseWithTransport):
2399
def make_repo_needing_autopacking(self, path='.'):
2400
# Make a repo in need of autopacking.
2401
tree = self.make_branch_and_tree('.', format='pack-0.92')
2402
repo = tree.branch.repository
2403
# monkey-patch the pack collection to disable autopacking
2404
repo._pack_collection._max_pack_count = lambda count: count
2406
tree.commit('commit %s' % x)
2407
self.assertEqual(10, len(repo._pack_collection.names()))
2408
del repo._pack_collection._max_pack_count
2411
def test_autopack_needed(self):
2412
repo = self.make_repo_needing_autopacking()
2414
self.addCleanup(repo.unlock)
2415
backing = self.get_transport()
2416
request = smart_packrepo.SmartServerPackRepositoryAutopack(
2418
response = request.execute(b'')
2419
self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
2420
repo._pack_collection.reload_pack_names()
2421
self.assertEqual(1, len(repo._pack_collection.names()))
2423
def test_autopack_not_needed(self):
2424
tree = self.make_branch_and_tree('.', format='pack-0.92')
2425
repo = tree.branch.repository
2427
self.addCleanup(repo.unlock)
2429
tree.commit('commit %s' % x)
2430
backing = self.get_transport()
2431
request = smart_packrepo.SmartServerPackRepositoryAutopack(
2433
response = request.execute(b'')
2434
self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
2435
repo._pack_collection.reload_pack_names()
2436
self.assertEqual(9, len(repo._pack_collection.names()))
2438
def test_autopack_on_nonpack_format(self):
2439
"""A request to autopack a non-pack repo is a no-op."""
2440
repo = self.make_repository('.', format='knit')
2441
backing = self.get_transport()
2442
request = smart_packrepo.SmartServerPackRepositoryAutopack(
2444
response = request.execute(b'')
2445
self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
2448
class TestSmartServerVfsGet(tests.TestCaseWithMemoryTransport):
2450
def test_unicode_path(self):
2451
"""VFS requests expect unicode paths to be escaped."""
2452
filename = u'foo\N{INTERROBANG}'
2453
filename_escaped = urlutils.escape(filename)
2454
backing = self.get_transport()
2455
request = vfs.GetRequest(backing)
2456
backing.put_bytes_non_atomic(filename_escaped, b'contents')
2457
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'contents'),
2458
request.execute(filename_escaped))
2461
class TestHandlers(tests.TestCase):
2462
"""Tests for the request.request_handlers object."""
2464
def test_all_registrations_exist(self):
2465
"""All registered request_handlers can be found."""
2466
# If there's a typo in a register_lazy call, this loop will fail with
2467
# an AttributeError.
2468
for key in smart_req.request_handlers.keys():
2470
item = smart_req.request_handlers.get(key)
2471
except AttributeError as e:
2472
raise AttributeError('failed to get %s: %s' % (key, e))
2474
def assertHandlerEqual(self, verb, handler):
2475
self.assertEqual(smart_req.request_handlers.get(verb), handler)
2477
def test_registered_methods(self):
2478
"""Test that known methods are registered to the correct object."""
2479
self.assertHandlerEqual(b'Branch.break_lock',
2480
smart_branch.SmartServerBranchBreakLock)
2481
self.assertHandlerEqual(b'Branch.get_config_file',
2482
smart_branch.SmartServerBranchGetConfigFile)
2483
self.assertHandlerEqual(b'Branch.put_config_file',
2484
smart_branch.SmartServerBranchPutConfigFile)
2485
self.assertHandlerEqual(b'Branch.get_parent',
2486
smart_branch.SmartServerBranchGetParent)
2487
self.assertHandlerEqual(b'Branch.get_physical_lock_status',
2488
smart_branch.SmartServerBranchRequestGetPhysicalLockStatus)
2489
self.assertHandlerEqual(b'Branch.get_tags_bytes',
2490
smart_branch.SmartServerBranchGetTagsBytes)
2491
self.assertHandlerEqual(b'Branch.lock_write',
2492
smart_branch.SmartServerBranchRequestLockWrite)
2493
self.assertHandlerEqual(b'Branch.last_revision_info',
2494
smart_branch.SmartServerBranchRequestLastRevisionInfo)
2495
self.assertHandlerEqual(b'Branch.revision_history',
2496
smart_branch.SmartServerRequestRevisionHistory)
2497
self.assertHandlerEqual(b'Branch.revision_id_to_revno',
2498
smart_branch.SmartServerBranchRequestRevisionIdToRevno)
2499
self.assertHandlerEqual(b'Branch.set_config_option',
2500
smart_branch.SmartServerBranchRequestSetConfigOption)
2501
self.assertHandlerEqual(b'Branch.set_last_revision',
2502
smart_branch.SmartServerBranchRequestSetLastRevision)
2503
self.assertHandlerEqual(b'Branch.set_last_revision_info',
2504
smart_branch.SmartServerBranchRequestSetLastRevisionInfo)
2505
self.assertHandlerEqual(b'Branch.set_last_revision_ex',
2506
smart_branch.SmartServerBranchRequestSetLastRevisionEx)
2507
self.assertHandlerEqual(b'Branch.set_parent_location',
2508
smart_branch.SmartServerBranchRequestSetParentLocation)
2509
self.assertHandlerEqual(b'Branch.unlock',
2510
smart_branch.SmartServerBranchRequestUnlock)
2511
self.assertHandlerEqual(b'BzrDir.destroy_branch',
2512
smart_dir.SmartServerBzrDirRequestDestroyBranch)
2513
self.assertHandlerEqual(b'BzrDir.find_repository',
2514
smart_dir.SmartServerRequestFindRepositoryV1)
2515
self.assertHandlerEqual(b'BzrDir.find_repositoryV2',
2516
smart_dir.SmartServerRequestFindRepositoryV2)
2517
self.assertHandlerEqual(b'BzrDirFormat.initialize',
2518
smart_dir.SmartServerRequestInitializeBzrDir)
2519
self.assertHandlerEqual(b'BzrDirFormat.initialize_ex_1.16',
2520
smart_dir.SmartServerRequestBzrDirInitializeEx)
2521
self.assertHandlerEqual(b'BzrDir.checkout_metadir',
2522
smart_dir.SmartServerBzrDirRequestCheckoutMetaDir)
2523
self.assertHandlerEqual(b'BzrDir.cloning_metadir',
2524
smart_dir.SmartServerBzrDirRequestCloningMetaDir)
2525
self.assertHandlerEqual(b'BzrDir.get_branches',
2526
smart_dir.SmartServerBzrDirRequestGetBranches)
2527
self.assertHandlerEqual(b'BzrDir.get_config_file',
2528
smart_dir.SmartServerBzrDirRequestConfigFile)
2529
self.assertHandlerEqual(b'BzrDir.open_branch',
2530
smart_dir.SmartServerRequestOpenBranch)
2531
self.assertHandlerEqual(b'BzrDir.open_branchV2',
2532
smart_dir.SmartServerRequestOpenBranchV2)
2533
self.assertHandlerEqual(b'BzrDir.open_branchV3',
2534
smart_dir.SmartServerRequestOpenBranchV3)
2535
self.assertHandlerEqual(b'PackRepository.autopack',
2536
smart_packrepo.SmartServerPackRepositoryAutopack)
2537
self.assertHandlerEqual(b'Repository.add_signature_text',
2538
smart_repo.SmartServerRepositoryAddSignatureText)
2539
self.assertHandlerEqual(b'Repository.all_revision_ids',
2540
smart_repo.SmartServerRepositoryAllRevisionIds)
2541
self.assertHandlerEqual(b'Repository.break_lock',
2542
smart_repo.SmartServerRepositoryBreakLock)
2543
self.assertHandlerEqual(b'Repository.gather_stats',
2544
smart_repo.SmartServerRepositoryGatherStats)
2545
self.assertHandlerEqual(b'Repository.get_parent_map',
2546
smart_repo.SmartServerRepositoryGetParentMap)
2547
self.assertHandlerEqual(b'Repository.get_physical_lock_status',
2548
smart_repo.SmartServerRepositoryGetPhysicalLockStatus)
2549
self.assertHandlerEqual(b'Repository.get_rev_id_for_revno',
2550
smart_repo.SmartServerRepositoryGetRevIdForRevno)
2551
self.assertHandlerEqual(b'Repository.get_revision_graph',
2552
smart_repo.SmartServerRepositoryGetRevisionGraph)
2553
self.assertHandlerEqual(b'Repository.get_revision_signature_text',
2554
smart_repo.SmartServerRepositoryGetRevisionSignatureText)
2555
self.assertHandlerEqual(b'Repository.get_stream',
2556
smart_repo.SmartServerRepositoryGetStream)
2557
self.assertHandlerEqual(b'Repository.get_stream_1.19',
2558
smart_repo.SmartServerRepositoryGetStream_1_19)
2559
self.assertHandlerEqual(b'Repository.iter_revisions',
2560
smart_repo.SmartServerRepositoryIterRevisions)
2561
self.assertHandlerEqual(b'Repository.has_revision',
2562
smart_repo.SmartServerRequestHasRevision)
2563
self.assertHandlerEqual(b'Repository.insert_stream',
2564
smart_repo.SmartServerRepositoryInsertStream)
2565
self.assertHandlerEqual(b'Repository.insert_stream_locked',
2566
smart_repo.SmartServerRepositoryInsertStreamLocked)
2567
self.assertHandlerEqual(b'Repository.is_shared',
2568
smart_repo.SmartServerRepositoryIsShared)
2569
self.assertHandlerEqual(b'Repository.iter_files_bytes',
2570
smart_repo.SmartServerRepositoryIterFilesBytes)
2571
self.assertHandlerEqual(b'Repository.lock_write',
2572
smart_repo.SmartServerRepositoryLockWrite)
2573
self.assertHandlerEqual(b'Repository.make_working_trees',
2574
smart_repo.SmartServerRepositoryMakeWorkingTrees)
2575
self.assertHandlerEqual(b'Repository.pack',
2576
smart_repo.SmartServerRepositoryPack)
2577
self.assertHandlerEqual(b'Repository.reconcile',
2578
smart_repo.SmartServerRepositoryReconcile)
2579
self.assertHandlerEqual(b'Repository.tarball',
2580
smart_repo.SmartServerRepositoryTarball)
2581
self.assertHandlerEqual(b'Repository.unlock',
2582
smart_repo.SmartServerRepositoryUnlock)
2583
self.assertHandlerEqual(b'Repository.start_write_group',
2584
smart_repo.SmartServerRepositoryStartWriteGroup)
2585
self.assertHandlerEqual(b'Repository.check_write_group',
2586
smart_repo.SmartServerRepositoryCheckWriteGroup)
2587
self.assertHandlerEqual(b'Repository.commit_write_group',
2588
smart_repo.SmartServerRepositoryCommitWriteGroup)
2589
self.assertHandlerEqual(b'Repository.abort_write_group',
2590
smart_repo.SmartServerRepositoryAbortWriteGroup)
2591
self.assertHandlerEqual(b'VersionedFileRepository.get_serializer_format',
2592
smart_repo.SmartServerRepositoryGetSerializerFormat)
2593
self.assertHandlerEqual(b'VersionedFileRepository.get_inventories',
2594
smart_repo.SmartServerRepositoryGetInventories)
2595
self.assertHandlerEqual(b'Transport.is_readonly',
2596
smart_req.SmartServerIsReadonly)
2599
class SmartTCPServerHookTests(tests.TestCaseWithMemoryTransport):
2600
"""Tests for SmartTCPServer hooks."""
2603
super(SmartTCPServerHookTests, self).setUp()
2604
self.server = server.SmartTCPServer(self.get_transport())
2606
def test_run_server_started_hooks(self):
2607
"""Test the server started hooks get fired properly."""
2609
server.SmartTCPServer.hooks.install_named_hook('server_started',
2610
lambda backing_urls, url: started_calls.append((backing_urls, url)),
2612
started_ex_calls = []
2613
server.SmartTCPServer.hooks.install_named_hook('server_started_ex',
2614
lambda backing_urls, url: started_ex_calls.append((backing_urls, url)),
2616
self.server._sockname = ('example.com', 42)
2617
self.server.run_server_started_hooks()
2618
self.assertEqual(started_calls,
2619
[([self.get_transport().base], 'bzr://example.com:42/')])
2620
self.assertEqual(started_ex_calls,
2621
[([self.get_transport().base], self.server)])
2623
def test_run_server_started_hooks_ipv6(self):
2624
"""Test that socknames can contain 4-tuples."""
2625
self.server._sockname = ('::', 42, 0, 0)
2627
server.SmartTCPServer.hooks.install_named_hook('server_started',
2628
lambda backing_urls, url: started_calls.append((backing_urls, url)),
2630
self.server.run_server_started_hooks()
2631
self.assertEqual(started_calls,
2632
[([self.get_transport().base], 'bzr://:::42/')])
2634
def test_run_server_stopped_hooks(self):
2635
"""Test the server stopped hooks."""
2636
self.server._sockname = ('example.com', 42)
2638
server.SmartTCPServer.hooks.install_named_hook('server_stopped',
2639
lambda backing_urls, url: stopped_calls.append((backing_urls, url)),
2641
self.server.run_server_stopped_hooks()
2642
self.assertEqual(stopped_calls,
2643
[([self.get_transport().base], 'bzr://example.com:42/')])
2646
class TestSmartServerRepositoryPack(tests.TestCaseWithMemoryTransport):
2648
def test_pack(self):
2649
backing = self.get_transport()
2650
request = smart_repo.SmartServerRepositoryPack(backing)
2651
tree = self.make_branch_and_memory_tree('.')
2652
repo_token = tree.branch.repository.lock_write().repository_token
2654
self.assertIs(None, request.execute(b'', repo_token, False))
2657
smart_req.SuccessfulSmartServerResponse((b'ok', ), ),
2658
request.do_body(b''))
2661
class TestSmartServerRepositoryGetInventories(tests.TestCaseWithTransport):
2663
def _get_serialized_inventory_delta(self, repository, base_revid, revid):
2664
base_inv = repository.revision_tree(base_revid).root_inventory
2665
inv = repository.revision_tree(revid).root_inventory
2666
inv_delta = inv._make_delta(base_inv)
2667
serializer = inventory_delta.InventoryDeltaSerializer(True, False)
2668
return "".join(serializer.delta_to_lines(base_revid, revid, inv_delta))
2670
def test_single(self):
2671
backing = self.get_transport()
2672
request = smart_repo.SmartServerRepositoryGetInventories(backing)
2673
t = self.make_branch_and_tree('.', format='2a')
2674
self.addCleanup(t.lock_write().unlock)
2675
self.build_tree_contents([("file", b"somecontents")])
2676
t.add(["file"], [b"thefileid"])
2677
t.commit(rev_id=b'somerev', message="add file")
2678
self.assertIs(None, request.execute(b'', b'unordered'))
2679
response = request.do_body(b"somerev\n")
2680
self.assertTrue(response.is_successful())
2681
self.assertEqual(response.args, (b"ok", ))
2682
stream = [('inventory-deltas', [
2683
versionedfile.FulltextContentFactory('somerev', None, None,
2684
self._get_serialized_inventory_delta(
2685
t.branch.repository, b'null:', b'somerev'))])]
2686
fmt = controldir.format_registry.get('2a')().repository_format
2688
b"".join(response.body_stream),
2689
b"".join(smart_repo._stream_to_byte_stream(stream, fmt)))
2691
def test_empty(self):
2692
backing = self.get_transport()
2693
request = smart_repo.SmartServerRepositoryGetInventories(backing)
2694
t = self.make_branch_and_tree('.', format='2a')
2695
self.addCleanup(t.lock_write().unlock)
2696
self.build_tree_contents([("file", b"somecontents")])
2697
t.add(["file"], [b"thefileid"])
2698
t.commit(rev_id=b'somerev', message="add file")
2699
self.assertIs(None, request.execute(b'', b'unordered'))
2700
response = request.do_body(b"")
2701
self.assertTrue(response.is_successful())
2702
self.assertEqual(response.args, (b"ok", ))
2703
self.assertEqual(b"".join(response.body_stream),
2704
b"Bazaar pack format 1 (introduced in 0.18)\nB54\n\nBazaar repository format 2a (needs bzr 1.16 or later)\nE")