summaryrefslogtreecommitdiff
path: root/tests/unit/modules/test_vyos_command.py
diff options
context:
space:
mode:
authoromnom62 <omnom62@outlook.com>2026-07-06 14:54:12 +1000
committerJohn Estabrook <jestabro@vyos.io>2026-08-21 14:02:19 -0500
commit45dc95873fd906c582fbbd5e6ca3838caf867399 (patch)
tree71b3e53e0080da60abf9fd2381705971dfe4d507 /tests/unit/modules/test_vyos_command.py
parent7a6b5e4f3a7a021cfa75faa7bf833741dfc09cff (diff)
downloadrest.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_command.py')
-rw-r--r--tests/unit/modules/test_vyos_command.py111
1 files changed, 111 insertions, 0 deletions
diff --git a/tests/unit/modules/test_vyos_command.py b/tests/unit/modules/test_vyos_command.py
new file mode 100644
index 0000000..5b474f9
--- /dev/null
+++ b/tests/unit/modules/test_vyos_command.py
@@ -0,0 +1,111 @@
+# -*- 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_command import (
+ evaluate_conditions,
+ parse_command,
+ run_commands,
+)
+
+
+class TestVyOSCommandParseCommand(unittest.TestCase):
+
+ def test_string_single_word(self):
+ self.assertEqual(parse_command("version"), ["version"])
+
+ def test_string_multi_word(self):
+ self.assertEqual(parse_command("ip route"), ["ip", "route"])
+
+ def test_list_passthrough(self):
+ self.assertEqual(parse_command(["ip", "route"]), ["ip", "route"])
+
+
+class TestVyOSCommandEvaluateConditions(unittest.TestCase):
+
+ def _stdout(self):
+ return ["VyOS 1.5.0 output", "eth0 192.168.1.1"]
+
+ def test_contains_match(self):
+ failed, conds = evaluate_conditions(
+ self._stdout(),
+ ["result[0] contains VyOS"],
+ "all",
+ )
+ self.assertFalse(failed)
+ self.assertEqual(conds, [])
+
+ def test_contains_no_match(self):
+ failed, conds = evaluate_conditions(
+ self._stdout(),
+ ["result[0] contains NonExistent"],
+ "all",
+ )
+ self.assertTrue(failed)
+ self.assertIn("result[0] contains NonExistent", conds)
+
+ def test_match_all_both_pass(self):
+ failed, conditions = evaluate_conditions(
+ self._stdout(),
+ ["result[0] contains VyOS", "result[1] contains eth0"],
+ "all",
+ )
+ self.assertFalse(failed)
+
+ def test_match_all_one_fails(self):
+ failed, conditions = evaluate_conditions(
+ self._stdout(),
+ ["result[0] contains VyOS", "result[1] contains NonExistent"],
+ "all",
+ )
+ self.assertTrue(failed)
+
+ def test_match_any_one_passes(self):
+ failed, conditions = evaluate_conditions(
+ self._stdout(),
+ ["result[0] contains VyOS", "result[1] contains NonExistent"],
+ "any",
+ )
+ self.assertFalse(failed)
+
+ def test_empty_conditions(self):
+ failed, conds = evaluate_conditions(self._stdout(), [], "all")
+ self.assertFalse(failed)
+ self.assertEqual(conds, [])
+
+
+class TestVyOSCommandRunCommands(unittest.TestCase):
+
+ def setUp(self):
+ self.mock_vyos = MagicMock()
+
+ def test_run_list_command(self):
+ self.mock_vyos.show = MagicMock(return_value="VyOS 1.5.0")
+ result = run_commands(self.mock_vyos, [["version"]])
+ self.mock_vyos.show.assert_called_once_with(["version"])
+ self.assertEqual(result, ["VyOS 1.5.0"])
+
+ def test_run_string_command(self):
+ self.mock_vyos.show = MagicMock(return_value="uptime")
+ run_commands(self.mock_vyos, ["system uptime"])
+ self.mock_vyos.show.assert_called_once_with(["system", "uptime"])
+
+ def test_run_multiple_commands(self):
+ self.mock_vyos.show = MagicMock(side_effect=["out1", "out2"])
+ result = run_commands(self.mock_vyos, [["version"], ["interfaces"]])
+ self.assertEqual(result, ["out1", "out2"])
+
+ def test_run_none_returns_empty_string(self):
+ self.mock_vyos.show = MagicMock(return_value=None)
+ result = run_commands(self.mock_vyos, [["version"]])
+ self.assertEqual(result, [""])
+
+
+if __name__ == "__main__":
+ unittest.main()