summaryrefslogtreecommitdiff
path: root/tests/unit/modules/test_vyos_system.py
blob: 9345b574cf80e9e4c34b24618485fbe1ad82f246 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function


__metaclass__ = type

import unittest

from ansible_collections.vyos.rest.plugins.module_utils.vyos import (
    dict_op,
    owned_config,
)
from ansible_collections.vyos.rest.plugins.modules.vyos_system import (
    _BASE,
    ARGUMENT_SPEC,
)

from .base import load_fixture


class TestOwnedConfig(unittest.TestCase):

    def setUp(self):
        self.fixture = load_fixture("system_running.json")

    def test_filters_to_owned_keys(self):
        result = owned_config(self.fixture, ARGUMENT_SPEC)
        self.assertIn("host-name", result)
        self.assertIn("domain-name", result)
        self.assertIn("name-server", result)

    def test_excludes_non_owned_keys(self):
        result = owned_config(self.fixture, ARGUMENT_SPEC)
        self.assertNotIn("config-management", result)
        self.assertNotIn("console", result)
        self.assertNotIn("login", result)
        self.assertNotIn("syslog", result)


class TestDictOp(unittest.TestCase):

    def _have(self):
        return {
            "host-name": "vyos150",
            "domain-name": "lab.example.com",
            "name-server": ["8.8.8.8", "8.8.4.4"],
        }

    def test_set_idempotent(self):
        want = {
            "host_name": "vyos150",
            "domain_name": "lab.example.com",
            "name_server": ["8.8.8.8", "8.8.4.4"],
        }
        cmds = dict_op(want, self._have(), _BASE, op="set")
        self.assertEqual(cmds, [])

    def test_set_new_value(self):
        want = {"domain_name": "new.example.com"}
        cmds = dict_op(want, self._have(), _BASE, op="set")
        self.assertIn(("set", ["system", "domain-name", "new.example.com"]), cmds)

    def test_set_new_nameserver(self):
        want = {"name_server": ["8.8.8.8", "8.8.4.4", "1.1.1.1"]}
        cmds = dict_op(want, self._have(), _BASE, op="set")
        self.assertIn(("set", ["system", "name-server", "1.1.1.1"]), cmds)
        self.assertNotIn(("set", ["system", "name-server", "8.8.8.8"]), cmds)

    def test_delete_scalar(self):
        want = {"domain_name": "lab.example.com"}
        cmds = dict_op(want, self._have(), _BASE, op="delete")
        self.assertIn(("delete", ["system", "domain-name"]), cmds)

    def test_delete_list_item(self):
        want = {"name_server": ["8.8.8.8"]}
        cmds = dict_op(want, self._have(), _BASE, op="delete")
        self.assertIn(("delete", ["system", "name-server", "8.8.8.8"]), cmds)
        self.assertNotIn(("delete", ["system", "name-server", "8.8.4.4"]), cmds)

    def test_delete_nonexistent(self):
        want = {"domain_name": "other.com"}
        have = {"host-name": "vyos150"}
        cmds = dict_op(want, have, _BASE, op="delete")
        self.assertEqual(cmds, [])

    def test_set_missing_key(self):
        want = {"host_name": "vyos150"}
        cmds = dict_op(want, {}, _BASE, op="set")
        self.assertIn(("set", ["system", "host-name", "vyos150"]), cmds)


if __name__ == "__main__":
    unittest.main()