/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2005-2011, 2015, 2016 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
907.1.48 by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
907.1.48 by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
907.1.48 by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
907.1.48 by John Arbash Meinel
Updated LocalTransport by passing it through the transport_test suite, and got it to pass.
16
17
6015.50.1 by Martin Pool
Use a chmod wrapper to cope with eperm from chmod
18
import errno
6039.1.6 by Jelmer Vernooij
Add more tests.
19
import os
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
20
import subprocess
21
import sys
22
import threading
1442.1.44 by Robert Collins
Many transport related tweaks:
23
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
24
from .. import (
2018.18.4 by Martin Pool
Change Transport.local_abspath to raise NotLocalUrl, and test.
25
    errors,
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
26
    osutils,
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
27
    tests,
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
28
    transport,
2018.18.4 by Martin Pool
Change Transport.local_abspath to raise NotLocalUrl, and test.
29
    urlutils,
30
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
31
from ..directory_service import directories
32
from ..sixish import (
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
33
    BytesIO,
34
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
35
from ..transport import (
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
36
    chroot,
4946.1.2 by John Arbash Meinel
Clean up a few more imports.
37
    fakenfs,
4912.2.4 by Martin Pool
Add test for unhtml_roughly, and truncate at 1000 bytes
38
    http,
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
39
    local,
6030.2.2 by Jelmer Vernooij
Add test.
40
    location_to_url,
4634.108.13 by John Arbash Meinel
Add a test case.
41
    memory,
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
42
    pathfilter,
4946.1.2 by John Arbash Meinel
Clean up a few more imports.
43
    readonly,
4634.108.13 by John Arbash Meinel
Add a test case.
44
    )
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
45
import breezy.transport.trace
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
46
from . import (
5017.3.22 by Vincent Ladeuil
selftest -s bt.test_transport passing
47
    features,
48
    test_server,
49
    )
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
50
51
52
# TODO: Should possibly split transport-specific tests into their own files.
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
53
54
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
55
class TestTransport(tests.TestCase):
1185.58.3 by John Arbash Meinel
code cleanup
56
    """Test the non transport-concrete class functionality."""
57
2241.3.1 by ghigo
uncomment test test__get_set_protocol_handlers
58
    def test__get_set_protocol_handlers(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
59
        handlers = transport._get_protocol_handlers()
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
60
        self.assertNotEqual([], handlers.keys())
61
        transport._clear_protocol_handlers()
62
        self.addCleanup(transport._set_protocol_handlers, handlers)
63
        self.assertEqual([], transport._get_protocol_handlers().keys())
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
64
65
    def test_get_transport_modules(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
66
        handlers = transport._get_protocol_handlers()
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
67
        self.addCleanup(transport._set_protocol_handlers, handlers)
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
68
        # don't pollute the current handlers
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
69
        transport._clear_protocol_handlers()
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
70
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
71
        class SampleHandler(object):
72
            """I exist, isnt that enough?"""
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
73
        transport._clear_protocol_handlers()
74
        transport.register_transport_proto('foo')
75
        transport.register_lazy_transport('foo',
7143.15.2 by Jelmer Vernooij
Run autopep8.
76
                                          'breezy.tests.test_transport',
77
                                          'TestTransport.SampleHandler')
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
78
        transport.register_transport_proto('bar')
79
        transport.register_lazy_transport('bar',
7143.15.2 by Jelmer Vernooij
Run autopep8.
80
                                          'breezy.tests.test_transport',
81
                                          'TestTransport.SampleHandler')
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
82
        self.assertEqual([SampleHandler.__module__,
7143.15.2 by Jelmer Vernooij
Run autopep8.
83
                          'breezy.transport.chroot',
84
                          'breezy.transport.pathfilter'],
85
                         transport._get_transport_modules())
1540.3.8 by Martin Pool
Some support for falling back between transport implementations.
86
87
    def test_transport_dependency(self):
88
        """Transport with missing dependency causes no error"""
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
89
        saved_handlers = transport._get_protocol_handlers()
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
90
        self.addCleanup(transport._set_protocol_handlers, saved_handlers)
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
91
        # don't pollute the current handlers
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
92
        transport._clear_protocol_handlers()
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
93
        transport.register_transport_proto('foo')
94
        transport.register_lazy_transport(
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
95
            'foo', 'breezy.tests.test_transport', 'BadTransportHandler')
1540.3.8 by Martin Pool
Some support for falling back between transport implementations.
96
        try:
6039.1.6 by Jelmer Vernooij
Add more tests.
97
            transport.get_transport_from_url('foo://fooserver/foo')
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
98
        except errors.UnsupportedProtocol as e:
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
99
            self.assertEqual('Unsupported protocol'
7143.15.2 by Jelmer Vernooij
Run autopep8.
100
                             ' for url "foo://fooserver/foo":'
101
                             ' Unable to import library "some_lib":'
102
                             ' testing missing dependency', str(e))
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
103
        else:
104
            self.fail('Did not raise UnsupportedProtocol')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
105
1540.3.10 by Martin Pool
[broken] keep hooking pycurl into test framework
106
    def test_transport_fallback(self):
107
        """Transport with missing dependency causes no error"""
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
108
        saved_handlers = transport._get_protocol_handlers()
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
109
        self.addCleanup(transport._set_protocol_handlers, saved_handlers)
110
        transport._clear_protocol_handlers()
111
        transport.register_transport_proto('foo')
112
        transport.register_lazy_transport(
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
113
            'foo', 'breezy.tests.test_transport', 'BackupTransportHandler')
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
114
        transport.register_lazy_transport(
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
115
            'foo', 'breezy.tests.test_transport', 'BadTransportHandler')
6039.1.6 by Jelmer Vernooij
Add more tests.
116
        t = transport.get_transport_from_url('foo://fooserver/foo')
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
117
        self.assertTrue(isinstance(t, BackupTransportHandler))
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
118
4011.4.1 by Jelmer Vernooij
Point out bzr+ssh:// to the user when they use ssh://.
119
    def test_ssh_hints(self):
120
        """Transport ssh:// should raise an error pointing out bzr+ssh://"""
121
        try:
6039.1.6 by Jelmer Vernooij
Add more tests.
122
            transport.get_transport_from_url('ssh://fooserver/foo')
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
123
        except errors.UnsupportedProtocol as e:
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
124
            self.assertEqual('Unsupported protocol'
7143.15.2 by Jelmer Vernooij
Run autopep8.
125
                             ' for url "ssh://fooserver/foo":'
126
                             ' bzr supports bzr+ssh to operate over ssh,'
127
                             ' use "bzr+ssh://fooserver/foo".',
128
                             str(e))
4011.4.1 by Jelmer Vernooij
Point out bzr+ssh:// to the user when they use ssh://.
129
        else:
130
            self.fail('Did not raise UnsupportedProtocol')
131
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
132
    def test_LateReadError(self):
133
        """The LateReadError helper should raise on read()."""
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
134
        a_file = transport.LateReadError('a path')
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
135
        try:
136
            a_file.read()
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
137
        except errors.ReadError as error:
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
138
            self.assertEqual('a path', error.path)
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
139
        self.assertRaises(errors.ReadError, a_file.read, 40)
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
140
        a_file.close()
141
2018.18.4 by Martin Pool
Change Transport.local_abspath to raise NotLocalUrl, and test.
142
    def test_local_abspath_non_local_transport(self):
143
        # the base implementation should throw
4634.108.13 by John Arbash Meinel
Add a test case.
144
        t = memory.MemoryTransport()
2018.18.4 by Martin Pool
Change Transport.local_abspath to raise NotLocalUrl, and test.
145
        e = self.assertRaises(errors.NotLocalUrl, t.local_abspath, 't')
146
        self.assertEqual('memory:///t is not a local path.', str(e))
147
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
148
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
149
class TestCoalesceOffsets(tests.TestCase):
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
150
151
    def check(self, expected, offsets, limit=0, max_size=0, fudge=0):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
152
        coalesce = transport.Transport._coalesce_offsets
153
        exp = [transport._CoalescedOffset(*x) for x in expected]
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
154
        out = list(coalesce(offsets, limit=limit, fudge_factor=fudge,
155
                            max_size=max_size))
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
156
        self.assertEqual(exp, out)
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
157
158
    def test_coalesce_empty(self):
159
        self.check([], [])
160
161
    def test_coalesce_simple(self):
162
        self.check([(0, 10, [(0, 10)])], [(0, 10)])
163
164
    def test_coalesce_unrelated(self):
165
        self.check([(0, 10, [(0, 10)]),
166
                    (20, 10, [(0, 10)]),
7143.15.2 by Jelmer Vernooij
Run autopep8.
167
                    ], [(0, 10), (20, 10)])
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
168
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
169
    def test_coalesce_unsorted(self):
170
        self.check([(20, 10, [(0, 10)]),
171
                    (0, 10, [(0, 10)]),
7143.15.2 by Jelmer Vernooij
Run autopep8.
172
                    ], [(20, 10), (0, 10)])
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
173
174
    def test_coalesce_nearby(self):
175
        self.check([(0, 20, [(0, 10), (10, 10)])],
176
                   [(0, 10), (10, 10)])
177
178
    def test_coalesce_overlapped(self):
3686.1.9 by John Arbash Meinel
Overlapping ranges are not allowed anymore.
179
        self.assertRaises(ValueError,
7143.15.2 by Jelmer Vernooij
Run autopep8.
180
                          self.check, [(0, 15, [(0, 10), (5, 10)])],
181
                          [(0, 10), (5, 10)])
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
182
183
    def test_coalesce_limit(self):
184
        self.check([(10, 50, [(0, 10), (10, 10), (20, 10),
185
                              (30, 10), (40, 10)]),
186
                    (60, 50, [(0, 10), (10, 10), (20, 10),
187
                              (30, 10), (40, 10)]),
7143.15.2 by Jelmer Vernooij
Run autopep8.
188
                    ], [(10, 10), (20, 10), (30, 10), (40, 10),
189
                        (50, 10), (60, 10), (70, 10), (80, 10),
190
                        (90, 10), (100, 10)],
191
                   limit=5)
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
192
193
    def test_coalesce_no_limit(self):
194
        self.check([(10, 100, [(0, 10), (10, 10), (20, 10),
195
                               (30, 10), (40, 10), (50, 10),
196
                               (60, 10), (70, 10), (80, 10),
197
                               (90, 10)]),
7143.15.2 by Jelmer Vernooij
Run autopep8.
198
                    ], [(10, 10), (20, 10), (30, 10), (40, 10),
199
                        (50, 10), (60, 10), (70, 10), (80, 10),
200
                        (90, 10), (100, 10)])
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
201
1864.5.3 by John Arbash Meinel
Allow collapsing ranges even if they are just 'close'
202
    def test_coalesce_fudge(self):
203
        self.check([(10, 30, [(0, 10), (20, 10)]),
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
204
                    (100, 10, [(0, 10)]),
7143.15.2 by Jelmer Vernooij
Run autopep8.
205
                    ], [(10, 10), (30, 10), (100, 10)],
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
206
                   fudge=10)
207
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
208
    def test_coalesce_max_size(self):
209
        self.check([(10, 20, [(0, 10), (10, 10)]),
210
                    (30, 50, [(0, 50)]),
211
                    # If one range is above max_size, it gets its own coalesced
212
                    # offset
7143.15.2 by Jelmer Vernooij
Run autopep8.
213
                    (100, 80, [(0, 80)]), ],
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
214
                   [(10, 10), (20, 10), (30, 50), (100, 80)],
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
215
                   max_size=50)
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
216
217
    def test_coalesce_no_max_size(self):
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
218
        self.check([(10, 170, [(0, 10), (10, 10), (20, 50), (70, 100)])],
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
219
                   [(10, 10), (20, 10), (30, 50), (80, 100)],
7143.15.2 by Jelmer Vernooij
Run autopep8.
220
                   )
1864.5.3 by John Arbash Meinel
Allow collapsing ranges even if they are just 'close'
221
3876.1.2 by John Arbash Meinel
Add a test case that checks the 100MB limit.
222
    def test_coalesce_default_limit(self):
223
        # By default we use a 100MB max size.
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
224
        ten_mb = 10 * 1024 * 1024
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
225
        self.check(
226
            [(0, 10 * ten_mb, [(i * ten_mb, ten_mb) for i in range(10)]),
227
             (10 * ten_mb, ten_mb, [(0, ten_mb)])],
228
            [(i * ten_mb, ten_mb) for i in range(11)])
229
        self.check(
230
            [(0, 11 * ten_mb, [(i * ten_mb, ten_mb) for i in range(11)])],
231
            [(i * ten_mb, ten_mb) for i in range(11)],
232
            max_size=1 * 1024 * 1024 * 1024)
3876.1.2 by John Arbash Meinel
Add a test case that checks the 100MB limit.
233
1540.3.3 by Martin Pool
Review updates of pycurl transport
234
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
235
class TestMemoryServer(tests.TestCase):
4634.108.13 by John Arbash Meinel
Add a test case.
236
237
    def test_create_server(self):
5017.3.45 by Vincent Ladeuil
Move MemoryServer back into bzrlib.transport.memory as it's needed as soon as a MemoryTransport is used. Add a NEWS entry.
238
        server = memory.MemoryServer()
4946.1.2 by John Arbash Meinel
Clean up a few more imports.
239
        server.start_server()
4634.108.13 by John Arbash Meinel
Add a test case.
240
        url = server.get_url()
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
241
        self.assertTrue(url in transport.transport_list_registry)
6039.1.6 by Jelmer Vernooij
Add more tests.
242
        t = transport.get_transport_from_url(url)
4634.108.13 by John Arbash Meinel
Add a test case.
243
        del t
4946.1.2 by John Arbash Meinel
Clean up a few more imports.
244
        server.stop_server()
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
245
        self.assertFalse(url in transport.transport_list_registry)
4634.108.13 by John Arbash Meinel
Add a test case.
246
        self.assertRaises(errors.UnsupportedProtocol,
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
247
                          transport.get_transport, url)
248
249
250
class TestMemoryTransport(tests.TestCase):
1442.1.44 by Robert Collins
Many transport related tweaks:
251
252
    def test_get_transport(self):
4634.108.13 by John Arbash Meinel
Add a test case.
253
        memory.MemoryTransport()
1442.1.44 by Robert Collins
Many transport related tweaks:
254
255
    def test_clone(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
256
        t = memory.MemoryTransport()
257
        self.assertTrue(isinstance(t, memory.MemoryTransport))
258
        self.assertEqual("memory:///", t.clone("/").base)
1442.1.44 by Robert Collins
Many transport related tweaks:
259
260
    def test_abspath(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
261
        t = memory.MemoryTransport()
262
        self.assertEqual("memory:///relpath", t.abspath('relpath'))
1442.1.44 by Robert Collins
Many transport related tweaks:
263
1910.15.1 by Andrew Bennetts
More tests for abspath and clone behaviour
264
    def test_abspath_of_root(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
265
        t = memory.MemoryTransport()
266
        self.assertEqual("memory:///", t.base)
267
        self.assertEqual("memory:///", t.abspath('/'))
1910.15.1 by Andrew Bennetts
More tests for abspath and clone behaviour
268
2070.3.1 by Andrew Bennetts
Fix memory_transport.abspath('/foo')
269
    def test_abspath_of_relpath_starting_at_root(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
270
        t = memory.MemoryTransport()
271
        self.assertEqual("memory:///foo", t.abspath('/foo'))
1442.1.44 by Robert Collins
Many transport related tweaks:
272
273
    def test_append_and_get(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
274
        t = memory.MemoryTransport()
6973.7.4 by Jelmer Vernooij
Fix some more tests.
275
        t.append_bytes('path', b'content')
276
        self.assertEqual(t.get('path').read(), b'content')
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
277
        t.append_file('path', BytesIO(b'content'))
6973.7.4 by Jelmer Vernooij
Fix some more tests.
278
        with t.get('path') as f:
279
            self.assertEqual(f.read(), b'contentcontent')
1442.1.44 by Robert Collins
Many transport related tweaks:
280
281
    def test_put_and_get(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
282
        t = memory.MemoryTransport()
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
283
        t.put_file('path', BytesIO(b'content'))
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
284
        self.assertEqual(t.get('path').read(), b'content')
285
        t.put_bytes('path', b'content')
286
        self.assertEqual(t.get('path').read(), b'content')
1442.1.44 by Robert Collins
Many transport related tweaks:
287
288
    def test_append_without_dir_fails(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
289
        t = memory.MemoryTransport()
290
        self.assertRaises(errors.NoSuchFile,
6973.7.4 by Jelmer Vernooij
Fix some more tests.
291
                          t.append_bytes, 'dir/path', b'content')
1442.1.44 by Robert Collins
Many transport related tweaks:
292
293
    def test_put_without_dir_fails(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
294
        t = memory.MemoryTransport()
295
        self.assertRaises(errors.NoSuchFile,
6621.22.2 by Martin
Use BytesIO or StringIO from bzrlib.sixish
296
                          t.put_file, 'dir/path', BytesIO(b'content'))
1442.1.44 by Robert Collins
Many transport related tweaks:
297
298
    def test_get_missing(self):
4634.108.13 by John Arbash Meinel
Add a test case.
299
        transport = memory.MemoryTransport()
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
300
        self.assertRaises(errors.NoSuchFile, transport.get, 'foo')
1442.1.44 by Robert Collins
Many transport related tweaks:
301
302
    def test_has_missing(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
303
        t = memory.MemoryTransport()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
304
        self.assertEqual(False, t.has('foo'))
1442.1.44 by Robert Collins
Many transport related tweaks:
305
306
    def test_has_present(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
307
        t = memory.MemoryTransport()
6973.7.4 by Jelmer Vernooij
Fix some more tests.
308
        t.append_bytes('foo', b'content')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
309
        self.assertEqual(True, t.has('foo'))
1442.1.44 by Robert Collins
Many transport related tweaks:
310
2120.3.1 by John Arbash Meinel
Fix MemoryTransport.list_dir() implementation, and update tests
311
    def test_list_dir(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
312
        t = memory.MemoryTransport()
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
313
        t.put_bytes('foo', b'content')
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
314
        t.mkdir('dir')
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
315
        t.put_bytes('dir/subfoo', b'content')
316
        t.put_bytes('dirlike', b'content')
2120.3.1 by John Arbash Meinel
Fix MemoryTransport.list_dir() implementation, and update tests
317
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
318
        self.assertEqual(['dir', 'dirlike', 'foo'], sorted(t.list_dir('.')))
319
        self.assertEqual(['subfoo'], sorted(t.list_dir('dir')))
2120.3.1 by John Arbash Meinel
Fix MemoryTransport.list_dir() implementation, and update tests
320
1442.1.44 by Robert Collins
Many transport related tweaks:
321
    def test_mkdir(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
322
        t = memory.MemoryTransport()
323
        t.mkdir('dir')
6973.7.4 by Jelmer Vernooij
Fix some more tests.
324
        t.append_bytes('dir/path', b'content')
325
        with t.get('dir/path') as f:
326
            self.assertEqual(f.read(), b'content')
1442.1.44 by Robert Collins
Many transport related tweaks:
327
328
    def test_mkdir_missing_parent(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
329
        t = memory.MemoryTransport()
330
        self.assertRaises(errors.NoSuchFile, t.mkdir, 'dir/dir')
1442.1.44 by Robert Collins
Many transport related tweaks:
331
332
    def test_mkdir_twice(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
333
        t = memory.MemoryTransport()
334
        t.mkdir('dir')
335
        self.assertRaises(errors.FileExists, t.mkdir, 'dir')
1530.1.5 by Robert Collins
Reinstate Memory parameter tests.
336
337
    def test_parameters(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
338
        t = memory.MemoryTransport()
339
        self.assertEqual(True, t.listable())
340
        self.assertEqual(False, t.is_readonly())
1442.1.44 by Robert Collins
Many transport related tweaks:
341
342
    def test_iter_files_recursive(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
343
        t = memory.MemoryTransport()
344
        t.mkdir('dir')
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
345
        t.put_bytes('dir/foo', b'content')
346
        t.put_bytes('dir/bar', b'content')
347
        t.put_bytes('bar', b'content')
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
348
        paths = set(t.iter_files_recursive())
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
349
        self.assertEqual({'dir/foo', 'dir/bar', 'bar'}, paths)
1442.1.44 by Robert Collins
Many transport related tweaks:
350
351
    def test_stat(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
352
        t = memory.MemoryTransport()
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
353
        t.put_bytes('foo', b'content')
354
        t.put_bytes('bar', b'phowar')
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
355
        self.assertEqual(7, t.stat('foo').st_size)
356
        self.assertEqual(6, t.stat('bar').st_size)
357
358
359
class ChrootDecoratorTransportTest(tests.TestCase):
2070.5.1 by Andrew Bennetts
Add ChrootTransportDecorator.
360
    """Chroot decoration specific tests."""
361
2018.5.54 by Andrew Bennetts
Fix ChrootTransportDecorator's abspath method to be consistent with its clone
362
    def test_abspath(self):
363
        # The abspath is always relative to the chroot_url.
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
364
        server = chroot.ChrootServer(
6039.1.6 by Jelmer Vernooij
Add more tests.
365
            transport.get_transport_from_url('memory:///foo/bar/'))
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
366
        self.start_server(server)
6039.1.6 by Jelmer Vernooij
Add more tests.
367
        t = transport.get_transport_from_url(server.get_url())
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
368
        self.assertEqual(server.get_url(), t.abspath('/'))
2018.5.54 by Andrew Bennetts
Fix ChrootTransportDecorator's abspath method to be consistent with its clone
369
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
370
        subdir_t = t.clone('subdir')
371
        self.assertEqual(server.get_url(), subdir_t.abspath('/'))
2379.2.1 by Robert Collins
Rewritten chroot transport that prevents accidental chroot escapes when
372
373
    def test_clone(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
374
        server = chroot.ChrootServer(
6039.1.6 by Jelmer Vernooij
Add more tests.
375
            transport.get_transport_from_url('memory:///foo/bar/'))
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
376
        self.start_server(server)
6039.1.6 by Jelmer Vernooij
Add more tests.
377
        t = transport.get_transport_from_url(server.get_url())
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
378
        # relpath from root and root path are the same
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
379
        relpath_cloned = t.clone('foo')
380
        abspath_cloned = t.clone('/foo')
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
381
        self.assertEqual(server, relpath_cloned.server)
382
        self.assertEqual(server, abspath_cloned.server)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
383
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
384
    def test_chroot_url_preserves_chroot(self):
385
        """Calling get_transport on a chroot transport's base should produce a
386
        transport with exactly the same behaviour as the original chroot
387
        transport.
388
389
        This is so that it is not possible to escape a chroot by doing::
390
            url = chroot_transport.base
391
            parent_url = urlutils.join(url, '..')
6039.1.6 by Jelmer Vernooij
Add more tests.
392
            new_t = transport.get_transport_from_url(parent_url)
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
393
        """
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
394
        server = chroot.ChrootServer(
6039.1.6 by Jelmer Vernooij
Add more tests.
395
            transport.get_transport_from_url('memory:///path/subpath'))
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
396
        self.start_server(server)
6039.1.6 by Jelmer Vernooij
Add more tests.
397
        t = transport.get_transport_from_url(server.get_url())
398
        new_t = transport.get_transport_from_url(t.base)
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
399
        self.assertEqual(t.server, new_t.server)
400
        self.assertEqual(t.base, new_t.base)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
401
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
402
    def test_urljoin_preserves_chroot(self):
403
        """Using urlutils.join(url, '..') on a chroot URL should not produce a
404
        URL that escapes the intended chroot.
405
406
        This is so that it is not possible to escape a chroot by doing::
407
            url = chroot_transport.base
408
            parent_url = urlutils.join(url, '..')
6039.1.6 by Jelmer Vernooij
Add more tests.
409
            new_t = transport.get_transport_from_url(parent_url)
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
410
        """
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
411
        server = chroot.ChrootServer(
6039.1.6 by Jelmer Vernooij
Add more tests.
412
            transport.get_transport_from_url('memory:///path/'))
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
413
        self.start_server(server)
6039.1.6 by Jelmer Vernooij
Add more tests.
414
        t = transport.get_transport_from_url(server.get_url())
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
415
        self.assertRaises(
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
416
            urlutils.InvalidURLJoin, urlutils.join, t.base, '..')
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
417
418
419
class TestChrootServer(tests.TestCase):
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
420
421
    def test_construct(self):
4634.108.13 by John Arbash Meinel
Add a test case.
422
        backing_transport = memory.MemoryTransport()
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
423
        server = chroot.ChrootServer(backing_transport)
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
424
        self.assertEqual(backing_transport, server.backing_transport)
425
426
    def test_setUp(self):
4634.108.13 by John Arbash Meinel
Add a test case.
427
        backing_transport = memory.MemoryTransport()
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
428
        server = chroot.ChrootServer(backing_transport)
4934.3.3 by Martin Pool
Rename Server.setUp to Server.start_server
429
        server.start_server()
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
430
        self.addCleanup(server.stop_server)
431
        self.assertTrue(server.scheme
432
                        in transport._get_protocol_handlers().keys())
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
433
4934.3.1 by Martin Pool
Rename Server.tearDown to .stop_server
434
    def test_stop_server(self):
4634.108.13 by John Arbash Meinel
Add a test case.
435
        backing_transport = memory.MemoryTransport()
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
436
        server = chroot.ChrootServer(backing_transport)
4934.3.3 by Martin Pool
Rename Server.setUp to Server.start_server
437
        server.start_server()
4934.3.1 by Martin Pool
Rename Server.tearDown to .stop_server
438
        server.stop_server()
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
439
        self.assertFalse(server.scheme
440
                         in transport._get_protocol_handlers().keys())
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
441
442
    def test_get_url(self):
4634.108.13 by John Arbash Meinel
Add a test case.
443
        backing_transport = memory.MemoryTransport()
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
444
        server = chroot.ChrootServer(backing_transport)
4934.3.3 by Martin Pool
Rename Server.setUp to Server.start_server
445
        server.start_server()
6006.2.1 by Martin Pool
Clean up transport tests to use addCleanup
446
        self.addCleanup(server.stop_server)
447
        self.assertEqual('chroot-%d:///' % id(server), server.get_url())
2018.5.53 by Andrew Bennetts
Small fix to urlutils.joinpath that was causing a misbehaviour in
448
2156.2.1 by v.ladeuil+lp at free
Make the tests windows compatible.
449
5436.3.1 by Martin
Create new post_connect hook for transports
450
class TestHooks(tests.TestCase):
451
    """Basic tests for transport hooks"""
452
453
    def _get_connected_transport(self):
454
        return transport.ConnectedTransport("bogus:nowhere")
455
456
    def test_transporthooks_initialisation(self):
5436.3.5 by Martin
Ahem, whitespace fix
457
        """Check all expected transport hook points are set up"""
458
        hookpoint = transport.TransportHooks()
459
        self.assertTrue("post_connect" in hookpoint,
7143.15.2 by Jelmer Vernooij
Run autopep8.
460
                        "post_connect not in %s" % (hookpoint,))
5436.3.1 by Martin
Create new post_connect hook for transports
461
462
    def test_post_connect(self):
5436.3.5 by Martin
Ahem, whitespace fix
463
        """Ensure the post_connect hook is called when _set_transport is"""
464
        calls = []
465
        transport.Transport.hooks.install_named_hook("post_connect",
7143.15.2 by Jelmer Vernooij
Run autopep8.
466
                                                     calls.append, None)
5436.3.1 by Martin
Create new post_connect hook for transports
467
        t = self._get_connected_transport()
468
        self.assertLength(0, calls)
469
        t._set_connection("connection", "auth")
470
        self.assertEqual(calls, [t])
471
472
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
473
class PathFilteringDecoratorTransportTest(tests.TestCase):
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
474
    """Pathfilter decoration specific tests."""
475
476
    def test_abspath(self):
477
        # The abspath is always relative to the base of the backing transport.
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
478
        server = pathfilter.PathFilteringServer(
6039.1.6 by Jelmer Vernooij
Add more tests.
479
            transport.get_transport_from_url('memory:///foo/bar/'),
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
480
            lambda x: x)
4934.3.3 by Martin Pool
Rename Server.setUp to Server.start_server
481
        server.start_server()
6039.1.6 by Jelmer Vernooij
Add more tests.
482
        t = transport.get_transport_from_url(server.get_url())
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
483
        self.assertEqual(server.get_url(), t.abspath('/'))
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
484
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
485
        subdir_t = t.clone('subdir')
486
        self.assertEqual(server.get_url(), subdir_t.abspath('/'))
4934.3.1 by Martin Pool
Rename Server.tearDown to .stop_server
487
        server.stop_server()
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
488
489
    def make_pf_transport(self, filter_func=None):
490
        """Make a PathFilteringTransport backed by a MemoryTransport.
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
491
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
492
        :param filter_func: by default this will be a no-op function.  Use this
493
            parameter to override it."""
494
        if filter_func is None:
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
495
            def filter_func(x):
496
                return x
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
497
        server = pathfilter.PathFilteringServer(
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
498
            transport.get_transport_from_url('memory:///foo/bar/'),
499
            filter_func)
4934.3.3 by Martin Pool
Rename Server.setUp to Server.start_server
500
        server.start_server()
4934.3.1 by Martin Pool
Rename Server.tearDown to .stop_server
501
        self.addCleanup(server.stop_server)
6039.1.6 by Jelmer Vernooij
Add more tests.
502
        return transport.get_transport_from_url(server.get_url())
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
503
504
    def test__filter(self):
505
        # _filter (with an identity func as filter_func) always returns
506
        # paths relative to the base of the backing transport.
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
507
        t = self.make_pf_transport()
508
        self.assertEqual('foo', t._filter('foo'))
509
        self.assertEqual('foo/bar', t._filter('foo/bar'))
510
        self.assertEqual('', t._filter('..'))
511
        self.assertEqual('', t._filter('/'))
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
512
        # The base of the pathfiltering transport is taken into account too.
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
513
        t = t.clone('subdir1/subdir2')
514
        self.assertEqual('subdir1/subdir2/foo', t._filter('foo'))
515
        self.assertEqual('subdir1/subdir2/foo/bar', t._filter('foo/bar'))
516
        self.assertEqual('subdir1', t._filter('..'))
517
        self.assertEqual('', t._filter('/'))
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
518
4634.44.2 by Andrew Bennetts
Add another test.
519
    def test_filter_invocation(self):
520
        filter_log = []
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
521
4634.44.2 by Andrew Bennetts
Add another test.
522
        def filter(path):
523
            filter_log.append(path)
524
            return path
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
525
        t = self.make_pf_transport(filter)
526
        t.has('abc')
4634.44.2 by Andrew Bennetts
Add another test.
527
        self.assertEqual(['abc'], filter_log)
528
        del filter_log[:]
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
529
        t.clone('abc').has('xyz')
4634.44.2 by Andrew Bennetts
Add another test.
530
        self.assertEqual(['abc/xyz'], filter_log)
531
        del filter_log[:]
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
532
        t.has('/abc')
4634.44.2 by Andrew Bennetts
Add another test.
533
        self.assertEqual(['abc'], filter_log)
534
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
535
    def test_clone(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
536
        t = self.make_pf_transport()
4634.44.2 by Andrew Bennetts
Add another test.
537
        # relpath from root and root path are the same
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
538
        relpath_cloned = t.clone('foo')
539
        abspath_cloned = t.clone('/foo')
540
        self.assertEqual(t.server, relpath_cloned.server)
541
        self.assertEqual(t.server, abspath_cloned.server)
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
542
543
    def test_url_preserves_pathfiltering(self):
544
        """Calling get_transport on a pathfiltered transport's base should
545
        produce a transport with exactly the same behaviour as the original
546
        pathfiltered transport.
547
548
        This is so that it is not possible to escape (accidentally or
549
        otherwise) the filtering by doing::
550
            url = filtered_transport.base
551
            parent_url = urlutils.join(url, '..')
6039.1.6 by Jelmer Vernooij
Add more tests.
552
            new_t = transport.get_transport_from_url(parent_url)
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
553
        """
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
554
        t = self.make_pf_transport()
6039.1.6 by Jelmer Vernooij
Add more tests.
555
        new_t = transport.get_transport_from_url(t.base)
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
556
        self.assertEqual(t.server, new_t.server)
557
        self.assertEqual(t.base, new_t.base)
558
559
560
class ReadonlyDecoratorTransportTest(tests.TestCase):
1534.4.9 by Robert Collins
Add a readonly decorator for transports.
561
    """Readonly decoration specific tests."""
562
563
    def test_local_parameters(self):
564
        # connect to . in readonly mode
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
565
        t = readonly.ReadonlyTransportDecorator('readonly+.')
566
        self.assertEqual(True, t.listable())
567
        self.assertEqual(True, t.is_readonly())
1534.4.9 by Robert Collins
Add a readonly decorator for transports.
568
569
    def test_http_parameters(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
570
        from breezy.tests.http_server import HttpServer
2929.3.7 by Vincent Ladeuil
Rename bzrlib/test/HttpServer.py to bzrlib/tests/http_server.py and fix uses.
571
        # connect to '.' via http which is not listable
1534.4.9 by Robert Collins
Add a readonly decorator for transports.
572
        server = HttpServer()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
573
        self.start_server(server)
6039.1.6 by Jelmer Vernooij
Add more tests.
574
        t = transport.get_transport_from_url('readonly+' + server.get_url())
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
575
        self.assertIsInstance(t, readonly.ReadonlyTransportDecorator)
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
576
        self.assertEqual(False, t.listable())
577
        self.assertEqual(True, t.is_readonly())
578
579
580
class FakeNFSDecoratorTests(tests.TestCaseInTempDir):
1558.10.2 by Robert Collins
Refactor the FakeNFS support into a TransportDecorator.
581
    """NFS decorator specific tests."""
582
583
    def get_nfs_transport(self, url):
584
        # connect to url with nfs decoration
585
        return fakenfs.FakeNFSTransportDecorator('fakenfs+' + url)
586
587
    def test_local_parameters(self):
2701.1.1 by Martin Pool
Remove Transport.should_cache.
588
        # the listable and is_readonly parameters
1558.10.2 by Robert Collins
Refactor the FakeNFS support into a TransportDecorator.
589
        # are not changed by the fakenfs decorator
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
590
        t = self.get_nfs_transport('.')
591
        self.assertEqual(True, t.listable())
592
        self.assertEqual(False, t.is_readonly())
1558.10.2 by Robert Collins
Refactor the FakeNFS support into a TransportDecorator.
593
594
    def test_http_parameters(self):
2701.1.1 by Martin Pool
Remove Transport.should_cache.
595
        # the listable and is_readonly parameters
1558.10.2 by Robert Collins
Refactor the FakeNFS support into a TransportDecorator.
596
        # are not changed by the fakenfs decorator
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
597
        from breezy.tests.http_server import HttpServer
2929.3.7 by Vincent Ladeuil
Rename bzrlib/test/HttpServer.py to bzrlib/tests/http_server.py and fix uses.
598
        # connect to '.' via http which is not listable
1558.10.2 by Robert Collins
Refactor the FakeNFS support into a TransportDecorator.
599
        server = HttpServer()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
600
        self.start_server(server)
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
601
        t = self.get_nfs_transport(server.get_url())
602
        self.assertIsInstance(t, fakenfs.FakeNFSTransportDecorator)
603
        self.assertEqual(False, t.listable())
604
        self.assertEqual(True, t.is_readonly())
1558.10.2 by Robert Collins
Refactor the FakeNFS support into a TransportDecorator.
605
606
    def test_fakenfs_server_default(self):
607
        # a FakeNFSServer() should bring up a local relpath server for itself
5017.3.22 by Vincent Ladeuil
selftest -s bt.test_transport passing
608
        server = test_server.FakeNFSServer()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
609
        self.start_server(server)
610
        # the url should be decorated appropriately
611
        self.assertStartsWith(server.get_url(), 'fakenfs+')
612
        # and we should be able to get a transport for it
6039.1.6 by Jelmer Vernooij
Add more tests.
613
        t = transport.get_transport_from_url(server.get_url())
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
614
        # which must be a FakeNFSTransportDecorator instance.
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
615
        self.assertIsInstance(t, fakenfs.FakeNFSTransportDecorator)
1558.10.2 by Robert Collins
Refactor the FakeNFS support into a TransportDecorator.
616
617
    def test_fakenfs_rename_semantics(self):
618
        # a FakeNFS transport must mangle the way rename errors occur to
619
        # look like NFS problems.
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
620
        t = self.get_nfs_transport('.')
1558.10.2 by Robert Collins
Refactor the FakeNFS support into a TransportDecorator.
621
        self.build_tree(['from/', 'from/foo', 'to/', 'to/bar'],
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
622
                        transport=t)
623
        self.assertRaises(errors.ResourceBusy, t.rename, 'from', 'to')
624
625
626
class FakeVFATDecoratorTests(tests.TestCaseInTempDir):
1608.2.4 by Martin Pool
[broken] Add FakeFVATTransport
627
    """Tests for simulation of VFAT restrictions"""
628
629
    def get_vfat_transport(self, url):
630
        """Return vfat-backed transport for test directory"""
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
631
        from breezy.transport.fakevfat import FakeVFATTransportDecorator
1608.2.4 by Martin Pool
[broken] Add FakeFVATTransport
632
        return FakeVFATTransportDecorator('vfat+' + url)
633
634
    def test_transport_creation(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
635
        from breezy.transport.fakevfat import FakeVFATTransportDecorator
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
636
        t = self.get_vfat_transport('.')
637
        self.assertIsInstance(t, FakeVFATTransportDecorator)
1608.2.4 by Martin Pool
[broken] Add FakeFVATTransport
638
639
    def test_transport_mkdir(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
640
        t = self.get_vfat_transport('.')
641
        t.mkdir('HELLO')
642
        self.assertTrue(t.has('hello'))
643
        self.assertTrue(t.has('Hello'))
1608.2.4 by Martin Pool
[broken] Add FakeFVATTransport
644
1608.2.11 by Martin Pool
(FakeVFAT) add test for detection of invalid characters
645
    def test_forbidden_chars(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
646
        t = self.get_vfat_transport('.')
647
        self.assertRaises(ValueError, t.has, "<NU>")
648
649
650
class BadTransportHandler(transport.Transport):
1540.3.8 by Martin Pool
Some support for falling back between transport implementations.
651
    def __init__(self, base_url):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
652
        raise errors.DependencyNotPresent('some_lib',
653
                                          'testing missing dependency')
654
655
656
class BackupTransportHandler(transport.Transport):
1540.3.8 by Martin Pool
Some support for falling back between transport implementations.
657
    """Test transport that works as a backup for the BadTransportHandler"""
1540.3.10 by Martin Pool
[broken] keep hooking pycurl into test framework
658
    pass
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
659
660
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
661
class TestTransportImplementation(tests.TestCaseInTempDir):
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
662
    """Implementation verification for transports.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
663
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
664
    To verify a transport we need a server factory, which is a callable
665
    that accepts no parameters and returns an implementation of
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
666
    breezy.transport.Server.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
667
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
668
    That Server is then used to construct transport instances and test
669
    the transport via loopback activity.
670
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
671
    Currently this assumes that the Transport object is connected to the
672
    current working directory.  So that whatever is done
673
    through the transport, should show up in the working
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
674
    directory, and vice-versa. This is a bug, because its possible to have
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
675
    URL schemes which provide access to something that may not be
676
    result in storage on the local disk, i.e. due to file system limits, or
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
677
    due to it being a database or some other non-filesystem tool.
678
679
    This also tests to make sure that the functions work with both
680
    generators and lists (assuming iter(list) is effectively a generator)
681
    """
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
682
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
683
    def setUp(self):
684
        super(TestTransportImplementation, self).setUp()
685
        self._server = self.transport_server()
4659.1.2 by Robert Collins
Refactor creation and shutdown of test servers to use a common helper,
686
        self.start_server(self._server)
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
687
2520.3.1 by Vincent Ladeuil
Fix 110448 by adding a relpath parameter to get_transport.
688
    def get_transport(self, relpath=None):
689
        """Return a connected transport to the local directory.
690
691
        :param relpath: a path relative to the base url.
692
        """
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
693
        base_url = self._server.get_url()
2520.3.1 by Vincent Ladeuil
Fix 110448 by adding a relpath parameter to get_transport.
694
        url = self._adjust_url(base_url, relpath)
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
695
        # try getting the transport via the regular interface:
6039.1.5 by Jelmer Vernooij
Add get_transport_from_url and get_transport_from_path functions.
696
        t = transport.get_transport_from_url(url)
2485.8.39 by Vincent Ladeuil
Add tests around connection reuse.
697
        # vila--20070607 if the following are commented out the test suite
698
        # still pass. Is this really still needed or was it a forgotten
699
        # temporary fix ?
1986.2.5 by Robert Collins
Unbreak transport tests.
700
        if not isinstance(t, self.transport_class):
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
701
            # we did not get the correct transport class type. Override the
702
            # regular connection behaviour by direct construction.
2520.3.1 by Vincent Ladeuil
Fix 110448 by adding a relpath parameter to get_transport.
703
            t = self.transport_class(url)
1871.1.2 by Robert Collins
Reduce code duplication in transport-parameterised tests.
704
        return t
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
705
706
6039.1.6 by Jelmer Vernooij
Add more tests.
707
class TestTransportFromPath(tests.TestCaseInTempDir):
708
709
    def test_with_path(self):
710
        t = transport.get_transport_from_path(self.test_dir)
711
        self.assertIsInstance(t, local.LocalTransport)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
712
        self.assertEqual(t.base.rstrip("/"),
7143.15.2 by Jelmer Vernooij
Run autopep8.
713
                         urlutils.local_path_to_url(self.test_dir))
6039.1.6 by Jelmer Vernooij
Add more tests.
714
715
    def test_with_url(self):
716
        t = transport.get_transport_from_path("file:")
717
        self.assertIsInstance(t, local.LocalTransport)
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
718
        self.assertEqual(
719
            t.base.rstrip("/"),
720
            urlutils.local_path_to_url(os.path.join(self.test_dir, "file:")))
6039.1.6 by Jelmer Vernooij
Add more tests.
721
722
723
class TestTransportFromUrl(tests.TestCaseInTempDir):
724
725
    def test_with_path(self):
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
726
        self.assertRaises(urlutils.InvalidURL, transport.get_transport_from_url,
7143.15.2 by Jelmer Vernooij
Run autopep8.
727
                          self.test_dir)
6039.1.6 by Jelmer Vernooij
Add more tests.
728
729
    def test_with_url(self):
730
        url = urlutils.local_path_to_url(self.test_dir)
731
        t = transport.get_transport_from_url(url)
732
        self.assertIsInstance(t, local.LocalTransport)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
733
        self.assertEqual(t.base.rstrip("/"), url)
6039.1.6 by Jelmer Vernooij
Add more tests.
734
5268.7.25 by Jelmer Vernooij
Make sure trailing slash is present.
735
    def test_with_url_and_segment_parameters(self):
7143.15.2 by Jelmer Vernooij
Run autopep8.
736
        url = urlutils.local_path_to_url(self.test_dir) + ",branch=foo"
5268.7.25 by Jelmer Vernooij
Make sure trailing slash is present.
737
        t = transport.get_transport_from_url(url)
738
        self.assertIsInstance(t, local.LocalTransport)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
739
        self.assertEqual(t.base.rstrip("/"), url)
5268.7.25 by Jelmer Vernooij
Make sure trailing slash is present.
740
        with open(os.path.join(self.test_dir, "afile"), 'w') as f:
741
            f.write("data")
742
        self.assertTrue(t.has("afile"))
743
6039.1.6 by Jelmer Vernooij
Add more tests.
744
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
745
class TestLocalTransports(tests.TestCase):
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
746
747
    def test_get_transport_from_abspath(self):
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
748
        here = osutils.abspath('.')
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
749
        t = transport.get_transport(here)
750
        self.assertIsInstance(t, local.LocalTransport)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
751
        self.assertEqual(t.base, urlutils.local_path_to_url(here) + '/')
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
752
753
    def test_get_transport_from_relpath(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
754
        t = transport.get_transport('.')
755
        self.assertIsInstance(t, local.LocalTransport)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
756
        self.assertEqual(t.base, urlutils.local_path_to_url('.') + '/')
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
757
758
    def test_get_transport_from_local_url(self):
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
759
        here = osutils.abspath('.')
1951.2.1 by Martin Pool
Change to using LocalURLServer for testing.
760
        here_url = urlutils.local_path_to_url(here) + '/'
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
761
        t = transport.get_transport(here_url)
762
        self.assertIsInstance(t, local.LocalTransport)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
763
        self.assertEqual(t.base, here_url)
2245.6.1 by Alexander Belchenko
win32 UNC path: recursive cloning UNC path to root stops on //HOST, not on //
764
2018.18.4 by Martin Pool
Change Transport.local_abspath to raise NotLocalUrl, and test.
765
    def test_local_abspath(self):
2804.4.1 by Alexander Belchenko
some win32-specific fixes for selftest
766
        here = osutils.abspath('.')
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
767
        t = transport.get_transport(here)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
768
        self.assertEqual(t.local_abspath(''), here)
2018.18.4 by Martin Pool
Change Transport.local_abspath to raise NotLocalUrl, and test.
769
2245.6.1 by Alexander Belchenko
win32 UNC path: recursive cloning UNC path to root stops on //HOST, not on //
770
6015.50.1 by Martin Pool
Use a chmod wrapper to cope with eperm from chmod
771
class TestLocalTransportMutation(tests.TestCaseInTempDir):
772
773
    def test_local_transport_mkdir(self):
774
        here = osutils.abspath('.')
775
        t = transport.get_transport(here)
776
        t.mkdir('test')
777
        self.assertTrue(os.path.exists('test'))
778
779
    def test_local_transport_mkdir_permission_denied(self):
780
        # See https://bugs.launchpad.net/bzr/+bug/606537
781
        here = osutils.abspath('.')
782
        t = transport.get_transport(here)
7143.15.2 by Jelmer Vernooij
Run autopep8.
783
6015.50.1 by Martin Pool
Use a chmod wrapper to cope with eperm from chmod
784
        def fake_chmod(path, mode):
785
            e = OSError('permission denied')
786
            e.errno = errno.EPERM
787
            raise e
788
        self.overrideAttr(os, 'chmod', fake_chmod)
789
        t.mkdir('test')
6619.3.14 by Jelmer Vernooij
Convert some octal numbers to new notations.
790
        t.mkdir('test2', mode=0o707)
6015.50.1 by Martin Pool
Use a chmod wrapper to cope with eperm from chmod
791
        self.assertTrue(os.path.exists('test'))
792
        self.assertTrue(os.path.exists('test2'))
793
794
6006.4.3 by Martin Pool
Add FileStream.fdatasync
795
class TestLocalTransportWriteStream(tests.TestCaseWithTransport):
796
797
    def test_local_fdatasync_calls_fdatasync(self):
798
        """Check fdatasync on a stream tries to flush the data to the OS.
7143.15.2 by Jelmer Vernooij
Run autopep8.
799
6006.4.3 by Martin Pool
Add FileStream.fdatasync
800
        We can't easily observe the external effect but we can at least see
801
        it's called.
802
        """
6056.1.1 by Vincent Ladeuil
os.fdatasync is not defined on BSD-based OSes
803
        sentinel = object()
804
        fdatasync = getattr(os, 'fdatasync', sentinel)
805
        if fdatasync is sentinel:
806
            raise tests.TestNotApplicable('fdatasync not supported')
6006.4.3 by Martin Pool
Add FileStream.fdatasync
807
        t = self.get_transport('.')
808
        calls = self.recordCalls(os, 'fdatasync')
809
        w = t.open_write_stream('out')
6973.7.4 by Jelmer Vernooij
Fix some more tests.
810
        w.write(b'foo')
6006.4.3 by Martin Pool
Add FileStream.fdatasync
811
        w.fdatasync()
812
        with open('out', 'rb') as f:
813
            # Should have been flushed.
6973.7.4 by Jelmer Vernooij
Fix some more tests.
814
            self.assertEqual(f.read(), b'foo')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
815
        self.assertEqual(len(calls), 1, calls)
6006.4.3 by Martin Pool
Add FileStream.fdatasync
816
6110.3.1 by Jelmer Vernooij
Raise NoSuchFile rather than IOError from open_write_stream when the target
817
    def test_missing_directory(self):
818
        t = self.get_transport('.')
819
        self.assertRaises(errors.NoSuchFile, t.open_write_stream, 'dir/foo')
820
6006.4.3 by Martin Pool
Add FileStream.fdatasync
821
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
822
class TestWin32LocalTransport(tests.TestCase):
2245.6.1 by Alexander Belchenko
win32 UNC path: recursive cloning UNC path to root stops on //HOST, not on //
823
824
    def test_unc_clone_to_root(self):
6603.1.4 by Vincent Ladeuil
Skip the missed test in test_transport.
825
        self.requireFeature(features.win32_feature)
2245.6.1 by Alexander Belchenko
win32 UNC path: recursive cloning UNC path to root stops on //HOST, not on //
826
        # Win32 UNC path like \\HOST\path
827
        # clone to root should stop at least at \\HOST part
828
        # not on \\
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
829
        t = local.EmulatedWin32LocalTransport('file://HOST/path/to/some/dir/')
6651.2.2 by Martin
Apply 2to3 xrange fix and fix up with sixish range
830
        for i in range(4):
2245.6.1 by Alexander Belchenko
win32 UNC path: recursive cloning UNC path to root stops on //HOST, not on //
831
            t = t.clone('..')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
832
        self.assertEqual(t.base, 'file://HOST/')
2245.6.1 by Alexander Belchenko
win32 UNC path: recursive cloning UNC path to root stops on //HOST, not on //
833
        # make sure we reach the root
834
        t = t.clone('..')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
835
        self.assertEqual(t.base, 'file://HOST/')
2477.1.7 by Martin Pool
test_transport must provide get_test_permutations
836
2485.8.61 by Vincent Ladeuil
From review comments, use a private scheme for testing.
837
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
838
class TestConnectedTransport(tests.TestCase):
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
839
    """Tests for connected to remote server transports"""
840
841
    def test_parse_url(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
842
        t = transport.ConnectedTransport(
843
            'http://simple.example.com/home/source')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
844
        self.assertEqual(t._parsed_url.host, 'simple.example.com')
845
        self.assertEqual(t._parsed_url.port, None)
846
        self.assertEqual(t._parsed_url.path, '/home/source/')
6055.2.1 by Jelmer Vernooij
Add UnparsedUrl.
847
        self.assertTrue(t._parsed_url.user is None)
848
        self.assertTrue(t._parsed_url.password is None)
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
849
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
850
        self.assertEqual(t.base, 'http://simple.example.com/home/source/')
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
851
3498.2.1 by Neil Martinsen-Burrell
Fix bug 228058: user names with @ signs should work
852
    def test_parse_url_with_at_in_user(self):
853
        # Bug 228058
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
854
        t = transport.ConnectedTransport('ftp://user@host.com@www.host.com/')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
855
        self.assertEqual(t._parsed_url.user, 'user@host.com')
3498.2.1 by Neil Martinsen-Burrell
Fix bug 228058: user names with @ signs should work
856
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
857
    def test_parse_quoted_url(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
858
        t = transport.ConnectedTransport(
859
            'http://ro%62ey:h%40t@ex%41mple.com:2222/path')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
860
        self.assertEqual(t._parsed_url.host, 'exAmple.com')
861
        self.assertEqual(t._parsed_url.port, 2222)
862
        self.assertEqual(t._parsed_url.user, 'robey')
863
        self.assertEqual(t._parsed_url.password, 'h@t')
864
        self.assertEqual(t._parsed_url.path, '/path/')
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
865
866
        # Base should not keep track of the password
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
867
        self.assertEqual(t.base, 'http://ro%62ey@ex%41mple.com:2222/path/')
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
868
869
    def test_parse_invalid_url(self):
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
870
        self.assertRaises(urlutils.InvalidURL,
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
871
                          transport.ConnectedTransport,
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
872
                          'sftp://lily.org:~janneke/public/bzr/gub')
873
874
    def test_relpath(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
875
        t = transport.ConnectedTransport('sftp://user@host.com/abs/path')
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
876
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
877
        self.assertEqual(t.relpath('sftp://user@host.com/abs/path/sub'),
7143.15.2 by Jelmer Vernooij
Run autopep8.
878
                         'sub')
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
879
        self.assertRaises(errors.PathNotChild, t.relpath,
880
                          'http://user@host.com/abs/path/sub')
881
        self.assertRaises(errors.PathNotChild, t.relpath,
882
                          'sftp://user2@host.com/abs/path/sub')
883
        self.assertRaises(errors.PathNotChild, t.relpath,
884
                          'sftp://user@otherhost.com/abs/path/sub')
885
        self.assertRaises(errors.PathNotChild, t.relpath,
886
                          'sftp://user@host.com:33/abs/path/sub')
887
        # Make sure it works when we don't supply a username
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
888
        t = transport.ConnectedTransport('sftp://host.com/abs/path')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
889
        self.assertEqual(t.relpath('sftp://host.com/abs/path/sub'), 'sub')
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
890
891
        # Make sure it works when parts of the path will be url encoded
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
892
        t = transport.ConnectedTransport('sftp://host.com/dev/%path')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
893
        self.assertEqual(t.relpath('sftp://host.com/dev/%path/sub'), 'sub')
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
894
2485.8.32 by Vincent Ladeuil
Keep credentials used at connection creation for reconnection purposes.
895
    def test_connection_sharing_propagate_credentials(self):
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
896
        t = transport.ConnectedTransport('ftp://user@host.com/abs/path')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
897
        self.assertEqual('user', t._parsed_url.user)
898
        self.assertEqual('host.com', t._parsed_url.host)
2485.8.32 by Vincent Ladeuil
Keep credentials used at connection creation for reconnection purposes.
899
        self.assertIs(None, t._get_connection())
6055.2.1 by Jelmer Vernooij
Add UnparsedUrl.
900
        self.assertIs(None, t._parsed_url.password)
2485.8.32 by Vincent Ladeuil
Keep credentials used at connection creation for reconnection purposes.
901
        c = t.clone('subdir')
2900.2.16 by Vincent Ladeuil
Make hhtp proxy aware of AuthenticationConfig (for password).
902
        self.assertIs(None, c._get_connection())
6055.2.1 by Jelmer Vernooij
Add UnparsedUrl.
903
        self.assertIs(None, t._parsed_url.password)
2485.8.32 by Vincent Ladeuil
Keep credentials used at connection creation for reconnection purposes.
904
905
        # Simulate the user entering a password
906
        password = 'secret'
907
        connection = object()
908
        t._set_connection(connection, password)
909
        self.assertIs(connection, t._get_connection())
910
        self.assertIs(password, t._get_credentials())
911
        self.assertIs(connection, c._get_connection())
912
        self.assertIs(password, c._get_credentials())
2485.8.30 by Vincent Ladeuil
Implement reliable connection sharing.
913
2485.8.39 by Vincent Ladeuil
Add tests around connection reuse.
914
        # credentials can be updated
915
        new_password = 'even more secret'
916
        c._update_credentials(new_password)
917
        self.assertIs(connection, t._get_connection())
918
        self.assertIs(new_password, t._get_credentials())
919
        self.assertIs(connection, c._get_connection())
920
        self.assertIs(new_password, c._get_credentials())
921
2477.1.7 by Martin Pool
test_transport must provide get_test_permutations
922
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
923
class TestReusedTransports(tests.TestCase):
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
924
    """Tests for transport reuse"""
2476.3.5 by Vincent Ladeuil
Naive implementation of transport reuse by Transport.get_transport().
925
926
    def test_reuse_same_transport(self):
1551.18.10 by Aaron Bentley
get_transport appends to possible_transports if it's an empty list
927
        possible_transports = []
7143.15.5 by Jelmer Vernooij
More PEP8 fixes.
928
        t1 = transport.get_transport_from_url(
929
            'http://foo/', possible_transports=possible_transports)
1551.18.10 by Aaron Bentley
get_transport appends to possible_transports if it's an empty list
930
        self.assertEqual([t1], possible_transports)
6039.1.6 by Jelmer Vernooij
Add more tests.
931
        t2 = transport.get_transport_from_url('http://foo/',
7143.15.2 by Jelmer Vernooij
Run autopep8.
932
                                              possible_transports=[t1])
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
933
        self.assertIs(t1, t2)
934
935
        # Also check that final '/' are handled correctly
6039.1.6 by Jelmer Vernooij
Add more tests.
936
        t3 = transport.get_transport_from_url('http://foo/path/')
937
        t4 = transport.get_transport_from_url('http://foo/path',
7143.15.2 by Jelmer Vernooij
Run autopep8.
938
                                              possible_transports=[t3])
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
939
        self.assertIs(t3, t4)
940
6039.1.6 by Jelmer Vernooij
Add more tests.
941
        t5 = transport.get_transport_from_url('http://foo/path')
942
        t6 = transport.get_transport_from_url('http://foo/path/',
7143.15.2 by Jelmer Vernooij
Run autopep8.
943
                                              possible_transports=[t5])
2485.8.39 by Vincent Ladeuil
Add tests around connection reuse.
944
        self.assertIs(t5, t6)
2476.3.5 by Vincent Ladeuil
Naive implementation of transport reuse by Transport.get_transport().
945
946
    def test_don_t_reuse_different_transport(self):
6039.1.6 by Jelmer Vernooij
Add more tests.
947
        t1 = transport.get_transport_from_url('http://foo/path')
948
        t2 = transport.get_transport_from_url('http://bar/path',
7143.15.2 by Jelmer Vernooij
Run autopep8.
949
                                              possible_transports=[t1])
2485.8.40 by Vincent Ladeuil
Fix typo.
950
        self.assertIsNot(t1, t2)
2476.3.13 by Vincent Ladeuil
merge bzr.dev@2495
951
952
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
953
class TestTransportTrace(tests.TestCase):
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
954
6039.1.5 by Jelmer Vernooij
Add get_transport_from_url and get_transport_from_path functions.
955
    def test_decorator(self):
6039.1.6 by Jelmer Vernooij
Add more tests.
956
        t = transport.get_transport_from_url('trace+memory://')
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
957
        self.assertIsInstance(
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
958
            t, breezy.transport.trace.TransportTraceDecorator)
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
959
960
    def test_clone_preserves_activity(self):
6039.1.6 by Jelmer Vernooij
Add more tests.
961
        t = transport.get_transport_from_url('trace+memory://')
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
962
        t2 = t.clone('.')
963
        self.assertTrue(t is not t2)
964
        self.assertTrue(t._activity is t2._activity)
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
965
966
    # the following specific tests are for the operations that have made use of
967
    # logging in tests; we could test every single operation but doing that
968
    # still won't cause a test failure when the top level Transport API
969
    # changes; so there is little return doing that.
970
    def test_get(self):
6039.1.6 by Jelmer Vernooij
Add more tests.
971
        t = transport.get_transport_from_url('trace+memory:///')
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
972
        t.put_bytes('foo', b'barish')
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
973
        t.get('foo')
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
974
        expected_result = []
975
        # put_bytes records the bytes, not the content to avoid memory
976
        # pressure.
977
        expected_result.append(('put_bytes', 'foo', 6, None))
978
        # get records the file name only.
979
        expected_result.append(('get', 'foo'))
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
980
        self.assertEqual(expected_result, t._activity)
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
981
982
    def test_readv(self):
6039.1.6 by Jelmer Vernooij
Add more tests.
983
        t = transport.get_transport_from_url('trace+memory:///')
6973.5.10 by Jelmer Vernooij
Random bunch of python3 bee-improvements.
984
        t.put_bytes('foo', b'barish')
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
985
        list(t.readv('foo', [(0, 1), (3, 2)],
986
                     adjust_for_latency=True, upper_limit=6))
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
987
        expected_result = []
988
        # put_bytes records the bytes, not the content to avoid memory
989
        # pressure.
990
        expected_result.append(('put_bytes', 'foo', 6, None))
991
        # readv records the supplied offset request
992
        expected_result.append(('readv', 'foo', [(0, 1), (3, 2)], True, 6))
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
993
        self.assertEqual(expected_result, t._activity)
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
994
995
996
class TestSSHConnections(tests.TestCaseWithTransport):
997
998
    def test_bzr_connect_to_bzr_ssh(self):
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
999
        """get_transport of a bzr+ssh:// behaves correctly.
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1000
1001
        bzr+ssh:// should cause bzr to run a remote bzr smart server over SSH.
1002
        """
1003
        # This test actually causes a bzr instance to be invoked, which is very
1004
        # expensive: it should be the only such test in the test suite.
1005
        # A reasonable evolution for this would be to simply check inside
1006
        # check_channel_exec_request that the command is appropriate, and then
1007
        # satisfy requests in-process.
4913.2.16 by John Arbash Meinel
Move bzrlib.tests.ParamikoFeature to bzrlib.tests.features.paramiko
1008
        self.requireFeature(features.paramiko)
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1009
        # SFTPFullAbsoluteServer has a get_url method, and doesn't
1010
        # override the interface (doesn't change self._vendor).
1011
        # Note that this does encryption, so can be slow.
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
1012
        from breezy.tests import stub_sftp
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1013
1014
        # Start an SSH server
1015
        self.command_executed = []
1016
        # XXX: This is horrible -- we define a really dumb SSH server that
1017
        # executes commands, and manage the hooking up of stdin/out/err to the
1018
        # SSH channel ourselves.  Surely this has already been implemented
1019
        # elsewhere?
4857.2.2 by John Arbash Meinel
Change the connect-to-bzr test so that it cleans itself up.
1020
        started = []
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
1021
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
1022
        class StubSSHServer(stub_sftp.StubServer):
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1023
1024
            test = self
1025
1026
            def check_channel_exec_request(self, channel, command):
1027
                self.test.command_executed.append(command)
1028
                proc = subprocess.Popen(
1029
                    command, shell=True, stdin=subprocess.PIPE,
6973.7.4 by Jelmer Vernooij
Fix some more tests.
1030
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
1031
                    bufsize=0)
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1032
1033
                # XXX: horribly inefficient, not to mention ugly.
6006.2.2 by Martin Pool
pep8 cleanup of test_transport
1034
                # Start a thread for each of stdin/out/err, and relay bytes
1035
                # from the subprocess to channel and vice versa.
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1036
                def ferry_bytes(read, write, close):
1037
                    while True:
1038
                        bytes = read(1)
6973.7.4 by Jelmer Vernooij
Fix some more tests.
1039
                        if bytes == b'':
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1040
                            close()
1041
                            break
1042
                        write(bytes)
1043
1044
                file_functions = [
1045
                    (channel.recv, proc.stdin.write, proc.stdin.close),
1046
                    (proc.stdout.read, channel.sendall, channel.close),
1047
                    (proc.stderr.read, channel.sendall_stderr, channel.close)]
4857.2.2 by John Arbash Meinel
Change the connect-to-bzr test so that it cleans itself up.
1048
                started.append(proc)
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1049
                for read, write, close in file_functions:
1050
                    t = threading.Thread(
1051
                        target=ferry_bytes, args=(read, write, close))
1052
                    t.start()
4857.2.2 by John Arbash Meinel
Change the connect-to-bzr test so that it cleans itself up.
1053
                    started.append(t)
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1054
1055
                return True
1056
4797.11.2 by Vincent Ladeuil
Stop requiring testtools for sftp use.
1057
        ssh_server = stub_sftp.SFTPFullAbsoluteServer(StubSSHServer)
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1058
        # We *don't* want to override the default SSH vendor: the detected one
1059
        # is the one to use.
5247.4.20 by Vincent Ladeuil
Ugly fix for the last test failure.
1060
1061
        # FIXME: I don't understand the above comment, SFTPFullAbsoluteServer
1062
        # inherits from SFTPServer which forces the SSH vendor to
1063
        # ssh.ParamikoVendor(). So it's forced, not detected. --vila 20100623
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1064
        self.start_server(ssh_server)
5247.4.20 by Vincent Ladeuil
Ugly fix for the last test failure.
1065
        port = ssh_server.port
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1066
1067
        if sys.platform == 'win32':
6622.1.1 by Jelmer Vernooij
Rename bzrlib => brzlib, bzr => brz.
1068
            bzr_remote_path = sys.executable + ' ' + self.get_brz_path()
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1069
        else:
6622.1.1 by Jelmer Vernooij
Rename bzrlib => brzlib, bzr => brz.
1070
            bzr_remote_path = self.get_brz_path()
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
1071
        self.overrideEnv('BZR_REMOTE_PATH', bzr_remote_path)
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1072
6622.1.33 by Jelmer Vernooij
Fix more tests (all?)
1073
        # Access the branch via a bzr+ssh URL.  The BZR_REMOTE_PATH environment
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1074
        # variable is used to tell bzr what command to run on the remote end.
1075
        path_to_branch = osutils.abspath('.')
1076
        if sys.platform == 'win32':
4700.1.2 by Robert Collins
Review feedback.
1077
            # On Windows, we export all drives as '/C:/, etc. So we need to
1078
            # prefix a '/' to get the right path.
1079
            path_to_branch = '/' + path_to_branch
1080
        url = 'bzr+ssh://fred:secret@localhost:%d%s' % (port, path_to_branch)
5010.2.10 by Vincent Ladeuil
Fix test_transport.py imports.
1081
        t = transport.get_transport(url)
4700.1.2 by Robert Collins
Review feedback.
1082
        self.permit_url(t.base)
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1083
        t.mkdir('foo')
1084
1085
        self.assertEqual(
7143.15.2 by Jelmer Vernooij
Run autopep8.
1086
            [b'%s serve --inet --directory=/ --allow-writes' %
1087
                bzr_remote_path.encode()],
4700.1.1 by Robert Collins
Focus and move out of blackbox the acceptance test for bzr+ssh connection handshake/process spawning.
1088
            self.command_executed)
4857.2.2 by John Arbash Meinel
Change the connect-to-bzr test so that it cleans itself up.
1089
        # Make sure to disconnect, so that the remote process can stop, and we
1090
        # can cleanup. Then pause the test until everything is shutdown
1091
        t._client._medium.disconnect()
1092
        if not started:
1093
            return
1094
        # First wait for the subprocess
1095
        started[0].wait()
1096
        # And the rest are threads
1097
        for t in started[1:]:
1098
            t.join()
4912.2.4 by Martin Pool
Add test for unhtml_roughly, and truncate at 1000 bytes
1099
1100
1101
class TestUnhtml(tests.TestCase):
1102
1103
    """Tests for unhtml_roughly"""
1104
1105
    def test_truncation(self):
1106
        fake_html = "<p>something!\n" * 1000
1107
        result = http.unhtml_roughly(fake_html)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1108
        self.assertEqual(len(result), 1000)
4912.2.4 by Martin Pool
Add test for unhtml_roughly, and truncate at 1000 bytes
1109
        self.assertStartsWith(result, " something!")
6030.2.2 by Jelmer Vernooij
Add test.
1110
1111
1112
class SomeDirectory(object):
1113
1114
    def look_up(self, name, url):
1115
        return "http://bar"
1116
1117
1118
class TestLocationToUrl(tests.TestCase):
1119
6060.1.2 by John Arbash Meinel
Fix location_to_url tests that needed to watch out for drive letters.
1120
    def get_base_location(self):
1121
        path = osutils.abspath('/foo/bar')
1122
        if path.startswith('/'):
1123
            url = 'file://%s' % (path,)
1124
        else:
1125
            # On Windows, abspaths start with the drive letter, so we have to
1126
            # add in the extra '/'
1127
            url = 'file:///%s' % (path,)
1128
        return path, url
1129
6030.2.2 by Jelmer Vernooij
Add test.
1130
    def test_regular_url(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1131
        self.assertEqual("file://foo", location_to_url("file://foo"))
6030.2.2 by Jelmer Vernooij
Add test.
1132
1133
    def test_directory(self):
1134
        directories.register("bar:", SomeDirectory, "Dummy directory")
1135
        self.addCleanup(directories.remove, "bar:")
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1136
        self.assertEqual("http://bar", location_to_url("bar:"))
6030.2.3 by Jelmer Vernooij
Move encoding of path to URL.
1137
1138
    def test_unicode_url(self):
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
1139
        self.assertRaises(urlutils.InvalidURL, location_to_url,
7143.15.2 by Jelmer Vernooij
Run autopep8.
1140
                          b"http://fo/\xc3\xaf".decode("utf-8"))
6030.2.3 by Jelmer Vernooij
Move encoding of path to URL.
1141
1142
    def test_unicode_path(self):
6060.1.2 by John Arbash Meinel
Fix location_to_url tests that needed to watch out for drive letters.
1143
        path, url = self.get_base_location()
7045.1.13 by Jelmer Vernooij
Fix a few more tests.
1144
        location = path + b"\xc3\xaf".decode("utf-8")
6060.1.2 by John Arbash Meinel
Fix location_to_url tests that needed to watch out for drive letters.
1145
        url += '%C3%AF'
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1146
        self.assertEqual(url, location_to_url(location))
6030.2.4 by Jelmer Vernooij
Move local file interpretation to location_to_url.
1147
1148
    def test_path(self):
6060.1.2 by John Arbash Meinel
Fix location_to_url tests that needed to watch out for drive letters.
1149
        path, url = self.get_base_location()
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1150
        self.assertEqual(url, location_to_url(path))
6037.1.1 by Jelmer Vernooij
Allow file: for use with relative local paths.
1151
1152
    def test_relative_file_url(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1153
        self.assertEqual(urlutils.local_path_to_url(".") + "/bar",
7143.15.2 by Jelmer Vernooij
Run autopep8.
1154
                         location_to_url("file:bar"))
6037.1.2 by Jelmer Vernooij
Add another test.
1155
1156
    def test_absolute_file_url(self):
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1157
        self.assertEqual("file:///bar", location_to_url("file:/bar"))