diff options
| author | omnom62 <75066712+omnom62@users.noreply.github.com> | 2026-08-21 23:41:34 +1000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-08-21 08:41:34 -0500 |
| commit | 6dc9d721513dca5a573c7ee01b74d703ff41f95f (patch) | |
| tree | af2ccddbe4050547ab1f12043f3a4c06c03b4365 /tests | |
| parent | 0fe5f71123e5ff1d489a91486076fc875fa2810e (diff) | |
| download | rest.vyos-6dc9d721513dca5a573c7ee01b74d703ff41f95f.tar.gz rest.vyos-6dc9d721513dca5a573c7ee01b74d703ff41f95f.zip | |
T8989: prefix_lists module
* prefix_lists module
* prefix_lists module rst
* t8989 prefix_lists module
* Converage tests fixes
* T8989: SIT updates
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/integration/targets/vyos_prefix_lists/tests/httpapi/deleted.yaml (renamed from tests/integration/targets/vyos_prefix_lists/tests/httpapi/_deleted.yaml) | 0 | ||||
| -rw-r--r-- | tests/integration/targets/vyos_prefix_lists/tests/httpapi/gathered.yaml (renamed from tests/integration/targets/vyos_prefix_lists/tests/httpapi/_gathered.yaml) | 0 | ||||
| -rw-r--r-- | tests/integration/targets/vyos_prefix_lists/tests/httpapi/merged.yaml (renamed from tests/integration/targets/vyos_prefix_lists/tests/httpapi/_merged.yaml) | 0 | ||||
| -rw-r--r-- | tests/integration/targets/vyos_prefix_lists/tests/httpapi/overridden.yaml | 38 | ||||
| -rw-r--r-- | tests/integration/targets/vyos_prefix_lists/tests/httpapi/replaced.yaml (renamed from tests/integration/targets/vyos_prefix_lists/tests/httpapi/_replaced.yaml) | 0 | ||||
| -rw-r--r-- | tests/integration/targets/vyos_prefix_lists/tests/httpapi/rtt.yaml | 81 | ||||
| -rw-r--r-- | tests/unit/fixtures/prefix_lists_running.json | 9 | ||||
| -rw-r--r-- | tests/unit/modules/test_vyos_prefix_lists.py | 222 |
8 files changed, 344 insertions, 6 deletions
diff --git a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/_deleted.yaml b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/deleted.yaml index 6cfb063..6cfb063 100644 --- a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/_deleted.yaml +++ b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/deleted.yaml diff --git a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/_gathered.yaml b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/gathered.yaml index 083cb48..083cb48 100644 --- a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/_gathered.yaml +++ b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/gathered.yaml diff --git a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/_merged.yaml b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/merged.yaml index a70f826..a70f826 100644 --- a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/_merged.yaml +++ b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/merged.yaml diff --git a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/overridden.yaml b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/overridden.yaml new file mode 100644 index 0000000..f895488 --- /dev/null +++ b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/overridden.yaml @@ -0,0 +1,38 @@ +--- +- debug: + msg: START vyos_prefix_lists overridden integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml +- include_tasks: _populate_config.yaml + +- block: + - name: Override prefix_lists configuration + register: result + vyos.rest.vyos_prefix_lists: &id001 + config: + - afi: ipv4 + prefix_lists: + - name: AnsibleIPv4PrefixList + description: Overridden by ansible + entries: + - sequence: 2 + action: permit + prefix: 92.168.10.0/26 + le: 32 + state: overridden + + - assert: + that: + - result.changed == true + + - name: Override prefix_lists configuration (IDEMPOTENT) + register: result + vyos.rest.vyos_prefix_lists: *id001 + + - assert: + that: + - result.changed == false + - result.commands == [] + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/_replaced.yaml b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/replaced.yaml index bd8c5c1..bd8c5c1 100644 --- a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/_replaced.yaml +++ b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/replaced.yaml diff --git a/tests/integration/targets/vyos_prefix_lists/tests/httpapi/rtt.yaml b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/rtt.yaml new file mode 100644 index 0000000..de816eb --- /dev/null +++ b/tests/integration/targets/vyos_prefix_lists/tests/httpapi/rtt.yaml @@ -0,0 +1,81 @@ +--- +- debug: + msg: START vyos_prefix_lists round trip integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- block: + - name: RTT - Apply base configuration + vyos.rest.vyos_prefix_lists: + config: + - afi: ipv4 + prefix_lists: + - name: AnsibleIPv4PrefixList + description: PL configured by ansible + entries: + - sequence: 2 + action: permit + prefix: 92.168.10.0/26 + le: 32 + - sequence: 3 + action: deny + prefix: 72.168.2.0/24 + ge: 26 + state: merged + + - name: RTT - Gather configuration + register: gathered + vyos.rest.vyos_prefix_lists: + state: gathered + + - name: RTT - Assert gathered matches applied + vars: + ipv4_pls: >- + {{ gathered.gathered + | selectattr('afi', 'eq', 'ipv4') + | map(attribute='prefix_lists') + | first }} + assert: + that: + - gathered.gathered | selectattr('afi', 'eq', 'ipv4') | list | length == 1 + - ipv4_pls | selectattr('name', 'eq', 'AnsibleIPv4PrefixList') | list | length == 1 + + - name: RTT - Modify configuration + vyos.rest.vyos_prefix_lists: + config: + - afi: ipv4 + prefix_lists: + - name: AnsibleIPv4PrefixList + description: Modified by ansible + entries: + - sequence: 2 + action: permit + prefix: 92.168.10.0/26 + le: 32 + state: replaced + + - name: RTT - Gather modified configuration + register: gathered2 + vyos.rest.vyos_prefix_lists: + state: gathered + + - name: RTT - Assert modification applied correctly + vars: + ipv4_pls2: >- + {{ gathered2.gathered + | selectattr('afi', 'eq', 'ipv4') + | map(attribute='prefix_lists') + | first }} + pl_entries: >- + {{ ipv4_pls2 + | selectattr('name', 'eq', 'AnsibleIPv4PrefixList') + | map(attribute='entries') + | first }} + assert: + that: + - gathered2.gathered | selectattr('afi', 'eq', 'ipv4') | list | length == 1 + - pl_entries | length == 1 + - pl_entries | selectattr('sequence', 'eq', 2) | list | length == 1 + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/unit/fixtures/prefix_lists_running.json b/tests/unit/fixtures/prefix_lists_running.json index 2a6ecd3..ebc72d0 100644 --- a/tests/unit/fixtures/prefix_lists_running.json +++ b/tests/unit/fixtures/prefix_lists_running.json @@ -6,14 +6,12 @@ "2": { "action": "permit", "prefix": "92.168.10.0/26", - "le": "32", - "description": "Rule 2 given by ansible" + "le": "32" }, "3": { "action": "deny", "prefix": "72.168.2.0/24", - "ge": "26", - "description": "Rule 3" + "ge": "26" } } } @@ -25,8 +23,7 @@ "5": { "action": "permit", "prefix": "2001:db8:8000::/35", - "le": "37", - "description": "Permit rule" + "le": "37" } } }, diff --git a/tests/unit/modules/test_vyos_prefix_lists.py b/tests/unit/modules/test_vyos_prefix_lists.py new file mode 100644 index 0000000..e33de42 --- /dev/null +++ b/tests/unit/modules/test_vyos_prefix_lists.py @@ -0,0 +1,222 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +import json +import os +import unittest + +from unittest.mock import MagicMock + +from ansible_collections.vyos.rest.plugins.modules.vyos_prefix_lists import ( + _normalize, + build_commands, + get_running_config, +) + + +def load_fixture(filename): + fixtures_dir = os.path.join(os.path.dirname(__file__), "..", "fixtures") + path = os.path.join(fixtures_dir, filename) + with open(path) as f: + return json.load(f) + + +class VyOSModuleTestCase(unittest.TestCase): + def setUp(self): + self.mock_vyos = MagicMock() + self.mock_vyos.get_config = MagicMock(return_value={}) + + def set_running_config(self, data): + self.mock_vyos.get_config.return_value = data + + +class TestVyOSPrefixListsGetRunning(VyOSModuleTestCase): + + def setUp(self): + super().setUp() + self.fixture = load_fixture("prefix_lists_running.json") + + def test_parses_ipv4_prefix_list(self): + self.set_running_config(self.fixture) + result = get_running_config(self.mock_vyos) + ipv4 = next((e for e in result if e["afi"] == "ipv4"), None) + self.assertIsNotNone(ipv4) + pl = next((p for p in ipv4["prefix_lists"] if p["name"] == "AnsibleIPv4PrefixList"), None) + self.assertIsNotNone(pl) + self.assertEqual(pl["description"], "PL configured by ansible") + + def test_parses_ipv4_rules(self): + self.set_running_config(self.fixture) + result = get_running_config(self.mock_vyos) + ipv4 = next(e for e in result if e["afi"] == "ipv4") + pl = ipv4["prefix_lists"][0] + seqs = [r["sequence"] for r in pl["entries"]] + self.assertIn(2, seqs) + self.assertIn(3, seqs) + + def test_parses_ipv4_rule_fields(self): + self.set_running_config(self.fixture) + result = get_running_config(self.mock_vyos) + ipv4 = next(e for e in result if e["afi"] == "ipv4") + rule2 = next(r for r in ipv4["prefix_lists"][0]["entries"] if r["sequence"] == 2) + self.assertEqual(rule2["action"], "permit") + self.assertEqual(rule2["prefix"], "92.168.10.0/26") + self.assertEqual(rule2["le"], 32) + + def test_parses_ipv6_prefix_lists(self): + self.set_running_config(self.fixture) + result = get_running_config(self.mock_vyos) + ipv6 = next((e for e in result if e["afi"] == "ipv6"), None) + self.assertIsNotNone(ipv6) + names = [p["name"] for p in ipv6["prefix_lists"]] + self.assertIn("AllowIPv6Prefix", names) + self.assertIn("DenyIPv6Prefix", names) + + def test_empty_returns_empty_list(self): + self.set_running_config({}) + result = get_running_config(self.mock_vyos) + self.assertEqual(result, []) + + +class TestVyOSPrefixListsNormalize(unittest.TestCase): + + def test_normalize_ipv4(self): + config = [ + { + "afi": "ipv4", + "prefix_lists": [ + { + "name": "PL1", + "entries": [{"sequence": 10, "action": "permit", "prefix": "10.0.0.0/8"}], + }, + ], + }, + ] + result = _normalize(config) + self.assertIn("PL1", result["ipv4"]) + self.assertIn(10, result["ipv4"]["PL1"]["rules"]) + self.assertEqual(result["ipv4"]["PL1"]["rules"][10]["action"], "permit") + + def test_normalize_filters_none_values(self): + config = [ + { + "afi": "ipv4", + "prefix_lists": [ + { + "name": "PL1", + "entries": [ + { + "sequence": 10, + "action": "permit", + "prefix": "10.0.0.0/8", + "ge": None, + "le": None, + }, + ], + }, + ], + }, + ] + result = _normalize(config) + rule = result["ipv4"]["PL1"]["rules"][10] + self.assertNotIn("ge", rule) + self.assertNotIn("le", rule) + + +class TestVyOSPrefixListsBuildCommands(unittest.TestCase): + + def _have_empty(self): + return [] + + def _have_with_ipv4_pl(self): + return [ + { + "afi": "ipv4", + "prefix_lists": [ + { + "name": "PL1", + "entries": [ + { + "sequence": 10, + "action": "permit", + "prefix": "10.0.0.0/8", + }, + ], + }, + ], + }, + ] + + def test_merged_adds_new_prefix_list(self): + config = [ + { + "afi": "ipv4", + "prefix_lists": [ + { + "name": "PL-NEW", + "entries": [ + { + "sequence": 5, + "action": "permit", + "prefix": "192.168.0.0/24", + }, + ], + }, + ], + }, + ] + cmds = build_commands(config, self._have_empty(), "merged") + paths = [c[1] for c in cmds] + self.assertIn(["policy", "prefix-list", "PL-NEW", "rule", "5", "action", "permit"], paths) + self.assertIn( + ["policy", "prefix-list", "PL-NEW", "rule", "5", "prefix", "192.168.0.0/24"], + paths, + ) + + def test_merged_idempotent_existing_rule(self): + config = self._have_with_ipv4_pl() + cmds = build_commands(config, self._have_with_ipv4_pl(), "merged") + self.assertEqual(cmds, []) + + def test_deleted_no_config_deletes_all(self): + cmds = build_commands([], self._have_with_ipv4_pl(), "deleted") + self.assertIn(("delete", ["policy", "prefix-list"]), cmds) + + def test_deleted_with_config_deletes_named(self): + config = [{"afi": "ipv4", "prefix_lists": [{"name": "PL1"}]}] + cmds = build_commands(config, self._have_with_ipv4_pl(), "deleted") + self.assertIn(("delete", ["policy", "prefix-list", "PL1"]), cmds) + + def test_replaced_deletes_then_resets(self): + config = [ + { + "afi": "ipv4", + "prefix_lists": [ + { + "name": "PL1", + "entries": [ + { + "sequence": 10, + "action": "deny", + "prefix": "10.0.0.0/8", + }, + ], + }, + ], + }, + ] + cmds = build_commands(config, self._have_with_ipv4_pl(), "replaced") + # Should delete PL1 first then re-add + ops = [(c[0], c[1]) for c in cmds] + delete_idx = next( + i for i, c in enumerate(ops) if c == ("delete", ["policy", "prefix-list", "PL1"]) + ) + set_idx = next(i for i, c in enumerate(ops) if c[0] == "set" and "deny" in c[1]) + self.assertLess(delete_idx, set_idx) + + +if __name__ == "__main__": + unittest.main() |
