~bzr-pqm/bzr/bzr.dev

1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
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
#
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
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
#
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
import os
18
import socket
19
import threading
20
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
21
import bzrlib.bzrdir as bzrdir
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
22
import bzrlib.errors as errors
1530.1.7 by Robert Collins
merge integration.
23
from bzrlib.osutils import pathjoin, lexists
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
24
from bzrlib.tests import TestCaseWithTransport, TestCase, TestSkipped
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
25
import bzrlib.transport
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
26
from bzrlib.workingtree import WorkingTree
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
27
28
try:
29
    import paramiko
30
    paramiko_loaded = True
31
except ImportError:
32
    paramiko_loaded = False
33
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
34
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
35
class TestCaseWithSFTPServer(TestCaseWithTransport):
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
36
    """A test case base class that provides a sftp server on localhost."""
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
37
38
    def setUp(self):
1532 by Robert Collins
Merge in John Meinels integration branch.
39
        if not paramiko_loaded:
40
            raise TestSkipped('you must have paramiko to run this test')
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
41
        super(TestCaseWithSFTPServer, self).setUp()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
42
        from bzrlib.transport.sftp import SFTPAbsoluteServer, SFTPHomeDirServer
43
        if getattr(self, '_get_remote_is_absolute', None) is None:
44
            self._get_remote_is_absolute = True
45
        if self._get_remote_is_absolute:
46
            self.transport_server = SFTPAbsoluteServer
47
        else:
48
            self.transport_server = SFTPHomeDirServer
49
        self.transport_readonly_server = bzrlib.transport.http.HttpServer
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
50
51
    def get_transport(self, path=None):
52
        """Return a transport relative to self._test_root."""
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
53
        return bzrlib.transport.get_transport(self.get_url(path))
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
54
55
56
class SFTPLockTests (TestCaseWithSFTPServer):
1185.16.127 by Martin Pool
[patch] paramiko sftp tests (robey)
57
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
58
    def test_sftp_locks(self):
59
        from bzrlib.errors import LockError
60
        t = self.get_transport()
61
62
        l = t.lock_write('bogus')
63
        self.failUnlessExists('bogus.write-lock')
64
65
        # Don't wait for the lock, locking an already locked
66
        # file should raise an assert
67
        self.assertRaises(LockError, t.lock_write, 'bogus')
68
69
        l.unlock()
1185.31.33 by John Arbash Meinel
A couple more path.join statements needed changing.
70
        self.failIf(lexists('bogus.write-lock'))
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
71
72
        open('something.write-lock', 'wb').write('fake lock\n')
73
        self.assertRaises(LockError, t.lock_write, 'something')
74
        os.remove('something.write-lock')
75
76
        l = t.lock_write('something')
77
78
        l2 = t.lock_write('bogus')
79
80
        l.unlock()
81
        l2.unlock()
82
1185.49.10 by John Arbash Meinel
Use a weakref dictionary to enable re-use of a connection (for sftp).
83
    def test_multiple_connections(self):
84
        t = self.get_transport()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
85
        self.assertTrue('sftpserver - new connection' in self.get_server().logs)
86
        self.get_server().logs = []
1185.49.10 by John Arbash Meinel
Use a weakref dictionary to enable re-use of a connection (for sftp).
87
        # The second request should reuse the first connection
88
        # SingleListener only allows for a single connection,
89
        # So the next line fails unless the connection is reused
90
        t2 = self.get_transport()
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
91
        self.assertEquals(self.get_server().logs, [])
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
92
93
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
94
class SFTPTransportTestRelative(TestCaseWithSFTPServer):
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
95
    """Test the SFTP transport with homedir based relative paths."""
96
97
    def test__remote_path(self):
98
        t = self.get_transport()
99
        # try what is currently used:
100
        # remote path = self._abspath(relpath)
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
101
        self.assertEqual(self.test_dir + '/relative', t._remote_path('relative'))
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
102
        # we dont os.path.join because windows gives us the wrong path
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
103
        root_segments = self.test_dir.split('/')
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
104
        root_parent = '/'.join(root_segments[:-1])
105
        # .. should be honoured
106
        self.assertEqual(root_parent + '/sibling', t._remote_path('../sibling'))
107
        # /  should be illegal ?
108
        ### FIXME decide and then test for all transports. RBC20051208
109
1530.1.6 by Robert Collins
Trim duplicate sftp tests.
110
111
class SFTPTransportTestRelative(TestCaseWithSFTPServer):
112
    """Test the SFTP transport with homedir based relative paths."""
113
114
    def setUp(self):
115
        self._get_remote_is_absolute = False
116
        super(SFTPTransportTestRelative, self).setUp()
117
1524.1.1 by Robert Collins
Test sftp with relative, absolute-in-homedir and absolute-not-in-homedir
118
    def test__remote_path_relative_root(self):
119
        # relative paths are preserved
120
        t = self.get_transport('')
121
        self.assertEqual('a', t._remote_path('a'))
122
123
1185.40.4 by Robey Pointer
fix sftp urls to support the ietf draft url spec wrt relative vs absolute sftp urls (this will break existing branch urls); fix username/password parsing in sftp urls; add unit tests to make sure sftp url parsing is working
124
class FakeSFTPTransport (object):
125
    _sftp = object()
126
fake = FakeSFTPTransport()
127
128
1185.49.14 by John Arbash Meinel
[merge] bzr.dev
129
class SFTPNonServerTest(TestCase):
1185.58.12 by John Arbash Meinel
Changing so that sftp tests are skipped rather than hidden when paramiko isn't present
130
    def setUp(self):
131
        TestCase.setUp(self)
132
        if not paramiko_loaded:
133
            raise TestSkipped('you must have paramiko to run this test')
134
1185.40.4 by Robey Pointer
fix sftp urls to support the ietf draft url spec wrt relative vs absolute sftp urls (this will break existing branch urls); fix username/password parsing in sftp urls; add unit tests to make sure sftp url parsing is working
135
    def test_parse_url(self):
136
        from bzrlib.transport.sftp import SFTPTransport
1534.1.8 by Robert Collins
Update SFTP Urls as per mailing list thread.
137
        s = SFTPTransport('sftp://simple.example.com/home/source', clone_from=fake)
1185.40.4 by Robey Pointer
fix sftp urls to support the ietf draft url spec wrt relative vs absolute sftp urls (this will break existing branch urls); fix username/password parsing in sftp urls; add unit tests to make sure sftp url parsing is working
138
        self.assertEquals(s._host, 'simple.example.com')
1185.49.23 by John Arbash Meinel
bugreport from Matthieu Moy: relpath was failing, but throwing an unhelpful exception.
139
        self.assertEquals(s._port, None)
1185.48.5 by James Henstridge
Change SFTP url parsing back to treat the path in sftp://host/path as
140
        self.assertEquals(s._path, '/home/source')
1185.49.19 by John Arbash Meinel
Testing that sftp.base gets properly sanitized
141
        self.failUnless(s._password is None)
142
1534.1.8 by Robert Collins
Update SFTP Urls as per mailing list thread.
143
        self.assertEquals(s.base, 'sftp://simple.example.com/home/source/')
144
145
        s = SFTPTransport('sftp://ro%62ey:h%40t@example.com:2222/~/relative', clone_from=fake)
1185.40.4 by Robey Pointer
fix sftp urls to support the ietf draft url spec wrt relative vs absolute sftp urls (this will break existing branch urls); fix username/password parsing in sftp urls; add unit tests to make sure sftp url parsing is working
146
        self.assertEquals(s._host, 'example.com')
147
        self.assertEquals(s._port, 2222)
148
        self.assertEquals(s._username, 'robey')
149
        self.assertEquals(s._password, 'h@t')
1185.48.5 by James Henstridge
Change SFTP url parsing back to treat the path in sftp://host/path as
150
        self.assertEquals(s._path, 'relative')
1185.33.58 by Martin Pool
[patch] Better error when sftp urls are given with invalid port numbers (Matthieu Moy)
151
1185.49.19 by John Arbash Meinel
Testing that sftp.base gets properly sanitized
152
        # Base should not keep track of the password
1534.1.8 by Robert Collins
Update SFTP Urls as per mailing list thread.
153
        self.assertEquals(s.base, 'sftp://robey@example.com:2222/~/relative/')
1185.49.23 by John Arbash Meinel
bugreport from Matthieu Moy: relpath was failing, but throwing an unhelpful exception.
154
155
    def test_relpath(self):
156
        from bzrlib.transport.sftp import SFTPTransport
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
157
        from bzrlib.errors import PathNotChild
1185.49.23 by John Arbash Meinel
bugreport from Matthieu Moy: relpath was failing, but throwing an unhelpful exception.
158
1534.1.8 by Robert Collins
Update SFTP Urls as per mailing list thread.
159
        s = SFTPTransport('sftp://user@host.com/abs/path', clone_from=fake)
160
        self.assertEquals(s.relpath('sftp://user@host.com/abs/path/sub'), 'sub')
1185.49.23 by John Arbash Meinel
bugreport from Matthieu Moy: relpath was failing, but throwing an unhelpful exception.
161
        # Can't test this one, because we actually get an AssertionError
162
        # TODO: Consider raising an exception rather than an assert
1534.1.8 by Robert Collins
Update SFTP Urls as per mailing list thread.
163
        #self.assertRaises(PathNotChild, s.relpath, 'http://user@host.com/abs/path/sub')
164
        self.assertRaises(PathNotChild, s.relpath, 'sftp://user2@host.com/abs/path/sub')
165
        self.assertRaises(PathNotChild, s.relpath, 'sftp://user@otherhost.com/abs/path/sub')
166
        self.assertRaises(PathNotChild, s.relpath, 'sftp://user@host.com:33/abs/path/sub')
167
        self.assertRaises(PathNotChild, s.relpath, 'sftp://user@host.com/~/rel/path/sub')
1185.49.19 by John Arbash Meinel
Testing that sftp.base gets properly sanitized
168
1185.49.25 by John Arbash Meinel
Added a couple more test cases, just in case.
169
        # Make sure it works when we don't supply a username
1534.1.8 by Robert Collins
Update SFTP Urls as per mailing list thread.
170
        s = SFTPTransport('sftp://host.com/abs/path', clone_from=fake)
171
        self.assertEquals(s.relpath('sftp://host.com/abs/path/sub'), 'sub')
1185.49.25 by John Arbash Meinel
Added a couple more test cases, just in case.
172
173
        # Make sure it works when parts of the path will be url encoded
174
        # TODO: These may be incorrect, we might need to urllib.urlencode() before
175
        # we pass the paths into the SFTPTransport constructor
176
        s = SFTPTransport('sftp://host.com/dev/,path', clone_from=fake)
177
        self.assertEquals(s.relpath('sftp://host.com/dev/,path/sub'), 'sub')
178
        s = SFTPTransport('sftp://host.com/dev/%path', clone_from=fake)
179
        self.assertEquals(s.relpath('sftp://host.com/dev/%path/sub'), 'sub')
180
1185.33.58 by Martin Pool
[patch] Better error when sftp urls are given with invalid port numbers (Matthieu Moy)
181
    def test_parse_invalid_url(self):
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
182
        from bzrlib.transport.sftp import SFTPTransport, TransportError
1185.33.58 by Martin Pool
[patch] Better error when sftp urls are given with invalid port numbers (Matthieu Moy)
183
        try:
184
            s = SFTPTransport('sftp://lilypond.org:~janneke/public_html/bzr/gub',
185
                              clone_from=fake)
186
            self.fail('expected exception not raised')
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
187
        except TransportError, e:
1185.33.58 by Martin Pool
[patch] Better error when sftp urls are given with invalid port numbers (Matthieu Moy)
188
            self.assertEquals(str(e), 
1824.2.1 by Johan Rydberg
Let TransportError inherit BzrNerError.
189
                    'Transport error: ~janneke: invalid port number ')
1185.33.58 by Martin Pool
[patch] Better error when sftp urls are given with invalid port numbers (Matthieu Moy)
190
1185.40.4 by Robey Pointer
fix sftp urls to support the ietf draft url spec wrt relative vs absolute sftp urls (this will break existing branch urls); fix username/password parsing in sftp urls; add unit tests to make sure sftp url parsing is working
191
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
192
class SFTPBranchTest(TestCaseWithSFTPServer):
193
    """Test some stuff when accessing a bzr Branch over sftp"""
194
195
    def test_lock_file(self):
1666.1.4 by Robert Collins
* 'Metadir' is now the default disk format. This improves behaviour in
196
        # old format branches use a special lock file on sftp.
197
        b = self.make_branch('', format=bzrdir.BzrDirFormat6())
198
        b = bzrlib.branch.Branch.open(self.get_url())
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
199
        self.failUnlessExists('.bzr/')
200
        self.failUnlessExists('.bzr/branch-format')
201
        self.failUnlessExists('.bzr/branch-lock')
202
1185.31.33 by John Arbash Meinel
A couple more path.join statements needed changing.
203
        self.failIf(lexists('.bzr/branch-lock.write-lock'))
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
204
        b.lock_write()
205
        self.failUnlessExists('.bzr/branch-lock.write-lock')
206
        b.unlock()
1185.31.33 by John Arbash Meinel
A couple more path.join statements needed changing.
207
        self.failIf(lexists('.bzr/branch-lock.write-lock'))
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
208
1185.49.26 by John Arbash Meinel
Adding tests for remote sftp branches without working trees, plus a bugfix to allow push to still work with a warning.
209
    def test_push_support(self):
210
        self.build_tree(['a/', 'a/foo'])
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
211
        t = bzrdir.BzrDir.create_standalone_workingtree('a')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
212
        b = t.branch
1185.49.26 by John Arbash Meinel
Adding tests for remote sftp branches without working trees, plus a bugfix to allow push to still work with a warning.
213
        t.add('foo')
214
        t.commit('foo', rev_id='a1')
215
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
216
        b2 = bzrdir.BzrDir.create_branch_and_repo(self.get_url('/b'))
1185.49.26 by John Arbash Meinel
Adding tests for remote sftp branches without working trees, plus a bugfix to allow push to still work with a warning.
217
        b2.pull(b)
218
219
        self.assertEquals(b2.revision_history(), ['a1'])
220
1185.31.48 by John Arbash Meinel
Added a small test to sftp to make sure some replacing was going on in the remote side.
221
        open('a/foo', 'wt').write('something new in foo\n')
222
        t.commit('new', rev_id='a2')
223
        b2.pull(b)
224
225
        self.assertEquals(b2.revision_history(), ['a1', 'a2'])
226
1185.49.3 by John Arbash Meinel
Added a form of locking to sftp branches. Refactored _sftp_open_exclusive to take a relative path
227
1547.1.4 by Robey Pointer
add a single full-handshake test to verify that sftp-over-ssh works
228
class SFTPFullHandshakingTest(TestCaseWithSFTPServer):
229
    """Verify that a full-handshake (SSH over loopback TCP) sftp connection works."""
230
    
231
    def test_connection(self):
1534.4.50 by Robert Collins
Got the bzrdir api straightened out, plenty of refactoring to use it pending, but the api is up and running.
232
        from bzrlib.transport.sftp import SFTPFullAbsoluteServer
233
        self.transport_server = SFTPFullAbsoluteServer
1547.1.4 by Robey Pointer
add a single full-handshake test to verify that sftp-over-ssh works
234
        self.get_transport()