45
46
TestCaseWithMemoryTransport,
46
47
TestCaseWithTransport,
49
from bzrlib.tests import(
51
from bzrlib.tests.http_server import HttpServer
52
from bzrlib.tests.http_utils import (
53
TestCaseWithTwoWebservers,
54
HTTPServerRedirecting,
53
56
from bzrlib.tests.test_http import TestWithTransport_pycurl
54
from bzrlib.transport import (
57
from bzrlib.transport import get_transport
58
58
from bzrlib.transport.http._urllib import HttpTransport_urllib
59
from bzrlib.transport.nosmart import NoSmartTransportDecorator
60
from bzrlib.transport.readonly import ReadonlyTransportDecorator
61
from bzrlib.repofmt import knitrepo, weaverepo, pack_repo
59
from bzrlib.transport.memory import MemoryServer
60
from bzrlib.repofmt import knitrepo, weaverepo
64
63
class TestDefaultFormat(TestCase):
130
129
my_format_registry = self.make_format_registry()
131
130
self.assertEqual('Format registered lazily',
132
131
my_format_registry.get_help('lazy'))
133
self.assertEqual('Format using knits',
132
self.assertEqual('Format using knits',
134
133
my_format_registry.get_help('knit'))
135
self.assertEqual('Format using knits',
134
self.assertEqual('Format using knits',
136
135
my_format_registry.get_help('default'))
137
136
self.assertEqual('Pre-0.8 format. Slower and does not support'
138
' checkouts or shared repositories',
137
' checkouts or shared repositories',
139
138
my_format_registry.get_help('weave'))
141
140
def test_help_topic(self):
142
141
topics = help_topics.HelpTopicRegistry()
143
registry = self.make_format_registry()
144
topics.register('current-formats', registry.help_topic,
146
topics.register('other-formats', registry.help_topic,
148
new = topics.get_detail('current-formats')
149
rest = topics.get_detail('other-formats')
142
topics.register('formats', self.make_format_registry().help_topic,
144
topic = topics.get_detail('formats')
145
new, rest = topic.split('Experimental formats')
150
146
experimental, deprecated = rest.split('Deprecated formats')
151
self.assertContainsRe(new, 'formats-help')
152
self.assertContainsRe(new,
147
self.assertContainsRe(new, 'These formats can be used')
148
self.assertContainsRe(new,
153
149
':knit:\n \(native\) \(default\) Format using knits\n')
154
self.assertContainsRe(experimental,
150
self.assertContainsRe(experimental,
155
151
':branch6:\n \(native\) Experimental successor to knit')
156
self.assertContainsRe(deprecated,
152
self.assertContainsRe(deprecated,
157
153
':lazy:\n \(native\) Format registered lazily\n')
158
154
self.assertNotContainsRe(new, 'hidden')
467
455
self.assertEqual(child_branch.base,
468
456
new_child.open_branch().get_stacked_on_url())
470
def test_default_stacking_with_stackable_branch_unstackable_repo(self):
471
# Make stackable source branch with an unstackable repo format.
472
source_bzrdir = self.make_bzrdir('source')
473
pack_repo.RepositoryFormatKnitPack1().initialize(source_bzrdir)
474
source_branch = bzrlib.branch.BzrBranchFormat7().initialize(
476
# Make a directory with a default stacking policy
477
parent_bzrdir = self.make_bzrdir('parent')
478
stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
479
parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
480
# Clone source into directory
481
target = source_bzrdir.clone(self.get_url('parent/target'))
483
458
def test_sprout_obeys_stacking_policy(self):
484
459
child_branch, new_child_transport = self.prepare_default_stacking()
485
460
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
511
486
self.assertTrue(repo._format.supports_external_lookups)
512
487
self.assertFalse(repo.supports_rich_root())
514
def test_clone_on_transport_upgrades_format_if_stacked_on_specified(self):
515
child_branch, new_child_transport = self.prepare_default_stacking(
516
child_format='pack-0.92')
517
new_child = child_branch.bzrdir.clone_on_transport(new_child_transport,
518
stacked_on=child_branch.bzrdir.root_transport.base)
519
self.assertEqual(child_branch.bzrdir.root_transport.base,
520
new_child.open_branch().get_stacked_on_url())
521
repo = new_child.open_repository()
522
self.assertTrue(repo._format.supports_external_lookups)
523
self.assertFalse(repo.supports_rich_root())
525
489
def test_sprout_upgrades_to_rich_root_format_if_needed(self):
526
490
child_branch, new_child_transport = self.prepare_default_stacking(
527
491
child_format='rich-root-pack')
532
496
self.assertTrue(repo.supports_rich_root())
534
498
def test_add_fallback_repo_handles_absolute_urls(self):
535
stack_on = self.make_branch('stack_on', format='1.6')
536
repo = self.make_repository('repo', format='1.6')
499
stack_on = self.make_branch('stack_on', format='development1')
500
repo = self.make_repository('repo', format='development1')
537
501
policy = bzrdir.UseExistingRepository(repo, stack_on.base)
538
502
policy._add_fallback(repo)
540
504
def test_add_fallback_repo_handles_relative_urls(self):
541
stack_on = self.make_branch('stack_on', format='1.6')
542
repo = self.make_repository('repo', format='1.6')
505
stack_on = self.make_branch('stack_on', format='development1')
506
repo = self.make_repository('repo', format='development1')
543
507
policy = bzrdir.UseExistingRepository(repo, '.', stack_on.base)
544
508
policy._add_fallback(repo)
546
510
def test_configure_relative_branch_stacking_url(self):
547
stack_on = self.make_branch('stack_on', format='1.6')
548
stacked = self.make_branch('stack_on/stacked', format='1.6')
511
stack_on = self.make_branch('stack_on', format='development1')
512
stacked = self.make_branch('stack_on/stacked', format='development1')
549
513
policy = bzrdir.UseExistingRepository(stacked.repository,
550
514
'.', stack_on.base)
551
515
policy.configure_branch(stacked)
552
516
self.assertEqual('..', stacked.get_stacked_on_url())
554
518
def test_relative_branch_stacking_to_absolute(self):
555
stack_on = self.make_branch('stack_on', format='1.6')
556
stacked = self.make_branch('stack_on/stacked', format='1.6')
519
stack_on = self.make_branch('stack_on', format='development1')
520
stacked = self.make_branch('stack_on/stacked', format='development1')
557
521
policy = bzrdir.UseExistingRepository(stacked.repository,
558
522
'.', self.get_readonly_url('stack_on'))
559
523
policy.configure_branch(stacked)
756
720
def test_sprout_recursive(self):
757
tree = self.make_branch_and_tree('tree1',
758
format='dirstate-with-subtree')
721
tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
759
722
sub_tree = self.make_branch_and_tree('tree1/subtree',
760
723
format='dirstate-with-subtree')
761
sub_tree.set_root_id('subtree-root')
762
724
tree.add_reference(sub_tree)
763
725
self.build_tree(['tree1/subtree/file'])
764
726
sub_tree.add('file')
765
727
tree.commit('Initial commit')
766
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
768
self.addCleanup(tree2.unlock)
728
tree.bzrdir.sprout('tree2')
769
729
self.failUnlessExists('tree2/subtree/file')
770
self.assertEqual('tree-reference', tree2.kind('subtree-root'))
772
731
def test_cloning_metadir(self):
773
732
"""Ensure that cloning metadir is suitable"""
900
859
def test_needs_conversion_different_working_tree(self):
901
860
# meta1dirs need an conversion if any element is not the default.
902
new_format = bzrdir.format_registry.make_bzrdir('dirstate')
903
tree = self.make_branch_and_tree('tree', format='knit')
904
self.assertTrue(tree.bzrdir.needs_format_conversion(
907
def test_initialize_on_format_uses_smart_transport(self):
908
self.setup_smart_server_with_call_log()
909
new_format = bzrdir.format_registry.make_bzrdir('dirstate')
910
transport = self.get_transport('target')
911
transport.ensure_base()
912
self.reset_smart_call_log()
913
instance = new_format.initialize_on_transport(transport)
914
self.assertIsInstance(instance, remote.RemoteBzrDir)
915
rpc_count = len(self.hpss_calls)
916
# This figure represent the amount of work to perform this use case. It
917
# is entirely ok to reduce this number if a test fails due to rpc_count
918
# being too low. If rpc_count increases, more network roundtrips have
919
# become necessary for this use case. Please do not adjust this number
920
# upwards without agreement from bzr's network support maintainers.
921
self.assertEqual(2, rpc_count)
861
old_format = bzrdir.BzrDirFormat.get_default_format()
863
new_default = bzrdir.format_registry.make_bzrdir('dirstate')
864
bzrdir.BzrDirFormat._set_default_format(new_default)
866
tree = self.make_branch_and_tree('tree', format='knit')
867
self.assertTrue(tree.bzrdir.needs_format_conversion())
869
bzrdir.BzrDirFormat._set_default_format(old_format)
924
872
class TestFormat5(TestCaseWithTransport):
925
873
"""Tests specific to the version 5 bzrdir format."""
927
875
def test_same_lockfiles_between_tree_repo_branch(self):
928
# this checks that only a single lockfiles instance is created
876
# this checks that only a single lockfiles instance is created
929
877
# for format 5 objects
930
878
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
931
879
def check_dir_components_use_same_lock(dir):
938
886
# and if we open it normally.
939
887
dir = bzrdir.BzrDir.open(self.get_url())
940
888
check_dir_components_use_same_lock(dir)
942
890
def test_can_convert(self):
943
891
# format 5 dirs are convertable
944
892
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
945
893
self.assertTrue(dir.can_convert_format())
947
895
def test_needs_conversion(self):
948
# format 5 dirs need a conversion if they are not the default,
950
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
951
# don't need to convert it to itself
952
self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
953
# do need to convert it to the current default
954
self.assertTrue(dir.needs_format_conversion(
955
bzrdir.BzrDirFormat.get_default_format()))
896
# format 5 dirs need a conversion if they are not the default.
897
# and they start of not the default.
898
old_format = bzrdir.BzrDirFormat.get_default_format()
899
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
901
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
902
self.assertFalse(dir.needs_format_conversion())
904
bzrdir.BzrDirFormat._set_default_format(old_format)
905
self.assertTrue(dir.needs_format_conversion())
958
908
class TestFormat6(TestCaseWithTransport):
959
909
"""Tests specific to the version 6 bzrdir format."""
961
911
def test_same_lockfiles_between_tree_repo_branch(self):
962
# this checks that only a single lockfiles instance is created
912
# this checks that only a single lockfiles instance is created
963
913
# for format 6 objects
964
914
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
965
915
def check_dir_components_use_same_lock(dir):
972
922
# and if we open it normally.
973
923
dir = bzrdir.BzrDir.open(self.get_url())
974
924
check_dir_components_use_same_lock(dir)
976
926
def test_can_convert(self):
977
927
# format 6 dirs are convertable
978
928
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
979
929
self.assertTrue(dir.can_convert_format())
981
931
def test_needs_conversion(self):
982
932
# format 6 dirs need an conversion if they are not the default.
983
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
984
self.assertTrue(dir.needs_format_conversion(
985
bzrdir.BzrDirFormat.get_default_format()))
933
old_format = bzrdir.BzrDirFormat.get_default_format()
934
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
936
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
937
self.assertTrue(dir.needs_format_conversion())
939
bzrdir.BzrDirFormat._set_default_format(old_format)
988
942
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1099
1053
workingtree.WorkingTreeFormat3)
1102
class TestHTTPRedirections(object):
1103
"""Test redirection between two http servers.
1056
class TestHTTPRedirectionLoop(object):
1057
"""Test redirection loop between two http servers.
1105
1059
This MUST be used by daughter classes that also inherit from
1106
1060
TestCaseWithTwoWebservers.
1108
1062
We can't inherit directly from TestCaseWithTwoWebservers or the
1109
1063
test framework will try to create an instance which cannot
1110
run, its implementation being incomplete.
1064
run, its implementation being incomplete.
1067
# Should be defined by daughter classes to ensure redirection
1068
# still use the same transport implementation (not currently
1069
# enforced as it's a bit tricky to get right (see the FIXME
1070
# in BzrDir.open_from_transport for the unique use case so
1113
1074
def create_transport_readonly_server(self):
1114
return http_utils.HTTPServerRedirecting()
1075
return HTTPServerRedirecting()
1116
1077
def create_transport_secondary_server(self):
1117
return http_utils.HTTPServerRedirecting()
1078
return HTTPServerRedirecting()
1119
1080
def setUp(self):
1120
super(TestHTTPRedirections, self).setUp()
1081
# Both servers redirect to each server creating a loop
1082
super(TestHTTPRedirectionLoop, self).setUp()
1121
1083
# The redirections will point to the new server
1122
1084
self.new_server = self.get_readonly_server()
1123
1085
# The requests to the old server will be redirected
1124
1086
self.old_server = self.get_secondary_server()
1125
1087
# Configure the redirections
1126
1088
self.old_server.redirect_to(self.new_server.host, self.new_server.port)
1089
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1091
def _qualified_url(self, host, port):
1092
return 'http+%s://%s:%s' % (self._qualifier, host, port)
1128
1094
def test_loop(self):
1129
# Both servers redirect to each other creating a loop
1130
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1131
1095
# Starting from either server should loop
1132
old_url = self._qualified_url(self.old_server.host,
1096
old_url = self._qualified_url(self.old_server.host,
1133
1097
self.old_server.port)
1134
1098
oldt = self._transport(old_url)
1135
1099
self.assertRaises(errors.NotBranchError,
1136
1100
bzrdir.BzrDir.open_from_transport, oldt)
1137
new_url = self._qualified_url(self.new_server.host,
1101
new_url = self._qualified_url(self.new_server.host,
1138
1102
self.new_server.port)
1139
1103
newt = self._transport(new_url)
1140
1104
self.assertRaises(errors.NotBranchError,
1141
1105
bzrdir.BzrDir.open_from_transport, newt)
1143
def test_qualifier_preserved(self):
1144
wt = self.make_branch_and_tree('branch')
1145
old_url = self._qualified_url(self.old_server.host,
1146
self.old_server.port)
1147
start = self._transport(old_url).clone('branch')
1148
bdir = bzrdir.BzrDir.open_from_transport(start)
1149
# Redirection should preserve the qualifier, hence the transport class
1151
self.assertIsInstance(bdir.root_transport, type(start))
1154
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1155
http_utils.TestCaseWithTwoWebservers):
1108
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
1109
TestCaseWithTwoWebservers):
1156
1110
"""Tests redirections for urllib implementation"""
1112
_qualifier = 'urllib'
1158
1113
_transport = HttpTransport_urllib
1160
def _qualified_url(self, host, port):
1161
result = 'http+urllib://%s:%s' % (host, port)
1162
self.permit_url(result)
1167
1117
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1168
TestHTTPRedirections,
1169
http_utils.TestCaseWithTwoWebservers):
1118
TestHTTPRedirectionLoop,
1119
TestCaseWithTwoWebservers):
1170
1120
"""Tests redirections for pycurl implementation"""
1172
def _qualified_url(self, host, port):
1173
result = 'http+pycurl://%s:%s' % (host, port)
1174
self.permit_url(result)
1178
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1179
http_utils.TestCaseWithTwoWebservers):
1180
"""Tests redirections for the nosmart decorator"""
1182
_transport = NoSmartTransportDecorator
1184
def _qualified_url(self, host, port):
1185
result = 'nosmart+http://%s:%s' % (host, port)
1186
self.permit_url(result)
1190
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1191
http_utils.TestCaseWithTwoWebservers):
1192
"""Tests redirections for readonly decoratror"""
1194
_transport = ReadonlyTransportDecorator
1196
def _qualified_url(self, host, port):
1197
result = 'readonly+http://%s:%s' % (host, port)
1198
self.permit_url(result)
1122
_qualifier = 'pycurl'
1202
1125
class TestDotBzrHidden(TestCaseWithTransport):
1316
1234
parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1317
1235
branch_tree = parent.bzrdir.sprout('branch').open_branch()
1318
1236
self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1321
class TestBzrDirHooks(TestCaseWithMemoryTransport):
1323
def test_pre_open_called(self):
1325
bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
1326
transport = self.get_transport('foo')
1327
url = transport.base
1328
self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
1329
self.assertEqual([transport.base], [t.base for t in calls])
1331
def test_pre_open_actual_exceptions_raised(self):
1333
def fail_once(transport):
1336
raise errors.BzrError("fail")
1337
bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
1338
transport = self.get_transport('foo')
1339
url = transport.base
1340
err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1341
self.assertEqual('fail', err._preformatted_string)
1343
def test_post_repo_init(self):
1344
from bzrlib.bzrdir import RepoInitHookParams
1346
bzrdir.BzrDir.hooks.install_named_hook('post_repo_init',
1348
self.make_repository('foo')
1349
self.assertLength(1, calls)
1351
self.assertIsInstance(params, RepoInitHookParams)
1352
self.assertTrue(hasattr(params, 'bzrdir'))
1353
self.assertTrue(hasattr(params, 'repository'))