Skip to content

Commit e4715eb

Browse files
committed
fix(proxmox): handle AttributeError when Qemu Agent is not available
- Add AttributeError to existing exception handling in _get_vm_hostname() - Fall back to using vm_name when Qemu Agent is unavailable - Add test case for AttributeError scenario - Maintain existing logging behavior for debugging Fixes #21300 Signed-off-by: puretension <rlrlfhtm5@gmail.com>
1 parent b065a49 commit e4715eb

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

proxmox/datadog_checks/proxmox/check.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ def _get_vm_hostname(self, vm_id, vm_name, node):
153153
self.config.proxmox_server,
154154
e,
155155
)
156-
hostname_json = {}
156+
hostname = vm_name
157+
return hostname
157158
hostname = hostname_json.get("data", {}).get("result", {}).get("host-name", vm_name)
158159
return hostname
159160

proxmox/tests/test_unit.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,36 @@ def test_get_hostname_error(dd_run_check, aggregator, instance, caplog):
299299
)
300300

301301

302+
class AttributeErrorResponse:
303+
"""Mock response that raises AttributeError when json() is called."""
304+
305+
def json(self):
306+
raise AttributeError("Qemu Agent not available")
307+
308+
309+
@pytest.mark.parametrize(
310+
('mock_http_get'),
311+
[
312+
pytest.param(
313+
{'http_error': {'/api2/json/nodes/ip-122-82-3-112/qemu/100/agent/get-host-name': AttributeErrorResponse()}},
314+
id='attribute_error',
315+
),
316+
],
317+
indirect=['mock_http_get'],
318+
)
319+
@pytest.mark.usefixtures('mock_http_get')
320+
def test_get_hostname_attribute_error(dd_run_check, aggregator, instance, caplog):
321+
"""Test that AttributeError is handled when Qemu Agent is not available."""
322+
check = ProxmoxCheck('proxmox', {}, [instance])
323+
check.check_id = 'test:123'
324+
caplog.set_level(logging.INFO)
325+
326+
dd_run_check(check)
327+
328+
aggregator.assert_metric("proxmox.vm.up", 1, tags=[], hostname="VM 100")
329+
assert "Failed to get hostname for vm 100 on node ip-122-82-3-112" in caplog.text
330+
331+
302332
@pytest.mark.usefixtures('mock_http_get')
303333
def test_external_tags(dd_run_check, aggregator, instance, datadog_agent):
304334
check = ProxmoxCheck('proxmox', {}, [instance])

0 commit comments

Comments
 (0)