/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
1
# Copyright (C) 2006 by Canonical Ltd
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
20
import signal
21
22
from bzrlib.branch import Branch
1910.19.11 by Andrew Bennetts
General code cleanup based on review comments and other observations.
23
from bzrlib.bzrdir import BzrDir
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
24
from bzrlib.tests import TestCaseWithTransport
1910.19.11 by Andrew Bennetts
General code cleanup based on review comments and other observations.
25
from bzrlib.transport import smart
26
27
28
class DoesNotCloseStdOutClient(smart.SmartStreamClient):
29
    """A client that doesn't close stdout upon disconnect().
30
    
31
    We wish to let stdout remain open so that we can see if the server writes
32
    anything to stdout during its shutdown.
33
    """
34
35
    def disconnect(self):
36
        if self._connected:
37
            self._connected = False
38
            # The client's out is the server's in.
39
            self._out.close()
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
40
41
42
class TestBzrServe(TestCaseWithTransport):
1910.19.11 by Andrew Bennetts
General code cleanup based on review comments and other observations.
43
44
    def test_bzr_serve_inet(self):
45
        # Make a branch
46
        self.make_branch('.')
47
48
        # Serve that branch from the current directory
49
        process = self.start_bzr_subprocess(['serve', '--inet'])
50
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)
57
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())
62
63
        # finish with the transport
64
        del transport
65
        # Disconnect the client forcefully JUST IN CASE because of __del__'s use
66
        # in the smart module.
67
        client.disconnect()
68
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.
73
        process.stdin = None
74
        result = self.finish_bzr_subprocess(process, retcode=0)
75
        self.assertEqual('', result[0])
76
        self.assertEqual('', result[1])
1910.19.7 by Andrew Bennetts
Allow specifying the host/interface to bzr serve, and use the new test
77
    
78
    def test_bzr_serve_port(self):
79
        # Make a branch
80
        self.make_branch('.')
81
82
        # Serve that branch from the current directory
1910.19.11 by Andrew Bennetts
General code cleanup based on review comments and other observations.
83
        process = self.start_bzr_subprocess(['serve', '--port', 'localhost:0'],
84
                                            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
85
        port_line = process.stdout.readline()
86
        prefix = 'listening on port: '
87
        self.assertStartsWith(port_line, prefix)
88
        port = int(port_line[len(prefix):])
89
90
        # Connect to the server
91
        branch = Branch.open('bzr://localhost:%d/' % port)
92
93
        # We get a working branch
94
        branch.repository.get_revision_graph()
95
        self.assertEqual(None, branch.last_revision())
96
97
        # Shutdown the server
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])
102
1910.19.8 by Andrew Bennetts
Give a sensible error if insufficient options are passed to 'bzr serve'.
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')
107