]>
Commit | Line | Data |
---|---|---|
1fca63c1 RZ |
1 | """ |
2 | Topotest conftest.py file. | |
3 | """ | |
4 | ||
007e7313 | 5 | from lib.topogen import get_topogen, diagnose_env |
3668ed8d | 6 | from lib.topotest import json_cmp_result |
e7ba3cd1 | 7 | from lib.topolog import logger |
1fca63c1 RZ |
8 | import pytest |
9 | ||
80cb48d2 MS |
10 | topology_only = False |
11 | ||
1fca63c1 RZ |
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 | """ | |
80cb48d2 MS |
25 | global topology_only |
26 | ||
27 | if topology_only: | |
8833a838 RZ |
28 | tgen = get_topogen() |
29 | if tgen is not None: | |
30 | # Allow user to play with the setup. | |
31 | tgen.mininet_cli() | |
32 | ||
1fca63c1 | 33 | pytest.exit('the topology executed successfully') |
3668ed8d RZ |
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 | |
007e7313 RZ |
46 | |
47 | def pytest_configure(config): | |
48 | "Assert that the environment is correctly configured." | |
80cb48d2 MS |
49 | |
50 | global topology_only | |
51 | ||
007e7313 RZ |
52 | if not diagnose_env(): |
53 | pytest.exit('enviroment has errors, please read the logs') | |
e7ba3cd1 | 54 | |
80cb48d2 MS |
55 | if config.getoption('--topology-only'): |
56 | topology_only = True | |
57 | ||
e7ba3cd1 RZ |
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 | ||
5684f810 RZ |
64 | parent = item.parent |
65 | modname = parent.module.__name__ | |
66 | ||
e7ba3cd1 RZ |
67 | # Treat skips as non errors |
68 | if call.excinfo.typename != 'AssertionError': | |
5684f810 RZ |
69 | logger.info('assert skipped at "{}/{}": {}'.format( |
70 | modname, item.name, call.excinfo.value)) | |
e7ba3cd1 RZ |
71 | return |
72 | ||
73 | # Handle assert failures | |
e7ba3cd1 | 74 | parent._previousfailed = item |
5684f810 RZ |
75 | logger.error('assert failed at "{}/{}": {}'.format( |
76 | modname, item.name, call.excinfo.value)) | |
c8c265f5 RZ |
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)) |