1
# Copyright (C) 2006 by Canonical Ltd
 
 
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.
 
 
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.
 
 
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
 
 
18
"""Tests of the bzr serve command."""
 
 
22
from bzrlib.branch import Branch
 
 
23
from bzrlib.bzrdir import BzrDir
 
 
24
from bzrlib.tests import TestCaseWithTransport
 
 
25
from bzrlib.transport import smart
 
 
28
class DoesNotCloseStdOutClient(smart.SmartStreamClient):
 
 
29
    """A client that doesn't close stdout upon disconnect().
 
 
31
    We wish to let stdout remain open so that we can see if the server writes
 
 
32
    anything to stdout during its shutdown.
 
 
37
            self._connected = False
 
 
38
            # The client's out is the server's in.
 
 
42
class TestBzrServe(TestCaseWithTransport):
 
 
44
    def test_bzr_serve_inet(self):
 
 
48
        # Serve that branch from the current directory
 
 
49
        process = self.start_bzr_subprocess(['serve', '--inet'])
 
 
51
        # Connect to the server
 
 
52
        # We use this url because while this is no valid URL to connect to this
 
 
53
        # server instance, the transport needs a URL.
 
 
54
        client = DoesNotCloseStdOutClient(
 
 
55
            lambda: (process.stdout, process.stdin))
 
 
56
        transport = smart.SmartTransport('bzr://localhost/', client=client)
 
 
58
        # We get a working branch
 
 
59
        branch = BzrDir.open_from_transport(transport).open_branch()
 
 
60
        branch.repository.get_revision_graph()
 
 
61
        self.assertEqual(None, branch.last_revision())
 
 
63
        # finish with the transport
 
 
65
        # Disconnect the client forcefully JUST IN CASE because of __del__'s use
 
 
66
        # in the smart module.
 
 
69
        # Shutdown the server: the client should have disconnected cleanly and
 
 
70
        # closed stdin, so the server process should shut itself down.
 
 
71
        self.assertTrue(process.stdin.closed)
 
 
72
        # Hide stdin from the subprocess module, so it won't fail to close it.
 
 
74
        result = self.finish_bzr_subprocess(process, retcode=0)
 
 
75
        self.assertEqual('', result[0])
 
 
76
        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)
 
 
85
        port_line = process.stdout.readline()
 
 
86
        prefix = 'listening on port: '
 
 
87
        self.assertStartsWith(port_line, prefix)
 
 
88
        port = int(port_line[len(prefix):])
 
 
90
        # Connect to the server
 
 
91
        branch = Branch.open('bzr://localhost:%d/' % port)
 
 
93
        # We get a working branch
 
 
94
        branch.repository.get_revision_graph()
 
 
95
        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])
 
 
103
    def test_bzr_serve_no_args(self):
 
 
104
        """'bzr serve' with no arguments or options should not traceback."""
 
 
105
        out, err = self.run_bzr_error(
 
 
106
            ['bzr serve requires one of --inet or --port'], 'serve')