~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart.py

  • Committer: Martin Packman
  • Date: 2012-01-05 09:50:04 UTC
  • mfrom: (6424 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6426.
  • Revision ID: martin.packman@canonical.com-20120105095004-mia9xb7y0efmto0v
Merge bzr.dev to resolve conflicts in bzrlib.builtins

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
"""
26
26
 
27
27
import bz2
 
28
import zlib
28
29
 
29
30
from bzrlib import (
30
31
    branch as _mod_branch,
31
32
    bzrdir,
32
33
    errors,
 
34
    gpg,
 
35
    inventory_delta,
33
36
    tests,
34
37
    transport,
35
38
    urlutils,
44
47
    server,
45
48
    vfs,
46
49
    )
 
50
from bzrlib.testament import Testament
47
51
from bzrlib.tests import test_server
48
52
from bzrlib.transport import (
49
53
    chroot,
221
225
        self.assertEqual(expected, request.execute('', 'False'))
222
226
 
223
227
 
 
228
class TestSmartServerBzrDirRequestCloningMetaDir(
 
229
    tests.TestCaseWithMemoryTransport):
 
230
    """Tests for BzrDir.checkout_metadir."""
 
231
 
 
232
    def test_checkout_metadir(self):
 
233
        backing = self.get_transport()
 
234
        request = smart_dir.SmartServerBzrDirRequestCheckoutMetaDir(
 
235
            backing)
 
236
        branch = self.make_branch('.', format='2a')
 
237
        response = request.execute('')
 
238
        self.assertEqual(
 
239
            smart_req.SmartServerResponse(
 
240
                ('Bazaar-NG meta directory, format 1\n',
 
241
                 'Bazaar repository format 2a (needs bzr 1.16 or later)\n',
 
242
                 'Bazaar Branch Format 7 (needs bzr 1.6)\n')),
 
243
            response)
 
244
 
 
245
 
 
246
class TestSmartServerBzrDirRequestDestroyBranch(
 
247
    tests.TestCaseWithMemoryTransport):
 
248
    """Tests for BzrDir.destroy_branch."""
 
249
 
 
250
    def test_destroy_branch_default(self):
 
251
        """The default branch can be removed."""
 
252
        backing = self.get_transport()
 
253
        dir = self.make_branch('.').bzrdir
 
254
        request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
 
255
        request = request_class(backing)
 
256
        expected = smart_req.SuccessfulSmartServerResponse(('ok',))
 
257
        self.assertEqual(expected, request.execute('', None))
 
258
 
 
259
    def test_destroy_branch_named(self):
 
260
        """A named branch can be removed."""
 
261
        backing = self.get_transport()
 
262
        dir = self.make_repository('.', format="development-colo").bzrdir
 
263
        dir.create_branch(name="branchname")
 
264
        request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
 
265
        request = request_class(backing)
 
266
        expected = smart_req.SuccessfulSmartServerResponse(('ok',))
 
267
        self.assertEqual(expected, request.execute('', "branchname"))
 
268
 
 
269
    def test_destroy_branch_missing(self):
 
270
        """An error is raised if the branch didn't exist."""
 
271
        backing = self.get_transport()
 
272
        dir = self.make_bzrdir('.', format="development-colo")
 
273
        request_class = smart_dir.SmartServerBzrDirRequestDestroyBranch
 
274
        request = request_class(backing)
 
275
        expected = smart_req.FailedSmartServerResponse(('nobranch',), None)
 
276
        self.assertEqual(expected, request.execute('', "branchname"))
 
277
 
 
278
 
 
279
class TestSmartServerBzrDirRequestHasWorkingTree(
 
280
    tests.TestCaseWithTransport):
 
281
    """Tests for BzrDir.has_workingtree."""
 
282
 
 
283
    def test_has_workingtree_yes(self):
 
284
        """A working tree is present."""
 
285
        backing = self.get_transport()
 
286
        dir = self.make_branch_and_tree('.').bzrdir
 
287
        request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
 
288
        request = request_class(backing)
 
289
        expected = smart_req.SuccessfulSmartServerResponse(('yes',))
 
290
        self.assertEqual(expected, request.execute(''))
 
291
 
 
292
    def test_has_workingtree_no(self):
 
293
        """A working tree is missing."""
 
294
        backing = self.get_transport()
 
295
        dir = self.make_bzrdir('.')
 
296
        request_class = smart_dir.SmartServerBzrDirRequestHasWorkingTree
 
297
        request = request_class(backing)
 
298
        expected = smart_req.SuccessfulSmartServerResponse(('no',))
 
299
        self.assertEqual(expected, request.execute(''))
 
300
 
 
301
 
 
302
class TestSmartServerBzrDirRequestDestroyRepository(
 
303
    tests.TestCaseWithMemoryTransport):
 
304
    """Tests for BzrDir.destroy_repository."""
 
305
 
 
306
    def test_destroy_repository_default(self):
 
307
        """The repository can be removed."""
 
308
        backing = self.get_transport()
 
309
        dir = self.make_repository('.').bzrdir
 
310
        request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
 
311
        request = request_class(backing)
 
312
        expected = smart_req.SuccessfulSmartServerResponse(('ok',))
 
313
        self.assertEqual(expected, request.execute(''))
 
314
 
 
315
    def test_destroy_repository_missing(self):
 
316
        """An error is raised if the repository didn't exist."""
 
317
        backing = self.get_transport()
 
318
        dir = self.make_bzrdir('.')
 
319
        request_class = smart_dir.SmartServerBzrDirRequestDestroyRepository
 
320
        request = request_class(backing)
 
321
        expected = smart_req.FailedSmartServerResponse(
 
322
            ('norepository',), None)
 
323
        self.assertEqual(expected, request.execute(''))
 
324
 
 
325
 
224
326
class TestSmartServerRequestCreateRepository(tests.TestCaseWithMemoryTransport):
225
327
    """Tests for BzrDir.create_repository."""
226
328
 
735
837
            request.execute(''))
736
838
 
737
839
 
 
840
class TestSmartServerBranchRequestRevisionIdToRevno(
 
841
    tests.TestCaseWithMemoryTransport):
 
842
 
 
843
    def test_null(self):
 
844
        backing = self.get_transport()
 
845
        request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
 
846
            backing)
 
847
        self.make_branch('.')
 
848
        self.assertEqual(smart_req.SmartServerResponse(('ok', '0')),
 
849
            request.execute('', 'null:'))
 
850
 
 
851
    def test_simple(self):
 
852
        backing = self.get_transport()
 
853
        request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
 
854
            backing)
 
855
        tree = self.make_branch_and_memory_tree('.')
 
856
        tree.lock_write()
 
857
        tree.add('')
 
858
        r1 = tree.commit('1st commit')
 
859
        tree.unlock()
 
860
        self.assertEqual(
 
861
            smart_req.SmartServerResponse(('ok', '1')),
 
862
            request.execute('', r1))
 
863
 
 
864
    def test_not_found(self):
 
865
        backing = self.get_transport()
 
866
        request = smart_branch.SmartServerBranchRequestRevisionIdToRevno(
 
867
            backing)
 
868
        branch = self.make_branch('.')
 
869
        self.assertEqual(
 
870
            smart_req.FailedSmartServerResponse(
 
871
                ('NoSuchRevision', 'idontexist')),
 
872
            request.execute('', 'idontexist'))
 
873
 
 
874
 
738
875
class TestSmartServerBranchRequestGetConfigFile(
739
876
    tests.TestCaseWithMemoryTransport):
740
877
 
769
906
        return branch_token, repo_token
770
907
 
771
908
 
 
909
class TestSmartServerBranchRequestPutConfigFile(TestLockedBranch):
 
910
 
 
911
    def test_with_content(self):
 
912
        backing = self.get_transport()
 
913
        request = smart_branch.SmartServerBranchPutConfigFile(backing)
 
914
        branch = self.make_branch('.')
 
915
        branch_token, repo_token = self.get_lock_tokens(branch)
 
916
        self.assertIs(None, request.execute('', branch_token, repo_token))
 
917
        self.assertEqual(
 
918
            smart_req.SmartServerResponse(('ok', )),
 
919
            request.do_body('foo bar baz'))
 
920
        self.assertEquals(
 
921
            branch.control_transport.get_bytes('branch.conf'),
 
922
            'foo bar baz')
 
923
        branch.unlock()
 
924
 
 
925
 
772
926
class TestSmartServerBranchRequestSetConfigOption(TestLockedBranch):
773
927
 
774
928
    def test_value_name(self):
1088
1242
        self.assertEqual('child-1', self.tree.branch.last_revision())
1089
1243
 
1090
1244
 
 
1245
class TestSmartServerBranchBreakLock(tests.TestCaseWithMemoryTransport):
 
1246
 
 
1247
    def test_lock_to_break(self):
 
1248
        base_branch = self.make_branch('base')
 
1249
        request = smart_branch.SmartServerBranchBreakLock(
 
1250
            self.get_transport())
 
1251
        base_branch.lock_write()
 
1252
        self.assertEqual(
 
1253
            smart_req.SuccessfulSmartServerResponse(('ok', ), None),
 
1254
            request.execute('base'))
 
1255
 
 
1256
    def test_nothing_to_break(self):
 
1257
        base_branch = self.make_branch('base')
 
1258
        request = smart_branch.SmartServerBranchBreakLock(
 
1259
            self.get_transport())
 
1260
        self.assertEqual(
 
1261
            smart_req.SuccessfulSmartServerResponse(('ok', ), None),
 
1262
            request.execute('base'))
 
1263
 
 
1264
 
1091
1265
class TestSmartServerBranchRequestGetParent(tests.TestCaseWithMemoryTransport):
1092
1266
 
1093
1267
    def test_get_parent_none(self):
1275
1449
        self.assertEqual('LockFailed', error_name)
1276
1450
 
1277
1451
 
 
1452
class TestSmartServerBranchRequestGetPhysicalLockStatus(TestLockedBranch):
 
1453
 
 
1454
    def setUp(self):
 
1455
        tests.TestCaseWithMemoryTransport.setUp(self)
 
1456
 
 
1457
    def test_true(self):
 
1458
        backing = self.get_transport()
 
1459
        request = smart_branch.SmartServerBranchRequestGetPhysicalLockStatus(
 
1460
            backing)
 
1461
        branch = self.make_branch('.')
 
1462
        branch_token, repo_token = self.get_lock_tokens(branch)
 
1463
        self.assertEquals(True, branch.get_physical_lock_status())
 
1464
        response = request.execute('')
 
1465
        self.assertEqual(
 
1466
            smart_req.SmartServerResponse(('yes',)), response)
 
1467
        branch.unlock()
 
1468
 
 
1469
    def test_false(self):
 
1470
        backing = self.get_transport()
 
1471
        request = smart_branch.SmartServerBranchRequestGetPhysicalLockStatus(
 
1472
            backing)
 
1473
        branch = self.make_branch('.')
 
1474
        self.assertEquals(False, branch.get_physical_lock_status())
 
1475
        response = request.execute('')
 
1476
        self.assertEqual(
 
1477
            smart_req.SmartServerResponse(('no',)), response)
 
1478
 
 
1479
 
1278
1480
class TestSmartServerBranchRequestUnlock(TestLockedBranch):
1279
1481
 
1280
1482
    def setUp(self):
1345
1547
            request.execute, 'subdir')
1346
1548
 
1347
1549
 
 
1550
class TestSmartServerRepositoryAddSignatureText(tests.TestCaseWithMemoryTransport):
 
1551
 
 
1552
    def test_add_text(self):
 
1553
        backing = self.get_transport()
 
1554
        request = smart_repo.SmartServerRepositoryAddSignatureText(backing)
 
1555
        tree = self.make_branch_and_memory_tree('.')
 
1556
        write_token = tree.lock_write()
 
1557
        self.addCleanup(tree.unlock)
 
1558
        tree.add('')
 
1559
        tree.commit("Message", rev_id='rev1')
 
1560
        tree.branch.repository.start_write_group()
 
1561
        write_group_tokens = tree.branch.repository.suspend_write_group()
 
1562
        self.assertEqual(None, request.execute('', write_token,
 
1563
            'rev1', *write_group_tokens))
 
1564
        response = request.do_body('somesignature')
 
1565
        self.assertTrue(response.is_successful())
 
1566
        self.assertEqual(response.args[0], 'ok')
 
1567
        write_group_tokens = response.args[1:]
 
1568
        tree.branch.repository.resume_write_group(write_group_tokens)
 
1569
        tree.branch.repository.commit_write_group()
 
1570
        tree.unlock()
 
1571
        self.assertEqual("somesignature",
 
1572
            tree.branch.repository.get_signature_text("rev1"))
 
1573
 
 
1574
 
 
1575
class TestSmartServerRepositoryAllRevisionIds(
 
1576
    tests.TestCaseWithMemoryTransport):
 
1577
 
 
1578
    def test_empty(self):
 
1579
        """An empty body should be returned for an empty repository."""
 
1580
        backing = self.get_transport()
 
1581
        request = smart_repo.SmartServerRepositoryAllRevisionIds(backing)
 
1582
        self.make_repository('.')
 
1583
        self.assertEquals(
 
1584
            smart_req.SuccessfulSmartServerResponse(("ok", ), ""),
 
1585
            request.execute(''))
 
1586
 
 
1587
    def test_some_revisions(self):
 
1588
        """An empty body should be returned for an empty repository."""
 
1589
        backing = self.get_transport()
 
1590
        request = smart_repo.SmartServerRepositoryAllRevisionIds(backing)
 
1591
        tree = self.make_branch_and_memory_tree('.')
 
1592
        tree.lock_write()
 
1593
        tree.add('')
 
1594
        tree.commit(rev_id='origineel', message="message")
 
1595
        tree.commit(rev_id='nog-een-revisie', message="message")
 
1596
        tree.unlock()
 
1597
        self.assertEquals(
 
1598
            smart_req.SuccessfulSmartServerResponse(("ok", ),
 
1599
                "origineel\nnog-een-revisie"),
 
1600
            request.execute(''))
 
1601
 
 
1602
 
 
1603
class TestSmartServerRepositoryBreakLock(tests.TestCaseWithMemoryTransport):
 
1604
 
 
1605
    def test_lock_to_break(self):
 
1606
        backing = self.get_transport()
 
1607
        request = smart_repo.SmartServerRepositoryBreakLock(backing)
 
1608
        tree = self.make_branch_and_memory_tree('.')
 
1609
        tree.branch.repository.lock_write()
 
1610
        self.assertEqual(
 
1611
            smart_req.SuccessfulSmartServerResponse(('ok', ), None),
 
1612
            request.execute(''))
 
1613
 
 
1614
    def test_nothing_to_break(self):
 
1615
        backing = self.get_transport()
 
1616
        request = smart_repo.SmartServerRepositoryBreakLock(backing)
 
1617
        tree = self.make_branch_and_memory_tree('.')
 
1618
        self.assertEqual(
 
1619
            smart_req.SuccessfulSmartServerResponse(('ok', ), None),
 
1620
            request.execute(''))
 
1621
 
 
1622
 
1348
1623
class TestSmartServerRepositoryGetParentMap(tests.TestCaseWithMemoryTransport):
1349
1624
 
1350
1625
    def test_trivial_bzipped(self):
1470
1745
            request.execute('stacked', 1, (3, r3)))
1471
1746
 
1472
1747
 
 
1748
class TestSmartServerRepositoryIterRevisions(
 
1749
    tests.TestCaseWithMemoryTransport):
 
1750
 
 
1751
    def test_basic(self):
 
1752
        backing = self.get_transport()
 
1753
        request = smart_repo.SmartServerRepositoryIterRevisions(backing)
 
1754
        tree = self.make_branch_and_memory_tree('.', format='2a')
 
1755
        tree.lock_write()
 
1756
        tree.add('')
 
1757
        tree.commit('1st commit', rev_id="rev1")
 
1758
        tree.commit('2nd commit', rev_id="rev2")
 
1759
        tree.unlock()
 
1760
 
 
1761
        self.assertIs(None, request.execute(''))
 
1762
        response = request.do_body("rev1\nrev2")
 
1763
        self.assertTrue(response.is_successful())
 
1764
        # Format 2a uses serializer format 10
 
1765
        self.assertEquals(response.args, ("ok", "10"))
 
1766
 
 
1767
        self.addCleanup(tree.branch.lock_read().unlock)
 
1768
        entries = [zlib.compress(record.get_bytes_as("fulltext")) for record in
 
1769
            tree.branch.repository.revisions.get_record_stream(
 
1770
            [("rev1", ), ("rev2", )], "unordered", True)]
 
1771
 
 
1772
        contents = "".join(response.body_stream)
 
1773
        self.assertTrue(contents in (
 
1774
            "".join([entries[0], entries[1]]),
 
1775
            "".join([entries[1], entries[0]])))
 
1776
 
 
1777
    def test_missing(self):
 
1778
        backing = self.get_transport()
 
1779
        request = smart_repo.SmartServerRepositoryIterRevisions(backing)
 
1780
        tree = self.make_branch_and_memory_tree('.', format='2a')
 
1781
 
 
1782
        self.assertIs(None, request.execute(''))
 
1783
        response = request.do_body("rev1\nrev2")
 
1784
        self.assertTrue(response.is_successful())
 
1785
        # Format 2a uses serializer format 10
 
1786
        self.assertEquals(response.args, ("ok", "10"))
 
1787
 
 
1788
        contents = "".join(response.body_stream)
 
1789
        self.assertEquals(contents, "")
 
1790
 
 
1791
 
1473
1792
class GetStreamTestBase(tests.TestCaseWithMemoryTransport):
1474
1793
 
1475
1794
    def make_two_commit_repo(self):
1549
1868
            request.execute('', rev_id_utf8))
1550
1869
 
1551
1870
 
 
1871
class TestSmartServerRepositoryIterFilesBytes(tests.TestCaseWithTransport):
 
1872
 
 
1873
    def test_single(self):
 
1874
        backing = self.get_transport()
 
1875
        request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
 
1876
        t = self.make_branch_and_tree('.')
 
1877
        self.addCleanup(t.lock_write().unlock)
 
1878
        self.build_tree_contents([("file", "somecontents")])
 
1879
        t.add(["file"], ["thefileid"])
 
1880
        t.commit(rev_id='somerev', message="add file")
 
1881
        self.assertIs(None, request.execute(''))
 
1882
        response = request.do_body("thefileid\0somerev\n")
 
1883
        self.assertTrue(response.is_successful())
 
1884
        self.assertEquals(response.args, ("ok", ))
 
1885
        self.assertEquals("".join(response.body_stream),
 
1886
            "ok\x000\n" + zlib.compress("somecontents"))
 
1887
 
 
1888
    def test_missing(self):
 
1889
        backing = self.get_transport()
 
1890
        request = smart_repo.SmartServerRepositoryIterFilesBytes(backing)
 
1891
        t = self.make_branch_and_tree('.')
 
1892
        self.addCleanup(t.lock_write().unlock)
 
1893
        self.assertIs(None, request.execute(''))
 
1894
        response = request.do_body("thefileid\0revision\n")
 
1895
        self.assertTrue(response.is_successful())
 
1896
        self.assertEquals(response.args, ("ok", ))
 
1897
        self.assertEquals("".join(response.body_stream),
 
1898
            "absent\x00thefileid\x00revision\x000\n")
 
1899
 
 
1900
 
 
1901
class TestSmartServerRequestHasSignatureForRevisionId(
 
1902
        tests.TestCaseWithMemoryTransport):
 
1903
 
 
1904
    def test_missing_revision(self):
 
1905
        """For a missing revision, NoSuchRevision is returned."""
 
1906
        backing = self.get_transport()
 
1907
        request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
 
1908
            backing)
 
1909
        self.make_repository('.')
 
1910
        self.assertEqual(
 
1911
            smart_req.FailedSmartServerResponse(
 
1912
                ('nosuchrevision', 'revid'), None),
 
1913
            request.execute('', 'revid'))
 
1914
 
 
1915
    def test_missing_signature(self):
 
1916
        """For a missing signature, ('no', ) is returned."""
 
1917
        backing = self.get_transport()
 
1918
        request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
 
1919
            backing)
 
1920
        tree = self.make_branch_and_memory_tree('.')
 
1921
        tree.lock_write()
 
1922
        tree.add('')
 
1923
        r1 = tree.commit('a commit', rev_id='A')
 
1924
        tree.unlock()
 
1925
        self.assertTrue(tree.branch.repository.has_revision('A'))
 
1926
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
1927
            request.execute('', 'A'))
 
1928
 
 
1929
    def test_present_signature(self):
 
1930
        """For a present signature, ('yes', ) is returned."""
 
1931
        backing = self.get_transport()
 
1932
        request = smart_repo.SmartServerRequestHasSignatureForRevisionId(
 
1933
            backing)
 
1934
        strategy = gpg.LoopbackGPGStrategy(None)
 
1935
        tree = self.make_branch_and_memory_tree('.')
 
1936
        tree.lock_write()
 
1937
        tree.add('')
 
1938
        r1 = tree.commit('a commit', rev_id='A')
 
1939
        tree.branch.repository.start_write_group()
 
1940
        tree.branch.repository.sign_revision('A', strategy)
 
1941
        tree.branch.repository.commit_write_group()
 
1942
        tree.unlock()
 
1943
        self.assertTrue(tree.branch.repository.has_revision('A'))
 
1944
        self.assertEqual(smart_req.SmartServerResponse(('yes', )),
 
1945
            request.execute('', 'A'))
 
1946
 
 
1947
 
1552
1948
class TestSmartServerRepositoryGatherStats(tests.TestCaseWithMemoryTransport):
1553
1949
 
1554
1950
    def test_empty_revid(self):
1607
2003
                         request.execute('',
1608
2004
                                         rev_id_utf8, 'yes'))
1609
2005
 
 
2006
    def test_unknown_revid(self):
 
2007
        """An unknown revision id causes a 'nosuchrevision' error."""
 
2008
        backing = self.get_transport()
 
2009
        request = smart_repo.SmartServerRepositoryGatherStats(backing)
 
2010
        repository = self.make_repository('.')
 
2011
        expected_body = 'revisions: 0\n'
 
2012
        self.assertEqual(
 
2013
            smart_req.FailedSmartServerResponse(
 
2014
                ('nosuchrevision', 'mia'), None),
 
2015
            request.execute('', 'mia', 'yes'))
 
2016
 
1610
2017
 
1611
2018
class TestSmartServerRepositoryIsShared(tests.TestCaseWithMemoryTransport):
1612
2019
 
1627
2034
            request.execute('', ))
1628
2035
 
1629
2036
 
 
2037
class TestSmartServerRepositoryGetRevisionSignatureText(
 
2038
        tests.TestCaseWithMemoryTransport):
 
2039
 
 
2040
    def test_get_signature(self):
 
2041
        backing = self.get_transport()
 
2042
        request = smart_repo.SmartServerRepositoryGetRevisionSignatureText(
 
2043
            backing)
 
2044
        bb = self.make_branch_builder('.')
 
2045
        bb.build_commit(rev_id='A')
 
2046
        repo = bb.get_branch().repository
 
2047
        strategy = gpg.LoopbackGPGStrategy(None)
 
2048
        self.addCleanup(repo.lock_write().unlock)
 
2049
        repo.start_write_group()
 
2050
        repo.sign_revision('A', strategy)
 
2051
        repo.commit_write_group()
 
2052
        expected_body = (
 
2053
            '-----BEGIN PSEUDO-SIGNED CONTENT-----\n' +
 
2054
            Testament.from_revision(repo, 'A').as_short_text() +
 
2055
            '-----END PSEUDO-SIGNED CONTENT-----\n')
 
2056
        self.assertEqual(
 
2057
            smart_req.SmartServerResponse(('ok', ), expected_body),
 
2058
            request.execute('', 'A'))
 
2059
 
 
2060
 
 
2061
class TestSmartServerRepositoryMakeWorkingTrees(
 
2062
        tests.TestCaseWithMemoryTransport):
 
2063
 
 
2064
    def test_make_working_trees(self):
 
2065
        """For a repository with working trees, ('yes', ) is returned."""
 
2066
        backing = self.get_transport()
 
2067
        request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
 
2068
        r = self.make_repository('.')
 
2069
        r.set_make_working_trees(True)
 
2070
        self.assertEqual(smart_req.SmartServerResponse(('yes', )),
 
2071
            request.execute('', ))
 
2072
 
 
2073
    def test_is_not_shared(self):
 
2074
        """For a repository with working trees, ('no', ) is returned."""
 
2075
        backing = self.get_transport()
 
2076
        request = smart_repo.SmartServerRepositoryMakeWorkingTrees(backing)
 
2077
        r = self.make_repository('.')
 
2078
        r.set_make_working_trees(False)
 
2079
        self.assertEqual(smart_req.SmartServerResponse(('no', )),
 
2080
            request.execute('', ))
 
2081
 
 
2082
 
1630
2083
class TestSmartServerRepositoryLockWrite(tests.TestCaseWithMemoryTransport):
1631
2084
 
1632
2085
    def test_lock_write_on_unlocked_repo(self):
1746
2199
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
1747
2200
 
1748
2201
 
 
2202
class TestSmartServerRepositoryGetPhysicalLockStatus(
 
2203
    tests.TestCaseWithTransport):
 
2204
 
 
2205
    def test_with_write_lock(self):
 
2206
        backing = self.get_transport()
 
2207
        repo = self.make_repository('.')
 
2208
        self.addCleanup(repo.lock_write().unlock)
 
2209
        # lock_write() doesn't necessarily actually take a physical
 
2210
        # lock out.
 
2211
        if repo.get_physical_lock_status():
 
2212
            expected = 'yes'
 
2213
        else:
 
2214
            expected = 'no'
 
2215
        request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
 
2216
        request = request_class(backing)
 
2217
        self.assertEqual(smart_req.SuccessfulSmartServerResponse((expected,)),
 
2218
            request.execute('', ))
 
2219
 
 
2220
    def test_without_write_lock(self):
 
2221
        backing = self.get_transport()
 
2222
        repo = self.make_repository('.')
 
2223
        self.assertEquals(False, repo.get_physical_lock_status())
 
2224
        request_class = smart_repo.SmartServerRepositoryGetPhysicalLockStatus
 
2225
        request = request_class(backing)
 
2226
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('no',)),
 
2227
            request.execute('', ))
 
2228
 
 
2229
 
 
2230
class TestSmartServerRepositoryReconcile(tests.TestCaseWithTransport):
 
2231
 
 
2232
    def test_reconcile(self):
 
2233
        backing = self.get_transport()
 
2234
        repo = self.make_repository('.')
 
2235
        token = repo.lock_write().repository_token
 
2236
        self.addCleanup(repo.unlock)
 
2237
        request_class = smart_repo.SmartServerRepositoryReconcile
 
2238
        request = request_class(backing)
 
2239
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(
 
2240
            ('ok', ),
 
2241
             'garbage_inventories: 0\n'
 
2242
             'inconsistent_parents: 0\n'),
 
2243
            request.execute('', token))
 
2244
 
 
2245
 
1749
2246
class TestSmartServerIsReadonly(tests.TestCaseWithMemoryTransport):
1750
2247
 
1751
2248
    def test_is_readonly_no(self):
1789
2286
        self.assertTrue(repo.make_working_trees())
1790
2287
 
1791
2288
 
 
2289
class TestSmartServerRepositoryGetSerializerFormat(
 
2290
    tests.TestCaseWithMemoryTransport):
 
2291
 
 
2292
    def test_get_serializer_format(self):
 
2293
        backing = self.get_transport()
 
2294
        repo = self.make_repository('.', format='2a')
 
2295
        request_class = smart_repo.SmartServerRepositoryGetSerializerFormat
 
2296
        request = request_class(backing)
 
2297
        self.assertEqual(
 
2298
            smart_req.SuccessfulSmartServerResponse(('ok', '10')),
 
2299
            request.execute(''))
 
2300
 
 
2301
 
 
2302
class TestSmartServerRepositoryWriteGroup(
 
2303
    tests.TestCaseWithMemoryTransport):
 
2304
 
 
2305
    def test_start_write_group(self):
 
2306
        backing = self.get_transport()
 
2307
        repo = self.make_repository('.')
 
2308
        lock_token = repo.lock_write().repository_token
 
2309
        self.addCleanup(repo.unlock)
 
2310
        request_class = smart_repo.SmartServerRepositoryStartWriteGroup
 
2311
        request = request_class(backing)
 
2312
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok', [])),
 
2313
            request.execute('', lock_token))
 
2314
 
 
2315
    def test_start_write_group_unsuspendable(self):
 
2316
        backing = self.get_transport()
 
2317
        repo = self.make_repository('.', format='knit')
 
2318
        lock_token = repo.lock_write().repository_token
 
2319
        self.addCleanup(repo.unlock)
 
2320
        request_class = smart_repo.SmartServerRepositoryStartWriteGroup
 
2321
        request = request_class(backing)
 
2322
        self.assertEqual(
 
2323
            smart_req.FailedSmartServerResponse(('UnsuspendableWriteGroup',)),
 
2324
            request.execute('', lock_token))
 
2325
 
 
2326
    def test_commit_write_group(self):
 
2327
        backing = self.get_transport()
 
2328
        repo = self.make_repository('.')
 
2329
        lock_token = repo.lock_write().repository_token
 
2330
        self.addCleanup(repo.unlock)
 
2331
        repo.start_write_group()
 
2332
        tokens = repo.suspend_write_group()
 
2333
        request_class = smart_repo.SmartServerRepositoryCommitWriteGroup
 
2334
        request = request_class(backing)
 
2335
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
 
2336
            request.execute('', lock_token, tokens))
 
2337
 
 
2338
    def test_abort_write_group(self):
 
2339
        backing = self.get_transport()
 
2340
        repo = self.make_repository('.')
 
2341
        lock_token = repo.lock_write().repository_token
 
2342
        repo.start_write_group()
 
2343
        tokens = repo.suspend_write_group()
 
2344
        self.addCleanup(repo.unlock)
 
2345
        request_class = smart_repo.SmartServerRepositoryAbortWriteGroup
 
2346
        request = request_class(backing)
 
2347
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
 
2348
            request.execute('', lock_token, tokens))
 
2349
 
 
2350
    def test_check_write_group(self):
 
2351
        backing = self.get_transport()
 
2352
        repo = self.make_repository('.')
 
2353
        lock_token = repo.lock_write().repository_token
 
2354
        repo.start_write_group()
 
2355
        tokens = repo.suspend_write_group()
 
2356
        self.addCleanup(repo.unlock)
 
2357
        request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
 
2358
        request = request_class(backing)
 
2359
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(('ok',)),
 
2360
            request.execute('', lock_token, tokens))
 
2361
 
 
2362
    def test_check_write_group_invalid(self):
 
2363
        backing = self.get_transport()
 
2364
        repo = self.make_repository('.')
 
2365
        lock_token = repo.lock_write().repository_token
 
2366
        self.addCleanup(repo.unlock)
 
2367
        request_class = smart_repo.SmartServerRepositoryCheckWriteGroup
 
2368
        request = request_class(backing)
 
2369
        self.assertEqual(smart_req.FailedSmartServerResponse(
 
2370
            ('UnresumableWriteGroup', ['random'],
 
2371
                'Malformed write group token')),
 
2372
            request.execute('', lock_token, ["random"]))
 
2373
 
 
2374
 
1792
2375
class TestSmartServerPackRepositoryAutopack(tests.TestCaseWithTransport):
1793
2376
 
1794
2377
    def make_repo_needing_autopacking(self, path='.'):
1860
2443
        """All registered request_handlers can be found."""
1861
2444
        # If there's a typo in a register_lazy call, this loop will fail with
1862
2445
        # an AttributeError.
1863
 
        for key, item in smart_req.request_handlers.iteritems():
1864
 
            pass
 
2446
        for key in smart_req.request_handlers.keys():
 
2447
            try:
 
2448
                item = smart_req.request_handlers.get(key)
 
2449
            except AttributeError, e:
 
2450
                raise AttributeError('failed to get %s: %s' % (key, e))
1865
2451
 
1866
2452
    def assertHandlerEqual(self, verb, handler):
1867
2453
        self.assertEqual(smart_req.request_handlers.get(verb), handler)
1868
2454
 
1869
2455
    def test_registered_methods(self):
1870
2456
        """Test that known methods are registered to the correct object."""
 
2457
        self.assertHandlerEqual('Branch.break_lock',
 
2458
            smart_branch.SmartServerBranchBreakLock)
1871
2459
        self.assertHandlerEqual('Branch.get_config_file',
1872
2460
            smart_branch.SmartServerBranchGetConfigFile)
 
2461
        self.assertHandlerEqual('Branch.put_config_file',
 
2462
            smart_branch.SmartServerBranchPutConfigFile)
1873
2463
        self.assertHandlerEqual('Branch.get_parent',
1874
2464
            smart_branch.SmartServerBranchGetParent)
 
2465
        self.assertHandlerEqual('Branch.get_physical_lock_status',
 
2466
            smart_branch.SmartServerBranchRequestGetPhysicalLockStatus)
1875
2467
        self.assertHandlerEqual('Branch.get_tags_bytes',
1876
2468
            smart_branch.SmartServerBranchGetTagsBytes)
1877
2469
        self.assertHandlerEqual('Branch.lock_write',
1880
2472
            smart_branch.SmartServerBranchRequestLastRevisionInfo)
1881
2473
        self.assertHandlerEqual('Branch.revision_history',
1882
2474
            smart_branch.SmartServerRequestRevisionHistory)
 
2475
        self.assertHandlerEqual('Branch.revision_id_to_revno',
 
2476
            smart_branch.SmartServerBranchRequestRevisionIdToRevno)
1883
2477
        self.assertHandlerEqual('Branch.set_config_option',
1884
2478
            smart_branch.SmartServerBranchRequestSetConfigOption)
1885
2479
        self.assertHandlerEqual('Branch.set_last_revision',
1892
2486
            smart_branch.SmartServerBranchRequestSetParentLocation)
1893
2487
        self.assertHandlerEqual('Branch.unlock',
1894
2488
            smart_branch.SmartServerBranchRequestUnlock)
 
2489
        self.assertHandlerEqual('BzrDir.destroy_branch',
 
2490
            smart_dir.SmartServerBzrDirRequestDestroyBranch)
1895
2491
        self.assertHandlerEqual('BzrDir.find_repository',
1896
2492
            smart_dir.SmartServerRequestFindRepositoryV1)
1897
2493
        self.assertHandlerEqual('BzrDir.find_repositoryV2',
1900
2496
            smart_dir.SmartServerRequestInitializeBzrDir)
1901
2497
        self.assertHandlerEqual('BzrDirFormat.initialize_ex_1.16',
1902
2498
            smart_dir.SmartServerRequestBzrDirInitializeEx)
 
2499
        self.assertHandlerEqual('BzrDir.checkout_metadir',
 
2500
            smart_dir.SmartServerBzrDirRequestCheckoutMetaDir)
1903
2501
        self.assertHandlerEqual('BzrDir.cloning_metadir',
1904
2502
            smart_dir.SmartServerBzrDirRequestCloningMetaDir)
1905
2503
        self.assertHandlerEqual('BzrDir.get_config_file',
1912
2510
            smart_dir.SmartServerRequestOpenBranchV3)
1913
2511
        self.assertHandlerEqual('PackRepository.autopack',
1914
2512
            smart_packrepo.SmartServerPackRepositoryAutopack)
 
2513
        self.assertHandlerEqual('Repository.add_signature_text',
 
2514
            smart_repo.SmartServerRepositoryAddSignatureText)
 
2515
        self.assertHandlerEqual('Repository.all_revision_ids',
 
2516
            smart_repo.SmartServerRepositoryAllRevisionIds)
 
2517
        self.assertHandlerEqual('Repository.break_lock',
 
2518
            smart_repo.SmartServerRepositoryBreakLock)
1915
2519
        self.assertHandlerEqual('Repository.gather_stats',
1916
2520
            smart_repo.SmartServerRepositoryGatherStats)
1917
2521
        self.assertHandlerEqual('Repository.get_parent_map',
1918
2522
            smart_repo.SmartServerRepositoryGetParentMap)
 
2523
        self.assertHandlerEqual('Repository.get_physical_lock_status',
 
2524
            smart_repo.SmartServerRepositoryGetPhysicalLockStatus)
1919
2525
        self.assertHandlerEqual('Repository.get_rev_id_for_revno',
1920
2526
            smart_repo.SmartServerRepositoryGetRevIdForRevno)
1921
2527
        self.assertHandlerEqual('Repository.get_revision_graph',
1922
2528
            smart_repo.SmartServerRepositoryGetRevisionGraph)
 
2529
        self.assertHandlerEqual('Repository.get_revision_signature_text',
 
2530
            smart_repo.SmartServerRepositoryGetRevisionSignatureText)
1923
2531
        self.assertHandlerEqual('Repository.get_stream',
1924
2532
            smart_repo.SmartServerRepositoryGetStream)
1925
2533
        self.assertHandlerEqual('Repository.get_stream_1.19',
1926
2534
            smart_repo.SmartServerRepositoryGetStream_1_19)
 
2535
        self.assertHandlerEqual('Repository.iter_revisions',
 
2536
            smart_repo.SmartServerRepositoryIterRevisions)
1927
2537
        self.assertHandlerEqual('Repository.has_revision',
1928
2538
            smart_repo.SmartServerRequestHasRevision)
1929
2539
        self.assertHandlerEqual('Repository.insert_stream',
1932
2542
            smart_repo.SmartServerRepositoryInsertStreamLocked)
1933
2543
        self.assertHandlerEqual('Repository.is_shared',
1934
2544
            smart_repo.SmartServerRepositoryIsShared)
 
2545
        self.assertHandlerEqual('Repository.iter_files_bytes',
 
2546
            smart_repo.SmartServerRepositoryIterFilesBytes)
1935
2547
        self.assertHandlerEqual('Repository.lock_write',
1936
2548
            smart_repo.SmartServerRepositoryLockWrite)
 
2549
        self.assertHandlerEqual('Repository.make_working_trees',
 
2550
            smart_repo.SmartServerRepositoryMakeWorkingTrees)
 
2551
        self.assertHandlerEqual('Repository.pack',
 
2552
            smart_repo.SmartServerRepositoryPack)
 
2553
        self.assertHandlerEqual('Repository.reconcile',
 
2554
            smart_repo.SmartServerRepositoryReconcile)
1937
2555
        self.assertHandlerEqual('Repository.tarball',
1938
2556
            smart_repo.SmartServerRepositoryTarball)
1939
2557
        self.assertHandlerEqual('Repository.unlock',
1940
2558
            smart_repo.SmartServerRepositoryUnlock)
 
2559
        self.assertHandlerEqual('Repository.start_write_group',
 
2560
            smart_repo.SmartServerRepositoryStartWriteGroup)
 
2561
        self.assertHandlerEqual('Repository.check_write_group',
 
2562
            smart_repo.SmartServerRepositoryCheckWriteGroup)
 
2563
        self.assertHandlerEqual('Repository.commit_write_group',
 
2564
            smart_repo.SmartServerRepositoryCommitWriteGroup)
 
2565
        self.assertHandlerEqual('Repository.abort_write_group',
 
2566
            smart_repo.SmartServerRepositoryAbortWriteGroup)
 
2567
        self.assertHandlerEqual('VersionedFileRepository.get_serializer_format',
 
2568
            smart_repo.SmartServerRepositoryGetSerializerFormat)
 
2569
        self.assertHandlerEqual('VersionedFileRepository.get_inventories',
 
2570
            smart_repo.SmartServerRepositoryGetInventories)
1941
2571
        self.assertHandlerEqual('Transport.is_readonly',
1942
2572
            smart_req.SmartServerIsReadonly)
1943
2573
 
1987
2617
        self.server.run_server_stopped_hooks()
1988
2618
        self.assertEquals(stopped_calls,
1989
2619
            [([self.get_transport().base], 'bzr://example.com:42/')])
 
2620
 
 
2621
 
 
2622
class TestSmartServerRepositoryPack(tests.TestCaseWithMemoryTransport):
 
2623
 
 
2624
    def test_pack(self):
 
2625
        backing = self.get_transport()
 
2626
        request = smart_repo.SmartServerRepositoryPack(backing)
 
2627
        tree = self.make_branch_and_memory_tree('.')
 
2628
        repo_token = tree.branch.repository.lock_write().repository_token
 
2629
 
 
2630
        self.assertIs(None, request.execute('', repo_token, False))
 
2631
 
 
2632
        self.assertEqual(
 
2633
            smart_req.SuccessfulSmartServerResponse(('ok', ), ),
 
2634
            request.do_body(''))
 
2635
 
 
2636
 
 
2637
class TestSmartServerRepositoryGetInventories(tests.TestCaseWithTransport):
 
2638
 
 
2639
    def _get_serialized_inventory_delta(self, repository, base_revid, revid):
 
2640
        base_inv = repository.revision_tree(base_revid).inventory
 
2641
        inv = repository.revision_tree(revid).inventory
 
2642
        inv_delta = inv._make_delta(base_inv)
 
2643
        serializer = inventory_delta.InventoryDeltaSerializer(True, False)
 
2644
        return "".join(serializer.delta_to_lines(base_revid, revid, inv_delta))
 
2645
 
 
2646
    def test_single(self):
 
2647
        backing = self.get_transport()
 
2648
        request = smart_repo.SmartServerRepositoryGetInventories(backing)
 
2649
        t = self.make_branch_and_tree('.', format='2a')
 
2650
        self.addCleanup(t.lock_write().unlock)
 
2651
        self.build_tree_contents([("file", "somecontents")])
 
2652
        t.add(["file"], ["thefileid"])
 
2653
        t.commit(rev_id='somerev', message="add file")
 
2654
        self.assertIs(None, request.execute('', 'unordered'))
 
2655
        response = request.do_body("somerev\n")
 
2656
        self.assertTrue(response.is_successful())
 
2657
        self.assertEquals(response.args, ("ok", ))
 
2658
        stream = [('inventory-deltas', [
 
2659
            versionedfile.FulltextContentFactory('somerev', None, None,
 
2660
                self._get_serialized_inventory_delta(
 
2661
                    t.branch.repository, 'null:', 'somerev'))])]
 
2662
        fmt = bzrdir.format_registry.get('2a')().repository_format
 
2663
        self.assertEquals(
 
2664
            "".join(response.body_stream),
 
2665
            "".join(smart_repo._stream_to_byte_stream(stream, fmt)))
 
2666
 
 
2667
    def test_empty(self):
 
2668
        backing = self.get_transport()
 
2669
        request = smart_repo.SmartServerRepositoryGetInventories(backing)
 
2670
        t = self.make_branch_and_tree('.', format='2a')
 
2671
        self.addCleanup(t.lock_write().unlock)
 
2672
        self.build_tree_contents([("file", "somecontents")])
 
2673
        t.add(["file"], ["thefileid"])
 
2674
        t.commit(rev_id='somerev', message="add file")
 
2675
        self.assertIs(None, request.execute('', 'unordered'))
 
2676
        response = request.do_body("")
 
2677
        self.assertTrue(response.is_successful())
 
2678
        self.assertEquals(response.args, ("ok", ))
 
2679
        self.assertEquals("".join(response.body_stream),
 
2680
            "Bazaar pack format 1 (introduced in 0.18)\nB54\n\nBazaar repository format 2a (needs bzr 1.16 or later)\nE")