summaryrefslogtreecommitdiff
path: root/tests/unit/modules/test_vyos_firewall_global.py
blob: a507c0c72366eb0a2807f68154b9c2534c4ceffa (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# -*- 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_firewall_global import (
    build_commands,
    get_running_config,
)


_BASE = ["firewall", "group"]


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 VyOSModuleTestCase(unittest.TestCase):
    def setUp(self):
        self.mock_vyos = MagicMock()
        self.fixture = load_fixture("firewall_global_running.json")
        self.mock_vyos.get_config = MagicMock(return_value=self.fixture)


class TestVyOSFirewallGlobalGetRunning(VyOSModuleTestCase):

    def test_parses_address_groups(self):
        result = get_running_config(self.mock_vyos)
        groups = result["group"]["address_group"]
        names = [g["name"] for g in groups]
        self.assertIn("SERVERS", names)
        self.assertIn("DNS", names)
        servers = next(g for g in groups if g["name"] == "SERVERS")
        self.assertEqual(servers["description"], "Web servers")
        self.assertIn("192.168.1.10", servers["address"])
        self.assertIn("192.168.1.11", servers["address"])

    def test_parses_network_groups(self):
        result = get_running_config(self.mock_vyos)
        groups = result["group"]["network_group"]
        dmz = next(g for g in groups if g["name"] == "DMZ")
        self.assertIn("10.0.0.0/8", dmz["network"])
        self.assertIn("172.16.0.0/12", dmz["network"])

    def test_parses_port_groups(self):
        result = get_running_config(self.mock_vyos)
        groups = result["group"]["port_group"]
        web = next(g for g in groups if g["name"] == "WEB-PORTS")
        self.assertIn("80", web["port"])
        self.assertIn("443", web["port"])

    def test_parses_interface_groups(self):
        result = get_running_config(self.mock_vyos)
        groups = result["group"]["interface_group"]
        lan = next(g for g in groups if g["name"] == "LAN-IFACES")
        self.assertIn("eth1", lan["interface"])

    def test_parses_ipv6_network_groups(self):
        result = get_running_config(self.mock_vyos)
        groups = result["group"]["ipv6_network_group"]
        ipv6 = next(g for g in groups if g["name"] == "IPV6-LAN")
        self.assertIn("2001:db8::/32", ipv6["network"])

    def test_empty_config(self):
        self.mock_vyos.get_config = MagicMock(return_value={})
        result = get_running_config(self.mock_vyos)
        self.assertEqual(result, {})


class TestVyOSFirewallGlobalBuildCommands(unittest.TestCase):

    def _have(self):
        return {
            "group": {
                "address_group": [
                    {"name": "SERVERS", "address": ["192.168.1.10", "192.168.1.11"]},
                ],
                "network_group": [
                    {"name": "LAN", "network": ["192.168.0.0/16"]},
                ],
            },
        }

    def test_deleted_with_have(self):
        cmds = build_commands({}, self._have(), "deleted")
        self.assertEqual(cmds, [("delete", _BASE)])

    def test_deleted_without_have(self):
        cmds = build_commands({}, {}, "deleted")
        self.assertEqual(cmds, [])

    def test_merged_address_group(self):
        config = {
            "group": {
                "address_group": [
                    {"name": "SERVERS", "address": ["192.168.1.10"]},
                ],
            },
        }
        cmds = build_commands(config, {}, "merged")
        self.assertIn(
            ("set", _BASE + ["address-group", "SERVERS", "address", "192.168.1.10"]),
            cmds,
        )

    def test_merged_network_group(self):
        config = {
            "group": {
                "network_group": [
                    {"name": "LAN", "network": ["192.168.0.0/16"]},
                ],
            },
        }
        cmds = build_commands(config, {}, "merged")
        self.assertIn(
            ("set", _BASE + ["network-group", "LAN", "network", "192.168.0.0/16"]),
            cmds,
        )

    def test_merged_port_group(self):
        config = {
            "group": {
                "port_group": [
                    {"name": "WEB", "port": ["80", "443"]},
                ],
            },
        }
        cmds = build_commands(config, {}, "merged")
        self.assertIn(
            ("set", _BASE + ["port-group", "WEB", "port", "80"]),
            cmds,
        )

    def test_merged_idempotent(self):
        have = self._have()
        config = {
            "group": {
                "address_group": [
                    {"name": "SERVERS", "address": ["192.168.1.10", "192.168.1.11"]},
                ],
                "network_group": [
                    {"name": "LAN", "network": ["192.168.0.0/16"]},
                ],
            },
        }
        cmds = build_commands(config, have, "merged")
        self.assertEqual(cmds, [])

    def test_replaced_removes_extra_group(self):
        have = self._have()
        config = {
            "group": {
                "network_group": [
                    {"name": "DMZ", "network": ["10.0.0.0/8"]},
                ],
            },
        }
        cmds = build_commands(config, have, "replaced")
        paths = [c[1] for c in cmds]
        self.assertIn(_BASE + ["address-group", "SERVERS"], paths)
        self.assertIn(_BASE + ["network-group", "LAN"], paths)

    def test_replaced_idempotent(self):
        have = self._have()
        config = {
            "group": {
                "address_group": [
                    {"name": "SERVERS", "address": ["192.168.1.10", "192.168.1.11"]},
                ],
                "network_group": [
                    {"name": "LAN", "network": ["192.168.0.0/16"]},
                ],
            },
        }
        cmds = build_commands(config, have, "replaced")
        self.assertEqual(cmds, [])


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