mirror of
https://github.com/GNS3/gns3-server
synced 2024-12-26 16:58:28 +00:00
Better mocking in Docker tests
This commit is contained in:
parent
e9e2dc2ca7
commit
1ce0c13fc9
@ -219,10 +219,10 @@ async def test_install_busybox():
|
|||||||
mock_process.returncode = 1 # means that busybox is not dynamically linked
|
mock_process.returncode = 1 # means that busybox is not dynamically linked
|
||||||
mock_process.communicate = AsyncioMagicMock(return_value=(b"", b"not a dynamic executable"))
|
mock_process.communicate = AsyncioMagicMock(return_value=(b"", b"not a dynamic executable"))
|
||||||
|
|
||||||
with patch("os.path.isfile", return_value=False):
|
with patch("gns3server.compute.docker.os.path.isfile", return_value=False):
|
||||||
with patch("shutil.which", return_value="/usr/bin/busybox"):
|
with patch("gns3server.compute.docker.shutil.which", return_value="/usr/bin/busybox"):
|
||||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as create_subprocess_mock:
|
with asyncio_patch("gns3server.compute.docker.asyncio.create_subprocess_exec", return_value=mock_process) as create_subprocess_mock:
|
||||||
with patch("shutil.copy2") as copy2_mock:
|
with patch("gns3server.compute.docker.shutil.copy2") as copy2_mock:
|
||||||
await Docker.install_busybox()
|
await Docker.install_busybox()
|
||||||
create_subprocess_mock.assert_called_with(
|
create_subprocess_mock.assert_called_with(
|
||||||
"ldd",
|
"ldd",
|
||||||
@ -241,8 +241,8 @@ async def test_install_busybox_dynamic_linked():
|
|||||||
mock_process.communicate = AsyncioMagicMock(return_value=(b"Dynamically linked library", b""))
|
mock_process.communicate = AsyncioMagicMock(return_value=(b"Dynamically linked library", b""))
|
||||||
|
|
||||||
with patch("os.path.isfile", return_value=False):
|
with patch("os.path.isfile", return_value=False):
|
||||||
with patch("shutil.which", return_value="/usr/bin/busybox"):
|
with patch("gns3server.compute.docker.shutil.which", return_value="/usr/bin/busybox"):
|
||||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process):
|
with asyncio_patch("gns3server.compute.docker.asyncio.create_subprocess_exec", return_value=mock_process):
|
||||||
with pytest.raises(DockerError) as e:
|
with pytest.raises(DockerError) as e:
|
||||||
await Docker.install_busybox()
|
await Docker.install_busybox()
|
||||||
assert str(e.value) == "No busybox executable could be found"
|
assert str(e.value) == "No busybox executable could be found"
|
||||||
@ -251,8 +251,8 @@ async def test_install_busybox_dynamic_linked():
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_install_busybox_no_executables():
|
async def test_install_busybox_no_executables():
|
||||||
|
|
||||||
with patch("os.path.isfile", return_value=False):
|
with patch("gns3server.compute.docker.os.path.isfile", return_value=False):
|
||||||
with patch("shutil.which", return_value=None):
|
with patch("gns3server.compute.docker.shutil.which", return_value=None):
|
||||||
with pytest.raises(DockerError) as e:
|
with pytest.raises(DockerError) as e:
|
||||||
await Docker.install_busybox()
|
await Docker.install_busybox()
|
||||||
assert str(e.value) == "No busybox executable could be found"
|
assert str(e.value) == "No busybox executable could be found"
|
||||||
|
@ -21,6 +21,8 @@ import pytest
|
|||||||
import pytest_asyncio
|
import pytest_asyncio
|
||||||
import uuid
|
import uuid
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
from unittest.mock import patch
|
||||||
from tests.utils import asyncio_patch, AsyncioMagicMock
|
from tests.utils import asyncio_patch, AsyncioMagicMock
|
||||||
|
|
||||||
from gns3server.compute.ubridge.ubridge_error import UbridgeNamespaceError
|
from gns3server.compute.ubridge.ubridge_error import UbridgeNamespaceError
|
||||||
@ -1072,8 +1074,9 @@ async def test_start(vm, manager, free_console_port):
|
|||||||
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
||||||
await vm.adapter_add_nio_binding(0, nio)
|
await vm.adapter_add_nio_binding(0, nio)
|
||||||
|
|
||||||
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
with patch("gns3server.compute.docker.Docker.install_busybox"):
|
||||||
await vm.start()
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
||||||
|
await vm.start()
|
||||||
|
|
||||||
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
|
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
|
||||||
vm._add_ubridge_connection.assert_called_once_with(nio, 0)
|
vm._add_ubridge_connection.assert_called_once_with(nio, 0)
|
||||||
@ -1092,15 +1095,16 @@ async def test_start_namespace_failed(vm, manager, free_console_port):
|
|||||||
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
|
||||||
await vm.adapter_add_nio_binding(0, nio)
|
await vm.adapter_add_nio_binding(0, nio)
|
||||||
|
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
with patch("gns3server.compute.docker.Docker.install_busybox"):
|
||||||
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
|
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection", side_effect=UbridgeNamespaceError()) as mock_add_ubridge_connection:
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._get_log", return_value='Hello not available') as mock_log:
|
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection", side_effect=UbridgeNamespaceError()) as mock_add_ubridge_connection:
|
||||||
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_log", return_value='Hello not available') as mock_log:
|
||||||
|
|
||||||
with pytest.raises(DockerError):
|
with pytest.raises(DockerError):
|
||||||
await vm.start()
|
await vm.start()
|
||||||
|
|
||||||
mock_query.assert_any_call("POST", "containers/e90e34656842/start")
|
mock_query.assert_any_call("POST", "containers/e90e34656842/start")
|
||||||
mock_add_ubridge_connection.assert_called_once_with(nio, 0)
|
mock_add_ubridge_connection.assert_called_once_with(nio, 0)
|
||||||
@ -1117,13 +1121,14 @@ async def test_start_without_nio(vm):
|
|||||||
assert vm.status != "started"
|
assert vm.status != "started"
|
||||||
vm.adapters = 1
|
vm.adapters = 1
|
||||||
|
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
with patch("gns3server.compute.docker.Docker.install_busybox"):
|
||||||
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
|
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42):
|
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection") as mock_add_ubridge_connection:
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42):
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._start_console") as mock_start_console:
|
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection") as mock_add_ubridge_connection:
|
||||||
await vm.start()
|
with asyncio_patch("gns3server.compute.docker.DockerVM._start_console") as mock_start_console:
|
||||||
|
await vm.start()
|
||||||
|
|
||||||
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
|
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
|
||||||
assert mock_add_ubridge_connection.called
|
assert mock_add_ubridge_connection.called
|
||||||
@ -1135,9 +1140,10 @@ async def test_start_without_nio(vm):
|
|||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_start_unpause(vm):
|
async def test_start_unpause(vm):
|
||||||
|
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="paused"):
|
with patch("gns3server.compute.docker.Docker.install_busybox"):
|
||||||
with asyncio_patch("gns3server.compute.docker.DockerVM.unpause", return_value="paused") as mock:
|
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="paused"):
|
||||||
await vm.start()
|
with asyncio_patch("gns3server.compute.docker.DockerVM.unpause", return_value="paused") as mock:
|
||||||
|
await vm.start()
|
||||||
assert mock.called
|
assert mock.called
|
||||||
assert vm.status == "started"
|
assert vm.status == "started"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user