Skip to content

Commit

Permalink
2882 Apply dynamic port fixture for tests of cli, config and server … (
Browse files Browse the repository at this point in the history
…#2892)

Co-authored-by: Adam Hopkins <adam@amhopkins.com>
  • Loading branch information
iAndriy and ahopkins committed Apr 1, 2024
1 parent afe4f27 commit 1a70930
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 58 deletions.
108 changes: 58 additions & 50 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from sanic.__main__ import main
from sanic.cli.inspector_client import InspectorClient

from .conftest import get_port


@pytest.fixture(scope="module", autouse=True)
def tty():
Expand Down Expand Up @@ -58,16 +60,14 @@ def read_app_info(lines: List[str]):
),
)
def test_server_run(
appname: str,
extra: Optional[str],
caplog: pytest.LogCaptureFixture,
appname: str, extra: Optional[str], caplog: pytest.LogCaptureFixture, port
):
command = [appname]
command = [appname, f"-p={port}"]
if extra:
command.append(extra)
lines = capture(command, caplog)

assert "Goin' Fast @ http://127.0.0.1:8000" in lines
assert f"Goin' Fast @ http://127.0.0.1:{port}" in lines


@pytest.mark.parametrize(
Expand All @@ -77,17 +77,19 @@ def test_server_run(
["fake.server.create_app_with_args"],
),
)
def test_server_run_factory_with_args(caplog, command):
def test_server_run_factory_with_args(caplog, command, port):
command.append(f"-p={port}")
lines = capture(command, caplog)

assert "target=fake.server.create_app_with_args" in lines


def test_server_run_factory_with_args_arbitrary(caplog):
def test_server_run_factory_with_args_arbitrary(caplog, port):
command = [
"fake.server.create_app_with_args",
"--factory",
"--foo=bar",
f"-p={port}",
]
lines = capture(command, caplog)

Expand All @@ -112,16 +114,16 @@ def test_server_run_factory_with_args_arbitrary(caplog):
),
),
)
def test_tls_options(cmd: Tuple[str, ...], caplog):
def test_tls_options(cmd: Tuple[str, ...], caplog, port):
command = [
"fake.server.app",
*cmd,
"--port=9999",
f"--port={port}",
"--debug",
"--single-process",
]
lines = capture(command, caplog)
assert "Goin' Fast @ https://127.0.0.1:9999" in lines
assert f"Goin' Fast @ https://127.0.0.1:{port}" in lines


@pytest.mark.parametrize(
Expand All @@ -136,8 +138,8 @@ def test_tls_options(cmd: Tuple[str, ...], caplog):
("--tls-strict-host",),
),
)
def test_tls_wrong_options(cmd: Tuple[str, ...], caplog):
command = ["fake.server.app", *cmd, "-p=9999", "--debug"]
def test_tls_wrong_options(cmd: Tuple[str, ...], caplog, port):
command = ["fake.server.app", *cmd, f"-p={port}", "--debug"]
lines = capture(command, caplog)

assert (
Expand All @@ -150,14 +152,15 @@ def test_tls_wrong_options(cmd: Tuple[str, ...], caplog):
@pytest.mark.parametrize(
"cmd",
(
("--host=localhost", "--port=9999"),
("-H", "localhost", "-p", "9999"),
("--host=localhost", "--port={port}"),
("-H", "localhost", "-p", "{port}"),
),
)
def test_host_port_localhost(cmd: Tuple[str, ...], caplog):
def test_host_port_localhost(cmd: Tuple[str, ...], caplog, port):
cmd = [c.format(port=str(port)) for c in cmd]
command = ["fake.server.app", *cmd]
lines = capture(command, caplog)
expected = "Goin' Fast @ http://localhost:9999"
expected = f"Goin' Fast @ http://localhost:{port}"

assert expected in lines

Expand All @@ -166,28 +169,30 @@ def test_host_port_localhost(cmd: Tuple[str, ...], caplog):
"cmd,expected",
(
(
("--host=localhost", "--port=9999"),
"Goin' Fast @ http://localhost:9999",
("--host=localhost", "--port={port}"),
"Goin' Fast @ http://localhost:{port}",
),
(
("-H", "localhost", "-p", "9999"),
"Goin' Fast @ http://localhost:9999",
("-H", "localhost", "-p", "{port}"),
"Goin' Fast @ http://localhost:{port}",
),
(
("--host=127.0.0.127", "--port=9999"),
"Goin' Fast @ http://127.0.0.127:9999",
("--host=127.0.0.1", "--port={port}"),
"Goin' Fast @ http://127.0.0.1:{port}",
),
(
("-H", "127.0.0.127", "-p", "9999"),
"Goin' Fast @ http://127.0.0.127:9999",
("-H", "127.0.0.1", "-p", "{port}"),
"Goin' Fast @ http://127.0.0.1:{port}",
),
(("--host=::", "--port=9999"), "Goin' Fast @ http://[::]:9999"),
(("-H", "::", "-p", "9999"), "Goin' Fast @ http://[::]:9999"),
(("--host=::1", "--port=9999"), "Goin' Fast @ http://[::1]:9999"),
(("-H", "::1", "-p", "9999"), "Goin' Fast @ http://[::1]:9999"),
(("--host=::", "--port={port}"), "Goin' Fast @ http://[::]:{port}"),
(("-H", "::", "-p", "{port}"), "Goin' Fast @ http://[::]:{port}"),
(("--host=::1", "--port={port}"), "Goin' Fast @ http://[::1]:{port}"),
(("-H", "::1", "-p", "{port}"), "Goin' Fast @ http://[::1]:{port}"),
),
)
def test_host_port(cmd: Tuple[str, ...], expected: str, caplog):
def test_host_port(cmd: Tuple[str, ...], expected: str, caplog, port):
cmd = [c.format(port=str(port)) for c in cmd]
expected = expected.format(port=str(port))
command = ["fake.server.app", *cmd]
lines = capture(command, caplog)

Expand All @@ -205,8 +210,8 @@ def test_host_port(cmd: Tuple[str, ...], expected: str, caplog):
(4, ("-w", "4")),
),
)
def test_num_workers(num: int, cmd: Tuple[str, ...], caplog):
command = ["fake.server.app", *cmd]
def test_num_workers(num: int, cmd: Tuple[str, ...], caplog, port):
command = ["fake.server.app", *cmd, f"-p={port}"]
lines = capture(command, caplog)

if num == 1:
Expand All @@ -218,8 +223,8 @@ def test_num_workers(num: int, cmd: Tuple[str, ...], caplog):


@pytest.mark.parametrize("cmd", ("--debug",))
def test_debug(cmd: str, caplog):
command = ["fake.server.app", cmd]
def test_debug(cmd: str, caplog, port):
command = ["fake.server.app", cmd, f"-p={port}"]
lines = capture(command, caplog)
info = read_app_info(lines)

Expand All @@ -228,8 +233,8 @@ def test_debug(cmd: str, caplog):


@pytest.mark.parametrize("cmd", ("--dev", "-d"))
def test_dev(cmd: str, caplog):
command = ["fake.server.app", cmd]
def test_dev(cmd: str, caplog, port):
command = ["fake.server.app", cmd, f"-p={port}"]
lines = capture(command, caplog)
info = read_app_info(lines)

Expand All @@ -238,13 +243,15 @@ def test_dev(cmd: str, caplog):


@pytest.mark.parametrize("cmd", ("--auto-reload", "-r"))
def test_auto_reload(cmd: str, caplog):
command = ["fake.server.app", cmd]
def test_auto_reload(cmd: str, caplog, port):
command = ["fake.server.app", cmd, f"-p={port}"]
lines = capture(command, caplog)
info = read_app_info(lines)

assert info["debug"] is False
assert info["auto_reload"] is True
assert info["debug"] is False, f"Unexpected value of debug {info}"
assert (
info["auto_reload"] is True
), f"Unexpected value of auto reload {info}"


@pytest.mark.parametrize(
Expand All @@ -256,14 +263,18 @@ def test_auto_reload(cmd: str, caplog):
("--no-access-log", False),
),
)
def test_access_logs(cmd: str, expected: bool, caplog):
command = ["fake.server.app"]
def test_access_logs(cmd: str, expected: bool, caplog, port):
command = ["fake.server.app", f"-p={port}"]
if cmd:
command.append(cmd)
lines = capture(command, caplog)
print(lines)
info = read_app_info(lines)

assert info["access_log"] is expected
if info["access_log"] != expected:
print(lines)
assert (
info["access_log"] is expected
), f"Expected: {expected}. Received: {info}. Lines: {lines}"


@pytest.mark.parametrize("cmd", ("--version", "-v"))
Expand All @@ -282,8 +293,8 @@ def test_version(cmd: str, caplog, capsys):
("--no-noisy-exceptions", False),
),
)
def test_noisy_exceptions(cmd: str, expected: bool, caplog):
command = ["fake.server.app", cmd]
def test_noisy_exceptions(cmd: str, expected: bool, caplog, port):
command = ["fake.server.app", cmd, f"-p={port}"]
lines = capture(command, caplog)
info = read_app_info(lines)

Expand Down Expand Up @@ -346,10 +357,7 @@ def test_server_run_with_repl(caplog, capsys):
)

def run():
command = [
"fake.server.app",
"--repl",
]
command = ["fake.server.app", "--repl", f"-p={get_port()}"]
return capture(command, capsys=capsys)

with patch("sanic.cli.app.is_atty", return_value=True):
Expand All @@ -359,5 +367,5 @@ def run():
assert "Welcome to the Sanic interactive console" in result.err
assert ">>> " in result.out

result = run()
run()
assert record in caplog.record_tuples
6 changes: 4 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from sanic.constants import LocalCertCreator
from sanic.exceptions import PyFileError

from .conftest import get_port


@contextmanager
def temp_path():
Expand Down Expand Up @@ -309,12 +311,12 @@ async def _request(sanic, loop):
app.stop()

await app.create_server(
port=1341, access_log=False, return_asyncio_server=True
port=get_port(), access_log=False, return_asyncio_server=True
)
assert app.config.ACCESS_LOG is False

await app.create_server(
port=1342, access_log=True, return_asyncio_server=True
port=get_port(), access_log=True, return_asyncio_server=True
)
assert app.config.ACCESS_LOG is True

Expand Down
16 changes: 10 additions & 6 deletions tests/test_server_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import pytest

from sanic_testing.testing import HOST, PORT
from sanic_testing.testing import HOST

from sanic import Blueprint
from sanic.exceptions import BadRequest, SanicException

from .conftest import get_port


AVAILABLE_LISTENERS = [
"before_server_start",
Expand Down Expand Up @@ -43,7 +45,9 @@ async def shutdown(app):
app.stop()

try:
random_name_app.run(HOST, PORT, single_process=True, **run_kwargs)
random_name_app.run(
HOST, get_port(), single_process=True, **run_kwargs
)
except KeyboardInterrupt:
pass

Expand Down Expand Up @@ -104,7 +108,7 @@ def test_all_listeners_as_convenience(app):


@pytest.mark.asyncio
async def test_trigger_before_events_create_server(app):
async def test_trigger_before_events_create_server(app, port):
class MySanicDb:
pass

Expand All @@ -113,7 +117,7 @@ async def init_db(app, loop):
app.ctx.db = MySanicDb()

srv = await app.create_server(
debug=True, return_asyncio_server=True, port=PORT
debug=True, return_asyncio_server=True, port=port
)
await srv.startup()
await srv.before_start()
Expand Down Expand Up @@ -198,13 +202,13 @@ async def after_stop(app, loop):


@pytest.mark.asyncio
async def test_missing_startup_raises_exception(app):
async def test_missing_startup_raises_exception(app, port):
@app.listener("before_server_start")
async def init_db(app, loop):
...

srv = await app.create_server(
debug=True, return_asyncio_server=True, port=PORT
debug=True, return_asyncio_server=True, port=port
)

with pytest.raises(SanicException):
Expand Down

0 comments on commit 1a70930

Please sign in to comment.