mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
Refactor tests
* Use pytest-aiohttp * Use the async def / await syntax. * Fix tests to run with Python 3.8
This commit is contained in:
1 parent
f498ab06b4
commit
d3ea67da24
87 files changed
+3697
-3528
No files matched your search
+10
-30
@@ -25,53 +25,33 @@ from gns3server.utils.asyncio import wait_run_in_executor, subprocess_check_outp
|
||||
from tests.utils import AsyncioMagicMock
|
||||
|
||||
|
||||
def test_wait_run_in_executor(loop):
|
||||
async def test_wait_run_in_executor():
|
||||
|
||||
def change_var(param):
|
||||
return param
|
||||
|
||||
exec = wait_run_in_executor(change_var, "test")
|
||||
result = loop.run_until_complete(asyncio.ensure_future(exec))
|
||||
result = await wait_run_in_executor(change_var, "test")
|
||||
assert result == "test"
|
||||
|
||||
|
||||
def test_exception_wait_run_in_executor(loop):
|
||||
async def test_exception_wait_run_in_executor():
|
||||
|
||||
def raise_exception():
|
||||
raise Exception("test")
|
||||
|
||||
exec = wait_run_in_executor(raise_exception)
|
||||
with pytest.raises(Exception):
|
||||
result = loop.run_until_complete(asyncio.ensure_future(exec))
|
||||
await wait_run_in_executor(raise_exception)
|
||||
|
||||
|
||||
@pytest.mark.skipif(sys.platform.startswith("win"), reason="Not supported on Windows")
|
||||
def test_subprocess_check_output(loop, tmpdir, restore_original_path):
|
||||
async def test_subprocess_check_output(loop, tmpdir):
|
||||
|
||||
path = str(tmpdir / "test")
|
||||
exec = subprocess_check_output("echo", "-n", path)
|
||||
result = loop.run_until_complete(asyncio.ensure_future(exec))
|
||||
result = await subprocess_check_output("echo", "-n", path)
|
||||
assert result == path
|
||||
|
||||
|
||||
def test_wait_for_process_termination(loop):
|
||||
|
||||
if sys.version_info >= (3, 5):
|
||||
# No need for test we use native version
|
||||
return
|
||||
process = MagicMock()
|
||||
process.returncode = 0
|
||||
exec = wait_for_process_termination(process)
|
||||
loop.run_until_complete(asyncio.ensure_future(exec))
|
||||
|
||||
process = MagicMock()
|
||||
process.returncode = None
|
||||
exec = wait_for_process_termination(process, timeout=0.5)
|
||||
with pytest.raises(asyncio.TimeoutError):
|
||||
loop.run_until_complete(asyncio.ensure_future(exec))
|
||||
|
||||
|
||||
def test_lock_decorator(loop):
|
||||
async def test_lock_decorator():
|
||||
"""
|
||||
The test check if the the second call to method_to_lock wait for the
|
||||
first call to finish
|
||||
@@ -84,11 +64,11 @@ def test_lock_decorator(loop):
|
||||
|
||||
@locking
|
||||
async def method_to_lock(self):
|
||||
res = self._test_val
|
||||
result = self._test_val
|
||||
await asyncio.sleep(0.1)
|
||||
self._test_val += 1
|
||||
return res
|
||||
return result
|
||||
|
||||
i = TestLock()
|
||||
res = set(loop.run_until_complete(asyncio.gather(i.method_to_lock(), i.method_to_lock())))
|
||||
res = set(await asyncio.gather(i.method_to_lock(), i.method_to_lock()))
|
||||
assert res == set((0, 1,)) # We use a set to test this to avoid order issue
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# Copyright (C) 2016 GNS3 Technologies Inc.
|
||||
# Copyright (C) 2020 GNS3 Technologies Inc.
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
@@ -24,41 +24,42 @@ from gns3server.utils.file_watcher import FileWatcher
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strategy", ['mtime', 'hash'])
|
||||
def test_file_watcher(async_run, tmpdir, strategy):
|
||||
async def test_file_watcher(tmpdir, strategy):
|
||||
|
||||
file = tmpdir / "test"
|
||||
file.write("a")
|
||||
callback = MagicMock()
|
||||
fw = FileWatcher(file, callback, delay=0.5, strategy=strategy)
|
||||
async_run(asyncio.sleep(1))
|
||||
FileWatcher(file, callback, delay=0.1, strategy=strategy)
|
||||
await asyncio.sleep(0.5)
|
||||
assert not callback.called
|
||||
file.write("b")
|
||||
async_run(asyncio.sleep(1.5))
|
||||
await asyncio.sleep(0.5)
|
||||
callback.assert_called_with(str(file))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strategy", ['mtime', 'hash'])
|
||||
def test_file_watcher_not_existing(async_run, tmpdir, strategy):
|
||||
async def test_file_watcher_not_existing(tmpdir, strategy):
|
||||
|
||||
file = tmpdir / "test"
|
||||
callback = MagicMock()
|
||||
fw = FileWatcher(file, callback, delay=0.5, strategy=strategy)
|
||||
async_run(asyncio.sleep(1))
|
||||
FileWatcher(file, callback, delay=0.1, strategy=strategy)
|
||||
await asyncio.sleep(0.5)
|
||||
assert not callback.called
|
||||
file.write("b")
|
||||
async_run(asyncio.sleep(1.5))
|
||||
await asyncio.sleep(0.5)
|
||||
callback.assert_called_with(str(file))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strategy", ['mtime', 'hash'])
|
||||
def test_file_watcher_list(async_run, tmpdir, strategy):
|
||||
async def test_file_watcher_list(tmpdir, strategy):
|
||||
|
||||
file = tmpdir / "test"
|
||||
file.write("a")
|
||||
|
||||
file2 = tmpdir / "test2"
|
||||
|
||||
callback = MagicMock()
|
||||
fw = FileWatcher([file, file2], callback, delay=0.5, strategy=strategy)
|
||||
async_run(asyncio.sleep(1))
|
||||
FileWatcher([file, file2], callback, delay=0.1, strategy=strategy)
|
||||
await asyncio.sleep(0.5)
|
||||
assert not callback.called
|
||||
file2.write("b")
|
||||
async_run(asyncio.sleep(1.5))
|
||||
await asyncio.sleep(0.5)
|
||||
callback.assert_called_with(str(file2))
|
||||
@@ -26,6 +26,7 @@ from gns3server.utils.images import md5sum, remove_checksum, images_directories,
|
||||
|
||||
|
||||
def test_images_directories(tmpdir):
|
||||
|
||||
path1 = tmpdir / "images1" / "QEMU" / "test1.bin"
|
||||
path1.write("1", ensure=True)
|
||||
path1 = force_unix_path(str(path1))
|
||||
@@ -48,6 +49,7 @@ def test_images_directories(tmpdir):
|
||||
|
||||
|
||||
def test_md5sum(tmpdir):
|
||||
|
||||
fake_img = str(tmpdir / 'hello载')
|
||||
|
||||
with open(fake_img, 'w+') as f:
|
||||
@@ -59,6 +61,7 @@ def test_md5sum(tmpdir):
|
||||
|
||||
|
||||
def test_md5sum_stopped_event(tmpdir):
|
||||
|
||||
fake_img = str(tmpdir / 'hello_stopped')
|
||||
with open(fake_img, 'w+') as f:
|
||||
f.write('hello')
|
||||
@@ -71,6 +74,7 @@ def test_md5sum_stopped_event(tmpdir):
|
||||
|
||||
|
||||
def test_md5sum_existing_digest(tmpdir):
|
||||
|
||||
fake_img = str(tmpdir / 'hello')
|
||||
|
||||
with open(fake_img, 'w+') as f:
|
||||
@@ -83,6 +87,7 @@ def test_md5sum_existing_digest(tmpdir):
|
||||
|
||||
|
||||
def test_md5sum_existing_digest_but_missing_image(tmpdir):
|
||||
|
||||
fake_img = str(tmpdir / 'hello')
|
||||
|
||||
with open(str(tmpdir / 'hello.md5sum'), 'w+') as f:
|
||||
@@ -92,6 +97,7 @@ def test_md5sum_existing_digest_but_missing_image(tmpdir):
|
||||
|
||||
|
||||
def test_md5sum_none(tmpdir):
|
||||
|
||||
assert md5sum(None) is None
|
||||
|
||||
|
||||
@@ -107,6 +113,7 @@ def test_remove_checksum(tmpdir):
|
||||
|
||||
|
||||
def test_list_images(tmpdir):
|
||||
|
||||
path1 = tmpdir / "images1" / "IOS" / "test1.image"
|
||||
path1.write(b'\x7fELF\x01\x02\x01', ensure=True)
|
||||
path1 = force_unix_path(str(path1))
|
||||
|
||||
@@ -21,6 +21,7 @@ from gns3server.utils.interfaces import interfaces, is_interface_up, has_netmask
|
||||
|
||||
|
||||
def test_interfaces():
|
||||
|
||||
# This test should pass on all platforms without crash
|
||||
interface_list = interfaces()
|
||||
assert isinstance(interface_list, list)
|
||||
@@ -39,6 +40,7 @@ def test_interfaces():
|
||||
|
||||
|
||||
def test_has_netmask():
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
# No loopback
|
||||
pass
|
||||
@@ -49,6 +51,7 @@ def test_has_netmask():
|
||||
|
||||
|
||||
def test_is_interface_up():
|
||||
|
||||
if sys.platform.startswith("win"):
|
||||
# is_interface_up() always returns True on Windows
|
||||
pass
|
||||
|
||||
@@ -24,6 +24,7 @@ from gns3server.utils.path import check_path_allowed, get_default_project_direct
|
||||
|
||||
|
||||
def test_check_path_allowed(config, tmpdir):
|
||||
|
||||
config.set("Server", "local", False)
|
||||
config.set("Server", "projects_path", str(tmpdir))
|
||||
with pytest.raises(aiohttp.web.HTTPForbidden):
|
||||
@@ -37,7 +38,6 @@ def test_check_path_allowed(config, tmpdir):
|
||||
def test_get_default_project_directory(config):
|
||||
|
||||
config.clear()
|
||||
|
||||
path = os.path.normpath(os.path.expanduser("~/GNS3/projects"))
|
||||
assert get_default_project_directory() == path
|
||||
assert os.path.exists(path)
|
||||
@@ -20,6 +20,7 @@ from gns3server.utils.picture import get_size
|
||||
|
||||
|
||||
def test_get_size():
|
||||
|
||||
with open("tests/resources/nvram_iou", "rb") as f:
|
||||
res = get_size(f.read(), default_width=100, default_height=50)
|
||||
assert res == (100, 50, None)
|
||||
|
||||
Reference in new issue
Block a user