1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-10-31 20:58:56 +00:00

Fix iou tests and add tests

This commit is contained in:
Julien Duponchelle 2015-03-16 11:52:22 +01:00
parent 3a6a04b8e5
commit cf247a9301
2 changed files with 81 additions and 21 deletions

View File

@ -46,6 +46,7 @@ import gns3server.utils.asyncio
import logging import logging
import sys
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -368,12 +369,17 @@ class IOUVM(BaseVM):
if len(user_ioukey) != 17: if len(user_ioukey) != 17:
raise IOUError("IOU key length is not 16 characters in iourc file".format(self.iourc_path)) raise IOUError("IOU key length is not 16 characters in iourc file".format(self.iourc_path))
user_ioukey = user_ioukey[:16] user_ioukey = user_ioukey[:16]
# We can't test this because it's mean distributing a valid licence key
# in tests or generating one
if not sys._called_from_test:
try: try:
hostid = (yield from gns3server.utils.asyncio.subprocess_check_output("hostid")).strip() hostid = (yield from gns3server.utils.asyncio.subprocess_check_output("hostid")).strip()
except FileNotFoundError as e: except FileNotFoundError as e:
raise IOUError("Could not find hostid: {}".format(e)) raise IOUError("Could not find hostid: {}".format(e))
except subprocess.SubprocessError as e: except subprocess.SubprocessError as e:
raise IOUError("Could not execute hostid: {}".format(e)) raise IOUError("Could not execute hostid: {}".format(e))
try: try:
ioukey = int(hostid, 16) ioukey = int(hostid, 16)
except ValueError: except ValueError:

View File

@ -20,6 +20,7 @@ import aiohttp
import asyncio import asyncio
import os import os
import stat import stat
import socket
from tests.utils import asyncio_patch from tests.utils import asyncio_patch
@ -37,7 +38,7 @@ def manager(port_manager):
@pytest.fixture(scope="function") @pytest.fixture(scope="function")
def vm(project, manager, tmpdir, fake_iou_bin): def vm(project, manager, tmpdir, fake_iou_bin, iourc_file):
fake_file = str(tmpdir / "iouyap") fake_file = str(tmpdir / "iouyap")
with open(fake_file, "w+") as f: with open(fake_file, "w+") as f:
f.write("1") f.write("1")
@ -45,17 +46,28 @@ def vm(project, manager, tmpdir, fake_iou_bin):
vm = IOUVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager) vm = IOUVM("test", "00010203-0405-0607-0809-0a0b0c0d0e0f", project, manager)
config = manager.config.get_section_config("IOU") config = manager.config.get_section_config("IOU")
config["iouyap_path"] = fake_file config["iouyap_path"] = fake_file
config["iourc_path"] = iourc_file
manager.config.set_section_config("IOU", config) manager.config.set_section_config("IOU", config)
vm.path = fake_iou_bin vm.path = fake_iou_bin
return vm return vm
@pytest.fixture
def iourc_file(tmpdir):
path = str(tmpdir / "iourc")
with open(path, "w+") as f:
hostname = socket.gethostname()
f.write("[license]\n{} = aaaaaaaaaaaaaaaa;".format(hostname))
return path
@pytest.fixture @pytest.fixture
def fake_iou_bin(tmpdir): def fake_iou_bin(tmpdir):
"""Create a fake IOU image on disk""" """Create a fake IOU image on disk"""
path = str(tmpdir / "iou.bin") os.makedirs(str(tmpdir / "IOU"), exist_ok=True)
path = str(tmpdir / "IOU" / "iou.bin")
with open(path, "w+") as f: with open(path, "w+") as f:
f.write('\x7fELF\x01\x01\x01') f.write('\x7fELF\x01\x01\x01')
os.chmod(path, stat.S_IREAD | stat.S_IEXEC) os.chmod(path, stat.S_IREAD | stat.S_IEXEC)
@ -313,3 +325,45 @@ def test_stop_capture(vm, tmpdir, manager, free_console_port, loop):
def test_get_legacy_vm_workdir(): def test_get_legacy_vm_workdir():
assert IOU.get_legacy_vm_workdir(42, "bla") == "iou/device-42" assert IOU.get_legacy_vm_workdir(42, "bla") == "iou/device-42"
def test_invalid_iou_file(loop, vm, iourc_file):
hostname = socket.gethostname()
loop.run_until_complete(asyncio.async(vm._check_iou_licence()))
# Missing ;
with pytest.raises(IOUError):
with open(iourc_file, "w+") as f:
f.write("[license]\n{} = aaaaaaaaaaaaaaaa".format(hostname))
loop.run_until_complete(asyncio.async(vm._check_iou_licence()))
# Key too short
with pytest.raises(IOUError):
with open(iourc_file, "w+") as f:
f.write("[license]\n{} = aaaaaaaaaaaaaa;".format(hostname))
loop.run_until_complete(asyncio.async(vm._check_iou_licence()))
# Invalid hostname
with pytest.raises(IOUError):
with open(iourc_file, "w+") as f:
f.write("[license]\nbla = aaaaaaaaaaaaaa;")
loop.run_until_complete(asyncio.async(vm._check_iou_licence()))
# Missing licence section
with pytest.raises(IOUError):
with open(iourc_file, "w+") as f:
f.write("[licensetest]\n{} = aaaaaaaaaaaaaaaa;")
loop.run_until_complete(asyncio.async(vm._check_iou_licence()))
# Broken config file
with pytest.raises(IOUError):
with open(iourc_file, "w+") as f:
f.write("[")
loop.run_until_complete(asyncio.async(vm._check_iou_licence()))
# Missing file
with pytest.raises(IOUError):
os.remove(iourc_file)
loop.run_until_complete(asyncio.async(vm._check_iou_licence()))