diff options
| author | omnom62 <omnom62@outlook.com> | 2026-07-06 14:54:12 +1000 |
|---|---|---|
| committer | John Estabrook <jestabro@vyos.io> | 2026-08-21 14:02:19 -0500 |
| commit | 45dc95873fd906c582fbbd5e6ca3838caf867399 (patch) | |
| tree | 71b3e53e0080da60abf9fd2381705971dfe4d507 /tests/unit/modules/test_vyos_vlan.py | |
| parent | 7a6b5e4f3a7a021cfa75faa7bf833741dfc09cff (diff) | |
| download | rest.vyos-45dc95873fd906c582fbbd5e6ca3838caf867399.tar.gz rest.vyos-45dc95873fd906c582fbbd5e6ca3838caf867399.zip | |
T8989: wave4 vyos_command, dict_op refactorT8989_wave4
* T8989: vyos_command module
* T8989: vyos_command module UAT and SIT
* T8989: vyos_command changelog
* T8989: vyos_command linter
* T8989: vyos_config module
* T8989: vyos_config module changelog
* T8989: Wave 4 vyos_config module with integration and unit tests
* T8323: vyos_system module
* T8332: vyos_system SIT and UAT
* T8323: vyos_vlan module
* T8323: vyos_vlan module
* T8323: vyos_vlan module SIT and UAT
* T8323: vyos_system module
* T8989: Wave 4 vyos_vlan reworked with dict_op engine
* T8989: Fix dict_op single-value string list handling, add vyos_system integration tests
* T8989: logging_global refactor
* T8989: migrate ntp_global, logging_global, firewall_global to dict_op engine
* T8989: vyos_nat module for REST API collection
* T8989: vyos_nat module for REST API collection, linter fixes
* T8989: vyos_ha module for REST API collection
* T8989: vyos_ha module for REST API collection
* T8989: vyos_ha module sanity and linter fixes
* T8989: vyos_ha module sanity and linter fixes
* T8989: vyos_ha module linter fixes
* T8989: vyos.rest AI comment fixes
* T8323: vyos_nat AI comment fixes
* T8989 ai fixes
* T8989: vyos_bgp_address_family dict_op
* T8989: vyos_bgp_address_family vyos_bgp_global dict_op
* T8989: dict_op refactor for firewall_*, nat, user
* T8989: dict_op refactor for firewall_*, nat, user
* T8989: dict_op refactor for ntp_global, ha
* T8989: snmp_server dict_op refactor
* T8989: snmp_server dict_op refactor
* T8989: route_map dict_op refactor
Diffstat (limited to 'tests/unit/modules/test_vyos_vlan.py')
| -rw-r--r-- | tests/unit/modules/test_vyos_vlan.py | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/tests/unit/modules/test_vyos_vlan.py b/tests/unit/modules/test_vyos_vlan.py new file mode 100644 index 0000000..58c1be1 --- /dev/null +++ b/tests/unit/modules/test_vyos_vlan.py @@ -0,0 +1,116 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import unittest + +from unittest.mock import MagicMock + +from ansible_collections.vyos.rest.plugins.modules.vyos_vlan import ( + build_commands, + get_running_config, +) + +from .base import load_fixture + + +_BASE = ["interfaces", "ethernet"] + + +class TestVyOSVlanGetRunning(unittest.TestCase): + + def setUp(self): + self.mock_vyos = MagicMock() + self.fixture = load_fixture("vlan_running.json") + self.mock_vyos.get_config = MagicMock(return_value=self.fixture) + + def test_parses_vlans(self): + result = get_running_config(self.mock_vyos) + vlan_ids = [v["vlan_id"] for v in result] + self.assertIn(10, vlan_ids) + self.assertIn(20, vlan_ids) + + def test_parses_description(self): + result = get_running_config(self.mock_vyos) + v10 = next(v for v in result if v["vlan_id"] == 10) + self.assertEqual(v10["description"], "VLAN10") + + def test_parses_address(self): + result = get_running_config(self.mock_vyos) + v10 = next(v for v in result if v["vlan_id"] == 10) + self.assertEqual(v10["address"], "192.168.10.1/24") + + def test_parses_multiple_interfaces(self): + result = get_running_config(self.mock_vyos) + v10 = next(v for v in result if v["vlan_id"] == 10) + self.assertIn("eth1", v10["interfaces"]) + self.assertIn("eth2", v10["interfaces"]) + + def test_empty_config(self): + self.mock_vyos.get_config = MagicMock(return_value={}) + result = get_running_config(self.mock_vyos) + self.assertEqual(result, []) + + +class TestVyOSVlanBuildCommands(unittest.TestCase): + + def _have(self): + return [ + { + "vlan_id": 10, + "interfaces": ["eth1"], + "description": "VLAN10", + "address": "192.168.10.1/24", + }, + {"vlan_id": 20, "interfaces": ["eth1"], "description": "VLAN20"}, + ] + + def test_present_new_vlan(self): + config = [{"vlan_id": 30, "description": "VLAN30", "interfaces": ["eth1"]}] + cmds = build_commands(config, [], "present") + self.assertIn( + ("set", _BASE + ["eth1", "vif", "30", "description", "VLAN30"]), + cmds, + ) + + def test_present_idempotent(self): + config = [ + { + "vlan_id": 10, + "description": "VLAN10", + "address": "192.168.10.1/24", + "interfaces": ["eth1"], + }, + {"vlan_id": 20, "description": "VLAN20", "interfaces": ["eth1"]}, + ] + cmds = build_commands(config, self._have(), "present") + self.assertEqual(cmds, []) + + def test_present_update_description(self): + config = [{"vlan_id": 10, "description": "VLAN10-new", "interfaces": ["eth1"]}] + cmds = build_commands(config, self._have(), "present") + self.assertIn( + ("set", _BASE + ["eth1", "vif", "10", "description", "VLAN10-new"]), + cmds, + ) + + def test_absent_existing(self): + config = [{"vlan_id": 10, "interfaces": ["eth1"]}] + cmds = build_commands(config, self._have(), "absent") + self.assertIn(("delete", _BASE + ["eth1", "vif", "10"]), cmds) + + def test_absent_nonexistent(self): + config = [{"vlan_id": 99, "interfaces": ["eth1"]}] + cmds = build_commands(config, self._have(), "absent") + self.assertEqual(cmds, []) + + def test_present_bare_vif(self): + config = [{"vlan_id": 30, "interfaces": ["eth1"]}] + cmds = build_commands(config, [], "present") + self.assertIn(("set", _BASE + ["eth1", "vif", "30"]), cmds) + + +if __name__ == "__main__": + unittest.main() |
