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