diff options
| author | Yuriy Andamasov <yuriy@vyos.io> | 2026-05-16 00:34:13 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-05-16 07:34:13 +1000 |
| commit | e8acd05dd327bd4eb2419de1b931960feddec427 (patch) | |
| tree | 99a2ac69a179a5db2a1928abbd0b52275152abd2 /tests | |
| parent | be77e3b4b870ae2fb8ec4a75794a960c422a8449 (diff) | |
| download | vyos.vyos-e8acd05dd327bd4eb2419de1b931960feddec427.tar.gz vyos.vyos-e8acd05dd327bd4eb2419de1b931960feddec427.zip | |
T8518: add unit tests for vyos_vlan module (#471)
Adds 7 unit tests for vyos_vlan covering present, present_no_change,
absent, absent_no_change, aggregate, purge, and address scenarios.
Fixture data moved to dedicated .cfg files under fixtures/.
🤖 Generated by [robots](https://vyos.io)
Co-authored-by: omnom62 <75066712+omnom62@users.noreply.github.com>
Co-authored-by: Daniil Baturin <daniil@baturin.org>
Diffstat (limited to 'tests')
3 files changed, 98 insertions, 42 deletions
diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_vlan_show_interfaces.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_vlan_show_interfaces.cfg new file mode 100644 index 00000000..b877a4b6 --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_vlan_show_interfaces.cfg @@ -0,0 +1,10 @@ +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 diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_vlan_show_interfaces_empty.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_vlan_show_interfaces_empty.cfg new file mode 100644 index 00000000..06ae56a0 --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_vlan_show_interfaces_empty.cfg @@ -0,0 +1,7 @@ +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 +eth1 - u/u +eth2 - u/u +lo 127.0.0.1/8 u/u diff --git a/tests/unit/modules/network/vyos/test_vyos_vlan.py b/tests/unit/modules/network/vyos/test_vyos_vlan.py index 4f2ea69a..5bbf87c2 100644 --- a/tests/unit/modules/network/vyos/test_vyos_vlan.py +++ b/tests/unit/modules/network/vyos/test_vyos_vlan.py @@ -15,6 +15,7 @@ # You should have received a copy of the GNU General Public License # along with Ansible. If not, see <http://www.gnu.org/licenses/>. +# Make coding more python3-ish from __future__ import absolute_import, division, print_function @@ -25,21 +26,7 @@ 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 -""" +from .vyos_module import TestVyosModule, load_fixture class TestVyosVlanModule(TestVyosModule): @@ -65,55 +52,107 @@ class TestVyosVlanModule(TestVyosModule): 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] + if filename == "empty": + self.run_commands.return_value = [ + load_fixture("vyos_vlan_show_interfaces_empty.cfg"), + ] + else: + self.run_commands.return_value = [ + load_fixture("vyos_vlan_show_interfaces.cfg"), + ] + + def test_vyos_vlan_present(self): + """Create a new VLAN with a description on eth2 (not in have).""" + 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_purge_no_bare_interfaces(self): - """Purge should only delete VLANs, not bare interfaces without vlan_id.""" + def test_vyos_vlan_present_no_change(self): + """Existing VLAN 100 on eth0 — no commands should be generated.""" set_module_args( dict( vlan_id=100, + name="vlan-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), + self.execute_module(changed=False, commands=[]) + + def test_vyos_vlan_absent(self): + """Delete an existing VLAN (200 on eth1).""" + set_module_args( + dict( + vlan_id=200, + interfaces=["eth1"], + state="absent", ) - # eth1.200 should be purged since it's not in the desired state - self.assertIn( - "delete interfaces ethernet eth1 vif 200", - result.get("commands", []), ) + commands = ["delete interfaces ethernet eth1 vif 200"] + self.execute_module(changed=True, commands=commands) - def test_vyos_vlan_present(self): + def test_vyos_vlan_absent_no_change(self): + """Delete a VLAN that does not exist — no commands.""" set_module_args( dict( - vlan_id=300, - name="vlan-300", - interfaces=["eth2"], - state="present", - ), + vlan_id=999, + interfaces=["eth0"], + state="absent", + ) + ) + self.execute_module(changed=False, commands=[]) + + def test_vyos_vlan_aggregate(self): + """Create two new VLANs via aggregate; neither is in have.""" + set_module_args( + dict( + aggregate=[ + dict(vlan_id=301, interfaces=["eth2"], name="vlan-301"), + dict(vlan_id=302, interfaces=["eth2"], name="vlan-302"), + ], + ) ) commands = [ - "set interfaces ethernet eth2 vif 300 description vlan-300", + "set interfaces ethernet eth2 vif 301 description vlan-301", + "set interfaces ethernet eth2 vif 302 description vlan-302", ] self.execute_module(changed=True, commands=commands) - def test_vyos_vlan_absent(self): + def test_vyos_vlan_purge(self): + """Purge VLANs not in want. Want only VLAN 100; VLAN 200 should be removed. + + The fixed parser only maps ethX.Y sub-interfaces, so bare ethX interfaces + (vlan_id=None) are not in have. Only real VLANs (eth1.200) are purged. + """ set_module_args( dict( vlan_id=100, interfaces=["eth0"], - state="absent", - ), + state="present", + purge=True, + ) ) commands = [ - "delete interfaces ethernet eth0 vif 100", + "delete interfaces ethernet eth1 vif 200", ] self.execute_module(changed=True, commands=commands) + + def test_vyos_vlan_with_address(self): + """Create a VLAN with an IP address and no description.""" + set_module_args( + dict( + vlan_id=400, + address="10.10.40.1/24", + interfaces=["eth1"], + state="present", + ) + ) + commands = ["set interfaces ethernet eth1 vif 400 address 10.10.40.1/24"] + self.execute_module(changed=True, commands=commands, filename="empty") |
