From ed39afbf3d48df6ead0870dbd26f0590f656bdb5 Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 6 May 2015 10:40:51 +0200 Subject: [PATCH] Throw an error if user put an invalid port range in config file Fix #117 --- gns3server/modules/port_manager.py | 2 +- gns3server/modules/project.py | 2 +- tests/modules/test_port_manager.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gns3server/modules/port_manager.py b/gns3server/modules/port_manager.py index b0f882d8..76248cd7 100644 --- a/gns3server/modules/port_manager.py +++ b/gns3server/modules/port_manager.py @@ -140,7 +140,7 @@ class PortManager: """ if end_port < start_port: - raise Exception("Invalid port range {}-{}".format(start_port, end_port)) + raise HTTPConflict(text="Invalid port range {}-{}".format(start_port, end_port)) if socket_type == "UDP": socket_type = socket.SOCK_DGRAM diff --git a/gns3server/modules/project.py b/gns3server/modules/project.py index 02d82d30..c162e73e 100644 --- a/gns3server/modules/project.py +++ b/gns3server/modules/project.py @@ -152,7 +152,7 @@ class Project: try: shutil.rmtree(old_path) except OSError as e: - raise aiohttp.web.HTTPConflict("Can't remove temporary directory {}: {}".format(old_path, e)) + raise aiohttp.web.HTTPConflict(text="Can't remove temporary directory {}: {}".format(old_path, e)) @property def name(self): diff --git a/tests/modules/test_port_manager.py b/tests/modules/test_port_manager.py index 05a2ac62..2f3b791e 100644 --- a/tests/modules/test_port_manager.py +++ b/tests/modules/test_port_manager.py @@ -45,3 +45,13 @@ def test_release_udp_port(): pm.reserve_udp_port(4242, project) pm.release_udp_port(4242, project) pm.reserve_udp_port(4242, project) + + +def test_find_unused_port(): + p = PortManager().find_unused_port(1000, 10000) + assert p is not None + + +def test_find_unused_port_invalid_range(): + with pytest.raises(aiohttp.web.HTTPConflict): + p = PortManager().find_unused_port(10000, 1000)