1
0
Fork 0
mirror of https://github.com/Kozea/Radicale.git synced 2025-06-26 16:45:52 +00:00
Radicale/radicale/tests/test_server.py

201 lines
7.3 KiB
Python
Raw Normal View History

2018-09-04 03:33:45 +02:00
# This file is part of Radicale Server - Calendar Server
2019-06-17 04:13:25 +02:00
# Copyright © 2018-2019 Unrud <unrud@outlook.com>
2018-09-04 03:33:45 +02:00
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
"""
Test the internal server.
"""
2018-09-06 09:12:53 +02:00
import os
2018-09-04 03:33:45 +02:00
import shutil
import socket
import ssl
2018-09-09 14:58:44 +02:00
import subprocess
import sys
2018-09-04 03:33:45 +02:00
import tempfile
import threading
import time
2019-06-17 04:13:25 +02:00
from configparser import RawConfigParser
2018-09-04 03:33:45 +02:00
from urllib import request
from urllib.error import HTTPError, URLError
2019-06-15 09:01:55 +02:00
import pytest
2018-09-04 03:33:45 +02:00
from radicale import config, server
2019-06-17 04:13:25 +02:00
from .helpers import configuration_to_dict, get_file_path
2018-09-04 03:33:45 +02:00
2018-09-09 14:58:44 +02:00
try:
import gunicorn
except ImportError:
gunicorn = None
2018-09-04 03:33:45 +02:00
class DisabledRedirectHandler(request.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
raise HTTPError(req.full_url, code, msg, headers, fp)
http_error_301 = http_error_303 = http_error_307 = http_error_302
class TestBaseServerRequests:
"""Test the internal server."""
def setup(self):
self.configuration = config.load()
self.colpath = tempfile.mkdtemp()
self.shutdown_socket, shutdown_socket_out = socket.socketpair()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Find available port
2018-09-06 10:50:54 +02:00
sock.bind(("127.0.0.1", 0))
2018-09-04 03:33:45 +02:00
self.sockname = sock.getsockname()
2019-06-17 04:13:25 +02:00
self.configuration.update({
"storage": {"filesystem_folder": self.colpath},
"server": {"hosts": "[%s]:%d" % self.sockname},
# Enable debugging for new processes
"logging": {"level": "debug"},
# Disable syncing to disk for better performance
"internal": {"filesystem_fsync": "False"}}, "test")
2018-09-04 03:33:45 +02:00
self.thread = threading.Thread(target=server.serve, args=(
self.configuration, shutdown_socket_out))
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
self.opener = request.build_opener(
request.HTTPSHandler(context=ssl_context),
DisabledRedirectHandler)
def teardown(self):
self.shutdown_socket.sendall(b" ")
2018-09-06 09:12:53 +02:00
try:
self.thread.join()
except RuntimeError: # Thread never started
pass
2018-09-04 03:33:45 +02:00
shutil.rmtree(self.colpath)
2018-09-09 14:58:44 +02:00
def request(self, method, path, data=None, is_alive_fn=None, **headers):
2018-09-04 03:33:45 +02:00
"""Send a request."""
2018-09-09 14:58:44 +02:00
if is_alive_fn is None:
is_alive_fn = self.thread.is_alive
2019-06-17 04:13:25 +02:00
scheme = ("https" if self.configuration.get("server", "ssl") else
"http")
2018-09-04 03:33:45 +02:00
req = request.Request(
"%s://[%s]:%d%s" % (scheme, *self.sockname, path),
data=data, headers=headers, method=method)
while True:
2018-09-09 14:58:44 +02:00
assert is_alive_fn()
2018-09-04 03:33:45 +02:00
try:
with self.opener.open(req) as f:
return f.getcode(), f.info(), f.read().decode()
except HTTPError as e:
return e.code, e.headers, e.read().decode()
except URLError as e:
if not isinstance(e.reason, ConnectionRefusedError):
raise
time.sleep(0.1)
def test_root(self):
self.thread.start()
status, _, _ = self.request("GET", "/")
assert status == 302
def test_ssl(self):
2019-06-17 04:13:25 +02:00
self.configuration.update({
"server": {"ssl": "True",
"certificate": get_file_path("cert.pem"),
"key": get_file_path("key.pem")}}, "test")
2018-09-04 03:33:45 +02:00
self.thread.start()
status, _, _ = self.request("GET", "/")
assert status == 302
2018-09-06 09:12:53 +02:00
2018-09-09 14:58:43 +02:00
@pytest.mark.skipif(not server.HAS_IPV6, reason="IPv6 not supported")
2018-09-06 09:12:53 +02:00
def test_ipv6(self):
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as sock:
sock.setsockopt(server.IPPROTO_IPV6, server.IPV6_V6ONLY, 1)
2018-09-08 09:24:46 +02:00
try:
# Find available port
sock.bind(("::1", 0))
except OSError:
pytest.skip("IPv6 not supported")
2018-09-06 09:12:53 +02:00
self.sockname = sock.getsockname()[:2]
2019-06-17 04:13:25 +02:00
self.configuration.update({
"server": {"hosts": "[%s]:%d" % self.sockname}}, "test")
2018-09-09 14:58:42 +02:00
savedEaiAddrfamily = server.EAI_ADDRFAMILY
if os.name == "nt" and server.EAI_ADDRFAMILY is None:
# HACK: incomplete errno conversion in WINE
server.EAI_ADDRFAMILY = -9
try:
self.thread.start()
status, _, _ = self.request("GET", "/")
finally:
server.EAI_ADDRFAMILY = savedEaiAddrfamily
2018-09-06 09:12:53 +02:00
assert status == 302
2018-09-09 14:58:44 +02:00
def test_command_line_interface(self):
config_args = []
2019-06-17 04:13:25 +02:00
for section, values in config.DEFAULT_CONFIG_SCHEMA.items():
if values.get("_internal", False):
continue
2018-09-09 14:58:44 +02:00
for option, data in values.items():
2019-06-17 04:13:25 +02:00
if option.startswith("_"):
continue
2018-09-09 14:58:44 +02:00
long_name = "--{0}-{1}".format(
section, option.replace("_", "-"))
if data["type"] == bool:
2019-06-17 04:13:25 +02:00
if not self.configuration.get(section, option):
2018-09-09 14:58:44 +02:00
long_name = "--no{0}".format(long_name[1:])
config_args.append(long_name)
else:
config_args.append(long_name)
2019-06-17 04:13:25 +02:00
config_args.append(
self.configuration.get_raw(section, option))
2018-09-09 14:58:44 +02:00
env = os.environ.copy()
env["PYTHONPATH"] = os.pathsep.join(sys.path)
p = subprocess.Popen(
[sys.executable, "-m", "radicale"] + config_args, env=env)
try:
status, _, _ = self.request(
"GET", "/", is_alive_fn=lambda: p.poll() is None)
assert status == 302
finally:
p.terminate()
p.wait()
if os.name == "posix":
assert p.returncode == 0
2018-09-09 14:58:44 +02:00
@pytest.mark.skipif(not gunicorn, reason="gunicorn module not found")
def test_wsgi_server(self):
config_path = os.path.join(self.colpath, "config")
2019-06-17 04:13:25 +02:00
parser = RawConfigParser()
parser.read_dict(configuration_to_dict(self.configuration))
2018-09-09 14:58:44 +02:00
with open(config_path, "w") as f:
2019-06-17 04:13:25 +02:00
parser.write(f)
2018-09-09 14:58:44 +02:00
env = os.environ.copy()
env["PYTHONPATH"] = os.pathsep.join(sys.path)
p = subprocess.Popen([
sys.executable,
"-c", "from gunicorn.app.wsgiapp import run; run()",
2019-06-17 04:13:25 +02:00
"--bind", self.configuration.get_raw("server", "hosts"),
2018-09-09 14:58:44 +02:00
"--env", "RADICALE_CONFIG=%s" % config_path, "radicale"], env=env)
try:
status, _, _ = self.request(
"GET", "/", is_alive_fn=lambda: p.poll() is None)
assert status == 302
finally:
p.terminate()
p.wait()
assert p.returncode == 0