Files
gns3-server/tests/test_server.py
T
YueGuobin 52c19002f2 fix: bound graceful shutdown and stop notification WebSockets on exit
By default uvicorn waits for open connections forever. A long-lived
streaming response keeps the server waiting after Ctrl+C. One common
case is the MCP Streamable HTTP transport: the client opens a
standalone GET request and the server answers with an SSE stream
(not the old SSE transport). A second Ctrl+C sets force_exit. Uvicorn
then skips the lifespan shutdown, so project and controller cleanup
does not run.

Add timeout_graceful_shutdown. After the timeout uvicorn cancels the
remaining tasks and runs the lifespan shutdown. Also make the four
notification WebSocket handlers stop when app.state.exiting is set.
The HTTP stream handlers already do this.
2026-11-04 00:32:34 +08:00

162 lines
5.2 KiB
Python

#!/usr/bin/env python
#
# Copyright (C) 2015 GNS3 Technologies Inc.
#
# This program 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 program 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 this program. If not, see <http://www.gnu.org/licenses/>.
import locale
import tempfile
import pytest
from gns3server.config import Config
from gns3server.main import parse_arguments as parse
from gns3server.server import GRACEFUL_SHUTDOWN_TIMEOUT, Server
def test_uvicorn_config_bounds_graceful_shutdown(config):
uvicorn_config = Server._create_uvicorn_config(None, "127.0.0.1", 3080, False)
assert uvicorn_config.timeout_graceful_shutdown == GRACEFUL_SHUTDOWN_TIMEOUT
def test_locale_check():
try:
locale.setlocale(locale.LC_ALL, ("fr_FR", "UTF-8"))
except: # Locale is not available on the server
return
Server._locale_check()
assert locale.getlocale() == ("fr_FR", "UTF-8")
def parse_arguments(server, argv):
parser, args = parse(argv)
return server._load_config_and_set_defaults(parser, args, argv)
def test_parse_arguments(capsys, config, tmpdir):
server = Server()
server_config = config.settings.Server
with pytest.raises(SystemExit):
parse_arguments(server, ["--fail"])
out, err = capsys.readouterr()
assert "usage" in err
assert "fail" in err
assert "unrecognized arguments" in err
# with pytest.raises(SystemExit):
# run.parse_arguments(["-v"])
# out, _ = capsys.readouterr()
# assert __version__ in out
# with pytest.raises(SystemExit):
# run.parse_arguments(["--version"])
# out, _ = capsys.readouterr()
# assert __version__ in out
#
# with pytest.raises(SystemExit):
# run.parse_arguments(["-h"])
# out, _ = capsys.readouterr()
# assert __version__ in out
# assert "optional arguments" in out
#
# with pytest.raises(SystemExit):
# run.parse_arguments(["--help"])
# out, _ = capsys.readouterr()
# assert __version__ in out
# assert "optional arguments" in out
assert parse_arguments(server, ["--host", "192.168.1.1"]).host == "192.168.1.1"
assert parse_arguments(server, []).host == "0.0.0.0"
server_config.host = "192.168.1.2"
assert parse_arguments(server, ["--host", "192.168.1.1"]).host == "192.168.1.1"
assert parse_arguments(server, []).host == "192.168.1.2"
assert parse_arguments(server, ["--port", "8002"]).port == 8002
assert parse_arguments(server, []).port == 3080
server_config.port = 8003
assert parse_arguments(server, []).port == 8003
assert parse_arguments(server, ["--ssl"]).ssl
assert parse_arguments(server, []).ssl is False
with tempfile.NamedTemporaryFile(dir=str(tmpdir)) as f:
server_config.certfile = f.name
server_config.certkey = f.name
server_config.enable_ssl = True
assert parse_arguments(server, []).ssl
assert parse_arguments(server, ["--certfile", "bla"]).certfile == "bla"
assert parse_arguments(server, ["--certkey", "blu"]).certkey == "blu"
assert parse_arguments(server, ["-L"]).local
assert parse_arguments(server, ["--local"]).local
server_config.local = False
assert parse_arguments(server, []).local is False
server_config.local = True
assert parse_arguments(server, []).local
assert parse_arguments(server, ["-A"]).allow
assert parse_arguments(server, ["--allow"]).allow
assert parse_arguments(server, []).allow is False
server_config.allow_remote_console = True
assert parse_arguments(server, []).allow
assert parse_arguments(server, ["-q"]).quiet
assert parse_arguments(server, ["--quiet"]).quiet
assert parse_arguments(server, []).quiet is False
assert parse_arguments(server, ["-d"]).debug
assert parse_arguments(server, ["--debug"]).debug
assert parse_arguments(server, []).debug is False
def test_set_config_with_args(tmpdir):
server = Server()
config = Config.instance()
with tempfile.NamedTemporaryFile(dir=str(tmpdir)) as f:
certfile = f.name
certkey = f.name
args = parse_arguments(
server,
[
"--host",
"192.168.1.1",
"--local",
"--allow",
"--port",
"8001",
"--ssl",
"--certfile",
certfile,
"--certkey",
certkey,
"--debug",
],
)
server._set_config_defaults_from_command_line(args)
server_config = config.settings.Server
assert server_config.local
assert server_config.allow_remote_console
assert server_config.host
assert server_config.port
assert server_config.enable_ssl
assert server_config.certfile
assert server_config.certkey