~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/per_transport.py

  • Committer: Tarmac
  • Author(s): Vincent Ladeuil
  • Date: 2017-01-30 14:42:05 UTC
  • mfrom: (6620.1.1 trunk)
  • Revision ID: tarmac-20170130144205-r8fh2xpmiuxyozpv
Merge  2.7 into trunk including fix for bug #1657238 [r=vila]

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2011 Canonical Ltd
 
1
# Copyright (C) 2005-2011, 2015, 2016 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
53
53
from bzrlib.tests.test_transport import TestTransportImplementation
54
54
from bzrlib.transport import (
55
55
    ConnectedTransport,
 
56
    Transport,
56
57
    _get_transport_modules,
57
58
    )
58
59
from bzrlib.transport.memory import MemoryTransport
 
60
from bzrlib.transport.remote import RemoteTransport
59
61
 
60
62
 
61
63
def get_transport_test_permutations(module):
518
520
        self.assertTransportMode(t, 'dir777', 0777)
519
521
 
520
522
    def test_put_bytes_unicode(self):
521
 
        # Expect put_bytes to raise AssertionError or UnicodeEncodeError if
522
 
        # given unicode "bytes".  UnicodeEncodeError doesn't really make sense
523
 
        # (we don't want to encode unicode here at all, callers should be
524
 
        # strictly passing bytes to put_bytes), but we allow it for backwards
525
 
        # compatibility.  At some point we should use a specific exception.
526
 
        # See https://bugs.launchpad.net/bzr/+bug/106898.
527
523
        t = self.get_transport()
528
524
        if t.is_readonly():
529
525
            return
530
526
        unicode_string = u'\u1234'
531
 
        self.assertRaises(
532
 
            (AssertionError, UnicodeEncodeError),
533
 
            t.put_bytes, 'foo', unicode_string)
534
 
 
535
 
    def test_put_file_unicode(self):
536
 
        # Like put_bytes, except with a StringIO.StringIO of a unicode string.
537
 
        # This situation can happen (and has) if code is careless about the type
538
 
        # of "string" they initialise/write to a StringIO with.  We cannot use
539
 
        # cStringIO, because it never returns unicode from read.
540
 
        # Like put_bytes, UnicodeEncodeError isn't quite the right exception to
541
 
        # raise, but we raise it for hysterical raisins.
542
 
        t = self.get_transport()
543
 
        if t.is_readonly():
544
 
            return
545
 
        unicode_file = pyStringIO(u'\u1234')
546
 
        self.assertRaises(UnicodeEncodeError, t.put_file, 'foo', unicode_file)
 
527
        self.assertRaises(TypeError, t.put_bytes, 'foo', unicode_string)
547
528
 
548
529
    def test_mkdir(self):
549
530
        t = self.get_transport()
628
609
    def test_opening_a_file_stream_can_set_mode(self):
629
610
        t = self.get_transport()
630
611
        if t.is_readonly():
 
612
            self.assertRaises((TransportNotPossible, NotImplementedError),
 
613
                              t.open_write_stream, 'foo')
631
614
            return
632
615
        if not t._can_roundtrip_unix_modebits():
633
616
            # Can't roundtrip, so no need to run this test
634
617
            return
 
618
 
635
619
        def check_mode(name, mode, expected):
636
620
            handle = t.open_write_stream(name, mode=mode)
637
621
            handle.close()
919
903
    def test_rename_dir_succeeds(self):
920
904
        t = self.get_transport()
921
905
        if t.is_readonly():
922
 
            raise TestSkipped("transport is readonly")
 
906
            self.assertRaises((TransportNotPossible, NotImplementedError),
 
907
                              t.rename, 'foo', 'bar')
 
908
            return
923
909
        t.mkdir('adir')
924
910
        t.mkdir('adir/asubdir')
925
911
        t.rename('adir', 'bdir')
930
916
        """Attempting to replace a nonemtpy directory should fail"""
931
917
        t = self.get_transport()
932
918
        if t.is_readonly():
933
 
            raise TestSkipped("transport is readonly")
 
919
            self.assertRaises((TransportNotPossible, NotImplementedError),
 
920
                              t.rename, 'foo', 'bar')
 
921
            return
934
922
        t.mkdir('adir')
935
923
        t.mkdir('adir/asubdir')
936
924
        t.mkdir('bdir')
1000
988
        # perhaps all of this could be done in a subdirectory
1001
989
 
1002
990
        t.put_bytes('a', 'a first file\n')
1003
 
        self.assertEquals([True, False], list(t.has_multi(['a', 'b'])))
 
991
        self.assertEqual([True, False], list(t.has_multi(['a', 'b'])))
1004
992
 
1005
993
        t.move('a', 'b')
1006
994
        self.assertTrue(t.has('b'))
1007
995
        self.assertFalse(t.has('a'))
1008
996
 
1009
997
        self.check_transport_contents('a first file\n', t, 'b')
1010
 
        self.assertEquals([False, True], list(t.has_multi(['a', 'b'])))
 
998
        self.assertEqual([False, True], list(t.has_multi(['a', 'b'])))
1011
999
 
1012
1000
        # Overwrite a file
1013
1001
        t.put_bytes('c', 'c this file\n')
1050
1038
        except NotImplementedError:
1051
1039
            raise TestSkipped("Transport %s has no bogus URL support." %
1052
1040
                              self._server.__class__)
1053
 
        t = _mod_transport.get_transport(url)
 
1041
        t = _mod_transport.get_transport_from_url(url)
1054
1042
        self.assertRaises((ConnectionError, NoSuchFile), t.get, '.bzr/branch')
1055
1043
 
1056
1044
    def test_stat(self):
1136
1124
            raise TestSkipped("Transport %s does not support symlinks." %
1137
1125
                              self._server.__class__)
1138
1126
        except IOError:
1139
 
            raise tests.KnownFailure("Paramiko fails to create symlinks during tests")
 
1127
            self.knownFailure("Paramiko fails to create symlinks during tests")
1140
1128
 
1141
1129
    def test_list_dir(self):
1142
1130
        # TODO: Test list_dir, just try once, and if it throws, stop testing
1206
1194
            raise TestSkipped("not a connected transport")
1207
1195
 
1208
1196
        t2 = t1.clone('subdir')
1209
 
        self.assertEquals(t1._scheme, t2._scheme)
1210
 
        self.assertEquals(t1._user, t2._user)
1211
 
        self.assertEquals(t1._password, t2._password)
1212
 
        self.assertEquals(t1._host, t2._host)
1213
 
        self.assertEquals(t1._port, t2._port)
 
1197
        self.assertEqual(t1._parsed_url.scheme, t2._parsed_url.scheme)
 
1198
        self.assertEqual(t1._parsed_url.user, t2._parsed_url.user)
 
1199
        self.assertEqual(t1._parsed_url.password, t2._parsed_url.password)
 
1200
        self.assertEqual(t1._parsed_url.host, t2._parsed_url.host)
 
1201
        self.assertEqual(t1._parsed_url.port, t2._parsed_url.port)
1214
1202
 
1215
1203
    def test__reuse_for(self):
1216
1204
        t = self.get_transport()
1223
1211
 
1224
1212
            Only the parameters different from None will be changed.
1225
1213
            """
1226
 
            if scheme   is None: scheme   = t._scheme
1227
 
            if user     is None: user     = t._user
1228
 
            if password is None: password = t._password
1229
 
            if user     is None: user     = t._user
1230
 
            if host     is None: host     = t._host
1231
 
            if port     is None: port     = t._port
1232
 
            if path     is None: path     = t._path
1233
 
            return t._unsplit_url(scheme, user, password, host, port, path)
 
1214
            if scheme   is None: scheme   = t._parsed_url.scheme
 
1215
            if user     is None: user     = t._parsed_url.user
 
1216
            if password is None: password = t._parsed_url.password
 
1217
            if user     is None: user     = t._parsed_url.user
 
1218
            if host     is None: host     = t._parsed_url.host
 
1219
            if port     is None: port     = t._parsed_url.port
 
1220
            if path     is None: path     = t._parsed_url.path
 
1221
            return str(urlutils.URL(scheme, user, password, host, port, path))
1234
1222
 
1235
 
        if t._scheme == 'ftp':
 
1223
        if t._parsed_url.scheme == 'ftp':
1236
1224
            scheme = 'sftp'
1237
1225
        else:
1238
1226
            scheme = 'ftp'
1239
1227
        self.assertIsNot(t, t._reuse_for(new_url(scheme=scheme)))
1240
 
        if t._user == 'me':
 
1228
        if t._parsed_url.user == 'me':
1241
1229
            user = 'you'
1242
1230
        else:
1243
1231
            user = 'me'
1254
1242
        #   (they may be typed by the user when prompted for example)
1255
1243
        self.assertIs(t, t._reuse_for(new_url(password='from space')))
1256
1244
        # We will not connect, we can use a invalid host
1257
 
        self.assertIsNot(t, t._reuse_for(new_url(host=t._host + 'bar')))
1258
 
        if t._port == 1234:
 
1245
        self.assertIsNot(t, t._reuse_for(new_url(host=t._parsed_url.host + 'bar')))
 
1246
        if t._parsed_url.port == 1234:
1259
1247
            port = 4321
1260
1248
        else:
1261
1249
            port = 1234
1418
1406
 
1419
1407
        # smoke test for abspath on win32.
1420
1408
        # a transport based on 'file:///' never fully qualifies the drive.
1421
 
        transport = _mod_transport.get_transport("file:///")
 
1409
        transport = _mod_transport.get_transport_from_url("file:///")
1422
1410
        self.assertEqual(transport.abspath("/"), "file:///")
1423
1411
 
1424
1412
        # but a transport that starts with a drive spec must keep it.
1425
 
        transport = _mod_transport.get_transport("file:///C:/")
 
1413
        transport = _mod_transport.get_transport_from_url("file:///C:/")
1426
1414
        self.assertEqual(transport.abspath("/"), "file:///C:/")
1427
1415
 
1428
1416
    def test_local_abspath(self):
1555
1543
 
1556
1544
        no_unicode_support = getattr(self._server, 'no_unicode_support', False)
1557
1545
        if no_unicode_support:
1558
 
            raise tests.KnownFailure("test server cannot handle unicode paths")
 
1546
            self.knownFailure("test server cannot handle unicode paths")
1559
1547
 
1560
1548
        try:
1561
1549
            self.build_tree(files, transport=t, line_endings='binary')
1776
1764
        self.assertListRaises((errors.ShortReadvError, errors.InvalidRange),
1777
1765
                              transport.readv, 'a', [(12,2)])
1778
1766
 
 
1767
    def test_no_segment_parameters(self):
 
1768
        """Segment parameters should be stripped and stored in
 
1769
        transport.segment_parameters."""
 
1770
        transport = self.get_transport("foo")
 
1771
        self.assertEqual({}, transport.get_segment_parameters())
 
1772
 
 
1773
    def test_segment_parameters(self):
 
1774
        """Segment parameters should be stripped and stored in
 
1775
        transport.get_segment_parameters()."""
 
1776
        base_url = self._server.get_url()
 
1777
        parameters = {"key1": "val1", "key2": "val2"}
 
1778
        url = urlutils.join_segment_parameters(base_url, parameters)
 
1779
        transport = _mod_transport.get_transport_from_url(url)
 
1780
        self.assertEqual(parameters, transport.get_segment_parameters())
 
1781
 
 
1782
    def test_set_segment_parameters(self):
 
1783
        """Segment parameters can be set and show up in base."""
 
1784
        transport = self.get_transport("foo")
 
1785
        orig_base = transport.base
 
1786
        transport.set_segment_parameter("arm", "board")
 
1787
        self.assertEqual("%s,arm=board" % orig_base, transport.base)
 
1788
        self.assertEqual({"arm": "board"}, transport.get_segment_parameters())
 
1789
        transport.set_segment_parameter("arm", None)
 
1790
        transport.set_segment_parameter("nonexistant", None)
 
1791
        self.assertEqual({}, transport.get_segment_parameters())
 
1792
        self.assertEqual(orig_base, transport.base)
 
1793
 
1779
1794
    def test_stat_symlink(self):
1780
1795
        # if a transport points directly to a symlink (and supports symlinks
1781
1796
        # at all) you can tell this.  helps with bug 32669.
1787
1802
        t2 = t.clone('link')
1788
1803
        st = t2.stat('')
1789
1804
        self.assertTrue(stat.S_ISLNK(st.st_mode))
 
1805
 
 
1806
    def test_abspath_url_unquote_unreserved(self):
 
1807
        """URLs from abspath should have unreserved characters unquoted
 
1808
        
 
1809
        Need consistent quoting notably for tildes, see lp:842223 for more.
 
1810
        """
 
1811
        t = self.get_transport()
 
1812
        needlessly_escaped_dir = "%2D%2E%30%39%41%5A%5F%61%7A%7E/"
 
1813
        self.assertEqual(t.base + "-.09AZ_az~",
 
1814
            t.abspath(needlessly_escaped_dir))
 
1815
 
 
1816
    def test_clone_url_unquote_unreserved(self):
 
1817
        """Base URL of a cloned branch needs unreserved characters unquoted
 
1818
        
 
1819
        Cloned transports should be prefix comparable for things like the
 
1820
        isolation checking of tests, see lp:842223 for more.
 
1821
        """
 
1822
        t1 = self.get_transport()
 
1823
        needlessly_escaped_dir = "%2D%2E%30%39%41%5A%5F%61%7A%7E/"
 
1824
        self.build_tree([needlessly_escaped_dir], transport=t1)
 
1825
        t2 = t1.clone(needlessly_escaped_dir)
 
1826
        self.assertEqual(t1.base + "-.09AZ_az~/", t2.base)
 
1827
 
 
1828
    def test_hook_post_connection_one(self):
 
1829
        """Fire post_connect hook after a ConnectedTransport is first used"""
 
1830
        log = []
 
1831
        Transport.hooks.install_named_hook("post_connect", log.append, None)
 
1832
        t = self.get_transport()
 
1833
        self.assertEqual([], log)
 
1834
        t.has("non-existant")
 
1835
        if isinstance(t, RemoteTransport):
 
1836
            self.assertEqual([t.get_smart_medium()], log)
 
1837
        elif isinstance(t, ConnectedTransport):
 
1838
            self.assertEqual([t], log)
 
1839
        else:
 
1840
            self.assertEqual([], log)
 
1841
 
 
1842
    def test_hook_post_connection_multi(self):
 
1843
        """Fire post_connect hook once per unshared underlying connection"""
 
1844
        log = []
 
1845
        Transport.hooks.install_named_hook("post_connect", log.append, None)
 
1846
        t1 = self.get_transport()
 
1847
        t2 = t1.clone(".")
 
1848
        t3 = self.get_transport()
 
1849
        self.assertEqual([], log)
 
1850
        t1.has("x")
 
1851
        t2.has("x")
 
1852
        t3.has("x")
 
1853
        if isinstance(t1, RemoteTransport):
 
1854
            self.assertEqual([t.get_smart_medium() for t in [t1, t3]], log)
 
1855
        elif isinstance(t1, ConnectedTransport):
 
1856
            self.assertEqual([t1, t3], log)
 
1857
        else:
 
1858
            self.assertEqual([], log)