summaryrefslogtreecommitdiff
path: root/smoketest/scripts/cli/test_policy_local-route.py
blob: 8d6ba40dcb6c94effd54641d482a88777aef5117 (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
#!/usr/bin/env python3
#
# Copyright (C) 2024 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import unittest

from base_vyostest_shim import VyOSUnitTestSHIM

interface = 'eth0'
mark = '100'
table_id = '101'
extra_table_id = '102'
vrf_name = 'LPBRVRF'
vrf_rt_id = '202'

class TestPolicyLocalRoute(VyOSUnitTestSHIM.TestCase):
    @classmethod
    def setUpClass(cls):
        super(TestPolicyLocalRoute, cls).setUpClass()
        # Clear out current configuration to allow running this test on a live system
        cls.cli_delete(cls, ['policy', 'local-route'])
        cls.cli_delete(cls, ['policy', 'local-route6'])

        cls.cli_set(cls, ['vrf', 'name', vrf_name, 'table', vrf_rt_id])

    @classmethod
    def tearDownClass(cls):
        cls.cli_delete(cls, ['vrf', 'name', vrf_name])

        super(TestPolicyLocalRoute, cls).tearDownClass()

    def tearDown(self):
        self.cli_delete(['policy', 'local-route'])
        self.cli_delete(['policy', 'local-route6'])
        self.cli_commit()

        ip_rule_search = [
            [f'lookup {table_id}']
        ]

        self.verify_rules(ip_rule_search, inverse=True)
        self.verify_rules(ip_rule_search, inverse=True, addr_family='inet6')

    def test_local_pbr_matching_criteria(self):
        self.cli_set(['policy', 'local-route', 'rule', '4', 'inbound-interface', interface])
        self.cli_set(['policy', 'local-route', 'rule', '4', 'protocol', 'udp'])
        self.cli_set(['policy', 'local-route', 'rule', '4', 'fwmark', mark])
        self.cli_set(['policy', 'local-route', 'rule', '4', 'destination', 'address', '198.51.100.0/24'])
        self.cli_set(['policy', 'local-route', 'rule', '4', 'destination', 'port', '111'])
        self.cli_set(['policy', 'local-route', 'rule', '4', 'source', 'address', '198.51.100.1'])
        self.cli_set(['policy', 'local-route', 'rule', '4', 'source', 'port', '443'])
        self.cli_set(['policy', 'local-route', 'rule', '4', 'set', 'table', table_id])

        self.cli_set(['policy', 'local-route6', 'rule', '6', 'inbound-interface', interface])
        self.cli_set(['policy', 'local-route6', 'rule', '6', 'protocol', 'tcp'])
        self.cli_set(['policy', 'local-route6', 'rule', '6', 'fwmark', mark])
        self.cli_set(['policy', 'local-route6', 'rule', '6', 'destination', 'address', '2001:db8::/64'])
        self.cli_set(['policy', 'local-route6', 'rule', '6', 'destination', 'port', '123'])
        self.cli_set(['policy', 'local-route6', 'rule', '6', 'source', 'address', '2001:db8::1'])
        self.cli_set(['policy', 'local-route6', 'rule', '6', 'source', 'port', '80'])
        self.cli_set(['policy', 'local-route6', 'rule', '6', 'set', 'table', table_id])

        self.cli_commit()

        rule_lookup = f'lookup {table_id}'
        rule_fwmark = 'fwmark ' + hex(int(mark))
        rule_interface = f'iif {interface}'

        ip4_rule_search = [
            ['from 198.51.100.1', 'to 198.51.100.0/24', rule_fwmark, rule_interface, 'ipproto udp', 'sport 443', 'dport 111', rule_lookup]
        ]

        self.verify_rules(ip4_rule_search)

        ip6_rule_search = [
            ['from 2001:db8::1', 'to 2001:db8::/64', rule_fwmark, rule_interface, 'ipproto tcp', 'sport 80', 'dport 123', rule_lookup]
        ]

        self.verify_rules(ip6_rule_search, addr_family='inet6')

    def test_local_pbr_rule_removal(self):
        self.cli_set(['policy', 'local-route', 'rule', '1', 'destination', 'address', '198.51.100.1'])
        self.cli_set(['policy', 'local-route', 'rule', '1', 'set', 'table', table_id])

        self.cli_set(['policy', 'local-route', 'rule', '2', 'destination', 'address', '198.51.100.2'])
        self.cli_set(['policy', 'local-route', 'rule', '2', 'set', 'table', table_id])

        self.cli_set(['policy', 'local-route', 'rule', '3', 'destination', 'address', '198.51.100.3'])
        self.cli_set(['policy', 'local-route', 'rule', '3', 'set', 'table', table_id])

        self.cli_commit()

        rule_lookup = f'lookup {table_id}'

        ip_rule_search = [
            ['to 198.51.100.1', rule_lookup],
            ['to 198.51.100.2', rule_lookup],
            ['to 198.51.100.3', rule_lookup],
        ]

        self.verify_rules(ip_rule_search)

        self.cli_delete(['policy', 'local-route', 'rule', '2'])
        self.cli_commit()

        ip_rule_missing = [
            ['to 198.51.100.2', rule_lookup],
        ]

        self.verify_rules(ip_rule_missing, inverse=True)

    def test_local_pbr_rule_changes(self):
        self.cli_set(['policy', 'local-route', 'rule', '1', 'destination', 'address', '198.51.100.0/24'])
        self.cli_set(['policy', 'local-route', 'rule', '1', 'set', 'table', table_id])

        self.cli_commit()

        self.cli_set(['policy', 'local-route', 'rule', '1', 'set', 'table', extra_table_id])
        self.cli_commit()

        ip_rule_search_extra = [
            ['to 198.51.100.0/24', f'lookup {extra_table_id}']
        ]

        self.verify_rules(ip_rule_search_extra)

        ip_rule_search_orig = [
            ['to 198.51.100.0/24', f'lookup {table_id}']
        ]

        self.verify_rules(ip_rule_search_orig, inverse=True)

        self.cli_delete(['policy', 'local-route', 'rule', '1', 'set', 'table'])
        self.cli_set(['policy', 'local-route', 'rule', '1', 'set', 'vrf', vrf_name])

        self.cli_commit()

        ip_rule_search_vrf = [
            ['to 198.51.100.0/24', f'lookup {vrf_name}']
        ]

        self.verify_rules(ip_rule_search_extra, inverse=True)
        self.verify_rules(ip_rule_search_vrf)

    def test_local_pbr_target_vrf(self):
        self.cli_set(['policy', 'local-route', 'rule', '1', 'destination', 'address', '198.51.100.0/24'])
        self.cli_set(['policy', 'local-route', 'rule', '1', 'set', 'vrf', vrf_name])

        self.cli_commit()

        ip_rule_search = [
            ['to 198.51.100.0/24', f'lookup {vrf_name}']
        ]

        self.verify_rules(ip_rule_search)


if __name__ == '__main__':
    unittest.main(verbosity=2)