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(source_bzrdir)
475
# Make a directory with a default stacking policy
476
parent_bzrdir = self.make_bzrdir('parent')
477
stacked_on = self.make_branch('parent/stacked-on', format='pack-0.92')
478
parent_bzrdir.get_config().set_default_stack_on(stacked_on.base)
479
# Clone source into directory
480
target = source_bzrdir.clone(self.get_url('parent/target'))
482
458
def test_sprout_obeys_stacking_policy(self):
483
459
child_branch, new_child_transport = self.prepare_default_stacking()
484
460
new_child = child_branch.bzrdir.sprout(new_child_transport.base)
755
731
def test_sprout_recursive(self):
756
tree = self.make_branch_and_tree('tree1',
757
format='dirstate-with-subtree')
732
tree = self.make_branch_and_tree('tree1', format='dirstate-with-subtree')
758
733
sub_tree = self.make_branch_and_tree('tree1/subtree',
759
734
format='dirstate-with-subtree')
760
sub_tree.set_root_id('subtree-root')
761
735
tree.add_reference(sub_tree)
762
736
self.build_tree(['tree1/subtree/file'])
763
737
sub_tree.add('file')
764
738
tree.commit('Initial commit')
765
tree2 = tree.bzrdir.sprout('tree2').open_workingtree()
767
self.addCleanup(tree2.unlock)
739
tree.bzrdir.sprout('tree2')
768
740
self.failUnlessExists('tree2/subtree/file')
769
self.assertEqual('tree-reference', tree2.kind('subtree-root'))
771
742
def test_cloning_metadir(self):
772
743
"""Ensure that cloning metadir is suitable"""
899
870
def test_needs_conversion_different_working_tree(self):
900
871
# meta1dirs need an conversion if any element is not the default.
901
new_format = bzrdir.format_registry.make_bzrdir('dirstate')
902
tree = self.make_branch_and_tree('tree', format='knit')
903
self.assertTrue(tree.bzrdir.needs_format_conversion(
906
def test_initialize_on_format_uses_smart_transport(self):
907
self.setup_smart_server_with_call_log()
908
new_format = bzrdir.format_registry.make_bzrdir('dirstate')
909
transport = self.get_transport('target')
910
transport.ensure_base()
911
self.reset_smart_call_log()
912
instance = new_format.initialize_on_transport(transport)
913
self.assertIsInstance(instance, remote.RemoteBzrDir)
914
rpc_count = len(self.hpss_calls)
915
# This figure represent the amount of work to perform this use case. It
916
# is entirely ok to reduce this number if a test fails due to rpc_count
917
# being too low. If rpc_count increases, more network roundtrips have
918
# become necessary for this use case. Please do not adjust this number
919
# upwards without agreement from bzr's network support maintainers.
920
self.assertEqual(2, rpc_count)
872
old_format = bzrdir.BzrDirFormat.get_default_format()
874
new_default = bzrdir.format_registry.make_bzrdir('dirstate')
875
bzrdir.BzrDirFormat._set_default_format(new_default)
877
tree = self.make_branch_and_tree('tree', format='knit')
878
self.assertTrue(tree.bzrdir.needs_format_conversion())
880
bzrdir.BzrDirFormat._set_default_format(old_format)
923
883
class TestFormat5(TestCaseWithTransport):
924
884
"""Tests specific to the version 5 bzrdir format."""
926
886
def test_same_lockfiles_between_tree_repo_branch(self):
927
# this checks that only a single lockfiles instance is created
887
# this checks that only a single lockfiles instance is created
928
888
# for format 5 objects
929
889
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
930
890
def check_dir_components_use_same_lock(dir):
937
897
# and if we open it normally.
938
898
dir = bzrdir.BzrDir.open(self.get_url())
939
899
check_dir_components_use_same_lock(dir)
941
901
def test_can_convert(self):
942
902
# format 5 dirs are convertable
943
903
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
944
904
self.assertTrue(dir.can_convert_format())
946
906
def test_needs_conversion(self):
947
# format 5 dirs need a conversion if they are not the default,
949
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
950
# don't need to convert it to itself
951
self.assertFalse(dir.needs_format_conversion(bzrdir.BzrDirFormat5()))
952
# do need to convert it to the current default
953
self.assertTrue(dir.needs_format_conversion(
954
bzrdir.BzrDirFormat.get_default_format()))
907
# format 5 dirs need a conversion if they are not the default.
908
# and they start of not the default.
909
old_format = bzrdir.BzrDirFormat.get_default_format()
910
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirFormat5())
912
dir = bzrdir.BzrDirFormat5().initialize(self.get_url())
913
self.assertFalse(dir.needs_format_conversion())
915
bzrdir.BzrDirFormat._set_default_format(old_format)
916
self.assertTrue(dir.needs_format_conversion())
957
919
class TestFormat6(TestCaseWithTransport):
958
920
"""Tests specific to the version 6 bzrdir format."""
960
922
def test_same_lockfiles_between_tree_repo_branch(self):
961
# this checks that only a single lockfiles instance is created
923
# this checks that only a single lockfiles instance is created
962
924
# for format 6 objects
963
925
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
964
926
def check_dir_components_use_same_lock(dir):
971
933
# and if we open it normally.
972
934
dir = bzrdir.BzrDir.open(self.get_url())
973
935
check_dir_components_use_same_lock(dir)
975
937
def test_can_convert(self):
976
938
# format 6 dirs are convertable
977
939
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
978
940
self.assertTrue(dir.can_convert_format())
980
942
def test_needs_conversion(self):
981
943
# format 6 dirs need an conversion if they are not the default.
982
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
983
self.assertTrue(dir.needs_format_conversion(
984
bzrdir.BzrDirFormat.get_default_format()))
944
old_format = bzrdir.BzrDirFormat.get_default_format()
945
bzrdir.BzrDirFormat._set_default_format(bzrdir.BzrDirMetaFormat1())
947
dir = bzrdir.BzrDirFormat6().initialize(self.get_url())
948
self.assertTrue(dir.needs_format_conversion())
950
bzrdir.BzrDirFormat._set_default_format(old_format)
987
953
class NotBzrDir(bzrlib.bzrdir.BzrDir):
1098
1064
workingtree.WorkingTreeFormat3)
1101
class TestHTTPRedirections(object):
1102
"""Test redirection between two http servers.
1067
class TestHTTPRedirectionLoop(object):
1068
"""Test redirection loop between two http servers.
1104
1070
This MUST be used by daughter classes that also inherit from
1105
1071
TestCaseWithTwoWebservers.
1107
1073
We can't inherit directly from TestCaseWithTwoWebservers or the
1108
1074
test framework will try to create an instance which cannot
1109
run, its implementation being incomplete.
1075
run, its implementation being incomplete.
1078
# Should be defined by daughter classes to ensure redirection
1079
# still use the same transport implementation (not currently
1080
# enforced as it's a bit tricky to get right (see the FIXME
1081
# in BzrDir.open_from_transport for the unique use case so
1112
1085
def create_transport_readonly_server(self):
1113
return http_utils.HTTPServerRedirecting()
1086
return HTTPServerRedirecting()
1115
1088
def create_transport_secondary_server(self):
1116
return http_utils.HTTPServerRedirecting()
1089
return HTTPServerRedirecting()
1118
1091
def setUp(self):
1119
super(TestHTTPRedirections, self).setUp()
1092
# Both servers redirect to each server creating a loop
1093
super(TestHTTPRedirectionLoop, self).setUp()
1120
1094
# The redirections will point to the new server
1121
1095
self.new_server = self.get_readonly_server()
1122
1096
# The requests to the old server will be redirected
1123
1097
self.old_server = self.get_secondary_server()
1124
1098
# Configure the redirections
1125
1099
self.old_server.redirect_to(self.new_server.host, self.new_server.port)
1100
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1102
def _qualified_url(self, host, port):
1103
return 'http+%s://%s:%s' % (self._qualifier, host, port)
1127
1105
def test_loop(self):
1128
# Both servers redirect to each other creating a loop
1129
self.new_server.redirect_to(self.old_server.host, self.old_server.port)
1130
1106
# Starting from either server should loop
1131
old_url = self._qualified_url(self.old_server.host,
1107
old_url = self._qualified_url(self.old_server.host,
1132
1108
self.old_server.port)
1133
1109
oldt = self._transport(old_url)
1134
1110
self.assertRaises(errors.NotBranchError,
1135
1111
bzrdir.BzrDir.open_from_transport, oldt)
1136
new_url = self._qualified_url(self.new_server.host,
1112
new_url = self._qualified_url(self.new_server.host,
1137
1113
self.new_server.port)
1138
1114
newt = self._transport(new_url)
1139
1115
self.assertRaises(errors.NotBranchError,
1140
1116
bzrdir.BzrDir.open_from_transport, newt)
1142
def test_qualifier_preserved(self):
1143
wt = self.make_branch_and_tree('branch')
1144
old_url = self._qualified_url(self.old_server.host,
1145
self.old_server.port)
1146
start = self._transport(old_url).clone('branch')
1147
bdir = bzrdir.BzrDir.open_from_transport(start)
1148
# Redirection should preserve the qualifier, hence the transport class
1150
self.assertIsInstance(bdir.root_transport, type(start))
1153
class TestHTTPRedirections_urllib(TestHTTPRedirections,
1154
http_utils.TestCaseWithTwoWebservers):
1119
class TestHTTPRedirections_urllib(TestHTTPRedirectionLoop,
1120
TestCaseWithTwoWebservers):
1155
1121
"""Tests redirections for urllib implementation"""
1123
_qualifier = 'urllib'
1157
1124
_transport = HttpTransport_urllib
1159
def _qualified_url(self, host, port):
1160
result = 'http+urllib://%s:%s' % (host, port)
1161
self.permit_url(result)
1166
1128
class TestHTTPRedirections_pycurl(TestWithTransport_pycurl,
1167
TestHTTPRedirections,
1168
http_utils.TestCaseWithTwoWebservers):
1129
TestHTTPRedirectionLoop,
1130
TestCaseWithTwoWebservers):
1169
1131
"""Tests redirections for pycurl implementation"""
1171
def _qualified_url(self, host, port):
1172
result = 'http+pycurl://%s:%s' % (host, port)
1173
self.permit_url(result)
1177
class TestHTTPRedirections_nosmart(TestHTTPRedirections,
1178
http_utils.TestCaseWithTwoWebservers):
1179
"""Tests redirections for the nosmart decorator"""
1181
_transport = NoSmartTransportDecorator
1183
def _qualified_url(self, host, port):
1184
result = 'nosmart+http://%s:%s' % (host, port)
1185
self.permit_url(result)
1189
class TestHTTPRedirections_readonly(TestHTTPRedirections,
1190
http_utils.TestCaseWithTwoWebservers):
1191
"""Tests redirections for readonly decoratror"""
1193
_transport = ReadonlyTransportDecorator
1195
def _qualified_url(self, host, port):
1196
result = 'readonly+http://%s:%s' % (host, port)
1197
self.permit_url(result)
1133
_qualifier = 'pycurl'
1201
1136
class TestDotBzrHidden(TestCaseWithTransport):
1315
1245
parent = grandparent_tree.bzrdir.sprout('parent').open_branch()
1316
1246
branch_tree = parent.bzrdir.sprout('branch').open_branch()
1317
1247
self.assertContainsRe(branch_tree.get_parent(), '/parent/$')
1320
class TestBzrDirHooks(TestCaseWithMemoryTransport):
1322
def test_pre_open_called(self):
1324
bzrdir.BzrDir.hooks.install_named_hook('pre_open', calls.append, None)
1325
transport = self.get_transport('foo')
1326
url = transport.base
1327
self.assertRaises(errors.NotBranchError, bzrdir.BzrDir.open, url)
1328
self.assertEqual([transport.base], [t.base for t in calls])
1330
def test_pre_open_actual_exceptions_raised(self):
1332
def fail_once(transport):
1335
raise errors.BzrError("fail")
1336
bzrdir.BzrDir.hooks.install_named_hook('pre_open', fail_once, None)
1337
transport = self.get_transport('foo')
1338
url = transport.base
1339
err = self.assertRaises(errors.BzrError, bzrdir.BzrDir.open, url)
1340
self.assertEqual('fail', err._preformatted_string)