2 // Created by jlr on 4/9/18.
11 #include <mon_internal.h>
13 #define JSON_DUMP_OPTS 0
15 static char *reconfigure()
17 MON_REQ *req = mon_req_new(NULL, MON_CMD_RECONFIGURE);
18 json_t *req_json = mon_req_encode(req);
19 char *result = json_dumps(req_json, JSON_DUMP_OPTS);
23 json_decref(req_json);
28 static char *show_plain()
30 MON_REQ *req = mon_req_new(NULL, MON_CMD_SHOW);
31 json_t *req_json = mon_req_encode(req);
32 char *result = json_dumps(req_json, JSON_DUMP_OPTS);
36 json_decref(req_json);
41 static char *show_options(const MON_OPT_TYPE *opts)
43 MON_REQ *req = mon_req_new(NULL, MON_CMD_SHOW);
44 json_t *req_json = NULL;
49 while (*opts != OPT_TYPE_UNKNOWN) {
50 assert(MON_SUCCESS == mon_req_add_option(req, *opts));
54 req_json = mon_req_encode(req);
57 result = json_dumps(req_json, JSON_DUMP_OPTS);
60 json_decref(req_json);
65 static char *read_file(const char *filename)
67 FILE *f = fopen(filename, "r");
70 ssize_t n = getline(&s, &nn, f);
73 if( (n > 0) && (s[n-1] == '\n'))
81 MON_OPT_TYPE opts[10];
82 char *expected = NULL;
84 // Test reconfigure command
86 expected = read_file("req_reconfigure.test");
88 assert(strcmp(expected, s) == 0);
92 // Test show without options
94 expected = read_file("req_show_no_options.test");
96 assert(strcmp(expected, s) == 0);
100 // Test show with empty options (this mostly tests the test)
101 opts[0] = OPT_TYPE_UNKNOWN;
102 s = show_options(opts);
103 expected = read_file("req_show_no_options.test");
105 assert(strcmp(expected, s) == 0);
109 // Test show with many options
110 opts[0] = OPT_TYPE_SHOW_SERIAL;
111 opts[1] = OPT_TYPE_SHOW_VERSION;
112 opts[2] = OPT_TYPE_SHOW_UPTIME;
113 opts[3] = OPT_TYPE_SHOW_TID_REQ_COUNT;
114 opts[4] = OPT_TYPE_SHOW_TID_REQ_PENDING;
115 opts[5] = OPT_TYPE_SHOW_ROUTES;
116 opts[6] = OPT_TYPE_SHOW_COMMUNITIES;
117 opts[7] = OPT_TYPE_UNKNOWN;
118 s = show_options(opts);
119 expected = read_file("req_show_all_options.test");
121 assert(strcmp(expected, s) == 0);