Page MenuHomeVyOS Platform

No OneTemporary

Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/changelogs/fragments/T8518-vlan-unit-tests.yml b/changelogs/fragments/T8518-vlan-unit-tests.yml
new file mode 100644
index 00000000..65fcdfd5
--- /dev/null
+++ b/changelogs/fragments/T8518-vlan-unit-tests.yml
@@ -0,0 +1,3 @@
+---
+trivial:
+ - Add unit tests for the vyos_vlan module covering present, absent, aggregate, purge, and address scenarios.
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
@@ -1,119 +1,158 @@
# (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/>.
+# Make coding more python3-ish
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
-"""
+from .vyos_module import TestVyosModule, load_fixture
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]
+ 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")

File Metadata

Mime Type
text/x-diff
Expires
Fri, Nov 6, 7:29 AM (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4545542
Default Alt Text
(9 KB)

Event Timeline