]> git.proxmox.com Git - ovs.git/blob - tests/test-unixctl.py
tests: Convert ovsdb-monitor-sort utility from Perl to Python.
[ovs.git] / tests / test-unixctl.py
1 # Copyright (c) 2012 Nicira, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import argparse
16 import sys
17
18 import ovs.daemon
19 import ovs.unixctl
20 import ovs.unixctl.server
21
22 vlog = ovs.vlog.Vlog("test-unixctl")
23 exiting = False
24
25
26 def unixctl_exit(conn, unused_argv, aux):
27 assert aux == "aux_exit"
28 global exiting
29
30 exiting = True
31 conn.reply(None)
32
33
34 def unixctl_echo(conn, argv, aux):
35 assert aux == "aux_echo"
36 conn.reply(str(argv))
37
38
39 def unixctl_echo_error(conn, argv, aux):
40 assert aux == "aux_echo_error"
41 conn.reply_error(str(argv))
42
43
44 def unixctl_log(conn, argv, unused_aux):
45 vlog.info(str(argv[0]))
46 conn.reply(None)
47
48
49 def unixctl_block(conn, unused_argv, unused_aux):
50 pass
51
52
53 def main():
54 parser = argparse.ArgumentParser(
55 description="Open vSwitch unixctl test program for Python")
56 parser.add_argument("--unixctl", help="UNIXCTL socket location or 'none'.")
57
58 ovs.daemon.add_args(parser)
59 ovs.vlog.add_args(parser)
60 args = parser.parse_args()
61 ovs.daemon.handle_args(args)
62 ovs.vlog.handle_args(args)
63
64 ovs.daemon.daemonize_start()
65 error, server = ovs.unixctl.server.UnixctlServer.create(args.unixctl)
66 if error:
67 ovs.util.ovs_fatal(error, "could not create unixctl server at %s"
68 % args.unixctl, vlog)
69
70 ovs.unixctl.command_register("exit", "", 0, 0, unixctl_exit, "aux_exit")
71 ovs.unixctl.command_register("echo", "[arg ...]", 1, 2, unixctl_echo,
72 "aux_echo")
73 ovs.unixctl.command_register("log", "[arg ...]", 1, 2, unixctl_log, None)
74 ovs.unixctl.command_register("echo_error", "[arg ...]", 1, 2,
75 unixctl_echo_error, "aux_echo_error")
76 ovs.unixctl.command_register("block", "", 0, 0, unixctl_block, None)
77 ovs.daemon.daemonize_complete()
78
79 vlog.info("Entering run loop.")
80 poller = ovs.poller.Poller()
81 while not exiting:
82 server.run()
83 server.wait(poller)
84 if exiting:
85 poller.immediate_wake()
86 poller.block()
87 server.close()
88
89
90 if __name__ == '__main__':
91 try:
92 main()
93 except SystemExit:
94 # Let system.exit() calls complete normally
95 raise
96 except:
97 vlog.exception("traceback")
98 sys.exit(ovs.daemon.RESTART_EXIT_CODE)