]> git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/conftest.py
Merge pull request #5288 from SumitAgarwal123/bfd_docs
[mirror_frr.git] / tests / topotests / conftest.py
1 """
2 Topotest conftest.py file.
3 """
4
5 from lib.topogen import get_topogen, diagnose_env
6 from lib.topotest import json_cmp_result
7 from lib.topolog import logger
8 import pytest
9
10 topology_only = False
11
12 def pytest_addoption(parser):
13 """
14 Add topology-only option to the topology tester. This option makes pytest
15 only run the setup_module() to setup the topology without running any tests.
16 """
17 parser.addoption('--topology-only', action='store_true',
18 help='Only set up this topology, don\'t run tests')
19
20 def pytest_runtest_call():
21 """
22 This function must be run after setup_module(), it does standarized post
23 setup routines. It is only being used for the 'topology-only' option.
24 """
25 global topology_only
26
27 if topology_only:
28 tgen = get_topogen()
29 if tgen is not None:
30 # Allow user to play with the setup.
31 tgen.mininet_cli()
32
33 pytest.exit('the topology executed successfully')
34
35 def pytest_assertrepr_compare(op, left, right):
36 """
37 Show proper assertion error message for json_cmp results.
38 """
39 json_result = left
40 if not isinstance(json_result, json_cmp_result):
41 json_result = right
42 if not isinstance(json_result, json_cmp_result):
43 return None
44
45 return json_result.errors
46
47 def pytest_configure(config):
48 "Assert that the environment is correctly configured."
49
50 global topology_only
51
52 if not diagnose_env():
53 pytest.exit('enviroment has errors, please read the logs')
54
55 if config.getoption('--topology-only'):
56 topology_only = True
57
58 def pytest_runtest_makereport(item, call):
59 "Log all assert messages to default logger with error level"
60 # Nothing happened
61 if call.excinfo is None:
62 return
63
64 parent = item.parent
65 modname = parent.module.__name__
66
67 # Treat skips as non errors
68 if call.excinfo.typename != 'AssertionError':
69 logger.info('assert skipped at "{}/{}": {}'.format(
70 modname, item.name, call.excinfo.value))
71 return
72
73 # Handle assert failures
74 parent._previousfailed = item
75 logger.error('assert failed at "{}/{}": {}'.format(
76 modname, item.name, call.excinfo.value))
77
78 # (topogen) Set topology error to avoid advancing in the test.
79 tgen = get_topogen()
80 if tgen is not None:
81 # This will cause topogen to report error on `routers_have_failure`.
82 tgen.set_error('{}/{}'.format(modname, item.name))