225
222
self.assertEqual(expected, request.execute('', 'False'))
225
class TestSmartServerBzrDirRequestHasWorkingTree(
226
tests.TestCaseWithTransport):
227
"""Tests for BzrDir.has_workingtree."""
229
def test_has_workingtree_yes(self):
230
"""A working tree is present."""
231
backing = self.get_transport()
232
dir = self.make_branch_and_tree('.').bzrdir
233
request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
234
request = request_class(backing)
235
expected = smart_req.SuccessfulSmartServerResponse(('yes',))
236
self.assertEqual(expected, request.execute(''))
238
def test_has_workingtree_no(self):
239
"""A working tree is missing."""
240
backing = self.get_transport()
241
dir = self.make_bzrdir('.')
242
request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
243
request = request_class(backing)
244
expected = smart_req.SuccessfulSmartServerResponse(('no',))
245
self.assertEqual(expected, request.execute(''))
248
class TestSmartServerBzrDirRequestDestroyRepository(
249
tests.TestCaseWithMemoryTransport):
250
"""Tests for BzrDir.destroy_repository."""
252
def test_destroy_repository_default(self):
253
"""The repository can be removed."""
254
backing = self.get_transport()
255
dir = self.make_repository('.').bzrdir
256
request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
257
request = request_class(backing)
258
expected = smart_req.SuccessfulSmartServerResponse(('ok',))
259
self.assertEqual(expected, request.execute(''))
261
def test_destroy_repository_missing(self):
262
"""An error is raised if the repository didn't exist."""
263
backing = self.get_transport()
264
dir = self.make_bzrdir('.')
265
request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
266
request = request_class(backing)
267
expected = smart_req.FailedSmartServerResponse(
268
('norepository',), None)
269
self.assertEqual(expected, request.execute(''))
228
272
class TestSmartServerRequestCreateRepository(tests.TestCaseWithMemoryTransport):
229
273
"""Tests for BzrDir.create_repository."""
767
811
class TestLockedBranch(tests.TestCaseWithMemoryTransport):
769
813
def get_lock_tokens(self, branch):
770
branch_token = branch.lock_write()
771
repo_token = branch.repository.lock_write()
814
branch_token = branch.lock_write().branch_token
815
repo_token = branch.repository.lock_write().repository_token
772
816
branch.repository.unlock()
773
817
return branch_token, repo_token
820
class TestSmartServerBranchRequestPutConfigFile(TestLockedBranch):
822
def test_with_content(self):
823
backing = self.get_transport()
824
request = smart_branch.SmartServerBranchPutConfigFile(backing)
825
branch = self.make_branch('.')
826
branch_token, repo_token = self.get_lock_tokens(branch)
827
self.assertIs(None, request.execute('', branch_token, repo_token))
829
smart_req.SmartServerResponse(('ok', )),
830
request.do_body('foo bar baz'))
832
branch.control_transport.get_bytes('branch.conf'),
776
837
class TestSmartServerBranchRequestSetConfigOption(TestLockedBranch):
778
839
def test_value_name(self):
866
class TestSmartServerBranchRequestSetConfigOptionDict(TestLockedBranch):
869
TestLockedBranch.setUp(self)
870
# A dict with non-ascii keys and values to exercise unicode
872
self.encoded_value_dict = (
873
'd5:ascii1:a11:unicode \xe2\x8c\x9a3:\xe2\x80\xbde')
875
'ascii': 'a', u'unicode \N{WATCH}': u'\N{INTERROBANG}'}
877
def test_value_name(self):
878
branch = self.make_branch('.')
879
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
880
branch.bzrdir.root_transport)
881
branch_token, repo_token = self.get_lock_tokens(branch)
882
config = branch._get_config()
883
result = request.execute('', branch_token, repo_token,
884
self.encoded_value_dict, 'foo', '')
885
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
886
self.assertEqual(self.value_dict, config.get_option('foo'))
890
def test_value_name_section(self):
891
branch = self.make_branch('.')
892
request = smart_branch.SmartServerBranchRequestSetConfigOptionDict(
893
branch.bzrdir.root_transport)
894
branch_token, repo_token = self.get_lock_tokens(branch)
895
config = branch._get_config()
896
result = request.execute('', branch_token, repo_token,
897
self.encoded_value_dict, 'foo', 'gam')
898
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), result)
899
self.assertEqual(self.value_dict, config.get_option('foo', 'gam'))
805
904
class TestSmartServerBranchRequestSetTagsBytes(TestLockedBranch):
806
905
# Only called when the branch format and tags match [yay factory
807
906
# methods] so only need to test straight forward cases.
1082
1181
branch.unlock()
1083
1182
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1084
1183
self.get_transport())
1085
branch_token = branch.lock_write()
1086
repo_token = branch.repository.lock_write()
1184
branch_token, repo_token = self.get_lock_tokens(branch)
1088
1186
response = request.execute('base', branch_token, repo_token, '')
1090
branch.repository.unlock()
1091
1188
branch.unlock()
1092
1189
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1093
1190
self.assertEqual(None, branch.get_parent())
1096
1193
branch = self.make_branch('base', format="1.9")
1097
1194
request = smart_branch.SmartServerBranchRequestSetParentLocation(
1098
1195
self.get_transport())
1099
branch_token = branch.lock_write()
1100
repo_token = branch.repository.lock_write()
1196
branch_token, repo_token = self.get_lock_tokens(branch)
1102
1198
response = request.execute('base', branch_token, repo_token,
1105
branch.repository.unlock()
1106
1201
branch.unlock()
1107
1202
self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1108
1203
self.assertEqual('http://bar/', branch.get_parent())
1180
1275
backing = self.get_transport()
1181
1276
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1182
1277
branch = self.make_branch('.', format='knit')
1183
branch_token = branch.lock_write()
1184
repo_token = branch.repository.lock_write()
1185
branch.repository.unlock()
1278
branch_token, repo_token = self.get_lock_tokens(branch)
1186
1279
branch.leave_lock_in_place()
1187
1280
branch.repository.leave_lock_in_place()
1188
1281
branch.unlock()
1203
1296
backing = self.get_transport()
1204
1297
request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1205
1298
branch = self.make_branch('.', format='knit')
1206
branch_token = branch.lock_write()
1207
repo_token = branch.repository.lock_write()
1208
branch.repository.unlock()
1299
branch_token, repo_token = self.get_lock_tokens(branch)
1209
1300
branch.leave_lock_in_place()
1210
1301
branch.repository.leave_lock_in_place()
1211
1302
branch.unlock()
1259
1350
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1260
1351
branch = self.make_branch('.', format='knit')
1261
1352
# Lock the branch
1262
branch_token = branch.lock_write()
1263
repo_token = branch.repository.lock_write()
1264
branch.repository.unlock()
1353
branch_token, repo_token = self.get_lock_tokens(branch)
1265
1354
# Unlock the branch (and repo) object, leaving the physical locks
1267
1356
branch.leave_lock_in_place()
1291
1380
request = smart_branch.SmartServerBranchRequestUnlock(backing)
1292
1381
branch = self.make_branch('.', format='knit')
1293
1382
# Lock the repository.
1294
repo_token = branch.repository.lock_write()
1383
repo_token = branch.repository.lock_write().repository_token
1295
1384
branch.repository.leave_lock_in_place()
1296
1385
branch.repository.unlock()
1297
1386
# Issue branch lock_write request on the unlocked branch (with locked
1299
response = request.execute(
1300
'', 'branch token', repo_token)
1388
response = request.execute('', 'branch token', repo_token)
1301
1389
self.assertEqual(
1302
1390
smart_req.SmartServerResponse(('TokenMismatch',)), response)
1485
1576
stream_bytes = ''.join(response.body_stream)
1486
1577
self.assertStartsWith(stream_bytes, 'Bazaar pack format 1')
1579
def test_search_everything(self):
1580
"""A search of 'everything' returns a stream."""
1581
backing = self.get_transport()
1582
request = smart_repo.SmartServerRepositoryGetStream_1_19(backing)
1583
repo, r1, r2 = self.make_two_commit_repo()
1584
serialised_fetch_spec = 'everything'
1585
request.execute('', repo._format.network_name())
1586
response = request.do_body(serialised_fetch_spec)
1587
self.assertEqual(('ok',), response.args)
1588
stream_bytes = ''.join(response.body_stream)
1589
self.assertStartsWith(stream_bytes, 'Bazaar pack format 1')
1489
1592
class TestSmartServerRequestHasRevision(tests.TestCaseWithMemoryTransport):
1511
1614
request.execute('', rev_id_utf8))
1617
class TestSmartServerRequestHasSignatureForRevisionId(
1618
tests.TestCaseWithMemoryTransport):
1620
def test_missing_revision(self):
1621
"""For a missing revision, NoSuchRevision is returned."""
1622
backing = self.get_transport()
1623
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1625
self.make_repository('.')
1627
smart_req.FailedSmartServerResponse(
1628
('nosuchrevision', 'revid'), None),
1629
request.execute('', 'revid'))
1631
def test_missing_signature(self):
1632
"""For a missing signature, ('no', ) is returned."""
1633
backing = self.get_transport()
1634
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1636
tree = self.make_branch_and_memory_tree('.')
1639
r1 = tree.commit('a commit', rev_id='A')
1641
self.assertTrue(tree.branch.repository.has_revision('A'))
1642
self.assertEqual(smart_req.SmartServerResponse(('no', )),
1643
request.execute('', 'A'))
1645
def test_present_signature(self):
1646
"""For a present signature, ('yes', ) is returned."""
1647
backing = self.get_transport()
1648
request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
1650
strategy = gpg.LoopbackGPGStrategy(None)
1651
tree = self.make_branch_and_memory_tree('.')
1654
r1 = tree.commit('a commit', rev_id='A')
1655
tree.branch.repository.start_write_group()
1656
tree.branch.repository.sign_revision('A', strategy)
1657
tree.branch.repository.commit_write_group()
1659
self.assertTrue(tree.branch.repository.has_revision('A'))
1660
self.assertEqual(smart_req.SmartServerResponse(('yes', )),
1661
request.execute('', 'A'))
1514
1664
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
1516
1666
def test_empty_revid(self):
1589
1739
request.execute('', ))
1742
class TestSmartServerRepositoryMakeWorkingTrees(
1743
tests.TestCaseWithMemoryTransport):
1745
def test_make_working_trees(self):
1746
"""For a repository with working trees, ('yes', ) is returned."""
1747
backing = self.get_transport()
1748
request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
1749
r = self.make_repository('.')
1750
r.set_make_working_trees(True)
1751
self.assertEqual(smart_req.SmartServerResponse(('yes', )),
1752
request.execute('', ))
1754
def test_is_not_shared(self):
1755
"""For a repository with working trees, ('no', ) is returned."""
1756
backing = self.get_transport()
1757
request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
1758
r = self.make_repository('.')
1759
r.set_make_working_trees(False)
1760
self.assertEqual(smart_req.SmartServerResponse(('no', )),
1761
request.execute('', ))
1592
1764
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
1594
1766
def test_lock_write_on_unlocked_repo(self):
1658
1830
request = smart_repo.SmartServerRepositoryInsertStreamLocked(
1660
1832
repository = self.make_repository('.', format='knit')
1661
lock_token = repository.lock_write()
1833
lock_token = repository.lock_write().repository_token
1662
1834
response = request.execute('', '', lock_token)
1663
1835
self.assertEqual(None, response)
1664
1836
response = request.do_chunk(self.make_empty_byte_stream(repository))
1832
2004
"""Test that known methods are registered to the correct object."""
1833
2005
self.assertHandlerEqual('Branch.get_config_file',
1834
2006
smart_branch.SmartServerBranchGetConfigFile)
2007
self.assertHandlerEqual('Branch.put_config_file',
2008
smart_branch.SmartServerBranchPutConfigFile)
1835
2009
self.assertHandlerEqual('Branch.get_parent',
1836
2010
smart_branch.SmartServerBranchGetParent)
1837
2011
self.assertHandlerEqual('Branch.get_tags_bytes',
1884
2058
smart_repo.SmartServerRepositoryGetRevisionGraph)
1885
2059
self.assertHandlerEqual('Repository.get_stream',
1886
2060
smart_repo.SmartServerRepositoryGetStream)
2061
self.assertHandlerEqual('Repository.get_stream_1.19',
2062
smart_repo.SmartServerRepositoryGetStream_1_19)
1887
2063
self.assertHandlerEqual('Repository.has_revision',
1888
2064
smart_repo.SmartServerRequestHasRevision)
1889
2065
self.assertHandlerEqual('Repository.insert_stream',
1894
2070
smart_repo.SmartServerRepositoryIsShared)
1895
2071
self.assertHandlerEqual('Repository.lock_write',
1896
2072
smart_repo.SmartServerRepositoryLockWrite)
2073
self.assertHandlerEqual('Repository.make_working_trees',
2074
smart_repo.SmartServerRepositoryMakeWorkingTrees)
1897
2075
self.assertHandlerEqual('Repository.tarball',
1898
2076
smart_repo.SmartServerRepositoryTarball)
1899
2077
self.assertHandlerEqual('Repository.unlock',
1900
2078
smart_repo.SmartServerRepositoryUnlock)
1901
2079
self.assertHandlerEqual('Transport.is_readonly',
1902
2080
smart_req.SmartServerIsReadonly)
2083
class SmartTCPServerHookTests(tests.TestCaseWithMemoryTransport):
2084
"""Tests for SmartTCPServer hooks."""
2087
super(SmartTCPServerHookTests, self).setUp()
2088
self.server = server.SmartTCPServer(self.get_transport())
2090
def test_run_server_started_hooks(self):
2091
"""Test the server started hooks get fired properly."""
2093
server.SmartTCPServer.hooks.install_named_hook('server_started',
2094
lambda backing_urls, url: started_calls.append((backing_urls, url)),
2096
started_ex_calls = []
2097
server.SmartTCPServer.hooks.install_named_hook('server_started_ex',
2098
lambda backing_urls, url: started_ex_calls.append((backing_urls, url)),
2100
self.server._sockname = ('example.com', 42)
2101
self.server.run_server_started_hooks()
2102
self.assertEquals(started_calls,
2103
[([self.get_transport().base], 'bzr://example.com:42/')])
2104
self.assertEquals(started_ex_calls,
2105
[([self.get_transport().base], self.server)])
2107
def test_run_server_started_hooks_ipv6(self):
2108
"""Test that socknames can contain 4-tuples."""
2109
self.server._sockname = ('::', 42, 0, 0)
2111
server.SmartTCPServer.hooks.install_named_hook('server_started',
2112
lambda backing_urls, url: started_calls.append((backing_urls, url)),
2114
self.server.run_server_started_hooks()
2115
self.assertEquals(started_calls,
2116
[([self.get_transport().base], 'bzr://:::42/')])
2118
def test_run_server_stopped_hooks(self):
2119
"""Test the server stopped hooks."""
2120
self.server._sockname = ('example.com', 42)
2122
server.SmartTCPServer.hooks.install_named_hook('server_stopped',
2123
lambda backing_urls, url: stopped_calls.append((backing_urls, url)),
2125
self.server.run_server_stopped_hooks()
2126
self.assertEquals(stopped_calls,
2127
[([self.get_transport().base], 'bzr://example.com:42/')])