[ovs-dev] [PATCH monitor_cond 12/12] tests: add testing for idl conditional monitoring

Liran Schour lirans at il.ibm.com
Tue Jan 5 13:14:05 UTC 2016


Testsing ovsdb-idl with condition change by "monitor_cond_change"
method.

Signed-off-by: Liran Schour <lirans at il.ibm.com>
---
 tests/ovsdb-idl.at  | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 tests/test-ovsdb.c  | 52 ++++++++++++++++++++++++++++++++++++-
 tests/test-ovsdb.py | 24 +++++++++++++++++
 3 files changed, 149 insertions(+), 1 deletion(-)

diff --git a/tests/ovsdb-idl.at b/tests/ovsdb-idl.at
index 2b3b084..7069f6c 100644
--- a/tests/ovsdb-idl.at
+++ b/tests/ovsdb-idl.at
@@ -329,6 +329,80 @@ OVSDB_CHECK_IDL([simple idl, destroy without commit or abort],
 004: done
 ]])
 
+OVSDB_CHECK_IDL([simple idl, conditional, false condition],
+  [['["idltest",
+       {"op": "insert",
+       "table": "simple",
+       "row": {"i": 1,
+               "r": 2.0,
+               "b": true}}]']],
+  [['condition {"simple": {"added":[false], "removed" :[]}}' \
+    'condition {"simple": {"added":[], "removed" :[false]}}']],
+  [[000: change conditions
+001: empty
+002: change conditions
+003: i=1 r=2 b=true s= u=<0> ia=[] ra=[] ba=[] sa=[] ua=[] uuid=<1>
+004: done
+]])
+
+OVSDB_CHECK_IDL([simple idl, conditional, true condition],
+  [['["idltest",
+       {"op": "insert",
+       "table": "simple",
+       "row": {"i": 1,
+               "r": 2.0,
+               "b": true}}]']],
+  [['condition {"simple": {"added":[false], "removed" :[]}}' \
+    'condition {"simple": {"added":[true], "removed" :[]}}']],
+  [[000: change conditions
+001: empty
+002: change conditions
+003: i=1 r=2 b=true s= u=<0> ia=[] ra=[] ba=[] sa=[] ua=[] uuid=<1>
+004: done
+]])
+
+OVSDB_CHECK_IDL([simple idl, conditional, modify as insert due to condition],
+  [['["idltest",
+       {"op": "insert",
+       "table": "simple",
+       "row": {"i": 1,
+               "r": 2.0,
+               "b": true}}]']],
+  [['condition {"simple": {"added":[false], "removed" :[]}}' \
+    'condition {"simple": {"added":[["i","==",1]], "removed" :[]}}']],
+  [[000: change conditions
+001: empty
+002: change conditions
+003: i=1 r=2 b=true s= u=<0> ia=[] ra=[] ba=[] sa=[] ua=[] uuid=<1>
+004: done
+]])
+
+OVSDB_CHECK_IDL([simple idl, conditional, modify as delete due to condition],
+  [['["idltest",
+       {"op": "insert",
+       "table": "simple",
+       "row": {"i": 1,
+               "r": 2.0,
+               "b": true}}]']],
+  [['condition {"simple": {"added":[false], "removed" :[]}}' \
+    'condition {"simple": {"added":[["i","==",1]], "removed" :[]}}' \
+    'condition {"simple": {"added":[["i","==",2]], "removed" :[["i","==",1]]}}' \
+    '+["idltest",
+       {"op": "insert",
+       "table": "simple",
+       "row": {"i": 3,
+               "r": 2.0,
+               "b": true}}]']],
+  [[000: change conditions
+001: empty
+002: change conditions
+003: i=1 r=2 b=true s= u=<0> ia=[] ra=[] ba=[] sa=[] ua=[] uuid=<1>
+004: change conditions
+005: {"error":null,"result":[{"uuid":["uuid","<2>"]}]}
+006: empty
+007: done
+]])
+
 OVSDB_CHECK_IDL([self-linking idl, consistent ops],
   [],
   [['["idltest",
diff --git a/tests/test-ovsdb.c b/tests/test-ovsdb.c
index 92159b4..f901160 100644
--- a/tests/test-ovsdb.c
+++ b/tests/test-ovsdb.c
@@ -2022,6 +2022,46 @@ idl_set(struct ovsdb_idl *idl, char *commands, int step)
 }
 
 static void
+change_conditions(struct ovsdb_idl *idl, const char *arg)
+{
+    struct json *cond, *json = parse_json(arg);
+    struct json *added, *removed;
+    const struct shash_node *table_node;
+    struct ovsdb_idl_condition add = OVSDB_CONDITION_INITIALIZER;
+    struct ovsdb_idl_condition remove= OVSDB_CONDITION_INITIALIZER;
+    struct ovsdb_error *ovsdb_error;
+
+    if (json->type != JSON_OBJECT) {
+        ovs_fatal(0, "condition should be in json object format");
+    }
+    table_node = shash_first(json_object(json));
+    cond = table_node->data;
+    added = shash_find_data(json_object(cond), "added");
+    if (added) {
+        ovsdb_error = ovsdb_idl_condition_from_json(idl,
+                                                    table_node->name,
+                                                    added,
+                                                    &add);
+        if (ovsdb_error) {
+            ovs_fatal(0, "Failed to parse json condition");
+        }
+    }
+    removed = shash_find_data(json_object(cond), "removed");
+    if (removed) {
+        ovsdb_error = ovsdb_idl_condition_from_json(idl,
+                                                    table_node->name,
+                                                    removed,
+                                                    &remove);
+        if (ovsdb_error) {
+            ovs_fatal(0, "Failed to parse json condition");
+        }
+    }
+    if (!ovsdb_idl_cond_change(idl, table_node->name, &add, &remove)) {
+        ovs_fatal(0, "Error change conditions");
+    }
+}
+
+static void
 do_idl(struct ovs_cmdl_context *ctx)
 {
     struct jsonrpc *rpc;
@@ -2059,7 +2099,14 @@ do_idl(struct ovs_cmdl_context *ctx)
     setvbuf(stdout, NULL, _IONBF, 0);
 
     symtab = ovsdb_symbol_table_create();
-    for (i = 2; i < ctx->argc; i++) {
+    if (ctx->argc > 2 && strstr(ctx->argv[2], "condition ")) {
+        change_conditions(idl, ctx->argv[2] + strlen("condition "));
+        printf("%03d: change conditions\n", step++);
+        i = 3;
+    } else {
+        i = 2;
+    }
+    for (; i < ctx->argc; i++) {
         char *arg = ctx->argv[i];
         struct jsonrpc_msg *request, *reply;
 
@@ -2093,6 +2140,9 @@ do_idl(struct ovs_cmdl_context *ctx)
         if (!strcmp(arg, "reconnect")) {
             printf("%03d: reconnect\n", step++);
             ovsdb_idl_force_reconnect(idl);
+        }  else if (strstr(arg, "condition ")) {
+            change_conditions(idl, arg + strlen("condition "));
+            printf("%03d: change conditions\n", step++);
         } else if (arg[0] != '[') {
             idl_set(idl, arg, step++);
         } else {
diff --git a/tests/test-ovsdb.py b/tests/test-ovsdb.py
index a6897f3..cde0c99 100644
--- a/tests/test-ovsdb.py
+++ b/tests/test-ovsdb.py
@@ -385,6 +385,17 @@ def idl_set(idl, commands, step):
     sys.stdout.write("\n")
     sys.stdout.flush()
 
+def change_condition(idl, command):
+    command = ovs.json.from_string(command)
+    for k, v in command.iteritems():
+        add = remove = None
+
+        if "added" in v:
+            add = v['added']
+        if "removed" in v:
+            remove = v['removed']
+
+        idl.cond_change(k, add, remove)
 
 def do_idl(schema_file, remote, *commands):
     schema_helper = ovs.db.idl.SchemaHelper(schema_file)
@@ -418,6 +429,14 @@ def do_idl(schema_file, remote, *commands):
     symtab = {}
     seqno = 0
     step = 0
+
+    commands = list(commands)
+    if len(commands) >= 1 and "condition" in commands[0]:
+        change_condition(idl, commands.pop(0)[len("condition "):])
+        sys.stdout.write("%03d: change conditions\n" % step)
+        sys.stdout.flush()
+        step += 1
+
     for command in commands:
         if command.startswith("+"):
             # The previous transaction didn't change anything.
@@ -442,6 +461,11 @@ def do_idl(schema_file, remote, *commands):
             sys.stdout.flush()
             step += 1
             idl.force_reconnect()
+        elif "condition" in command:
+            change_condition(idl, command[len("condition "):])
+            sys.stdout.write("%03d: change conditions\n" % step)
+            sys.stdout.flush()
+            step += 1
         elif not command.startswith("["):
             idl_set(idl, command, step)
             step += 1
-- 
2.1.4





More information about the dev mailing list