21
from bzrlib.branch import Branch
21
import bzrlib.bzrdir as bzrdir
22
22
import bzrlib.errors as errors
23
23
from bzrlib.osutils import pathjoin, lexists
24
from bzrlib.tests import TestCaseInTempDir, TestCase, TestSkipped
24
from bzrlib.tests import TestCaseWithTransport, TestCase, TestSkipped
25
25
import bzrlib.transport
26
26
from bzrlib.workingtree import WorkingTree
32
32
paramiko_loaded = False
35
class TestCaseWithSFTPServer(TestCaseInTempDir):
35
class TestCaseWithSFTPServer(TestCaseWithTransport):
36
36
"""A test case base class that provides a sftp server on localhost."""
39
39
if not paramiko_loaded:
40
40
raise TestSkipped('you must have paramiko to run this test')
41
41
super(TestCaseWithSFTPServer, self).setUp()
42
from bzrlib.transport.sftp import SFTPAbsoluteServer, SFTPHomeDirServer, SFTPServer
43
if getattr(self, '_full_handshake', False):
44
self.server = SFTPServer()
46
self._full_handshake = False
47
if getattr(self, '_get_remote_is_absolute', None) is None:
48
self._get_remote_is_absolute = True
49
if self._get_remote_is_absolute:
50
self.server = SFTPAbsoluteServer()
52
self.server = SFTPHomeDirServer()
54
self.addCleanup(self.server.tearDown)
55
if self._full_handshake:
56
self._sftp_url = self.server._get_sftp_url("")
58
self._sftp_url = self.server.get_url()
59
self._root = self.test_dir
60
# Set to a string in setUp to give sftp server a new homedir.
61
self._override_home = None
62
self._is_setup = False
65
def get_remote_url(self, relpath_to_test_root):
67
return self._sftp_url + '/' + relpath_to_test_root
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
48
self.transport_server = SFTPHomeDirServer
49
self.transport_readonly_server = bzrlib.transport.http.HttpServer
69
51
def get_transport(self, path=None):
70
52
"""Return a transport relative to self._test_root."""
71
from bzrlib.transport import get_transport
72
transport = get_transport(self._sftp_url)
76
return transport.clone(path)
53
return bzrlib.transport.get_transport(self.get_url(path))
79
56
class SFTPLockTests (TestCaseWithSFTPServer):
106
83
def test_multiple_connections(self):
107
84
t = self.get_transport()
108
self.assertTrue('sftpserver - new connection' in self.server.logs)
109
self.server.logs = []
85
self.assertTrue('sftpserver - new connection' in self.get_server().logs)
86
self.get_server().logs = []
110
87
# The second request should reuse the first connection
111
88
# SingleListener only allows for a single connection,
112
89
# So the next line fails unless the connection is reused
113
90
t2 = self.get_transport()
114
self.assertEquals(self.server.logs, [])
91
self.assertEquals(self.get_server().logs, [])
117
94
class SFTPTransportTestRelative(TestCaseWithSFTPServer):
121
98
t = self.get_transport()
122
99
# try what is currently used:
123
100
# remote path = self._abspath(relpath)
124
self.assertEqual(self._root + '/relative', t._remote_path('relative'))
101
self.assertEqual(self.test_dir + '/relative', t._remote_path('relative'))
125
102
# we dont os.path.join because windows gives us the wrong path
126
root_segments = self._root.split('/')
103
root_segments = self.test_dir.split('/')
127
104
root_parent = '/'.join(root_segments[:-1])
128
105
# .. should be honoured
129
106
self.assertEqual(root_parent + '/sibling', t._remote_path('../sibling'))
218
195
def test_lock_file(self):
219
196
"""Make sure that a Branch accessed over sftp tries to lock itself."""
220
b = Branch.create(self._sftp_url)
197
b = bzrdir.BzrDir.create_branch_and_repo(self.get_url())
221
198
self.failUnlessExists('.bzr/')
222
199
self.failUnlessExists('.bzr/branch-format')
223
200
self.failUnlessExists('.bzr/branch-lock')
229
206
self.failIf(lexists('.bzr/branch-lock.write-lock'))
231
def test_no_working_tree(self):
232
b = Branch.create(self._sftp_url)
233
self.assertRaises(errors.NoWorkingTree, b.working_tree)
235
208
def test_push_support(self):
236
209
self.build_tree(['a/', 'a/foo'])
237
t = WorkingTree.create_standalone('a')
210
t = bzrdir.BzrDir.create_standalone_workingtree('a')
240
213
t.commit('foo', rev_id='a1')
243
b2 = Branch.create(self._sftp_url + '/b')
215
b2 = bzrdir.BzrDir.create_branch_and_repo(self.get_url('/b'))
246
218
self.assertEquals(b2.revision_history(), ['a1'])
255
227
class SFTPFullHandshakingTest(TestCaseWithSFTPServer):
256
228
"""Verify that a full-handshake (SSH over loopback TCP) sftp connection works."""
257
_full_handshake = True
259
230
def test_connection(self):
231
from bzrlib.transport.sftp import SFTPFullAbsoluteServer
232
self.transport_server = SFTPFullAbsoluteServer
260
233
self.get_transport()