]> git.proxmox.com Git - ovs.git/blame - tests/ovstest.c
Fix ovs-dpctl-top by removing 3 wrong hunks in py3-compat.patch.
[ovs.git] / tests / ovstest.c
CommitLineData
3932d8db
AZ
1/*
2 * Copyright (c) 2014 Nicira, Inc.
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17/* The mother of all test programs that links with libopevswitch.la */
18
19#include <config.h>
3f636c7e 20#undef NDEBUG
3932d8db
AZ
21#include <inttypes.h>
22#include <limits.h>
23#include <stdlib.h>
24#include "command-line.h"
3e8a2ad1 25#include "openvswitch/dynamic-string.h"
3f636c7e 26#include "ovstest.h"
3932d8db
AZ
27#include "util.h"
28
5f383751 29static struct ovs_cmdl_command *commands = NULL;
3932d8db
AZ
30static size_t n_commands = 0;
31static size_t allocated_commands = 0;
32
33static void
5f383751 34add_command(struct ovs_cmdl_command *cmd)
3932d8db 35{
1f4a7252 36 const struct ovs_cmdl_command nil = {NULL, NULL, 0, 0, NULL, OVS_RO};
3932d8db
AZ
37
38 while (n_commands + 1 >= allocated_commands) {
39 commands = x2nrealloc(commands, &allocated_commands,
40 sizeof *cmd);
41 }
42
43 commands[n_commands] = *cmd;
44 commands[n_commands + 1] = nil;
45 n_commands++;
46}
47
01fed651
AZ
48#define OVSTEST_USAGE \
49"TEST [TESTARGS] where 'TEST' is a string, 'TESTARGS' are optional \n"\
50"arguments of the TEST"
51
52static void
53flush_help_string(struct ds *ds)
54{
55 if (ds->length > 2 ) {
56 ds->length -= 2;
57 printf ("%s\n", ds_cstr(ds));
58 ds_clear(ds);
59 }
60}
61
3932d8db 62static void
1636c761 63help(struct ovs_cmdl_context *ctx OVS_UNUSED)
3932d8db 64{
5f383751 65 const struct ovs_cmdl_command *p;
01fed651
AZ
66 struct ds test_names = DS_EMPTY_INITIALIZER;
67 const int linesize = 70;
68
69 printf("%s: the big test executable\n"
70 "usage: %s TEST [TESTARGS]\n"
71 "where TEST is one of the following. \n\n",
72 program_name, program_name);
3932d8db
AZ
73
74 for(p = commands; p->name != NULL; p++) {
01fed651
AZ
75 if (*p->name != '-') { /* Skip internal commands */
76 ds_put_format(&test_names, "%s, ", p->name);
77 if ((test_names.length) >= linesize) {
78 flush_help_string(&test_names);
79 }
80 }
3932d8db 81 }
01fed651
AZ
82 flush_help_string(&test_names);
83 ds_destroy(&test_names);
3932d8db
AZ
84}
85
86static void
87add_top_level_commands(void)
88{
1f4a7252 89 struct ovs_cmdl_command help_cmd = {"--help", NULL, 0, 0, help, OVS_RO };
3932d8db
AZ
90
91 add_command(&help_cmd);
92}
93
94void
1636c761 95ovstest_register(const char *test_name, ovs_cmdl_handler f)
3932d8db 96{
5f383751 97 struct ovs_cmdl_command test_cmd;
3932d8db
AZ
98
99 test_cmd.name = test_name;
451de37e 100 test_cmd.usage = NULL;
01fed651
AZ
101 test_cmd.min_args = 0;
102 test_cmd.max_args = INT_MAX;
3932d8db 103 test_cmd.handler = f;
2e3cf773 104 test_cmd.mode = OVS_RO;
3932d8db
AZ
105
106 add_command(&test_cmd);
107}
108
109static void
110cleanup(void)
111{
112 if (allocated_commands) {
113 free(commands);
114 }
115}
116
117int
118main(int argc, char *argv[])
119{
120 set_program_name(argv[0]);
121
01fed651
AZ
122 if (argc < 2) {
123 ovs_fatal(0, "expect test program to be specified; "
124 "use --help for usage");
125 }
126
3932d8db
AZ
127 add_top_level_commands();
128 if (argc > 1) {
1636c761
RB
129 struct ovs_cmdl_context ctx = {
130 .argc = argc - 1,
131 .argv = argv + 1,
132 };
133 ovs_cmdl_run_command(&ctx, commands);
3932d8db
AZ
134 }
135 cleanup();
136
137 return 0;
138}