mirror of
https://github.com/GNS3/gns3-server
synced 2025-01-11 16:41:04 +00:00
Merge pull request #2223 from GNS3/fix/2214
Use proc.communicate() when checking for subprocess output
This commit is contained in:
commit
088ae699de
@ -71,10 +71,10 @@ async def subprocess_check_output(*args, cwd=None, env=None, stderr=False):
|
|||||||
|
|
||||||
if stderr:
|
if stderr:
|
||||||
proc = await asyncio.create_subprocess_exec(*args, stderr=asyncio.subprocess.PIPE, cwd=cwd, env=env)
|
proc = await asyncio.create_subprocess_exec(*args, stderr=asyncio.subprocess.PIPE, cwd=cwd, env=env)
|
||||||
output = await proc.stderr.read()
|
_, output = await proc.communicate()
|
||||||
else:
|
else:
|
||||||
proc = await asyncio.create_subprocess_exec(*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.DEVNULL, cwd=cwd, env=env)
|
proc = await asyncio.create_subprocess_exec(*args, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.DEVNULL, cwd=cwd, env=env)
|
||||||
output = await proc.stdout.read()
|
output, _ = await proc.communicate()
|
||||||
if output is None:
|
if output is None:
|
||||||
return ""
|
return ""
|
||||||
# If we received garbage we ignore invalid characters
|
# If we received garbage we ignore invalid characters
|
||||||
|
@ -102,6 +102,7 @@ async def test_start(vm):
|
|||||||
|
|
||||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as mock_exec:
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as mock_exec:
|
||||||
mock_process.returncode = None
|
mock_process.returncode = None
|
||||||
|
mock_process.communicate = AsyncioMagicMock(return_value=(None, None))
|
||||||
await vm.start()
|
await vm.start()
|
||||||
assert vm.is_running()
|
assert vm.is_running()
|
||||||
assert vm.command_line == ' '.join(mock_exec.call_args[0])
|
assert vm.command_line == ' '.join(mock_exec.call_args[0])
|
||||||
@ -130,6 +131,7 @@ async def test_start_with_iourc(vm, tmpdir):
|
|||||||
with patch("gns3server.config.Config.get_section_config", return_value={"iourc_path": fake_file}):
|
with patch("gns3server.config.Config.get_section_config", return_value={"iourc_path": fake_file}):
|
||||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as exec_mock:
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=mock_process) as exec_mock:
|
||||||
mock_process.returncode = None
|
mock_process.returncode = None
|
||||||
|
mock_process.communicate = AsyncioMagicMock(return_value=(None, None))
|
||||||
await vm.start()
|
await vm.start()
|
||||||
assert vm.is_running()
|
assert vm.is_running()
|
||||||
arsgs, kwargs = exec_mock.call_args
|
arsgs, kwargs = exec_mock.call_args
|
||||||
@ -165,11 +167,12 @@ async def test_stop(vm):
|
|||||||
future = asyncio.Future()
|
future = asyncio.Future()
|
||||||
future.set_result(True)
|
future.set_result(True)
|
||||||
process.wait.return_value = future
|
process.wait.return_value = future
|
||||||
|
process.returncode = None
|
||||||
|
process.communicate = AsyncioMagicMock(return_value=(None, None))
|
||||||
|
|
||||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
||||||
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
|
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
|
||||||
await vm.start()
|
await vm.start()
|
||||||
process.returncode = None
|
|
||||||
assert vm.is_running()
|
assert vm.is_running()
|
||||||
await vm.stop()
|
await vm.stop()
|
||||||
assert vm.is_running() is False
|
assert vm.is_running() is False
|
||||||
@ -190,6 +193,7 @@ async def test_reload(vm, fake_iou_bin):
|
|||||||
future.set_result(True)
|
future.set_result(True)
|
||||||
process.wait.return_value = future
|
process.wait.return_value = future
|
||||||
process.returncode = None
|
process.returncode = None
|
||||||
|
process.communicate = AsyncioMagicMock(return_value=(None, None))
|
||||||
|
|
||||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
||||||
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
|
with asyncio_patch("gns3server.utils.asyncio.wait_for_process_termination"):
|
||||||
@ -202,10 +206,13 @@ async def test_reload(vm, fake_iou_bin):
|
|||||||
|
|
||||||
async def test_close(vm, port_manager):
|
async def test_close(vm, port_manager):
|
||||||
|
|
||||||
|
process = MagicMock()
|
||||||
|
process.returncode = None
|
||||||
|
process.communicate = AsyncioMagicMock(return_value=(None, None))
|
||||||
vm._start_ubridge = AsyncioMagicMock(return_value=True)
|
vm._start_ubridge = AsyncioMagicMock(return_value=True)
|
||||||
vm._ubridge_send = AsyncioMagicMock()
|
vm._ubridge_send = AsyncioMagicMock()
|
||||||
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._check_requirements", return_value=True):
|
with asyncio_patch("gns3server.compute.iou.iou_vm.IOUVM._check_requirements", return_value=True):
|
||||||
with asyncio_patch("asyncio.create_subprocess_exec", return_value=MagicMock()):
|
with asyncio_patch("asyncio.create_subprocess_exec", return_value=process):
|
||||||
await vm.start()
|
await vm.start()
|
||||||
port = vm.console
|
port = vm.console
|
||||||
await vm.close()
|
await vm.close()
|
||||||
|
Loading…
Reference in New Issue
Block a user