mirror of
https://github.com/GNS3/gns3-server
synced 2024-11-24 09:18:08 +00:00
Fix ethernet interfaces tests if eth0 doesn't exists
Also on Windows and MacOS it return error if we try to use a non existing interfaces. Fix #230
This commit is contained in:
parent
bd4b6396a8
commit
7c7f8fe3ed
@ -20,6 +20,7 @@ import sys
|
|||||||
import aiohttp
|
import aiohttp
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
|
import netifaces
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -89,6 +90,9 @@ def is_interface_up(interface):
|
|||||||
:returns: boolean
|
:returns: boolean
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if interface not in netifaces.interfaces():
|
||||||
|
return False
|
||||||
|
|
||||||
if sys.platform.startswith("linux"):
|
if sys.platform.startswith("linux"):
|
||||||
import fcntl
|
import fcntl
|
||||||
SIOCGIFFLAGS = 0x8913
|
SIOCGIFFLAGS = 0x8913
|
||||||
@ -100,8 +104,7 @@ def is_interface_up(interface):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
except OSError as e:
|
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:
|
else:
|
||||||
# TODO: Windows & OSX support
|
# TODO: Windows & OSX support
|
||||||
return True
|
return True
|
||||||
@ -116,13 +119,9 @@ def interfaces():
|
|||||||
|
|
||||||
results = []
|
results = []
|
||||||
if not sys.platform.startswith("win"):
|
if not sys.platform.startswith("win"):
|
||||||
try:
|
|
||||||
import netifaces
|
|
||||||
for interface in netifaces.interfaces():
|
for interface in netifaces.interfaces():
|
||||||
results.append({"id": interface,
|
results.append({"id": interface,
|
||||||
"name": interface})
|
"name": interface})
|
||||||
except ImportError:
|
|
||||||
raise aiohttp.web.HTTPInternalServerError(text="Could not import netifaces module")
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
results = get_windows_interfaces()
|
results = get_windows_interfaces()
|
||||||
|
@ -128,6 +128,12 @@ def free_console_port(request, port_manager, project):
|
|||||||
return port
|
return port
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def ethernet_device():
|
||||||
|
import netifaces
|
||||||
|
return netifaces.interfaces()[0]
|
||||||
|
|
||||||
|
|
||||||
@pytest.yield_fixture(autouse=True)
|
@pytest.yield_fixture(autouse=True)
|
||||||
def run_around_tests(monkeypatch):
|
def run_around_tests(monkeypatch):
|
||||||
"""
|
"""
|
||||||
|
@ -204,32 +204,32 @@ def test_iou_nio_create_udp(server, vm):
|
|||||||
assert response.json["type"] == "nio_udp"
|
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",
|
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)
|
example=True)
|
||||||
assert response.status == 201
|
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.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["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",
|
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)
|
example=False)
|
||||||
assert response.status == 201
|
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.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["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):
|
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",
|
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.status == 201
|
||||||
assert response.route == "/projects/{project_id}/iou/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
|
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"
|
assert response.json["type"] == "nio_tap"
|
||||||
|
@ -76,10 +76,10 @@ def test_vpcs_nio_create_udp(server, vm):
|
|||||||
assert response.json["type"] == "nio_udp"
|
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):
|
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",
|
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.status == 201
|
||||||
assert response.route == "/projects/{project_id}/vpcs/vms/{vm_id}/adapters/{adapter_number:\d+}/ports/{port_number:\d+}/nio"
|
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"
|
assert response.json["type"] == "nio_tap"
|
||||||
|
@ -136,11 +136,11 @@ def test_add_nio_binding_udp(vm, loop):
|
|||||||
assert nio.lport == 4242
|
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):
|
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)))
|
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):
|
def test_port_remove_nio_binding(vm, loop):
|
||||||
|
@ -150,9 +150,9 @@ def test_add_nio_binding_udp(vm):
|
|||||||
assert nio.lport == 4242
|
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):
|
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)
|
vm.port_add_nio_binding(0, nio)
|
||||||
assert nio.tap_device == "test"
|
assert nio.tap_device == "test"
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import netifaces
|
||||||
|
|
||||||
from gns3server.utils.interfaces import interfaces, is_interface_up
|
from gns3server.utils.interfaces import interfaces, is_interface_up
|
||||||
|
|
||||||
@ -25,6 +27,10 @@ def test_interfaces():
|
|||||||
|
|
||||||
|
|
||||||
def test_is_interface_up():
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user