ovsdb: Free "error" string in ovsdb_execute_insert().
[openvswitch] / tests / test-reconnect.py
1 # Copyright (c) 2009, 2010 Nicira Networks.
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 errno
16 import logging
17 import sys
18
19 import ovs.reconnect
20
21 def do_enable(arg):
22     r.enable(now)
23
24 def do_disable(arg):
25     r.disable(now)
26
27 def do_force_reconnect(arg):
28     r.force_reconnect(now)
29
30 def error_from_string(s):
31     if not s:
32         return 0
33     elif s == "ECONNREFUSED":
34         return errno.ECONNREFUSED
35     elif s == "EOF":
36         return EOF
37     else:
38         sys.stderr.write("unknown error '%s'\n" % s)
39         sys.exit(1)
40
41 def do_disconnected(arg):
42     r.disconnected(now, error_from_string(arg))
43     
44 def do_connecting(arg):
45     r.connecting(now)
46
47 def do_connect_failed(arg):
48     r.connect_failed(now, error_from_string(arg))
49
50 def do_connected(arg):
51     r.connected(now)
52
53 def do_received(arg):
54     r.received(now)
55
56 def do_run(arg):
57     global now
58     if arg is not None:
59         now += int(arg)
60
61     action = r.run(now)
62     if action is None:
63         pass
64     elif action == ovs.reconnect.CONNECT:
65         print "  should connect"
66     elif action == ovs.reconnect.DISCONNECT:
67         print "  should disconnect"
68     elif action == ovs.reconnect.PROBE:
69         print "  should send probe"
70     else:
71         assert False
72
73 def do_advance(arg):
74     global now
75     now += int(arg)
76
77 def do_timeout(arg):
78     global now
79     timeout = r.timeout(now)
80     if timeout >= 0:
81         print "  advance %d ms" % timeout
82         now += timeout
83     else:
84         print "  no timeout"
85
86 def do_set_max_tries(arg):
87     r.set_max_tries(int(arg))
88
89 def diff_stats(old, new):
90     if (old.state != new.state or
91         old.state_elapsed != new.state_elapsed or
92         old.backoff != new.backoff):
93         print("  in %s for %d ms (%d ms backoff)"
94               % (new.state, new.state_elapsed, new.backoff))
95
96     if (old.creation_time != new.creation_time or
97         old.last_received != new.last_received or
98         old.last_connected != new.last_connected):
99         print("  created %d, last received %d, last connected %d"
100               % (new.creation_time, new.last_received, new.last_connected))
101
102     if (old.n_successful_connections != new.n_successful_connections or
103         old.n_attempted_connections != new.n_attempted_connections or
104         old.seqno != new.seqno):
105         print("  %d successful connections out of %d attempts, seqno %d"
106               % (new.n_successful_connections, new.n_attempted_connections,
107                  new.seqno))
108
109     if (old.is_connected != new.is_connected or
110         old.current_connection_duration != new.current_connection_duration or
111         old.total_connected_duration != new.total_connected_duration):
112         if new.is_connected:
113             negate = ""
114         else:
115             negate = "not "
116         print("  %sconnected (%d ms), total %d ms connected"
117               % (negate, new.current_connection_duration,
118                  new.total_connected_duration))
119
120 def do_set_passive(arg):
121     r.set_passive(True, now)
122
123 def do_listening(arg):
124     r.listening(now)
125
126 def do_listen_error(arg):
127     r.listen_error(now, int(arg))
128
129 def main():
130     commands = {
131         "enable": do_enable,
132         "disable": do_disable,
133         "force-reconnect": do_force_reconnect,
134         "disconnected": do_disconnected,
135         "connecting": do_connecting,
136         "connect-failed": do_connect_failed,
137         "connected": do_connected,
138         "received": do_received,
139         "run": do_run,
140         "advance": do_advance,
141         "timeout": do_timeout,
142         "set-max-tries": do_set_max_tries,
143         "passive": do_set_passive,
144         "listening": do_listening,
145         "listen-error": do_listen_error
146     }
147
148     logging.basicConfig(level=logging.CRITICAL)
149
150     global now
151     global r
152
153     now = 1000
154     r = ovs.reconnect.Reconnect(now)
155     r.set_name("remote")
156     prev = r.get_stats(now)
157     print "### t=%d ###" % now
158     old_time = now
159     old_max_tries = r.get_max_tries()
160     while True:
161         line = sys.stdin.readline()
162         if line == "":
163             break
164
165         print line[:-1]
166         if line[0] == "#":
167             continue
168
169         args = line.split()
170         if len(args) == 0:
171             continue
172
173         command = args[0]
174         if len(args) > 1:
175             op = args[1]
176         else:
177             op = None
178         commands[command](op)
179
180         if old_time != now:
181             print
182             print "### t=%d ###" % now
183             old_time = now
184
185         cur = r.get_stats(now)
186         diff_stats(prev, cur)
187         prev = cur
188         if r.get_max_tries() != old_max_tries:
189             old_max_tries = r.get_max_tries()
190             print "  %d tries left" % old_max_tries
191
192 if __name__ == '__main__':
193     main()
194
195