summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorEthan Jackson <ethan@nicira.com>2011-09-23 23:43:12 -0700
committerEthan Jackson <ethan@nicira.com>2011-09-24 16:32:54 -0700
commit26bb0f31299d3f8eb06551d6a219846929c27149 (patch)
tree1d444ab340374c00a48113b634075e74299c9227 /tests
parentb17a80ee405da3843710f428b4f83015dc35c342 (diff)
downloadopenvswitch-26bb0f31299d3f8eb06551d6a219846929c27149.tar.gz
python: Style cleanup.
This patch does minor style cleanups to the code in the python and tests directory. There's other code floating around that could use similar treatment, but updating it is not convenient at the moment.
Diffstat (limited to 'tests')
-rw-r--r--tests/test-json.py10
-rw-r--r--tests/test-ovsdb.py23
-rw-r--r--tests/test-reconnect.py56
3 files changed, 67 insertions, 22 deletions
diff --git a/tests/test-json.py b/tests/test-json.py
index bffb88a03..0ec1cfc30 100644
--- a/tests/test-json.py
+++ b/tests/test-json.py
@@ -1,11 +1,11 @@
# Copyright (c) 2009, 2010 Nicira Networks.
-#
+#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,6 +18,7 @@ import sys
import ovs.json
+
def print_json(json):
if type(json) in [str, unicode]:
print "error: %s" % json
@@ -27,6 +28,7 @@ def print_json(json):
sys.stdout.write("\n")
return True
+
def parse_multiple(stream):
buf = stream.read(4096)
ok = True
@@ -49,6 +51,7 @@ def parse_multiple(stream):
ok = False
return ok
+
def main(argv):
argv0 = argv[0]
@@ -88,5 +91,6 @@ def main(argv):
if not ok:
sys.exit(1)
+
if __name__ == '__main__':
main(sys.argv)
diff --git a/tests/test-ovsdb.py b/tests/test-ovsdb.py
index dbdd8f8f5..df29fdb05 100644
--- a/tests/test-ovsdb.py
+++ b/tests/test-ovsdb.py
@@ -28,12 +28,14 @@ import ovs.ovsuuid
import ovs.poller
import ovs.util
+
def unbox_json(json):
if type(json) == list and len(json) == 1:
return json[0]
else:
return json
+
def do_default_atoms():
for type_ in types.ATOMIC_TYPES:
if type_ == types.VoidType:
@@ -48,6 +50,7 @@ def do_default_atoms():
sys.stdout.write("OK\n")
+
def do_default_data():
any_errors = False
for n_min in 0, 1:
@@ -74,21 +77,25 @@ def do_default_data():
if any_errors:
sys.exit(1)
+
def do_parse_atomic_type(type_string):
type_json = unbox_json(ovs.json.from_string(type_string))
atomic_type = types.AtomicType.from_json(type_json)
print ovs.json.to_string(atomic_type.to_json(), sort_keys=True)
+
def do_parse_base_type(type_string):
type_json = unbox_json(ovs.json.from_string(type_string))
base_type = types.BaseType.from_json(type_json)
print ovs.json.to_string(base_type.to_json(), sort_keys=True)
+
def do_parse_type(type_string):
type_json = unbox_json(ovs.json.from_string(type_string))
type_ = types.Type.from_json(type_json)
print ovs.json.to_string(type_.to_json(), sort_keys=True)
+
def do_parse_atoms(type_string, *atom_strings):
type_json = unbox_json(ovs.json.from_string(type_string))
base = types.BaseType.from_json(type_json)
@@ -100,6 +107,7 @@ def do_parse_atoms(type_string, *atom_strings):
except error.Error, e:
print unicode(e)
+
def do_parse_data(type_string, *data_strings):
type_json = unbox_json(ovs.json.from_string(type_string))
type_ = types.Type.from_json(type_json)
@@ -108,6 +116,7 @@ def do_parse_data(type_string, *data_strings):
datum = data.Datum.from_json(type_, datum_json)
print ovs.json.to_string(datum.to_json())
+
def do_sort_atoms(type_string, atom_strings):
type_json = unbox_json(ovs.json.from_string(type_string))
base = types.BaseType.from_json(type_json)
@@ -116,22 +125,26 @@ def do_sort_atoms(type_string, atom_strings):
print ovs.json.to_string([data.Atom.to_json(atom)
for atom in sorted(atoms)])
+
def do_parse_column(name, column_string):
column_json = unbox_json(ovs.json.from_string(column_string))
column = ovs.db.schema.ColumnSchema.from_json(column_json, name)
print ovs.json.to_string(column.to_json(), sort_keys=True)
+
def do_parse_table(name, table_string, default_is_root_string='false'):
default_is_root = default_is_root_string == 'true'
table_json = unbox_json(ovs.json.from_string(table_string))
table = ovs.db.schema.TableSchema.from_json(table_json, name)
print ovs.json.to_string(table.to_json(default_is_root), sort_keys=True)
+
def do_parse_schema(schema_string):
schema_json = unbox_json(ovs.json.from_string(schema_string))
schema = ovs.db.schema.DbSchema.from_json(schema_json)
print ovs.json.to_string(schema.to_json(), sort_keys=True)
+
def print_idl(idl, step):
simple = idl.tables["simple"].rows
l1 = idl.tables["link1"].rows
@@ -176,6 +189,7 @@ def print_idl(idl, step):
print("%03d: empty" % step)
sys.stdout.flush()
+
def substitute_uuids(json, symtab):
if type(json) in [str, unicode]:
symbol = symtab.get(json)
@@ -190,6 +204,7 @@ def substitute_uuids(json, symtab):
return d
return json
+
def parse_uuids(json, symtab):
if type(json) in [str, unicode] and ovs.ovsuuid.is_valid_string(json):
name = "#%d#" % len(symtab)
@@ -202,12 +217,14 @@ def parse_uuids(json, symtab):
for value in json.itervalues():
parse_uuids(value, symtab)
+
def idltest_find_simple(idl, i):
for row in idl.tables["simple"].rows.itervalues():
if row.i == i:
return row
return None
+
def idl_set(idl, commands, step):
txn = ovs.db.idl.Transaction(idl)
increment = False
@@ -301,6 +318,7 @@ def idl_set(idl, commands, step):
sys.stdout.write("\n")
sys.stdout.flush()
+
def do_idl(schema_file, remote, *commands):
schema = ovs.db.schema.DbSchema.from_json(ovs.json.from_file(schema_file))
idl = ovs.db.idl.Idl(remote, schema)
@@ -331,7 +349,7 @@ def do_idl(schema_file, remote, *commands):
idl.wait(poller)
rpc.wait(poller)
poller.block()
-
+
print_idl(idl, step)
step += 1
@@ -377,6 +395,7 @@ def do_idl(schema_file, remote, *commands):
idl.close()
print("%03d: done" % step)
+
def usage():
print """\
%(program_name)s: test utility for Open vSwitch database Python bindings
@@ -419,6 +438,7 @@ The following options are also available:
""" % {'program_name': ovs.util.PROGRAM_NAME}
sys.exit(0)
+
def main(argv):
try:
options, args = getopt.gnu_getopt(argv[1:], 't:h',
@@ -489,6 +509,7 @@ def main(argv):
func(*args)
+
if __name__ == '__main__':
try:
main(sys.argv)
diff --git a/tests/test-reconnect.py b/tests/test-reconnect.py
index af52c8acb..90d337af3 100644
--- a/tests/test-reconnect.py
+++ b/tests/test-reconnect.py
@@ -1,11 +1,11 @@
# Copyright (c) 2009, 2010 Nicira Networks.
-#
+#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
-#
+#
# http://www.apache.org/licenses/LICENSE-2.0
-#
+#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -18,15 +18,22 @@ import sys
import ovs.reconnect
-def do_enable(arg):
+now = 0
+r = None
+
+
+def do_enable(_):
r.enable(now)
-def do_disable(arg):
+
+def do_disable(_):
r.disable(now)
-def do_force_reconnect(arg):
+
+def do_force_reconnect(_):
r.force_reconnect(now)
+
def error_from_string(s):
if not s:
return 0
@@ -38,21 +45,27 @@ def error_from_string(s):
sys.stderr.write("unknown error '%s'\n" % s)
sys.exit(1)
+
def do_disconnected(arg):
r.disconnected(now, error_from_string(arg))
-
-def do_connecting(arg):
+
+
+def do_connecting(_):
r.connecting(now)
+
def do_connect_failed(arg):
r.connect_failed(now, error_from_string(arg))
-def do_connected(arg):
+
+def do_connected(_):
r.connected(now)
-def do_received(arg):
+
+def do_received(_):
r.received(now)
+
def do_run(arg):
global now
if arg is not None:
@@ -70,11 +83,13 @@ def do_run(arg):
else:
assert False
+
def do_advance(arg):
global now
now += int(arg)
-def do_timeout(arg):
+
+def do_timeout(_):
global now
timeout = r.timeout(now)
if timeout >= 0:
@@ -83,9 +98,11 @@ def do_timeout(arg):
else:
print " no timeout"
+
def do_set_max_tries(arg):
r.set_max_tries(int(arg))
+
def diff_stats(old, new, delta):
if (old.state != new.state or
old.state_elapsed != new.state_elapsed or
@@ -116,9 +133,9 @@ def diff_stats(old, new, delta):
if (old.last_connected != new.last_connected or
(new.msec_since_connect != None and
old.msec_since_connect != new.msec_since_connect - delta) or
- (old.total_connected_duration != new.total_connected_duration - delta and
- not (old.total_connected_duration == 0 and
- new.total_connected_duration == 0))):
+ (old.total_connected_duration != new.total_connected_duration - delta
+ and not (old.total_connected_duration == 0 and
+ new.total_connected_duration == 0))):
print(" last connected %d ms ago, connected %d ms total"
% (new.msec_since_connect, new.total_connected_duration))
@@ -128,15 +145,19 @@ def diff_stats(old, new, delta):
print(" disconnected at %d ms (%d ms ago)"
% (new.last_disconnected, new.msec_since_disconnect))
-def do_set_passive(arg):
+
+def do_set_passive(_):
r.set_passive(True, now)
-def do_listening(arg):
+
+def do_listening(_):
r.listening(now)
+
def do_listen_error(arg):
r.listen_error(now, int(arg))
+
def main():
commands = {
"enable": do_enable,
@@ -201,7 +222,6 @@ def main():
old_time = now
+
if __name__ == '__main__':
main()
-
-