summaryrefslogtreecommitdiffstats
path: root/doc/developer/topotests-jsontopo.rst
blob: e2cc72cc566fc40789d2ed95bd89f68ba337a5fe (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
.. _topotests-json:

Topotests with JSON
===================

Overview
--------

On top of current topotests framework following enhancements are done:


* Creating the topology and assigning IPs to router' interfaces dynamically.
  It is achieved by using json file, in which user specify the number of
  routers, links to each router, interfaces for the routers and protocol
  configurations for all routers.

* Creating the configurations dynamically. It is achieved by using
  :file:`/usr/lib/frr/frr-reload.py` utility, which takes running configuration
  and the newly created configuration for any particular router and creates a
  delta file(diff file) and loads it to  router.


Logging of test case executions
-------------------------------

* The execution log for each test is saved in the test specific directory create
  under `/tmp/topotests` (e.g.,
  `/tmp/topotests/<testdirname.testfilename>/exec.log`)

* Additionally all test logs are captured in the `topotest.xml` results file.
  This file will be saved in `/tmp/topotests/topotests.xml`. In order to extract
  the logs for a particular test one can use the `analyze.py` utility found in
  the topotests base directory.

* Router's current configuration, as it is changed during the test, can be
  displayed on console or sent to logs by adding ``show_router_config = True`` in
  :file:`pytest.ini`.

Note: directory "/tmp/topotests/" is created by topotests by default, making
use of same directory to save execution logs.

Guidelines
----------

Writing New Tests
^^^^^^^^^^^^^^^^^

This section will guide you in all recommended steps to produce a standard
topology test.

This is the recommended test writing routine:

* Create a json file which will have routers and protocol configurations
* Write and debug the tests
* Format the new code using `black <https://github.com/psf/black>`_
* Create a Pull Request

.. Note::

   BGP tests MUST use generous convergence timeouts - you must ensure that any
   test involving BGP uses a convergence timeout that is proportional to the
   configured BGP timers. If the timers are not reduced from their defaults this
   means 130 seconds; however, it is highly recommended that timers be reduced
   from the default values unless the test requires they not be.

File Hierarchy
^^^^^^^^^^^^^^

Before starting to write any tests one must know the file hierarchy. The
repository hierarchy looks like this:

.. code-block:: console

   $ cd frr/tests/topotests
   $ find ./*
   ...
   ./example_test/
   ./example_test/test_template_json.json # input json file, having topology, interfaces, bgp and other configuration
   ./example_test/test_template_json.py # test script to write and execute testcases
   ...
   ./lib # shared test/topology functions
   ./lib/topojson.py # library to create topology and configurations dynamically from json file
   ./lib/common_config.py # library to create protocol's common configurations ex- static_routes, prefix_lists, route_maps etc.
   ./lib/bgp.py # library to create and test bgp configurations

Defining the Topology and initial configuration in JSON file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The first step to write a new test is to define the topology and initial
configuration. User has to define topology and initial configuration in JSON
file. Here is an example of JSON file::

   BGP neighborship with single phy-link, sample JSON file:
   {
   "ipv4base": "192.168.0.0",
   "ipv4mask": 30,
   "ipv6base": "fd00::",
   "ipv6mask": 64,
   "link_ip_start": {"ipv4": "192.168.0.0", "v4mask": 30, "ipv6": "fd00::", "v6mask": 64},
   "lo_prefix": {"ipv4": "1.0.", "v4mask": 32, "ipv6": "2001:DB8:F::", "v6mask": 128},
   "routers": {
       "r1": {
           "links": {
               "lo": {"ipv4": "auto", "ipv6": "auto", "type": "loopback"},
               "r2": {"ipv4": "auto", "ipv6": "auto"},
               "r3": {"ipv4": "auto", "ipv6": "auto"}
           },
           "bgp": {
               "local_as": "64512",
               "address_family": {
                   "ipv4": {
                       "unicast": {
                           "neighbor": {
                               "r2": {
                                   "dest_link": {
                                       "r1": {}
                                   }
                               },
                               "r3": {
                                   "dest_link": {
                                       "r1": {}
                                   }
                               }
                           }
                       }
                   }
               }
           }
       },
       "r2": {
           "links": {
               "lo": {"ipv4": "auto", "ipv6": "auto", "type": "loopback"},
               "r1": {"ipv4": "auto", "ipv6": "auto"},
               "r3": {"ipv4": "auto", "ipv6": "auto"}
           },
           "bgp": {
               "local_as": "64512",
               "address_family": {
                   "ipv4": {
                       "unicast": {
                           "redistribute": [
                               {
                                   "redist_type": "static"
                               }
                           ],
                           "neighbor": {
                               "r1": {
                                   "dest_link": {
                                       "r2": {}
                                   }
                               },
                               "r3": {
                                   "dest_link": {
                                       "r2": {}
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
       ...


BGP neighboship with loopback interface, sample JSON file::

   {
   "ipv4base": "192.168.0.0",
   "ipv4mask": 30,
   "ipv6base": "fd00::",
   "ipv6mask": 64,
   "link_ip_start": {"ipv4": "192.168.0.0", "v4mask": 30, "ipv6": "fd00::", "v6mask": 64},
   "lo_prefix": {"ipv4": "1.0.", "v4mask": 32, "ipv6": "2001:DB8:F::", "v6mask": 128},
   "routers": {
       "r1": {
           "links": {
               "lo": {"ipv4": "auto", "ipv6": "auto", "type": "loopback",
                      "add_static_route":"yes"},
               "r2": {"ipv4": "auto", "ipv6": "auto"}
           },
           "bgp": {
               "local_as": "64512",
               "address_family": {
                   "ipv4": {
                       "unicast": {
                           "neighbor": {
                               "r2": {
                                   "dest_link": {
                                       "lo": {
                                           "source_link": "lo"
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           },
           "static_routes": [
               {
                   "network": "1.0.2.17/32",
                   "next_hop": "192.168.0.1
               }
           ]
       },
       "r2": {
           "links": {
               "lo": {"ipv4": "auto", "ipv6": "auto", "type": "loopback",
                      "add_static_route":"yes"},
               "r1": {"ipv4": "auto", "ipv6": "auto"},
               "r3": {"ipv4": "auto", "ipv6": "auto"}
           },
           "bgp": {
               "local_as": "64512",
               "address_family": {
                   "ipv4": {
                       "unicast": {
                           "redistribute": [
                               {
                                   "redist_type": "static"
                               }
                           ],
                           "neighbor": {
                               "r1": {
                                   "dest_link": {
                                       "lo": {
                                           "source_link": "lo"
                                       }
                                   }
                               },
                               "r3": {
                                   "dest_link": {
                                       "lo": {
                                           "source_link": "lo"
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           },
           "static_routes": [
               {
                   "network": "192.0.20.1/32",
                   "no_of_ip": 9,
                   "admin_distance": 100,
                   "next_hop": "192.168.0.1",
                   "tag": 4001
               }
           ],
       }
       ...

BGP neighborship with Multiple phy-links, sample JSON file::

   {
   "ipv4base": "192.168.0.0",
   "ipv4mask": 30,
   "ipv6base": "fd00::",
   "ipv6mask": 64,
   "link_ip_start": {"ipv4": "192.168.0.0", "v4mask": 30, "ipv6": "fd00::", "v6mask": 64},
   "lo_prefix": {"ipv4": "1.0.", "v4mask": 32, "ipv6": "2001:DB8:F::", "v6mask": 128},
   "routers": {
       "r1": {
           "links": {
               "lo": {"ipv4": "auto", "ipv6": "auto", "type": "loopback"},
               "r2-link1": {"ipv4": "auto", "ipv6": "auto"},
               "r2-link2": {"ipv4": "auto", "ipv6": "auto"}
           },
           "bgp": {
               "local_as": "64512",
               "address_family": {
                   "ipv4": {
                       "unicast": {
                           "neighbor": {
                               "r2": {
                                   "dest_link": {
                                       "r1-link1": {}
                                   }
                               }
                           }
                       }
                   }
               }
           }
       },
       "r2": {
           "links": {
               "lo": {"ipv4": "auto", "ipv6": "auto", "type": "loopback"},
               "r1-link1": {"ipv4": "auto", "ipv6": "auto"},
               "r1-link2": {"ipv4": "auto", "ipv6": "auto"},
               "r3-link1": {"ipv4": "auto", "ipv6": "auto"},
               "r3-link2": {"ipv4": "auto", "ipv6": "auto"}
           },
           "bgp": {
               "local_as": "64512",
               "address_family": {
                   "ipv4": {
                       "unicast": {
                           "redistribute": [
                               {
                                   "redist_type": "static"
                               }
                           ],
                           "neighbor": {
                               "r1": {
                                   "dest_link": {
                                       "r2-link1": {}
                                   }
                               },
                               "r3": {
                                   "dest_link": {
                                       "r2-link1": {}
                                   }
                               }
                           }
                       }
                   }
               }
           }
       }
       ...


JSON File Explained
"""""""""""""""""""

Mandatory keywords/options in JSON:

* ``ipv4base`` : base ipv4 address to generate ips,  ex - 192.168.0.0
* ``ipv4mask`` : mask for ipv4 address, ex - 30
* ``ipv6base`` : base ipv6 address to generate ips,  ex - fd00:
* ``ipv6mask`` : mask for ipv6 address, ex - 64
* ``link_ip_start`` : physical interface base ipv4 and ipv6 address
* ``lo_prefix`` : loopback interface base ipv4 and ipv6 address
* ``routers``   : user can add number of routers as per topology, router's name
  can be any logical name, ex- r1 or a0.
* ``r1`` : name of the router
* ``lo`` : loopback interface dict, ipv4 and/or ipv6 addresses generated automatically
* ``type`` : type of interface, to identify loopback interface
* ``links`` : physical interfaces dict, ipv4 and/or ipv6 addresses generated
  automatically
* ``r2-link1`` : it will be used when routers have multiple links. 'r2' is router
  name, 'link' is any logical name, '1' is to identify link number,
  router name and link must be seperated by hyphen (``-``), ex- a0-peer1

Optional keywords/options in JSON:

* ``bgp`` : bgp configuration
* ``local_as`` : Local AS number
* ``unicast`` : All SAFI configuration
* ``neighbor``: All neighbor details
* ``dest_link`` : Destination link to which router will connect
* ``router_id`` : bgp router-id
* ``source_link`` : if user wants to establish bgp neighborship with loopback
  interface, add ``source_link``: ``lo``
* ``keepalivetimer`` : Keep alive timer for BGP neighbor
* ``holddowntimer`` : Hold down timer for BGP neighbor
* ``static_routes`` : create static routes for routers
* ``redistribute`` : redistribute static and/or connected routes
* ``prefix_lists`` : create Prefix-lists for routers

Building topology and configurations
""""""""""""""""""""""""""""""""""""

Topology and initial configuration as well as teardown are invoked through the
use of a pytest fixture::


   from lib import fixtures

   tgen = pytest.fixture(fixtures.tgen_json, scope="module")


   # tgen is defined above
   # topo is a fixture defined in ../conftest.py and automatically available
   def test_bgp_convergence(tgen, topo):
       bgp_convergence = bgp.verify_bgp_convergence(tgen, topo)
       assert bgp_convergence

The `fixtures.topo_json` function calls `topojson.setup_module_from_json()` to
create and return a new `topogen.Topogen()` object using the JSON config file
with the same base filename as the test (i.e., `test_file.py` ->
`test_file.json`). Additionally, the fixture calls `tgen.stop_topology()` after
all the tests have run to cleanup. The function is only invoked once per
file/module (scope="module"), but the resulting object is passed to each
function that has `tgen` as an argument.

For more info on the powerful pytest fixtures feature please see `FIXTURES`_.

.. _FIXTURES: https://docs.pytest.org/en/6.2.x/fixture.html

Creating configuration files
""""""""""""""""""""""""""""

Router's configuration would be saved in config file frr_json.conf. Common
configurations are like, static routes, prefixlists and route maps etc configs,
these configs can be used by any other protocols as it is.
BGP config will be specific to BGP protocol testing.

* json file is passed to API Topogen() which saves the JSON object in
  `self.json_topo`
* The Topogen object is then passed to API build_config_from_json(), which looks
  for configuration tags in new JSON object.
* If tag is found in the JSON object, configuration is created as per input and
  written to file frr_json.conf
* Once JSON parsing is over, frr_json.conf is loaded onto respective router.
  Config loading is done using 'vtysh -f <file>'. Initial config at this point
  is also saved frr_json_initial.conf. This file can be used to reset
  configuration on router, during the course of execution.
* Reset of configuration is done using frr "reload.py" utility, which
  calculates the difference between router's running config and user's config
  and loads delta file to router. API used - reset_config_on_router()

Writing Tests
"""""""""""""

Test topologies should always be bootstrapped from the
`example_test/test_template_json.py` when possible in order to take advantage of
the most recent infrastructure support code.

Example:


* Define a module scoped fixture to setup/teardown and supply the tests with the
  `Topogen` object.

.. code-block:: python

   import pytest
   from lib import fixtures

   tgen = pytest.fixture(fixtures.tgen_json, scope="module")


* Define test functions using pytest fixtures

.. code-block:: python

   from lib import bgp

   # tgen is defined above
   # topo is a global available fixture defined in ../conftest.py
   def test_bgp_convergence(tgen, topo):
       "Test for BGP convergence."

       # Don't run this test if we have any failure.
       if tgen.routers_have_failure():
           pytest.skip(tgen.errors)

       bgp_convergence = bgp.verify_bgp_convergence(tgen, topo)
       assert bgp_convergence