2017-01-13 01:09:52 +01:00
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import time
|
2023-01-18 04:06:51 +01:00
|
|
|
from contextlib import ExitStack, contextmanager
|
2019-02-02 23:53:29 +01:00
|
|
|
from typing import Iterator, Optional
|
2017-01-13 01:09:52 +01:00
|
|
|
|
2017-04-03 11:48:57 +02:00
|
|
|
# Verify the Zulip venv is available.
|
|
|
|
from tools.lib import sanity_check
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-04-03 11:48:57 +02:00
|
|
|
sanity_check.check_venv(__file__)
|
|
|
|
|
|
|
|
import django
|
|
|
|
import requests
|
2017-01-13 01:09:52 +01:00
|
|
|
|
2020-06-21 20:05:43 +02:00
|
|
|
MAX_SERVER_WAIT = 180
|
2020-03-28 21:39:20 +01:00
|
|
|
|
2017-01-13 01:09:52 +01:00
|
|
|
TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
if TOOLS_DIR not in sys.path:
|
|
|
|
sys.path.insert(0, os.path.dirname(TOOLS_DIR))
|
|
|
|
|
2019-07-16 08:10:29 +02:00
|
|
|
from scripts.lib.zulip_tools import get_or_create_dev_uuid_var_path
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.lib.test_fixtures import update_test_databases_if_required
|
|
|
|
|
2017-01-13 01:09:52 +01:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def set_up_django(external_host: str) -> None:
|
2021-02-12 08:20:45 +01:00
|
|
|
os.environ["FULL_STACK_ZULIP_TEST"] = "1"
|
|
|
|
os.environ["TEST_EXTERNAL_HOST"] = external_host
|
|
|
|
os.environ["LOCAL_UPLOADS_DIR"] = get_or_create_dev_uuid_var_path("test-backend/test_uploads")
|
|
|
|
os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.test_settings"
|
2017-01-13 01:09:52 +01:00
|
|
|
django.setup()
|
2021-02-12 08:20:45 +01:00
|
|
|
os.environ["PYTHONUNBUFFERED"] = "y"
|
2017-01-13 01:09:52 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
|
2017-01-13 01:09:52 +01:00
|
|
|
"""Get the exit code of the server, or None if it is still running."""
|
|
|
|
if server.poll() is not None:
|
2021-02-12 08:20:45 +01:00
|
|
|
message = "Server died unexpectedly!"
|
2017-01-25 18:31:51 +01:00
|
|
|
if log_file:
|
2021-02-12 08:20:45 +01:00
|
|
|
message += f"\nSee {log_file}\n"
|
2017-01-25 18:31:51 +01:00
|
|
|
raise RuntimeError(message)
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
python: Convert function type annotations to Python 3 style.
Generated by com2ann (slightly patched to avoid also converting
assignment type annotations, which require Python 3.6), followed by
some manual whitespace adjustment, and six fixes for runtime issues:
- def __init__(self, token: Token, parent: Optional[Node]) -> None:
+ def __init__(self, token: Token, parent: "Optional[Node]") -> None:
-def main(options: argparse.Namespace) -> NoReturn:
+def main(options: argparse.Namespace) -> "NoReturn":
-def fetch_request(url: str, callback: Any, **kwargs: Any) -> Generator[Callable[..., Any], Any, None]:
+def fetch_request(url: str, callback: Any, **kwargs: Any) -> "Generator[Callable[..., Any], Any, None]":
-def assert_server_running(server: subprocess.Popen[bytes], log_file: Optional[str]) -> None:
+def assert_server_running(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> None:
-def server_is_up(server: subprocess.Popen[bytes], log_file: Optional[str]) -> bool:
+def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
- method_kwarg_pairs: List[FuncKwargPair],
+ method_kwarg_pairs: "List[FuncKwargPair]",
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-19 03:48:37 +02:00
|
|
|
def server_is_up(server: "subprocess.Popen[bytes]", log_file: Optional[str]) -> bool:
|
2017-01-25 18:31:51 +01:00
|
|
|
assert_server_running(server, log_file)
|
2017-01-13 01:09:52 +01:00
|
|
|
try:
|
|
|
|
# We could get a 501 error if the reverse proxy is up but the Django app isn't.
|
2020-03-28 21:16:52 +01:00
|
|
|
# Note that zulipdev.com is mapped via DNS to 127.0.0.1.
|
2021-02-12 08:20:45 +01:00
|
|
|
return requests.get("http://zulipdev.com:9981/accounts/home").status_code == 200
|
2020-10-09 03:32:34 +02:00
|
|
|
except requests.RequestException:
|
2017-01-13 01:09:52 +01:00
|
|
|
return False
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-01-13 01:09:52 +01:00
|
|
|
@contextmanager
|
2021-02-12 08:19:30 +01:00
|
|
|
def test_server_running(
|
2021-03-02 20:59:19 +01:00
|
|
|
skip_provision_check: bool = False,
|
2021-02-12 08:20:45 +01:00
|
|
|
external_host: str = "testserver",
|
2021-02-12 08:19:30 +01:00
|
|
|
log_file: Optional[str] = None,
|
|
|
|
dots: bool = False,
|
|
|
|
) -> Iterator[None]:
|
2023-01-18 04:06:51 +01:00
|
|
|
with ExitStack() as stack:
|
|
|
|
log = sys.stdout
|
|
|
|
if log_file:
|
|
|
|
if os.path.exists(log_file) and os.path.getsize(log_file) < 100000:
|
|
|
|
log = stack.enter_context(open(log_file, "a"))
|
|
|
|
log.write("\n\n")
|
|
|
|
else:
|
|
|
|
log = stack.enter_context(open(log_file, "w"))
|
|
|
|
|
|
|
|
set_up_django(external_host)
|
|
|
|
|
|
|
|
update_test_databases_if_required(rebuild_test_database=True)
|
|
|
|
|
|
|
|
# Run this not through the shell, so that we have the actual PID.
|
2023-03-04 02:17:54 +01:00
|
|
|
run_dev_server_command = ["tools/run-dev", "--test", "--streamlined"]
|
2023-01-18 04:06:51 +01:00
|
|
|
if skip_provision_check:
|
|
|
|
run_dev_server_command.append("--skip-provision-check")
|
|
|
|
server = subprocess.Popen(run_dev_server_command, stdout=log, stderr=log)
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Wait for the server to start up.
|
|
|
|
print(end="\nWaiting for test server (may take a while)")
|
|
|
|
if not dots:
|
|
|
|
print("\n", flush=True)
|
|
|
|
t = time.time()
|
|
|
|
while not server_is_up(server, log_file):
|
|
|
|
if dots:
|
|
|
|
print(end=".", flush=True)
|
|
|
|
time.sleep(0.4)
|
|
|
|
if time.time() - t > MAX_SERVER_WAIT:
|
|
|
|
raise Exception("Timeout waiting for server")
|
|
|
|
print("\n\n--- SERVER IS UP! ---\n", flush=True)
|
|
|
|
|
|
|
|
# DO OUR ACTUAL TESTING HERE!!!
|
|
|
|
yield
|
|
|
|
|
|
|
|
finally:
|
|
|
|
assert_server_running(server, log_file)
|
|
|
|
server.terminate()
|
|
|
|
server.wait()
|
2017-01-13 01:09:52 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
if __name__ == "__main__":
|
2017-01-13 01:09:52 +01:00
|
|
|
# The code below is for testing this module works
|
|
|
|
with test_server_running():
|
2021-02-12 08:20:45 +01:00
|
|
|
print("\n\n SERVER IS UP!\n\n")
|