2 * Copyright (c) 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* The mother of all test programs that links with libopevswitch.la */
23 #include "command-line.h"
25 #include "dynamic-string.h"
28 static struct command *commands = NULL;
29 static size_t n_commands = 0;
30 static size_t allocated_commands = 0;
33 add_command(struct command *cmd)
35 const struct command nil = {NULL, 0, 0, NULL};
37 while (n_commands + 1 >= allocated_commands) {
38 commands = x2nrealloc(commands, &allocated_commands,
42 commands[n_commands] = *cmd;
43 commands[n_commands + 1] = nil;
47 #define OVSTEST_USAGE \
48 "TEST [TESTARGS] where 'TEST' is a string, 'TESTARGS' are optional \n"\
49 "arguments of the TEST"
52 flush_help_string(struct ds *ds)
54 if (ds->length > 2 ) {
56 printf ("%s\n", ds_cstr(ds));
62 help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
64 const struct command *p;
65 struct ds test_names = DS_EMPTY_INITIALIZER;
66 const int linesize = 70;
68 printf("%s: the big test executable\n"
69 "usage: %s TEST [TESTARGS]\n"
70 "where TEST is one of the following. \n\n",
71 program_name, program_name);
73 for(p = commands; p->name != NULL; p++) {
74 if (*p->name != '-') { /* Skip internal commands */
75 ds_put_format(&test_names, "%s, ", p->name);
76 if ((test_names.length) >= linesize) {
77 flush_help_string(&test_names);
81 flush_help_string(&test_names);
82 ds_destroy(&test_names);
86 add_top_level_commands(void)
88 struct command help_cmd = {"--help", 0, 0, help};
90 add_command(&help_cmd);
94 ovstest_register(const char *test_name, ovstest_func f)
96 struct command test_cmd;
98 test_cmd.name = test_name;
99 test_cmd.min_args = 0;
100 test_cmd.max_args = INT_MAX;
101 test_cmd.handler = f;
103 add_command(&test_cmd);
109 if (allocated_commands) {
115 main(int argc, char *argv[])
117 set_program_name(argv[0]);
120 ovs_fatal(0, "expect test program to be specified; "
121 "use --help for usage");
124 add_top_level_commands();
126 run_command(argc - 1, argv + 1, commands);