63 lines
1.4 KiB
Python
63 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import socket
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
|
|
from ssl_check import Target, main, run_check # noqa: E402
|
|
|
|
|
|
def unused_local_port() -> int:
|
|
sock = socket.socket()
|
|
sock.bind(("127.0.0.1", 0))
|
|
port = int(sock.getsockname()[1])
|
|
sock.close()
|
|
return port
|
|
|
|
|
|
def test_ssl_check_unreachable_returns_stable_json() -> None:
|
|
target = Target(
|
|
name="Unreachable",
|
|
host="127.0.0.1",
|
|
port=unused_local_port(),
|
|
owner="tests",
|
|
profile="relaxed",
|
|
expected_hostname="127.0.0.1",
|
|
timeout=0.5,
|
|
)
|
|
|
|
result = run_check(target)
|
|
|
|
assert result["reachable"] is False
|
|
assert result["error"]
|
|
assert result["target"] == "127.0.0.1"
|
|
assert "warnings" in result
|
|
|
|
|
|
def test_main_prints_json_for_unreachable_target(capsys) -> None: # type: ignore[no-untyped-def]
|
|
port = unused_local_port()
|
|
|
|
exit_code = main(
|
|
[
|
|
"--host",
|
|
"127.0.0.1",
|
|
"--port",
|
|
str(port),
|
|
"--expected-hostname",
|
|
"127.0.0.1",
|
|
"--timeout",
|
|
"0.5",
|
|
]
|
|
)
|
|
|
|
captured = capsys.readouterr()
|
|
payload = json.loads(captured.out)
|
|
|
|
assert exit_code == 0
|
|
assert payload["reachable"] is False
|
|
assert payload["error"]
|