diff --git a/gns3server/utils/interfaces.py b/gns3server/utils/interfaces.py index 85d6d1bd..e9aa30b3 100644 --- a/gns3server/utils/interfaces.py +++ b/gns3server/utils/interfaces.py @@ -20,6 +20,7 @@ import sys import aiohttp import socket import struct +import netifaces import logging log = logging.getLogger(__name__) @@ -89,6 +90,9 @@ def is_interface_up(interface): :returns: boolean """ + if interface not in netifaces.interfaces(): + return False + if sys.platform.startswith("linux"): import fcntl SIOCGIFFLAGS = 0x8913 @@ -100,8 +104,7 @@ def is_interface_up(interface): return True return False except OSError as e: - raise e - #raise aiohttp.web.HTTPInternalServerError(text="Exception when checking if {} is up: {}".format(interface, e)) + raise aiohttp.web.HTTPInternalServerError(text="Exception when checking if {} is up: {}".format(interface, e)) else: # TODO: Windows & OSX support return True @@ -116,13 +119,9 @@ def interfaces(): results = [] if not sys.platform.startswith("win"): - try: - import netifaces - for interface in netifaces.interfaces(): - results.append({"id": interface, - "name": interface}) - except ImportError: - raise aiohttp.web.HTTPInternalServerError(text="Could not import netifaces module") + for interface in netifaces.interfaces(): + results.append({"id": interface, + "name": interface}) else: try: results = get_windows_interfaces() diff --git a/tests/conftest.py b/tests/conftest.py index 5e49a7da..9942e4ad 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -128,6 +128,12 @@ def free_console_port(request, port_manager, project): return port +@pytest.fixture +def ethernet_device(): + import netifaces + return netifaces.interfaces()[0] + + @pytest.yield_fixture(autouse=True) def run_around_tests(monkeypatch): """ diff --git a/tests/handlers/api/test_iou.py b/tests/handlers/api/test_iou.py index 743a0790..de6e0a22 100644 --- a/tests/handlers/api/test_iou.py +++ b/tests/handlers/api/test_iou.py @@ -204,32 +204,32 @@ def test_iou_nio_create_udp(server, vm): assert response.json["type"] == "nio_udp" -def test_iou_nio_create_ethernet(server, vm): +def test_iou_nio_create_ethernet(server, vm, ethernet_device): response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet", - "ethernet_device": "eth0", + "ethernet_device": ethernet_device, }, example=True) assert response.status == 201 assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio" assert response.json["type"] == "nio_generic_ethernet" - assert response.json["ethernet_device"] == "eth0" + assert response.json["ethernet_device"] == ethernet_device -def test_iou_nio_create_ethernet_different_port(server, vm): +def test_iou_nio_create_ethernet_different_port(server, vm, ethernet_device): response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/0/ports/3/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_generic_ethernet", - "ethernet_device": "eth0", + "ethernet_device": ethernet_device, }, example=False) assert response.status == 201 assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio" assert response.json["type"] == "nio_generic_ethernet" - assert response.json["ethernet_device"] == "eth0" + assert response.json["ethernet_device"] == ethernet_device -def test_iou_nio_create_tap(server, vm): +def test_iou_nio_create_tap(server, vm, ethernet_device): with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=True): response = server.post("/projects/{project_id}/iou/vms/{vm_id}/adapters/1/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_tap", - "tap_device": "test"}) + "tap_device": ethernet_device}) assert response.status == 201 assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio" assert response.json["type"] == "nio_tap" diff --git a/tests/handlers/api/test_vpcs.py b/tests/handlers/api/test_vpcs.py index 736fb953..dc93d565 100644 --- a/tests/handlers/api/test_vpcs.py +++ b/tests/handlers/api/test_vpcs.py @@ -76,10 +76,10 @@ def test_vpcs_nio_create_udp(server, vm): assert response.json["type"] == "nio_udp" -def test_vpcs_nio_create_tap(server, vm): +def test_vpcs_nio_create_tap(server, vm, ethernet_device): with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=True): response = server.post("/projects/{project_id}/vpcs/vms/{vm_id}/adapters/0/ports/0/nio".format(project_id=vm["project_id"], vm_id=vm["vm_id"]), {"type": "nio_tap", - "tap_device": "test"}) + "tap_device": ethernet_device}) assert response.status == 201 assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio" assert response.json["type"] == "nio_tap" diff --git a/tests/modules/qemu/test_qemu_vm.py b/tests/modules/qemu/test_qemu_vm.py index b03bb428..01b9b6f6 100644 --- a/tests/modules/qemu/test_qemu_vm.py +++ b/tests/modules/qemu/test_qemu_vm.py @@ -136,11 +136,11 @@ def test_add_nio_binding_udp(vm, loop): assert nio.lport == 4242 -def test_add_nio_binding_ethernet(vm, loop): +def test_add_nio_binding_ethernet(vm, loop, ethernet_device): with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=True): - nio = Qemu.instance().create_nio(vm.qemu_path, {"type": "nio_generic_ethernet", "ethernet_device": "eth0"}) + nio = Qemu.instance().create_nio(vm.qemu_path, {"type": "nio_generic_ethernet", "ethernet_device": ethernet_device}) loop.run_until_complete(asyncio.async(vm.adapter_add_nio_binding(0, nio))) - assert nio.ethernet_device == "eth0" + assert nio.ethernet_device == ethernet_device def test_port_remove_nio_binding(vm, loop): diff --git a/tests/modules/vpcs/test_vpcs_vm.py b/tests/modules/vpcs/test_vpcs_vm.py index 416147ee..3fb363f1 100644 --- a/tests/modules/vpcs/test_vpcs_vm.py +++ b/tests/modules/vpcs/test_vpcs_vm.py @@ -150,9 +150,9 @@ def test_add_nio_binding_udp(vm): assert nio.lport == 4242 -def test_add_nio_binding_tap(vm): +def test_add_nio_binding_tap(vm, ethernet_device): with patch("gns3server.modules.base_manager.BaseManager._has_privileged_access", return_value=True): - nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_tap", "tap_device": "test"}) + nio = VPCS.instance().create_nio(vm.vpcs_path, {"type": "nio_tap", "tap_device": ethernet_device}) vm.port_add_nio_binding(0, nio) assert nio.tap_device == "test" diff --git a/tests/utils/test_interfaces.py b/tests/utils/test_interfaces.py index e712e4a3..401b0de1 100644 --- a/tests/utils/test_interfaces.py +++ b/tests/utils/test_interfaces.py @@ -15,6 +15,8 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +import sys +import netifaces from gns3server.utils.interfaces import interfaces, is_interface_up @@ -25,6 +27,10 @@ def test_interfaces(): def test_is_interface_up(): - assert is_interface_up("eth0") is True - - + if sys.platform.startswith("win"): + assert is_interface_up(netifaces.interfaces[0]) is True + elif sys.platform.startswith("darwin"): + assert is_interface_up("lo0") is True + else: + assert is_interface_up("lo") is True + assert is_interface_up("fake0") is False