summaryrefslogtreecommitdiff
path: root/src/tests/test_ospf_migration.py
blob: 95a73cbe9fd04611501848a47d787a9c35fe94ea (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Copyright VyOS maintainers and contributors <maintainers@vyos.io>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library.  If not, see <http://www.gnu.org/licenses/>.

import importlib.machinery
import importlib.util
from unittest import TestCase

from vyos.configtree import ConfigTree

migration_script = 'src/migration-scripts/ospf/2-to-3'

ospf = ['protocols', 'ospf']
vrf_ospf = ['vrf', 'name', 'red', 'protocols', 'ospf']


def load_migrate():
    loader = importlib.machinery.SourceFileLoader('ospf_2_to_3', migration_script)
    spec = importlib.util.spec_from_loader(loader.name, loader)
    module = importlib.util.module_from_spec(spec)
    loader.exec_module(module)
    return module.migrate


def opaque_lsa(base):
    return base + ['parameters', 'opaque-lsa']


def capability_opaque(base):
    return base + ['capability', 'opaque']


class TestOspfOpaqueLsaMigration(TestCase):
    def setUp(self):
        self.migrate = load_migrate()

    def test_no_ospf(self):
        # must not raise when OSPF is not configured
        config = ConfigTree('system {\n    host-name vyos\n}\n')
        self.migrate(config)
        self.assertFalse(config.exists(ospf))
        self.assertFalse(config.exists(['vrf']))

    def test_neither_node(self):
        config = ConfigTree(
            'protocols {\n'
            '    ospf {\n'
            '        parameters {\n'
            '            router-id 192.0.2.1\n'
            '        }\n'
            '    }\n'
            '}\n'
        )
        self.migrate(config)
        self.assertFalse(config.exists(capability_opaque(ospf)))
        self.assertEqual(
            config.return_value(ospf + ['parameters', 'router-id']), '192.0.2.1'
        )

    def test_global_instance(self):
        config = ConfigTree(
            'protocols {\n'
            '    ospf {\n'
            '        parameters {\n'
            '            opaque-lsa\n'
            '            router-id 192.0.2.1\n'
            '        }\n'
            '    }\n'
            '}\n'
        )
        self.migrate(config)
        self.assertTrue(config.exists(capability_opaque(ospf)))
        self.assertFalse(config.exists(opaque_lsa(ospf)))
        # the other parameters stay
        self.assertEqual(
            config.return_value(ospf + ['parameters', 'router-id']), '192.0.2.1'
        )

    def test_empty_parameters_node_is_removed(self):
        config = ConfigTree(
            'protocols {\n'
            '    ospf {\n'
            '        parameters {\n'
            '            opaque-lsa\n'
            '        }\n'
            '    }\n'
            '}\n'
        )
        self.migrate(config)
        self.assertTrue(config.exists(capability_opaque(ospf)))
        self.assertFalse(config.exists(ospf + ['parameters']))

    def test_vrf_instance(self):
        # FRR refuses opaque LSAs outside the default VRF: the option never
        # took effect there, so it is dropped, not turned into "capability
        # opaque" (which FRR would refuse at commit time)
        config = ConfigTree(
            'vrf {\n'
            '    name red {\n'
            '        protocols {\n'
            '            ospf {\n'
            '                parameters {\n'
            '                    opaque-lsa\n'
            '                    router-id 192.0.2.2\n'
            '                }\n'
            '            }\n'
            '        }\n'
            '        table 1000\n'
            '    }\n'
            '}\n'
        )
        self.migrate(config)
        self.assertFalse(config.exists(capability_opaque(vrf_ospf)))
        self.assertFalse(config.exists(opaque_lsa(vrf_ospf)))
        self.assertEqual(
            config.return_value(vrf_ospf + ['parameters', 'router-id']), '192.0.2.2'
        )
        # nothing is created in the default VRF
        self.assertFalse(config.exists(ospf))

    def test_vrf_capability_opaque_dropped(self):
        # a VRF instance that already has "capability opaque" set directly
        # (without "parameters opaque-lsa") is just as refused by FRR; drop it
        config = ConfigTree(
            'vrf {\n'
            '    name red {\n'
            '        protocols {\n'
            '            ospf {\n'
            '                capability {\n'
            '                    opaque\n'
            '                }\n'
            '                parameters {\n'
            '                    router-id 192.0.2.2\n'
            '                }\n'
            '            }\n'
            '        }\n'
            '        table 1000\n'
            '    }\n'
            '}\n'
        )
        self.migrate(config)
        self.assertFalse(config.exists(capability_opaque(vrf_ospf)))
        self.assertFalse(config.exists(vrf_ospf + ['capability']))
        self.assertEqual(
            config.return_value(vrf_ospf + ['parameters', 'router-id']), '192.0.2.2'
        )

    def test_both_nodes_set_in_vrf(self):
        # both the dead "parameters opaque-lsa" deletion and the "capability
        # opaque" drop fire for the same VRF instance in one pass
        config = ConfigTree(
            'vrf {\n'
            '    name red {\n'
            '        protocols {\n'
            '            ospf {\n'
            '                capability {\n'
            '                    opaque\n'
            '                }\n'
            '                parameters {\n'
            '                    opaque-lsa\n'
            '                }\n'
            '            }\n'
            '        }\n'
            '        table 1000\n'
            '    }\n'
            '}\n'
        )
        self.migrate(config)
        self.assertFalse(config.exists(capability_opaque(vrf_ospf)))
        self.assertFalse(config.exists(opaque_lsa(vrf_ospf)))
        self.assertFalse(config.exists(vrf_ospf + ['capability']))
        self.assertFalse(config.exists(vrf_ospf + ['parameters']))

    def test_global_and_vrf_instances_are_independent(self):
        # the default instance keeps the move to "capability opaque"; VRF
        # "red" only loses the dead option; VRF "blue" has neither and must
        # stay as it is
        config = ConfigTree(
            'protocols {\n'
            '    ospf {\n'
            '        parameters {\n'
            '            opaque-lsa\n'
            '        }\n'
            '    }\n'
            '}\n'
            'vrf {\n'
            '    name blue {\n'
            '        protocols {\n'
            '            ospf {\n'
            '                parameters {\n'
            '                    router-id 192.0.2.3\n'
            '                }\n'
            '            }\n'
            '        }\n'
            '    }\n'
            '    name red {\n'
            '        protocols {\n'
            '            ospf {\n'
            '                parameters {\n'
            '                    opaque-lsa\n'
            '                }\n'
            '            }\n'
            '        }\n'
            '    }\n'
            '}\n'
        )
        self.migrate(config)
        blue = ['vrf', 'name', 'blue', 'protocols', 'ospf']
        # capability opaque exists only in the default instance
        self.assertTrue(config.exists(capability_opaque(ospf)))
        self.assertFalse(config.exists(capability_opaque(vrf_ospf)))
        self.assertFalse(config.exists(opaque_lsa(vrf_ospf)))
        self.assertFalse(config.exists(capability_opaque(blue)))
        self.assertEqual(
            config.return_value(blue + ['parameters', 'router-id']), '192.0.2.3'
        )

    def test_both_nodes_set(self):
        config = ConfigTree(
            'protocols {\n'
            '    ospf {\n'
            '        capability {\n'
            '            opaque\n'
            '        }\n'
            '        parameters {\n'
            '            opaque-lsa\n'
            '        }\n'
            '    }\n'
            '}\n'
        )
        self.migrate(config)
        self.assertTrue(config.exists(capability_opaque(ospf)))
        self.assertFalse(config.exists(opaque_lsa(ospf)))

    def test_only_capability_set(self):
        config = ConfigTree(
            'protocols {\n'
            '    ospf {\n'
            '        capability {\n'
            '            opaque\n'
            '        }\n'
            '    }\n'
            '}\n'
        )
        self.migrate(config)
        self.assertTrue(config.exists(capability_opaque(ospf)))
        self.assertFalse(config.exists(ospf + ['parameters']))