Skip to content

Commit 1ce0c13

Browse files
committed
Better mocking in Docker tests
1 parent e9e2dc2 commit 1ce0c13

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

tests/compute/docker/test_docker.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -219,10 +219,10 @@ async def test_install_busybox():
219219
mock_process.returncode = 1 # means that busybox is not dynamically linked
220220
mock_process.communicate = AsyncioMagicMock(return_value=(b"", b"not a dynamic executable"))
221221

222-
with patch("os.path.isfile", return_value=False):
223-
with patch("shutil.which", return_value="/usr/bin/busybox"):
224-
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as create_subprocess_mock:
225-
with patch("shutil.copy2") as copy2_mock:
222+
with patch("gns3server.compute.docker.os.path.isfile", return_value=False):
223+
with patch("gns3server.compute.docker.shutil.which", return_value="/usr/bin/busybox"):
224+
with asyncio_patch("gns3server.compute.docker.asyncio.create_subprocess_exec", return_value=mock_process) as create_subprocess_mock:
225+
with patch("gns3server.compute.docker.shutil.copy2") as copy2_mock:
226226
await Docker.install_busybox()
227227
create_subprocess_mock.assert_called_with(
228228
"ldd",
@@ -241,8 +241,8 @@ async def test_install_busybox_dynamic_linked():
241241
mock_process.communicate = AsyncioMagicMock(return_value=(b"Dynamically linked library", b""))
242242

243243
with patch("os.path.isfile", return_value=False):
244-
with patch("shutil.which", return_value="/usr/bin/busybox"):
245-
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process):
244+
with patch("gns3server.compute.docker.shutil.which", return_value="/usr/bin/busybox"):
245+
with asyncio_patch("gns3server.compute.docker.asyncio.create_subprocess_exec", return_value=mock_process):
246246
with pytest.raises(DockerError) as e:
247247
await Docker.install_busybox()
248248
assert str(e.value) == "No busybox executable could be found"
@@ -251,8 +251,8 @@ async def test_install_busybox_dynamic_linked():
251251
@pytest.mark.asyncio
252252
async def test_install_busybox_no_executables():
253253

254-
with patch("os.path.isfile", return_value=False):
255-
with patch("shutil.which", return_value=None):
254+
with patch("gns3server.compute.docker.os.path.isfile", return_value=False):
255+
with patch("gns3server.compute.docker.shutil.which", return_value=None):
256256
with pytest.raises(DockerError) as e:
257257
await Docker.install_busybox()
258258
assert str(e.value) == "No busybox executable could be found"

tests/compute/docker/test_docker_vm.py

+26-20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import pytest_asyncio
2222
import uuid
2323
import os
24+
25+
from unittest.mock import patch
2426
from tests.utils import asyncio_patch, AsyncioMagicMock
2527

2628
from gns3server.compute.ubridge.ubridge_error import UbridgeNamespaceError
@@ -1072,8 +1074,9 @@ async def test_start(vm, manager, free_console_port):
10721074
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
10731075
await vm.adapter_add_nio_binding(0, nio)
10741076

1075-
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
1076-
await vm.start()
1077+
with patch("gns3server.compute.docker.Docker.install_busybox"):
1078+
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
1079+
await vm.start()
10771080

10781081
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
10791082
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):
10921095
nio = manager.create_nio({"type": "nio_udp", "lport": free_console_port, "rport": free_console_port, "rhost": "127.0.0.1"})
10931096
await vm.adapter_add_nio_binding(0, nio)
10941097

1095-
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
1096-
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
1097-
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
1098-
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
1099-
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection", side_effect=UbridgeNamespaceError()) as mock_add_ubridge_connection:
1100-
with asyncio_patch("gns3server.compute.docker.DockerVM._get_log", return_value='Hello not available') as mock_log:
1098+
with patch("gns3server.compute.docker.Docker.install_busybox"):
1099+
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
1100+
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
1101+
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
1102+
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42) as mock_namespace:
1103+
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection", side_effect=UbridgeNamespaceError()) as mock_add_ubridge_connection:
1104+
with asyncio_patch("gns3server.compute.docker.DockerVM._get_log", return_value='Hello not available') as mock_log:
11011105

1102-
with pytest.raises(DockerError):
1103-
await vm.start()
1106+
with pytest.raises(DockerError):
1107+
await vm.start()
11041108

11051109
mock_query.assert_any_call("POST", "containers/e90e34656842/start")
11061110
mock_add_ubridge_connection.assert_called_once_with(nio, 0)
@@ -1117,13 +1121,14 @@ async def test_start_without_nio(vm):
11171121
assert vm.status != "started"
11181122
vm.adapters = 1
11191123

1120-
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
1121-
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
1122-
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
1123-
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42):
1124-
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection") as mock_add_ubridge_connection:
1125-
with asyncio_patch("gns3server.compute.docker.DockerVM._start_console") as mock_start_console:
1126-
await vm.start()
1124+
with patch("gns3server.compute.docker.Docker.install_busybox"):
1125+
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="stopped"):
1126+
with asyncio_patch("gns3server.compute.docker.Docker.query") as mock_query:
1127+
with asyncio_patch("gns3server.compute.docker.DockerVM._start_ubridge") as mock_start_ubridge:
1128+
with asyncio_patch("gns3server.compute.docker.DockerVM._get_namespace", return_value=42):
1129+
with asyncio_patch("gns3server.compute.docker.DockerVM._add_ubridge_connection") as mock_add_ubridge_connection:
1130+
with asyncio_patch("gns3server.compute.docker.DockerVM._start_console") as mock_start_console:
1131+
await vm.start()
11271132

11281133
mock_query.assert_called_with("POST", "containers/e90e34656842/start")
11291134
assert mock_add_ubridge_connection.called
@@ -1135,9 +1140,10 @@ async def test_start_without_nio(vm):
11351140
@pytest.mark.asyncio
11361141
async def test_start_unpause(vm):
11371142

1138-
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="paused"):
1139-
with asyncio_patch("gns3server.compute.docker.DockerVM.unpause", return_value="paused") as mock:
1140-
await vm.start()
1143+
with patch("gns3server.compute.docker.Docker.install_busybox"):
1144+
with asyncio_patch("gns3server.compute.docker.DockerVM._get_container_state", return_value="paused"):
1145+
with asyncio_patch("gns3server.compute.docker.DockerVM.unpause", return_value="paused") as mock:
1146+
await vm.start()
11411147
assert mock.called
11421148
assert vm.status == "started"
11431149

0 commit comments

Comments
 (0)