"""The SNMP agent through napalm-device-types' configure_snmp_agent (netOrk #1105). fix_snmp hard-coded community "public" and sent the config-mode command through _send_command, which waits for the exec prompt and so ran into its timeout in config mode (#6). The caller's values go through _apply_config_lines now, which waits for any prompt and reports a rejected line. """ from __future__ import annotations from unittest.mock import MagicMock import pytest from napalm_netgear.netgear_smart import NetgearSmartDriver SETTINGS = {"community": "c0mm", "allowed_sources": [], "contact": "ops", "location": "rack 1"} @pytest.fixture def driver(): d = NetgearSmartDriver("10.0.0.30", "admin", "pw") d.device = MagicMock() d.applied: list[str] = [] d._detect_cli_v7 = lambda: False d._enter_config_mode = MagicMock() d._exit_config_mode = MagicMock() d._save_config = MagicMock() d._apply_config_lines = lambda text: d.applied.extend(text.splitlines()) or [] d._send_command = MagicMock( return_value="Community-String Community-Access\nc0mm Read Only\n" ) return d def test_community_contact_and_location_through_the_config_helper(driver): result = driver.configure_snmp_agent(SETTINGS) assert result["success"] is True assert driver.applied == [ "snmp-server community c0mm ro", 'snmp-server contact "ops"', 'snmp-server location "rack 1"', ] driver._exit_config_mode.assert_called_once() driver._save_config.assert_called_once() def test_config_mode_is_left_even_when_a_line_is_refused(driver): driver._apply_config_lines = lambda text: [" 'snmp-server community c0mm ro': Invalid input"] result = driver.configure_snmp_agent(SETTINGS) assert result["success"] is False driver._exit_config_mode.assert_called_once() driver._save_config.assert_not_called() def test_v7_firmware_says_it_has_no_snmp_cli(driver): driver._detect_cli_v7 = lambda: True result = driver.configure_snmp_agent(SETTINGS) assert result["success"] is False assert "web UI" in result["output"] and "warning" not in result["output"] @pytest.mark.parametrize("community", ["two words", 'q"uote', ""]) def test_a_community_that_would_break_the_cli_is_refused(driver, community): result = driver.configure_snmp_agent({"community": community, "allowed_sources": []}) assert result["success"] is False assert driver.applied == [] def test_the_old_action_is_gone(): assert not hasattr(NetgearSmartDriver, "run_device_action") assert not hasattr(NetgearSmartDriver, "_action_fix_snmp")