1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-12-01 04:38:12 +00:00

Fix tests and rename path to iou_path

This commit is contained in:
Julien Duponchelle 2015-02-12 15:20:47 +01:00
parent fb69c693f6
commit ebc214d6fa
5 changed files with 68 additions and 61 deletions

View File

@ -50,7 +50,7 @@ class IOUHandler:
request.json.get("vm_id"), request.json.get("vm_id"),
console=request.json.get("console"), console=request.json.get("console"),
) )
vm.iou_path = request.json.get("iou_path", vm.iou_path) vm.path = request.json.get("path", vm.path)
vm.iourc_path = request.json.get("iourc_path", vm.iourc_path) vm.iourc_path = request.json.get("iourc_path", vm.iourc_path)
response.set_status(201) response.set_status(201)
response.json(vm) response.json(vm)
@ -97,7 +97,7 @@ class IOUHandler:
vm = iou_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"]) vm = iou_manager.get_vm(request.match_info["vm_id"], project_id=request.match_info["project_id"])
vm.name = request.json.get("name", vm.name) vm.name = request.json.get("name", vm.name)
vm.console = request.json.get("console", vm.console) vm.console = request.json.get("console", vm.console)
vm.iou_path = request.json.get("iou_path", vm.iou_path) vm.path = request.json.get("path", vm.path)
vm.iourc_path = request.json.get("iourc_path", vm.iourc_path) vm.iourc_path = request.json.get("iourc_path", vm.iourc_path)
response.json(vm) response.json(vm)

View File

@ -69,7 +69,7 @@ class IOUVM(BaseVM):
self._iou_process = None self._iou_process = None
self._iou_stdout_file = "" self._iou_stdout_file = ""
self._started = False self._started = False
self._iou_path = None self._path = None
self._iourc_path = None self._iourc_path = None
self._ioucon_thread = None self._ioucon_thread = None
self._console_host = console_host self._console_host = console_host
@ -96,40 +96,40 @@ class IOUVM(BaseVM):
self._console = None self._console = None
@property @property
def iou_path(self): def path(self):
"""Path of the iou binary""" """Path of the iou binary"""
return self._iou_path return self._path
@iou_path.setter @path.setter
def iou_path(self, path): def path(self, path):
""" """
Path of the iou binary Path of the iou binary
:params path: Path to the binary :params path: Path to the binary
""" """
self._iou_path = path self._path = path
if not os.path.isfile(self._iou_path) or not os.path.exists(self._iou_path): if not os.path.isfile(self._path) or not os.path.exists(self._path):
if os.path.islink(self._iou_path): if os.path.islink(self._path):
raise IOUError("IOU image '{}' linked to '{}' is not accessible".format(self._iou_path, os.path.realpath(self._iou_path))) raise IOUError("IOU image '{}' linked to '{}' is not accessible".format(self._path, os.path.realpath(self._path)))
else: else:
raise IOUError("IOU image '{}' is not accessible".format(self._iou_path)) raise IOUError("IOU image '{}' is not accessible".format(self._path))
try: try:
with open(self._iou_path, "rb") as f: with open(self._path, "rb") as f:
# read the first 7 bytes of the file. # read the first 7 bytes of the file.
elf_header_start = f.read(7) elf_header_start = f.read(7)
except OSError as e: except OSError as e:
raise IOUError("Cannot read ELF header for IOU image '{}': {}".format(self._iou_path, e)) raise IOUError("Cannot read ELF header for IOU image '{}': {}".format(self._path, e))
# IOU images must start with the ELF magic number, be 32-bit, little endian # IOU images must start with the ELF magic number, be 32-bit, little endian
# and have an ELF version of 1 normal IOS image are big endian! # and have an ELF version of 1 normal IOS image are big endian!
if elf_header_start != b'\x7fELF\x01\x01\x01': if elf_header_start != b'\x7fELF\x01\x01\x01':
raise IOUError("'{}' is not a valid IOU image".format(self._iou_path)) raise IOUError("'{}' is not a valid IOU image".format(self._path))
if not os.access(self._iou_path, os.X_OK): if not os.access(self._path, os.X_OK):
raise IOUError("IOU image '{}' is not executable".format(self._iou_path)) raise IOUError("IOU image '{}' is not executable".format(self._path))
@property @property
def iourc_path(self): def iourc_path(self):
@ -194,7 +194,7 @@ class IOUVM(BaseVM):
"console": self._console, "console": self._console,
"project_id": self.project.id, "project_id": self.project.id,
"iourc_path": self._iourc_path, "iourc_path": self._iourc_path,
"iou_path": self.iou_path "path": self.path
} }
@property @property
@ -245,7 +245,7 @@ class IOUVM(BaseVM):
""" """
try: try:
output = subprocess.check_output(["ldd", self._iou_path]) output = subprocess.check_output(["ldd", self._path])
except (FileNotFoundError, subprocess.SubprocessError) as e: except (FileNotFoundError, subprocess.SubprocessError) as e:
log.warn("could not determine the shared library dependencies for {}: {}".format(self._path, e)) log.warn("could not determine the shared library dependencies for {}: {}".format(self._path, e))
return return
@ -296,8 +296,8 @@ class IOUVM(BaseVM):
raise IOUError("could not start IOU: {}: 32-bit binary support is probably not installed".format(e)) raise IOUError("could not start IOU: {}: 32-bit binary support is probably not installed".format(e))
except (OSError, subprocess.SubprocessError) as e: except (OSError, subprocess.SubprocessError) as e:
iou_stdout = self.read_iou_stdout() iou_stdout = self.read_iou_stdout()
log.error("could not start IOU {}: {}\n{}".format(self._iou_path, e, iou_stdout)) log.error("could not start IOU {}: {}\n{}".format(self._path, e, iou_stdout))
raise IOUError("could not start IOU {}: {}\n{}".format(self._iou_path, e, iou_stdout)) raise IOUError("could not start IOU {}: {}\n{}".format(self._path, e, iou_stdout))
# start console support # start console support
self._start_ioucon() self._start_ioucon()
@ -412,13 +412,14 @@ class IOUVM(BaseVM):
self._iou_process = None self._iou_process = None
self._terminate_process_iouyap() if self._iouyap_process is not None:
try: self._terminate_process_iouyap()
yield from asyncio.wait_for(self._iouyap_process.wait(), timeout=3) try:
except asyncio.TimeoutError: yield from asyncio.wait_for(self._iouyap_process.wait(), timeout=3)
self._iou_process.kill() except asyncio.TimeoutError:
if self._iouyap_process.returncode is None: self._iou_process.kill()
log.warn("IOUYAP process {} is still running".format(self._iou_process.pid)) if self._iouyap_process.returncode is None:
log.warn("IOUYAP process {} is still running".format(self._iou_process.pid))
self._started = False self._started = False
@ -512,7 +513,7 @@ class IOUVM(BaseVM):
-N Ignore the NETMAP file -N Ignore the NETMAP file
""" """
command = [self._iou_path] command = [self._path]
if len(self._ethernet_adapters) != 2: if len(self._ethernet_adapters) != 2:
command.extend(["-e", str(len(self._ethernet_adapters))]) command.extend(["-e", str(len(self._ethernet_adapters))])
if len(self._serial_adapters) != 2: if len(self._serial_adapters) != 2:

View File

@ -42,7 +42,7 @@ IOU_CREATE_SCHEMA = {
"maximum": 65535, "maximum": 65535,
"type": ["integer", "null"] "type": ["integer", "null"]
}, },
"iou_path": { "path": {
"description": "Path of iou binary", "description": "Path of iou binary",
"type": "string" "type": "string"
}, },
@ -52,7 +52,7 @@ IOU_CREATE_SCHEMA = {
}, },
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["name", "iou_path", "iourc_path"] "required": ["name", "path", "iourc_path"]
} }
IOU_UPDATE_SCHEMA = { IOU_UPDATE_SCHEMA = {
@ -71,7 +71,7 @@ IOU_UPDATE_SCHEMA = {
"maximum": 65535, "maximum": 65535,
"type": ["integer", "null"] "type": ["integer", "null"]
}, },
"iou_path": { "path": {
"description": "Path of iou binary", "description": "Path of iou binary",
"type": "string" "type": "string"
}, },
@ -113,7 +113,7 @@ IOU_OBJECT_SCHEMA = {
"maxLength": 36, "maxLength": 36,
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$" "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
}, },
"iou_path": { "path": {
"description": "Path of iou binary", "description": "Path of iou binary",
"type": "string" "type": "string"
}, },
@ -123,5 +123,5 @@ IOU_OBJECT_SCHEMA = {
}, },
}, },
"additionalProperties": False, "additionalProperties": False,
"required": ["name", "vm_id", "console", "project_id", "iou_path", "iourc_path"] "required": ["name", "vm_id", "console", "project_id", "path", "iourc_path"]
} }

View File

@ -36,7 +36,7 @@ def fake_iou_bin(tmpdir):
@pytest.fixture @pytest.fixture
def base_params(tmpdir, fake_iou_bin): def base_params(tmpdir, fake_iou_bin):
"""Return standard parameters""" """Return standard parameters"""
return {"name": "PC TEST 1", "iou_path": fake_iou_bin, "iourc_path": str(tmpdir / "iourc")} return {"name": "PC TEST 1", "path": fake_iou_bin, "iourc_path": str(tmpdir / "iourc")}
@pytest.fixture @pytest.fixture

View File

@ -47,8 +47,8 @@ def vm(project, manager, tmpdir, fake_iou_bin):
config["iouyap_path"] = fake_file config["iouyap_path"] = fake_file
manager.config.set_section_config("IOU", config) manager.config.set_section_config("IOU", config)
vm.iou_path = fake_iou_bin vm.path = fake_iou_bin
vm.iourc = fake_file vm.iourc_path = fake_file
return vm return vm
@ -76,11 +76,13 @@ def test_vm_invalid_iouyap_path(project, manager, loop):
loop.run_until_complete(asyncio.async(vm.start())) loop.run_until_complete(asyncio.async(vm.start()))
def test_start(loop, vm): def test_start(loop, vm, monkeypatch):
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
loop.run_until_complete(asyncio.async(vm.start())) with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
assert vm.is_running() with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running()
def test_stop(loop, vm): def test_stop(loop, vm):
@ -92,12 +94,14 @@ def test_stop(loop, vm):
process.wait.return_value = future process.wait.return_value = future
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
loop.run_until_complete(asyncio.async(vm.start())) with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
assert vm.is_running() with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
loop.run_until_complete(asyncio.async(vm.stop())) loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running() is False assert vm.is_running()
process.terminate.assert_called_with() loop.run_until_complete(asyncio.async(vm.stop()))
assert vm.is_running() is False
process.terminate.assert_called_with()
def test_reload(loop, vm, fake_iou_bin): def test_reload(loop, vm, fake_iou_bin):
@ -109,12 +113,14 @@ def test_reload(loop, vm, fake_iou_bin):
process.wait.return_value = future process.wait.return_value = future
with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._check_requirements", return_value=True):
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process): with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_ioucon", return_value=True):
loop.run_until_complete(asyncio.async(vm.start())) with asyncio_patch("gns3server.modules.iou.iou_vm.IOUVM._start_iouyap", return_value=True):
assert vm.is_running() with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
loop.run_until_complete(asyncio.async(vm.reload())) loop.run_until_complete(asyncio.async(vm.start()))
assert vm.is_running() is True assert vm.is_running()
process.terminate.assert_called_with() loop.run_until_complete(asyncio.async(vm.reload()))
assert vm.is_running() is True
process.terminate.assert_called_with()
def test_close(vm, port_manager): def test_close(vm, port_manager):
@ -128,23 +134,23 @@ def test_close(vm, port_manager):
assert vm.is_running() is False assert vm.is_running() is False
def test_iou_path(vm, fake_iou_bin): def test_path(vm, fake_iou_bin):
vm.iou_path = fake_iou_bin vm.path = fake_iou_bin
assert vm.iou_path == fake_iou_bin assert vm.path == fake_iou_bin
def test_path_invalid_bin(vm, tmpdir): def test_path_invalid_bin(vm, tmpdir):
iou_path = str(tmpdir / "test.bin") path = str(tmpdir / "test.bin")
with pytest.raises(IOUError): with pytest.raises(IOUError):
vm.iou_path = iou_path vm.path = path
with open(iou_path, "w+") as f: with open(path, "w+") as f:
f.write("BUG") f.write("BUG")
with pytest.raises(IOUError): with pytest.raises(IOUError):
vm.iou_path = iou_path vm.path = path
def test_create_netmap_config(vm): def test_create_netmap_config(vm):
@ -161,4 +167,4 @@ def test_create_netmap_config(vm):
def test_build_command(vm): def test_build_command(vm):
assert vm._build_command() == [vm.iou_path, '-L', str(vm.application_id)] assert vm._build_command() == [vm.path, '-L', str(vm.application_id)]