mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-03 05:51:15 +02:00
refactor: ruff autofixes
This commit is contained in:
455 files changed
+12322
-14762
No files matched your search
@@ -17,9 +17,9 @@
|
||||
|
||||
import asyncio
|
||||
|
||||
#from gns3server.utils.asyncio.embed_shell import EmbedShell
|
||||
# from gns3server.utils.asyncio.embed_shell import EmbedShell
|
||||
|
||||
#FIXME: this is broken with recent Python >= 3.6
|
||||
# FIXME: this is broken with recent Python >= 3.6
|
||||
# def test_embed_shell_help(async_run):
|
||||
# class Application(EmbedShell):
|
||||
#
|
||||
|
||||
@@ -11,6 +11,7 @@ from gns3server.utils.asyncio.ssh_server import AsyncioSSHServer
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class DummyUpstreamWriter:
|
||||
def __init__(self):
|
||||
self.buffer = bytearray()
|
||||
@@ -49,15 +50,14 @@ async def _wait_for_session_count(server, expected_count, timeout=2):
|
||||
if last_count == expected_count:
|
||||
return
|
||||
await asyncio.sleep(0.05)
|
||||
assert last_count == expected_count, (
|
||||
f"Expected {expected_count} sessions, got {last_count} after {timeout}s"
|
||||
)
|
||||
assert last_count == expected_count, f"Expected {expected_count} sessions, got {last_count} after {timeout}s"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Tests
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_ssh_server_forwards_client_input_to_upstream_writer():
|
||||
"""Data typed by a connected SSH client must reach the upstream writer."""
|
||||
|
||||
@@ -113,9 +113,7 @@ async def test_telnet_server_naws_callback_uses_negotiated_dimensions():
|
||||
|
||||
client_writer = None
|
||||
try:
|
||||
_, client_writer = await telnetlib3.open_connection(
|
||||
"127.0.0.1", port, encoding=False, cols=132, rows=44
|
||||
)
|
||||
_, client_writer = await telnetlib3.open_connection("127.0.0.1", port, encoding=False, cols=132, rows=44)
|
||||
|
||||
await asyncio.wait_for(callback_event.wait(), timeout=2)
|
||||
assert callback_result == {"columns": 132, "rows": 44}
|
||||
|
||||
@@ -22,7 +22,6 @@ import pytest
|
||||
from gns3server.utils.asyncio import wait_run_in_executor, subprocess_check_output, locking
|
||||
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_wait_run_in_executor():
|
||||
|
||||
@@ -59,7 +58,6 @@ async def test_lock_decorator():
|
||||
"""
|
||||
|
||||
class TestLock:
|
||||
|
||||
def __init__(self):
|
||||
self._test_val = 0
|
||||
|
||||
@@ -72,4 +70,9 @@ async def test_lock_decorator():
|
||||
|
||||
i = TestLock()
|
||||
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
|
||||
assert res == set(
|
||||
(
|
||||
0,
|
||||
1,
|
||||
)
|
||||
) # We use a set to test this to avoid order issue
|
||||
@@ -23,7 +23,7 @@ from unittest.mock import MagicMock
|
||||
from gns3server.utils.file_watcher import FileWatcher
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strategy", ['mtime', 'hash'])
|
||||
@pytest.mark.parametrize("strategy", ["mtime", "hash"])
|
||||
@pytest.mark.asyncio
|
||||
async def test_file_watcher(tmpdir, strategy):
|
||||
|
||||
@@ -38,7 +38,7 @@ async def test_file_watcher(tmpdir, strategy):
|
||||
callback.assert_called_with(str(file))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strategy", ['mtime', 'hash'])
|
||||
@pytest.mark.parametrize("strategy", ["mtime", "hash"])
|
||||
@pytest.mark.asyncio
|
||||
async def test_file_watcher_not_existing(tmpdir, strategy):
|
||||
|
||||
@@ -52,7 +52,7 @@ async def test_file_watcher_not_existing(tmpdir, strategy):
|
||||
callback.assert_called_with(str(file))
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strategy", ['mtime', 'hash'])
|
||||
@pytest.mark.parametrize("strategy", ["mtime", "hash"])
|
||||
@pytest.mark.asyncio
|
||||
async def test_file_watcher_list(tmpdir, strategy):
|
||||
|
||||
|
||||
+48
-59
@@ -49,48 +49,48 @@ def test_images_directories(tmpdir, config):
|
||||
|
||||
def test_md5sum(tmpdir):
|
||||
|
||||
fake_img = str(tmpdir / 'hello载')
|
||||
fake_img = str(tmpdir / "hello载")
|
||||
|
||||
with open(fake_img, 'w+') as f:
|
||||
f.write('hello')
|
||||
with open(fake_img, "w+") as f:
|
||||
f.write("hello")
|
||||
|
||||
assert md5sum(fake_img) == '5d41402abc4b2a76b9719d911017c592'
|
||||
with open(str(tmpdir / 'hello载.md5sum')) as f:
|
||||
assert f.read() == '5d41402abc4b2a76b9719d911017c592'
|
||||
assert md5sum(fake_img) == "5d41402abc4b2a76b9719d911017c592"
|
||||
with open(str(tmpdir / "hello载.md5sum")) as f:
|
||||
assert f.read() == "5d41402abc4b2a76b9719d911017c592"
|
||||
|
||||
|
||||
def test_md5sum_stopped_event(tmpdir):
|
||||
|
||||
fake_img = str(tmpdir / 'hello_stopped')
|
||||
with open(fake_img, 'w+') as f:
|
||||
f.write('hello')
|
||||
fake_img = str(tmpdir / "hello_stopped")
|
||||
with open(fake_img, "w+") as f:
|
||||
f.write("hello")
|
||||
|
||||
event = threading.Event()
|
||||
event.set()
|
||||
|
||||
assert md5sum(fake_img, stopped_event=event) is None
|
||||
assert not os.path.exists(str(tmpdir / 'hello_stopped.md5sum'))
|
||||
assert not os.path.exists(str(tmpdir / "hello_stopped.md5sum"))
|
||||
|
||||
|
||||
def test_md5sum_existing_digest(tmpdir):
|
||||
|
||||
fake_img = str(tmpdir / 'hello')
|
||||
fake_img = str(tmpdir / "hello")
|
||||
|
||||
with open(fake_img, 'w+') as f:
|
||||
f.write('hello')
|
||||
with open(fake_img, "w+") as f:
|
||||
f.write("hello")
|
||||
|
||||
with open(str(tmpdir / 'hello.md5sum'), 'w+') as f:
|
||||
f.write('aaaaa02abc4b2a76b9719d911017c592')
|
||||
with open(str(tmpdir / "hello.md5sum"), "w+") as f:
|
||||
f.write("aaaaa02abc4b2a76b9719d911017c592")
|
||||
|
||||
assert md5sum(fake_img) == 'aaaaa02abc4b2a76b9719d911017c592'
|
||||
assert md5sum(fake_img) == "aaaaa02abc4b2a76b9719d911017c592"
|
||||
|
||||
|
||||
def test_md5sum_existing_digest_but_missing_image(tmpdir):
|
||||
|
||||
fake_img = str(tmpdir / 'hello')
|
||||
fake_img = str(tmpdir / "hello")
|
||||
|
||||
with open(str(tmpdir / 'hello.md5sum'), 'w+') as f:
|
||||
f.write('aaaaa02abc4b2a76b9719d911017c592')
|
||||
with open(str(tmpdir / "hello.md5sum"), "w+") as f:
|
||||
f.write("aaaaa02abc4b2a76b9719d911017c592")
|
||||
|
||||
assert md5sum(fake_img) is None
|
||||
|
||||
@@ -102,13 +102,13 @@ def test_md5sum_none(tmpdir):
|
||||
|
||||
def test_remove_checksum(tmpdir):
|
||||
|
||||
with open(str(tmpdir / 'hello.md5sum'), 'w+') as f:
|
||||
f.write('aaaaa02abc4b2a76b9719d911017c592')
|
||||
remove_checksum(str(tmpdir / 'hello'))
|
||||
with open(str(tmpdir / "hello.md5sum"), "w+") as f:
|
||||
f.write("aaaaa02abc4b2a76b9719d911017c592")
|
||||
remove_checksum(str(tmpdir / "hello"))
|
||||
|
||||
assert not os.path.exists(str(tmpdir / 'hello.md5sum'))
|
||||
assert not os.path.exists(str(tmpdir / "hello.md5sum"))
|
||||
|
||||
remove_checksum(str(tmpdir / 'not_exists'))
|
||||
remove_checksum(str(tmpdir / "not_exists"))
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -116,34 +116,33 @@ async def test_list_images(tmpdir, config):
|
||||
|
||||
# IOS image in the images directory
|
||||
ios_image_1 = tmpdir / "images1" / "IOS" / "ios_image_1.image"
|
||||
ios_image_1.write(b'\x7fELF\x01\x02\x01', ensure=True)
|
||||
ios_image_1.write(b"\x7fELF\x01\x02\x01", ensure=True)
|
||||
ios_image_1 = force_unix_path(str(ios_image_1))
|
||||
|
||||
# IOS image in an additional images path
|
||||
ios_image_2 = tmpdir / "images2" / "ios_image_2.image"
|
||||
ios_image_2.write(b'\x7fELF\x01\x02\x01', ensure=True)
|
||||
ios_image_2.write(b"\x7fELF\x01\x02\x01", ensure=True)
|
||||
ios_image_2 = force_unix_path(str(ios_image_2))
|
||||
|
||||
# Not a valid elf file
|
||||
not_elf_file = tmpdir / "images1" / "IOS" / "not_elf.image"
|
||||
not_elf_file.write(b'NOTANELF', ensure=True)
|
||||
not_elf_file.write(b"NOTANELF", ensure=True)
|
||||
not_elf_file = force_unix_path(str(not_elf_file))
|
||||
|
||||
# Invalid image because it is very small
|
||||
small_file = tmpdir / "images1" / "too_small.image"
|
||||
small_file.write(b'1', ensure=True)
|
||||
small_file.write(b"1", ensure=True)
|
||||
|
||||
if sys.platform.startswith("linux"):
|
||||
# 64-bit IOU image
|
||||
iou_image_1 = tmpdir / "images1" / "IOU" / "iou64.bin"
|
||||
iou_image_1.write(b'\x7fELF\x02\x01\x01', ensure=True)
|
||||
iou_image_1.write(b"\x7fELF\x02\x01\x01", ensure=True)
|
||||
iou_image_1 = force_unix_path(str(iou_image_1))
|
||||
# 32-bit IOU image
|
||||
iou_image_2 = tmpdir / "images1" / "IOU" / "iou32.bin"
|
||||
iou_image_2.write(b'\x7fELF\x01\x01\x01', ensure=True) # 32-bit IOU image
|
||||
iou_image_2.write(b"\x7fELF\x01\x01\x01", ensure=True) # 32-bit IOU image
|
||||
iou_image_2 = force_unix_path(str(iou_image_2))
|
||||
|
||||
|
||||
# Qemu image
|
||||
qemu_image_1 = tmpdir / "images1" / "QEMU" / "qemu_image.qcow2"
|
||||
qemu_image_1.write("1234567", ensure=True)
|
||||
@@ -151,7 +150,7 @@ async def test_list_images(tmpdir, config):
|
||||
|
||||
# ELF file inside the Qemu
|
||||
elf_file = tmpdir / "images1" / "QEMU" / "elf_file.bin"
|
||||
elf_file.write(b'\x7fELF\x02\x01\x01', ensure=True) # ELF file
|
||||
elf_file.write(b"\x7fELF\x02\x01\x01", ensure=True) # ELF file
|
||||
elf_file = force_unix_path(str(elf_file))
|
||||
|
||||
md5sum_file = tmpdir / "images1" / "QEMU" / "image.qcow2.md5sum"
|
||||
@@ -161,42 +160,32 @@ async def test_list_images(tmpdir, config):
|
||||
config.settings.Server.images_path = str(tmpdir / "images1")
|
||||
config.settings.Server.additional_images_paths = "/tmp/null24564;" + str(tmpdir / "images2")
|
||||
|
||||
assert sorted(await list_images("dynamips"), key=lambda k: k['filename']) == [
|
||||
assert sorted(await list_images("dynamips"), key=lambda k: k["filename"]) == [
|
||||
{
|
||||
'filename': 'ios_image_1.image',
|
||||
'filesize': 7,
|
||||
'md5sum': 'b0d5aa897d937aced5a6b1046e8f7e2e',
|
||||
'path': 'ios_image_1.image'
|
||||
"filename": "ios_image_1.image",
|
||||
"filesize": 7,
|
||||
"md5sum": "b0d5aa897d937aced5a6b1046e8f7e2e",
|
||||
"path": "ios_image_1.image",
|
||||
},
|
||||
{
|
||||
'filename': 'ios_image_2.image',
|
||||
'filesize': 7,
|
||||
'md5sum': 'b0d5aa897d937aced5a6b1046e8f7e2e',
|
||||
'path': str(ios_image_2)
|
||||
}
|
||||
"filename": "ios_image_2.image",
|
||||
"filesize": 7,
|
||||
"md5sum": "b0d5aa897d937aced5a6b1046e8f7e2e",
|
||||
"path": str(ios_image_2),
|
||||
},
|
||||
]
|
||||
|
||||
if sys.platform.startswith("linux"):
|
||||
assert sorted(await list_images("iou"), key=lambda k: k['filename']) == [
|
||||
{
|
||||
'filename': 'iou32.bin',
|
||||
'filesize': 7,
|
||||
'md5sum': 'e573e8f5c93c6c00783f20c7a170aa6c',
|
||||
'path': 'iou32.bin'
|
||||
},
|
||||
{
|
||||
'filename': 'iou64.bin',
|
||||
'filesize': 7,
|
||||
'md5sum': 'c73626d23469519894d58bc98bee9655',
|
||||
'path': 'iou64.bin'
|
||||
}
|
||||
assert sorted(await list_images("iou"), key=lambda k: k["filename"]) == [
|
||||
{"filename": "iou32.bin", "filesize": 7, "md5sum": "e573e8f5c93c6c00783f20c7a170aa6c", "path": "iou32.bin"},
|
||||
{"filename": "iou64.bin", "filesize": 7, "md5sum": "c73626d23469519894d58bc98bee9655", "path": "iou64.bin"},
|
||||
]
|
||||
|
||||
assert await list_images("qemu") == [
|
||||
{
|
||||
'filename': 'qemu_image.qcow2',
|
||||
'filesize': 7,
|
||||
'md5sum': 'fcea920f7412b5da7be0cf42b8c93759',
|
||||
'path': 'qemu_image.qcow2'
|
||||
"filename": "qemu_image.qcow2",
|
||||
"filesize": 7,
|
||||
"md5sum": "fcea920f7412b5da7be0cf42b8c93759",
|
||||
"path": "qemu_image.qcow2",
|
||||
}
|
||||
]
|
||||
@@ -7,7 +7,7 @@ from gns3server.utils.packet_filter_validation import (
|
||||
validate_filter_parameters,
|
||||
validate_all_filters,
|
||||
filter_inactive_filters,
|
||||
FilterValidationError
|
||||
FilterValidationError,
|
||||
)
|
||||
|
||||
|
||||
@@ -143,17 +143,14 @@ class TestPacketFilterValidation:
|
||||
|
||||
def test_validate_all_filters(self):
|
||||
"""Test validating multiple filters at once."""
|
||||
filters = {
|
||||
"frequency_drop": [10],
|
||||
"delay": [100, 50]
|
||||
}
|
||||
filters = {"frequency_drop": [10], "delay": [100, 50]}
|
||||
validate_all_filters(filters) # Should not raise
|
||||
|
||||
def test_validate_all_filters_with_invalid(self):
|
||||
"""Test validate_all_filters with invalid filter."""
|
||||
filters = {
|
||||
"frequency_drop": [10],
|
||||
"packet_loss": [150] # Invalid: over 100%
|
||||
"packet_loss": [150], # Invalid: over 100%
|
||||
}
|
||||
with pytest.raises(FilterValidationError):
|
||||
validate_all_filters(filters)
|
||||
@@ -242,16 +239,13 @@ class TestFilterInactiveFilters:
|
||||
def test_filter_inactive_multiple_filters_mixed(self):
|
||||
"""Test multiple filters with mixed active/inactive states."""
|
||||
filters = {
|
||||
"delay": [0, 0], # Disabled: [0, 0]
|
||||
"packet_loss": [0], # Disabled: 0%
|
||||
"corrupt": [2], # Active: 2%
|
||||
"frequency_drop": [10] # Active: every 10th packet
|
||||
"delay": [0, 0], # Disabled: [0, 0]
|
||||
"packet_loss": [0], # Disabled: 0%
|
||||
"corrupt": [2], # Active: 2%
|
||||
"frequency_drop": [10], # Active: every 10th packet
|
||||
}
|
||||
result = filter_inactive_filters(filters)
|
||||
assert result == {
|
||||
"corrupt": [2],
|
||||
"frequency_drop": [10]
|
||||
}
|
||||
assert result == {"corrupt": [2], "frequency_drop": [10]}
|
||||
|
||||
def test_filter_inactive_empty_filters(self):
|
||||
"""Test empty filters dictionary."""
|
||||
@@ -262,4 +256,4 @@ class TestFilterInactiveFilters:
|
||||
def test_filter_inactive_none_filters(self):
|
||||
"""Test None filters."""
|
||||
result = filter_inactive_filters(None)
|
||||
assert result == {}
|
||||
assert result == {}
|
||||
Reference in new issue
Block a user