]>
git.proxmox.com Git - mirror_frr.git/blob - tests/topotests/ospfapi/ctester.py
2 # -*- coding: utf-8 eval: (blacken-mode 1) -*-
3 # SPDX-License-Identifier: MIT
5 # January 17 2022, Christian Hopps <chopps@labn.net>
7 # Copyright 2022, LabN Consulting, L.L.C.
14 CWD
= os
.path
.dirname(os
.path
.realpath(__file__
))
16 CLIENTDIR
= os
.path
.abspath(os
.path
.join(CWD
, "../../../ospfclient"))
17 if not os
.path
.exists(CLIENTDIR
):
18 CLIENTDIR
= os
.path
.join(CWD
, "/usr/lib/frr")
19 assert os
.path
.exists(
20 os
.path
.join(CLIENTDIR
, "ospfclient.py")
21 ), "can't locate ospfclient.py"
23 sys
.path
[0:0] = [CLIENTDIR
]
25 import ospfclient
as api
# pylint: disable=E0401 # noqa: E402
28 async def do_monitor(c
, args
):
29 cv
= asyncio
.Condition()
31 async def cb(new_router_id
, _
):
32 assert new_router_id
== c
.router_id
33 logging
.info("NEW ROUTER ID: %s", new_router_id
)
38 logging
.debug("API using monitor router ID callback")
39 await c
.monitor_router_id(callback
=cb
)
41 for check
in args
.monitor
:
42 logging
.info("Waiting for %s", check
)
47 if str(check
) == str(got
):
49 logging
.debug("expected '%s' != '%s'\nwaiting on notify", check
, got
)
52 logging
.info("SUCCESS: %s", check
)
53 print("SUCCESS: {}".format(check
))
57 async def do_wait(c
, args
):
58 cv
= asyncio
.Condition()
60 async def cb(added
, removed
):
61 logging
.debug("callback: added: %s removed: %s", added
, removed
)
66 logging
.debug("API using monitor reachable callback")
67 await c
.monitor_reachable(callback
=cb
)
70 check
= ",".join(sorted(list(w
.split(","))))
71 logging
.info("Waiting for %s", check
)
75 got
= ",".join(sorted([str(x
) for x
in c
.reachable_routers
]))
78 logging
.debug("expected '%s' != '%s'\nwaiting on notify", check
, got
)
81 logging
.info("SUCCESS: %s", check
)
82 print("SUCCESS: {}".format(check
))
86 async def async_main(args
):
87 c
= api
.OspfOpaqueClient(args
.server
)
89 if sys
.version_info
[1] > 6:
90 asyncio
.create_task(c
._handle
_msg
_loop
()) # pylint: disable=W0212
92 asyncio
.get_event_loop().create_task(
93 c
._handle
_msg
_loop
() # pylint: disable=W0212
97 await do_monitor(c
, args
)
99 await do_wait(c
, args
)
104 ap
= argparse
.ArgumentParser(args
)
106 "--monitor", action
="append", help="monitor and wait for this router ID"
108 ap
.add_argument("--server", default
="localhost", help="OSPF API server")
110 "--wait", action
="append", help="wait for comma-sep set of reachable routers"
112 ap
.add_argument("-v", "--verbose", action
="store_true", help="be verbose")
113 args
= ap
.parse_args()
115 level
= logging
.DEBUG
if args
.verbose
else logging
.INFO
117 level
=level
, format
="%(asctime)s %(levelname)s: TESTER: %(name)s: %(message)s"
120 # We need to flush this output to stdout right away
121 h
= logging
.StreamHandler(sys
.stdout
)
122 h
.flush
= sys
.stdout
.flush
123 f
= logging
.Formatter("%(asctime)s %(name)s: %(levelname)s: %(message)s")
125 logger
= logging
.getLogger("ospfclient")
127 logger
.propagate
= False
129 logging
.info("ctester: starting")
134 if sys
.version_info
[1] > 6:
135 status
= asyncio
.run(async_main(args
))
137 loop
= asyncio
.get_event_loop()
139 status
= loop
.run_until_complete(async_main(args
))
142 except KeyboardInterrupt:
143 logging
.info("Exiting, received KeyboardInterrupt in main")
144 except Exception as error
:
145 logging
.info("Exiting, unexpected exception %s", error
, exc_info
=True)
147 logging
.info("api: clean exit")
152 if __name__
== "__main__":
154 sys
.exit(exit_status
)