Skip to content

Commit 244febb

Browse files
committed
testsuite: cover job-info lookup JSON_DECODE flag
Problem: There is no coverage for the new job info lookup JSON_DECODE flag. Add coverage to t2230-job-info-lookup.t and add a --json-decode flag to the utility test tool job-info/info_lookup.
1 parent 4cd265a commit 244febb

File tree

2 files changed

+75
-17
lines changed

2 files changed

+75
-17
lines changed

t/job-info/info_lookup.c

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,56 @@
1414
#include <unistd.h>
1515
#include <jansson.h>
1616
#include <stdio.h>
17+
#include <getopt.h>
1718
#include <flux/core.h>
1819

1920
#include "src/common/libutil/log.h"
21+
#include "ccan/str/str.h"
22+
23+
#define OPTIONS "j"
24+
static const struct option longopts[] = {
25+
{"json-decode", no_argument, 0, 'j'},
26+
{ 0, 0, 0, 0 },
27+
};
28+
29+
static void usage (void)
30+
{
31+
fprintf (stderr, "Usage: info_lookup [--json-decode] <jobid> <key> ...\n");
32+
exit (1);
33+
}
2034

2135
int main (int argc, char *argv[])
2236
{
2337
flux_t *h;
2438
flux_future_t *f;
2539
flux_jobid_t id;
2640
json_t *keys;
27-
int i;
41+
int i, ch;
42+
int flags = 0;
2843

29-
if (argc < 3) {
30-
fprintf (stderr, "Usage: info_lookup <jobid> <key> ...\n");
31-
exit (1);
44+
while ((ch = getopt_long (argc, argv, OPTIONS, longopts, NULL)) != -1) {
45+
switch (ch) {
46+
case 'j': /* --json-decode */
47+
flags |= FLUX_JOB_LOOKUP_JSON_DECODE;
48+
break;
49+
default:
50+
usage ();
51+
break;
52+
}
3253
}
54+
if ((argc - optind) < 2)
55+
usage ();
3356

3457
if (!(h = flux_open (NULL, 0)))
3558
log_err_exit ("flux_open");
3659

37-
if (flux_job_id_parse (argv[1], &id) < 0)
60+
if (flux_job_id_parse (argv[optind++], &id) < 0)
3861
log_msg_exit ("error parsing jobid: %s", argv[1]);
3962

4063
if (!(keys = json_array ()))
4164
log_msg_exit ("json_array");
4265

43-
for (i = 2; i < argc; i++) {
66+
for (i = optind; i < argc; i++) {
4467
json_t *key = json_string (argv[i]);
4568
if (!key)
4669
log_msg_exit ("json_string_value");
@@ -55,20 +78,39 @@ int main (int argc, char *argv[])
5578
"{s:I s:O s:i}",
5679
"id", id,
5780
"keys", keys,
58-
"flags", 0)))
81+
"flags", flags)))
5982
log_err_exit ("flux_rpc_pack");
6083

61-
for (i = 2; i < argc; i++) {
62-
json_t *value;
63-
char *s;
64-
if (flux_rpc_get_unpack (f, "{s:o}", argv[i], &value) < 0)
65-
log_msg_exit ("job-info.lookup: %s",
66-
future_strerror (f, errno));
67-
if (!(s = json_dumps (value, JSON_ENCODE_ANY)))
68-
log_msg_exit ("invalid json result");
69-
printf ("%s\n", s);
84+
for (i = optind; i < argc; i++) {
85+
if (flags & FLUX_JOB_LOOKUP_JSON_DECODE) {
86+
json_t *value;
87+
char *s;
88+
if (flux_rpc_get_unpack (f, "{s:o}", argv[i], &value) < 0)
89+
log_msg_exit ("job-info.lookup: %s",
90+
future_strerror (f, errno));
91+
if (streq (argv[i], "jobspec") || streq (argv[i], "R")) {
92+
if (!json_is_object (value))
93+
log_msg_exit ("job-info.lookup: key %s not an object",
94+
argv[i]);
95+
}
96+
else {
97+
if (!json_is_string (value))
98+
log_msg_exit ("job-info.lookup: key %s not a string",
99+
argv[i]);
100+
}
101+
if (!(s = json_dumps (value, JSON_ENCODE_ANY)))
102+
log_msg_exit ("invalid json result");
103+
printf ("%s\n", s);
104+
free (s);
105+
}
106+
else {
107+
char *s;
108+
if (flux_rpc_get_unpack (f, "{s:s}", argv[i], &s) < 0)
109+
log_msg_exit ("job-info.lookup: %s",
110+
future_strerror (f, errno));
111+
printf ("%s\n", s);
112+
}
70113
fflush (stdout);
71-
free (s);
72114
}
73115

74116
flux_future_destroy (f);

t/t2230-job-info-lookup.t

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,22 @@ test_expect_success 'job-info.lookup multiple keys fails on 1 bad entry' '
310310
test_must_fail ${INFOLOOKUP} $jobid eventlog jobspec J > all_info_b.out
311311
'
312312

313+
#
314+
# job info json decode tests
315+
#
316+
317+
test_expect_success 'job-info.lookup: decode non-json returns string' '
318+
jobid=$(submit_job) &&
319+
${INFOLOOKUP} --json-decode $jobid eventlog > info_decode_1.out &&
320+
grep submit info_decode_1.out
321+
'
322+
323+
test_expect_success 'job-info.lookup: decode json returns object' '
324+
jobid=$(submit_job) &&
325+
${INFOLOOKUP} --json-decode $jobid jobspec > info_decode_2.out &&
326+
grep sleep info_decode_2.out
327+
'
328+
313329
#
314330
# stats & corner cases
315331
#

0 commit comments

Comments
 (0)