/brz/remove-bazaar

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