-
Notifications
You must be signed in to change notification settings - Fork 454
/
Copy pathjson-test-monitoring.c
638 lines (537 loc) · 19.9 KB
/
json-test-monitoring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/*
* Copyright 2018-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mongoc/mongoc-client-private.h"
#include "mongoc/mongoc-config.h"
#include "mongoc/mongoc-collection-private.h"
#include "mongoc/mongoc-host-list-private.h"
#include "mongoc/mongoc-server-description-private.h"
#include "mongoc/mongoc-topology-description-private.h"
#include "mongoc/mongoc-topology-private.h"
#include "mongoc/mongoc-util-private.h"
#include "mongoc/mongoc-util-private.h"
#include <bson/bson-json-private.h>
#include "TestSuite.h"
#include "test-conveniences.h"
#include "json-test.h"
#include "json-test-operations.h"
#include "test-libmongoc.h"
#ifdef _MSC_VER
#include <io.h>
#else
#include <dirent.h>
#endif
#ifdef BSON_HAVE_STRINGS_H
#include <strings.h>
#endif
static bool
ends_with (const char *s, const char *suffix)
{
size_t s_len;
size_t suffix_len;
if (!s) {
return false;
}
s_len = strlen (s);
suffix_len = strlen (suffix);
return s_len >= suffix_len && !strcmp (s + s_len - suffix_len, suffix);
}
/* test that an event's "host" field is set to a reasonable value */
static void
assert_host_in_uri (const mongoc_host_list_t *host, const mongoc_uri_t *uri)
{
const mongoc_host_list_t *hosts;
hosts = mongoc_uri_get_hosts (uri);
while (hosts) {
if (_mongoc_host_list_compare_one (hosts, host)) {
return;
}
hosts = hosts->next;
}
fprintf (stderr,
"Host \"%s\" not in \"%s\"",
host->host_and_port,
mongoc_uri_get_string (uri));
fflush (stderr);
abort ();
}
static void
started_cb (const mongoc_apm_command_started_t *event)
{
json_test_ctx_t *ctx =
(json_test_ctx_t *) mongoc_apm_command_started_get_context (event);
bson_t *events = &ctx->events;
char str[16];
const char *key;
bson_t *new_event;
if (ctx->verbose) {
char *cmd_json;
cmd_json = bson_as_canonical_extended_json (event->command, NULL);
printf ("%s\n", cmd_json);
fflush (stdout);
bson_free (cmd_json);
}
bson_mutex_lock (&ctx->mutex);
/* Track the last two lsid's */
if (bson_has_field (event->command, "lsid")) {
bson_t lsid;
/* Push on the circular queue */
bson_destroy (ctx->sent_lsids[0]);
ctx->sent_lsids[0] = ctx->sent_lsids[1];
bson_lookup_doc (event->command, "lsid", &lsid);
ctx->sent_lsids[1] = bson_copy (&lsid);
bson_destroy (&lsid);
}
BSON_ASSERT (mongoc_apm_command_started_get_request_id (event) > 0);
BSON_ASSERT (mongoc_apm_command_started_get_server_id (event) > 0);
/* check that event->host is sane */
assert_host_in_uri (event->host, ctx->test_framework_uri);
new_event = BCON_NEW ("command_started_event",
"{",
"command",
BCON_DOCUMENT (event->command),
"command_name",
BCON_UTF8 (event->command_name),
"database_name",
BCON_UTF8 (event->database_name),
"operation_id",
BCON_INT64 (event->operation_id),
"}");
bson_uint32_to_string (ctx->n_events, &key, str, sizeof str);
BSON_APPEND_DOCUMENT (events, key, new_event);
ctx->n_events++;
bson_destroy (new_event);
bson_mutex_unlock (&ctx->mutex);
}
static void
succeeded_cb (const mongoc_apm_command_succeeded_t *event)
{
json_test_ctx_t *ctx =
(json_test_ctx_t *) mongoc_apm_command_succeeded_get_context (event);
char str[16];
const char *key;
bson_t *new_event;
if (ctx->verbose) {
char *reply_json;
reply_json = bson_as_canonical_extended_json (event->reply, NULL);
MONGOC_DEBUG ("<-- COMMAND SUCCEEDED: %s\n", reply_json);
bson_free (reply_json);
}
if (ctx->config->command_started_events_only) {
return;
}
BSON_ASSERT (mongoc_apm_command_succeeded_get_request_id (event) > 0);
BSON_ASSERT (mongoc_apm_command_succeeded_get_server_id (event) > 0);
assert_host_in_uri (event->host, ctx->test_framework_uri);
new_event = BCON_NEW ("command_succeeded_event",
"{",
"reply",
BCON_DOCUMENT (event->reply),
"command_name",
BCON_UTF8 (event->command_name),
"operation_id",
BCON_INT64 (event->operation_id),
"}");
bson_mutex_lock (&ctx->mutex);
bson_uint32_to_string (ctx->n_events, &key, str, sizeof str);
BSON_APPEND_DOCUMENT (&ctx->events, key, new_event);
ctx->n_events++;
bson_destroy (new_event);
bson_mutex_unlock (&ctx->mutex);
}
static void
failed_cb (const mongoc_apm_command_failed_t *event)
{
json_test_ctx_t *ctx =
(json_test_ctx_t *) mongoc_apm_command_failed_get_context (event);
char str[16];
const char *key;
bson_t *new_event;
if (ctx->verbose) {
char *reply_json;
reply_json = bson_as_canonical_extended_json (event->reply, NULL);
MONGOC_DEBUG ("<-- %s COMMAND FAILED: %s\nREPLY: %s\n",
event->command_name,
event->error->message,
reply_json);
bson_free (reply_json);
}
if (ctx->config->command_started_events_only) {
return;
}
BSON_ASSERT (mongoc_apm_command_failed_get_request_id (event) > 0);
BSON_ASSERT (mongoc_apm_command_failed_get_server_id (event) > 0);
assert_host_in_uri (event->host, ctx->test_framework_uri);
new_event = BCON_NEW ("command_failed_event",
"{",
"command_name",
BCON_UTF8 (event->command_name),
"operation_id",
BCON_INT64 (event->operation_id),
"}");
bson_mutex_lock (&ctx->mutex);
bson_uint32_to_string (ctx->n_events, &key, str, sizeof str);
BSON_APPEND_DOCUMENT (&ctx->events, key, new_event);
ctx->n_events++;
bson_destroy (new_event);
bson_mutex_unlock (&ctx->mutex);
}
static void
server_changed_cb (const mongoc_apm_server_changed_t *event)
{
json_test_ctx_t *ctx;
const mongoc_server_description_t *sd;
const mongoc_server_description_t *prev_sd;
ctx = mongoc_apm_server_changed_get_context (event);
sd = mongoc_apm_server_changed_get_new_description (event);
prev_sd = mongoc_apm_server_changed_get_previous_description (event);
if (ctx->verbose) {
MONGOC_DEBUG ("SERVER CHANGED: (%s) %s --> %s\n",
sd->host.host_and_port,
mongoc_server_description_type (prev_sd),
mongoc_server_description_type (sd));
}
bson_mutex_lock (&ctx->mutex);
if (sd->type == MONGOC_SERVER_UNKNOWN) {
ctx->total_ServerMarkedUnknownEvent++;
}
if (sd->type == MONGOC_SERVER_RS_PRIMARY &&
!_mongoc_host_list_compare_one (&sd->host, &ctx->primary_host)) {
ctx->total_PrimaryChangedEvent++;
memcpy (&ctx->primary_host, &sd->host, sizeof (mongoc_host_list_t));
}
bson_mutex_unlock (&ctx->mutex);
}
void
set_apm_callbacks (json_test_ctx_t *ctx, mongoc_client_t *client)
{
mongoc_apm_callbacks_t *callbacks;
callbacks = mongoc_apm_callbacks_new ();
mongoc_apm_set_command_started_cb (callbacks, started_cb);
/* Even if test only checks command started events (i.e.
* command_started_events_only is set on test config), set callbacks for the
* benefit of logging. */
mongoc_apm_set_command_succeeded_cb (callbacks, succeeded_cb);
mongoc_apm_set_command_failed_cb (callbacks, failed_cb);
mongoc_client_set_apm_callbacks (ctx->client, callbacks, ctx);
mongoc_apm_callbacks_destroy (callbacks);
}
void
set_apm_callbacks_pooled (json_test_ctx_t *ctx, mongoc_client_pool_t *pool)
{
mongoc_apm_callbacks_t *callbacks;
callbacks = mongoc_apm_callbacks_new ();
mongoc_apm_set_command_started_cb (callbacks, started_cb);
mongoc_apm_set_server_changed_cb (callbacks, server_changed_cb);
mongoc_apm_set_command_succeeded_cb (callbacks, succeeded_cb);
mongoc_apm_set_command_failed_cb (callbacks, failed_cb);
mongoc_client_pool_set_apm_callbacks (pool, callbacks, ctx);
mongoc_apm_callbacks_destroy (callbacks);
}
static bool
lsids_match (const bson_t *a, const bson_t *b)
{
return match_bson (a, b, false);
}
typedef struct {
char *command_name;
int64_t cursor_id;
bson_t lsids[2];
} apm_match_visitor_ctx_t;
void
apm_match_visitor_ctx_reset (apm_match_visitor_ctx_t *ctx)
{
bson_free (ctx->command_name);
ctx->command_name = NULL;
}
static match_action_t
apm_match_visitor (match_ctx_t *ctx,
bson_iter_t *pattern_iter,
bson_iter_t *doc_iter)
{
const char *key = bson_iter_key (pattern_iter);
apm_match_visitor_ctx_t *visitor_ctx =
(apm_match_visitor_ctx_t *) ctx->visitor_ctx;
#define SHOULD_EXIST \
do { \
if (!doc_iter) { \
match_err (ctx, "expected %s", key); \
return MATCH_ACTION_ABORT; \
} \
} while (0)
#define IS_COMMAND(cmd) (ends_with (ctx->path, "command") && !strcmp (key, cmd))
if (ends_with (ctx->path, "command") && !visitor_ctx->command_name &&
doc_iter) {
visitor_ctx->command_name = bson_strdup (bson_iter_key (doc_iter));
}
if (IS_COMMAND ("find") || IS_COMMAND ("aggregate")) {
/* New query. Next server reply or getMore will set cursor_id. */
visitor_ctx->cursor_id = 0;
} else if (!strcmp (key, "id") && ends_with (ctx->path, "cursor")) {
visitor_ctx->cursor_id = bson_iter_as_int64 (doc_iter);
} else if (!strcmp (key, "errmsg")) {
/* "errmsg values of "" MUST assert that the value is not empty" */
const char *errmsg = bson_iter_utf8 (pattern_iter, NULL);
if (strcmp (errmsg, "") == 0) {
if (!doc_iter || bson_iter_type (doc_iter) != BSON_TYPE_UTF8 ||
strlen (bson_iter_utf8 (doc_iter, NULL)) == 0) {
match_err (ctx, "expected non-empty 'errmsg'");
return MATCH_ACTION_ABORT;
}
return MATCH_ACTION_SKIP;
}
} else if (IS_COMMAND ("getMore")) {
/* "When encountering a cursor or getMore value of "42" in a test, the
* driver MUST assert that the values are equal to each other and
* greater than zero."
*/
SHOULD_EXIST;
if (visitor_ctx->cursor_id == 0) {
/* A cursor id may not have been set in the visitor context if the spec
* test only checked command started events. Set the cursor_id now, so
* it can at least verify subsequent getMores use with the same id. */
visitor_ctx->cursor_id = bson_iter_as_int64 (doc_iter);
} else if (visitor_ctx->cursor_id != bson_iter_as_int64 (doc_iter)) {
match_err (ctx,
"cursor requested in getMore (%" PRId64
") does not match previously seen (%" PRId64 ")",
bson_iter_as_int64 (doc_iter),
visitor_ctx->cursor_id);
return MATCH_ACTION_ABORT;
}
} else if (!strcmp (key, "lsid")) {
const char *session_name = bson_iter_utf8 (pattern_iter, NULL);
bson_t lsid;
bool fail = false;
SHOULD_EXIST;
bson_iter_bson (doc_iter, &lsid);
/* Transactions tests: "Each command-started event in "expectations"
* includes an lsid with the value "session0" or "session1". Tests MUST
* assert that the command's actual lsid matches the id of the correct
* ClientSession named session0 or session1." */
if (!strcmp (session_name, "session0") &&
!lsids_match (&visitor_ctx->lsids[0], &lsid)) {
fail = true;
}
if (!strcmp (session_name, "session1") &&
!lsids_match (&visitor_ctx->lsids[1], &lsid)) {
fail = true;
}
if (fail) {
char *str = bson_as_json (&lsid, NULL);
match_err (
ctx, "expected %s, but used session: %s", session_name, str);
bson_free (str);
return MATCH_ACTION_ABORT;
} else {
return MATCH_ACTION_SKIP;
}
} else if (strstr (ctx->path, "updates.")) {
/* tests expect "multi: false" and "upsert: false" explicitly;
* we don't send them. fix when path is like "updates.0", "updates.1", ...
*/
if (!strcmp (key, "multi") && !bson_iter_bool (pattern_iter)) {
return MATCH_ACTION_SKIP;
}
if (!strcmp (key, "upsert") && !bson_iter_bool (pattern_iter)) {
return MATCH_ACTION_SKIP;
}
} else if (visitor_ctx->command_name &&
!strcmp (visitor_ctx->command_name, "findAndModify") &&
!strcmp (key, "new")) {
/* transaction tests expect "new: false" explicitly; we don't send it */
return MATCH_ACTION_SKIP;
}
return MATCH_ACTION_CONTINUE;
}
static void
_print_bson_array_as_json (FILE *out, const bson_t *arr)
{
bson_iter_t it;
for (bson_iter_init (&it, arr); bson_iter_next (&it);) {
bson_t elem;
bson_iter_bson (&it, &elem);
bson_json_opts_t opts = {0};
opts.initial_indent = "";
opts.level_indent = " ";
opts.subsequent_indent = " ";
opts.mode = BSON_JSON_MODE_CANONICAL;
opts.max_len = BSON_MAX_LEN_UNLIMITED;
char *str = bson_as_json_with_opts (&elem, NULL, &opts);
fprintf (out, " - %s\n", str);
bson_free (str);
}
}
static void
_apm_match_error_context (const bson_t *actual, const bson_t *expectations)
{
fprintf (stderr, "Error in APM matching.\nFull list of captured events:\n");
_print_bson_array_as_json (stderr, actual);
fprintf (stderr, "Expected events:\n");
_print_bson_array_as_json (stderr, expectations);
}
bool
skip_cse_list_collections (const bson_t *doc)
{
/* see CDRIVER-3856: Sharing a MongoClient for metadata lookup can lead to
* deadlock in drivers using automatic encryption. Since the C driver does
* not use a separate 'mongoc_client_t' for listCollections and finds on the
* key vault, we skip these checks. */
const char *val;
if (!bson_has_field (doc, "command_started_event.command.listCollections"))
return false;
if (!bson_has_field (doc, "command_started_event.command.$db"))
return false;
val = bson_lookup_utf8 (doc, "command_started_event.command.$db");
if (0 != strcmp (val, "keyvault"))
return false;
return true;
}
/*
*-----------------------------------------------------------------------
*
* check_json_apm_events --
*
* Compare actual APM events with expected sequence. The two docs
* are each like:
*
* [
* {
* "command_started_event": {
* "command": { ... },
* "command_name": "count",
* "database_name": "command-monitoring-tests",
* "operation_id": 123
* }
* },
* {
* "command_failed_event": {
* "command_name": "count",
* "operation_id": 123
* }
* }
* ]
*
* If @allow_subset is true, then expectations is allowed to be
* a subset of events.
*
*-----------------------------------------------------------------------
*/
void
check_json_apm_events (json_test_ctx_t *ctx, const bson_t *expectations)
{
bson_iter_t expectations_iter, actual_iter;
bool allow_subset;
match_ctx_t match_ctx = {{0}};
apm_match_visitor_ctx_t apm_match_visitor_ctx = {0};
int i;
for (i = 0; i < 2; i++) {
bson_copy_to (&ctx->lsids[i], &apm_match_visitor_ctx.lsids[i]);
}
/* Old mongod returns a double for "count", newer returns int32.
* Ignore this and other insignificant type differences. */
match_ctx.strict_numeric_types = false;
match_ctx.retain_dots_in_keys = true;
match_ctx.allow_placeholders = true;
match_ctx.visitor_fn = apm_match_visitor;
match_ctx.visitor_ctx = (void *) &apm_match_visitor_ctx;
allow_subset = ctx->config->command_monitoring_allow_subset;
BSON_ASSERT (bson_iter_init (&expectations_iter, expectations));
BSON_ASSERT (bson_iter_init (&actual_iter, &ctx->events));
/* Compare the captured actual events against the expectations. */
while (bson_iter_next (&expectations_iter)) {
bson_t expectation, actual;
bool matched = false;
bson_iter_bson (&expectations_iter, &expectation);
/* match against the current actual event, and possibly skip actual events
* if we allow subset matching. */
while (bson_iter_next (&actual_iter)) {
bson_iter_bson (&actual_iter, &actual);
matched = match_bson_with_ctx (&actual, &expectation, &match_ctx);
apm_match_visitor_ctx_reset (&apm_match_visitor_ctx);
bson_destroy (&actual);
if (matched) {
break;
}
if (allow_subset) {
/* if we allow matching only a subset of actual events, skip
* non-matching ones */
continue;
}
if (skip_cse_list_collections (&actual)) {
continue;
}
_apm_match_error_context (&ctx->events, expectations);
test_error ("could not match APM event\n"
"\texpected: %s\n\n"
"\tactual : %s\n\n"
"\terror : %s\n\n",
bson_as_canonical_extended_json (&expectation, NULL),
bson_as_canonical_extended_json (&actual, NULL),
match_ctx.errmsg);
}
if (!matched) {
_apm_match_error_context (&ctx->events, expectations);
test_error ("expectation unmatched\n"
"\texpected: %s\n\n",
bson_as_canonical_extended_json (&expectation, NULL));
}
bson_destroy (&expectation);
}
/* If we do not allow matching against a subset of actual events, check if
* there are extra "actual" events */
if (!allow_subset && bson_iter_next (&actual_iter)) {
bson_t extra;
bson_iter_bson (&actual_iter, &extra);
_apm_match_error_context (&ctx->events, expectations);
test_error ("extra actual event was not found in expectations: %s\n",
bson_as_canonical_extended_json (&extra, NULL));
}
for (i = 0; i < 2; i++) {
bson_destroy (&apm_match_visitor_ctx.lsids[i]);
}
}
/* Test that apm_match_visitor verifies the cursor id requested in a getMore
* is the same cursor id returned in a find reply. */
void
test_apm_matching (void)
{
apm_match_visitor_ctx_t match_visitor_ctx = {0};
match_ctx_t match_ctx = {{0}};
const char *e1 = "{"
" 'command_succeeded_event': {"
" 'command_name': 'find',"
" 'reply': {'cursor': { 'id': 123 }}"
" }"
"}";
const char *e2 = "{"
" 'command_started_event': {"
" 'command_name': 'getMore',"
" 'command': {'getMore': 124}"
" }"
"}";
match_ctx.visitor_fn = apm_match_visitor;
match_ctx.visitor_ctx = (void *) &match_visitor_ctx;
BSON_ASSERT (match_bson_with_ctx (tmp_bson (e1), tmp_bson (e1), &match_ctx));
BSON_ASSERT (
!match_bson_with_ctx (tmp_bson (e2), tmp_bson (e2), &match_ctx));
ASSERT_CONTAINS (match_ctx.errmsg, "cursor requested in getMore");
apm_match_visitor_ctx_reset (&match_visitor_ctx);
}
void
test_apm_install (TestSuite *suite)
{
TestSuite_Add (suite, "/apm_test_matching", test_apm_matching);
}