/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7143.20.1 by Jelmer Vernooij
Move mergeable functions to breezy.mergeable.
1
# Copyright (C) 2005-2013, 2016 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
7479.2.1 by Jelmer Vernooij
Drop python2 support.
17
import socketserver
7143.20.1 by Jelmer Vernooij
Move mergeable functions to breezy.mergeable.
18
19
from ..mergeable import read_mergeable_from_url
20
from ..directory_service import directories
21
from .. import (
22
    errors,
23
    tests,
24
    )
25
from . import (
26
    test_read_bundle,
27
    test_server,
28
    )
29
30
31
class TestReadMergeableFromUrl(tests.TestCaseWithTransport):
32
33
    def test_read_mergeable_skips_local(self):
34
        """A local bundle named like the URL should not be read.
35
        """
36
        out, wt = test_read_bundle.create_bundle_file(self)
37
38
        class FooService(object):
39
            """A directory service that always returns source"""
40
41
            def look_up(self, name, url):
42
                return 'source'
43
        directories.register('foo:', FooService, 'Testing directory service')
44
        self.addCleanup(directories.remove, 'foo:')
45
        self.build_tree_contents([('./foo:bar', out.getvalue())])
46
        self.assertRaises(errors.NotABundle, read_mergeable_from_url,
47
                          'foo:bar')
48
49
    def test_infinite_redirects_are_not_a_bundle(self):
50
        """If a URL causes TooManyRedirections then NotABundle is raised.
51
        """
52
        from .blackbox.test_push import RedirectingMemoryServer
53
        server = RedirectingMemoryServer()
54
        self.start_server(server)
55
        url = server.get_url() + 'infinite-loop'
56
        self.assertRaises(errors.NotABundle, read_mergeable_from_url, url)
57
58
    def test_smart_server_connection_reset(self):
59
        """If a smart server connection fails during the attempt to read a
60
        bundle, then the ConnectionReset error should be propagated.
61
        """
62
        # Instantiate a server that will provoke a ConnectionReset
63
        sock_server = DisconnectingServer()
64
        self.start_server(sock_server)
65
        # We don't really care what the url is since the server will close the
66
        # connection without interpreting it
67
        url = sock_server.get_url()
68
        self.assertRaises(errors.ConnectionReset, read_mergeable_from_url, url)
69
70
71
class DisconnectingHandler(socketserver.BaseRequestHandler):
72
    """A request handler that immediately closes any connection made to it."""
73
74
    def handle(self):
75
        self.request.close()
76
77
78
class DisconnectingServer(test_server.TestingTCPServerInAThread):
79
80
    def __init__(self):
81
        super(DisconnectingServer, self).__init__(
82
            ('127.0.0.1', 0),
83
            test_server.TestingTCPServer,
84
            DisconnectingHandler)
85
86
    def get_url(self):
87
        """Return the url of the server"""
88
        return "bzr://%s:%d/" % self.server.server_address