system-traffic: Skip all vxlan tests if unsupported.
[cascardo/ovs.git] / tests / test-ovsdb.py
index 392ed4b..a6897f3 100644 (file)
@@ -105,7 +105,7 @@ def do_parse_atoms(type_string, *atom_strings):
             atom = data.Atom.from_json(base, atom_json)
             print ovs.json.to_string(atom.to_json())
         except error.Error, e:
-            print unicode(e)
+            print e.args[0].encode("utf8")
 
 
 def do_parse_data(type_string, *data_strings):
@@ -146,44 +146,53 @@ def do_parse_schema(schema_string):
 
 
 def print_idl(idl, step):
-    simple = idl.tables["simple"].rows
-    l1 = idl.tables["link1"].rows
-    l2 = idl.tables["link2"].rows
-
     n = 0
-    for row in simple.itervalues():
-        s = ("%03d: i=%s r=%s b=%s s=%s u=%s "
-             "ia=%s ra=%s ba=%s sa=%s ua=%s uuid=%s"
-             % (step, row.i, row.r, row.b, row.s, row.u,
-                row.ia, row.ra, row.ba, row.sa, row.ua, row.uuid))
-        s = re.sub('""|,|u?\'', "", s)
-        s = re.sub('UUID\(([^)]+)\)', r'\1', s)
-        s = re.sub('False', 'false', s)
-        s = re.sub('True', 'true', s)
-        s = re.sub(r'(ba)=([^[][^ ]*) ', r'\1=[\2] ', s)
-        print(s)
-        n += 1
-
-    for row in l1.itervalues():
-        s = ["%03d: i=%s k=" % (step, row.i)]
-        if row.k:
-            s.append(str(row.k.i))
-        s.append(" ka=[")
-        s.append(' '.join(sorted(str(ka.i) for ka in row.ka)))
-        s.append("] l2=")
-        if row.l2:
-            s.append(str(row.l2[0].i))
-        s.append(" uuid=%s" % row.uuid)
-        print(''.join(s))
-        n += 1
-
-    for row in l2.itervalues():
-        s = ["%03d: i=%s l1=" % (step, row.i)]
-        if row.l1:
-            s.append(str(row.l1[0].i))
-        s.append(" uuid=%s" % row.uuid)
-        print(''.join(s))
-        n += 1
+    if "simple" in idl.tables:
+        simple_columns = ["i", "r", "b", "s", "u", "ia",
+                          "ra", "ba", "sa", "ua", "uuid"]
+        simple = idl.tables["simple"].rows
+        for row in simple.itervalues():
+            s = "%03d:" % step
+            for column in simple_columns:
+                if hasattr(row, column) and not (type(getattr(row, column))
+                                                 is ovs.db.data.Atom):
+                    s += " %s=%s" % (column, getattr(row, column))
+            s = re.sub('""|,|u?\'', "", s)
+            s = re.sub('UUID\(([^)]+)\)', r'\1', s)
+            s = re.sub('False', 'false', s)
+            s = re.sub('True', 'true', s)
+            s = re.sub(r'(ba)=([^[][^ ]*) ', r'\1=[\2] ', s)
+            print(s)
+            n += 1
+
+    if "link1" in idl.tables:
+        l1 = idl.tables["link1"].rows
+        for row in l1.itervalues():
+            s = ["%03d: i=%s k=" % (step, row.i)]
+            if hasattr(row, "k") and row.k:
+                s.append(str(row.k.i))
+            if hasattr(row, "ka"):
+                s.append(" ka=[")
+                s.append(' '.join(sorted(str(ka.i) for ka in row.ka)))
+                s.append("] l2=")
+            if hasattr(row, "l2") and row.l2:
+                s.append(str(row.l2[0].i))
+            if hasattr(row, "uuid"):
+                s.append(" uuid=%s" % row.uuid)
+            print(''.join(s))
+            n += 1
+
+    if "link2" in idl.tables:
+        l2 = idl.tables["link2"].rows
+        for row in l2.itervalues():
+            s = ["%03d:" % step]
+            s.append(" i=%s l1=" % row.i)
+            if hasattr(row, "l1") and row.l1:
+                s.append(str(row.l1[0].i))
+            if hasattr(row, "uuid"):
+                s.append(" uuid=%s" % row.uuid)
+            print(''.join(s))
+            n += 1
 
     if not n:
         print("%03d: empty" % step)
@@ -228,11 +237,28 @@ def idltest_find_simple(idl, i):
 def idl_set(idl, commands, step):
     txn = ovs.db.idl.Transaction(idl)
     increment = False
+    fetch_cmds = []
+    events = []
     for command in commands.split(','):
         words = command.split()
         name = words[0]
         args = words[1:]
 
+        if name == "notifytest":
+            name = args[0]
+            args = args[1:]
+            old_notify = idl.notify
+
+            def notify(event, row, updates=None):
+                if updates:
+                    upcol = updates._data.keys()[0]
+                else:
+                    upcol = None
+                events.append("%s|%s|%s" % (event, row.i, upcol))
+                idl.notify = old_notify
+
+            idl.notify = notify
+
         if name == "set":
             if len(args) != 3:
                 sys.stderr.write('"set" command requires 3 arguments\n')
@@ -291,6 +317,20 @@ def idl_set(idl, commands, step):
                 sys.stderr.write('"verify" command asks for unknown column '
                                  '"%s"\n' % args[1])
                 sys.exit(1)
+        elif name == "fetch":
+            if len(args) != 2:
+                sys.stderr.write('"fetch" command requires 2 argument\n')
+                sys.exit(1)
+
+            row = idltest_find_simple(idl, int(args[0]))
+            if not row:
+                sys.stderr.write('"fetch" command asks for nonexistent i=%d\n'
+                                 % int(args[0]))
+                sys.exit(1)
+
+            column = args[1]
+            row.fetch(column)
+            fetch_cmds.append([row, column])
         elif name == "increment":
             if len(args) != 1:
                 sys.stderr.write('"increment" command requires 1 argument\n')
@@ -338,13 +378,31 @@ def idl_set(idl, commands, step):
                      % (step, ovs.db.idl.Transaction.status_to_string(status)))
     if increment and status == ovs.db.idl.Transaction.SUCCESS:
         sys.stdout.write(", increment=%d" % txn.get_increment_new_value())
+    if events:
+        # Event notifications from operations in a single transaction are
+        # not in a gauranteed order due to update messages being dicts
+        sys.stdout.write(", events=" + ", ".join(sorted(events)))
     sys.stdout.write("\n")
     sys.stdout.flush()
 
 
 def do_idl(schema_file, remote, *commands):
     schema_helper = ovs.db.idl.SchemaHelper(schema_file)
-    schema_helper.register_all()
+    if commands and commands[0].startswith("?"):
+        monitor = {}
+        readonly = {}
+        for x in commands[0][1:].split("?"):
+            readonly = []
+            table, columns = x.split(":")
+            columns = columns.split(",")
+            for index, column in enumerate(columns):
+                if column[-1] == '!':
+                    columns[index] = columns[index][:-1]
+                    readonly.append(columns[index])
+            schema_helper.register_columns(table, columns, readonly)
+        commands = commands[1:]
+    else:
+        schema_helper.register_all()
     idl = ovs.db.idl.Idl(remote, schema_helper)
 
     if commands:
@@ -455,11 +513,28 @@ parse-table NAME OBJECT [DEFAULT-IS-ROOT]
   parse table NAME with info OBJECT
 parse-schema JSON
   parse JSON as an OVSDB schema, and re-serialize
-idl SCHEMA SERVER [TRANSACTION...]
+idl SCHEMA SERVER [?T1:C1,C2...[?T2:C1,C2,...]...] [TRANSACTION...]
   connect to SERVER (which has the specified SCHEMA) and dump the
   contents of the database as seen initially by the IDL implementation
   and after executing each TRANSACTION.  (Each TRANSACTION must modify
   the database or this command will hang.)
+  By default, all columns of all tables are monitored. The "?" option
+  can be used to monitor specific Table:Column(s). The table and their
+  columns are listed as a string of the form starting with "?":
+      ?<table-name>:<column-name>,<column-name>,...
+  e.g.:
+      ?simple:b - Monitor column "b" in table "simple"
+  Entries for multiple tables are seperated by "?":
+      ?<table-name>:<column-name>,...?<table-name>:<column-name>,...
+  e.g.:
+      ?simple:b?link1:i,k - Monitor column "b" in table "simple",
+                            and column "i", "k" in table "link1"
+  Readonly columns: Suffixing a "!" after a column indicates that the
+  column is to be registered "readonly".
+  e.g.:
+      ?simple:i,b!  - Register interest in column "i" (monitoring) and
+                      column "b" (readonly).
+
 
 The following options are also available:
   -t, --timeout=SECS          give up after SECS seconds