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.
28
from io import BytesIO
34
branch as _mod_branch,
42
from breezy.bzr import (
43
branch as _mod_bzrbranch,
47
from breezy.bzr.smart import (
48
branch as smart_branch,
50
repository as smart_repo,
51
packrepository as smart_packrepo,
56
from breezy.bzr.testament import Testament
57
from breezy.tests import test_server
58
from breezy.transport import (
64
def load_tests(loader, standard_tests, pattern):
65
"""Multiply tests version and protocol consistency."""
66
# FindRepository tests.
69
"_request_class": smart_dir.SmartServerRequestFindRepositoryV1}),
70
("find_repositoryV2", {
71
"_request_class": smart_dir.SmartServerRequestFindRepositoryV2}),
72
("find_repositoryV3", {
73
"_request_class": smart_dir.SmartServerRequestFindRepositoryV3}),
75
to_adapt, result = tests.split_suite_by_re(
76
standard_tests, "TestSmartServerRequestFindRepository")
77
v2_only, v1_and_2 = tests.split_suite_by_re(to_adapt, "_v2")
78
tests.multiply_tests(v1_and_2, scenarios, result)
79
# The first scenario is only applicable to v1 protocols, it is deleted
81
tests.multiply_tests(v2_only, scenarios[1:], result)
85
class TestCaseWithChrootedTransport(tests.TestCaseWithTransport):
88
self.vfs_transport_factory = memory.MemoryServer
89
super(TestCaseWithChrootedTransport, self).setUp()
90
self._chroot_server = None
92
def get_transport(self, relpath=None):
93
if self._chroot_server is None:
94
backing_transport = tests.TestCaseWithTransport.get_transport(self)
95
self._chroot_server = chroot.ChrootServer(backing_transport)
96
self.start_server(self._chroot_server)
97
t = transport.get_transport_from_url(self._chroot_server.get_url())
98
if relpath is not None:
103
class TestCaseWithSmartMedium(tests.TestCaseWithMemoryTransport):
106
super(TestCaseWithSmartMedium, self).setUp()
107
# We're allowed to set the transport class here, so that we don't use
108
# the default or a parameterized class, but rather use the
109
# TestCaseWithTransport infrastructure to set up a smart server and
111
self.overrideAttr(self, "transport_server", self.make_transport_server)
113
def make_transport_server(self):
114
return test_server.SmartTCPServer_for_testing('-' + self.id())
116
def get_smart_medium(self):
117
"""Get a smart medium to use in tests."""
118
return self.get_transport().get_smart_medium()
121
class TestByteStreamToStream(tests.TestCase):
123
def test_repeated_substreams_same_kind_are_one_stream(self):
124
# Make a stream - an iterable of bytestrings.
126
('text', [versionedfile.FulltextContentFactory((b'k1',), None,
128
('text', [versionedfile.FulltextContentFactory((b'k2',), None,
130
fmt = controldir.format_registry.get('pack-0.92')().repository_format
131
bytes = smart_repo._stream_to_byte_stream(stream, fmt)
133
# Iterate the resulting iterable; checking that we get only one stream
135
fmt, stream = smart_repo._byte_stream_to_stream(bytes)
136
for kind, substream in stream:
137
streams.append((kind, list(substream)))
138
self.assertLength(1, streams)
139
self.assertLength(2, streams[0][1])
142
class TestSmartServerResponse(tests.TestCase):
144
def test__eq__(self):
145
self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
146
smart_req.SmartServerResponse((b'ok', )))
147
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'body'),
148
smart_req.SmartServerResponse((b'ok', ), b'body'))
149
self.assertNotEqual(smart_req.SmartServerResponse((b'ok', )),
150
smart_req.SmartServerResponse((b'notok', )))
151
self.assertNotEqual(smart_req.SmartServerResponse((b'ok', ), b'body'),
152
smart_req.SmartServerResponse((b'ok', )))
153
self.assertNotEqual(None,
154
smart_req.SmartServerResponse((b'ok', )))
156
def test__str__(self):
157
"""SmartServerResponses can be stringified."""
159
str(smart_req.SuccessfulSmartServerResponse((b'args',), b'body')),
160
("<SuccessfulSmartServerResponse args=(b'args',) body=b'body'>",
161
"<SuccessfulSmartServerResponse args=('args',) body='body'>"))
163
str(smart_req.FailedSmartServerResponse((b'args',), b'body')),
164
("<FailedSmartServerResponse args=(b'args',) body=b'body'>",
165
"<FailedSmartServerResponse args=('args',) body='body'>"))
168
class TestSmartServerRequest(tests.TestCaseWithMemoryTransport):
170
def test_translate_client_path(self):
171
transport = self.get_transport()
172
request = smart_req.SmartServerRequest(transport, 'foo/')
173
self.assertEqual('./', request.translate_client_path(b'foo/'))
175
urlutils.InvalidURLJoin, request.translate_client_path, b'foo/..')
177
errors.PathNotChild, request.translate_client_path, b'/')
179
errors.PathNotChild, request.translate_client_path, b'bar/')
180
self.assertEqual('./baz', request.translate_client_path(b'foo/baz'))
181
e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'
183
u'./' + urlutils.escape(e_acute),
184
request.translate_client_path(b'foo/' + e_acute.encode('utf-8')))
186
def test_translate_client_path_vfs(self):
187
"""VfsRequests receive escaped paths rather than raw UTF-8."""
188
transport = self.get_transport()
189
request = vfs.VfsRequest(transport, 'foo/')
190
e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'
191
escaped = urlutils.escape(u'foo/' + e_acute)
193
'./' + urlutils.escape(e_acute),
194
request.translate_client_path(escaped.encode('ascii')))
196
def test_transport_from_client_path(self):
197
transport = self.get_transport()
198
request = smart_req.SmartServerRequest(transport, 'foo/')
201
request.transport_from_client_path(b'foo/').base)
204
class TestSmartServerBzrDirRequestCloningMetaDir(
205
tests.TestCaseWithMemoryTransport):
206
"""Tests for BzrDir.cloning_metadir."""
208
def test_cloning_metadir(self):
209
"""When there is a bzrdir present, the call succeeds."""
210
backing = self.get_transport()
211
dir = self.make_controldir('.')
212
local_result = dir.cloning_metadir()
213
request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
214
request = request_class(backing)
215
expected = smart_req.SuccessfulSmartServerResponse(
216
(local_result.network_name(),
217
local_result.repository_format.network_name(),
218
(b'branch', local_result.get_branch_format().network_name())))
219
self.assertEqual(expected, request.execute(b'', b'False'))
221
def test_cloning_metadir_reference(self):
222
"""The request fails when bzrdir contains a branch reference."""
223
backing = self.get_transport()
224
referenced_branch = self.make_branch('referenced')
225
dir = self.make_controldir('.')
226
dir.cloning_metadir()
227
_mod_bzrbranch.BranchReferenceFormat().initialize(
228
dir, target_branch=referenced_branch)
229
_mod_bzrbranch.BranchReferenceFormat().get_reference(dir)
230
# The server shouldn't try to follow the branch reference, so it's fine
231
# if the referenced branch isn't reachable.
232
backing.rename('referenced', 'moved')
233
request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
234
request = request_class(backing)
235
expected = smart_req.FailedSmartServerResponse((b'BranchReference',))
236
self.assertEqual(expected, request.execute(b'', b'False'))
239
class TestSmartServerBzrDirRequestCheckoutMetaDir(
240
tests.TestCaseWithMemoryTransport):
241
"""Tests for BzrDir.checkout_metadir."""
243
def test_checkout_metadir(self):
244
backing = self.get_transport()
245
request = smart_dir.SmartServerBzrDirRequestCheckoutMetaDir(
247
self.make_branch('.', format='2a')
248
response = request.execute(b'')
250
smart_req.SmartServerResponse(
251
(b'Bazaar-NG meta directory, format 1\n',
252
b'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
253
b'Bazaar Branch Format 7 (needs bzr 1.6)\n')),
257
class TestSmartServerBzrDirRequestDestroyBranch(
258
tests.TestCaseWithMemoryTransport):
259
"""Tests for BzrDir.destroy_branch."""
261
def test_destroy_branch_default(self):
262
"""The default branch can be removed."""
263
backing = self.get_transport()
264
self.make_branch('.')
265
request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
266
request = request_class(backing)
267
expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
268
self.assertEqual(expected, request.execute(b'', None))
270
def test_destroy_branch_named(self):
271
"""A named branch can be removed."""
272
backing = self.get_transport()
273
dir = self.make_repository('.', format="development-colo").controldir
274
dir.create_branch(name="branchname")
275
request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
276
request = request_class(backing)
277
expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
278
self.assertEqual(expected, request.execute(b'', b"branchname"))
280
def test_destroy_branch_missing(self):
281
"""An error is raised if the branch didn't exist."""
282
backing = self.get_transport()
283
self.make_controldir('.', format="development-colo")
284
request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
285
request = request_class(backing)
286
expected = smart_req.FailedSmartServerResponse((b'nobranch',), None)
287
self.assertEqual(expected, request.execute(b'', b"branchname"))
290
class TestSmartServerBzrDirRequestHasWorkingTree(
291
tests.TestCaseWithTransport):
292
"""Tests for BzrDir.has_workingtree."""
294
def test_has_workingtree_yes(self):
295
"""A working tree is present."""
296
backing = self.get_transport()
297
self.make_branch_and_tree('.')
298
request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
299
request = request_class(backing)
300
expected = smart_req.SuccessfulSmartServerResponse((b'yes',))
301
self.assertEqual(expected, request.execute(b''))
303
def test_has_workingtree_no(self):
304
"""A working tree is missing."""
305
backing = self.get_transport()
306
self.make_controldir('.')
307
request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
308
request = request_class(backing)
309
expected = smart_req.SuccessfulSmartServerResponse((b'no',))
310
self.assertEqual(expected, request.execute(b''))
313
class TestSmartServerBzrDirRequestDestroyRepository(
314
tests.TestCaseWithMemoryTransport):
315
"""Tests for BzrDir.destroy_repository."""
317
def test_destroy_repository_default(self):
318
"""The repository can be removed."""
319
backing = self.get_transport()
320
self.make_repository('.')
321
request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
322
request = request_class(backing)
323
expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
324
self.assertEqual(expected, request.execute(b''))
326
def test_destroy_repository_missing(self):
327
"""An error is raised if the repository didn't exist."""
328
backing = self.get_transport()
329
self.make_controldir('.')
330
request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
331
request = request_class(backing)
332
expected = smart_req.FailedSmartServerResponse(
333
(b'norepository',), None)
334
self.assertEqual(expected, request.execute(b''))
337
class TestSmartServerRequestCreateRepository(
338
tests.TestCaseWithMemoryTransport):
339
"""Tests for BzrDir.create_repository."""
341
def test_makes_repository(self):
342
"""When there is a bzrdir present, the call succeeds."""
343
backing = self.get_transport()
344
self.make_controldir('.')
345
request_class = smart_dir.SmartServerRequestCreateRepository
346
request = request_class(backing)
347
reference_bzrdir_format = controldir.format_registry.get('pack-0.92')()
348
reference_format = reference_bzrdir_format.repository_format
349
network_name = reference_format.network_name()
350
expected = smart_req.SuccessfulSmartServerResponse(
351
(b'ok', b'no', b'no', b'no', network_name))
352
self.assertEqual(expected, request.execute(b'', network_name, b'True'))
355
class TestSmartServerRequestFindRepository(tests.TestCaseWithMemoryTransport):
356
"""Tests for BzrDir.find_repository."""
358
def test_no_repository(self):
359
"""If no repository is found, ('norepository', ) is returned."""
360
backing = self.get_transport()
361
request = self._request_class(backing)
362
self.make_controldir('.')
363
self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
364
request.execute(b''))
366
def test_nonshared_repository(self):
367
# nonshared repositorys only allow 'find' to return a handle when the
368
# path the repository is being searched on is the same as that that
369
# the repository is at.
370
backing = self.get_transport()
371
request = self._request_class(backing)
372
result = self._make_repository_and_result()
373
self.assertEqual(result, request.execute(b''))
374
self.make_controldir('subdir')
375
self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
376
request.execute(b'subdir'))
378
def _make_repository_and_result(self, shared=False, format=None):
379
"""Convenience function to setup a repository.
381
:result: The SmartServerResponse to expect when opening it.
383
repo = self.make_repository('.', shared=shared, format=format)
384
if repo.supports_rich_root():
388
if repo._format.supports_tree_reference:
392
if repo._format.supports_external_lookups:
396
if (smart_dir.SmartServerRequestFindRepositoryV3 ==
397
self._request_class):
398
return smart_req.SuccessfulSmartServerResponse(
399
(b'ok', b'', rich_root, subtrees, external,
400
repo._format.network_name()))
401
elif (smart_dir.SmartServerRequestFindRepositoryV2 ==
402
self._request_class):
403
# All tests so far are on formats, and for non-external
405
return smart_req.SuccessfulSmartServerResponse(
406
(b'ok', b'', rich_root, subtrees, external))
408
return smart_req.SuccessfulSmartServerResponse(
409
(b'ok', b'', rich_root, subtrees))
411
def test_shared_repository(self):
412
"""for a shared repository, we get 'ok', 'relpath-to-repo'."""
413
backing = self.get_transport()
414
request = self._request_class(backing)
415
result = self._make_repository_and_result(shared=True)
416
self.assertEqual(result, request.execute(b''))
417
self.make_controldir('subdir')
418
result2 = smart_req.SmartServerResponse(
419
result.args[0:1] + (b'..', ) + result.args[2:])
420
self.assertEqual(result2,
421
request.execute(b'subdir'))
422
self.make_controldir('subdir/deeper')
423
result3 = smart_req.SmartServerResponse(
424
result.args[0:1] + (b'../..', ) + result.args[2:])
425
self.assertEqual(result3,
426
request.execute(b'subdir/deeper'))
428
def test_rich_root_and_subtree_encoding(self):
429
"""Test for the format attributes for rich root and subtree support."""
430
backing = self.get_transport()
431
request = self._request_class(backing)
432
result = self._make_repository_and_result(
433
format='development-subtree')
434
# check the test will be valid
435
self.assertEqual(b'yes', result.args[2])
436
self.assertEqual(b'yes', result.args[3])
437
self.assertEqual(result, request.execute(b''))
439
def test_supports_external_lookups_no_v2(self):
440
"""Test for the supports_external_lookups attribute."""
441
backing = self.get_transport()
442
request = self._request_class(backing)
443
result = self._make_repository_and_result(
444
format='development-subtree')
445
# check the test will be valid
446
self.assertEqual(b'yes', result.args[4])
447
self.assertEqual(result, request.execute(b''))
450
class TestSmartServerBzrDirRequestGetConfigFile(
451
tests.TestCaseWithMemoryTransport):
452
"""Tests for BzrDir.get_config_file."""
454
def test_present(self):
455
backing = self.get_transport()
456
dir = self.make_controldir('.')
457
dir.get_config().set_default_stack_on("/")
458
local_result = dir._get_config()._get_config_file().read()
459
request_class = smart_dir.SmartServerBzrDirRequestConfigFile
460
request = request_class(backing)
461
expected = smart_req.SuccessfulSmartServerResponse((), local_result)
462
self.assertEqual(expected, request.execute(b''))
464
def test_missing(self):
465
backing = self.get_transport()
466
self.make_controldir('.')
467
request_class = smart_dir.SmartServerBzrDirRequestConfigFile
468
request = request_class(backing)
469
expected = smart_req.SuccessfulSmartServerResponse((), b'')
470
self.assertEqual(expected, request.execute(b''))
473
class TestSmartServerBzrDirRequestGetBranches(
474
tests.TestCaseWithMemoryTransport):
475
"""Tests for BzrDir.get_branches."""
477
def test_simple(self):
478
backing = self.get_transport()
479
branch = self.make_branch('.')
480
request_class = smart_dir.SmartServerBzrDirRequestGetBranches
481
request = request_class(backing)
482
local_result = bencode.bencode(
483
{b"": (b"branch", branch._format.network_name())})
484
expected = smart_req.SuccessfulSmartServerResponse(
485
(b"success", ), local_result)
486
self.assertEqual(expected, request.execute(b''))
489
backing = self.get_transport()
490
dir = self.make_controldir('foo')
491
b = self.make_branch('bar')
492
dir.set_branch_reference(b)
493
request_class = smart_dir.SmartServerBzrDirRequestGetBranches
494
request = request_class(backing)
495
local_result = bencode.bencode(
496
{b"": (b"ref", b'../bar/')})
497
expected = smart_req.SuccessfulSmartServerResponse(
498
(b"success", ), local_result)
499
self.assertEqual(expected, request.execute(b'foo'))
501
def test_empty(self):
502
backing = self.get_transport()
503
self.make_controldir('.')
504
request_class = smart_dir.SmartServerBzrDirRequestGetBranches
505
request = request_class(backing)
506
local_result = bencode.bencode({})
507
expected = smart_req.SuccessfulSmartServerResponse(
508
(b'success',), local_result)
509
self.assertEqual(expected, request.execute(b''))
512
class TestSmartServerRequestInitializeBzrDir(
513
tests.TestCaseWithMemoryTransport):
515
def test_empty_dir(self):
516
"""Initializing an empty dir should succeed and do it."""
517
backing = self.get_transport()
518
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
519
self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
520
request.execute(b''))
521
made_dir = controldir.ControlDir.open_from_transport(backing)
522
# no branch, tree or repository is expected with the current
524
self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
525
self.assertRaises(errors.NotBranchError, made_dir.open_branch)
526
self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
528
def test_missing_dir(self):
529
"""Initializing a missing directory should fail like the bzrdir api."""
530
backing = self.get_transport()
531
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
532
self.assertRaises(errors.NoSuchFile,
533
request.execute, b'subdir')
535
def test_initialized_dir(self):
536
"""Initializing an extant bzrdir should fail like the bzrdir api."""
537
backing = self.get_transport()
538
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
539
self.make_controldir('subdir')
540
self.assertRaises(errors.AlreadyControlDirError,
541
request.execute, b'subdir')
544
class TestSmartServerRequestBzrDirInitializeEx(
545
tests.TestCaseWithMemoryTransport):
546
"""Basic tests for BzrDir.initialize_ex_1.16 in the smart server.
548
The main unit tests in test_bzrdir exercise the API comprehensively.
551
def test_empty_dir(self):
552
"""Initializing an empty dir should succeed and do it."""
553
backing = self.get_transport()
554
name = self.make_controldir('reference')._format.network_name()
555
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
557
smart_req.SmartServerResponse((b'', b'', b'', b'', b'', b'', name,
558
b'False', b'', b'', b'')),
559
request.execute(name, b'', b'True', b'False', b'False', b'', b'',
561
made_dir = controldir.ControlDir.open_from_transport(backing)
562
# no branch, tree or repository is expected with the current
564
self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
565
self.assertRaises(errors.NotBranchError, made_dir.open_branch)
566
self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
568
def test_missing_dir(self):
569
"""Initializing a missing directory should fail like the bzrdir api."""
570
backing = self.get_transport()
571
name = self.make_controldir('reference')._format.network_name()
572
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
573
self.assertRaises(errors.NoSuchFile, request.execute, name,
574
b'subdir/dir', b'False', b'False', b'False', b'',
575
b'', b'', b'', b'False')
577
def test_initialized_dir(self):
578
"""Initializing an extant directory should fail like the bzrdir api."""
579
backing = self.get_transport()
580
name = self.make_controldir('reference')._format.network_name()
581
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
582
self.make_controldir('subdir')
583
self.assertRaises(errors.FileExists, request.execute, name, b'subdir',
584
b'False', b'False', b'False', b'', b'', b'', b'',
588
class TestSmartServerRequestOpenBzrDir(tests.TestCaseWithMemoryTransport):
590
def test_no_directory(self):
591
backing = self.get_transport()
592
request = smart_dir.SmartServerRequestOpenBzrDir(backing)
593
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
594
request.execute(b'does-not-exist'))
596
def test_empty_directory(self):
597
backing = self.get_transport()
598
backing.mkdir('empty')
599
request = smart_dir.SmartServerRequestOpenBzrDir(backing)
600
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
601
request.execute(b'empty'))
603
def test_outside_root_client_path(self):
604
backing = self.get_transport()
605
request = smart_dir.SmartServerRequestOpenBzrDir(
606
backing, root_client_path='root')
607
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
608
request.execute(b'not-root'))
611
class TestSmartServerRequestOpenBzrDir_2_1(tests.TestCaseWithMemoryTransport):
613
def test_no_directory(self):
614
backing = self.get_transport()
615
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
616
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
617
request.execute(b'does-not-exist'))
619
def test_empty_directory(self):
620
backing = self.get_transport()
621
backing.mkdir('empty')
622
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
623
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
624
request.execute(b'empty'))
626
def test_present_without_workingtree(self):
627
backing = self.get_transport()
628
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
629
self.make_controldir('.')
630
self.assertEqual(smart_req.SmartServerResponse((b'yes', b'no')),
631
request.execute(b''))
633
def test_outside_root_client_path(self):
634
backing = self.get_transport()
635
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(
636
backing, root_client_path='root')
637
self.assertEqual(smart_req.SmartServerResponse((b'no',)),
638
request.execute(b'not-root'))
641
class TestSmartServerRequestOpenBzrDir_2_1_disk(TestCaseWithChrootedTransport):
643
def test_present_with_workingtree(self):
644
self.vfs_transport_factory = test_server.LocalURLServer
645
backing = self.get_transport()
646
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
647
bd = self.make_controldir('.')
648
bd.create_repository()
650
bd.create_workingtree()
651
self.assertEqual(smart_req.SmartServerResponse((b'yes', b'yes')),
652
request.execute(b''))
655
class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
657
def test_no_branch(self):
658
"""When there is no branch, ('nobranch', ) is returned."""
659
backing = self.get_transport()
660
request = smart_dir.SmartServerRequestOpenBranch(backing)
661
self.make_controldir('.')
662
self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
663
request.execute(b''))
665
def test_branch(self):
666
"""When there is a branch, 'ok' is returned."""
667
backing = self.get_transport()
668
request = smart_dir.SmartServerRequestOpenBranch(backing)
669
self.make_branch('.')
670
self.assertEqual(smart_req.SmartServerResponse((b'ok', b'')),
671
request.execute(b''))
673
def test_branch_reference(self):
674
"""When there is a branch reference, the reference URL is returned."""
675
self.vfs_transport_factory = test_server.LocalURLServer
676
backing = self.get_transport()
677
request = smart_dir.SmartServerRequestOpenBranch(backing)
678
branch = self.make_branch('branch')
679
checkout = branch.create_checkout('reference', lightweight=True)
680
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
681
checkout.controldir).encode('utf-8')
682
self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
683
self.assertEqual(smart_req.SmartServerResponse((b'ok', reference_url)),
684
request.execute(b'reference'))
686
def test_notification_on_branch_from_repository(self):
687
"""When there is a repository, the error should return details."""
688
backing = self.get_transport()
689
request = smart_dir.SmartServerRequestOpenBranch(backing)
690
self.make_repository('.')
691
self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
692
request.execute(b''))
695
class TestSmartServerRequestOpenBranchV2(TestCaseWithChrootedTransport):
697
def test_no_branch(self):
698
"""When there is no branch, ('nobranch', ) is returned."""
699
backing = self.get_transport()
700
self.make_controldir('.')
701
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
702
self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
703
request.execute(b''))
705
def test_branch(self):
706
"""When there is a branch, 'ok' is returned."""
707
backing = self.get_transport()
708
expected = self.make_branch('.')._format.network_name()
709
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
710
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
711
(b'branch', expected)),
712
request.execute(b''))
714
def test_branch_reference(self):
715
"""When there is a branch reference, the reference URL is returned."""
716
self.vfs_transport_factory = test_server.LocalURLServer
717
backing = self.get_transport()
718
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
719
branch = self.make_branch('branch')
720
checkout = branch.create_checkout('reference', lightweight=True)
721
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
722
checkout.controldir).encode('utf-8')
723
self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
724
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
725
(b'ref', reference_url)),
726
request.execute(b'reference'))
728
def test_stacked_branch(self):
729
"""Opening a stacked branch does not open the stacked-on branch."""
730
trunk = self.make_branch('trunk')
731
feature = self.make_branch('feature')
732
feature.set_stacked_on_url(trunk.base)
734
_mod_branch.Branch.hooks.install_named_hook(
735
'open', opened_branches.append, None)
736
backing = self.get_transport()
737
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
740
response = request.execute(b'feature')
742
request.teardown_jail()
743
expected_format = feature._format.network_name()
744
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
745
(b'branch', expected_format)),
747
self.assertLength(1, opened_branches)
749
def test_notification_on_branch_from_repository(self):
750
"""When there is a repository, the error should return details."""
751
backing = self.get_transport()
752
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
753
self.make_repository('.')
754
self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
755
request.execute(b''))
758
class TestSmartServerRequestOpenBranchV3(TestCaseWithChrootedTransport):
760
def test_no_branch(self):
761
"""When there is no branch, ('nobranch', ) is returned."""
762
backing = self.get_transport()
763
self.make_controldir('.')
764
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
765
self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
766
request.execute(b''))
768
def test_branch(self):
769
"""When there is a branch, 'ok' is returned."""
770
backing = self.get_transport()
771
expected = self.make_branch('.')._format.network_name()
772
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
773
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
774
(b'branch', expected)),
775
request.execute(b''))
777
def test_branch_reference(self):
778
"""When there is a branch reference, the reference URL is returned."""
779
self.vfs_transport_factory = test_server.LocalURLServer
780
backing = self.get_transport()
781
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
782
branch = self.make_branch('branch')
783
checkout = branch.create_checkout('reference', lightweight=True)
784
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
785
checkout.controldir).encode('utf-8')
786
self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
787
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
788
(b'ref', reference_url)),
789
request.execute(b'reference'))
791
def test_stacked_branch(self):
792
"""Opening a stacked branch does not open the stacked-on branch."""
793
trunk = self.make_branch('trunk')
794
feature = self.make_branch('feature')
795
feature.set_stacked_on_url(trunk.base)
797
_mod_branch.Branch.hooks.install_named_hook(
798
'open', opened_branches.append, None)
799
backing = self.get_transport()
800
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
803
response = request.execute(b'feature')
805
request.teardown_jail()
806
expected_format = feature._format.network_name()
807
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
808
(b'branch', expected_format)),
810
self.assertLength(1, opened_branches)
812
def test_notification_on_branch_from_repository(self):
813
"""When there is a repository, the error should return details."""
814
backing = self.get_transport()
815
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
816
self.make_repository('.')
817
self.assertEqual(smart_req.SmartServerResponse(
818
(b'nobranch', b'location is a repository')),
819
request.execute(b''))
822
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
824
def test_empty(self):
825
"""For an empty branch, the body is empty."""
826
backing = self.get_transport()
827
request = smart_branch.SmartServerRequestRevisionHistory(backing)
828
self.make_branch('.')
829
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b''),
830
request.execute(b''))
832
def test_not_empty(self):
833
"""For a non-empty branch, the body is empty."""
834
backing = self.get_transport()
835
request = smart_branch.SmartServerRequestRevisionHistory(backing)
836
tree = self.make_branch_and_memory_tree('.')
839
r1 = tree.commit('1st commit')
840
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
843
smart_req.SmartServerResponse((b'ok', ), (b'\x00'.join([r1, r2]))),
844
request.execute(b''))
847
class TestSmartServerBranchRequest(tests.TestCaseWithMemoryTransport):
849
def test_no_branch(self):
850
"""When there is a bzrdir and no branch, NotBranchError is raised."""
851
backing = self.get_transport()
852
request = smart_branch.SmartServerBranchRequest(backing)
853
self.make_controldir('.')
854
self.assertRaises(errors.NotBranchError,
855
request.execute, b'')
857
def test_branch_reference(self):
858
"""When there is a branch reference, NotBranchError is raised."""
859
backing = self.get_transport()
860
request = smart_branch.SmartServerBranchRequest(backing)
861
branch = self.make_branch('branch')
862
branch.create_checkout('reference', lightweight=True)
863
self.assertRaises(errors.NotBranchError,
864
request.execute, b'checkout')
867
class TestSmartServerBranchRequestLastRevisionInfo(
868
tests.TestCaseWithMemoryTransport):
870
def test_empty(self):
871
"""For an empty branch, the result is ('ok', '0', b'null:')."""
872
backing = self.get_transport()
873
request = smart_branch.SmartServerBranchRequestLastRevisionInfo(
875
self.make_branch('.')
877
smart_req.SmartServerResponse((b'ok', b'0', b'null:')),
878
request.execute(b''))
880
def test_ghost(self):
881
"""For an empty branch, the result is ('ok', '0', b'null:')."""
882
backing = self.get_transport()
883
request = smart_branch.SmartServerBranchRequestLastRevisionInfo(
885
branch = self.make_branch('.')
887
def last_revision_info():
888
raise errors.GhostRevisionsHaveNoRevno(b'revid1', b'revid2')
889
self.overrideAttr(branch, 'last_revision_info', last_revision_info)
890
self.assertRaises(errors.GhostRevisionsHaveNoRevno,
891
request.do_with_branch, branch)
893
def test_not_empty(self):
894
"""For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
895
backing = self.get_transport()
896
request = smart_branch.SmartServerBranchRequestLastRevisionInfo(
898
tree = self.make_branch_and_memory_tree('.')
901
rev_id_utf8 = u'\xc8'.encode('utf-8')
902
tree.commit('1st commit')
903
tree.commit('2nd commit', rev_id=rev_id_utf8)
906
smart_req.SmartServerResponse((b'ok', b'2', rev_id_utf8)),
907
request.execute(b''))
910
class TestSmartServerBranchRequestRevisionIdToRevno(
911
tests.TestCaseWithMemoryTransport):
914
backing = self.get_transport()
915
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
917
self.make_branch('.')
918
self.assertEqual(smart_req.SmartServerResponse((b'ok', b'0')),
919
request.execute(b'', b'null:'))
921
def test_ghost_revision(self):
922
backing = self.get_transport()
923
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
925
branch = self.make_branch('.')
926
def revision_id_to_dotted_revno(revid):
927
raise errors.GhostRevisionsHaveNoRevno(revid, b'ghost-revid')
928
self.overrideAttr(branch, 'revision_id_to_dotted_revno', revision_id_to_dotted_revno)
930
smart_req.FailedSmartServerResponse(
931
(b'GhostRevisionsHaveNoRevno', b'revid', b'ghost-revid')),
932
request.do_with_branch(branch, b'revid'))
934
def test_simple(self):
935
backing = self.get_transport()
936
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
938
tree = self.make_branch_and_memory_tree('.')
941
r1 = tree.commit('1st commit')
944
smart_req.SmartServerResponse((b'ok', b'1')),
945
request.execute(b'', r1))
947
def test_not_found(self):
948
backing = self.get_transport()
949
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
951
self.make_branch('.')
953
smart_req.FailedSmartServerResponse(
954
(b'NoSuchRevision', b'idontexist')),
955
request.execute(b'', b'idontexist'))
958
class TestSmartServerBranchRequestGetConfigFile(
959
tests.TestCaseWithMemoryTransport):
961
def test_default(self):
962
"""With no file, we get empty content."""
963
backing = self.get_transport()
964
request = smart_branch.SmartServerBranchGetConfigFile(backing)
965
self.make_branch('.')
966
# there should be no file by default
968
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), content),
969
request.execute(b''))
971
def test_with_content(self):
972
# SmartServerBranchGetConfigFile should return the content from
973
# branch.control_files.get('branch.conf') for now - in the future it
974
# may perform more complex processing.
975
backing = self.get_transport()
976
request = smart_branch.SmartServerBranchGetConfigFile(backing)
977
branch = self.make_branch('.')
978
branch._transport.put_bytes('branch.conf', b'foo bar baz')
980
smart_req.SmartServerResponse((b'ok', ), b'foo bar baz'),
981
request.execute(b''))
984
class TestLockedBranch(tests.TestCaseWithMemoryTransport):
986
def get_lock_tokens(self, branch):
987
branch_token = branch.lock_write().token
988
repo_token = branch.repository.lock_write().repository_token
989
branch.repository.unlock()
990
return branch_token, repo_token
993
class TestSmartServerBranchRequestPutConfigFile(TestLockedBranch):
995
def test_with_content(self):
996
backing = self.get_transport()
997
request = smart_branch.SmartServerBranchPutConfigFile(backing)
998
branch = self.make_branch('.')
999
branch_token, repo_token = self.get_lock_tokens(branch)
1000
self.assertIs(None, request.execute(b'', branch_token, repo_token))
1002
smart_req.SmartServerResponse((b'ok', )),
1003
request.do_body(b'foo bar baz'))
1005
branch.control_transport.get_bytes('branch.conf'),
1010
class TestSmartServerBranchRequestSetConfigOption(TestLockedBranch):
1012
def test_value_name(self):
1013
branch = self.make_branch('.')
1014
request = smart_branch.SmartServerBranchRequestSetConfigOption(
1015
branch.controldir.root_transport)
1016
branch_token, repo_token = self.get_lock_tokens(branch)
1017
config = branch._get_config()
1018
result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
1020
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1021
self.assertEqual('bar', config.get_option('foo'))
1025
def test_value_name_section(self):
1026
branch = self.make_branch('.')
1027
request = smart_branch.SmartServerBranchRequestSetConfigOption(
1028
branch.controldir.root_transport)
1029
branch_token, repo_token = self.get_lock_tokens(branch)
1030
config = branch._get_config()
1031
result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
1033
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1034
self.assertEqual('bar', config.get_option('foo', 'gam'))
1039
class TestSmartServerBranchRequestSetConfigOptionDict(TestLockedBranch):
1042
TestLockedBranch.setUp(self)
1043
# A dict with non-ascii keys and values to exercise unicode
1045
self.encoded_value_dict = (
1046
b'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde')
1048
'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
1050
def test_value_name(self):
1051
branch = self.make_branch('.')
1052
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
1053
branch.controldir.root_transport)
1054
branch_token, repo_token = self.get_lock_tokens(branch)
1055
config = branch._get_config()
1056
result = request.execute(b'', branch_token, repo_token,
1057
self.encoded_value_dict, b'foo', b'')
1058
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1059
self.assertEqual(self.value_dict, config.get_option('foo'))
1063
def test_value_name_section(self):
1064
branch = self.make_branch('.')
1065
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
1066
branch.controldir.root_transport)
1067
branch_token, repo_token = self.get_lock_tokens(branch)
1068
config = branch._get_config()
1069
result = request.execute(b'', branch_token, repo_token,
1070
self.encoded_value_dict, b'foo', b'gam')
1071
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1072
self.assertEqual(self.value_dict, config.get_option('foo', 'gam'))
1077
class TestSmartServerBranchRequestSetTagsBytes(TestLockedBranch):
1078
# Only called when the branch format and tags match [yay factory
1079
# methods] so only need to test straight forward cases.
1081
def test_set_bytes(self):
1082
base_branch = self.make_branch('base')
1083
tag_bytes = base_branch._get_tags_bytes()
1084
# get_lock_tokens takes out a lock.
1085
branch_token, repo_token = self.get_lock_tokens(base_branch)
1086
request = smart_branch.SmartServerBranchSetTagsBytes(
1087
self.get_transport())
1088
response = request.execute(b'base', branch_token, repo_token)
1089
self.assertEqual(None, response)
1090
response = request.do_chunk(tag_bytes)
1091
self.assertEqual(None, response)
1092
response = request.do_end()
1094
smart_req.SuccessfulSmartServerResponse(()), response)
1095
base_branch.unlock()
1097
def test_lock_failed(self):
1098
base_branch = self.make_branch('base')
1099
base_branch.lock_write()
1100
tag_bytes = base_branch._get_tags_bytes()
1101
request = smart_branch.SmartServerBranchSetTagsBytes(
1102
self.get_transport())
1103
self.assertRaises(errors.TokenMismatch, request.execute,
1104
b'base', b'wrong token', b'wrong token')
1105
# The request handler will keep processing the message parts, so even
1106
# if the request fails immediately do_chunk and do_end are still
1108
request.do_chunk(tag_bytes)
1110
base_branch.unlock()
1113
class SetLastRevisionTestBase(TestLockedBranch):
1114
"""Base test case for verbs that implement set_last_revision."""
1117
super(SetLastRevisionTestBase, self).setUp()
1118
backing_transport = self.get_transport()
1119
self.request = self.request_class(backing_transport)
1120
self.tree = self.make_branch_and_memory_tree('.')
1122
def lock_branch(self):
1123
return self.get_lock_tokens(self.tree.branch)
1125
def unlock_branch(self):
1126
self.tree.branch.unlock()
1128
def set_last_revision(self, revision_id, revno):
1129
branch_token, repo_token = self.lock_branch()
1130
response = self._set_last_revision(
1131
revision_id, revno, branch_token, repo_token)
1132
self.unlock_branch()
1135
def assertRequestSucceeds(self, revision_id, revno):
1136
response = self.set_last_revision(revision_id, revno)
1137
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
1141
class TestSetLastRevisionVerbMixin(object):
1142
"""Mixin test case for verbs that implement set_last_revision."""
1144
def test_set_null_to_null(self):
1145
"""An empty branch can have its last revision set to b'null:'."""
1146
self.assertRequestSucceeds(b'null:', 0)
1148
def test_NoSuchRevision(self):
1149
"""If the revision_id is not present, the verb returns NoSuchRevision.
1151
revision_id = b'non-existent revision'
1153
smart_req.FailedSmartServerResponse(
1154
(b'NoSuchRevision', revision_id)),
1155
self.set_last_revision(revision_id, 1))
1157
def make_tree_with_two_commits(self):
1158
self.tree.lock_write()
1160
rev_id_utf8 = u'\xc8'.encode('utf-8')
1161
self.tree.commit('1st commit', rev_id=rev_id_utf8)
1162
self.tree.commit('2nd commit', rev_id=b'rev-2')
1165
def test_branch_last_revision_info_is_updated(self):
1166
"""A branch's tip can be set to a revision that is present in its
1169
# Make a branch with an empty revision history, but two revisions in
1171
self.make_tree_with_two_commits()
1172
rev_id_utf8 = u'\xc8'.encode('utf-8')
1173
self.tree.branch.set_last_revision_info(0, b'null:')
1175
(0, b'null:'), self.tree.branch.last_revision_info())
1176
# We can update the branch to a revision that is present in the
1178
self.assertRequestSucceeds(rev_id_utf8, 1)
1180
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1182
def test_branch_last_revision_info_rewind(self):
1183
"""A branch's tip can be set to a revision that is an ancestor of the
1186
self.make_tree_with_two_commits()
1187
rev_id_utf8 = u'\xc8'.encode('utf-8')
1189
(2, b'rev-2'), self.tree.branch.last_revision_info())
1190
self.assertRequestSucceeds(rev_id_utf8, 1)
1192
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1194
def test_TipChangeRejected(self):
1195
"""If a pre_change_branch_tip hook raises TipChangeRejected, the verb
1196
returns TipChangeRejected.
1198
rejection_message = u'rejection message\N{INTERROBANG}'
1200
def hook_that_rejects(params):
1201
raise errors.TipChangeRejected(rejection_message)
1202
_mod_branch.Branch.hooks.install_named_hook(
1203
'pre_change_branch_tip', hook_that_rejects, None)
1205
smart_req.FailedSmartServerResponse(
1206
(b'TipChangeRejected', rejection_message.encode('utf-8'))),
1207
self.set_last_revision(b'null:', 0))
1210
class TestSmartServerBranchRequestSetLastRevision(
1211
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1212
"""Tests for Branch.set_last_revision verb."""
1214
request_class = smart_branch.SmartServerBranchRequestSetLastRevision
1216
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1217
return self.request.execute(
1218
b'', branch_token, repo_token, revision_id)
1221
class TestSmartServerBranchRequestSetLastRevisionInfo(
1222
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1223
"""Tests for Branch.set_last_revision_info verb."""
1225
request_class = smart_branch.SmartServerBranchRequestSetLastRevisionInfo
1227
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1228
return self.request.execute(
1229
b'', branch_token, repo_token, revno, revision_id)
1231
def test_NoSuchRevision(self):
1232
"""Branch.set_last_revision_info does not have to return
1233
NoSuchRevision if the revision_id is absent.
1235
raise tests.TestNotApplicable()
1238
class TestSmartServerBranchRequestSetLastRevisionEx(
1239
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1240
"""Tests for Branch.set_last_revision_ex verb."""
1242
request_class = smart_branch.SmartServerBranchRequestSetLastRevisionEx
1244
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1245
return self.request.execute(
1246
b'', branch_token, repo_token, revision_id, 0, 0)
1248
def assertRequestSucceeds(self, revision_id, revno):
1249
response = self.set_last_revision(revision_id, revno)
1251
smart_req.SuccessfulSmartServerResponse(
1252
(b'ok', revno, revision_id)),
1255
def test_branch_last_revision_info_rewind(self):
1256
"""A branch's tip can be set to a revision that is an ancestor of the
1257
current tip, but only if allow_overwrite_descendant is passed.
1259
self.make_tree_with_two_commits()
1260
rev_id_utf8 = u'\xc8'.encode('utf-8')
1262
(2, b'rev-2'), self.tree.branch.last_revision_info())
1263
# If allow_overwrite_descendant flag is 0, then trying to set the tip
1264
# to an older revision ID has no effect.
1265
branch_token, repo_token = self.lock_branch()
1266
response = self.request.execute(
1267
b'', branch_token, repo_token, rev_id_utf8, 0, 0)
1269
smart_req.SuccessfulSmartServerResponse((b'ok', 2, b'rev-2')),
1272
(2, b'rev-2'), self.tree.branch.last_revision_info())
1274
# If allow_overwrite_descendant flag is 1, then setting the tip to an
1276
response = self.request.execute(
1277
b'', branch_token, repo_token, rev_id_utf8, 0, 1)
1279
smart_req.SuccessfulSmartServerResponse((b'ok', 1, rev_id_utf8)),
1281
self.unlock_branch()
1283
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1285
def make_branch_with_divergent_history(self):
1286
"""Make a branch with divergent history in its repo.
1288
The branch's tip will be 'child-2', and the repo will also contain
1289
'child-1', which diverges from a common base revision.
1291
self.tree.lock_write()
1293
self.tree.commit('1st commit')
1294
revno_1, revid_1 = self.tree.branch.last_revision_info()
1295
self.tree.commit('2nd commit', rev_id=b'child-1')
1296
# Undo the second commit
1297
self.tree.branch.set_last_revision_info(revno_1, revid_1)
1298
self.tree.set_parent_ids([revid_1])
1299
# Make a new second commit, child-2. child-2 has diverged from
1301
self.tree.commit('2nd commit', rev_id=b'child-2')
1304
def test_not_allow_diverged(self):
1305
"""If allow_diverged is not passed, then setting a divergent history
1306
returns a Diverged error.
1308
self.make_branch_with_divergent_history()
1310
smart_req.FailedSmartServerResponse((b'Diverged',)),
1311
self.set_last_revision(b'child-1', 2))
1312
# The branch tip was not changed.
1313
self.assertEqual(b'child-2', self.tree.branch.last_revision())
1315
def test_allow_diverged(self):
1316
"""If allow_diverged is passed, then setting a divergent history
1319
self.make_branch_with_divergent_history()
1320
branch_token, repo_token = self.lock_branch()
1321
response = self.request.execute(
1322
b'', branch_token, repo_token, b'child-1', 1, 0)
1324
smart_req.SuccessfulSmartServerResponse((b'ok', 2, b'child-1')),
1326
self.unlock_branch()
1327
# The branch tip was changed.
1328
self.assertEqual(b'child-1', self.tree.branch.last_revision())
1331
class TestSmartServerBranchBreakLock(tests.TestCaseWithMemoryTransport):
1333
def test_lock_to_break(self):
1334
base_branch = self.make_branch('base')
1335
request = smart_branch.SmartServerBranchBreakLock(
1336
self.get_transport())
1337
base_branch.lock_write()
1339
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1340
request.execute(b'base'))
1342
def test_nothing_to_break(self):
1343
self.make_branch('base')
1344
request = smart_branch.SmartServerBranchBreakLock(
1345
self.get_transport())
1347
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1348
request.execute(b'base'))
1351
class TestSmartServerBranchRequestGetParent(tests.TestCaseWithMemoryTransport):
1353
def test_get_parent_none(self):
1354
self.make_branch('base')
1355
request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1356
response = request.execute(b'base')
1358
smart_req.SuccessfulSmartServerResponse((b'',)), response)
1360
def test_get_parent_something(self):
1361
base_branch = self.make_branch('base')
1362
base_branch.set_parent(self.get_url('foo'))
1363
request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1364
response = request.execute(b'base')
1366
smart_req.SuccessfulSmartServerResponse((b"../foo",)),
1370
class TestSmartServerBranchRequestSetParent(TestLockedBranch):
1372
def test_set_parent_none(self):
1373
branch = self.make_branch('base', format="1.9")
1375
branch._set_parent_location('foo')
1377
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1378
self.get_transport())
1379
branch_token, repo_token = self.get_lock_tokens(branch)
1381
response = request.execute(b'base', branch_token, repo_token, b'')
1384
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1385
# Refresh branch as SetParentLocation modified it
1386
branch = branch.controldir.open_branch()
1387
self.assertEqual(None, branch.get_parent())
1389
def test_set_parent_something(self):
1390
branch = self.make_branch('base', format="1.9")
1391
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1392
self.get_transport())
1393
branch_token, repo_token = self.get_lock_tokens(branch)
1395
response = request.execute(b'base', branch_token, repo_token,
1399
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1400
refreshed = _mod_branch.Branch.open(branch.base)
1401
self.assertEqual('http://bar/', refreshed.get_parent())
1404
class TestSmartServerBranchRequestGetTagsBytes(
1405
tests.TestCaseWithMemoryTransport):
1406
# Only called when the branch format and tags match [yay factory
1407
# methods] so only need to test straight forward cases.
1409
def test_get_bytes(self):
1410
self.make_branch('base')
1411
request = smart_branch.SmartServerBranchGetTagsBytes(
1412
self.get_transport())
1413
response = request.execute(b'base')
1415
smart_req.SuccessfulSmartServerResponse((b'',)), response)
1418
class TestSmartServerBranchRequestGetStackedOnURL(
1419
tests.TestCaseWithMemoryTransport):
1421
def test_get_stacked_on_url(self):
1422
self.make_branch('base', format='1.6')
1423
stacked_branch = self.make_branch('stacked', format='1.6')
1424
# typically should be relative
1425
stacked_branch.set_stacked_on_url('../base')
1426
request = smart_branch.SmartServerBranchRequestGetStackedOnURL(
1427
self.get_transport())
1428
response = request.execute(b'stacked')
1430
smart_req.SmartServerResponse((b'ok', b'../base')),
1434
class TestSmartServerBranchRequestLockWrite(TestLockedBranch):
1436
def test_lock_write_on_unlocked_branch(self):
1437
backing = self.get_transport()
1438
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1439
branch = self.make_branch('.', format='knit')
1440
repository = branch.repository
1441
response = request.execute(b'')
1442
branch_nonce = branch.control_files._lock.peek().get('nonce')
1443
repository_nonce = repository.control_files._lock.peek().get('nonce')
1444
self.assertEqual(smart_req.SmartServerResponse(
1445
(b'ok', branch_nonce, repository_nonce)),
1447
# The branch (and associated repository) is now locked. Verify that
1448
# with a new branch object.
1449
new_branch = repository.controldir.open_branch()
1450
self.assertRaises(errors.LockContention, new_branch.lock_write)
1452
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1453
response = request.execute(b'', branch_nonce, repository_nonce)
1455
def test_lock_write_on_locked_branch(self):
1456
backing = self.get_transport()
1457
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1458
branch = self.make_branch('.')
1459
branch_token = branch.lock_write().token
1460
branch.leave_lock_in_place()
1462
response = request.execute(b'')
1464
smart_req.SmartServerResponse((b'LockContention',)), response)
1466
branch.lock_write(branch_token)
1467
branch.dont_leave_lock_in_place()
1470
def test_lock_write_with_tokens_on_locked_branch(self):
1471
backing = self.get_transport()
1472
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1473
branch = self.make_branch('.', format='knit')
1474
branch_token, repo_token = self.get_lock_tokens(branch)
1475
branch.leave_lock_in_place()
1476
branch.repository.leave_lock_in_place()
1478
response = request.execute(b'',
1479
branch_token, repo_token)
1481
smart_req.SmartServerResponse((b'ok', branch_token, repo_token)),
1484
branch.repository.lock_write(repo_token)
1485
branch.repository.dont_leave_lock_in_place()
1486
branch.repository.unlock()
1487
branch.lock_write(branch_token)
1488
branch.dont_leave_lock_in_place()
1491
def test_lock_write_with_mismatched_tokens_on_locked_branch(self):
1492
backing = self.get_transport()
1493
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1494
branch = self.make_branch('.', format='knit')
1495
branch_token, repo_token = self.get_lock_tokens(branch)
1496
branch.leave_lock_in_place()
1497
branch.repository.leave_lock_in_place()
1499
response = request.execute(b'',
1500
branch_token + b'xxx', repo_token)
1502
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1504
branch.repository.lock_write(repo_token)
1505
branch.repository.dont_leave_lock_in_place()
1506
branch.repository.unlock()
1507
branch.lock_write(branch_token)
1508
branch.dont_leave_lock_in_place()
1511
def test_lock_write_on_locked_repo(self):
1512
backing = self.get_transport()
1513
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1514
branch = self.make_branch('.', format='knit')
1515
repo = branch.repository
1516
repo_token = repo.lock_write().repository_token
1517
repo.leave_lock_in_place()
1519
response = request.execute(b'')
1521
smart_req.SmartServerResponse((b'LockContention',)), response)
1523
repo.lock_write(repo_token)
1524
repo.dont_leave_lock_in_place()
1527
def test_lock_write_on_readonly_transport(self):
1528
backing = self.get_readonly_transport()
1529
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1530
self.make_branch('.')
1531
root = self.get_transport().clone('/')
1532
path = urlutils.relative_url(root.base, self.get_transport().base)
1533
response = request.execute(path.encode('utf-8'))
1534
error_name, lock_str, why_str = response.args
1535
self.assertFalse(response.is_successful())
1536
self.assertEqual(b'LockFailed', error_name)
1539
class TestSmartServerBranchRequestGetPhysicalLockStatus(TestLockedBranch):
1541
def test_true(self):
1542
backing = self.get_transport()
1543
request = smart_branch.SmartServerBranchRequestGetPhysicalLockStatus(
1545
branch = self.make_branch('.')
1546
branch_token, repo_token = self.get_lock_tokens(branch)
1547
self.assertEqual(True, branch.get_physical_lock_status())
1548
response = request.execute(b'')
1550
smart_req.SmartServerResponse((b'yes',)), response)
1553
def test_false(self):
1554
backing = self.get_transport()
1555
request = smart_branch.SmartServerBranchRequestGetPhysicalLockStatus(
1557
branch = self.make_branch('.')
1558
self.assertEqual(False, branch.get_physical_lock_status())
1559
response = request.execute(b'')
1561
smart_req.SmartServerResponse((b'no',)), response)
1564
class TestSmartServerBranchRequestUnlock(TestLockedBranch):
1566
def test_unlock_on_locked_branch_and_repo(self):
1567
backing = self.get_transport()
1568
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1569
branch = self.make_branch('.', format='knit')
1571
branch_token, repo_token = self.get_lock_tokens(branch)
1572
# Unlock the branch (and repo) object, leaving the physical locks
1574
branch.leave_lock_in_place()
1575
branch.repository.leave_lock_in_place()
1577
response = request.execute(b'',
1578
branch_token, repo_token)
1580
smart_req.SmartServerResponse((b'ok',)), response)
1581
# The branch is now unlocked. Verify that with a new branch
1583
new_branch = branch.controldir.open_branch()
1584
new_branch.lock_write()
1587
def test_unlock_on_unlocked_branch_unlocked_repo(self):
1588
backing = self.get_transport()
1589
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1590
self.make_branch('.', format='knit')
1591
response = request.execute(
1592
b'', b'branch token', b'repo token')
1594
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1596
def test_unlock_on_unlocked_branch_locked_repo(self):
1597
backing = self.get_transport()
1598
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1599
branch = self.make_branch('.', format='knit')
1600
# Lock the repository.
1601
repo_token = branch.repository.lock_write().repository_token
1602
branch.repository.leave_lock_in_place()
1603
branch.repository.unlock()
1604
# Issue branch lock_write request on the unlocked branch (with locked
1606
response = request.execute(b'', b'branch token', repo_token)
1608
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1610
branch.repository.lock_write(repo_token)
1611
branch.repository.dont_leave_lock_in_place()
1612
branch.repository.unlock()
1615
class TestSmartServerRepositoryRequest(tests.TestCaseWithMemoryTransport):
1617
def test_no_repository(self):
1618
"""Raise NoRepositoryPresent when there is a bzrdir and no repo."""
1619
# we test this using a shared repository above the named path,
1620
# thus checking the right search logic is used - that is, that
1621
# its the exact path being looked at and the server is not
1623
backing = self.get_transport()
1624
request = smart_repo.SmartServerRepositoryRequest(backing)
1625
self.make_repository('.', shared=True)
1626
self.make_controldir('subdir')
1627
self.assertRaises(errors.NoRepositoryPresent,
1628
request.execute, b'subdir')
1631
class TestSmartServerRepositoryAddSignatureText(
1632
tests.TestCaseWithMemoryTransport):
1634
def test_add_text(self):
1635
backing = self.get_transport()
1636
request = smart_repo.SmartServerRepositoryAddSignatureText(backing)
1637
tree = self.make_branch_and_memory_tree('.')
1638
write_token = tree.lock_write()
1639
self.addCleanup(tree.unlock)
1641
tree.commit("Message", rev_id=b'rev1')
1642
tree.branch.repository.start_write_group()
1643
write_group_tokens = tree.branch.repository.suspend_write_group()
1645
None, request.execute(
1646
b'', write_token, b'rev1',
1647
*[token.encode('utf-8') for token in write_group_tokens]))
1648
response = request.do_body(b'somesignature')
1649
self.assertTrue(response.is_successful())
1650
self.assertEqual(response.args[0], b'ok')
1651
write_group_tokens = [token.decode('utf-8')
1652
for token in response.args[1:]]
1653
tree.branch.repository.resume_write_group(write_group_tokens)
1654
tree.branch.repository.commit_write_group()
1656
self.assertEqual(b"somesignature",
1657
tree.branch.repository.get_signature_text(b"rev1"))
1660
class TestSmartServerRepositoryAllRevisionIds(
1661
tests.TestCaseWithMemoryTransport):
1663
def test_empty(self):
1664
"""An empty body should be returned for an empty repository."""
1665
backing = self.get_transport()
1666
request = smart_repo.SmartServerRepositoryAllRevisionIds(backing)
1667
self.make_repository('.')
1669
smart_req.SuccessfulSmartServerResponse((b"ok", ), b""),
1670
request.execute(b''))
1672
def test_some_revisions(self):
1673
"""An empty body should be returned for an empty repository."""
1674
backing = self.get_transport()
1675
request = smart_repo.SmartServerRepositoryAllRevisionIds(backing)
1676
tree = self.make_branch_and_memory_tree('.')
1679
tree.commit(rev_id=b'origineel', message="message")
1680
tree.commit(rev_id=b'nog-een-revisie', message="message")
1683
request.execute(b''),
1684
[smart_req.SuccessfulSmartServerResponse(
1685
(b"ok", ), b"origineel\nnog-een-revisie"),
1686
smart_req.SuccessfulSmartServerResponse(
1687
(b"ok", ), b"nog-een-revisie\norigineel")])
1690
class TestSmartServerRepositoryBreakLock(tests.TestCaseWithMemoryTransport):
1692
def test_lock_to_break(self):
1693
backing = self.get_transport()
1694
request = smart_repo.SmartServerRepositoryBreakLock(backing)
1695
tree = self.make_branch_and_memory_tree('.')
1696
tree.branch.repository.lock_write()
1698
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1699
request.execute(b''))
1701
def test_nothing_to_break(self):
1702
backing = self.get_transport()
1703
request = smart_repo.SmartServerRepositoryBreakLock(backing)
1704
self.make_branch_and_memory_tree('.')
1706
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1707
request.execute(b''))
1710
class TestSmartServerRepositoryGetParentMap(tests.TestCaseWithMemoryTransport):
1712
def test_trivial_bzipped(self):
1713
# This tests that the wire encoding is actually bzipped
1714
backing = self.get_transport()
1715
request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1716
self.make_branch_and_memory_tree('.')
1718
self.assertEqual(None,
1719
request.execute(b'', b'missing-id'))
1720
# Note that it returns a body that is bzipped.
1722
smart_req.SuccessfulSmartServerResponse(
1723
(b'ok', ), bz2.compress(b'')),
1724
request.do_body(b'\n\n0\n'))
1726
def test_trivial_include_missing(self):
1727
backing = self.get_transport()
1728
request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1729
self.make_branch_and_memory_tree('.')
1732
None, request.execute(b'', b'missing-id', b'include-missing:'))
1734
smart_req.SuccessfulSmartServerResponse(
1735
(b'ok', ), bz2.compress(b'missing:missing-id')),
1736
request.do_body(b'\n\n0\n'))
1739
class TestSmartServerRepositoryGetRevisionGraph(
1740
tests.TestCaseWithMemoryTransport):
1742
def test_none_argument(self):
1743
backing = self.get_transport()
1744
request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1745
tree = self.make_branch_and_memory_tree('.')
1748
r1 = tree.commit('1st commit')
1749
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1752
# the lines of revision_id->revision_parent_list has no guaranteed
1753
# order coming out of a dict, so sort both our test and response
1754
lines = sorted([b' '.join([r2, r1]), r1])
1755
response = request.execute(b'', b'')
1756
response.body = b'\n'.join(sorted(response.body.split(b'\n')))
1759
smart_req.SmartServerResponse((b'ok', ), b'\n'.join(lines)),
1762
def test_specific_revision_argument(self):
1763
backing = self.get_transport()
1764
request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1765
tree = self.make_branch_and_memory_tree('.')
1768
rev_id_utf8 = u'\xc9'.encode('utf-8')
1769
tree.commit('1st commit', rev_id=rev_id_utf8)
1770
tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1773
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), rev_id_utf8),
1774
request.execute(b'', rev_id_utf8))
1776
def test_no_such_revision(self):
1777
backing = self.get_transport()
1778
request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1779
tree = self.make_branch_and_memory_tree('.')
1782
tree.commit('1st commit')
1785
# Note that it still returns body (of zero bytes).
1786
self.assertEqual(smart_req.SmartServerResponse(
1787
(b'nosuchrevision', b'missingrevision', ), b''),
1788
request.execute(b'', b'missingrevision'))
1791
class TestSmartServerRepositoryGetRevIdForRevno(
1792
tests.TestCaseWithMemoryTransport):
1794
def test_revno_found(self):
1795
backing = self.get_transport()
1796
request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1797
tree = self.make_branch_and_memory_tree('.')
1800
rev1_id_utf8 = u'\xc8'.encode('utf-8')
1801
rev2_id_utf8 = u'\xc9'.encode('utf-8')
1802
tree.commit('1st commit', rev_id=rev1_id_utf8)
1803
tree.commit('2nd commit', rev_id=rev2_id_utf8)
1806
self.assertEqual(smart_req.SmartServerResponse((b'ok', rev1_id_utf8)),
1807
request.execute(b'', 1, (2, rev2_id_utf8)))
1809
def test_known_revid_missing(self):
1810
backing = self.get_transport()
1811
request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1812
self.make_repository('.')
1814
smart_req.FailedSmartServerResponse((b'nosuchrevision', b'ghost')),
1815
request.execute(b'', 1, (2, b'ghost')))
1817
def test_history_incomplete(self):
1818
backing = self.get_transport()
1819
request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1820
parent = self.make_branch_and_memory_tree('parent', format='1.9')
1822
parent.add([''], [b'TREE_ROOT'])
1823
parent.commit(message='first commit')
1824
r2 = parent.commit(message='second commit')
1826
local = self.make_branch_and_memory_tree('local', format='1.9')
1827
local.branch.pull(parent.branch)
1828
local.set_parent_ids([r2])
1829
r3 = local.commit(message='local commit')
1830
local.branch.create_clone_on_transport(
1831
self.get_transport('stacked'), stacked_on=self.get_url('parent'))
1833
smart_req.SmartServerResponse((b'history-incomplete', 2, r2)),
1834
request.execute(b'stacked', 1, (3, r3)))
1837
class TestSmartServerRepositoryIterRevisions(
1838
tests.TestCaseWithMemoryTransport):
1840
def test_basic(self):
1841
backing = self.get_transport()
1842
request = smart_repo.SmartServerRepositoryIterRevisions(backing)
1843
tree = self.make_branch_and_memory_tree('.', format='2a')
1846
tree.commit('1st commit', rev_id=b"rev1")
1847
tree.commit('2nd commit', rev_id=b"rev2")
1850
self.assertIs(None, request.execute(b''))
1851
response = request.do_body(b"rev1\nrev2")
1852
self.assertTrue(response.is_successful())
1853
# Format 2a uses serializer format 10
1854
self.assertEqual(response.args, (b"ok", b"10"))
1856
self.addCleanup(tree.branch.lock_read().unlock)
1857
entries = [zlib.compress(record.get_bytes_as("fulltext")) for record in
1858
tree.branch.repository.revisions.get_record_stream(
1859
[(b"rev1", ), (b"rev2", )], "unordered", True)]
1861
contents = b"".join(response.body_stream)
1862
self.assertTrue(contents in (
1863
b"".join([entries[0], entries[1]]),
1864
b"".join([entries[1], entries[0]])))
1866
def test_missing(self):
1867
backing = self.get_transport()
1868
request = smart_repo.SmartServerRepositoryIterRevisions(backing)
1869
self.make_branch_and_memory_tree('.', format='2a')
1871
self.assertIs(None, request.execute(b''))
1872
response = request.do_body(b"rev1\nrev2")
1873
self.assertTrue(response.is_successful())
1874
# Format 2a uses serializer format 10
1875
self.assertEqual(response.args, (b"ok", b"10"))
1877
contents = b"".join(response.body_stream)
1878
self.assertEqual(contents, b"")
1881
class GetStreamTestBase(tests.TestCaseWithMemoryTransport):
1883
def make_two_commit_repo(self):
1884
tree = self.make_branch_and_memory_tree('.')
1887
r1 = tree.commit('1st commit')
1888
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1890
repo = tree.branch.repository
1894
class TestSmartServerRepositoryGetStream(GetStreamTestBase):
1896
def test_ancestry_of(self):
1897
"""The search argument may be a 'ancestry-of' some heads'."""
1898
backing = self.get_transport()
1899
request = smart_repo.SmartServerRepositoryGetStream(backing)
1900
repo, r1, r2 = self.make_two_commit_repo()
1901
fetch_spec = [b'ancestry-of', r2]
1902
lines = b'\n'.join(fetch_spec)
1903
request.execute(b'', repo._format.network_name())
1904
response = request.do_body(lines)
1905
self.assertEqual((b'ok',), response.args)
1906
stream_bytes = b''.join(response.body_stream)
1907
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
1909
def test_search(self):
1910
"""The search argument may be a 'search' of some explicit keys."""
1911
backing = self.get_transport()
1912
request = smart_repo.SmartServerRepositoryGetStream(backing)
1913
repo, r1, r2 = self.make_two_commit_repo()
1914
fetch_spec = [b'search', r1 + b' ' + r2, b'null:', b'2']
1915
lines = b'\n'.join(fetch_spec)
1916
request.execute(b'', repo._format.network_name())
1917
response = request.do_body(lines)
1918
self.assertEqual((b'ok',), response.args)
1919
stream_bytes = b''.join(response.body_stream)
1920
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
1922
def test_search_everything(self):
1923
"""A search of 'everything' returns a stream."""
1924
backing = self.get_transport()
1925
request = smart_repo.SmartServerRepositoryGetStream_1_19(backing)
1926
repo, r1, r2 = self.make_two_commit_repo()
1927
serialised_fetch_spec = b'everything'
1928
request.execute(b'', repo._format.network_name())
1929
response = request.do_body(serialised_fetch_spec)
1930
self.assertEqual((b'ok',), response.args)
1931
stream_bytes = b''.join(response.body_stream)
1932
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
1935
class TestSmartServerRequestHasRevision(tests.TestCaseWithMemoryTransport):
1937
def test_missing_revision(self):
1938
"""For a missing revision, ('no', ) is returned."""
1939
backing = self.get_transport()
1940
request = smart_repo.SmartServerRequestHasRevision(backing)
1941
self.make_repository('.')
1942
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
1943
request.execute(b'', b'revid'))
1945
def test_present_revision(self):
1946
"""For a present revision, ('yes', ) is returned."""
1947
backing = self.get_transport()
1948
request = smart_repo.SmartServerRequestHasRevision(backing)
1949
tree = self.make_branch_and_memory_tree('.')
1952
rev_id_utf8 = u'\xc8abc'.encode('utf-8')
1953
tree.commit('a commit', rev_id=rev_id_utf8)
1955
self.assertTrue(tree.branch.repository.has_revision(rev_id_utf8))
1956
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
1957
request.execute(b'', rev_id_utf8))
1960
class TestSmartServerRepositoryIterFilesBytes(tests.TestCaseWithTransport):
1962
def test_single(self):
1963
backing = self.get_transport()
1964
request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
1965
t = self.make_branch_and_tree('.')
1966
self.addCleanup(t.lock_write().unlock)
1967
self.build_tree_contents([("file", b"somecontents")])
1968
t.add(["file"], [b"thefileid"])
1969
t.commit(rev_id=b'somerev', message="add file")
1970
self.assertIs(None, request.execute(b''))
1971
response = request.do_body(b"thefileid\0somerev\n")
1972
self.assertTrue(response.is_successful())
1973
self.assertEqual(response.args, (b"ok", ))
1974
self.assertEqual(b"".join(response.body_stream),
1975
b"ok\x000\n" + zlib.compress(b"somecontents"))
1977
def test_missing(self):
1978
backing = self.get_transport()
1979
request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
1980
t = self.make_branch_and_tree('.')
1981
self.addCleanup(t.lock_write().unlock)
1982
self.assertIs(None, request.execute(b''))
1983
response = request.do_body(b"thefileid\0revision\n")
1984
self.assertTrue(response.is_successful())
1985
self.assertEqual(response.args, (b"ok", ))
1986
self.assertEqual(b"".join(response.body_stream),
1987
b"absent\x00thefileid\x00revision\x000\n")
1990
class TestSmartServerRequestHasSignatureForRevisionId(
1991
tests.TestCaseWithMemoryTransport):
1993
def test_missing_revision(self):
1994
"""For a missing revision, NoSuchRevision is returned."""
1995
backing = self.get_transport()
1996
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1998
self.make_repository('.')
2000
smart_req.FailedSmartServerResponse(
2001
(b'nosuchrevision', b'revid'), None),
2002
request.execute(b'', b'revid'))
2004
def test_missing_signature(self):
2005
"""For a missing signature, ('no', ) is returned."""
2006
backing = self.get_transport()
2007
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
2009
tree = self.make_branch_and_memory_tree('.')
2012
tree.commit('a commit', rev_id=b'A')
2014
self.assertTrue(tree.branch.repository.has_revision(b'A'))
2015
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2016
request.execute(b'', b'A'))
2018
def test_present_signature(self):
2019
"""For a present signature, ('yes', ) is returned."""
2020
backing = self.get_transport()
2021
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
2023
strategy = gpg.LoopbackGPGStrategy(None)
2024
tree = self.make_branch_and_memory_tree('.')
2027
tree.commit('a commit', rev_id=b'A')
2028
tree.branch.repository.start_write_group()
2029
tree.branch.repository.sign_revision(b'A', strategy)
2030
tree.branch.repository.commit_write_group()
2032
self.assertTrue(tree.branch.repository.has_revision(b'A'))
2033
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2034
request.execute(b'', b'A'))
2037
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
2039
def test_empty_revid(self):
2040
"""With an empty revid, we get only size an number and revisions"""
2041
backing = self.get_transport()
2042
request = smart_repo.SmartServerRepositoryGatherStats(backing)
2043
repository = self.make_repository('.')
2044
repository.gather_stats()
2045
expected_body = b'revisions: 0\n'
2047
smart_req.SmartServerResponse((b'ok', ), expected_body),
2048
request.execute(b'', b'', b'no'))
2050
def test_revid_with_committers(self):
2051
"""For a revid we get more infos."""
2052
backing = self.get_transport()
2053
rev_id_utf8 = u'\xc8abc'.encode('utf-8')
2054
request = smart_repo.SmartServerRepositoryGatherStats(backing)
2055
tree = self.make_branch_and_memory_tree('.')
2058
# Let's build a predictable result
2059
tree.commit('a commit', timestamp=123456.2, timezone=3600)
2060
tree.commit('a commit', timestamp=654321.4, timezone=0,
2064
tree.branch.repository.gather_stats()
2065
expected_body = (b'firstrev: 123456.200 3600\n'
2066
b'latestrev: 654321.400 0\n'
2069
smart_req.SmartServerResponse((b'ok', ), expected_body),
2070
request.execute(b'', rev_id_utf8, b'no'))
2072
def test_not_empty_repository_with_committers(self):
2073
"""For a revid and requesting committers we get the whole thing."""
2074
backing = self.get_transport()
2075
rev_id_utf8 = u'\xc8abc'.encode('utf-8')
2076
request = smart_repo.SmartServerRepositoryGatherStats(backing)
2077
tree = self.make_branch_and_memory_tree('.')
2080
# Let's build a predictable result
2081
tree.commit('a commit', timestamp=123456.2, timezone=3600,
2083
tree.commit('a commit', timestamp=654321.4, timezone=0,
2084
committer='bar', rev_id=rev_id_utf8)
2086
tree.branch.repository.gather_stats()
2088
expected_body = (b'committers: 2\n'
2089
b'firstrev: 123456.200 3600\n'
2090
b'latestrev: 654321.400 0\n'
2093
smart_req.SmartServerResponse((b'ok', ), expected_body),
2094
request.execute(b'', rev_id_utf8, b'yes'))
2096
def test_unknown_revid(self):
2097
"""An unknown revision id causes a 'nosuchrevision' error."""
2098
backing = self.get_transport()
2099
request = smart_repo.SmartServerRepositoryGatherStats(backing)
2100
self.make_repository('.')
2102
smart_req.FailedSmartServerResponse(
2103
(b'nosuchrevision', b'mia'), None),
2104
request.execute(b'', b'mia', b'yes'))
2107
class TestSmartServerRepositoryIsShared(tests.TestCaseWithMemoryTransport):
2109
def test_is_shared(self):
2110
"""For a shared repository, ('yes', ) is returned."""
2111
backing = self.get_transport()
2112
request = smart_repo.SmartServerRepositoryIsShared(backing)
2113
self.make_repository('.', shared=True)
2114
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2115
request.execute(b'', ))
2117
def test_is_not_shared(self):
2118
"""For a shared repository, ('no', ) is returned."""
2119
backing = self.get_transport()
2120
request = smart_repo.SmartServerRepositoryIsShared(backing)
2121
self.make_repository('.', shared=False)
2122
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2123
request.execute(b'', ))
2126
class TestSmartServerRepositoryGetRevisionSignatureText(
2127
tests.TestCaseWithMemoryTransport):
2129
def test_get_signature(self):
2130
backing = self.get_transport()
2131
request = smart_repo.SmartServerRepositoryGetRevisionSignatureText(
2133
bb = self.make_branch_builder('.')
2134
bb.build_commit(rev_id=b'A')
2135
repo = bb.get_branch().repository
2136
strategy = gpg.LoopbackGPGStrategy(None)
2137
self.addCleanup(repo.lock_write().unlock)
2138
repo.start_write_group()
2139
repo.sign_revision(b'A', strategy)
2140
repo.commit_write_group()
2142
b'-----BEGIN PSEUDO-SIGNED CONTENT-----\n' +
2143
Testament.from_revision(repo, b'A').as_short_text() +
2144
b'-----END PSEUDO-SIGNED CONTENT-----\n')
2146
smart_req.SmartServerResponse((b'ok', ), expected_body),
2147
request.execute(b'', b'A'))
2150
class TestSmartServerRepositoryMakeWorkingTrees(
2151
tests.TestCaseWithMemoryTransport):
2153
def test_make_working_trees(self):
2154
"""For a repository with working trees, ('yes', ) is returned."""
2155
backing = self.get_transport()
2156
request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
2157
r = self.make_repository('.')
2158
r.set_make_working_trees(True)
2159
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2160
request.execute(b'', ))
2162
def test_is_not_shared(self):
2163
"""For a repository with working trees, ('no', ) is returned."""
2164
backing = self.get_transport()
2165
request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
2166
r = self.make_repository('.')
2167
r.set_make_working_trees(False)
2168
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2169
request.execute(b'', ))
2172
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
2174
def test_lock_write_on_unlocked_repo(self):
2175
backing = self.get_transport()
2176
request = smart_repo.SmartServerRepositoryLockWrite(backing)
2177
repository = self.make_repository('.', format='knit')
2178
response = request.execute(b'')
2179
nonce = repository.control_files._lock.peek().get('nonce')
2180
self.assertEqual(smart_req.SmartServerResponse(
2181
(b'ok', nonce)), response)
2182
# The repository is now locked. Verify that with a new repository
2184
new_repo = repository.controldir.open_repository()
2185
self.assertRaises(errors.LockContention, new_repo.lock_write)
2187
request = smart_repo.SmartServerRepositoryUnlock(backing)
2188
response = request.execute(b'', nonce)
2190
def test_lock_write_on_locked_repo(self):
2191
backing = self.get_transport()
2192
request = smart_repo.SmartServerRepositoryLockWrite(backing)
2193
repository = self.make_repository('.', format='knit')
2194
repo_token = repository.lock_write().repository_token
2195
repository.leave_lock_in_place()
2197
response = request.execute(b'')
2199
smart_req.SmartServerResponse((b'LockContention',)), response)
2201
repository.lock_write(repo_token)
2202
repository.dont_leave_lock_in_place()
2205
def test_lock_write_on_readonly_transport(self):
2206
backing = self.get_readonly_transport()
2207
request = smart_repo.SmartServerRepositoryLockWrite(backing)
2208
self.make_repository('.', format='knit')
2209
response = request.execute(b'')
2210
self.assertFalse(response.is_successful())
2211
self.assertEqual(b'LockFailed', response.args[0])
2214
class TestInsertStreamBase(tests.TestCaseWithMemoryTransport):
2216
def make_empty_byte_stream(self, repo):
2217
byte_stream = smart_repo._stream_to_byte_stream([], repo._format)
2218
return b''.join(byte_stream)
2221
class TestSmartServerRepositoryInsertStream(TestInsertStreamBase):
2223
def test_insert_stream_empty(self):
2224
backing = self.get_transport()
2225
request = smart_repo.SmartServerRepositoryInsertStream(backing)
2226
repository = self.make_repository('.')
2227
response = request.execute(b'', b'')
2228
self.assertEqual(None, response)
2229
response = request.do_chunk(self.make_empty_byte_stream(repository))
2230
self.assertEqual(None, response)
2231
response = request.do_end()
2232
self.assertEqual(smart_req.SmartServerResponse((b'ok', )), response)
2235
class TestSmartServerRepositoryInsertStreamLocked(TestInsertStreamBase):
2237
def test_insert_stream_empty(self):
2238
backing = self.get_transport()
2239
request = smart_repo.SmartServerRepositoryInsertStreamLocked(
2241
repository = self.make_repository('.', format='knit')
2242
lock_token = repository.lock_write().repository_token
2243
response = request.execute(b'', b'', lock_token)
2244
self.assertEqual(None, response)
2245
response = request.do_chunk(self.make_empty_byte_stream(repository))
2246
self.assertEqual(None, response)
2247
response = request.do_end()
2248
self.assertEqual(smart_req.SmartServerResponse((b'ok', )), response)
2251
def test_insert_stream_with_wrong_lock_token(self):
2252
backing = self.get_transport()
2253
request = smart_repo.SmartServerRepositoryInsertStreamLocked(
2255
repository = self.make_repository('.', format='knit')
2256
with repository.lock_write():
2258
errors.TokenMismatch, request.execute, b'', b'',
2262
class TestSmartServerRepositoryUnlock(tests.TestCaseWithMemoryTransport):
2264
def test_unlock_on_locked_repo(self):
2265
backing = self.get_transport()
2266
request = smart_repo.SmartServerRepositoryUnlock(backing)
2267
repository = self.make_repository('.', format='knit')
2268
token = repository.lock_write().repository_token
2269
repository.leave_lock_in_place()
2271
response = request.execute(b'', token)
2273
smart_req.SmartServerResponse((b'ok',)), response)
2274
# The repository is now unlocked. Verify that with a new repository
2276
new_repo = repository.controldir.open_repository()
2277
new_repo.lock_write()
2280
def test_unlock_on_unlocked_repo(self):
2281
backing = self.get_transport()
2282
request = smart_repo.SmartServerRepositoryUnlock(backing)
2283
self.make_repository('.', format='knit')
2284
response = request.execute(b'', b'some token')
2286
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
2289
class TestSmartServerRepositoryGetPhysicalLockStatus(
2290
tests.TestCaseWithTransport):
2292
def test_with_write_lock(self):
2293
backing = self.get_transport()
2294
repo = self.make_repository('.')
2295
self.addCleanup(repo.lock_write().unlock)
2296
# lock_write() doesn't necessarily actually take a physical
2298
if repo.get_physical_lock_status():
2302
request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2303
request = request_class(backing)
2304
self.assertEqual(smart_req.SuccessfulSmartServerResponse((expected,)),
2305
request.execute(b'', ))
2307
def test_without_write_lock(self):
2308
backing = self.get_transport()
2309
repo = self.make_repository('.')
2310
self.assertEqual(False, repo.get_physical_lock_status())
2311
request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2312
request = request_class(backing)
2313
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'no',)),
2314
request.execute(b'', ))
2317
class TestSmartServerRepositoryReconcile(tests.TestCaseWithTransport):
2319
def test_reconcile(self):
2320
backing = self.get_transport()
2321
repo = self.make_repository('.')
2322
token = repo.lock_write().repository_token
2323
self.addCleanup(repo.unlock)
2324
request_class = smart_repo.SmartServerRepositoryReconcile
2325
request = request_class(backing)
2326
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
2328
b'garbage_inventories: 0\n'
2329
b'inconsistent_parents: 0\n'),
2330
request.execute(b'', token))
2333
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
2335
def test_is_readonly_no(self):
2336
backing = self.get_transport()
2337
request = smart_req.SmartServerIsReadonly(backing)
2338
response = request.execute()
2340
smart_req.SmartServerResponse((b'no',)), response)
2342
def test_is_readonly_yes(self):
2343
backing = self.get_readonly_transport()
2344
request = smart_req.SmartServerIsReadonly(backing)
2345
response = request.execute()
2347
smart_req.SmartServerResponse((b'yes',)), response)
2350
class TestSmartServerRepositorySetMakeWorkingTrees(
2351
tests.TestCaseWithMemoryTransport):
2353
def test_set_false(self):
2354
backing = self.get_transport()
2355
repo = self.make_repository('.', shared=True)
2356
repo.set_make_working_trees(True)
2357
request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2358
request = request_class(backing)
2359
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2360
request.execute(b'', b'False'))
2361
repo = repo.controldir.open_repository()
2362
self.assertFalse(repo.make_working_trees())
2364
def test_set_true(self):
2365
backing = self.get_transport()
2366
repo = self.make_repository('.', shared=True)
2367
repo.set_make_working_trees(False)
2368
request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2369
request = request_class(backing)
2370
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2371
request.execute(b'', b'True'))
2372
repo = repo.controldir.open_repository()
2373
self.assertTrue(repo.make_working_trees())
2376
class TestSmartServerRepositoryGetSerializerFormat(
2377
tests.TestCaseWithMemoryTransport):
2379
def test_get_serializer_format(self):
2380
backing = self.get_transport()
2381
repo = self.make_repository('.', format='2a')
2382
request_class = smart_repo.SmartServerRepositoryGetSerializerFormat
2383
request = request_class(backing)
2385
smart_req.SuccessfulSmartServerResponse((b'ok', b'10')),
2386
request.execute(b''))
2389
class TestSmartServerRepositoryWriteGroup(
2390
tests.TestCaseWithMemoryTransport):
2392
def test_start_write_group(self):
2393
backing = self.get_transport()
2394
repo = self.make_repository('.')
2395
lock_token = repo.lock_write().repository_token
2396
self.addCleanup(repo.unlock)
2397
request_class = smart_repo.SmartServerRepositoryStartWriteGroup
2398
request = request_class(backing)
2399
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok', [])),
2400
request.execute(b'', lock_token))
2402
def test_start_write_group_unsuspendable(self):
2403
backing = self.get_transport()
2404
repo = self.make_repository('.', format='knit')
2405
lock_token = repo.lock_write().repository_token
2406
self.addCleanup(repo.unlock)
2407
request_class = smart_repo.SmartServerRepositoryStartWriteGroup
2408
request = request_class(backing)
2410
smart_req.FailedSmartServerResponse((b'UnsuspendableWriteGroup',)),
2411
request.execute(b'', lock_token))
2413
def test_commit_write_group(self):
2414
backing = self.get_transport()
2415
repo = self.make_repository('.')
2416
lock_token = repo.lock_write().repository_token
2417
self.addCleanup(repo.unlock)
2418
repo.start_write_group()
2419
tokens = repo.suspend_write_group()
2420
request_class = smart_repo.SmartServerRepositoryCommitWriteGroup
2421
request = request_class(backing)
2422
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2423
request.execute(b'', lock_token, tokens))
2425
def test_abort_write_group(self):
2426
backing = self.get_transport()
2427
repo = self.make_repository('.')
2428
lock_token = repo.lock_write().repository_token
2429
repo.start_write_group()
2430
tokens = repo.suspend_write_group()
2431
self.addCleanup(repo.unlock)
2432
request_class = smart_repo.SmartServerRepositoryAbortWriteGroup
2433
request = request_class(backing)
2434
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2435
request.execute(b'', lock_token, tokens))
2437
def test_check_write_group(self):
2438
backing = self.get_transport()
2439
repo = self.make_repository('.')
2440
lock_token = repo.lock_write().repository_token
2441
repo.start_write_group()
2442
tokens = repo.suspend_write_group()
2443
self.addCleanup(repo.unlock)
2444
request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
2445
request = request_class(backing)
2446
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2447
request.execute(b'', lock_token, tokens))
2449
def test_check_write_group_invalid(self):
2450
backing = self.get_transport()
2451
repo = self.make_repository('.')
2452
lock_token = repo.lock_write().repository_token
2453
self.addCleanup(repo.unlock)
2454
request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
2455
request = request_class(backing)
2456
self.assertEqual(smart_req.FailedSmartServerResponse(
2457
(b'UnresumableWriteGroup', [b'random'],
2458
b'Malformed write group token')),
2459
request.execute(b'', lock_token, [b"random"]))
2462
class TestSmartServerPackRepositoryAutopack(tests.TestCaseWithTransport):
2464
def make_repo_needing_autopacking(self, path='.'):
2465
# Make a repo in need of autopacking.
2466
tree = self.make_branch_and_tree('.', format='pack-0.92')
2467
repo = tree.branch.repository
2468
# monkey-patch the pack collection to disable autopacking
2469
repo._pack_collection._max_pack_count = lambda count: count
2471
tree.commit('commit %s' % x)
2472
self.assertEqual(10, len(repo._pack_collection.names()))
2473
del repo._pack_collection._max_pack_count
2476
def test_autopack_needed(self):
2477
repo = self.make_repo_needing_autopacking()
2479
self.addCleanup(repo.unlock)
2480
backing = self.get_transport()
2481
request = smart_packrepo.SmartServerPackRepositoryAutopack(
2483
response = request.execute(b'')
2484
self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
2485
repo._pack_collection.reload_pack_names()
2486
self.assertEqual(1, len(repo._pack_collection.names()))
2488
def test_autopack_not_needed(self):
2489
tree = self.make_branch_and_tree('.', format='pack-0.92')
2490
repo = tree.branch.repository
2492
self.addCleanup(repo.unlock)
2494
tree.commit('commit %s' % x)
2495
backing = self.get_transport()
2496
request = smart_packrepo.SmartServerPackRepositoryAutopack(
2498
response = request.execute(b'')
2499
self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
2500
repo._pack_collection.reload_pack_names()
2501
self.assertEqual(9, len(repo._pack_collection.names()))
2503
def test_autopack_on_nonpack_format(self):
2504
"""A request to autopack a non-pack repo is a no-op."""
2505
repo = self.make_repository('.', format='knit')
2506
backing = self.get_transport()
2507
request = smart_packrepo.SmartServerPackRepositoryAutopack(
2509
response = request.execute(b'')
2510
self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
2513
class TestSmartServerVfsGet(tests.TestCaseWithMemoryTransport):
2515
def test_unicode_path(self):
2516
"""VFS requests expect unicode paths to be escaped."""
2517
filename = u'foo\N{INTERROBANG}'
2518
filename_escaped = urlutils.escape(filename)
2519
backing = self.get_transport()
2520
request = vfs.GetRequest(backing)
2521
backing.put_bytes_non_atomic(filename_escaped, b'contents')
2522
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'contents'),
2523
request.execute(filename_escaped.encode('ascii')))
2526
class TestHandlers(tests.TestCase):
2527
"""Tests for the request.request_handlers object."""
2529
def test_all_registrations_exist(self):
2530
"""All registered request_handlers can be found."""
2531
# If there's a typo in a register_lazy call, this loop will fail with
2532
# an AttributeError.
2533
for key in smart_req.request_handlers.keys():
2535
item = smart_req.request_handlers.get(key)
2536
except AttributeError as e:
2537
raise AttributeError('failed to get %s: %s' % (key, e))
2539
def assertHandlerEqual(self, verb, handler):
2540
self.assertEqual(smart_req.request_handlers.get(verb), handler)
2542
def test_registered_methods(self):
2543
"""Test that known methods are registered to the correct object."""
2544
self.assertHandlerEqual(b'Branch.break_lock',
2545
smart_branch.SmartServerBranchBreakLock)
2546
self.assertHandlerEqual(b'Branch.get_config_file',
2547
smart_branch.SmartServerBranchGetConfigFile)
2548
self.assertHandlerEqual(b'Branch.put_config_file',
2549
smart_branch.SmartServerBranchPutConfigFile)
2550
self.assertHandlerEqual(b'Branch.get_parent',
2551
smart_branch.SmartServerBranchGetParent)
2552
self.assertHandlerEqual(b'Branch.get_physical_lock_status',
2553
smart_branch.SmartServerBranchRequestGetPhysicalLockStatus)
2554
self.assertHandlerEqual(b'Branch.get_tags_bytes',
2555
smart_branch.SmartServerBranchGetTagsBytes)
2556
self.assertHandlerEqual(b'Branch.lock_write',
2557
smart_branch.SmartServerBranchRequestLockWrite)
2558
self.assertHandlerEqual(b'Branch.last_revision_info',
2559
smart_branch.SmartServerBranchRequestLastRevisionInfo)
2560
self.assertHandlerEqual(b'Branch.revision_history',
2561
smart_branch.SmartServerRequestRevisionHistory)
2562
self.assertHandlerEqual(b'Branch.revision_id_to_revno',
2563
smart_branch.SmartServerBranchRequestRevisionIdToRevno)
2564
self.assertHandlerEqual(b'Branch.set_config_option',
2565
smart_branch.SmartServerBranchRequestSetConfigOption)
2566
self.assertHandlerEqual(b'Branch.set_last_revision',
2567
smart_branch.SmartServerBranchRequestSetLastRevision)
2568
self.assertHandlerEqual(b'Branch.set_last_revision_info',
2569
smart_branch.SmartServerBranchRequestSetLastRevisionInfo)
2570
self.assertHandlerEqual(b'Branch.set_last_revision_ex',
2571
smart_branch.SmartServerBranchRequestSetLastRevisionEx)
2572
self.assertHandlerEqual(b'Branch.set_parent_location',
2573
smart_branch.SmartServerBranchRequestSetParentLocation)
2574
self.assertHandlerEqual(b'Branch.unlock',
2575
smart_branch.SmartServerBranchRequestUnlock)
2576
self.assertHandlerEqual(b'BzrDir.destroy_branch',
2577
smart_dir.SmartServerBzrDirRequestDestroyBranch)
2578
self.assertHandlerEqual(b'BzrDir.find_repository',
2579
smart_dir.SmartServerRequestFindRepositoryV1)
2580
self.assertHandlerEqual(b'BzrDir.find_repositoryV2',
2581
smart_dir.SmartServerRequestFindRepositoryV2)
2582
self.assertHandlerEqual(b'BzrDirFormat.initialize',
2583
smart_dir.SmartServerRequestInitializeBzrDir)
2584
self.assertHandlerEqual(b'BzrDirFormat.initialize_ex_1.16',
2585
smart_dir.SmartServerRequestBzrDirInitializeEx)
2586
self.assertHandlerEqual(b'BzrDir.checkout_metadir',
2587
smart_dir.SmartServerBzrDirRequestCheckoutMetaDir)
2588
self.assertHandlerEqual(b'BzrDir.cloning_metadir',
2589
smart_dir.SmartServerBzrDirRequestCloningMetaDir)
2590
self.assertHandlerEqual(b'BzrDir.get_branches',
2591
smart_dir.SmartServerBzrDirRequestGetBranches)
2592
self.assertHandlerEqual(b'BzrDir.get_config_file',
2593
smart_dir.SmartServerBzrDirRequestConfigFile)
2594
self.assertHandlerEqual(b'BzrDir.open_branch',
2595
smart_dir.SmartServerRequestOpenBranch)
2596
self.assertHandlerEqual(b'BzrDir.open_branchV2',
2597
smart_dir.SmartServerRequestOpenBranchV2)
2598
self.assertHandlerEqual(b'BzrDir.open_branchV3',
2599
smart_dir.SmartServerRequestOpenBranchV3)
2600
self.assertHandlerEqual(b'PackRepository.autopack',
2601
smart_packrepo.SmartServerPackRepositoryAutopack)
2602
self.assertHandlerEqual(b'Repository.add_signature_text',
2603
smart_repo.SmartServerRepositoryAddSignatureText)
2604
self.assertHandlerEqual(b'Repository.all_revision_ids',
2605
smart_repo.SmartServerRepositoryAllRevisionIds)
2606
self.assertHandlerEqual(b'Repository.break_lock',
2607
smart_repo.SmartServerRepositoryBreakLock)
2608
self.assertHandlerEqual(b'Repository.gather_stats',
2609
smart_repo.SmartServerRepositoryGatherStats)
2610
self.assertHandlerEqual(b'Repository.get_parent_map',
2611
smart_repo.SmartServerRepositoryGetParentMap)
2612
self.assertHandlerEqual(b'Repository.get_physical_lock_status',
2613
smart_repo.SmartServerRepositoryGetPhysicalLockStatus)
2614
self.assertHandlerEqual(b'Repository.get_rev_id_for_revno',
2615
smart_repo.SmartServerRepositoryGetRevIdForRevno)
2616
self.assertHandlerEqual(b'Repository.get_revision_graph',
2617
smart_repo.SmartServerRepositoryGetRevisionGraph)
2618
self.assertHandlerEqual(b'Repository.get_revision_signature_text',
2619
smart_repo.SmartServerRepositoryGetRevisionSignatureText)
2620
self.assertHandlerEqual(b'Repository.get_stream',
2621
smart_repo.SmartServerRepositoryGetStream)
2622
self.assertHandlerEqual(b'Repository.get_stream_1.19',
2623
smart_repo.SmartServerRepositoryGetStream_1_19)
2624
self.assertHandlerEqual(b'Repository.iter_revisions',
2625
smart_repo.SmartServerRepositoryIterRevisions)
2626
self.assertHandlerEqual(b'Repository.has_revision',
2627
smart_repo.SmartServerRequestHasRevision)
2628
self.assertHandlerEqual(b'Repository.insert_stream',
2629
smart_repo.SmartServerRepositoryInsertStream)
2630
self.assertHandlerEqual(b'Repository.insert_stream_locked',
2631
smart_repo.SmartServerRepositoryInsertStreamLocked)
2632
self.assertHandlerEqual(b'Repository.is_shared',
2633
smart_repo.SmartServerRepositoryIsShared)
2634
self.assertHandlerEqual(b'Repository.iter_files_bytes',
2635
smart_repo.SmartServerRepositoryIterFilesBytes)
2636
self.assertHandlerEqual(b'Repository.lock_write',
2637
smart_repo.SmartServerRepositoryLockWrite)
2638
self.assertHandlerEqual(b'Repository.make_working_trees',
2639
smart_repo.SmartServerRepositoryMakeWorkingTrees)
2640
self.assertHandlerEqual(b'Repository.pack',
2641
smart_repo.SmartServerRepositoryPack)
2642
self.assertHandlerEqual(b'Repository.reconcile',
2643
smart_repo.SmartServerRepositoryReconcile)
2644
self.assertHandlerEqual(b'Repository.tarball',
2645
smart_repo.SmartServerRepositoryTarball)
2646
self.assertHandlerEqual(b'Repository.unlock',
2647
smart_repo.SmartServerRepositoryUnlock)
2648
self.assertHandlerEqual(b'Repository.start_write_group',
2649
smart_repo.SmartServerRepositoryStartWriteGroup)
2650
self.assertHandlerEqual(b'Repository.check_write_group',
2651
smart_repo.SmartServerRepositoryCheckWriteGroup)
2652
self.assertHandlerEqual(b'Repository.commit_write_group',
2653
smart_repo.SmartServerRepositoryCommitWriteGroup)
2654
self.assertHandlerEqual(b'Repository.abort_write_group',
2655
smart_repo.SmartServerRepositoryAbortWriteGroup)
2656
self.assertHandlerEqual(b'VersionedFileRepository.get_serializer_format',
2657
smart_repo.SmartServerRepositoryGetSerializerFormat)
2658
self.assertHandlerEqual(b'VersionedFileRepository.get_inventories',
2659
smart_repo.SmartServerRepositoryGetInventories)
2660
self.assertHandlerEqual(b'Transport.is_readonly',
2661
smart_req.SmartServerIsReadonly)
2664
class SmartTCPServerHookTests(tests.TestCaseWithMemoryTransport):
2665
"""Tests for SmartTCPServer hooks."""
2668
super(SmartTCPServerHookTests, self).setUp()
2669
self.server = server.SmartTCPServer(self.get_transport())
2671
def test_run_server_started_hooks(self):
2672
"""Test the server started hooks get fired properly."""
2674
server.SmartTCPServer.hooks.install_named_hook('server_started',
2675
lambda backing_urls, url: started_calls.append(
2676
(backing_urls, url)),
2678
started_ex_calls = []
2679
server.SmartTCPServer.hooks.install_named_hook('server_started_ex',
2680
lambda backing_urls, url: started_ex_calls.append(
2681
(backing_urls, url)),
2683
self.server._sockname = ('example.com', 42)
2684
self.server.run_server_started_hooks()
2685
self.assertEqual(started_calls,
2686
[([self.get_transport().base], 'bzr://example.com:42/')])
2687
self.assertEqual(started_ex_calls,
2688
[([self.get_transport().base], self.server)])
2690
def test_run_server_started_hooks_ipv6(self):
2691
"""Test that socknames can contain 4-tuples."""
2692
self.server._sockname = ('::', 42, 0, 0)
2694
server.SmartTCPServer.hooks.install_named_hook('server_started',
2695
lambda backing_urls, url: started_calls.append(
2696
(backing_urls, url)),
2698
self.server.run_server_started_hooks()
2699
self.assertEqual(started_calls,
2700
[([self.get_transport().base], 'bzr://:::42/')])
2702
def test_run_server_stopped_hooks(self):
2703
"""Test the server stopped hooks."""
2704
self.server._sockname = ('example.com', 42)
2706
server.SmartTCPServer.hooks.install_named_hook('server_stopped',
2707
lambda backing_urls, url: stopped_calls.append(
2708
(backing_urls, url)),
2710
self.server.run_server_stopped_hooks()
2711
self.assertEqual(stopped_calls,
2712
[([self.get_transport().base], 'bzr://example.com:42/')])
2715
class TestSmartServerRepositoryPack(tests.TestCaseWithMemoryTransport):
2717
def test_pack(self):
2718
backing = self.get_transport()
2719
request = smart_repo.SmartServerRepositoryPack(backing)
2720
tree = self.make_branch_and_memory_tree('.')
2721
repo_token = tree.branch.repository.lock_write().repository_token
2723
self.assertIs(None, request.execute(b'', repo_token, False))
2726
smart_req.SuccessfulSmartServerResponse((b'ok', ), ),
2727
request.do_body(b''))
2730
class TestSmartServerRepositoryGetInventories(tests.TestCaseWithTransport):
2732
def _get_serialized_inventory_delta(self, repository, base_revid, revid):
2733
base_inv = repository.revision_tree(base_revid).root_inventory
2734
inv = repository.revision_tree(revid).root_inventory
2735
inv_delta = inv._make_delta(base_inv)
2736
serializer = inventory_delta.InventoryDeltaSerializer(True, True)
2737
return b"".join(serializer.delta_to_lines(base_revid, revid, inv_delta))
2739
def test_single(self):
2740
backing = self.get_transport()
2741
request = smart_repo.SmartServerRepositoryGetInventories(backing)
2742
t = self.make_branch_and_tree('.', format='2a')
2743
self.addCleanup(t.lock_write().unlock)
2744
self.build_tree_contents([("file", b"somecontents")])
2745
t.add(["file"], [b"thefileid"])
2746
t.commit(rev_id=b'somerev', message="add file")
2747
self.assertIs(None, request.execute(b'', b'unordered'))
2748
response = request.do_body(b"somerev\n")
2749
self.assertTrue(response.is_successful())
2750
self.assertEqual(response.args, (b"ok", ))
2751
stream = [('inventory-deltas', [
2752
versionedfile.FulltextContentFactory(b'somerev', None, None,
2753
self._get_serialized_inventory_delta(
2754
t.branch.repository, b'null:', b'somerev'))])]
2755
fmt = controldir.format_registry.get('2a')().repository_format
2757
b"".join(response.body_stream),
2758
b"".join(smart_repo._stream_to_byte_stream(stream, fmt)))
2760
def test_empty(self):
2761
backing = self.get_transport()
2762
request = smart_repo.SmartServerRepositoryGetInventories(backing)
2763
t = self.make_branch_and_tree('.', format='2a')
2764
self.addCleanup(t.lock_write().unlock)
2765
self.build_tree_contents([("file", b"somecontents")])
2766
t.add(["file"], [b"thefileid"])
2767
t.commit(rev_id=b'somerev', message="add file")
2768
self.assertIs(None, request.execute(b'', b'unordered'))
2769
response = request.do_body(b"")
2770
self.assertTrue(response.is_successful())
2771
self.assertEqual(response.args, (b"ok", ))
2772
self.assertEqual(b"".join(response.body_stream),
2773
b"Bazaar pack format 1 (introduced in 0.18)\nB54\n\nBazaar repository format 2a (needs bzr 1.16 or later)\nE")
2776
class TestSmartServerRepositoryGetStreamForMissingKeys(GetStreamTestBase):
2778
def test_missing(self):
2779
"""The search argument may be a 'ancestry-of' some heads'."""
2780
backing = self.get_transport()
2781
request = smart_repo.SmartServerRepositoryGetStreamForMissingKeys(
2783
repo, r1, r2 = self.make_two_commit_repo()
2784
request.execute(b'', repo._format.network_name())
2785
lines = b'inventories\t' + r1
2786
response = request.do_body(lines)
2787
self.assertEqual((b'ok',), response.args)
2788
stream_bytes = b''.join(response.body_stream)
2789
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
2791
def test_unknown_format(self):
2792
"""The format may not be known by the remote server."""
2793
backing = self.get_transport()
2794
request = smart_repo.SmartServerRepositoryGetStreamForMissingKeys(
2796
repo, r1, r2 = self.make_two_commit_repo()
2797
request.execute(b'', b'yada yada yada')
2798
expected = smart_req.FailedSmartServerResponse(
2799
(b'UnknownFormat', b'repository', b'yada yada yada'))
2802
class TestSmartServerRepositoryRevisionArchive(tests.TestCaseWithTransport):
2804
backing = self.get_transport()
2805
request = smart_repo.SmartServerRepositoryRevisionArchive(backing)
2806
t = self.make_branch_and_tree('.')
2807
self.addCleanup(t.lock_write().unlock)
2808
self.build_tree_contents([("file", b"somecontents")])
2809
t.add(["file"], [b"thefileid"])
2810
t.commit(rev_id=b'somerev', message="add file")
2811
response = request.execute(b'', b"somerev", b"tar", b"foo.tar", b"foo")
2812
self.assertTrue(response.is_successful())
2813
self.assertEqual(response.args, (b"ok", ))
2814
b = BytesIO(b"".join(response.body_stream))
2815
with tarfile.open(mode='r', fileobj=b) as tf:
2816
self.assertEqual(['foo/file'], tf.getnames())
2819
class TestSmartServerRepositoryAnnotateFileRevision(tests.TestCaseWithTransport):
2822
backing = self.get_transport()
2823
request = smart_repo.SmartServerRepositoryAnnotateFileRevision(backing)
2824
t = self.make_branch_and_tree('.')
2825
self.addCleanup(t.lock_write().unlock)
2826
self.build_tree_contents([("file", b"somecontents\nmorecontents\n")])
2827
t.add(["file"], [b"thefileid"])
2828
t.commit(rev_id=b'somerev', message="add file")
2829
response = request.execute(b'', b"somerev", b"file")
2830
self.assertTrue(response.is_successful())
2831
self.assertEqual(response.args, (b"ok", ))
2833
[[b'somerev', b'somecontents\n'], [b'somerev', b'morecontents\n']],
2834
bencode.bdecode(response.body))
2837
class TestSmartServerBranchRequestGetAllReferenceInfo(TestLockedBranch):
2839
def test_get_some(self):
2840
backing = self.get_transport()
2841
request = smart_branch.SmartServerBranchRequestGetAllReferenceInfo(backing)
2842
branch = self.make_branch('.')
2843
branch.set_reference_info('some/path', 'http://www.example.com/')
2844
response = request.execute(b'')
2845
self.assertTrue(response.is_successful())
2846
self.assertEqual(response.args, (b"ok", ))
2848
[[b'some/path', b'http://www.example.com/', b'']],
2849
bencode.bdecode(response.body))