import pytest from unittest.mock import MagicMock, patch from infra_cli.dns import DNSManager class MockConfig: def get_node(self, name): return {"host": "10.32.2.1", "pass": "secret"} def get(self, key, default=None): if key == 'proxmox.dnsmasq_lxc_id': return '11209' return default @patch('infra_cli.dns.SSHClient') def test_dns_add_host_logic(mock_ssh): mock_instance = mock_ssh.return_value # Calls in order: # 1. touch (success 0) # 2. grep (fail 1, not found) # 3. echo (success 0) # 4. test (success 0) # 5. reload (success 0) res_touch = MagicMock(returncode=0) res_mac_check = MagicMock(returncode=1) res_echo = MagicMock(returncode=0) res_test = MagicMock(returncode=0) res_reload = MagicMock(returncode=0) mock_instance.run.side_effect = [res_touch, res_mac_check, res_echo, res_test, res_reload] mgr = DNSManager(MockConfig()) mgr.add_host("aa:bb:cc:11:22:33", "10.32.70.100", "test-host") # Verify the commands called calls = [str(c) for c in mock_instance.run.call_args_list] assert any("dhcp-host=aa:bb:cc:11:22:33,test-host,10.32.70.100" in c for c in calls) @patch('infra_cli.dns.SSHClient') def test_dns_duplicate_mac_prevention(mock_ssh): mock_instance = mock_ssh.return_value # 1. touch (0) # 2. grep (0, exists) mock_instance.run.side_effect = [MagicMock(returncode=0), MagicMock(returncode=0)] mgr = DNSManager(MockConfig()) with pytest.raises(ValueError, match="already exists"): mgr.add_host("aa:bb:cc:11:22:33", "10.32.70.100", "test-host")