summaryrefslogtreecommitdiff
path: root/tests/unit/modules
diff options
context:
space:
mode:
authoromnom62 <omnom62@outlook.com>2026-07-07 19:28:52 +1000
committeromnom62 <omnom62@outlook.com>2026-07-07 19:28:52 +1000
commitbe24a31fb8a4dcdd7c237eee28299cc4a20e4f9c (patch)
tree3ef69ebbfd8a15f09af454c590ac6a3260e0ecf3 /tests/unit/modules
parent7c57a114f8a6b63047b47817bd0ecc71ac3ad4cc (diff)
downloadrest.vyos-be24a31fb8a4dcdd7c237eee28299cc4a20e4f9c.tar.gz
rest.vyos-be24a31fb8a4dcdd7c237eee28299cc4a20e4f9c.zip
T8332: vyos_system SIT and UAT
Diffstat (limited to 'tests/unit/modules')
-rw-r--r--tests/unit/modules/test_vyos_system.py142
1 files changed, 142 insertions, 0 deletions
diff --git a/tests/unit/modules/test_vyos_system.py b/tests/unit/modules/test_vyos_system.py
new file mode 100644
index 0000000..1bd9cff
--- /dev/null
+++ b/tests/unit/modules/test_vyos_system.py
@@ -0,0 +1,142 @@
+# -*- 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_system import (
+ build_commands,
+ get_running_config,
+)
+
+
+_BASE = ["system"]
+
+
+def load_fixture(filename):
+ fixtures_dir = os.path.join(os.path.dirname(__file__), "..", "fixtures")
+ with open(os.path.join(fixtures_dir, filename)) as f:
+ return json.load(f)
+
+
+class TestVyOSSystemGetRunning(unittest.TestCase):
+
+ def setUp(self):
+ self.mock_vyos = MagicMock()
+ self.fixture = load_fixture("system_running.json")
+ self.mock_vyos.get_config = MagicMock(return_value=self.fixture)
+
+ def test_parses_hostname(self):
+ result = get_running_config(self.mock_vyos)
+ self.assertEqual(result["host_name"], "vyos150")
+
+ def test_parses_domain_name(self):
+ result = get_running_config(self.mock_vyos)
+ self.assertEqual(result["domain_name"], "lab.example.com")
+
+ def test_parses_name_servers_list(self):
+ result = get_running_config(self.mock_vyos)
+ self.assertIn("8.8.8.8", result["name_server"])
+ self.assertIn("8.8.4.4", result["name_server"])
+
+ def test_parses_domain_search_string(self):
+ result = get_running_config(self.mock_vyos)
+ self.assertIn("sub1.example.com", result["domain_search"])
+
+ def test_empty_config(self):
+ self.mock_vyos.get_config = MagicMock(return_value={})
+ result = get_running_config(self.mock_vyos)
+ self.assertEqual(result, {})
+
+
+class TestVyOSSystemBuildCommands(unittest.TestCase):
+
+ def _have(self):
+ return {
+ "host_name": "vyos150",
+ "domain_name": "lab.example.com",
+ "name_server": ["8.8.8.8", "8.8.4.4"],
+ "domain_search": ["sub1.example.com"],
+ }
+
+ def test_present_idempotent(self):
+ config = {
+ "host_name": "vyos150",
+ "domain_name": "lab.example.com",
+ "name_server": ["8.8.8.8", "8.8.4.4"],
+ "domain_search": ["sub1.example.com"],
+ }
+ cmds = build_commands(config, self._have(), "present")
+ self.assertEqual(cmds, [])
+
+ def test_present_new_hostname(self):
+ config = {
+ "host_name": "router1",
+ "domain_name": None,
+ "name_server": None,
+ "domain_search": None,
+ }
+ cmds = build_commands(config, self._have(), "present")
+ self.assertIn(("set", _BASE + ["host-name", "router1"]), cmds)
+
+ def test_present_new_nameserver(self):
+ config = {
+ "host_name": None,
+ "domain_name": None,
+ "name_server": ["1.1.1.1"],
+ "domain_search": None,
+ }
+ cmds = build_commands(config, self._have(), "present")
+ self.assertIn(("set", _BASE + ["name-server", "1.1.1.1"]), cmds)
+
+ def test_present_existing_nameserver_skipped(self):
+ config = {
+ "host_name": None,
+ "domain_name": None,
+ "name_server": ["8.8.8.8"],
+ "domain_search": None,
+ }
+ cmds = build_commands(config, self._have(), "present")
+ self.assertEqual(cmds, [])
+
+ def test_absent_domain_name(self):
+ config = {
+ "host_name": None,
+ "domain_name": "lab.example.com",
+ "name_server": None,
+ "domain_search": None,
+ }
+ cmds = build_commands(config, self._have(), "absent")
+ self.assertIn(("delete", _BASE + ["domain-name"]), cmds)
+
+ def test_absent_nameserver(self):
+ config = {
+ "host_name": None,
+ "domain_name": None,
+ "name_server": ["8.8.8.8"],
+ "domain_search": None,
+ }
+ cmds = build_commands(config, self._have(), "absent")
+ self.assertIn(("delete", _BASE + ["name-server", "8.8.8.8"]), cmds)
+
+ def test_absent_nonexistent_skipped(self):
+ # domain_name not in have - nothing to delete
+ config = {
+ "host_name": None,
+ "domain_name": "other.com",
+ "name_server": None,
+ "domain_search": None,
+ }
+ have = {"host_name": "vyos150"} # no domain_name in have
+ cmds = build_commands(config, have, "absent")
+ self.assertEqual(cmds, [])
+
+
+if __name__ == "__main__":
+ unittest.main()