~ubuntu-branches/ubuntu/wily/openvswitch/wily

« back to all changes in this revision

Viewing changes to tests/test-jsonrpc.c

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-08-10 11:35:15 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20150810113515-575vj06oq29emxsn
Tags: 2.4.0~git20150810.97bab95-0ubuntu1
* New upstream snapshot from 2.4 branch:
  - d/*: Align any relevant packaging changes with upstream.
* d/*: wrap-and-sort.
* d/openvswitch-{common,vswitch}.install: Correct install location for
  bash completion files.
* d/tests/openflow.py: Explicitly use ovs-testcontroller as provided
  by 2.4.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 */
16
16
 
17
17
#include <config.h>
18
 
 
 
18
#undef NDEBUG
19
19
#include "jsonrpc.h"
20
 
 
21
20
#include <errno.h>
22
21
#include <fcntl.h>
23
22
#include <getopt.h>
24
23
#include <stdio.h>
25
24
#include <stdlib.h>
26
 
 
27
25
#include "command-line.h"
28
26
#include "daemon.h"
29
27
#include "json.h"
 
28
#include "ovstest.h"
30
29
#include "poll-loop.h"
31
30
#include "stream-ssl.h"
32
31
#include "stream.h"
33
32
#include "timeval.h"
34
33
#include "util.h"
35
 
#include "vlog.h"
36
 
#include "ovstest.h"
 
34
#include "openvswitch/vlog.h"
37
35
 
38
 
static void usage(void) NO_RETURN;
 
36
OVS_NO_RETURN static void usage(void);
39
37
static void parse_options(int argc, char *argv[]);
40
 
static struct command *get_all_commands(void);
 
38
static struct ovs_cmdl_command *get_all_commands(void);
41
39
 
42
40
static void
43
41
test_jsonrpc_main(int argc, char *argv[])
44
42
{
45
 
    proctitle_init(argc, argv);
 
43
    struct ovs_cmdl_context ctx = { .argc = 0, };
 
44
    ovs_cmdl_proctitle_init(argc, argv);
46
45
    set_program_name(argv[0]);
 
46
    service_start(&argc, &argv);
47
47
    parse_options(argc, argv);
48
 
    run_command(argc - optind, argv + optind, get_all_commands());
 
48
    ctx.argc = argc - optind;
 
49
    ctx.argv = argv + optind;
 
50
    ovs_cmdl_run_command(&ctx, get_all_commands());
49
51
}
50
52
 
51
53
static void
63
65
        STREAM_SSL_LONG_OPTIONS,
64
66
        {NULL, 0, NULL, 0},
65
67
    };
66
 
    char *short_options = long_options_to_short_options(long_options);
 
68
    char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
67
69
 
68
70
    for (;;) {
69
71
        int c = getopt_long(argc, argv, short_options, long_options, NULL);
167
169
}
168
170
 
169
171
static void
170
 
do_listen(int argc OVS_UNUSED, char *argv[])
 
172
do_listen(struct ovs_cmdl_context *ctx)
171
173
{
172
174
    struct pstream *pstream;
173
175
    struct jsonrpc **rpcs;
175
177
    bool done;
176
178
    int error;
177
179
 
178
 
    error = jsonrpc_pstream_open(argv[1], &pstream, DSCP_DEFAULT);
 
180
    error = jsonrpc_pstream_open(ctx->argv[1], &pstream, DSCP_DEFAULT);
179
181
    if (error) {
180
 
        ovs_fatal(error, "could not listen on \"%s\"", argv[1]);
 
182
        ovs_fatal(error, "could not listen on \"%s\"", ctx->argv[1]);
181
183
    }
182
184
 
183
185
    daemonize();
250
252
}
251
253
 
252
254
static void
253
 
do_request(int argc OVS_UNUSED, char *argv[])
 
255
do_request(struct ovs_cmdl_context *ctx)
254
256
{
255
257
    struct jsonrpc_msg *msg;
256
258
    struct jsonrpc *rpc;
260
262
    char *string;
261
263
    int error;
262
264
 
263
 
    method = argv[2];
264
 
    params = parse_json(argv[3]);
 
265
    method = ctx->argv[2];
 
266
    params = parse_json(ctx->argv[3]);
265
267
    msg = jsonrpc_create_request(method, params, NULL);
266
268
    string = jsonrpc_msg_is_valid(msg);
267
269
    if (string) {
268
270
        ovs_fatal(0, "not a valid JSON-RPC request: %s", string);
269
271
    }
270
272
 
271
 
    error = stream_open_block(jsonrpc_stream_open(argv[1], &stream,
 
273
    error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
272
274
                              DSCP_DEFAULT), &stream);
273
275
    if (error) {
274
 
        ovs_fatal(error, "could not open \"%s\"", argv[1]);
 
276
        ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]);
275
277
    }
276
278
    rpc = jsonrpc_open(stream);
277
279
 
290
292
}
291
293
 
292
294
static void
293
 
do_notify(int argc OVS_UNUSED, char *argv[])
 
295
do_notify(struct ovs_cmdl_context *ctx)
294
296
{
295
297
    struct jsonrpc_msg *msg;
296
298
    struct jsonrpc *rpc;
300
302
    char *string;
301
303
    int error;
302
304
 
303
 
    method = argv[2];
304
 
    params = parse_json(argv[3]);
 
305
    method = ctx->argv[2];
 
306
    params = parse_json(ctx->argv[3]);
305
307
    msg = jsonrpc_create_notify(method, params);
306
308
    string = jsonrpc_msg_is_valid(msg);
307
309
    if (string) {
308
310
        ovs_fatal(0, "not a JSON RPC-valid notification: %s", string);
309
311
    }
310
312
 
311
 
    error = stream_open_block(jsonrpc_stream_open(argv[1], &stream,
 
313
    error = stream_open_block(jsonrpc_stream_open(ctx->argv[1], &stream,
312
314
                              DSCP_DEFAULT), &stream);
313
315
    if (error) {
314
 
        ovs_fatal(error, "could not open \"%s\"", argv[1]);
 
316
        ovs_fatal(error, "could not open \"%s\"", ctx->argv[1]);
315
317
    }
316
318
    rpc = jsonrpc_open(stream);
317
319
 
323
325
}
324
326
 
325
327
static void
326
 
do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 
328
do_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
327
329
{
328
330
    usage();
329
331
}
330
332
 
331
 
static struct command all_commands[] = {
332
 
    { "listen", 1, 1, do_listen },
333
 
    { "request", 3, 3, do_request },
334
 
    { "notify", 3, 3, do_notify },
335
 
    { "help", 0, INT_MAX, do_help },
336
 
    { NULL, 0, 0, NULL },
 
333
static struct ovs_cmdl_command all_commands[] = {
 
334
    { "listen", NULL, 1, 1, do_listen },
 
335
    { "request", NULL, 3, 3, do_request },
 
336
    { "notify", NULL, 3, 3, do_notify },
 
337
    { "help", NULL, 0, INT_MAX, do_help },
 
338
    { NULL, NULL, 0, 0, NULL },
337
339
};
338
340
 
339
 
static struct command *
 
341
static struct ovs_cmdl_command *
340
342
get_all_commands(void)
341
343
{
342
344
    return all_commands;