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.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(standard_tests,
76
"TestSmartServerRequestFindRepository")
77
v2_only, v1_and_2 = tests.split_suite_by_re(to_adapt,
79
tests.multiply_tests(v1_and_2, scenarios, result)
80
# The first scenario is only applicable to v1 protocols, it is deleted
82
tests.multiply_tests(v2_only, scenarios[1:], result)
86
class TestCaseWithChrootedTransport(tests.TestCaseWithTransport):
89
self.vfs_transport_factory = memory.MemoryServer
90
super(TestCaseWithChrootedTransport, self).setUp()
91
self._chroot_server = None
93
def get_transport(self, relpath=None):
94
if self._chroot_server is None:
95
backing_transport = tests.TestCaseWithTransport.get_transport(self)
96
self._chroot_server = chroot.ChrootServer(backing_transport)
97
self.start_server(self._chroot_server)
98
t = transport.get_transport_from_url(self._chroot_server.get_url())
99
if relpath is not None:
104
class TestCaseWithSmartMedium(tests.TestCaseWithMemoryTransport):
107
super(TestCaseWithSmartMedium, self).setUp()
108
# We're allowed to set the transport class here, so that we don't use
109
# the default or a parameterized class, but rather use the
110
# TestCaseWithTransport infrastructure to set up a smart server and
112
self.overrideAttr(self, "transport_server", self.make_transport_server)
114
def make_transport_server(self):
115
return test_server.SmartTCPServer_for_testing('-' + self.id())
117
def get_smart_medium(self):
118
"""Get a smart medium to use in tests."""
119
return self.get_transport().get_smart_medium()
122
class TestByteStreamToStream(tests.TestCase):
124
def test_repeated_substreams_same_kind_are_one_stream(self):
125
# Make a stream - an iterable of bytestrings.
126
stream = [('text', [versionedfile.FulltextContentFactory((b'k1',), None,
127
None, b'foo')]), ('text', [
128
versionedfile.FulltextContentFactory((b'k2',), None, None, b'bar')])]
129
fmt = controldir.format_registry.get('pack-0.92')().repository_format
130
bytes = smart_repo._stream_to_byte_stream(stream, fmt)
132
# Iterate the resulting iterable; checking that we get only one stream
134
fmt, stream = smart_repo._byte_stream_to_stream(bytes)
135
for kind, substream in stream:
136
streams.append((kind, list(substream)))
137
self.assertLength(1, streams)
138
self.assertLength(2, streams[0][1])
141
class TestSmartServerResponse(tests.TestCase):
143
def test__eq__(self):
144
self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
145
smart_req.SmartServerResponse((b'ok', )))
146
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'body'),
147
smart_req.SmartServerResponse((b'ok', ), b'body'))
148
self.assertNotEqual(smart_req.SmartServerResponse((b'ok', )),
149
smart_req.SmartServerResponse((b'notok', )))
150
self.assertNotEqual(smart_req.SmartServerResponse((b'ok', ), b'body'),
151
smart_req.SmartServerResponse((b'ok', )))
152
self.assertNotEqual(None,
153
smart_req.SmartServerResponse((b'ok', )))
155
def test__str__(self):
156
"""SmartServerResponses can be stringified."""
158
str(smart_req.SuccessfulSmartServerResponse((b'args',), b'body')),
159
("<SuccessfulSmartServerResponse args=(b'args',) body=b'body'>",
160
"<SuccessfulSmartServerResponse args=('args',) body='body'>"))
162
str(smart_req.FailedSmartServerResponse((b'args',), b'body')),
163
("<FailedSmartServerResponse args=(b'args',) body=b'body'>",
164
"<FailedSmartServerResponse args=('args',) body='body'>"))
167
class TestSmartServerRequest(tests.TestCaseWithMemoryTransport):
169
def test_translate_client_path(self):
170
transport = self.get_transport()
171
request = smart_req.SmartServerRequest(transport, 'foo/')
172
self.assertEqual('./', request.translate_client_path(b'foo/'))
174
urlutils.InvalidURLJoin, request.translate_client_path, b'foo/..')
176
errors.PathNotChild, request.translate_client_path, b'/')
178
errors.PathNotChild, request.translate_client_path, b'bar/')
179
self.assertEqual('./baz', request.translate_client_path(b'foo/baz'))
180
e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'
181
self.assertEqual(u'./' + urlutils.escape(e_acute),
182
request.translate_client_path(b'foo/' + e_acute.encode('utf-8')))
184
def test_translate_client_path_vfs(self):
185
"""VfsRequests receive escaped paths rather than raw UTF-8."""
186
transport = self.get_transport()
187
request = vfs.VfsRequest(transport, 'foo/')
188
e_acute = u'\N{LATIN SMALL LETTER E WITH ACUTE}'
189
escaped = urlutils.escape(u'foo/' + e_acute)
190
self.assertEqual('./' + urlutils.escape(e_acute),
191
request.translate_client_path(escaped.encode('ascii')))
193
def test_transport_from_client_path(self):
194
transport = self.get_transport()
195
request = smart_req.SmartServerRequest(transport, 'foo/')
198
request.transport_from_client_path(b'foo/').base)
201
class TestSmartServerBzrDirRequestCloningMetaDir(
202
tests.TestCaseWithMemoryTransport):
203
"""Tests for BzrDir.cloning_metadir."""
205
def test_cloning_metadir(self):
206
"""When there is a bzrdir present, the call succeeds."""
207
backing = self.get_transport()
208
dir = self.make_controldir('.')
209
local_result = dir.cloning_metadir()
210
request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
211
request = request_class(backing)
212
expected = smart_req.SuccessfulSmartServerResponse(
213
(local_result.network_name(),
214
local_result.repository_format.network_name(),
215
(b'branch', local_result.get_branch_format().network_name())))
216
self.assertEqual(expected, request.execute(b'', b'False'))
218
def test_cloning_metadir_reference(self):
219
"""The request fails when bzrdir contains a branch reference."""
220
backing = self.get_transport()
221
referenced_branch = self.make_branch('referenced')
222
dir = self.make_controldir('.')
223
local_result = dir.cloning_metadir()
224
reference = _mod_bzrbranch.BranchReferenceFormat().initialize(
225
dir, target_branch=referenced_branch)
226
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(dir)
227
# The server shouldn't try to follow the branch reference, so it's fine
228
# if the referenced branch isn't reachable.
229
backing.rename('referenced', 'moved')
230
request_class = smart_dir.SmartServerBzrDirRequestCloningMetaDir
231
request = request_class(backing)
232
expected = smart_req.FailedSmartServerResponse((b'BranchReference',))
233
self.assertEqual(expected, request.execute(b'', b'False'))
236
class TestSmartServerBzrDirRequestCloningMetaDir(
237
tests.TestCaseWithMemoryTransport):
238
"""Tests for BzrDir.checkout_metadir."""
240
def test_checkout_metadir(self):
241
backing = self.get_transport()
242
request = smart_dir.SmartServerBzrDirRequestCheckoutMetaDir(
244
branch = self.make_branch('.', format='2a')
245
response = request.execute(b'')
247
smart_req.SmartServerResponse(
248
(b'Bazaar-NG meta directory, format 1\n',
249
b'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
250
b'Bazaar Branch Format 7 (needs bzr 1.6)\n')),
254
class TestSmartServerBzrDirRequestDestroyBranch(
255
tests.TestCaseWithMemoryTransport):
256
"""Tests for BzrDir.destroy_branch."""
258
def test_destroy_branch_default(self):
259
"""The default branch can be removed."""
260
backing = self.get_transport()
261
dir = self.make_branch('.').controldir
262
request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
263
request = request_class(backing)
264
expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
265
self.assertEqual(expected, request.execute(b'', None))
267
def test_destroy_branch_named(self):
268
"""A named branch can be removed."""
269
backing = self.get_transport()
270
dir = self.make_repository('.', format="development-colo").controldir
271
dir.create_branch(name="branchname")
272
request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
273
request = request_class(backing)
274
expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
275
self.assertEqual(expected, request.execute(b'', b"branchname"))
277
def test_destroy_branch_missing(self):
278
"""An error is raised if the branch didn't exist."""
279
backing = self.get_transport()
280
dir = self.make_controldir('.', format="development-colo")
281
request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
282
request = request_class(backing)
283
expected = smart_req.FailedSmartServerResponse((b'nobranch',), None)
284
self.assertEqual(expected, request.execute(b'', b"branchname"))
287
class TestSmartServerBzrDirRequestHasWorkingTree(
288
tests.TestCaseWithTransport):
289
"""Tests for BzrDir.has_workingtree."""
291
def test_has_workingtree_yes(self):
292
"""A working tree is present."""
293
backing = self.get_transport()
294
dir = self.make_branch_and_tree('.').controldir
295
request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
296
request = request_class(backing)
297
expected = smart_req.SuccessfulSmartServerResponse((b'yes',))
298
self.assertEqual(expected, request.execute(b''))
300
def test_has_workingtree_no(self):
301
"""A working tree is missing."""
302
backing = self.get_transport()
303
dir = self.make_controldir('.')
304
request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
305
request = request_class(backing)
306
expected = smart_req.SuccessfulSmartServerResponse((b'no',))
307
self.assertEqual(expected, request.execute(b''))
310
class TestSmartServerBzrDirRequestDestroyRepository(
311
tests.TestCaseWithMemoryTransport):
312
"""Tests for BzrDir.destroy_repository."""
314
def test_destroy_repository_default(self):
315
"""The repository can be removed."""
316
backing = self.get_transport()
317
dir = self.make_repository('.').controldir
318
request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
319
request = request_class(backing)
320
expected = smart_req.SuccessfulSmartServerResponse((b'ok',))
321
self.assertEqual(expected, request.execute(b''))
323
def test_destroy_repository_missing(self):
324
"""An error is raised if the repository didn't exist."""
325
backing = self.get_transport()
326
dir = self.make_controldir('.')
327
request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
328
request = request_class(backing)
329
expected = smart_req.FailedSmartServerResponse(
330
(b'norepository',), None)
331
self.assertEqual(expected, request.execute(b''))
334
class TestSmartServerRequestCreateRepository(tests.TestCaseWithMemoryTransport):
335
"""Tests for BzrDir.create_repository."""
337
def test_makes_repository(self):
338
"""When there is a bzrdir present, the call succeeds."""
339
backing = self.get_transport()
340
self.make_controldir('.')
341
request_class = smart_dir.SmartServerRequestCreateRepository
342
request = request_class(backing)
343
reference_bzrdir_format = controldir.format_registry.get('pack-0.92')()
344
reference_format = reference_bzrdir_format.repository_format
345
network_name = reference_format.network_name()
346
expected = smart_req.SuccessfulSmartServerResponse(
347
(b'ok', b'no', b'no', b'no', network_name))
348
self.assertEqual(expected, request.execute(b'', network_name, b'True'))
351
class TestSmartServerRequestFindRepository(tests.TestCaseWithMemoryTransport):
352
"""Tests for BzrDir.find_repository."""
354
def test_no_repository(self):
355
"""When there is no repository to be found, ('norepository', ) is returned."""
356
backing = self.get_transport()
357
request = self._request_class(backing)
358
self.make_controldir('.')
359
self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
360
request.execute(b''))
362
def test_nonshared_repository(self):
363
# nonshared repositorys only allow 'find' to return a handle when the
364
# path the repository is being searched on is the same as that that
365
# the repository is at.
366
backing = self.get_transport()
367
request = self._request_class(backing)
368
result = self._make_repository_and_result()
369
self.assertEqual(result, request.execute(b''))
370
self.make_controldir('subdir')
371
self.assertEqual(smart_req.SmartServerResponse((b'norepository', )),
372
request.execute(b'subdir'))
374
def _make_repository_and_result(self, shared=False, format=None):
375
"""Convenience function to setup a repository.
377
:result: The SmartServerResponse to expect when opening it.
379
repo = self.make_repository('.', shared=shared, format=format)
380
if repo.supports_rich_root():
384
if repo._format.supports_tree_reference:
388
if repo._format.supports_external_lookups:
392
if (smart_dir.SmartServerRequestFindRepositoryV3 ==
393
self._request_class):
394
return smart_req.SuccessfulSmartServerResponse(
395
(b'ok', b'', rich_root, subtrees, external,
396
repo._format.network_name()))
397
elif (smart_dir.SmartServerRequestFindRepositoryV2 ==
398
self._request_class):
399
# All tests so far are on formats, and for non-external
401
return smart_req.SuccessfulSmartServerResponse(
402
(b'ok', b'', rich_root, subtrees, external))
404
return smart_req.SuccessfulSmartServerResponse(
405
(b'ok', b'', rich_root, subtrees))
407
def test_shared_repository(self):
408
"""When there is a shared repository, we get 'ok', 'relpath-to-repo'."""
409
backing = self.get_transport()
410
request = self._request_class(backing)
411
result = self._make_repository_and_result(shared=True)
412
self.assertEqual(result, request.execute(b''))
413
self.make_controldir('subdir')
414
result2 = smart_req.SmartServerResponse(
415
result.args[0:1] + (b'..', ) + result.args[2:])
416
self.assertEqual(result2,
417
request.execute(b'subdir'))
418
self.make_controldir('subdir/deeper')
419
result3 = smart_req.SmartServerResponse(
420
result.args[0:1] + (b'../..', ) + result.args[2:])
421
self.assertEqual(result3,
422
request.execute(b'subdir/deeper'))
424
def test_rich_root_and_subtree_encoding(self):
425
"""Test for the format attributes for rich root and subtree support."""
426
backing = self.get_transport()
427
request = self._request_class(backing)
428
result = self._make_repository_and_result(
429
format='development-subtree')
430
# check the test will be valid
431
self.assertEqual(b'yes', result.args[2])
432
self.assertEqual(b'yes', result.args[3])
433
self.assertEqual(result, request.execute(b''))
435
def test_supports_external_lookups_no_v2(self):
436
"""Test for the supports_external_lookups attribute."""
437
backing = self.get_transport()
438
request = self._request_class(backing)
439
result = self._make_repository_and_result(
440
format='development-subtree')
441
# check the test will be valid
442
self.assertEqual(b'yes', result.args[4])
443
self.assertEqual(result, request.execute(b''))
446
class TestSmartServerBzrDirRequestGetConfigFile(
447
tests.TestCaseWithMemoryTransport):
448
"""Tests for BzrDir.get_config_file."""
450
def test_present(self):
451
backing = self.get_transport()
452
dir = self.make_controldir('.')
453
dir.get_config().set_default_stack_on("/")
454
local_result = dir._get_config()._get_config_file().read()
455
request_class = smart_dir.SmartServerBzrDirRequestConfigFile
456
request = request_class(backing)
457
expected = smart_req.SuccessfulSmartServerResponse((), local_result)
458
self.assertEqual(expected, request.execute(b''))
460
def test_missing(self):
461
backing = self.get_transport()
462
dir = self.make_controldir('.')
463
request_class = smart_dir.SmartServerBzrDirRequestConfigFile
464
request = request_class(backing)
465
expected = smart_req.SuccessfulSmartServerResponse((), b'')
466
self.assertEqual(expected, request.execute(b''))
469
class TestSmartServerBzrDirRequestGetBranches(
470
tests.TestCaseWithMemoryTransport):
471
"""Tests for BzrDir.get_branches."""
473
def test_simple(self):
474
backing = self.get_transport()
475
branch = self.make_branch('.')
476
request_class = smart_dir.SmartServerBzrDirRequestGetBranches
477
request = request_class(backing)
478
local_result = bencode.bencode(
479
{b"": (b"branch", branch._format.network_name())})
480
expected = smart_req.SuccessfulSmartServerResponse(
481
(b"success", ), local_result)
482
self.assertEqual(expected, request.execute(b''))
484
def test_empty(self):
485
backing = self.get_transport()
486
dir = self.make_controldir('.')
487
request_class = smart_dir.SmartServerBzrDirRequestGetBranches
488
request = request_class(backing)
489
local_result = bencode.bencode({})
490
expected = smart_req.SuccessfulSmartServerResponse(
491
(b'success',), local_result)
492
self.assertEqual(expected, request.execute(b''))
495
class TestSmartServerRequestInitializeBzrDir(tests.TestCaseWithMemoryTransport):
497
def test_empty_dir(self):
498
"""Initializing an empty dir should succeed and do it."""
499
backing = self.get_transport()
500
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
501
self.assertEqual(smart_req.SmartServerResponse((b'ok', )),
502
request.execute(b''))
503
made_dir = controldir.ControlDir.open_from_transport(backing)
504
# no branch, tree or repository is expected with the current
506
self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
507
self.assertRaises(errors.NotBranchError, made_dir.open_branch)
508
self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
510
def test_missing_dir(self):
511
"""Initializing a missing directory should fail like the bzrdir api."""
512
backing = self.get_transport()
513
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
514
self.assertRaises(errors.NoSuchFile,
515
request.execute, b'subdir')
517
def test_initialized_dir(self):
518
"""Initializing an extant bzrdir should fail like the bzrdir api."""
519
backing = self.get_transport()
520
request = smart_dir.SmartServerRequestInitializeBzrDir(backing)
521
self.make_controldir('subdir')
522
self.assertRaises(errors.AlreadyControlDirError,
523
request.execute, b'subdir')
526
class TestSmartServerRequestBzrDirInitializeEx(
527
tests.TestCaseWithMemoryTransport):
528
"""Basic tests for BzrDir.initialize_ex_1.16 in the smart server.
530
The main unit tests in test_bzrdir exercise the API comprehensively.
533
def test_empty_dir(self):
534
"""Initializing an empty dir should succeed and do it."""
535
backing = self.get_transport()
536
name = self.make_controldir('reference')._format.network_name()
537
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
539
smart_req.SmartServerResponse((b'', b'', b'', b'', b'', b'', name,
540
b'False', b'', b'', b'')),
541
request.execute(name, b'', b'True', b'False', b'False', b'', b'', b'', b'',
543
made_dir = controldir.ControlDir.open_from_transport(backing)
544
# no branch, tree or repository is expected with the current
546
self.assertRaises(errors.NoWorkingTree, made_dir.open_workingtree)
547
self.assertRaises(errors.NotBranchError, made_dir.open_branch)
548
self.assertRaises(errors.NoRepositoryPresent, made_dir.open_repository)
550
def test_missing_dir(self):
551
"""Initializing a missing directory should fail like the bzrdir api."""
552
backing = self.get_transport()
553
name = self.make_controldir('reference')._format.network_name()
554
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
555
self.assertRaises(errors.NoSuchFile, request.execute, name,
556
b'subdir/dir', b'False', b'False', b'False', b'', b'', b'', b'', b'False')
558
def test_initialized_dir(self):
559
"""Initializing an extant directory should fail like the bzrdir api."""
560
backing = self.get_transport()
561
name = self.make_controldir('reference')._format.network_name()
562
request = smart_dir.SmartServerRequestBzrDirInitializeEx(backing)
563
self.make_controldir('subdir')
564
self.assertRaises(errors.FileExists, request.execute, name, b'subdir',
565
b'False', b'False', b'False', b'', b'', b'', b'', b'False')
568
class TestSmartServerRequestOpenBzrDir(tests.TestCaseWithMemoryTransport):
570
def test_no_directory(self):
571
backing = self.get_transport()
572
request = smart_dir.SmartServerRequestOpenBzrDir(backing)
573
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
574
request.execute(b'does-not-exist'))
576
def test_empty_directory(self):
577
backing = self.get_transport()
578
backing.mkdir('empty')
579
request = smart_dir.SmartServerRequestOpenBzrDir(backing)
580
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
581
request.execute(b'empty'))
583
def test_outside_root_client_path(self):
584
backing = self.get_transport()
585
request = smart_dir.SmartServerRequestOpenBzrDir(backing,
586
root_client_path='root')
587
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
588
request.execute(b'not-root'))
591
class TestSmartServerRequestOpenBzrDir_2_1(tests.TestCaseWithMemoryTransport):
593
def test_no_directory(self):
594
backing = self.get_transport()
595
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
596
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
597
request.execute(b'does-not-exist'))
599
def test_empty_directory(self):
600
backing = self.get_transport()
601
backing.mkdir('empty')
602
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
603
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
604
request.execute(b'empty'))
606
def test_present_without_workingtree(self):
607
backing = self.get_transport()
608
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
609
self.make_controldir('.')
610
self.assertEqual(smart_req.SmartServerResponse((b'yes', b'no')),
611
request.execute(b''))
613
def test_outside_root_client_path(self):
614
backing = self.get_transport()
615
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing,
616
root_client_path='root')
617
self.assertEqual(smart_req.SmartServerResponse((b'no',)),
618
request.execute(b'not-root'))
621
class TestSmartServerRequestOpenBzrDir_2_1_disk(TestCaseWithChrootedTransport):
623
def test_present_with_workingtree(self):
624
self.vfs_transport_factory = test_server.LocalURLServer
625
backing = self.get_transport()
626
request = smart_dir.SmartServerRequestOpenBzrDir_2_1(backing)
627
bd = self.make_controldir('.')
628
bd.create_repository()
630
bd.create_workingtree()
631
self.assertEqual(smart_req.SmartServerResponse((b'yes', b'yes')),
632
request.execute(b''))
635
class TestSmartServerRequestOpenBranch(TestCaseWithChrootedTransport):
637
def test_no_branch(self):
638
"""When there is no branch, ('nobranch', ) is returned."""
639
backing = self.get_transport()
640
request = smart_dir.SmartServerRequestOpenBranch(backing)
641
self.make_controldir('.')
642
self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
643
request.execute(b''))
645
def test_branch(self):
646
"""When there is a branch, 'ok' is returned."""
647
backing = self.get_transport()
648
request = smart_dir.SmartServerRequestOpenBranch(backing)
649
self.make_branch('.')
650
self.assertEqual(smart_req.SmartServerResponse((b'ok', b'')),
651
request.execute(b''))
653
def test_branch_reference(self):
654
"""When there is a branch reference, the reference URL is returned."""
655
self.vfs_transport_factory = test_server.LocalURLServer
656
backing = self.get_transport()
657
request = smart_dir.SmartServerRequestOpenBranch(backing)
658
branch = self.make_branch('branch')
659
checkout = branch.create_checkout('reference', lightweight=True)
660
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
661
checkout.controldir).encode('utf-8')
662
self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
663
self.assertEqual(smart_req.SmartServerResponse((b'ok', reference_url)),
664
request.execute(b'reference'))
666
def test_notification_on_branch_from_repository(self):
667
"""When there is a repository, the error should return details."""
668
backing = self.get_transport()
669
request = smart_dir.SmartServerRequestOpenBranch(backing)
670
repo = self.make_repository('.')
671
self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
672
request.execute(b''))
675
class TestSmartServerRequestOpenBranchV2(TestCaseWithChrootedTransport):
677
def test_no_branch(self):
678
"""When there is no branch, ('nobranch', ) is returned."""
679
backing = self.get_transport()
680
self.make_controldir('.')
681
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
682
self.assertEqual(smart_req.SmartServerResponse((b'nobranch', )),
683
request.execute(b''))
685
def test_branch(self):
686
"""When there is a branch, 'ok' is returned."""
687
backing = self.get_transport()
688
expected = self.make_branch('.')._format.network_name()
689
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
690
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
691
(b'branch', expected)),
692
request.execute(b''))
694
def test_branch_reference(self):
695
"""When there is a branch reference, the reference URL is returned."""
696
self.vfs_transport_factory = test_server.LocalURLServer
697
backing = self.get_transport()
698
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
699
branch = self.make_branch('branch')
700
checkout = branch.create_checkout('reference', lightweight=True)
701
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
702
checkout.controldir).encode('utf-8')
703
self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
704
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
705
(b'ref', reference_url)),
706
request.execute(b'reference'))
708
def test_stacked_branch(self):
709
"""Opening a stacked branch does not open the stacked-on branch."""
710
trunk = self.make_branch('trunk')
711
feature = self.make_branch('feature')
712
feature.set_stacked_on_url(trunk.base)
714
_mod_branch.Branch.hooks.install_named_hook(
715
'open', opened_branches.append, None)
716
backing = self.get_transport()
717
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
720
response = request.execute(b'feature')
722
request.teardown_jail()
723
expected_format = feature._format.network_name()
724
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
725
(b'branch', expected_format)),
727
self.assertLength(1, opened_branches)
729
def test_notification_on_branch_from_repository(self):
730
"""When there is a repository, the error should return details."""
731
backing = self.get_transport()
732
request = smart_dir.SmartServerRequestOpenBranchV2(backing)
733
repo = self.make_repository('.')
734
self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
735
request.execute(b''))
738
class TestSmartServerRequestOpenBranchV3(TestCaseWithChrootedTransport):
740
def test_no_branch(self):
741
"""When there is no branch, ('nobranch', ) is returned."""
742
backing = self.get_transport()
743
self.make_controldir('.')
744
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
745
self.assertEqual(smart_req.SmartServerResponse((b'nobranch',)),
746
request.execute(b''))
748
def test_branch(self):
749
"""When there is a branch, 'ok' is returned."""
750
backing = self.get_transport()
751
expected = self.make_branch('.')._format.network_name()
752
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
753
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
754
(b'branch', expected)),
755
request.execute(b''))
757
def test_branch_reference(self):
758
"""When there is a branch reference, the reference URL is returned."""
759
self.vfs_transport_factory = test_server.LocalURLServer
760
backing = self.get_transport()
761
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
762
branch = self.make_branch('branch')
763
checkout = branch.create_checkout('reference', lightweight=True)
764
reference_url = _mod_bzrbranch.BranchReferenceFormat().get_reference(
765
checkout.controldir).encode('utf-8')
766
self.assertFileEqual(reference_url, 'reference/.bzr/branch/location')
767
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
768
(b'ref', reference_url)),
769
request.execute(b'reference'))
771
def test_stacked_branch(self):
772
"""Opening a stacked branch does not open the stacked-on branch."""
773
trunk = self.make_branch('trunk')
774
feature = self.make_branch('feature')
775
feature.set_stacked_on_url(trunk.base)
777
_mod_branch.Branch.hooks.install_named_hook(
778
'open', opened_branches.append, None)
779
backing = self.get_transport()
780
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
783
response = request.execute(b'feature')
785
request.teardown_jail()
786
expected_format = feature._format.network_name()
787
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
788
(b'branch', expected_format)),
790
self.assertLength(1, opened_branches)
792
def test_notification_on_branch_from_repository(self):
793
"""When there is a repository, the error should return details."""
794
backing = self.get_transport()
795
request = smart_dir.SmartServerRequestOpenBranchV3(backing)
796
repo = self.make_repository('.')
797
self.assertEqual(smart_req.SmartServerResponse(
798
(b'nobranch', b'location is a repository')),
799
request.execute(b''))
802
class TestSmartServerRequestRevisionHistory(tests.TestCaseWithMemoryTransport):
804
def test_empty(self):
805
"""For an empty branch, the body is empty."""
806
backing = self.get_transport()
807
request = smart_branch.SmartServerRequestRevisionHistory(backing)
808
self.make_branch('.')
809
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b''),
810
request.execute(b''))
812
def test_not_empty(self):
813
"""For a non-empty branch, the body is empty."""
814
backing = self.get_transport()
815
request = smart_branch.SmartServerRequestRevisionHistory(backing)
816
tree = self.make_branch_and_memory_tree('.')
819
r1 = tree.commit('1st commit')
820
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
823
smart_req.SmartServerResponse((b'ok', ), (b'\x00'.join([r1, r2]))),
824
request.execute(b''))
827
class TestSmartServerBranchRequest(tests.TestCaseWithMemoryTransport):
829
def test_no_branch(self):
830
"""When there is a bzrdir and no branch, NotBranchError is raised."""
831
backing = self.get_transport()
832
request = smart_branch.SmartServerBranchRequest(backing)
833
self.make_controldir('.')
834
self.assertRaises(errors.NotBranchError,
835
request.execute, b'')
837
def test_branch_reference(self):
838
"""When there is a branch reference, NotBranchError is raised."""
839
backing = self.get_transport()
840
request = smart_branch.SmartServerBranchRequest(backing)
841
branch = self.make_branch('branch')
842
checkout = branch.create_checkout('reference', lightweight=True)
843
self.assertRaises(errors.NotBranchError,
844
request.execute, b'checkout')
847
class TestSmartServerBranchRequestLastRevisionInfo(
848
tests.TestCaseWithMemoryTransport):
850
def test_empty(self):
851
"""For an empty branch, the result is ('ok', '0', b'null:')."""
852
backing = self.get_transport()
853
request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
854
self.make_branch('.')
855
self.assertEqual(smart_req.SmartServerResponse((b'ok', b'0', b'null:')),
856
request.execute(b''))
858
def test_ghost(self):
859
"""For an empty branch, the result is ('ok', '0', b'null:')."""
860
backing = self.get_transport()
861
request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
862
branch = self.make_branch('.')
863
def last_revision_info():
864
raise errors.GhostRevisionsHaveNoRevno(b'revid1', b'revid2')
865
self.overrideAttr(branch, 'last_revision_info', last_revision_info)
866
self.assertRaises(errors.GhostRevisionsHaveNoRevno,
867
request.do_with_branch, branch)
869
def test_not_empty(self):
870
"""For a non-empty branch, the result is ('ok', 'revno', 'revid')."""
871
backing = self.get_transport()
872
request = smart_branch.SmartServerBranchRequestLastRevisionInfo(backing)
873
tree = self.make_branch_and_memory_tree('.')
876
rev_id_utf8 = u'\xc8'.encode('utf-8')
877
r1 = tree.commit('1st commit')
878
r2 = tree.commit('2nd commit', rev_id=rev_id_utf8)
881
smart_req.SmartServerResponse((b'ok', b'2', rev_id_utf8)),
882
request.execute(b''))
885
class TestSmartServerBranchRequestRevisionIdToRevno(
886
tests.TestCaseWithMemoryTransport):
889
backing = self.get_transport()
890
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
892
self.make_branch('.')
893
self.assertEqual(smart_req.SmartServerResponse((b'ok', b'0')),
894
request.execute(b'', b'null:'))
896
def test_simple(self):
897
backing = self.get_transport()
898
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
900
tree = self.make_branch_and_memory_tree('.')
903
r1 = tree.commit('1st commit')
906
smart_req.SmartServerResponse((b'ok', b'1')),
907
request.execute(b'', r1))
909
def test_not_found(self):
910
backing = self.get_transport()
911
request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
913
branch = self.make_branch('.')
915
smart_req.FailedSmartServerResponse(
916
(b'NoSuchRevision', b'idontexist')),
917
request.execute(b'', b'idontexist'))
920
class TestSmartServerBranchRequestGetConfigFile(
921
tests.TestCaseWithMemoryTransport):
923
def test_default(self):
924
"""With no file, we get empty content."""
925
backing = self.get_transport()
926
request = smart_branch.SmartServerBranchGetConfigFile(backing)
927
branch = self.make_branch('.')
928
# there should be no file by default
930
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), content),
931
request.execute(b''))
933
def test_with_content(self):
934
# SmartServerBranchGetConfigFile should return the content from
935
# branch.control_files.get('branch.conf') for now - in the future it may
936
# perform more complex processing.
937
backing = self.get_transport()
938
request = smart_branch.SmartServerBranchGetConfigFile(backing)
939
branch = self.make_branch('.')
940
branch._transport.put_bytes('branch.conf', b'foo bar baz')
941
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'foo bar baz'),
942
request.execute(b''))
945
class TestLockedBranch(tests.TestCaseWithMemoryTransport):
947
def get_lock_tokens(self, branch):
948
branch_token = branch.lock_write().token
949
repo_token = branch.repository.lock_write().repository_token
950
branch.repository.unlock()
951
return branch_token, repo_token
954
class TestSmartServerBranchRequestPutConfigFile(TestLockedBranch):
956
def test_with_content(self):
957
backing = self.get_transport()
958
request = smart_branch.SmartServerBranchPutConfigFile(backing)
959
branch = self.make_branch('.')
960
branch_token, repo_token = self.get_lock_tokens(branch)
961
self.assertIs(None, request.execute(b'', branch_token, repo_token))
963
smart_req.SmartServerResponse((b'ok', )),
964
request.do_body(b'foo bar baz'))
966
branch.control_transport.get_bytes('branch.conf'),
971
class TestSmartServerBranchRequestSetConfigOption(TestLockedBranch):
973
def test_value_name(self):
974
branch = self.make_branch('.')
975
request = smart_branch.SmartServerBranchRequestSetConfigOption(
976
branch.controldir.root_transport)
977
branch_token, repo_token = self.get_lock_tokens(branch)
978
config = branch._get_config()
979
result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
981
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
982
self.assertEqual('bar', config.get_option('foo'))
986
def test_value_name_section(self):
987
branch = self.make_branch('.')
988
request = smart_branch.SmartServerBranchRequestSetConfigOption(
989
branch.controldir.root_transport)
990
branch_token, repo_token = self.get_lock_tokens(branch)
991
config = branch._get_config()
992
result = request.execute(b'', branch_token, repo_token, b'bar', b'foo',
994
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
995
self.assertEqual('bar', config.get_option('foo', 'gam'))
1000
class TestSmartServerBranchRequestSetConfigOptionDict(TestLockedBranch):
1003
TestLockedBranch.setUp(self)
1004
# A dict with non-ascii keys and values to exercise unicode
1006
self.encoded_value_dict = (
1007
b'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde')
1009
'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
1011
def test_value_name(self):
1012
branch = self.make_branch('.')
1013
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
1014
branch.controldir.root_transport)
1015
branch_token, repo_token = self.get_lock_tokens(branch)
1016
config = branch._get_config()
1017
result = request.execute(b'', branch_token, repo_token,
1018
self.encoded_value_dict, b'foo', b'')
1019
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1020
self.assertEqual(self.value_dict, config.get_option('foo'))
1024
def test_value_name_section(self):
1025
branch = self.make_branch('.')
1026
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
1027
branch.controldir.root_transport)
1028
branch_token, repo_token = self.get_lock_tokens(branch)
1029
config = branch._get_config()
1030
result = request.execute(b'', branch_token, repo_token,
1031
self.encoded_value_dict, b'foo', b'gam')
1032
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
1033
self.assertEqual(self.value_dict, config.get_option('foo', 'gam'))
1038
class TestSmartServerBranchRequestSetTagsBytes(TestLockedBranch):
1039
# Only called when the branch format and tags match [yay factory
1040
# methods] so only need to test straight forward cases.
1042
def test_set_bytes(self):
1043
base_branch = self.make_branch('base')
1044
tag_bytes = base_branch._get_tags_bytes()
1045
# get_lock_tokens takes out a lock.
1046
branch_token, repo_token = self.get_lock_tokens(base_branch)
1047
request = smart_branch.SmartServerBranchSetTagsBytes(
1048
self.get_transport())
1049
response = request.execute(b'base', branch_token, repo_token)
1050
self.assertEqual(None, response)
1051
response = request.do_chunk(tag_bytes)
1052
self.assertEqual(None, response)
1053
response = request.do_end()
1055
smart_req.SuccessfulSmartServerResponse(()), response)
1056
base_branch.unlock()
1058
def test_lock_failed(self):
1059
base_branch = self.make_branch('base')
1060
base_branch.lock_write()
1061
tag_bytes = base_branch._get_tags_bytes()
1062
request = smart_branch.SmartServerBranchSetTagsBytes(
1063
self.get_transport())
1064
self.assertRaises(errors.TokenMismatch, request.execute,
1065
b'base', b'wrong token', b'wrong token')
1066
# The request handler will keep processing the message parts, so even
1067
# if the request fails immediately do_chunk and do_end are still
1069
request.do_chunk(tag_bytes)
1071
base_branch.unlock()
1075
class SetLastRevisionTestBase(TestLockedBranch):
1076
"""Base test case for verbs that implement set_last_revision."""
1079
super(SetLastRevisionTestBase, self).setUp()
1080
backing_transport = self.get_transport()
1081
self.request = self.request_class(backing_transport)
1082
self.tree = self.make_branch_and_memory_tree('.')
1084
def lock_branch(self):
1085
return self.get_lock_tokens(self.tree.branch)
1087
def unlock_branch(self):
1088
self.tree.branch.unlock()
1090
def set_last_revision(self, revision_id, revno):
1091
branch_token, repo_token = self.lock_branch()
1092
response = self._set_last_revision(
1093
revision_id, revno, branch_token, repo_token)
1094
self.unlock_branch()
1097
def assertRequestSucceeds(self, revision_id, revno):
1098
response = self.set_last_revision(revision_id, revno)
1099
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
1103
class TestSetLastRevisionVerbMixin(object):
1104
"""Mixin test case for verbs that implement set_last_revision."""
1106
def test_set_null_to_null(self):
1107
"""An empty branch can have its last revision set to b'null:'."""
1108
self.assertRequestSucceeds(b'null:', 0)
1110
def test_NoSuchRevision(self):
1111
"""If the revision_id is not present, the verb returns NoSuchRevision.
1113
revision_id = b'non-existent revision'
1114
self.assertEqual(smart_req.FailedSmartServerResponse((b'NoSuchRevision',
1116
self.set_last_revision(revision_id, 1))
1118
def make_tree_with_two_commits(self):
1119
self.tree.lock_write()
1121
rev_id_utf8 = u'\xc8'.encode('utf-8')
1122
r1 = self.tree.commit('1st commit', rev_id=rev_id_utf8)
1123
r2 = self.tree.commit('2nd commit', rev_id=b'rev-2')
1126
def test_branch_last_revision_info_is_updated(self):
1127
"""A branch's tip can be set to a revision that is present in its
1130
# Make a branch with an empty revision history, but two revisions in
1132
self.make_tree_with_two_commits()
1133
rev_id_utf8 = u'\xc8'.encode('utf-8')
1134
self.tree.branch.set_last_revision_info(0, b'null:')
1136
(0, b'null:'), self.tree.branch.last_revision_info())
1137
# We can update the branch to a revision that is present in the
1139
self.assertRequestSucceeds(rev_id_utf8, 1)
1141
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1143
def test_branch_last_revision_info_rewind(self):
1144
"""A branch's tip can be set to a revision that is an ancestor of the
1147
self.make_tree_with_two_commits()
1148
rev_id_utf8 = u'\xc8'.encode('utf-8')
1150
(2, b'rev-2'), self.tree.branch.last_revision_info())
1151
self.assertRequestSucceeds(rev_id_utf8, 1)
1153
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1155
def test_TipChangeRejected(self):
1156
"""If a pre_change_branch_tip hook raises TipChangeRejected, the verb
1157
returns TipChangeRejected.
1159
rejection_message = u'rejection message\N{INTERROBANG}'
1160
def hook_that_rejects(params):
1161
raise errors.TipChangeRejected(rejection_message)
1162
_mod_branch.Branch.hooks.install_named_hook(
1163
'pre_change_branch_tip', hook_that_rejects, None)
1165
smart_req.FailedSmartServerResponse(
1166
(b'TipChangeRejected', rejection_message.encode('utf-8'))),
1167
self.set_last_revision(b'null:', 0))
1170
class TestSmartServerBranchRequestSetLastRevision(
1171
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1172
"""Tests for Branch.set_last_revision verb."""
1174
request_class = smart_branch.SmartServerBranchRequestSetLastRevision
1176
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1177
return self.request.execute(
1178
b'', branch_token, repo_token, revision_id)
1181
class TestSmartServerBranchRequestSetLastRevisionInfo(
1182
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1183
"""Tests for Branch.set_last_revision_info verb."""
1185
request_class = smart_branch.SmartServerBranchRequestSetLastRevisionInfo
1187
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1188
return self.request.execute(
1189
b'', branch_token, repo_token, revno, revision_id)
1191
def test_NoSuchRevision(self):
1192
"""Branch.set_last_revision_info does not have to return
1193
NoSuchRevision if the revision_id is absent.
1195
raise tests.TestNotApplicable()
1198
class TestSmartServerBranchRequestSetLastRevisionEx(
1199
SetLastRevisionTestBase, TestSetLastRevisionVerbMixin):
1200
"""Tests for Branch.set_last_revision_ex verb."""
1202
request_class = smart_branch.SmartServerBranchRequestSetLastRevisionEx
1204
def _set_last_revision(self, revision_id, revno, branch_token, repo_token):
1205
return self.request.execute(
1206
b'', branch_token, repo_token, revision_id, 0, 0)
1208
def assertRequestSucceeds(self, revision_id, revno):
1209
response = self.set_last_revision(revision_id, revno)
1211
smart_req.SuccessfulSmartServerResponse((b'ok', revno, revision_id)),
1214
def test_branch_last_revision_info_rewind(self):
1215
"""A branch's tip can be set to a revision that is an ancestor of the
1216
current tip, but only if allow_overwrite_descendant is passed.
1218
self.make_tree_with_two_commits()
1219
rev_id_utf8 = u'\xc8'.encode('utf-8')
1221
(2, b'rev-2'), self.tree.branch.last_revision_info())
1222
# If allow_overwrite_descendant flag is 0, then trying to set the tip
1223
# to an older revision ID has no effect.
1224
branch_token, repo_token = self.lock_branch()
1225
response = self.request.execute(
1226
b'', branch_token, repo_token, rev_id_utf8, 0, 0)
1228
smart_req.SuccessfulSmartServerResponse((b'ok', 2, b'rev-2')),
1231
(2, b'rev-2'), self.tree.branch.last_revision_info())
1233
# If allow_overwrite_descendant flag is 1, then setting the tip to an
1235
response = self.request.execute(
1236
b'', branch_token, repo_token, rev_id_utf8, 0, 1)
1238
smart_req.SuccessfulSmartServerResponse((b'ok', 1, rev_id_utf8)),
1240
self.unlock_branch()
1242
(1, rev_id_utf8), self.tree.branch.last_revision_info())
1244
def make_branch_with_divergent_history(self):
1245
"""Make a branch with divergent history in its repo.
1247
The branch's tip will be 'child-2', and the repo will also contain
1248
'child-1', which diverges from a common base revision.
1250
self.tree.lock_write()
1252
r1 = self.tree.commit('1st commit')
1253
revno_1, revid_1 = self.tree.branch.last_revision_info()
1254
r2 = self.tree.commit('2nd commit', rev_id=b'child-1')
1255
# Undo the second commit
1256
self.tree.branch.set_last_revision_info(revno_1, revid_1)
1257
self.tree.set_parent_ids([revid_1])
1258
# Make a new second commit, child-2. child-2 has diverged from
1260
new_r2 = self.tree.commit('2nd commit', rev_id=b'child-2')
1263
def test_not_allow_diverged(self):
1264
"""If allow_diverged is not passed, then setting a divergent history
1265
returns a Diverged error.
1267
self.make_branch_with_divergent_history()
1269
smart_req.FailedSmartServerResponse((b'Diverged',)),
1270
self.set_last_revision(b'child-1', 2))
1271
# The branch tip was not changed.
1272
self.assertEqual(b'child-2', self.tree.branch.last_revision())
1274
def test_allow_diverged(self):
1275
"""If allow_diverged is passed, then setting a divergent history
1278
self.make_branch_with_divergent_history()
1279
branch_token, repo_token = self.lock_branch()
1280
response = self.request.execute(
1281
b'', branch_token, repo_token, b'child-1', 1, 0)
1283
smart_req.SuccessfulSmartServerResponse((b'ok', 2, b'child-1')),
1285
self.unlock_branch()
1286
# The branch tip was changed.
1287
self.assertEqual(b'child-1', self.tree.branch.last_revision())
1290
class TestSmartServerBranchBreakLock(tests.TestCaseWithMemoryTransport):
1292
def test_lock_to_break(self):
1293
base_branch = self.make_branch('base')
1294
request = smart_branch.SmartServerBranchBreakLock(
1295
self.get_transport())
1296
base_branch.lock_write()
1298
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1299
request.execute(b'base'))
1301
def test_nothing_to_break(self):
1302
base_branch = self.make_branch('base')
1303
request = smart_branch.SmartServerBranchBreakLock(
1304
self.get_transport())
1306
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1307
request.execute(b'base'))
1310
class TestSmartServerBranchRequestGetParent(tests.TestCaseWithMemoryTransport):
1312
def test_get_parent_none(self):
1313
base_branch = self.make_branch('base')
1314
request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1315
response = request.execute(b'base')
1317
smart_req.SuccessfulSmartServerResponse((b'',)), response)
1319
def test_get_parent_something(self):
1320
base_branch = self.make_branch('base')
1321
base_branch.set_parent(self.get_url('foo'))
1322
request = smart_branch.SmartServerBranchGetParent(self.get_transport())
1323
response = request.execute(b'base')
1325
smart_req.SuccessfulSmartServerResponse((b"../foo",)),
1329
class TestSmartServerBranchRequestSetParent(TestLockedBranch):
1331
def test_set_parent_none(self):
1332
branch = self.make_branch('base', format="1.9")
1334
branch._set_parent_location('foo')
1336
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1337
self.get_transport())
1338
branch_token, repo_token = self.get_lock_tokens(branch)
1340
response = request.execute(b'base', branch_token, repo_token, b'')
1343
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1344
# Refresh branch as SetParentLocation modified it
1345
branch = branch.controldir.open_branch()
1346
self.assertEqual(None, branch.get_parent())
1348
def test_set_parent_something(self):
1349
branch = self.make_branch('base', format="1.9")
1350
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1351
self.get_transport())
1352
branch_token, repo_token = self.get_lock_tokens(branch)
1354
response = request.execute(b'base', branch_token, repo_token,
1358
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1359
refreshed = _mod_branch.Branch.open(branch.base)
1360
self.assertEqual('http://bar/', refreshed.get_parent())
1363
class TestSmartServerBranchRequestGetTagsBytes(
1364
tests.TestCaseWithMemoryTransport):
1365
# Only called when the branch format and tags match [yay factory
1366
# methods] so only need to test straight forward cases.
1368
def test_get_bytes(self):
1369
base_branch = self.make_branch('base')
1370
request = smart_branch.SmartServerBranchGetTagsBytes(
1371
self.get_transport())
1372
response = request.execute(b'base')
1374
smart_req.SuccessfulSmartServerResponse((b'',)), response)
1377
class TestSmartServerBranchRequestGetStackedOnURL(tests.TestCaseWithMemoryTransport):
1379
def test_get_stacked_on_url(self):
1380
base_branch = self.make_branch('base', format='1.6')
1381
stacked_branch = self.make_branch('stacked', format='1.6')
1382
# typically should be relative
1383
stacked_branch.set_stacked_on_url('../base')
1384
request = smart_branch.SmartServerBranchRequestGetStackedOnURL(
1385
self.get_transport())
1386
response = request.execute(b'stacked')
1388
smart_req.SmartServerResponse((b'ok', b'../base')),
1392
class TestSmartServerBranchRequestLockWrite(TestLockedBranch):
1394
def test_lock_write_on_unlocked_branch(self):
1395
backing = self.get_transport()
1396
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1397
branch = self.make_branch('.', format='knit')
1398
repository = branch.repository
1399
response = request.execute(b'')
1400
branch_nonce = branch.control_files._lock.peek().get('nonce')
1401
repository_nonce = repository.control_files._lock.peek().get('nonce')
1402
self.assertEqual(smart_req.SmartServerResponse(
1403
(b'ok', branch_nonce, repository_nonce)),
1405
# The branch (and associated repository) is now locked. Verify that
1406
# with a new branch object.
1407
new_branch = repository.controldir.open_branch()
1408
self.assertRaises(errors.LockContention, new_branch.lock_write)
1410
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1411
response = request.execute(b'', branch_nonce, repository_nonce)
1413
def test_lock_write_on_locked_branch(self):
1414
backing = self.get_transport()
1415
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1416
branch = self.make_branch('.')
1417
branch_token = branch.lock_write().token
1418
branch.leave_lock_in_place()
1420
response = request.execute(b'')
1422
smart_req.SmartServerResponse((b'LockContention',)), response)
1424
branch.lock_write(branch_token)
1425
branch.dont_leave_lock_in_place()
1428
def test_lock_write_with_tokens_on_locked_branch(self):
1429
backing = self.get_transport()
1430
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1431
branch = self.make_branch('.', format='knit')
1432
branch_token, repo_token = self.get_lock_tokens(branch)
1433
branch.leave_lock_in_place()
1434
branch.repository.leave_lock_in_place()
1436
response = request.execute(b'',
1437
branch_token, repo_token)
1439
smart_req.SmartServerResponse((b'ok', branch_token, repo_token)),
1442
branch.repository.lock_write(repo_token)
1443
branch.repository.dont_leave_lock_in_place()
1444
branch.repository.unlock()
1445
branch.lock_write(branch_token)
1446
branch.dont_leave_lock_in_place()
1449
def test_lock_write_with_mismatched_tokens_on_locked_branch(self):
1450
backing = self.get_transport()
1451
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1452
branch = self.make_branch('.', format='knit')
1453
branch_token, repo_token = self.get_lock_tokens(branch)
1454
branch.leave_lock_in_place()
1455
branch.repository.leave_lock_in_place()
1457
response = request.execute(b'',
1458
branch_token+b'xxx', repo_token)
1460
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1462
branch.repository.lock_write(repo_token)
1463
branch.repository.dont_leave_lock_in_place()
1464
branch.repository.unlock()
1465
branch.lock_write(branch_token)
1466
branch.dont_leave_lock_in_place()
1469
def test_lock_write_on_locked_repo(self):
1470
backing = self.get_transport()
1471
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1472
branch = self.make_branch('.', format='knit')
1473
repo = branch.repository
1474
repo_token = repo.lock_write().repository_token
1475
repo.leave_lock_in_place()
1477
response = request.execute(b'')
1479
smart_req.SmartServerResponse((b'LockContention',)), response)
1481
repo.lock_write(repo_token)
1482
repo.dont_leave_lock_in_place()
1485
def test_lock_write_on_readonly_transport(self):
1486
backing = self.get_readonly_transport()
1487
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1488
branch = self.make_branch('.')
1489
root = self.get_transport().clone('/')
1490
path = urlutils.relative_url(root.base, self.get_transport().base)
1491
response = request.execute(path.encode('utf-8'))
1492
error_name, lock_str, why_str = response.args
1493
self.assertFalse(response.is_successful())
1494
self.assertEqual(b'LockFailed', error_name)
1497
class TestSmartServerBranchRequestGetPhysicalLockStatus(TestLockedBranch):
1499
def test_true(self):
1500
backing = self.get_transport()
1501
request = smart_branch.SmartServerBranchRequestGetPhysicalLockStatus(
1503
branch = self.make_branch('.')
1504
branch_token, repo_token = self.get_lock_tokens(branch)
1505
self.assertEqual(True, branch.get_physical_lock_status())
1506
response = request.execute(b'')
1508
smart_req.SmartServerResponse((b'yes',)), response)
1511
def test_false(self):
1512
backing = self.get_transport()
1513
request = smart_branch.SmartServerBranchRequestGetPhysicalLockStatus(
1515
branch = self.make_branch('.')
1516
self.assertEqual(False, branch.get_physical_lock_status())
1517
response = request.execute(b'')
1519
smart_req.SmartServerResponse((b'no',)), response)
1522
class TestSmartServerBranchRequestUnlock(TestLockedBranch):
1524
def test_unlock_on_locked_branch_and_repo(self):
1525
backing = self.get_transport()
1526
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1527
branch = self.make_branch('.', format='knit')
1529
branch_token, repo_token = self.get_lock_tokens(branch)
1530
# Unlock the branch (and repo) object, leaving the physical locks
1532
branch.leave_lock_in_place()
1533
branch.repository.leave_lock_in_place()
1535
response = request.execute(b'',
1536
branch_token, repo_token)
1538
smart_req.SmartServerResponse((b'ok',)), response)
1539
# The branch is now unlocked. Verify that with a new branch
1541
new_branch = branch.controldir.open_branch()
1542
new_branch.lock_write()
1545
def test_unlock_on_unlocked_branch_unlocked_repo(self):
1546
backing = self.get_transport()
1547
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1548
branch = self.make_branch('.', format='knit')
1549
response = request.execute(
1550
b'', b'branch token', b'repo token')
1552
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1554
def test_unlock_on_unlocked_branch_locked_repo(self):
1555
backing = self.get_transport()
1556
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1557
branch = self.make_branch('.', format='knit')
1558
# Lock the repository.
1559
repo_token = branch.repository.lock_write().repository_token
1560
branch.repository.leave_lock_in_place()
1561
branch.repository.unlock()
1562
# Issue branch lock_write request on the unlocked branch (with locked
1564
response = request.execute(b'', b'branch token', repo_token)
1566
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
1568
branch.repository.lock_write(repo_token)
1569
branch.repository.dont_leave_lock_in_place()
1570
branch.repository.unlock()
1573
class TestSmartServerRepositoryRequest(tests.TestCaseWithMemoryTransport):
1575
def test_no_repository(self):
1576
"""Raise NoRepositoryPresent when there is a bzrdir and no repo."""
1577
# we test this using a shared repository above the named path,
1578
# thus checking the right search logic is used - that is, that
1579
# its the exact path being looked at and the server is not
1581
backing = self.get_transport()
1582
request = smart_repo.SmartServerRepositoryRequest(backing)
1583
self.make_repository('.', shared=True)
1584
self.make_controldir('subdir')
1585
self.assertRaises(errors.NoRepositoryPresent,
1586
request.execute, b'subdir')
1589
class TestSmartServerRepositoryAddSignatureText(tests.TestCaseWithMemoryTransport):
1591
def test_add_text(self):
1592
backing = self.get_transport()
1593
request = smart_repo.SmartServerRepositoryAddSignatureText(backing)
1594
tree = self.make_branch_and_memory_tree('.')
1595
write_token = tree.lock_write()
1596
self.addCleanup(tree.unlock)
1598
tree.commit("Message", rev_id=b'rev1')
1599
tree.branch.repository.start_write_group()
1600
write_group_tokens = tree.branch.repository.suspend_write_group()
1601
self.assertEqual(None, request.execute(b'', write_token,
1602
b'rev1', *[token.encode('utf-8') for token in write_group_tokens]))
1603
response = request.do_body(b'somesignature')
1604
self.assertTrue(response.is_successful())
1605
self.assertEqual(response.args[0], b'ok')
1606
write_group_tokens = [token.decode('utf-8') for token in response.args[1:]]
1607
tree.branch.repository.resume_write_group(write_group_tokens)
1608
tree.branch.repository.commit_write_group()
1610
self.assertEqual(b"somesignature",
1611
tree.branch.repository.get_signature_text(b"rev1"))
1614
class TestSmartServerRepositoryAllRevisionIds(
1615
tests.TestCaseWithMemoryTransport):
1617
def test_empty(self):
1618
"""An empty body should be returned for an empty repository."""
1619
backing = self.get_transport()
1620
request = smart_repo.SmartServerRepositoryAllRevisionIds(backing)
1621
self.make_repository('.')
1623
smart_req.SuccessfulSmartServerResponse((b"ok", ), b""),
1624
request.execute(b''))
1626
def test_some_revisions(self):
1627
"""An empty body should be returned for an empty repository."""
1628
backing = self.get_transport()
1629
request = smart_repo.SmartServerRepositoryAllRevisionIds(backing)
1630
tree = self.make_branch_and_memory_tree('.')
1633
tree.commit(rev_id=b'origineel', message="message")
1634
tree.commit(rev_id=b'nog-een-revisie', message="message")
1637
request.execute(b''),
1638
[smart_req.SuccessfulSmartServerResponse((b"ok", ),
1639
b"origineel\nnog-een-revisie"),
1640
smart_req.SuccessfulSmartServerResponse((b"ok", ),
1641
b"nog-een-revisie\norigineel")])
1644
class TestSmartServerRepositoryBreakLock(tests.TestCaseWithMemoryTransport):
1646
def test_lock_to_break(self):
1647
backing = self.get_transport()
1648
request = smart_repo.SmartServerRepositoryBreakLock(backing)
1649
tree = self.make_branch_and_memory_tree('.')
1650
tree.branch.repository.lock_write()
1652
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1653
request.execute(b''))
1655
def test_nothing_to_break(self):
1656
backing = self.get_transport()
1657
request = smart_repo.SmartServerRepositoryBreakLock(backing)
1658
tree = self.make_branch_and_memory_tree('.')
1660
smart_req.SuccessfulSmartServerResponse((b'ok', ), None),
1661
request.execute(b''))
1664
class TestSmartServerRepositoryGetParentMap(tests.TestCaseWithMemoryTransport):
1666
def test_trivial_bzipped(self):
1667
# This tests that the wire encoding is actually bzipped
1668
backing = self.get_transport()
1669
request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1670
tree = self.make_branch_and_memory_tree('.')
1672
self.assertEqual(None,
1673
request.execute(b'', b'missing-id'))
1674
# Note that it returns a body that is bzipped.
1676
smart_req.SuccessfulSmartServerResponse((b'ok', ), bz2.compress(b'')),
1677
request.do_body(b'\n\n0\n'))
1679
def test_trivial_include_missing(self):
1680
backing = self.get_transport()
1681
request = smart_repo.SmartServerRepositoryGetParentMap(backing)
1682
tree = self.make_branch_and_memory_tree('.')
1684
self.assertEqual(None,
1685
request.execute(b'', b'missing-id', b'include-missing:'))
1687
smart_req.SuccessfulSmartServerResponse((b'ok', ),
1688
bz2.compress(b'missing:missing-id')),
1689
request.do_body(b'\n\n0\n'))
1692
class TestSmartServerRepositoryGetRevisionGraph(
1693
tests.TestCaseWithMemoryTransport):
1695
def test_none_argument(self):
1696
backing = self.get_transport()
1697
request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1698
tree = self.make_branch_and_memory_tree('.')
1701
r1 = tree.commit('1st commit')
1702
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1705
# the lines of revision_id->revision_parent_list has no guaranteed
1706
# order coming out of a dict, so sort both our test and response
1707
lines = sorted([b' '.join([r2, r1]), r1])
1708
response = request.execute(b'', b'')
1709
response.body = b'\n'.join(sorted(response.body.split(b'\n')))
1712
smart_req.SmartServerResponse((b'ok', ), b'\n'.join(lines)), response)
1714
def test_specific_revision_argument(self):
1715
backing = self.get_transport()
1716
request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1717
tree = self.make_branch_and_memory_tree('.')
1720
rev_id_utf8 = u'\xc9'.encode('utf-8')
1721
r1 = tree.commit('1st commit', rev_id=rev_id_utf8)
1722
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1725
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), rev_id_utf8),
1726
request.execute(b'', rev_id_utf8))
1728
def test_no_such_revision(self):
1729
backing = self.get_transport()
1730
request = smart_repo.SmartServerRepositoryGetRevisionGraph(backing)
1731
tree = self.make_branch_and_memory_tree('.')
1734
r1 = tree.commit('1st commit')
1737
# Note that it still returns body (of zero bytes).
1738
self.assertEqual(smart_req.SmartServerResponse(
1739
(b'nosuchrevision', b'missingrevision', ), b''),
1740
request.execute(b'', b'missingrevision'))
1743
class TestSmartServerRepositoryGetRevIdForRevno(
1744
tests.TestCaseWithMemoryTransport):
1746
def test_revno_found(self):
1747
backing = self.get_transport()
1748
request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1749
tree = self.make_branch_and_memory_tree('.')
1752
rev1_id_utf8 = u'\xc8'.encode('utf-8')
1753
rev2_id_utf8 = u'\xc9'.encode('utf-8')
1754
tree.commit('1st commit', rev_id=rev1_id_utf8)
1755
tree.commit('2nd commit', rev_id=rev2_id_utf8)
1758
self.assertEqual(smart_req.SmartServerResponse((b'ok', rev1_id_utf8)),
1759
request.execute(b'', 1, (2, rev2_id_utf8)))
1761
def test_known_revid_missing(self):
1762
backing = self.get_transport()
1763
request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1764
repo = self.make_repository('.')
1766
smart_req.FailedSmartServerResponse((b'nosuchrevision', b'ghost')),
1767
request.execute(b'', 1, (2, b'ghost')))
1769
def test_history_incomplete(self):
1770
backing = self.get_transport()
1771
request = smart_repo.SmartServerRepositoryGetRevIdForRevno(backing)
1772
parent = self.make_branch_and_memory_tree('parent', format='1.9')
1774
parent.add([''], [b'TREE_ROOT'])
1775
r1 = parent.commit(message='first commit')
1776
r2 = parent.commit(message='second commit')
1778
local = self.make_branch_and_memory_tree('local', format='1.9')
1779
local.branch.pull(parent.branch)
1780
local.set_parent_ids([r2])
1781
r3 = local.commit(message='local commit')
1782
local.branch.create_clone_on_transport(
1783
self.get_transport('stacked'), stacked_on=self.get_url('parent'))
1785
smart_req.SmartServerResponse((b'history-incomplete', 2, r2)),
1786
request.execute(b'stacked', 1, (3, r3)))
1789
class TestSmartServerRepositoryIterRevisions(
1790
tests.TestCaseWithMemoryTransport):
1792
def test_basic(self):
1793
backing = self.get_transport()
1794
request = smart_repo.SmartServerRepositoryIterRevisions(backing)
1795
tree = self.make_branch_and_memory_tree('.', format='2a')
1798
tree.commit('1st commit', rev_id=b"rev1")
1799
tree.commit('2nd commit', rev_id=b"rev2")
1802
self.assertIs(None, request.execute(b''))
1803
response = request.do_body(b"rev1\nrev2")
1804
self.assertTrue(response.is_successful())
1805
# Format 2a uses serializer format 10
1806
self.assertEqual(response.args, (b"ok", b"10"))
1808
self.addCleanup(tree.branch.lock_read().unlock)
1809
entries = [zlib.compress(record.get_bytes_as("fulltext")) for record in
1810
tree.branch.repository.revisions.get_record_stream(
1811
[(b"rev1", ), (b"rev2", )], "unordered", True)]
1813
contents = b"".join(response.body_stream)
1814
self.assertTrue(contents in (
1815
b"".join([entries[0], entries[1]]),
1816
b"".join([entries[1], entries[0]])))
1818
def test_missing(self):
1819
backing = self.get_transport()
1820
request = smart_repo.SmartServerRepositoryIterRevisions(backing)
1821
tree = self.make_branch_and_memory_tree('.', format='2a')
1823
self.assertIs(None, request.execute(b''))
1824
response = request.do_body(b"rev1\nrev2")
1825
self.assertTrue(response.is_successful())
1826
# Format 2a uses serializer format 10
1827
self.assertEqual(response.args, (b"ok", b"10"))
1829
contents = b"".join(response.body_stream)
1830
self.assertEqual(contents, b"")
1833
class GetStreamTestBase(tests.TestCaseWithMemoryTransport):
1835
def make_two_commit_repo(self):
1836
tree = self.make_branch_and_memory_tree('.')
1839
r1 = tree.commit('1st commit')
1840
r2 = tree.commit('2nd commit', rev_id=u'\xc8'.encode('utf-8'))
1842
repo = tree.branch.repository
1846
class TestSmartServerRepositoryGetStream(GetStreamTestBase):
1848
def test_ancestry_of(self):
1849
"""The search argument may be a 'ancestry-of' some heads'."""
1850
backing = self.get_transport()
1851
request = smart_repo.SmartServerRepositoryGetStream(backing)
1852
repo, r1, r2 = self.make_two_commit_repo()
1853
fetch_spec = [b'ancestry-of', r2]
1854
lines = b'\n'.join(fetch_spec)
1855
request.execute(b'', repo._format.network_name())
1856
response = request.do_body(lines)
1857
self.assertEqual((b'ok',), response.args)
1858
stream_bytes = b''.join(response.body_stream)
1859
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
1861
def test_search(self):
1862
"""The search argument may be a 'search' of some explicit keys."""
1863
backing = self.get_transport()
1864
request = smart_repo.SmartServerRepositoryGetStream(backing)
1865
repo, r1, r2 = self.make_two_commit_repo()
1866
fetch_spec = [b'search', r1 + b' ' + r2, b'null:', b'2']
1867
lines = b'\n'.join(fetch_spec)
1868
request.execute(b'', repo._format.network_name())
1869
response = request.do_body(lines)
1870
self.assertEqual((b'ok',), response.args)
1871
stream_bytes = b''.join(response.body_stream)
1872
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
1874
def test_search_everything(self):
1875
"""A search of 'everything' returns a stream."""
1876
backing = self.get_transport()
1877
request = smart_repo.SmartServerRepositoryGetStream_1_19(backing)
1878
repo, r1, r2 = self.make_two_commit_repo()
1879
serialised_fetch_spec = b'everything'
1880
request.execute(b'', repo._format.network_name())
1881
response = request.do_body(serialised_fetch_spec)
1882
self.assertEqual((b'ok',), response.args)
1883
stream_bytes = b''.join(response.body_stream)
1884
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
1887
class TestSmartServerRequestHasRevision(tests.TestCaseWithMemoryTransport):
1889
def test_missing_revision(self):
1890
"""For a missing revision, ('no', ) is returned."""
1891
backing = self.get_transport()
1892
request = smart_repo.SmartServerRequestHasRevision(backing)
1893
self.make_repository('.')
1894
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
1895
request.execute(b'', b'revid'))
1897
def test_present_revision(self):
1898
"""For a present revision, ('yes', ) is returned."""
1899
backing = self.get_transport()
1900
request = smart_repo.SmartServerRequestHasRevision(backing)
1901
tree = self.make_branch_and_memory_tree('.')
1904
rev_id_utf8 = u'\xc8abc'.encode('utf-8')
1905
r1 = tree.commit('a commit', rev_id=rev_id_utf8)
1907
self.assertTrue(tree.branch.repository.has_revision(rev_id_utf8))
1908
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
1909
request.execute(b'', rev_id_utf8))
1912
class TestSmartServerRepositoryIterFilesBytes(tests.TestCaseWithTransport):
1914
def test_single(self):
1915
backing = self.get_transport()
1916
request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
1917
t = self.make_branch_and_tree('.')
1918
self.addCleanup(t.lock_write().unlock)
1919
self.build_tree_contents([("file", b"somecontents")])
1920
t.add(["file"], [b"thefileid"])
1921
t.commit(rev_id=b'somerev', message="add file")
1922
self.assertIs(None, request.execute(b''))
1923
response = request.do_body(b"thefileid\0somerev\n")
1924
self.assertTrue(response.is_successful())
1925
self.assertEqual(response.args, (b"ok", ))
1926
self.assertEqual(b"".join(response.body_stream),
1927
b"ok\x000\n" + zlib.compress(b"somecontents"))
1929
def test_missing(self):
1930
backing = self.get_transport()
1931
request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
1932
t = self.make_branch_and_tree('.')
1933
self.addCleanup(t.lock_write().unlock)
1934
self.assertIs(None, request.execute(b''))
1935
response = request.do_body(b"thefileid\0revision\n")
1936
self.assertTrue(response.is_successful())
1937
self.assertEqual(response.args, (b"ok", ))
1938
self.assertEqual(b"".join(response.body_stream),
1939
b"absent\x00thefileid\x00revision\x000\n")
1942
class TestSmartServerRequestHasSignatureForRevisionId(
1943
tests.TestCaseWithMemoryTransport):
1945
def test_missing_revision(self):
1946
"""For a missing revision, NoSuchRevision is returned."""
1947
backing = self.get_transport()
1948
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1950
self.make_repository('.')
1952
smart_req.FailedSmartServerResponse(
1953
(b'nosuchrevision', b'revid'), None),
1954
request.execute(b'', b'revid'))
1956
def test_missing_signature(self):
1957
"""For a missing signature, ('no', ) is returned."""
1958
backing = self.get_transport()
1959
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1961
tree = self.make_branch_and_memory_tree('.')
1964
r1 = tree.commit('a commit', rev_id=b'A')
1966
self.assertTrue(tree.branch.repository.has_revision(b'A'))
1967
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
1968
request.execute(b'', b'A'))
1970
def test_present_signature(self):
1971
"""For a present signature, ('yes', ) is returned."""
1972
backing = self.get_transport()
1973
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1975
strategy = gpg.LoopbackGPGStrategy(None)
1976
tree = self.make_branch_and_memory_tree('.')
1979
r1 = tree.commit('a commit', rev_id=b'A')
1980
tree.branch.repository.start_write_group()
1981
tree.branch.repository.sign_revision(b'A', strategy)
1982
tree.branch.repository.commit_write_group()
1984
self.assertTrue(tree.branch.repository.has_revision(b'A'))
1985
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
1986
request.execute(b'', b'A'))
1989
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
1991
def test_empty_revid(self):
1992
"""With an empty revid, we get only size an number and revisions"""
1993
backing = self.get_transport()
1994
request = smart_repo.SmartServerRepositoryGatherStats(backing)
1995
repository = self.make_repository('.')
1996
stats = repository.gather_stats()
1997
expected_body = b'revisions: 0\n'
1998
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), expected_body),
1999
request.execute(b'', b'', b'no'))
2001
def test_revid_with_committers(self):
2002
"""For a revid we get more infos."""
2003
backing = self.get_transport()
2004
rev_id_utf8 = u'\xc8abc'.encode('utf-8')
2005
request = smart_repo.SmartServerRepositoryGatherStats(backing)
2006
tree = self.make_branch_and_memory_tree('.')
2009
# Let's build a predictable result
2010
tree.commit('a commit', timestamp=123456.2, timezone=3600)
2011
tree.commit('a commit', timestamp=654321.4, timezone=0,
2015
stats = tree.branch.repository.gather_stats()
2016
expected_body = (b'firstrev: 123456.200 3600\n'
2017
b'latestrev: 654321.400 0\n'
2019
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), expected_body),
2020
request.execute(b'', rev_id_utf8, b'no'))
2022
def test_not_empty_repository_with_committers(self):
2023
"""For a revid and requesting committers we get the whole thing."""
2024
backing = self.get_transport()
2025
rev_id_utf8 = u'\xc8abc'.encode('utf-8')
2026
request = smart_repo.SmartServerRepositoryGatherStats(backing)
2027
tree = self.make_branch_and_memory_tree('.')
2030
# Let's build a predictable result
2031
tree.commit('a commit', timestamp=123456.2, timezone=3600,
2033
tree.commit('a commit', timestamp=654321.4, timezone=0,
2034
committer='bar', rev_id=rev_id_utf8)
2036
stats = tree.branch.repository.gather_stats()
2038
expected_body = (b'committers: 2\n'
2039
b'firstrev: 123456.200 3600\n'
2040
b'latestrev: 654321.400 0\n'
2042
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), expected_body),
2043
request.execute(b'',
2044
rev_id_utf8, b'yes'))
2046
def test_unknown_revid(self):
2047
"""An unknown revision id causes a 'nosuchrevision' error."""
2048
backing = self.get_transport()
2049
request = smart_repo.SmartServerRepositoryGatherStats(backing)
2050
repository = self.make_repository('.')
2051
expected_body = b'revisions: 0\n'
2053
smart_req.FailedSmartServerResponse(
2054
(b'nosuchrevision', b'mia'), None),
2055
request.execute(b'', b'mia', b'yes'))
2058
class TestSmartServerRepositoryIsShared(tests.TestCaseWithMemoryTransport):
2060
def test_is_shared(self):
2061
"""For a shared repository, ('yes', ) is returned."""
2062
backing = self.get_transport()
2063
request = smart_repo.SmartServerRepositoryIsShared(backing)
2064
self.make_repository('.', shared=True)
2065
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2066
request.execute(b'', ))
2068
def test_is_not_shared(self):
2069
"""For a shared repository, ('no', ) is returned."""
2070
backing = self.get_transport()
2071
request = smart_repo.SmartServerRepositoryIsShared(backing)
2072
self.make_repository('.', shared=False)
2073
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2074
request.execute(b'', ))
2077
class TestSmartServerRepositoryGetRevisionSignatureText(
2078
tests.TestCaseWithMemoryTransport):
2080
def test_get_signature(self):
2081
backing = self.get_transport()
2082
request = smart_repo.SmartServerRepositoryGetRevisionSignatureText(
2084
bb = self.make_branch_builder('.')
2085
bb.build_commit(rev_id=b'A')
2086
repo = bb.get_branch().repository
2087
strategy = gpg.LoopbackGPGStrategy(None)
2088
self.addCleanup(repo.lock_write().unlock)
2089
repo.start_write_group()
2090
repo.sign_revision(b'A', strategy)
2091
repo.commit_write_group()
2093
b'-----BEGIN PSEUDO-SIGNED CONTENT-----\n' +
2094
Testament.from_revision(repo, b'A').as_short_text() +
2095
b'-----END PSEUDO-SIGNED CONTENT-----\n')
2097
smart_req.SmartServerResponse((b'ok', ), expected_body),
2098
request.execute(b'', b'A'))
2101
class TestSmartServerRepositoryMakeWorkingTrees(
2102
tests.TestCaseWithMemoryTransport):
2104
def test_make_working_trees(self):
2105
"""For a repository with working trees, ('yes', ) is returned."""
2106
backing = self.get_transport()
2107
request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
2108
r = self.make_repository('.')
2109
r.set_make_working_trees(True)
2110
self.assertEqual(smart_req.SmartServerResponse((b'yes', )),
2111
request.execute(b'', ))
2113
def test_is_not_shared(self):
2114
"""For a repository with working trees, ('no', ) is returned."""
2115
backing = self.get_transport()
2116
request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
2117
r = self.make_repository('.')
2118
r.set_make_working_trees(False)
2119
self.assertEqual(smart_req.SmartServerResponse((b'no', )),
2120
request.execute(b'', ))
2123
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
2125
def test_lock_write_on_unlocked_repo(self):
2126
backing = self.get_transport()
2127
request = smart_repo.SmartServerRepositoryLockWrite(backing)
2128
repository = self.make_repository('.', format='knit')
2129
response = request.execute(b'')
2130
nonce = repository.control_files._lock.peek().get('nonce')
2131
self.assertEqual(smart_req.SmartServerResponse((b'ok', nonce)), response)
2132
# The repository is now locked. Verify that with a new repository
2134
new_repo = repository.controldir.open_repository()
2135
self.assertRaises(errors.LockContention, new_repo.lock_write)
2137
request = smart_repo.SmartServerRepositoryUnlock(backing)
2138
response = request.execute(b'', nonce)
2140
def test_lock_write_on_locked_repo(self):
2141
backing = self.get_transport()
2142
request = smart_repo.SmartServerRepositoryLockWrite(backing)
2143
repository = self.make_repository('.', format='knit')
2144
repo_token = repository.lock_write().repository_token
2145
repository.leave_lock_in_place()
2147
response = request.execute(b'')
2149
smart_req.SmartServerResponse((b'LockContention',)), response)
2151
repository.lock_write(repo_token)
2152
repository.dont_leave_lock_in_place()
2155
def test_lock_write_on_readonly_transport(self):
2156
backing = self.get_readonly_transport()
2157
request = smart_repo.SmartServerRepositoryLockWrite(backing)
2158
repository = self.make_repository('.', format='knit')
2159
response = request.execute(b'')
2160
self.assertFalse(response.is_successful())
2161
self.assertEqual(b'LockFailed', response.args[0])
2164
class TestInsertStreamBase(tests.TestCaseWithMemoryTransport):
2166
def make_empty_byte_stream(self, repo):
2167
byte_stream = smart_repo._stream_to_byte_stream([], repo._format)
2168
return b''.join(byte_stream)
2171
class TestSmartServerRepositoryInsertStream(TestInsertStreamBase):
2173
def test_insert_stream_empty(self):
2174
backing = self.get_transport()
2175
request = smart_repo.SmartServerRepositoryInsertStream(backing)
2176
repository = self.make_repository('.')
2177
response = request.execute(b'', b'')
2178
self.assertEqual(None, response)
2179
response = request.do_chunk(self.make_empty_byte_stream(repository))
2180
self.assertEqual(None, response)
2181
response = request.do_end()
2182
self.assertEqual(smart_req.SmartServerResponse((b'ok', )), response)
2185
class TestSmartServerRepositoryInsertStreamLocked(TestInsertStreamBase):
2187
def test_insert_stream_empty(self):
2188
backing = self.get_transport()
2189
request = smart_repo.SmartServerRepositoryInsertStreamLocked(
2191
repository = self.make_repository('.', format='knit')
2192
lock_token = repository.lock_write().repository_token
2193
response = request.execute(b'', b'', lock_token)
2194
self.assertEqual(None, response)
2195
response = request.do_chunk(self.make_empty_byte_stream(repository))
2196
self.assertEqual(None, response)
2197
response = request.do_end()
2198
self.assertEqual(smart_req.SmartServerResponse((b'ok', )), response)
2201
def test_insert_stream_with_wrong_lock_token(self):
2202
backing = self.get_transport()
2203
request = smart_repo.SmartServerRepositoryInsertStreamLocked(
2205
repository = self.make_repository('.', format='knit')
2206
lock_token = repository.lock_write().repository_token
2208
errors.TokenMismatch, request.execute, b'', b'', b'wrong-token')
2212
class TestSmartServerRepositoryUnlock(tests.TestCaseWithMemoryTransport):
2214
def test_unlock_on_locked_repo(self):
2215
backing = self.get_transport()
2216
request = smart_repo.SmartServerRepositoryUnlock(backing)
2217
repository = self.make_repository('.', format='knit')
2218
token = repository.lock_write().repository_token
2219
repository.leave_lock_in_place()
2221
response = request.execute(b'', token)
2223
smart_req.SmartServerResponse((b'ok',)), response)
2224
# The repository is now unlocked. Verify that with a new repository
2226
new_repo = repository.controldir.open_repository()
2227
new_repo.lock_write()
2230
def test_unlock_on_unlocked_repo(self):
2231
backing = self.get_transport()
2232
request = smart_repo.SmartServerRepositoryUnlock(backing)
2233
repository = self.make_repository('.', format='knit')
2234
response = request.execute(b'', b'some token')
2236
smart_req.SmartServerResponse((b'TokenMismatch',)), response)
2239
class TestSmartServerRepositoryGetPhysicalLockStatus(
2240
tests.TestCaseWithTransport):
2242
def test_with_write_lock(self):
2243
backing = self.get_transport()
2244
repo = self.make_repository('.')
2245
self.addCleanup(repo.lock_write().unlock)
2246
# lock_write() doesn't necessarily actually take a physical
2248
if repo.get_physical_lock_status():
2252
request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2253
request = request_class(backing)
2254
self.assertEqual(smart_req.SuccessfulSmartServerResponse((expected,)),
2255
request.execute(b'', ))
2257
def test_without_write_lock(self):
2258
backing = self.get_transport()
2259
repo = self.make_repository('.')
2260
self.assertEqual(False, repo.get_physical_lock_status())
2261
request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
2262
request = request_class(backing)
2263
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'no',)),
2264
request.execute(b'', ))
2267
class TestSmartServerRepositoryReconcile(tests.TestCaseWithTransport):
2269
def test_reconcile(self):
2270
backing = self.get_transport()
2271
repo = self.make_repository('.')
2272
token = repo.lock_write().repository_token
2273
self.addCleanup(repo.unlock)
2274
request_class = smart_repo.SmartServerRepositoryReconcile
2275
request = request_class(backing)
2276
self.assertEqual(smart_req.SuccessfulSmartServerResponse(
2278
b'garbage_inventories: 0\n'
2279
b'inconsistent_parents: 0\n'),
2280
request.execute(b'', token))
2283
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
2285
def test_is_readonly_no(self):
2286
backing = self.get_transport()
2287
request = smart_req.SmartServerIsReadonly(backing)
2288
response = request.execute()
2290
smart_req.SmartServerResponse((b'no',)), response)
2292
def test_is_readonly_yes(self):
2293
backing = self.get_readonly_transport()
2294
request = smart_req.SmartServerIsReadonly(backing)
2295
response = request.execute()
2297
smart_req.SmartServerResponse((b'yes',)), response)
2300
class TestSmartServerRepositorySetMakeWorkingTrees(
2301
tests.TestCaseWithMemoryTransport):
2303
def test_set_false(self):
2304
backing = self.get_transport()
2305
repo = self.make_repository('.', shared=True)
2306
repo.set_make_working_trees(True)
2307
request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2308
request = request_class(backing)
2309
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2310
request.execute(b'', b'False'))
2311
repo = repo.controldir.open_repository()
2312
self.assertFalse(repo.make_working_trees())
2314
def test_set_true(self):
2315
backing = self.get_transport()
2316
repo = self.make_repository('.', shared=True)
2317
repo.set_make_working_trees(False)
2318
request_class = smart_repo.SmartServerRepositorySetMakeWorkingTrees
2319
request = request_class(backing)
2320
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2321
request.execute(b'', b'True'))
2322
repo = repo.controldir.open_repository()
2323
self.assertTrue(repo.make_working_trees())
2326
class TestSmartServerRepositoryGetSerializerFormat(
2327
tests.TestCaseWithMemoryTransport):
2329
def test_get_serializer_format(self):
2330
backing = self.get_transport()
2331
repo = self.make_repository('.', format='2a')
2332
request_class = smart_repo.SmartServerRepositoryGetSerializerFormat
2333
request = request_class(backing)
2335
smart_req.SuccessfulSmartServerResponse((b'ok', b'10')),
2336
request.execute(b''))
2339
class TestSmartServerRepositoryWriteGroup(
2340
tests.TestCaseWithMemoryTransport):
2342
def test_start_write_group(self):
2343
backing = self.get_transport()
2344
repo = self.make_repository('.')
2345
lock_token = repo.lock_write().repository_token
2346
self.addCleanup(repo.unlock)
2347
request_class = smart_repo.SmartServerRepositoryStartWriteGroup
2348
request = request_class(backing)
2349
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok', [])),
2350
request.execute(b'', lock_token))
2352
def test_start_write_group_unsuspendable(self):
2353
backing = self.get_transport()
2354
repo = self.make_repository('.', format='knit')
2355
lock_token = repo.lock_write().repository_token
2356
self.addCleanup(repo.unlock)
2357
request_class = smart_repo.SmartServerRepositoryStartWriteGroup
2358
request = request_class(backing)
2360
smart_req.FailedSmartServerResponse((b'UnsuspendableWriteGroup',)),
2361
request.execute(b'', lock_token))
2363
def test_commit_write_group(self):
2364
backing = self.get_transport()
2365
repo = self.make_repository('.')
2366
lock_token = repo.lock_write().repository_token
2367
self.addCleanup(repo.unlock)
2368
repo.start_write_group()
2369
tokens = repo.suspend_write_group()
2370
request_class = smart_repo.SmartServerRepositoryCommitWriteGroup
2371
request = request_class(backing)
2372
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2373
request.execute(b'', lock_token, tokens))
2375
def test_abort_write_group(self):
2376
backing = self.get_transport()
2377
repo = self.make_repository('.')
2378
lock_token = repo.lock_write().repository_token
2379
repo.start_write_group()
2380
tokens = repo.suspend_write_group()
2381
self.addCleanup(repo.unlock)
2382
request_class = smart_repo.SmartServerRepositoryAbortWriteGroup
2383
request = request_class(backing)
2384
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2385
request.execute(b'', lock_token, tokens))
2387
def test_check_write_group(self):
2388
backing = self.get_transport()
2389
repo = self.make_repository('.')
2390
lock_token = repo.lock_write().repository_token
2391
repo.start_write_group()
2392
tokens = repo.suspend_write_group()
2393
self.addCleanup(repo.unlock)
2394
request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
2395
request = request_class(backing)
2396
self.assertEqual(smart_req.SuccessfulSmartServerResponse((b'ok',)),
2397
request.execute(b'', lock_token, tokens))
2399
def test_check_write_group_invalid(self):
2400
backing = self.get_transport()
2401
repo = self.make_repository('.')
2402
lock_token = repo.lock_write().repository_token
2403
self.addCleanup(repo.unlock)
2404
request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
2405
request = request_class(backing)
2406
self.assertEqual(smart_req.FailedSmartServerResponse(
2407
(b'UnresumableWriteGroup', [b'random'],
2408
b'Malformed write group token')),
2409
request.execute(b'', lock_token, [b"random"]))
2412
class TestSmartServerPackRepositoryAutopack(tests.TestCaseWithTransport):
2414
def make_repo_needing_autopacking(self, path='.'):
2415
# Make a repo in need of autopacking.
2416
tree = self.make_branch_and_tree('.', format='pack-0.92')
2417
repo = tree.branch.repository
2418
# monkey-patch the pack collection to disable autopacking
2419
repo._pack_collection._max_pack_count = lambda count: count
2421
tree.commit('commit %s' % x)
2422
self.assertEqual(10, len(repo._pack_collection.names()))
2423
del repo._pack_collection._max_pack_count
2426
def test_autopack_needed(self):
2427
repo = self.make_repo_needing_autopacking()
2429
self.addCleanup(repo.unlock)
2430
backing = self.get_transport()
2431
request = smart_packrepo.SmartServerPackRepositoryAutopack(
2433
response = request.execute(b'')
2434
self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
2435
repo._pack_collection.reload_pack_names()
2436
self.assertEqual(1, len(repo._pack_collection.names()))
2438
def test_autopack_not_needed(self):
2439
tree = self.make_branch_and_tree('.', format='pack-0.92')
2440
repo = tree.branch.repository
2442
self.addCleanup(repo.unlock)
2444
tree.commit('commit %s' % x)
2445
backing = self.get_transport()
2446
request = smart_packrepo.SmartServerPackRepositoryAutopack(
2448
response = request.execute(b'')
2449
self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
2450
repo._pack_collection.reload_pack_names()
2451
self.assertEqual(9, len(repo._pack_collection.names()))
2453
def test_autopack_on_nonpack_format(self):
2454
"""A request to autopack a non-pack repo is a no-op."""
2455
repo = self.make_repository('.', format='knit')
2456
backing = self.get_transport()
2457
request = smart_packrepo.SmartServerPackRepositoryAutopack(
2459
response = request.execute(b'')
2460
self.assertEqual(smart_req.SmartServerResponse((b'ok',)), response)
2463
class TestSmartServerVfsGet(tests.TestCaseWithMemoryTransport):
2465
def test_unicode_path(self):
2466
"""VFS requests expect unicode paths to be escaped."""
2467
filename = u'foo\N{INTERROBANG}'
2468
filename_escaped = urlutils.escape(filename)
2469
backing = self.get_transport()
2470
request = vfs.GetRequest(backing)
2471
backing.put_bytes_non_atomic(filename_escaped, b'contents')
2472
self.assertEqual(smart_req.SmartServerResponse((b'ok', ), b'contents'),
2473
request.execute(filename_escaped.encode('ascii')))
2476
class TestHandlers(tests.TestCase):
2477
"""Tests for the request.request_handlers object."""
2479
def test_all_registrations_exist(self):
2480
"""All registered request_handlers can be found."""
2481
# If there's a typo in a register_lazy call, this loop will fail with
2482
# an AttributeError.
2483
for key in smart_req.request_handlers.keys():
2485
item = smart_req.request_handlers.get(key)
2486
except AttributeError as e:
2487
raise AttributeError('failed to get %s: %s' % (key, e))
2489
def assertHandlerEqual(self, verb, handler):
2490
self.assertEqual(smart_req.request_handlers.get(verb), handler)
2492
def test_registered_methods(self):
2493
"""Test that known methods are registered to the correct object."""
2494
self.assertHandlerEqual(b'Branch.break_lock',
2495
smart_branch.SmartServerBranchBreakLock)
2496
self.assertHandlerEqual(b'Branch.get_config_file',
2497
smart_branch.SmartServerBranchGetConfigFile)
2498
self.assertHandlerEqual(b'Branch.put_config_file',
2499
smart_branch.SmartServerBranchPutConfigFile)
2500
self.assertHandlerEqual(b'Branch.get_parent',
2501
smart_branch.SmartServerBranchGetParent)
2502
self.assertHandlerEqual(b'Branch.get_physical_lock_status',
2503
smart_branch.SmartServerBranchRequestGetPhysicalLockStatus)
2504
self.assertHandlerEqual(b'Branch.get_tags_bytes',
2505
smart_branch.SmartServerBranchGetTagsBytes)
2506
self.assertHandlerEqual(b'Branch.lock_write',
2507
smart_branch.SmartServerBranchRequestLockWrite)
2508
self.assertHandlerEqual(b'Branch.last_revision_info',
2509
smart_branch.SmartServerBranchRequestLastRevisionInfo)
2510
self.assertHandlerEqual(b'Branch.revision_history',
2511
smart_branch.SmartServerRequestRevisionHistory)
2512
self.assertHandlerEqual(b'Branch.revision_id_to_revno',
2513
smart_branch.SmartServerBranchRequestRevisionIdToRevno)
2514
self.assertHandlerEqual(b'Branch.set_config_option',
2515
smart_branch.SmartServerBranchRequestSetConfigOption)
2516
self.assertHandlerEqual(b'Branch.set_last_revision',
2517
smart_branch.SmartServerBranchRequestSetLastRevision)
2518
self.assertHandlerEqual(b'Branch.set_last_revision_info',
2519
smart_branch.SmartServerBranchRequestSetLastRevisionInfo)
2520
self.assertHandlerEqual(b'Branch.set_last_revision_ex',
2521
smart_branch.SmartServerBranchRequestSetLastRevisionEx)
2522
self.assertHandlerEqual(b'Branch.set_parent_location',
2523
smart_branch.SmartServerBranchRequestSetParentLocation)
2524
self.assertHandlerEqual(b'Branch.unlock',
2525
smart_branch.SmartServerBranchRequestUnlock)
2526
self.assertHandlerEqual(b'BzrDir.destroy_branch',
2527
smart_dir.SmartServerBzrDirRequestDestroyBranch)
2528
self.assertHandlerEqual(b'BzrDir.find_repository',
2529
smart_dir.SmartServerRequestFindRepositoryV1)
2530
self.assertHandlerEqual(b'BzrDir.find_repositoryV2',
2531
smart_dir.SmartServerRequestFindRepositoryV2)
2532
self.assertHandlerEqual(b'BzrDirFormat.initialize',
2533
smart_dir.SmartServerRequestInitializeBzrDir)
2534
self.assertHandlerEqual(b'BzrDirFormat.initialize_ex_1.16',
2535
smart_dir.SmartServerRequestBzrDirInitializeEx)
2536
self.assertHandlerEqual(b'BzrDir.checkout_metadir',
2537
smart_dir.SmartServerBzrDirRequestCheckoutMetaDir)
2538
self.assertHandlerEqual(b'BzrDir.cloning_metadir',
2539
smart_dir.SmartServerBzrDirRequestCloningMetaDir)
2540
self.assertHandlerEqual(b'BzrDir.get_branches',
2541
smart_dir.SmartServerBzrDirRequestGetBranches)
2542
self.assertHandlerEqual(b'BzrDir.get_config_file',
2543
smart_dir.SmartServerBzrDirRequestConfigFile)
2544
self.assertHandlerEqual(b'BzrDir.open_branch',
2545
smart_dir.SmartServerRequestOpenBranch)
2546
self.assertHandlerEqual(b'BzrDir.open_branchV2',
2547
smart_dir.SmartServerRequestOpenBranchV2)
2548
self.assertHandlerEqual(b'BzrDir.open_branchV3',
2549
smart_dir.SmartServerRequestOpenBranchV3)
2550
self.assertHandlerEqual(b'PackRepository.autopack',
2551
smart_packrepo.SmartServerPackRepositoryAutopack)
2552
self.assertHandlerEqual(b'Repository.add_signature_text',
2553
smart_repo.SmartServerRepositoryAddSignatureText)
2554
self.assertHandlerEqual(b'Repository.all_revision_ids',
2555
smart_repo.SmartServerRepositoryAllRevisionIds)
2556
self.assertHandlerEqual(b'Repository.break_lock',
2557
smart_repo.SmartServerRepositoryBreakLock)
2558
self.assertHandlerEqual(b'Repository.gather_stats',
2559
smart_repo.SmartServerRepositoryGatherStats)
2560
self.assertHandlerEqual(b'Repository.get_parent_map',
2561
smart_repo.SmartServerRepositoryGetParentMap)
2562
self.assertHandlerEqual(b'Repository.get_physical_lock_status',
2563
smart_repo.SmartServerRepositoryGetPhysicalLockStatus)
2564
self.assertHandlerEqual(b'Repository.get_rev_id_for_revno',
2565
smart_repo.SmartServerRepositoryGetRevIdForRevno)
2566
self.assertHandlerEqual(b'Repository.get_revision_graph',
2567
smart_repo.SmartServerRepositoryGetRevisionGraph)
2568
self.assertHandlerEqual(b'Repository.get_revision_signature_text',
2569
smart_repo.SmartServerRepositoryGetRevisionSignatureText)
2570
self.assertHandlerEqual(b'Repository.get_stream',
2571
smart_repo.SmartServerRepositoryGetStream)
2572
self.assertHandlerEqual(b'Repository.get_stream_1.19',
2573
smart_repo.SmartServerRepositoryGetStream_1_19)
2574
self.assertHandlerEqual(b'Repository.iter_revisions',
2575
smart_repo.SmartServerRepositoryIterRevisions)
2576
self.assertHandlerEqual(b'Repository.has_revision',
2577
smart_repo.SmartServerRequestHasRevision)
2578
self.assertHandlerEqual(b'Repository.insert_stream',
2579
smart_repo.SmartServerRepositoryInsertStream)
2580
self.assertHandlerEqual(b'Repository.insert_stream_locked',
2581
smart_repo.SmartServerRepositoryInsertStreamLocked)
2582
self.assertHandlerEqual(b'Repository.is_shared',
2583
smart_repo.SmartServerRepositoryIsShared)
2584
self.assertHandlerEqual(b'Repository.iter_files_bytes',
2585
smart_repo.SmartServerRepositoryIterFilesBytes)
2586
self.assertHandlerEqual(b'Repository.lock_write',
2587
smart_repo.SmartServerRepositoryLockWrite)
2588
self.assertHandlerEqual(b'Repository.make_working_trees',
2589
smart_repo.SmartServerRepositoryMakeWorkingTrees)
2590
self.assertHandlerEqual(b'Repository.pack',
2591
smart_repo.SmartServerRepositoryPack)
2592
self.assertHandlerEqual(b'Repository.reconcile',
2593
smart_repo.SmartServerRepositoryReconcile)
2594
self.assertHandlerEqual(b'Repository.tarball',
2595
smart_repo.SmartServerRepositoryTarball)
2596
self.assertHandlerEqual(b'Repository.unlock',
2597
smart_repo.SmartServerRepositoryUnlock)
2598
self.assertHandlerEqual(b'Repository.start_write_group',
2599
smart_repo.SmartServerRepositoryStartWriteGroup)
2600
self.assertHandlerEqual(b'Repository.check_write_group',
2601
smart_repo.SmartServerRepositoryCheckWriteGroup)
2602
self.assertHandlerEqual(b'Repository.commit_write_group',
2603
smart_repo.SmartServerRepositoryCommitWriteGroup)
2604
self.assertHandlerEqual(b'Repository.abort_write_group',
2605
smart_repo.SmartServerRepositoryAbortWriteGroup)
2606
self.assertHandlerEqual(b'VersionedFileRepository.get_serializer_format',
2607
smart_repo.SmartServerRepositoryGetSerializerFormat)
2608
self.assertHandlerEqual(b'VersionedFileRepository.get_inventories',
2609
smart_repo.SmartServerRepositoryGetInventories)
2610
self.assertHandlerEqual(b'Transport.is_readonly',
2611
smart_req.SmartServerIsReadonly)
2614
class SmartTCPServerHookTests(tests.TestCaseWithMemoryTransport):
2615
"""Tests for SmartTCPServer hooks."""
2618
super(SmartTCPServerHookTests, self).setUp()
2619
self.server = server.SmartTCPServer(self.get_transport())
2621
def test_run_server_started_hooks(self):
2622
"""Test the server started hooks get fired properly."""
2624
server.SmartTCPServer.hooks.install_named_hook('server_started',
2625
lambda backing_urls, url: started_calls.append((backing_urls, url)),
2627
started_ex_calls = []
2628
server.SmartTCPServer.hooks.install_named_hook('server_started_ex',
2629
lambda backing_urls, url: started_ex_calls.append((backing_urls, url)),
2631
self.server._sockname = ('example.com', 42)
2632
self.server.run_server_started_hooks()
2633
self.assertEqual(started_calls,
2634
[([self.get_transport().base], 'bzr://example.com:42/')])
2635
self.assertEqual(started_ex_calls,
2636
[([self.get_transport().base], self.server)])
2638
def test_run_server_started_hooks_ipv6(self):
2639
"""Test that socknames can contain 4-tuples."""
2640
self.server._sockname = ('::', 42, 0, 0)
2642
server.SmartTCPServer.hooks.install_named_hook('server_started',
2643
lambda backing_urls, url: started_calls.append((backing_urls, url)),
2645
self.server.run_server_started_hooks()
2646
self.assertEqual(started_calls,
2647
[([self.get_transport().base], 'bzr://:::42/')])
2649
def test_run_server_stopped_hooks(self):
2650
"""Test the server stopped hooks."""
2651
self.server._sockname = ('example.com', 42)
2653
server.SmartTCPServer.hooks.install_named_hook('server_stopped',
2654
lambda backing_urls, url: stopped_calls.append((backing_urls, url)),
2656
self.server.run_server_stopped_hooks()
2657
self.assertEqual(stopped_calls,
2658
[([self.get_transport().base], 'bzr://example.com:42/')])
2661
class TestSmartServerRepositoryPack(tests.TestCaseWithMemoryTransport):
2663
def test_pack(self):
2664
backing = self.get_transport()
2665
request = smart_repo.SmartServerRepositoryPack(backing)
2666
tree = self.make_branch_and_memory_tree('.')
2667
repo_token = tree.branch.repository.lock_write().repository_token
2669
self.assertIs(None, request.execute(b'', repo_token, False))
2672
smart_req.SuccessfulSmartServerResponse((b'ok', ), ),
2673
request.do_body(b''))
2676
class TestSmartServerRepositoryGetInventories(tests.TestCaseWithTransport):
2678
def _get_serialized_inventory_delta(self, repository, base_revid, revid):
2679
base_inv = repository.revision_tree(base_revid).root_inventory
2680
inv = repository.revision_tree(revid).root_inventory
2681
inv_delta = inv._make_delta(base_inv)
2682
serializer = inventory_delta.InventoryDeltaSerializer(True, False)
2683
return b"".join(serializer.delta_to_lines(base_revid, revid, inv_delta))
2685
def test_single(self):
2686
backing = self.get_transport()
2687
request = smart_repo.SmartServerRepositoryGetInventories(backing)
2688
t = self.make_branch_and_tree('.', format='2a')
2689
self.addCleanup(t.lock_write().unlock)
2690
self.build_tree_contents([("file", b"somecontents")])
2691
t.add(["file"], [b"thefileid"])
2692
t.commit(rev_id=b'somerev', message="add file")
2693
self.assertIs(None, request.execute(b'', b'unordered'))
2694
response = request.do_body(b"somerev\n")
2695
self.assertTrue(response.is_successful())
2696
self.assertEqual(response.args, (b"ok", ))
2697
stream = [('inventory-deltas', [
2698
versionedfile.FulltextContentFactory(b'somerev', None, None,
2699
self._get_serialized_inventory_delta(
2700
t.branch.repository, b'null:', b'somerev'))])]
2701
fmt = controldir.format_registry.get('2a')().repository_format
2703
b"".join(response.body_stream),
2704
b"".join(smart_repo._stream_to_byte_stream(stream, fmt)))
2706
def test_empty(self):
2707
backing = self.get_transport()
2708
request = smart_repo.SmartServerRepositoryGetInventories(backing)
2709
t = self.make_branch_and_tree('.', format='2a')
2710
self.addCleanup(t.lock_write().unlock)
2711
self.build_tree_contents([("file", b"somecontents")])
2712
t.add(["file"], [b"thefileid"])
2713
t.commit(rev_id=b'somerev', message="add file")
2714
self.assertIs(None, request.execute(b'', b'unordered'))
2715
response = request.do_body(b"")
2716
self.assertTrue(response.is_successful())
2717
self.assertEqual(response.args, (b"ok", ))
2718
self.assertEqual(b"".join(response.body_stream),
2719
b"Bazaar pack format 1 (introduced in 0.18)\nB54\n\nBazaar repository format 2a (needs bzr 1.16 or later)\nE")
2722
class TestSmartServerRepositoryGetStreamForMissingKeys(GetStreamTestBase):
2724
def test_missing(self):
2725
"""The search argument may be a 'ancestry-of' some heads'."""
2726
backing = self.get_transport()
2727
request = smart_repo.SmartServerRepositoryGetStreamForMissingKeys(
2729
repo, r1, r2 = self.make_two_commit_repo()
2730
request.execute(b'', repo._format.network_name())
2731
lines = b'inventories\t' + r1
2732
response = request.do_body(lines)
2733
self.assertEqual((b'ok',), response.args)
2734
stream_bytes = b''.join(response.body_stream)
2735
self.assertStartsWith(stream_bytes, b'Bazaar pack format 1')
2737
def test_unknown_format(self):
2738
"""The format may not be known by the remote server."""
2739
backing = self.get_transport()
2740
request = smart_repo.SmartServerRepositoryGetStreamForMissingKeys(
2742
repo, r1, r2 = self.make_two_commit_repo()
2743
request.execute(b'', b'yada yada yada')
2744
expected = smart_req.FailedSmartServerResponse(
2745
(b'UnknownFormat', b'repository', b'yada yada yada'))
2748
class TestSmartServerRepositoryRevisionArchive(tests.TestCaseWithTransport):
2750
backing = self.get_transport()
2751
request = smart_repo.SmartServerRepositoryRevisionArchive(backing)
2752
t = self.make_branch_and_tree('.')
2753
self.addCleanup(t.lock_write().unlock)
2754
self.build_tree_contents([("file", b"somecontents")])
2755
t.add(["file"], [b"thefileid"])
2756
t.commit(rev_id=b'somerev', message="add file")
2757
response = request.execute(b'', b"somerev", b"tar", b"foo.tar", b"foo")
2758
self.assertTrue(response.is_successful())
2759
self.assertEqual(response.args, (b"ok", ))
2760
b = BytesIO(b"".join(response.body_stream))
2761
with tarfile.open(mode='r', fileobj=b) as tf:
2762
self.assertEqual(['foo/file'], tf.getnames())
2765
class TestSmartServerRepositoryAnnotateFileRevision(tests.TestCaseWithTransport):
2768
backing = self.get_transport()
2769
request = smart_repo.SmartServerRepositoryAnnotateFileRevision(backing)
2770
t = self.make_branch_and_tree('.')
2771
self.addCleanup(t.lock_write().unlock)
2772
self.build_tree_contents([("file", b"somecontents\nmorecontents\n")])
2773
t.add(["file"], [b"thefileid"])
2774
t.commit(rev_id=b'somerev', message="add file")
2775
response = request.execute(b'', b"somerev", b"file")
2776
self.assertTrue(response.is_successful())
2777
self.assertEqual(response.args, (b"ok", ))
2779
[[b'somerev', b'somecontents\n'], [b'somerev', b'morecontents\n']],
2780
bencode.bdecode(response.body))