diff options
| author | omnom62 <75066712+omnom62@users.noreply.github.com> | 2026-05-02 08:26:17 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-05-02 08:26:17 +1000 |
| commit | c3d68af3525509930acb5f843d9456011f8460ca (patch) | |
| tree | 54d3b713bbc47118f033abd11c31a106b8aa10c1 | |
| parent | 785096c533d6ad3f11efcb6e8de0fc446547b1c4 (diff) | |
| parent | a2ee09579a75165ae691efcbf3e952db83145b83 (diff) | |
| download | vyos.vyos-dependabot/github_actions/ansible/team-devtools/dot-github/workflows/ah_token_refresh.yml-26.4.0.tar.gz vyos.vyos-dependabot/github_actions/ansible/team-devtools/dot-github/workflows/ah_token_refresh.yml-26.4.0.zip | |
Merge branch 'main' into dependabot/github_actions/ansible/team-devtools/dot-github/workflows/ah_token_refresh.yml-26.4.0dependabot/github_actions/ansible/team-devtools/dot-github/workflows/ah_token_refresh.yml-26.4.0
| -rw-r--r-- | changelogs/fragments/fix-vlan-purge.yml | 3 | ||||
| -rw-r--r-- | plugins/modules/vyos_vlan.py | 12 | ||||
| -rw-r--r-- | tests/unit/modules/network/vyos/test_vyos_vlan.py | 119 |
3 files changed, 126 insertions, 8 deletions
diff --git a/changelogs/fragments/fix-vlan-purge.yml b/changelogs/fragments/fix-vlan-purge.yml new file mode 100644 index 00000000..bbc1d08a --- /dev/null +++ b/changelogs/fragments/fix-vlan-purge.yml @@ -0,0 +1,3 @@ +--- +bugfixes: + - vyos_vlan - fix purge generating invalid ``delete ... vif None`` commands for bare interfaces without VLAN sub-interfaces. diff --git a/plugins/modules/vyos_vlan.py b/plugins/modules/vyos_vlan.py index f0b68bc9..9d23cc7c 100644 --- a/plugins/modules/vyos_vlan.py +++ b/plugins/modules/vyos_vlan.py @@ -279,15 +279,11 @@ def map_config_to_obj(module): obj = {} eth = splitted_line[0].strip("'") - if eth.startswith("eth"): + if eth.startswith("eth") and "." in eth: obj["interfaces"] = [] - if "." in eth: - interface = eth.split(".")[0] - obj["interfaces"].append(interface) - obj["vlan_id"] = eth.split(".")[-1] - else: - obj["interfaces"].append(eth) - obj["vlan_id"] = None + interface = eth.split(".")[0] + obj["interfaces"].append(interface) + obj["vlan_id"] = eth.split(".")[-1] if splitted_line[1].strip("'") != "-": obj["address"] = splitted_line[1].strip("'") diff --git a/tests/unit/modules/network/vyos/test_vyos_vlan.py b/tests/unit/modules/network/vyos/test_vyos_vlan.py new file mode 100644 index 00000000..4f2ea69a --- /dev/null +++ b/tests/unit/modules/network/vyos/test_vyos_vlan.py @@ -0,0 +1,119 @@ +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +from unittest.mock import patch + +from ansible_collections.vyos.vyos.plugins.modules import vyos_vlan +from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args + +from .vyos_module import TestVyosModule + + +SHOW_INTERFACES_OUTPUT = """\ +Codes: S - State, L - Link, u - Up, D - Down, A - Admin Down +Interface IP Address S/L Description +--------- ---------- --- ----------- +eth0 10.0.2.15/24 u/u +eth0.100 - u/u vlan-100 +eth1 - u/u +eth1.200 192.0.2.1/24 u/u vlan-200 +eth2 - u/u +lo 127.0.0.1/8 u/u + ::1/128 +""" + + +class TestVyosVlanModule(TestVyosModule): + module = vyos_vlan + + def setUp(self): + super(TestVyosVlanModule, self).setUp() + + self.mock_load_config = patch( + "ansible_collections.vyos.vyos.plugins.modules.vyos_vlan.load_config", + ) + self.load_config = self.mock_load_config.start() + + self.mock_run_commands = patch( + "ansible_collections.vyos.vyos.plugins.modules.vyos_vlan.run_commands", + ) + self.run_commands = self.mock_run_commands.start() + + def tearDown(self): + super(TestVyosVlanModule, self).tearDown() + self.mock_load_config.stop() + self.mock_run_commands.stop() + + def load_fixtures(self, commands=None, filename=None): + self.load_config.return_value = dict(diff=None, session="session") + self.run_commands.return_value = [SHOW_INTERFACES_OUTPUT] + + def test_vyos_vlan_purge_no_bare_interfaces(self): + """Purge should only delete VLANs, not bare interfaces without vlan_id.""" + set_module_args( + dict( + vlan_id=100, + interfaces=["eth0"], + state="present", + purge=True, + ), + ) + result = self.execute_module(changed=True) + # Should only delete eth1.200, not bare eth0/eth1/eth2 with vif None + for cmd in result.get("commands", []): + self.assertNotIn( + "vif None", + cmd, + "Purge generated 'vif None' command for bare interface: {0}".format(cmd), + ) + # eth1.200 should be purged since it's not in the desired state + self.assertIn( + "delete interfaces ethernet eth1 vif 200", + result.get("commands", []), + ) + + def test_vyos_vlan_present(self): + set_module_args( + dict( + vlan_id=300, + name="vlan-300", + interfaces=["eth2"], + state="present", + ), + ) + commands = [ + "set interfaces ethernet eth2 vif 300 description vlan-300", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_vlan_absent(self): + set_module_args( + dict( + vlan_id=100, + interfaces=["eth0"], + state="absent", + ), + ) + commands = [ + "delete interfaces ethernet eth0 vif 100", + ] + self.execute_module(changed=True, commands=commands) |
