"""``push_ssid_config`` and ``set_vlan_name`` (NetOrk/netork#695). netOrk wrote both itself, as UCI through the driver's ``cli()``, and never looked at what came back. Here each is a checked step (NAPALM/napalm-openwrt#35): a refused one raises and reverts what was staged. ``uci -q show wireless`` is the shape of ``test_wireless_writes``; ``uci -q show network`` the shape OpenWrt 23.05 prints a DSA bridge in, with one anonymous and one named ``bridge-vlan`` section. """ from __future__ import annotations import pytest from napalm.base.exceptions import CommandErrorException from napalm_device_types import CommandResult from tests.unit.test_wireless_writes import INVALID_ARGUMENT, SHOW, _Ap, _driver, _uci SHOW_NETWORK = "uci -q show network" NETWORK = """\ network.loopback=interface network.loopback.device='lo' network.loopback.proto='static' network.apbr=device network.apbr.name='br-ap' network.apbr.type='bridge' network.apbr.ports='eth0' network.apbr.vlan_filtering='1' network.@bridge-vlan[0]=bridge-vlan network.@bridge-vlan[0].device='br-ap' network.@bridge-vlan[0].vlan='10' network.@bridge-vlan[0].ports='eth0:u*' network.apbr_vlan_20=bridge-vlan network.apbr_vlan_20.device='br-ap' network.apbr_vlan_20.vlan='20' network.apbr_vlan_20.ports='eth0:t' network.apbr_vlan_20.name='Guests' """ class _NetAp(_Ap): def __call__(self, command: str, **kwargs: object) -> CommandResult: # type: ignore[override] if command == SHOW_NETWORK and command not in self.refuse: self.commands.append(command) return CommandResult(NETWORK, "", 0) return super().__call__(command, **kwargs) # type: ignore[arg-type] # -- push_ssid_config -------------------------------------------------------------------------- def test_each_setting_is_written_to_every_section_with_the_ssid_then_wifi_reloads(): ap = _Ap() _driver(ap).push_ssid_config( "CorpWiFi", {"hidden": True, "enabled": False, "max_inactivity": 300} ) assert ap.commands == [ SHOW, _uci("set", "wireless.default_radio0.hidden=1"), _uci("set", "wireless.default_radio0.disabled=1"), _uci("set", "wireless.default_radio0.max_inactivity=300"), _uci("set", "wireless.default_radio1.hidden=1"), _uci("set", "wireless.default_radio1.disabled=1"), _uci("set", "wireless.default_radio1.max_inactivity=300"), _uci("commit", "wireless"), "wifi reload", ] def test_encryption_and_key_are_written_as_given(): """Whether a keyed mode may go without its key is the caller's policy, not the driver's.""" ap = _Ap() _driver(ap).push_ssid_config( "Bob's Guest", {"encryption": "sae", "key": "it's a secret", "disassoc_low_ack": False} ) assert ap.commands[1:4] == [ _uci("set", "wireless.guest.encryption=sae"), _uci("set", "wireless.guest.key=it's a secret"), _uci("set", "wireless.guest.disassoc_low_ack=0"), ] def test_enabled_is_written_as_disabled_off(): ap = _Ap() _driver(ap).push_ssid_config("Bob's Guest", {"enabled": True}) assert _uci("set", "wireless.guest.disabled=0") in ap.commands def test_an_empty_config_sends_nothing(): ap = _Ap() _driver(ap).push_ssid_config("CorpWiFi", {}) assert ap.commands == [] @pytest.mark.parametrize( "config", [ {"colour": "blue"}, {"max_inactivity": -1}, {"max_inactivity": "300"}, {"max_inactivity": True}, ], ) def test_a_setting_the_ap_cannot_take_raises_before_anything_is_sent(config): ap = _Ap() with pytest.raises(ValueError): _driver(ap).push_ssid_config("CorpWiFi", config) # type: ignore[arg-type] assert ap.commands == [] def test_an_ssid_no_section_carries_raises_and_writes_nothing(): ap = _Ap() with pytest.raises(CommandErrorException, match="no wifi-iface carries the SSID 'Nobody'"): _driver(ap).push_ssid_config("Nobody", {"hidden": True}) assert ap.commands == [SHOW] def test_a_refused_step_raises_reverts_and_does_not_reload(): refused = _uci("set", "wireless.default_radio1.hidden=1") ap = _Ap({refused: INVALID_ARGUMENT}) with pytest.raises(CommandErrorException, match="Invalid argument"): _driver(ap).push_ssid_config("CorpWiFi", {"hidden": True}) assert ap.commands[-2:] == [refused, _uci("revert", "wireless")] assert "wifi reload" not in ap.commands # -- set_vlan_name ----------------------------------------------------------------------------- def test_a_vlan_name_is_set_on_its_bridge_vlan_and_committed(): """A label only: nothing restarts for it.""" ap = _NetAp() assert _driver(ap).set_vlan_name(10, "Staff 'A'") is True assert ap.commands == [ SHOW_NETWORK, _uci("set", "network.@bridge-vlan[0].name=Staff 'A'"), _uci("commit", "network"), ] def test_a_named_section_is_renamed_in_place(): ap = _NetAp() assert _driver(ap).set_vlan_name(20, "Visitors") is True assert _uci("set", "network.apbr_vlan_20.name=Visitors") in ap.commands def test_a_vlan_the_ap_does_not_carry_is_false_and_writes_nothing(): ap = _NetAp() assert _driver(ap).set_vlan_name(30, "IoT") is False assert ap.commands == [SHOW_NETWORK] def test_a_refused_name_raises_and_reverts(): refused = _uci("set", "network.@bridge-vlan[0].name=Staff") ap = _NetAp({refused: INVALID_ARGUMENT}) with pytest.raises(CommandErrorException, match="Invalid argument"): _driver(ap).set_vlan_name(10, "Staff") assert ap.commands[-1] == _uci("revert", "network") def test_a_failed_read_raises(): ap = _NetAp({SHOW_NETWORK: CommandResult("", "uci: Entry not found\n", 1)}) with pytest.raises(CommandErrorException, match="Entry not found"): _driver(ap).set_vlan_name(10, "Staff")