Test all IOU requirements at VM startup

This allow user to create the node and upload the image after
This commit is contained in:
Julien Duponchelle 2015-06-19 16:35:19 +02:00
parent 5c4a49a8c7
commit 4aadfa3b67
2 changed files with 27 additions and 23 deletions

View File

@ -142,27 +142,6 @@ class IOUVM(BaseVM):
if os.path.isfile(fix_path):
self._path = fix_path
if not os.path.isfile(self._path) or not os.path.exists(self._path):
if os.path.islink(self._path):
raise IOUError("IOU image '{}' linked to '{}' is not accessible".format(self._path, os.path.realpath(self._path)))
else:
raise IOUError("IOU image '{}' is not accessible".format(self._path))
try:
with open(self._path, "rb") as f:
# read the first 7 bytes of the file.
elf_header_start = f.read(7)
except OSError as 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
# and have an ELF version of 1 normal IOS image are big endian!
if elf_header_start != b'\x7fELF\x01\x01\x01':
raise IOUError("'{}' is not a valid IOU image".format(self._path))
if not os.access(self._path, os.X_OK):
raise IOUError("IOU image '{}' is not executable".format(self._path))
@property
def use_default_iou_values(self):
"""
@ -189,9 +168,30 @@ class IOUVM(BaseVM):
def _check_requirements(self):
"""
Checks if IOUYAP executable is available.
Checks if IOUYAP executable is available and if image is accessible.
"""
if not os.path.isfile(self._path) or not os.path.exists(self._path):
if os.path.islink(self._path):
raise IOUError("IOU image '{}' linked to '{}' is not accessible".format(self._path, os.path.realpath(self._path)))
else:
raise IOUError("IOU image '{}' is not accessible".format(self._path))
try:
with open(self._path, "rb") as f:
# read the first 7 bytes of the file.
elf_header_start = f.read(7)
except OSError as 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
# and have an ELF version of 1 normal IOS image are big endian!
if elf_header_start != b'\x7fELF\x01\x01\x01':
raise IOUError("'{}' is not a valid IOU image".format(self._path))
if not os.access(self._path, os.X_OK):
raise IOUError("IOU image '{}' is not executable".format(self._path))
path = self.iouyap_path
if not path:
raise IOUError("No path to iouyap program has been set")

View File

@ -96,9 +96,10 @@ def test_vm_startup_config_content(project, manager):
@patch("gns3server.config.Config.get_section_config", return_value={"iouyap_path": "/bin/test_fake"})
def test_vm_invalid_iouyap_path(project, manager, loop):
def test_vm_invalid_iouyap_path(project, manager, loop, fake_iou_bin):
with pytest.raises(IOUError):
vm = IOUVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0e", project, manager)
vm.path = fake_iou_bin
loop.run_until_complete(asyncio.async(vm.start()))
@ -231,12 +232,14 @@ def test_path_invalid_bin(vm, tmpdir):
path = str(tmpdir / "test.bin")
with pytest.raises(IOUError):
vm.path = path
vm._check_requirements()
with open(path, "w+") as f:
f.write("BUG")
with pytest.raises(IOUError):
vm.path = path
vm._check_requirements()
def test_create_netmap_config(vm):
@ -255,6 +258,7 @@ def test_build_command(vm, loop):
assert loop.run_until_complete(asyncio.async(vm._build_command())) == [vm.path, "-L", str(vm.application_id)]
def test_get_startup_config(vm):
content = "service timestamps debug datetime msec\nservice timestamps log datetime msec\nno service password-encryption"