Skip to content

Commit 6e82f81

Browse files
authored
Merge pull request #5846 from grondo/issue#5845
flux-job: fix `wait-event -m, --match-context`
2 parents ed07466 + 90ec0b2 commit 6e82f81

File tree

2 files changed

+60
-80
lines changed

2 files changed

+60
-80
lines changed

src/cmd/job/eventlog.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,30 +234,30 @@ struct wait_event_ctx {
234234
static bool wait_event_test_context (struct wait_event_ctx *ctx,
235235
json_t *context)
236236
{
237-
void *iter;
238-
bool match = false;
237+
const char *key;
238+
json_t *value;
239239

240-
iter = json_object_iter (context);
241-
while (iter && !match) {
242-
const char *key = json_object_iter_key (iter);
243-
json_t *value = json_object_iter_value (iter);
240+
json_object_foreach (context, key, value) {
244241
if (streq (key, ctx->context_key)) {
242+
bool match;
245243
char *str = json_dumps (value, JSON_ENCODE_ANY|JSON_COMPACT);
246-
if (streq (str, ctx->context_value))
247-
match = true;
244+
match = streq (str, ctx->context_value);
248245
free (str);
249-
}
250-
/* special case, json_dumps() will put quotes around string
251-
* values. Consider the case when user does not surround
252-
* string value with quotes */
253-
if (!match && json_is_string (value)) {
254-
const char *str = json_string_value (value);
255-
if (streq (str, ctx->context_value))
246+
247+
/* Also try json_string_value() when value is a string:
248+
*/
249+
if (!match
250+
&& json_is_string (value)
251+
&& streq (json_string_value (value), ctx->context_value))
256252
match = true;
253+
254+
/* Return immediately if a match was found:
255+
*/
256+
if (match)
257+
return true;
257258
}
258-
iter = json_object_iter_next (context, iter);
259259
}
260-
return match;
260+
return false;
261261
}
262262

263263
static bool wait_event_test (struct wait_event_ctx *ctx, json_t *event)

t/t2231-job-info-eventlog-watch.t

Lines changed: 43 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,24 @@ test_expect_success 'job-info: generate jobspec for simple test job' '
6868
flux run --dry-run -n1 -N1 sleep 300 > sleeplong.json
6969
'
7070

71+
test_expect_success 'submit and cancel job for test use' '
72+
JOBID=$(submit_job)
73+
'
74+
7175
test_expect_success 'flux job wait-event works' '
72-
jobid=$(submit_job) &&
73-
fj_wait_event $jobid submit > wait_event1.out &&
76+
fj_wait_event $JOBID submit > wait_event1.out &&
7477
grep submit wait_event1.out
7578
'
7679

7780
test_expect_success NO_CHAIN_LINT 'flux job wait-event errors on non-event' '
78-
jobid=$(submit_job) &&
79-
test_must_fail fj_wait_event $jobid foobar 2> wait_event2.err &&
81+
test_must_fail fj_wait_event $JOBID foobar 2> wait_event2.err &&
8082
grep "never received" wait_event2.err
8183
'
8284

8385
test_expect_success NO_CHAIN_LINT 'flux job wait-event does not see event after clean' '
84-
jobid=$(submit_job) &&
85-
kvsdir=$(flux job id --to=kvs $jobid) &&
86+
kvsdir=$(flux job id --to=kvs $JOBID) &&
8687
flux kvs eventlog append ${kvsdir}.eventlog foobar &&
87-
test_must_fail fj_wait_event -v $jobid foobar 2> wait_event3.err &&
88+
test_must_fail fj_wait_event -v $JOBID foobar 2> wait_event3.err &&
8889
grep "never received" wait_event3.err
8990
'
9091

@@ -93,22 +94,19 @@ test_expect_success 'flux job wait-event fails on bad id' '
9394
'
9495

9596
test_expect_success 'flux job wait-event --quiet works' '
96-
jobid=$(submit_job) &&
97-
fj_wait_event --quiet $jobid submit > wait_event4.out &&
97+
fj_wait_event --quiet $JOBID submit > wait_event4.out &&
9898
! test -s wait_event4.out
9999
'
100100

101101
test_expect_success 'flux job wait-event --verbose works' '
102-
jobid=$(submit_job) &&
103-
fj_wait_event --verbose $jobid clean > wait_event5.out &&
102+
fj_wait_event --verbose $JOBID clean > wait_event5.out &&
104103
grep submit wait_event5.out &&
105104
grep start wait_event5.out &&
106105
grep clean wait_event5.out
107106
'
108107

109108
test_expect_success 'flux job wait-event --verbose doesnt show events after wait event' '
110-
jobid=$(submit_job) &&
111-
fj_wait_event --verbose $jobid submit > wait_event6.out &&
109+
fj_wait_event --verbose $JOBID submit > wait_event6.out &&
112110
grep submit wait_event6.out &&
113111
! grep start wait_event6.out &&
114112
! grep clean wait_event6.out
@@ -122,140 +120,123 @@ test_expect_success 'flux job wait-event --timeout works' '
122120
'
123121

124122
test_expect_success 'flux job wait-event --format=json works' '
125-
jobid=$(submit_job) &&
126-
fj_wait_event --format=json $jobid submit > wait_event_format1.out &&
123+
fj_wait_event --format=json $JOBID submit > wait_event_format1.out &&
127124
grep -q "\"name\":\"submit\"" wait_event_format1.out &&
128125
grep -q "\"userid\":$(id -u)" wait_event_format1.out
129126
'
130127

131128
test_expect_success 'flux job wait-event --format=text works' '
132-
jobid=$(submit_job) &&
133-
fj_wait_event --format=text $jobid submit > wait_event_format2.out &&
129+
fj_wait_event --format=text $JOBID submit > wait_event_format2.out &&
134130
grep -q "submit" wait_event_format2.out &&
135131
grep -q "userid=$(id -u)" wait_event_format2.out
136132
'
137133

138134
test_expect_success 'flux job wait-event --format=invalid fails' '
139-
jobid=$(submit_job) &&
140-
test_must_fail fj_wait_event --format=invalid $jobid submit
135+
test_must_fail fj_wait_event --format=invalid $JOBID submit
141136
'
142137

143138
test_expect_success 'flux job wait-event --time-format=raw works' '
144-
jobid=$(submit_job) &&
145-
fj_wait_event --time-format=raw $jobid submit > wait_event_time_format1.out &&
139+
fj_wait_event --time-format=raw $JOBID submit > wait_event_time_format1.out &&
146140
get_timestamp_field submit wait_event_time_format1.out | grep "\."
147141
'
148142

149143
test_expect_success 'flux job wait-event --time-format=iso works' '
150-
jobid=$(submit_job) &&
151-
fj_wait_event --time-format=iso $jobid submit > wait_event_time_format2.out &&
144+
fj_wait_event --time-format=iso $JOBID submit > wait_event_time_format2.out &&
152145
get_timestamp_field submit wait_event_time_format2.out | grep T | grep Z
153146
'
154147

155148
test_expect_success 'flux job wait-event --time-format=offset works' '
156-
jobid=$(submit_job) &&
157-
fj_wait_event --time-format=offset $jobid submit > wait_event_time_format3A.out &&
149+
fj_wait_event --time-format=offset $JOBID submit > wait_event_time_format3A.out &&
158150
get_timestamp_field submit wait_event_time_format3A.out | grep "0.000000" &&
159-
fj_wait_event --time-format=offset $jobid exception > wait_event_time_format3B.out &&
151+
fj_wait_event --time-format=offset $JOBID exception > wait_event_time_format3B.out &&
160152
get_timestamp_field exception wait_event_time_format3B.out | grep -v "0.000000"
161153
'
162154

163155
test_expect_success 'flux job wait-event --time-format=invalid fails works' '
164-
jobid=$(submit_job) &&
165-
test_must_fail fj_wait_event --time-format=invalid $jobid submit
156+
test_must_fail fj_wait_event --time-format=invalid $JOBID submit
166157
'
167158

168159
test_expect_success 'flux job wait-event w/ match-context works (string w/ quotes)' '
169-
jobid=$(submit_job) &&
170-
fj_wait_event --match-context="type=\"cancel\"" $jobid exception > wait_event_context1.out &&
160+
fj_wait_event --match-context="type=\"cancel\"" $JOBID exception > wait_event_context1.out &&
171161
grep -q "exception" wait_event_context1.out &&
172162
grep -q "type=\"cancel\"" wait_event_context1.out
173163
'
174164

175165
test_expect_success 'flux job wait-event w/ match-context works (string w/o quotes)' '
176-
jobid=$(submit_job) &&
177-
fj_wait_event --match-context=type=cancel $jobid exception > wait_event_context2.out &&
166+
fj_wait_event --match-context=type=cancel $JOBID exception > wait_event_context2.out &&
178167
grep -q "exception" wait_event_context2.out &&
179168
grep -q "type=\"cancel\"" wait_event_context2.out
180169
'
181170

182171
test_expect_success 'flux job wait-event w/ match-context works (int)' '
183-
jobid=$(submit_job) &&
184-
fj_wait_event --match-context=flags=0 $jobid submit > wait_event_context3.out &&
172+
fj_wait_event --match-context=flags=0 $JOBID submit > wait_event_context3.out &&
185173
grep -q "submit" wait_event_context3.out &&
186174
grep -q "flags=0" wait_event_context3.out
187175
'
188176

189177
test_expect_success 'flux job wait-event w/ bad match-context fails (invalid key)' '
190-
jobid=$(submit_job) &&
191-
test_must_fail fj_wait_event --match-context=foo=bar $jobid exception
178+
test_must_fail fj_wait_event --match-context=foo=bar $JOBID exception
192179
'
193180

194181
test_expect_success 'flux job wait-event w/ bad match-context fails (invalid value)' '
195-
jobid=$(submit_job) &&
196-
test_must_fail fj_wait_event --match-context=type=foo $jobid exception
182+
test_must_fail fj_wait_event --match-context=type=foo $JOBID exception
183+
'
184+
185+
# Note: in test below, foo=0 would match severity=0 in buggy version
186+
test_expect_success 'flux job wait-event w/ bad match-context fails (issue #5845)' '
187+
test_must_fail fj_wait_event --match-context=foo=0 $JOBID exception
197188
'
198189

199190
test_expect_success 'flux job wait-event w/ bad match-context fails (invalid input)' '
200-
jobid=$(submit_job) &&
201-
test_must_fail fj_wait_event --match-context=foo $jobid exception
191+
test_must_fail fj_wait_event --match-context=foo $JOBID exception
202192
'
203193

204194
test_expect_success 'flux job wait-event -p works (eventlog)' '
205-
jobid=$(submit_job) &&
206-
fj_wait_event -p "eventlog" $jobid submit > wait_event_path1.out &&
195+
fj_wait_event -p "eventlog" $JOBID submit > wait_event_path1.out &&
207196
grep submit wait_event_path1.out
208197
'
209198

210199
test_expect_success 'flux job wait-event -p works (guest.exec.eventlog)' '
211-
jobid=$(submit_job) &&
212-
fj_wait_event -p "guest.exec.eventlog" $jobid done > wait_event_path2.out &&
200+
fj_wait_event -p "guest.exec.eventlog" $JOBID done > wait_event_path2.out &&
213201
grep done wait_event_path2.out
214202
'
215203
test_expect_success 'flux job wait-event -p works (exec)' '
216-
fj_wait_event -p exec $jobid done > wait_event_path2.1.out &&
204+
fj_wait_event -p exec $JOBID done > wait_event_path2.1.out &&
217205
grep done wait_event_path2.1.out
218206
'
219207
test_expect_success 'flux job wait-event -p works (non-guest eventlog)' '
220-
jobid=$(submit_job) &&
221-
kvsdir=$(flux job id --to=kvs $jobid) &&
208+
kvsdir=$(flux job id --to=kvs $JOBID) &&
222209
flux kvs eventlog append ${kvsdir}.foobar.eventlog foobar &&
223-
fj_wait_event -p "foobar.eventlog" $jobid foobar > wait_event_path3.out &&
210+
fj_wait_event -p "foobar.eventlog" $JOBID foobar > wait_event_path3.out &&
224211
grep foobar wait_event_path3.out
225212
'
226213

227214
test_expect_success 'flux job wait-event -p fails on invalid path' '
228-
jobid=$(submit_job) &&
229-
test_must_fail fj_wait_event -p "foobar" $jobid submit
215+
test_must_fail fj_wait_event -p "foobar" $JOBID submit
230216
'
231217

232218
test_expect_success 'flux job wait-event -p fails on invalid guest path' '
233-
jobid=$(submit_job) &&
234-
test_must_fail fj_wait_event -p "guest.foobar" $jobid submit
219+
test_must_fail fj_wait_event -p "guest.foobar" $JOBID submit
235220
'
236221

237222
test_expect_success 'flux job wait-event -p fails on path "guest."' '
238-
jobid=$(submit_job) &&
239-
test_must_fail fj_wait_event -p "guest." $jobid submit
223+
test_must_fail fj_wait_event -p "guest." $JOBID submit
240224
'
241225

242226
test_expect_success 'flux job wait-event -p and --waitcreate works (exec)' '
243-
jobid=$(submit_job) &&
244-
fj_wait_event --waitcreate -p exec $jobid done > wait_event_path4.out &&
227+
fj_wait_event --waitcreate -p exec $JOBID done > wait_event_path4.out &&
245228
grep done wait_event_path4.out
246229
'
247230

248231
test_expect_success 'flux job wait-event -p invalid and --waitcreate fails' '
249-
jobid=$(submit_job) &&
250-
test_must_fail fj_wait_event --waitcreate -p "guest.invalid" $jobid foobar 2> waitcreate1.err &&
232+
test_must_fail fj_wait_event --waitcreate -p "guest.invalid" $JOBID foobar 2> waitcreate1.err &&
251233
grep "not found" waitcreate1.err
252234
'
253235

254236
test_expect_success 'flux job wait-event -p hangs on non-guest eventlog' '
255-
jobid=$(submit_job) &&
256-
kvsdir=$(flux job id --to=kvs $jobid) &&
237+
kvsdir=$(flux job id --to=kvs $JOBID) &&
257238
flux kvs eventlog append ${kvsdir}.foobar.eventlog foo &&
258-
test_expect_code 142 run_timeout -s ALRM 0.2 flux job wait-event -p "foobar.eventlog" $jobid bar
239+
test_expect_code 142 run_timeout -s ALRM 0.2 flux job wait-event -p "foobar.eventlog" $JOBID bar
259240
'
260241

261242
test_expect_success NO_CHAIN_LINT 'flux job wait-event -p guest.exec.eventlog works (live job)' '
@@ -312,8 +293,7 @@ test_expect_success 'flux job wait-event --count=0 errors' '
312293
'
313294

314295
test_expect_success 'flux job wait-event --count=1 works' '
315-
jobid=$(submit_job) &&
316-
fj_wait_event --count=1 ${jobid} clean
296+
fj_wait_event --count=1 ${JOBID} clean
317297
'
318298

319299
test_expect_success 'flux job wait-event --count=2 works' '

0 commit comments

Comments
 (0)