75
61
self.assertEqual('', result[0])
76
62
self.assertEqual('', result[1])
78
def test_bzr_serve_port(self):
82
# Serve that branch from the current directory
83
process = self.start_bzr_subprocess(['serve', '--port', 'localhost:0'],
84
skip_if_plan_to_signal=True)
64
def assertServerFinishesCleanly(self, process):
65
"""Shutdown the bzr serve instance process looking for errors."""
67
result = self.finish_bzr_subprocess(process, retcode=3,
68
send_signal=signal.SIGINT)
69
self.assertEqual('', result[0])
70
self.assertEqual('bzr: interrupted\n', result[1])
72
def start_server_inet(self, extra_options=()):
73
"""Start a bzr server subprocess using the --inet option.
75
:param extra_options: extra options to give the server.
76
:return: a tuple with the bzr process handle for passing to
77
finish_bzr_subprocess, a client for the server, and a transport.
79
# Serve from the current directory
80
process = self.start_bzr_subprocess(['serve', '--inet'])
82
# Connect to the server
83
# We use this url because while this is no valid URL to connect to this
84
# server instance, the transport needs a URL.
85
client = DoesNotCloseStdOutClient(
86
lambda: (process.stdout, process.stdin))
87
transport = smart.SmartTransport('bzr://localhost/', client=client)
88
return process, client, transport
90
def start_server_port(self, extra_options=()):
91
"""Start a bzr server subprocess.
93
:param extra_options: extra options to give the server.
94
:return: a tuple with the bzr process handle for passing to
95
finish_bzr_subprocess, and the base url for the server.
97
# Serve from the current directory
98
args = ['serve', '--port', 'localhost:0']
99
args.extend(extra_options)
100
process = self.start_bzr_subprocess(args, skip_if_plan_to_signal=True)
85
101
port_line = process.stdout.readline()
86
102
prefix = 'listening on port: '
87
103
self.assertStartsWith(port_line, prefix)
88
104
port = int(port_line[len(prefix):])
105
return process,'bzr://localhost:%d/' % port
107
def test_bzr_serve_inet_readonly(self):
108
"""bzr server should provide a read only filesystem by default."""
109
process, client, transport = self.start_server_inet()
110
self.assertRaises(errors.TransportNotPossible, transport.mkdir, 'adir')
111
# finish with the transport
113
self.assertInetServerShutsdownCleanly(client, process)
115
def test_bzr_serve_inet_readwrite(self):
117
self.make_branch('.')
119
process, client, transport = self.start_server_inet(['--allow-writes'])
121
# We get a working branch
122
branch = BzrDir.open_from_transport(transport).open_branch()
123
branch.repository.get_revision_graph()
124
self.assertEqual(None, branch.last_revision())
126
# finish with the transport
129
self.assertInetServerShutsdownCleanly(client, process)
131
def test_bzr_serve_port_readonly(self):
132
"""bzr server should provide a read only filesystem by default."""
133
process, url = self.start_server_port()
134
transport = get_transport(url)
135
self.assertRaises(errors.TransportNotPossible, transport.mkdir, 'adir')
136
self.assertServerFinishesCleanly(process)
138
def test_bzr_serve_port_readwrite(self):
140
self.make_branch('.')
142
process, url = self.start_server_port(['--allow-writes'])
90
144
# Connect to the server
91
branch = Branch.open('bzr://localhost:%d/' % port)
145
branch = Branch.open(url)
93
147
# We get a working branch
94
148
branch.repository.get_revision_graph()
95
149
self.assertEqual(None, branch.last_revision())
98
result = self.finish_bzr_subprocess(process, retcode=3,
99
send_signal=signal.SIGINT)
100
self.assertEqual('', result[0])
101
self.assertEqual('bzr: interrupted\n', result[1])
151
self.assertServerFinishesCleanly(process)
103
153
def test_bzr_serve_no_args(self):
104
154
"""'bzr serve' with no arguments or options should not traceback."""
105
155
out, err = self.run_bzr_error(
106
156
['bzr serve requires one of --inet or --port'], 'serve')
158
def test_bzr_connect_to_bzr_ssh(self):
159
"""User acceptance that get_transport of a bzr+ssh:// behaves correctly.
161
bzr+ssh:// should cause bzr to run a remote bzr smart server over SSH.
164
from bzrlib.transport.sftp import SFTPServer
165
except ParamikoNotPresent:
166
raise TestSkipped('Paramiko not installed')
167
from bzrlib.tests.stub_sftp import StubServer
170
self.make_branch('a_branch')
172
# Start an SSH server
173
self.command_executed = []
174
# XXX: This is horrible -- we define a really dumb SSH server that
175
# executes commands, and manage the hooking up of stdin/out/err to the
176
# SSH channel ourselves. Surely this has already been implemented
178
class StubSSHServer(StubServer):
182
def check_channel_exec_request(self, channel, command):
183
self.test.command_executed.append(command)
184
proc = subprocess.Popen(
185
command, shell=True, stdin=subprocess.PIPE,
186
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
188
# XXX: horribly inefficient, not to mention ugly.
189
# Start a thread for each of stdin/out/err, and relay bytes from
190
# the subprocess to channel and vice versa.
191
def ferry_bytes(read, write, close):
200
(channel.recv, proc.stdin.write, proc.stdin.close),
201
(proc.stdout.read, channel.sendall, channel.close),
202
(proc.stderr.read, channel.sendall_stderr, channel.close)]
203
for read, write, close in file_functions:
204
t = threading.Thread(
205
target=ferry_bytes, args=(read, write, close))
210
ssh_server = SFTPServer(StubSSHServer)
211
# XXX: We *don't* want to override the default SSH vendor, so we set
212
# _vendor to what _get_ssh_vendor returns.
214
self.addCleanup(ssh_server.tearDown)
215
port = ssh_server._listener.port
217
# Access the branch via a bzr+ssh URL. The BZR_REMOTE_PATH environment
218
# variable is used to tell bzr what command to run on the remote end.
219
path_to_branch = os.path.abspath('a_branch')
221
orig_bzr_remote_path = os.environ.get('BZR_REMOTE_PATH')
222
os.environ['BZR_REMOTE_PATH'] = self.get_bzr_path()
224
branch = Branch.open(
225
'bzr+ssh://fred:secret@localhost:%d%s' % (port, path_to_branch))
227
branch.repository.get_revision_graph()
228
self.assertEqual(None, branch.last_revision())
229
# Check we can perform write operations
230
branch.bzrdir.root_transport.mkdir('foo')
232
# Restore the BZR_REMOTE_PATH environment variable back to its
234
if orig_bzr_remote_path is None:
235
del os.environ['BZR_REMOTE_PATH']
237
os.environ['BZR_REMOTE_PATH'] = orig_bzr_remote_path
240
['%s serve --inet --directory=/ --allow-writes'
241
% self.get_bzr_path()],
242
self.command_executed)