| 1 |
# Copyright (c) 2025 Ian (@aph3rson) |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from io import StringIO |
| 8 |
from unittest.mock import MagicMock, mock_open, patch |
| 9 |
|
| 10 |
import pytest |
| 11 |
from ansible.errors import AnsibleConnectionFailure, AnsibleError |
| 12 |
from ansible.playbook.play_context import PlayContext |
| 13 |
from ansible.plugins.loader import connection_loader |
| 14 |
|
| 15 |
from ansible_collections.community.proxmox.plugins.connection import proxmox_qemu_api as qemu_module |
| 16 |
|
| 17 |
proxmoxer = pytest.importorskip("proxmoxer") |
| 18 |
|
| 19 |
# Test constants |
| 20 |
TEST_API_HOST = "pve.example.com" |
| 21 |
TEST_API_PORT = 8006 |
| 22 |
TEST_NODE = "pve-1" |
| 23 |
TEST_VMID = 100 |
| 24 |
TEST_CONNECT_TIMEOUT = 60 |
| 25 |
|
| 26 |
|
| 27 |
@pytest.fixture |
| 28 |
def connection(): |
| 29 |
"""Create a connection instance with minimal required options.""" |
| 30 |
play_context = PlayContext() |
| 31 |
in_stream = StringIO() |
| 32 |
conn = connection_loader.get("community.proxmox.proxmox_qemu_api", play_context, in_stream) |
| 33 |
conn.set_option("api_host", TEST_API_HOST) |
| 34 |
conn.set_option("api_port", TEST_API_PORT) |
| 35 |
conn.set_option("api_token_id", "user@pam!token") |
| 36 |
conn.set_option("api_token_secret", "secret-uuid") |
| 37 |
conn.set_option("node", TEST_NODE) |
| 38 |
conn.set_option("vmid", TEST_VMID) |
| 39 |
conn.set_option("validate_certs", True) |
| 40 |
conn.set_option("remote_tmp", "/tmp") |
| 41 |
conn.set_option("executable", "/bin/sh") |
| 42 |
conn.set_option("connect_timeout", TEST_CONNECT_TIMEOUT) |
| 43 |
return conn |
| 44 |
|
| 45 |
|
| 46 |
def test_connection_options(connection): |
| 47 |
"""Test that connection options are properly set.""" |
| 48 |
assert connection.get_option("api_host") == "pve.example.com" |
| 49 |
assert connection.get_option("api_port") == TEST_API_PORT |
| 50 |
assert connection.get_option("node") == "pve-1" |
| 51 |
assert connection.get_option("vmid") == TEST_VMID |
| 52 |
assert connection.get_option("validate_certs") is True |
| 53 |
assert connection.get_option("remote_tmp") == "/tmp" |
| 54 |
assert connection.get_option("executable") == "/bin/sh" |
| 55 |
assert connection.get_option("connect_timeout") == TEST_CONNECT_TIMEOUT |
| 56 |
|
| 57 |
|
| 58 |
def test_transport_name(connection): |
| 59 |
"""Test that the transport name is correct.""" |
| 60 |
assert connection.transport == "community.proxmox.proxmox_qemu_api" |
| 61 |
|
| 62 |
|
| 63 |
def test_has_pipelining(connection): |
| 64 |
"""Test that pipelining is disabled.""" |
| 65 |
assert connection.has_pipelining is False |
| 66 |
|
| 67 |
|
| 68 |
def test_has_tty(connection): |
| 69 |
"""Test that TTY is disabled.""" |
| 70 |
assert connection.has_tty is False |
| 71 |
|
| 72 |
|
| 73 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 74 |
def test_get_proxmox_with_token(mock_api, connection): |
| 75 |
"""Test ProxmoxAPI initialization with token authentication.""" |
| 76 |
connection._get_proxmox() |
| 77 |
|
| 78 |
mock_api.assert_called_once_with( |
| 79 |
"pve.example.com", |
| 80 |
port=TEST_API_PORT, |
| 81 |
user="user@pam", |
| 82 |
token_name="token", |
| 83 |
token_value="secret-uuid", |
| 84 |
verify_ssl=True, |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 89 |
def test_get_proxmox_with_password(mock_api, connection): |
| 90 |
"""Test ProxmoxAPI initialization with password authentication.""" |
| 91 |
connection.set_option("api_token_id", None) |
| 92 |
connection.set_option("api_token_secret", None) |
| 93 |
connection.set_option("api_user", "root@pam") |
| 94 |
connection.set_option("api_password", "password123") |
| 95 |
|
| 96 |
connection._get_proxmox() |
| 97 |
|
| 98 |
mock_api.assert_called_once_with( |
| 99 |
"pve.example.com", |
| 100 |
port=TEST_API_PORT, |
| 101 |
user="root@pam", |
| 102 |
password="password123", |
| 103 |
verify_ssl=True, |
| 104 |
) |
| 105 |
|
| 106 |
|
| 107 |
def test_get_proxmox_no_auth(connection): |
| 108 |
"""Test that missing authentication raises an error.""" |
| 109 |
connection.set_option("api_token_id", None) |
| 110 |
connection.set_option("api_token_secret", None) |
| 111 |
connection.set_option("api_user", None) |
| 112 |
connection.set_option("api_password", None) |
| 113 |
|
| 114 |
with pytest.raises(AnsibleConnectionFailure, match="No authentication configured"): |
| 115 |
connection._get_proxmox() |
| 116 |
|
| 117 |
|
| 118 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 119 |
def test_get_proxmox_caches_instance(mock_api, connection): |
| 120 |
"""Test that ProxmoxAPI is only created once.""" |
| 121 |
connection._get_proxmox() |
| 122 |
connection._get_proxmox() |
| 123 |
|
| 124 |
assert mock_api.call_count == 1 |
| 125 |
|
| 126 |
|
| 127 |
@patch.object(qemu_module, "HAS_PROXMOXER", False) |
| 128 |
def test_get_proxmox_missing_library(connection): |
| 129 |
"""Test that missing proxmoxer library raises an error.""" |
| 130 |
connection._proxmox = None |
| 131 |
|
| 132 |
with pytest.raises(AnsibleError, match="requires the 'proxmoxer' library"): |
| 133 |
connection._get_proxmox() |
| 134 |
|
| 135 |
|
| 136 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 137 |
def test_agent_returns_correct_path(mock_api, connection): |
| 138 |
"""Test that _agent() builds the correct API path.""" |
| 139 |
mock_proxmox = MagicMock() |
| 140 |
mock_api.return_value = mock_proxmox |
| 141 |
|
| 142 |
connection._get_proxmox() |
| 143 |
connection._agent() |
| 144 |
|
| 145 |
mock_proxmox.nodes.assert_called_once_with("pve-1") |
| 146 |
mock_proxmox.nodes("pve-1").qemu.assert_called_once_with(100) |
| 147 |
|
| 148 |
|
| 149 |
@patch.object(qemu_module.time, "sleep") |
| 150 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 151 |
def test_connect_success(mock_api, mock_sleep, connection): |
| 152 |
"""Test successful connection to guest agent.""" |
| 153 |
mock_proxmox = MagicMock() |
| 154 |
mock_api.return_value = mock_proxmox |
| 155 |
|
| 156 |
connection._connect() |
| 157 |
|
| 158 |
assert connection._connected is True |
| 159 |
mock_sleep.assert_not_called() |
| 160 |
|
| 161 |
|
| 162 |
@patch.object(qemu_module.time, "sleep") |
| 163 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 164 |
def test_connect_retries_on_failure(mock_api, mock_sleep, connection): |
| 165 |
"""Test that connection retries when guest agent is not ready.""" |
| 166 |
mock_proxmox = MagicMock() |
| 167 |
mock_api.return_value = mock_proxmox |
| 168 |
|
| 169 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 170 |
agent.ping.post.side_effect = [Exception("not ready"), Exception("not ready"), None] |
| 171 |
|
| 172 |
connection.set_option("connect_timeout", 15) |
| 173 |
connection._connect() |
| 174 |
|
| 175 |
assert connection._connected is True |
| 176 |
assert mock_sleep.call_count == 2 # noqa: PLR2004 |
| 177 |
|
| 178 |
|
| 179 |
@patch.object(qemu_module.time, "sleep") |
| 180 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 181 |
def test_connect_timeout(mock_api, mock_sleep, connection): |
| 182 |
"""Test that connection fails after timeout.""" |
| 183 |
mock_proxmox = MagicMock() |
| 184 |
mock_api.return_value = mock_proxmox |
| 185 |
|
| 186 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 187 |
agent.ping.post.side_effect = Exception("not ready") |
| 188 |
|
| 189 |
connection.set_option("connect_timeout", 10) |
| 190 |
|
| 191 |
with pytest.raises(AnsibleConnectionFailure, match="QEMU guest agent is not responding"): |
| 192 |
connection._connect() |
| 193 |
|
| 194 |
|
| 195 |
def test_connect_already_connected(connection): |
| 196 |
"""Test that _connect() is a no-op when already connected.""" |
| 197 |
connection._connected = True |
| 198 |
result = connection._connect() |
| 199 |
assert result is connection |
| 200 |
|
| 201 |
|
| 202 |
@patch.object(qemu_module.time, "sleep") |
| 203 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 204 |
def test_exec_command_success(mock_api, mock_sleep, connection): |
| 205 |
"""Test successful command execution.""" |
| 206 |
mock_proxmox = MagicMock() |
| 207 |
mock_api.return_value = mock_proxmox |
| 208 |
|
| 209 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 210 |
agent.exec.post.return_value = {"pid": 42} |
| 211 |
agent("exec-status").get.return_value = {"exited": 1, "exitcode": 0, "out-data": "hello\n", "err-data": ""} |
| 212 |
|
| 213 |
connection._connected = True |
| 214 |
connection._proxmox = mock_proxmox |
| 215 |
|
| 216 |
rc, stdout, stderr = connection.exec_command("echo hello") |
| 217 |
|
| 218 |
assert rc == 0 |
| 219 |
assert stdout == "hello\n" |
| 220 |
assert stderr == "" |
| 221 |
agent.exec.post.assert_called_once_with(command=["/bin/sh", "-c", "echo hello"]) |
| 222 |
|
| 223 |
|
| 224 |
@patch.object(qemu_module.time, "sleep") |
| 225 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 226 |
def test_exec_command_nonzero_exit(mock_api, mock_sleep, connection): |
| 227 |
"""Test command execution with non-zero exit code.""" |
| 228 |
mock_proxmox = MagicMock() |
| 229 |
mock_api.return_value = mock_proxmox |
| 230 |
|
| 231 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 232 |
agent.exec.post.return_value = {"pid": 42} |
| 233 |
agent("exec-status").get.return_value = {"exited": 1, "exitcode": 1, "out-data": "", "err-data": "not found\n"} |
| 234 |
|
| 235 |
connection._connected = True |
| 236 |
connection._proxmox = mock_proxmox |
| 237 |
|
| 238 |
rc, stdout, stderr = connection.exec_command("false") |
| 239 |
|
| 240 |
assert rc == 1 |
| 241 |
assert stderr == "not found\n" |
| 242 |
|
| 243 |
|
| 244 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 245 |
def test_exec_command_api_failure(mock_api, connection): |
| 246 |
"""Test command execution when API call fails.""" |
| 247 |
mock_proxmox = MagicMock() |
| 248 |
mock_api.return_value = mock_proxmox |
| 249 |
|
| 250 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 251 |
agent.exec.post.side_effect = Exception("API error") |
| 252 |
|
| 253 |
connection._connected = True |
| 254 |
connection._proxmox = mock_proxmox |
| 255 |
|
| 256 |
with pytest.raises(AnsibleConnectionFailure, match="Failed to execute command"): |
| 257 |
connection.exec_command("echo hello") |
| 258 |
|
| 259 |
|
| 260 |
@patch.object(qemu_module.time, "sleep") |
| 261 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 262 |
def test_poll_exec_status_waits(mock_api, mock_sleep, connection): |
| 263 |
"""Test that exec status polling waits for completion.""" |
| 264 |
mock_proxmox = MagicMock() |
| 265 |
mock_api.return_value = mock_proxmox |
| 266 |
connection._proxmox = mock_proxmox |
| 267 |
|
| 268 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 269 |
agent("exec-status").get.side_effect = [ |
| 270 |
{"exited": 0}, |
| 271 |
{"exited": 0}, |
| 272 |
{"exited": 1, "exitcode": 0, "out-data": "done"}, |
| 273 |
] |
| 274 |
|
| 275 |
status = connection._poll_exec_status(42) |
| 276 |
|
| 277 |
assert status["out-data"] == "done" |
| 278 |
assert mock_sleep.call_count == 3 # noqa: PLR2004 |
| 279 |
|
| 280 |
|
| 281 |
@patch.object(qemu_module.time, "sleep") |
| 282 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 283 |
def test_file_write_success(mock_api, mock_sleep, connection): |
| 284 |
"""Test successful file write.""" |
| 285 |
mock_proxmox = MagicMock() |
| 286 |
mock_api.return_value = mock_proxmox |
| 287 |
connection._proxmox = mock_proxmox |
| 288 |
|
| 289 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 290 |
|
| 291 |
connection._file_write("/tmp/test", b"hello") |
| 292 |
|
| 293 |
agent("file-write").post.assert_called_once() |
| 294 |
call_kwargs = agent("file-write").post.call_args[1] |
| 295 |
assert call_kwargs["file"] == "/tmp/test" |
| 296 |
assert call_kwargs["encode"] == 0 |
| 297 |
|
| 298 |
|
| 299 |
@patch.object(qemu_module.time, "sleep") |
| 300 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 301 |
def test_file_write_retries(mock_api, mock_sleep, connection): |
| 302 |
"""Test that file write retries on failure.""" |
| 303 |
mock_proxmox = MagicMock() |
| 304 |
mock_api.return_value = mock_proxmox |
| 305 |
connection._proxmox = mock_proxmox |
| 306 |
|
| 307 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 308 |
agent("file-write").post.side_effect = [Exception("busy"), Exception("busy"), None] |
| 309 |
|
| 310 |
connection._file_write("/tmp/test", b"hello") |
| 311 |
|
| 312 |
assert agent("file-write").post.call_count == 3 # noqa: PLR2004 |
| 313 |
|
| 314 |
|
| 315 |
@patch.object(qemu_module.time, "sleep") |
| 316 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 317 |
def test_file_write_exhausts_retries(mock_api, mock_sleep, connection): |
| 318 |
"""Test that file write fails after exhausting retries.""" |
| 319 |
mock_proxmox = MagicMock() |
| 320 |
mock_api.return_value = mock_proxmox |
| 321 |
connection._proxmox = mock_proxmox |
| 322 |
|
| 323 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 324 |
agent("file-write").post.side_effect = Exception("busy") |
| 325 |
|
| 326 |
with pytest.raises(AnsibleConnectionFailure, match="Failed to write file"): |
| 327 |
connection._file_write("/tmp/test", b"hello") |
| 328 |
|
| 329 |
|
| 330 |
@patch.object(qemu_module.time, "sleep") |
| 331 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 332 |
def test_put_file_small(mock_api, mock_sleep, connection): |
| 333 |
"""Test putting a small file (under chunk size).""" |
| 334 |
mock_proxmox = MagicMock() |
| 335 |
mock_api.return_value = mock_proxmox |
| 336 |
connection._connected = True |
| 337 |
connection._proxmox = mock_proxmox |
| 338 |
|
| 339 |
with patch("builtins.open", mock_open(read_data=b"small file")): |
| 340 |
connection.put_file("/local/path", "/remote/path") |
| 341 |
|
| 342 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 343 |
agent("file-write").post.assert_called_once() |
| 344 |
|
| 345 |
|
| 346 |
@patch.object(qemu_module.time, "sleep") |
| 347 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 348 |
def test_put_file_chunked(mock_api, mock_sleep, connection): |
| 349 |
"""Test putting a large file that requires chunking.""" |
| 350 |
mock_proxmox = MagicMock() |
| 351 |
mock_api.return_value = mock_proxmox |
| 352 |
connection._connected = True |
| 353 |
connection._proxmox = mock_proxmox |
| 354 |
|
| 355 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 356 |
agent.exec.post.return_value = {"pid": 1} |
| 357 |
agent("exec-status").get.return_value = {"exited": 1, "exitcode": 0, "out-data": "", "err-data": ""} |
| 358 |
|
| 359 |
large_data = b"x" * 100000 |
| 360 |
with patch("builtins.open", mock_open(read_data=large_data)): |
| 361 |
connection.put_file("/local/path", "/remote/path") |
| 362 |
|
| 363 |
# Should have written 3 chunks (45000 + 45000 + 10000) |
| 364 |
assert agent("file-write").post.call_count == 3 # noqa: PLR2004 |
| 365 |
# Should have run cat to assemble |
| 366 |
agent.exec.post.assert_called_once() |
| 367 |
|
| 368 |
|
| 369 |
@patch.object(qemu_module.time, "sleep") |
| 370 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 371 |
def test_put_file_chunked_uses_remote_tmp(mock_api, mock_sleep, connection): |
| 372 |
"""Test that chunked file transfer uses the configured remote_tmp.""" |
| 373 |
mock_proxmox = MagicMock() |
| 374 |
mock_api.return_value = mock_proxmox |
| 375 |
connection._connected = True |
| 376 |
connection._proxmox = mock_proxmox |
| 377 |
connection.set_option("remote_tmp", "/var/tmp") |
| 378 |
|
| 379 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 380 |
agent.exec.post.return_value = {"pid": 1} |
| 381 |
agent("exec-status").get.return_value = {"exited": 1, "exitcode": 0, "out-data": "", "err-data": ""} |
| 382 |
|
| 383 |
large_data = b"x" * 50000 |
| 384 |
with patch("builtins.open", mock_open(read_data=large_data)): |
| 385 |
connection.put_file("/local/path", "/remote/path") |
| 386 |
|
| 387 |
# Check that the cat command references /var/tmp |
| 388 |
exec_call = agent.exec.post.call_args |
| 389 |
cmd = exec_call[1]["command"][2] |
| 390 |
assert "/var/tmp/.ansible_part_" in cmd |
| 391 |
|
| 392 |
|
| 393 |
@patch.object(qemu_module.time, "sleep") |
| 394 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 395 |
def test_put_file_chunked_assemble_failure(mock_api, mock_sleep, connection): |
| 396 |
"""Test that chunked file transfer fails if assembly fails.""" |
| 397 |
mock_proxmox = MagicMock() |
| 398 |
mock_api.return_value = mock_proxmox |
| 399 |
connection._connected = True |
| 400 |
connection._proxmox = mock_proxmox |
| 401 |
|
| 402 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 403 |
agent.exec.post.return_value = {"pid": 1} |
| 404 |
agent("exec-status").get.return_value = {"exited": 1, "exitcode": 1, "out-data": "", "err-data": "disk full"} |
| 405 |
|
| 406 |
large_data = b"x" * 50000 |
| 407 |
with pytest.raises(AnsibleConnectionFailure, match="Failed to assemble chunked file"), patch( |
| 408 |
"builtins.open", mock_open(read_data=large_data) |
| 409 |
): |
| 410 |
connection.put_file("/local/path", "/remote/path") |
| 411 |
|
| 412 |
|
| 413 |
@patch.object(qemu_module.time, "sleep") |
| 414 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 415 |
def test_fetch_file_small(mock_api, mock_sleep, connection): |
| 416 |
"""Test fetching a small file (single chunk).""" |
| 417 |
mock_proxmox = MagicMock() |
| 418 |
mock_api.return_value = mock_proxmox |
| 419 |
connection._connected = True |
| 420 |
connection._proxmox = mock_proxmox |
| 421 |
|
| 422 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 423 |
agent("file-read").get.return_value = {"content": "hello world"} |
| 424 |
|
| 425 |
with patch.object(connection, "exec_command") as mock_exec: |
| 426 |
mock_exec.side_effect = [ |
| 427 |
(0, "", ""), # split |
| 428 |
(0, "/tmp/.ansible_fetch_aa\n", ""), # ls |
| 429 |
(0, "", ""), # rm |
| 430 |
] |
| 431 |
m = mock_open() |
| 432 |
with patch("builtins.open", m): |
| 433 |
connection.fetch_file("/remote/path", "/local/path") |
| 434 |
|
| 435 |
m.assert_called_with("/local/path", "wb") |
| 436 |
m().write.assert_called_once_with(b"hello world") |
| 437 |
|
| 438 |
|
| 439 |
@patch.object(qemu_module.time, "sleep") |
| 440 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 441 |
def test_fetch_file_chunked(mock_api, mock_sleep, connection): |
| 442 |
"""Test fetching a large file that requires multiple chunks.""" |
| 443 |
mock_proxmox = MagicMock() |
| 444 |
mock_api.return_value = mock_proxmox |
| 445 |
connection._connected = True |
| 446 |
connection._proxmox = mock_proxmox |
| 447 |
|
| 448 |
chunk_a = "a" * 45000 |
| 449 |
chunk_b = "b" * 45000 |
| 450 |
chunk_c = "c" * 10000 |
| 451 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 452 |
agent("file-read").get.side_effect = [ |
| 453 |
{"content": chunk_a}, |
| 454 |
{"content": chunk_b}, |
| 455 |
{"content": chunk_c}, |
| 456 |
] |
| 457 |
|
| 458 |
with patch.object(connection, "exec_command") as mock_exec: |
| 459 |
mock_exec.side_effect = [ |
| 460 |
(0, "", ""), # split |
| 461 |
(0, "/tmp/.ansible_fetch_aa\n/tmp/.ansible_fetch_ab\n/tmp/.ansible_fetch_ac\n", ""), # ls |
| 462 |
(0, "", ""), # rm |
| 463 |
] |
| 464 |
m = mock_open() |
| 465 |
with patch("builtins.open", m): |
| 466 |
connection.fetch_file("/remote/path", "/local/path") |
| 467 |
|
| 468 |
assert agent("file-read").get.call_count == 3 # noqa: PLR2004 |
| 469 |
calls = m().write.call_args_list |
| 470 |
assert len(calls) == 3 # noqa: PLR2004 |
| 471 |
assert calls[0][0][0] == chunk_a.encode("latin-1") |
| 472 |
assert calls[1][0][0] == chunk_b.encode("latin-1") |
| 473 |
assert calls[2][0][0] == chunk_c.encode("latin-1") |
| 474 |
|
| 475 |
|
| 476 |
@patch.object(qemu_module.time, "sleep") |
| 477 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 478 |
def test_fetch_file_binary(mock_api, mock_sleep, connection): |
| 479 |
"""Test fetching a binary file preserves bytes via latin-1 encoding.""" |
| 480 |
mock_proxmox = MagicMock() |
| 481 |
mock_api.return_value = mock_proxmox |
| 482 |
connection._connected = True |
| 483 |
connection._proxmox = mock_proxmox |
| 484 |
|
| 485 |
binary_as_str = "".join(chr(i) for i in range(256)) |
| 486 |
agent = mock_proxmox.nodes("pve-1").qemu(TEST_VMID).agent |
| 487 |
agent("file-read").get.return_value = {"content": binary_as_str} |
| 488 |
|
| 489 |
with patch.object(connection, "exec_command") as mock_exec: |
| 490 |
mock_exec.side_effect = [ |
| 491 |
(0, "", ""), # split |
| 492 |
(0, "/tmp/.ansible_fetch_aa\n", ""), # ls |
| 493 |
(0, "", ""), # rm |
| 494 |
] |
| 495 |
m = mock_open() |
| 496 |
with patch("builtins.open", m): |
| 497 |
connection.fetch_file("/remote/path", "/local/path") |
| 498 |
|
| 499 |
written = m().write.call_args[0][0] |
| 500 |
assert written == bytes(range(256)) |
| 501 |
|
| 502 |
|
| 503 |
@patch.object(qemu_module.time, "sleep") |
| 504 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 505 |
def test_fetch_file_split_failure(mock_api, mock_sleep, connection): |
| 506 |
"""Test that fetch fails if split command fails.""" |
| 507 |
mock_proxmox = MagicMock() |
| 508 |
mock_api.return_value = mock_proxmox |
| 509 |
connection._connected = True |
| 510 |
connection._proxmox = mock_proxmox |
| 511 |
|
| 512 |
with patch.object(connection, "exec_command") as mock_exec: |
| 513 |
mock_exec.return_value = (1, "", "No such file") |
| 514 |
with pytest.raises(AnsibleConnectionFailure, match="Failed to split file"): |
| 515 |
connection.fetch_file("/remote/path", "/local/path") |
| 516 |
|
| 517 |
|
| 518 |
def test_close(connection): |
| 519 |
"""Test connection close.""" |
| 520 |
connection._connected = True |
| 521 |
connection._proxmox = MagicMock() |
| 522 |
|
| 523 |
connection.close() |
| 524 |
|
| 525 |
assert connection._connected is False |
| 526 |
assert connection._proxmox is None |
| 527 |
|
| 528 |
|
| 529 |
@patch.object(qemu_module.time, "sleep") |
| 530 |
@patch.object(qemu_module, "ProxmoxAPI") |
| 531 |
def test_reset(mock_api, mock_sleep, connection): |
| 532 |
"""Test connection reset closes and reconnects.""" |
| 533 |
mock_proxmox = MagicMock() |
| 534 |
mock_api.return_value = mock_proxmox |
| 535 |
connection._connected = True |
| 536 |
connection._proxmox = mock_proxmox |
| 537 |
|
| 538 |
connection.reset() |
| 539 |
|
| 540 |
# After reset, should be connected again |
| 541 |
assert connection._connected is True |