ovs-vsctl: Support references among records at creation time.
[openvswitch] / tests / test-ovsdb.c
index 4b38ecb5a533f2a92909bf7acc524440e4687f19..21825d74964b1cc2de7f559649ff967b9075aa67 100644 (file)
@@ -159,6 +159,8 @@ usage(void)
            "  query-distinct TABLE [ROW,...] [CONDITION,...] COLUMNS\n"
            "    add each ROW to TABLE, then query and print the rows that\n"
            "    satisfy each CONDITION and have distinct COLUMNS.\n"
+           "  parse-schema JSON\n"
+           "    parse JSON as an OVSDB schema, and re-serialize\n"
            "  transact COMMAND\n"
            "    execute each specified transactional COMMAND:\n"
            "      commit\n"
@@ -255,34 +257,24 @@ static void
 do_log_io(int argc, char *argv[])
 {
     const char *name = argv[1];
-    char *mode = argv[2];
+    char *mode_string = argv[2];
 
     struct ovsdb_error *error;
+    enum ovsdb_log_open_mode mode;
     struct ovsdb_log *log;
-    char *save_ptr = NULL;
-    const char *token;
-    int flags;
     int i;
 
-    for (flags = 0, token = strtok_r(mode, " |", &save_ptr); token != NULL;
-         token = strtok_r(NULL, " |", &save_ptr))
-    {
-        if (!strcmp(token, "O_RDONLY")) {
-            flags |= O_RDONLY;
-        } else if (!strcmp(token, "O_RDWR")) {
-            flags |= O_RDWR;
-        } else if (!strcmp(token, "O_TRUNC")) {
-            flags |= O_TRUNC;
-        } else if (!strcmp(token, "O_CREAT")) {
-            flags |= O_CREAT;
-        } else if (!strcmp(token, "O_EXCL")) {
-            flags |= O_EXCL;
-        } else if (!strcmp(token, "O_TRUNC")) {
-            flags |= O_TRUNC;
-        }
+    if (!strcmp(mode_string, "read-only")) {
+        mode = OVSDB_LOG_READ_ONLY;
+    } else if (!strcmp(mode_string, "read/write")) {
+        mode = OVSDB_LOG_READ_WRITE;
+    } else if (!strcmp(mode_string, "create")) {
+        mode = OVSDB_LOG_CREATE;
+    } else {
+        ovs_fatal(0, "unknown log-io open mode \"%s\"", mode_string);
     }
 
-    check_ovsdb_error(ovsdb_log_open(name, flags, &log));
+    check_ovsdb_error(ovsdb_log_open(name, mode, -1, &log));
     printf("%s: open successful\n", name);
 
     for (i = 3; i < argc; i++) {
@@ -323,7 +315,7 @@ do_log_io(int argc, char *argv[])
 }
 
 static void
-do_parse_atomic_type(int argc UNUSED, char *argv[])
+do_parse_atomic_type(int argc OVS_UNUSED, char *argv[])
 {
     enum ovsdb_atomic_type type;
     struct json *json;
@@ -335,7 +327,7 @@ do_parse_atomic_type(int argc UNUSED, char *argv[])
 }
 
 static void
-do_parse_base_type(int argc UNUSED, char *argv[])
+do_parse_base_type(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_base_type base;
     struct json *json;
@@ -348,7 +340,7 @@ do_parse_base_type(int argc UNUSED, char *argv[])
 }
 
 static void
-do_parse_type(int argc UNUSED, char *argv[])
+do_parse_type(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_type type;
     struct json *json;
@@ -404,7 +396,7 @@ do_parse_atom_strings(int argc, char *argv[])
         union ovsdb_atom atom;
         struct ds out;
 
-        die_if_error(ovsdb_atom_from_string(&atom, &base, argv[i]));
+        die_if_error(ovsdb_atom_from_string(&atom, &base, argv[i], NULL));
 
         ds_init(&out);
         ovsdb_atom_to_string(&atom, base.type, &out);
@@ -456,7 +448,7 @@ do_parse_data_strings(int argc, char *argv[])
         struct ovsdb_datum datum;
         struct ds out;
 
-        die_if_error(ovsdb_datum_from_string(&datum, &type, argv[i]));
+        die_if_error(ovsdb_datum_from_string(&datum, &type, argv[i], NULL));
 
         ds_init(&out);
         ovsdb_datum_to_string(&datum, &type, &out);
@@ -480,7 +472,7 @@ compare_atoms(const void *a_, const void *b_)
 }
 
 static void
-do_sort_atoms(int argc UNUSED, char *argv[])
+do_sort_atoms(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_base_type base;
     union ovsdb_atom *atoms;
@@ -522,7 +514,7 @@ do_sort_atoms(int argc UNUSED, char *argv[])
 }
 
 static void
-do_parse_column(int argc UNUSED, char *argv[])
+do_parse_column(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_column *column;
     struct json *json;
@@ -535,7 +527,7 @@ do_parse_column(int argc UNUSED, char *argv[])
 }
 
 static void
-do_parse_table(int argc UNUSED, char *argv[])
+do_parse_table(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_table_schema *ts;
     struct json *json;
@@ -703,7 +695,7 @@ do_parse_conditions(int argc, char *argv[])
 }
 
 static void
-do_evaluate_conditions(int argc UNUSED, char *argv[])
+do_evaluate_conditions(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_table_schema *ts;
     struct ovsdb_table *table;
@@ -749,7 +741,7 @@ do_evaluate_conditions(int argc UNUSED, char *argv[])
     json_destroy(json);
 
     for (i = 0; i < n_conditions; i++) {
-        printf("condition %2d:", i);
+        printf("condition %2zu:", i);
         for (j = 0; j < n_rows; j++) {
             bool result = ovsdb_condition_evaluate(rows[j], &conditions[i]);
             if (j % 5 == 0) {
@@ -808,7 +800,7 @@ do_parse_mutations(int argc, char *argv[])
 }
 
 static void
-do_execute_mutations(int argc UNUSED, char *argv[])
+do_execute_mutations(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_table_schema *ts;
     struct ovsdb_table *table;
@@ -855,7 +847,7 @@ do_execute_mutations(int argc UNUSED, char *argv[])
     json_destroy(json);
 
     for (i = 0; i < n_sets; i++) {
-        printf("mutation %2d:\n", i);
+        printf("mutation %2zu:\n", i);
         for (j = 0; j < n_rows; j++) {
             struct ovsdb_error *error;
             struct ovsdb_row *row;
@@ -924,7 +916,7 @@ do_query_cb(const struct ovsdb_row *row, void *cbdata_)
 }
 
 static void
-do_query(int argc UNUSED, char *argv[])
+do_query(int argc OVS_UNUSED, char *argv[])
 {
     struct do_query_cbdata cbdata;
     struct ovsdb_table_schema *ts;
@@ -977,7 +969,7 @@ do_query(int argc UNUSED, char *argv[])
         memset(cbdata.counts, 0, cbdata.n_rows * sizeof *cbdata.counts);
         ovsdb_query(table, &cnd, do_query_cb, &cbdata);
 
-        printf("query %2d:", i);
+        printf("query %2zu:", i);
         for (j = 0; j < cbdata.n_rows; j++) {
             if (j % 5 == 0) {
                 putchar(' ');
@@ -1014,7 +1006,7 @@ struct do_query_distinct_row {
 };
 
 static void
-do_query_distinct(int argc UNUSED, char *argv[])
+do_query_distinct(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_column_set columns;
     struct ovsdb_table_schema *ts;
@@ -1110,7 +1102,7 @@ do_query_distinct(int argc UNUSED, char *argv[])
         }
         ovsdb_row_set_destroy(&results);
 
-        printf("query %2d:", i);
+        printf("query %2zu:", i);
         for (j = 0; j < n_rows; j++) {
             int count = rows[j].class->count;
 
@@ -1139,7 +1131,20 @@ do_query_distinct(int argc UNUSED, char *argv[])
 }
 
 static void
-do_execute(int argc UNUSED, char *argv[])
+do_parse_schema(int argc OVS_UNUSED, char *argv[])
+{
+    struct ovsdb_schema *schema;
+    struct json *json;
+
+    json = parse_json(argv[1]);
+    check_ovsdb_error(ovsdb_schema_from_json(json, &schema));
+    json_destroy(json);
+    print_and_free_json(ovsdb_schema_to_json(schema));
+    ovsdb_schema_destroy(schema);
+}
+
+static void
+do_execute(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_schema *schema;
     struct json *json;
@@ -1189,7 +1194,7 @@ do_trigger_dump(struct test_trigger *t, long long int now, const char *title)
 }
 
 static void
-do_trigger(int argc UNUSED, char *argv[])
+do_trigger(int argc OVS_UNUSED, char *argv[])
 {
     struct ovsdb_schema *schema;
     struct list completions;
@@ -1244,7 +1249,7 @@ do_trigger(int argc UNUSED, char *argv[])
 }
 
 static void
-do_help(int argc UNUSED, char *argv[] UNUSED)
+do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     usage();
 }
@@ -1256,14 +1261,14 @@ static struct ovsdb_txn *do_transact_txn;
 static struct ovsdb_table *do_transact_table;
 
 static void
-do_transact_commit(int argc UNUSED, char *argv[] UNUSED)
+do_transact_commit(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     ovsdb_txn_commit(do_transact_txn, false);
     do_transact_txn = NULL;
 }
 
 static void
-do_transact_abort(int argc UNUSED, char *argv[] UNUSED)
+do_transact_abort(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     ovsdb_txn_abort(do_transact_txn);
     do_transact_txn = NULL;
@@ -1323,7 +1328,7 @@ do_transact_set_i_j(struct ovsdb_row *row,
 }
 
 static void
-do_transact_insert(int argc UNUSED, char *argv[] UNUSED)
+do_transact_insert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     struct ovsdb_row *row;
     struct uuid *uuid;
@@ -1345,14 +1350,14 @@ do_transact_insert(int argc UNUSED, char *argv[] UNUSED)
 }
 
 static void
-do_transact_delete(int argc UNUSED, char *argv[] UNUSED)
+do_transact_delete(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     const struct ovsdb_row *row = do_transact_find_row(argv[1]);
     ovsdb_txn_row_delete(do_transact_txn, row);
 }
 
 static void
-do_transact_modify(int argc UNUSED, char *argv[] UNUSED)
+do_transact_modify(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     const struct ovsdb_row *row_ro;
     struct ovsdb_row *row_rw;
@@ -1372,7 +1377,7 @@ compare_rows_by_uuid(const void *a_, const void *b_)
 }
 
 static void
-do_transact_print(int argc UNUSED, char *argv[] UNUSED)
+do_transact_print(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     const struct ovsdb_row **rows;
     const struct ovsdb_row *row;
@@ -1592,7 +1597,7 @@ parse_uuids(const struct json *json, struct ovsdb_symbol_table *symtab,
     struct uuid uuid;
 
     if (json->type == JSON_STRING && uuid_from_string(&uuid, json->u.string)) {
-        char *name = xasprintf("#%d#", *n);
+        char *name = xasprintf("#%zu#", *n);
         fprintf(stderr, "%s = "UUID_FMT"\n", name, UUID_ARGS(&uuid));
         ovsdb_symbol_table_put(symtab, name, &uuid, false);
         free(name);
@@ -1730,12 +1735,7 @@ idl_set(struct ovsdb_idl *idl, char *commands, int step)
         }
     }
 
-    while ((status = ovsdb_idl_txn_commit(txn)) == TXN_INCOMPLETE) {
-        ovsdb_idl_run(idl);
-        ovsdb_idl_wait(idl);
-        ovsdb_idl_txn_wait(txn);
-        poll_block();
-    }
+    status = ovsdb_idl_txn_commit_block(txn);
     printf("%03d: commit, status=%s",
            step, ovsdb_idl_txn_status_to_string(status));
     if (increment) {
@@ -1764,7 +1764,8 @@ do_idl(int argc, char *argv[])
     if (argc > 2) {
         struct stream *stream;
 
-        error = stream_open_block(argv[1], &stream);
+        error = stream_open_block(jsonrpc_stream_open(argv[1], &stream),
+                                  &stream);
         if (error) {
             ovs_fatal(error, "failed to connect to \"%s\"", argv[1]);
         }
@@ -1773,20 +1774,28 @@ do_idl(int argc, char *argv[])
         rpc = NULL;
     }
 
+    setvbuf(stdout, NULL, _IOLBF, 0);
+
     symtab = ovsdb_symbol_table_create();
     for (i = 2; i < argc; i++) {
+        char *arg = argv[i];
         struct jsonrpc_msg *request, *reply;
         int error;
 
-        seqno = print_updated_idl(idl, rpc, step++, seqno);
+        if (*arg == '+') {
+            /* The previous transaction didn't change anything. */
+            arg++;
+        } else {
+            seqno = print_updated_idl(idl, rpc, step++, seqno);
+        }
 
-        if (!strcmp(argv[i], "reconnect")) {
+        if (!strcmp(arg, "reconnect")) {
             printf("%03d: reconnect\n", step++);
             ovsdb_idl_force_reconnect(idl);
-        } else if (argv[i][0] != '[') {
-            idl_set(idl, argv[i], step++);
+        } else if (arg[0] != '[') {
+            idl_set(idl, arg, step++);
         } else {
-            struct json *json = parse_json(argv[i]);
+            struct json *json = parse_json(arg);
             substitute_uuids(json, symtab);
             request = jsonrpc_create_request("transact", json, NULL);
             error = jsonrpc_transact_block(rpc, request, &reply);
@@ -1833,6 +1842,7 @@ static struct command all_commands[] = {
     { "query", 3, 3, do_query },
     { "query-distinct", 4, 4, do_query_distinct },
     { "transact", 1, INT_MAX, do_transact },
+    { "parse-schema", 1, 1, do_parse_schema },
     { "execute", 2, INT_MAX, do_execute },
     { "trigger", 2, INT_MAX, do_trigger },
     { "idl", 1, INT_MAX, do_idl },