~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2006 Canonical Ltd
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
2
#
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.
7
#
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.
12
#
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
18
"""Tests of the bzr serve command."""
19
2018.1.2 by Andrew Bennetts
Tidy imports, skip test if paramiko isn't installed.
20
import os
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
21
import signal
2018.1.2 by Andrew Bennetts
Tidy imports, skip test if paramiko isn't installed.
22
import subprocess
2309.2.5 by Alexander Belchenko
test_bzr_connect_to_bzr_ssh: win32-compatibility
23
import sys
2018.1.2 by Andrew Bennetts
Tidy imports, skip test if paramiko isn't installed.
24
import threading
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
25
2309.2.5 by Alexander Belchenko
test_bzr_connect_to_bzr_ssh: win32-compatibility
26
from bzrlib import (
27
    errors,
28
    osutils,
2598.5.1 by Aaron Bentley
Start eliminating the use of None to indicate null revision
29
    revision as _mod_revision,
2309.2.5 by Alexander Belchenko
test_bzr_connect_to_bzr_ssh: win32-compatibility
30
    )
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
31
from bzrlib.branch import Branch
1910.19.11 by Andrew Bennetts
General code cleanup based on review comments and other observations.
32
from bzrlib.bzrdir import BzrDir
2018.1.2 by Andrew Bennetts
Tidy imports, skip test if paramiko isn't installed.
33
from bzrlib.errors import ParamikoNotPresent
2400.1.9 by Andrew Bennetts
Fix blackbox/test_serve, there were some trivial changes that needed to be brought across from the hpss branch.
34
from bzrlib.smart import medium
2018.1.2 by Andrew Bennetts
Tidy imports, skip test if paramiko isn't installed.
35
from bzrlib.tests import TestCaseWithTransport, TestSkipped
2018.5.21 by Andrew Bennetts
Move bzrlib.transport.smart to bzrlib.smart
36
from bzrlib.transport import get_transport, remote
1910.19.11 by Andrew Bennetts
General code cleanup based on review comments and other observations.
37
38
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
39
class TestBzrServe(TestCaseWithTransport):
1910.19.11 by Andrew Bennetts
General code cleanup based on review comments and other observations.
40
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
41
    def assertInetServerShutsdownCleanly(self, process):
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
42
        """Shutdown the server process looking for errors."""
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
43
        # Shutdown the server: the server should shut down when it cannot read
44
        # from stdin anymore.
45
        process.stdin.close()
1910.19.11 by Andrew Bennetts
General code cleanup based on review comments and other observations.
46
        # Hide stdin from the subprocess module, so it won't fail to close it.
47
        process.stdin = None
2581.1.2 by Martin Pool
Remove unnecessary retcode=0 to run_bzr calls
48
        result = self.finish_bzr_subprocess(process)
1910.19.11 by Andrew Bennetts
General code cleanup based on review comments and other observations.
49
        self.assertEqual('', result[0])
50
        self.assertEqual('', result[1])
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
51
    
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
52
    def assertServerFinishesCleanly(self, process):
53
        """Shutdown the bzr serve instance process looking for errors."""
54
        # Shutdown the server
55
        result = self.finish_bzr_subprocess(process, retcode=3,
56
                                            send_signal=signal.SIGINT)
57
        self.assertEqual('', result[0])
58
        self.assertEqual('bzr: interrupted\n', result[1])
59
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
60
    def make_read_requests(self, branch):
61
        """Do some read only requests."""
62
        branch.lock_read()
63
        try:
64
            branch.repository.all_revision_ids()
65
            self.assertEqual(_mod_revision.NULL_REVISION,
66
                             _mod_revision.ensure_null(branch.last_revision()))
67
        finally:
68
            branch.unlock()
69
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
70
    def start_server_inet(self, extra_options=()):
71
        """Start a bzr server subprocess using the --inet option.
72
73
        :param extra_options: extra options to give the server.
74
        :return: a tuple with the bzr process handle for passing to
75
            finish_bzr_subprocess, a client for the server, and a transport.
76
        """
77
        # Serve from the current directory
78
        process = self.start_bzr_subprocess(['serve', '--inet'])
79
80
        # Connect to the server
81
        # We use this url because while this is no valid URL to connect to this
82
        # server instance, the transport needs a URL.
3431.3.7 by Andrew Bennetts
Fix incidental breakage in blackbox/test_serve.py
83
        url = 'bzr://localhost/'
2018.5.15 by Andrew Bennetts
Tidy some imports, and bugs introduced when adding server.py
84
        client_medium = medium.SmartSimplePipesClientMedium(
3431.3.7 by Andrew Bennetts
Fix incidental breakage in blackbox/test_serve.py
85
            process.stdout, process.stdin, url)
86
        transport = remote.RemoteTransport(url, medium=client_medium)
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
87
        return process, transport
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
88
89
    def start_server_port(self, extra_options=()):
90
        """Start a bzr server subprocess.
91
92
        :param extra_options: extra options to give the server.
93
        :return: a tuple with the bzr process handle for passing to
94
            finish_bzr_subprocess, and the base url for the server.
95
        """
96
        # Serve from the current directory
97
        args = ['serve', '--port', 'localhost:0']
98
        args.extend(extra_options)
99
        process = self.start_bzr_subprocess(args, skip_if_plan_to_signal=True)
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
100
        port_line = process.stdout.readline()
101
        prefix = 'listening on port: '
102
        self.assertStartsWith(port_line, prefix)
103
        port = int(port_line[len(prefix):])
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
104
        return process,'bzr://localhost:%d/' % port
105
106
    def test_bzr_serve_inet_readonly(self):
107
        """bzr server should provide a read only filesystem by default."""
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
108
        process, transport = self.start_server_inet()
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
109
        self.assertRaises(errors.TransportNotPossible, transport.mkdir, 'adir')
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
110
        self.assertInetServerShutsdownCleanly(process)
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
111
112
    def test_bzr_serve_inet_readwrite(self):
113
        # Make a branch
114
        self.make_branch('.')
115
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
116
        process, transport = self.start_server_inet(['--allow-writes'])
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
117
118
        # We get a working branch
119
        branch = BzrDir.open_from_transport(transport).open_branch()
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
120
        self.make_read_requests(branch)
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
121
        self.assertInetServerShutsdownCleanly(process)
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
122
123
    def test_bzr_serve_port_readonly(self):
124
        """bzr server should provide a read only filesystem by default."""
125
        process, url = self.start_server_port()
126
        transport = get_transport(url)
127
        self.assertRaises(errors.TransportNotPossible, transport.mkdir, 'adir')
128
        self.assertServerFinishesCleanly(process)
129
130
    def test_bzr_serve_port_readwrite(self):
131
        # Make a branch
132
        self.make_branch('.')
133
134
        process, url = self.start_server_port(['--allow-writes'])
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
135
136
        # Connect to the server
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
137
        branch = Branch.open(url)
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
138
        self.make_read_requests(branch)
2020.1.1 by Robert Collins
Add readonly support to the smart server, enabled by default via `bzr server`.
139
        self.assertServerFinishesCleanly(process)
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
140
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
141
    def test_bzr_connect_to_bzr_ssh(self):
2018.1.7 by Andrew Bennetts
Use bzrlib rather than a bzr subprocess in test_bzr_connect_to_bzr_ssh.
142
        """User acceptance that get_transport of a bzr+ssh:// behaves correctly.
143
144
        bzr+ssh:// should cause bzr to run a remote bzr smart server over SSH.
145
        """
2018.1.2 by Andrew Bennetts
Tidy imports, skip test if paramiko isn't installed.
146
        try:
147
            from bzrlib.transport.sftp import SFTPServer
148
        except ParamikoNotPresent:
149
            raise TestSkipped('Paramiko not installed')
150
        from bzrlib.tests.stub_sftp import StubServer
2018.1.11 by Andrew Bennetts
Improvements to test_bzr_connect_to_bzr_ssh based on review comments
151
        
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
152
        # Make a branch
2018.1.2 by Andrew Bennetts
Tidy imports, skip test if paramiko isn't installed.
153
        self.make_branch('a_branch')
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
154
155
        # Start an SSH server
2018.1.11 by Andrew Bennetts
Improvements to test_bzr_connect_to_bzr_ssh based on review comments
156
        self.command_executed = []
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
157
        # XXX: This is horrible -- we define a really dumb SSH server that
158
        # executes commands, and manage the hooking up of stdin/out/err to the
159
        # SSH channel ourselves.  Surely this has already been implemented
160
        # elsewhere?
2018.1.2 by Andrew Bennetts
Tidy imports, skip test if paramiko isn't installed.
161
        class StubSSHServer(StubServer):
162
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
163
            test = self
164
165
            def check_channel_exec_request(self, channel, command):
2018.1.11 by Andrew Bennetts
Improvements to test_bzr_connect_to_bzr_ssh based on review comments
166
                self.test.command_executed.append(command)
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
167
                proc = subprocess.Popen(
168
                    command, shell=True, stdin=subprocess.PIPE,
169
                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
170
                
171
                # XXX: horribly inefficient, not to mention ugly.
2018.1.11 by Andrew Bennetts
Improvements to test_bzr_connect_to_bzr_ssh based on review comments
172
                # Start a thread for each of stdin/out/err, and relay bytes from
173
                # the subprocess to channel and vice versa.
174
                def ferry_bytes(read, write, close):
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
175
                    while True:
2018.1.11 by Andrew Bennetts
Improvements to test_bzr_connect_to_bzr_ssh based on review comments
176
                        bytes = read(1)
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
177
                        if bytes == '':
2018.1.11 by Andrew Bennetts
Improvements to test_bzr_connect_to_bzr_ssh based on review comments
178
                            close()
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
179
                            break
2018.1.11 by Andrew Bennetts
Improvements to test_bzr_connect_to_bzr_ssh based on review comments
180
                        write(bytes)
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
181
2018.1.11 by Andrew Bennetts
Improvements to test_bzr_connect_to_bzr_ssh based on review comments
182
                file_functions = [
183
                    (channel.recv, proc.stdin.write, proc.stdin.close),
184
                    (proc.stdout.read, channel.sendall, channel.close),
185
                    (proc.stderr.read, channel.sendall_stderr, channel.close)]
186
                for read, write, close in file_functions:
187
                    t = threading.Thread(
188
                        target=ferry_bytes, args=(read, write, close))
189
                    t.start()
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
190
191
                return True
192
2018.1.2 by Andrew Bennetts
Tidy imports, skip test if paramiko isn't installed.
193
        ssh_server = SFTPServer(StubSSHServer)
2018.1.7 by Andrew Bennetts
Use bzrlib rather than a bzr subprocess in test_bzr_connect_to_bzr_ssh.
194
        # XXX: We *don't* want to override the default SSH vendor, so we set
195
        # _vendor to what _get_ssh_vendor returns.
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
196
        ssh_server.setUp()
197
        self.addCleanup(ssh_server.tearDown)
198
        port = ssh_server._listener.port
199
200
        # Access the branch via a bzr+ssh URL.  The BZR_REMOTE_PATH environment
201
        # variable is used to tell bzr what command to run on the remote end.
2309.2.5 by Alexander Belchenko
test_bzr_connect_to_bzr_ssh: win32-compatibility
202
        path_to_branch = osutils.abspath('a_branch')
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
203
        
2018.1.7 by Andrew Bennetts
Use bzrlib rather than a bzr subprocess in test_bzr_connect_to_bzr_ssh.
204
        orig_bzr_remote_path = os.environ.get('BZR_REMOTE_PATH')
2309.2.5 by Alexander Belchenko
test_bzr_connect_to_bzr_ssh: win32-compatibility
205
        bzr_remote_path = self.get_bzr_path()
206
        if sys.platform == 'win32':
207
            bzr_remote_path = sys.executable + ' ' + self.get_bzr_path()
208
        os.environ['BZR_REMOTE_PATH'] = bzr_remote_path
2018.1.7 by Andrew Bennetts
Use bzrlib rather than a bzr subprocess in test_bzr_connect_to_bzr_ssh.
209
        try:
2309.2.5 by Alexander Belchenko
test_bzr_connect_to_bzr_ssh: win32-compatibility
210
            if sys.platform == 'win32':
211
                path_to_branch = os.path.splitdrive(path_to_branch)[1]
2018.1.7 by Andrew Bennetts
Use bzrlib rather than a bzr subprocess in test_bzr_connect_to_bzr_ssh.
212
            branch = Branch.open(
2018.1.9 by Andrew Bennetts
Implement ParamikoVendor.connect_ssh
213
                'bzr+ssh://fred:secret@localhost:%d%s' % (port, path_to_branch))
3287.6.4 by Robert Collins
Fix up deprecation warnings for get_revision_graph.
214
            self.make_read_requests(branch)
2018.1.10 by Andrew Bennetts
Merge --allow-writes from Robert
215
            # Check we can perform write operations
216
            branch.bzrdir.root_transport.mkdir('foo')
2018.1.7 by Andrew Bennetts
Use bzrlib rather than a bzr subprocess in test_bzr_connect_to_bzr_ssh.
217
        finally:
218
            # Restore the BZR_REMOTE_PATH environment variable back to its
219
            # original state.
220
            if orig_bzr_remote_path is None:
221
                del os.environ['BZR_REMOTE_PATH']
222
            else:
223
                os.environ['BZR_REMOTE_PATH'] = orig_bzr_remote_path
224
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
225
        self.assertEqual(
2018.1.11 by Andrew Bennetts
Improvements to test_bzr_connect_to_bzr_ssh based on review comments
226
            ['%s serve --inet --directory=/ --allow-writes'
2309.2.5 by Alexander Belchenko
test_bzr_connect_to_bzr_ssh: win32-compatibility
227
             % bzr_remote_path],
2018.1.1 by Andrew Bennetts
Make bzr+ssh:// actually work (at least with absolute paths).
228
            self.command_executed)
229