1
1
# Copyright (C) 2005 by Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
from bzrlib.tests import TestCaseWithTransport
23
class TestCaseWithWebserver(TestCaseWithTransport):
24
"""A support class that provides readonly urls that are http://.
26
This is done by forcing the readonly server to be an http one. This
27
will current fail if the primary transport is not backed by regular disk
17
import BaseHTTPServer, SimpleHTTPServer
18
from bzrlib.selftest import TestCaseInTempDir
21
class WebserverNotAvailable(Exception):
24
class BadWebserverPath(ValueError):
26
return 'path %s is not in %s' % self.args
28
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
29
def log_message(self, format, *args):
30
self.server.test_case.log("webserver - %s - - [%s] %s\n" %
31
(self.address_string(),
32
self.log_date_time_string(),
35
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
36
def __init__(self, server_address, RequestHandlerClass, test_case):
37
BaseHTTPServer.HTTPServer.__init__(self, server_address,
39
self.test_case = test_case
41
class TestCaseWithWebserver(TestCaseInTempDir):
42
"""Derived class that starts a localhost-only webserver
43
(in addition to what TestCaseInTempDir does).
45
This is useful for testing RemoteBranch.
48
_HTTP_PORTS = range(13000, 0x8000)
50
def _http_start(self):
51
import SimpleHTTPServer, BaseHTTPServer, socket, errno
53
for port in self._HTTP_PORTS:
55
httpd = TestingHTTPServer(('localhost', port),
56
TestingHTTPRequestHandler,
58
except socket.error, e:
59
if e.args[0] == errno.EADDRINUSE:
61
print >>sys.stderr, "Cannot run webserver :-("
67
raise WebserverNotAvailable("Cannot run webserver :-( "
68
"no free ports in range %s..%s" %
69
(_HTTP_PORTS[0], _HTTP_PORTS[-1]))
71
self._http_base_url = 'http://localhost:%s/' % port
72
self._http_starting.release()
73
httpd.socket.settimeout(1)
75
while self._http_running:
77
httpd.handle_request()
78
except socket.timeout:
81
def get_remote_url(self, path):
84
path_parts = path.split(os.path.sep)
85
if os.path.isabs(path):
86
if path_parts[:len(self._local_path_parts)] != \
87
self._local_path_parts:
88
raise BadWebserverPath(path, self.test_dir)
89
remote_path = '/'.join(path_parts[len(self._local_path_parts):])
91
remote_path = '/'.join(path_parts)
93
self._http_starting.acquire()
94
self._http_starting.release()
95
return self._http_base_url + remote_path
32
98
super(TestCaseWithWebserver, self).setUp()
33
self.transport_readonly_server = bzrlib.transport.http.HttpServer
100
self._local_path_parts = self.test_dir.split(os.path.sep)
101
self._http_starting = threading.Lock()
102
self._http_starting.acquire()
103
self._http_running = True
104
self._http_base_url = None
105
self._http_thread = threading.Thread(target=self._http_start)
106
self._http_thread.setDaemon(True)
107
self._http_thread.start()
110
self._http_running = False
111
self._http_thread.join()
112
super(TestCaseWithWebserver, self).tearDown()