/
runner.c
1685 lines (1451 loc) · 52.4 KB
/
runner.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2020-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 "bsonutil/bson-parser.h"
#include "entity-map.h"
#include "json-test.h"
#include "operation.h"
#include "runner.h"
#include "test-conveniences.h"
#include "test-libmongoc.h"
#include "test-diagnostics.h"
#include "utlist.h"
#include "util.h"
typedef struct {
const char *file_description;
const char *test_description;
} skipped_unified_test_t;
#define SKIP_ALL_TESTS NULL
/* clang-format off */
skipped_unified_test_t SKIPPED_TESTS[] = {
/* CDRIVER-3630: libmongoc does not unconditionally raise an error when using
* hint option with an unacknowledged write concern */
{"unacknowledged-bulkWrite-delete-hint-clientError", "Unacknowledged bulkWrite deleteOne with hints fails with client-side error"},
{"unacknowledged-bulkWrite-delete-hint-clientError", "Unacknowledged bulkWrite deleteMany with hints fails with client-side error"},
{"unacknowledged-bulkWrite-update-hint-clientError", "Unacknowledged bulkWrite updateOne with hints fails with client-side error"},
{"unacknowledged-bulkWrite-update-hint-clientError", "Unacknowledged bulkWrite updateMany with hints fails with client-side error"},
{"unacknowledged-bulkWrite-update-hint-clientError", "Unacknowledged bulkWrite replaceOne with hints fails with client-side error"},
{"unacknowledged-deleteMany-hint-clientError", "Unacknowledged deleteMany with hint string fails with client-side error"},
{"unacknowledged-deleteMany-hint-clientError", "Unacknowledged deleteMany with hint document fails with client-side error"},
{"unacknowledged-deleteOne-hint-clientError", "Unacknowledged deleteOne with hint string fails with client-side error"},
{"unacknowledged-deleteOne-hint-clientError", "Unacknowledged deleteOne with hint document fails with client-side error"},
{"unacknowledged-findOneAndDelete-hint-clientError", "Unacknowledged findOneAndDelete with hint string fails with client-side error"},
{"unacknowledged-findOneAndDelete-hint-clientError", "Unacknowledged findOneAndDelete with hint document fails with client-side error"},
{"unacknowledged-findOneAndReplace-hint-clientError", "Unacknowledged findOneAndReplace with hint string fails with client-side error"},
{"unacknowledged-findOneAndReplace-hint-clientError", "Unacknowledged findOneAndReplace with hint document fails with client-side error"},
{"unacknowledged-findOneAndUpdate-hint-clientError", "Unacknowledged findOneAndUpdate with hint string fails with client-side error"},
{"unacknowledged-findOneAndUpdate-hint-clientError", "Unacknowledged findOneAndUpdate with hint document fails with client-side error"},
{"unacknowledged-replaceOne-hint-clientError", "Unacknowledged ReplaceOne with hint string fails with client-side error"},
{"unacknowledged-replaceOne-hint-clientError", "Unacknowledged ReplaceOne with hint document fails with client-side error"},
{"unacknowledged-updateMany-hint-clientError", "Unacknowledged updateMany with hint string fails with client-side error"},
{"unacknowledged-updateMany-hint-clientError", "Unacknowledged updateMany with hint document fails with client-side error"},
{"unacknowledged-updateOne-hint-clientError", "Unacknowledged updateOne with hint string fails with client-side error"},
{"unacknowledged-updateOne-hint-clientError", "Unacknowledged updateOne with hint document fails with client-side error"},
/* CDRIVER-4001, DRIVERS-1781, and DRIVERS-1448: 5.0 cursor behavior */
{"poc-command-monitoring", "A successful find event with a getmore and the server kills the cursor"},
/* CDRIVER-3867: drivers atlas testing (schema version 1.2) */
{"entity-client-storeEventsAsEntities", SKIP_ALL_TESTS},
/* libmongoc does not have a distinct helper, so skip snapshot tests testing particular distinct functionality */
{"snapshot-sessions", "Distinct operation with snapshot"},
{"snapshot-sessions", "Mixed operation with snapshot"},
/* CDRIVER-3886: serverless testing (schema version 1.4) */
{"poc-crud", SKIP_ALL_TESTS},
{"db-aggregate", SKIP_ALL_TESTS},
{"mongos-unpin", SKIP_ALL_TESTS},
/* CDRIVER-2871: CMAP is not implemented */
{"assertNumberConnectionsCheckedOut", SKIP_ALL_TESTS},
{"entity-client-cmap-events", SKIP_ALL_TESTS},
{"expectedEventsForClient-eventType", SKIP_ALL_TESTS},
/* CDRIVER-4115: listCollections does not support batchSize. */
{"cursors are correctly pinned to connections for load-balanced clusters", "listCollections pins the cursor to a connection"},
/* CDRIVER-4116: listIndexes does not support batchSize. */
{"cursors are correctly pinned to connections for load-balanced clusters", "listIndexes pins the cursor to a connection"},
/* libmongoc does not pin connections to cursors. It cannot force an error from waitQueueTimeoutMS by creating cursors in load balanced mode. */
{"wait queue timeout errors include details about checked out connections", SKIP_ALL_TESTS},
{0},
};
/* clang-format on */
static bool
is_test_file_skipped (test_file_t *test_file)
{
skipped_unified_test_t *skip;
for (skip = SKIPPED_TESTS; skip->file_description != NULL; skip++) {
if (!strcmp (skip->file_description, test_file->description) &&
skip->test_description == SKIP_ALL_TESTS) {
return true;
}
}
return false;
}
static bool
is_test_skipped (test_t *test)
{
skipped_unified_test_t *skip;
for (skip = SKIPPED_TESTS; skip->file_description != NULL; skip++) {
if (!strcmp (skip->file_description, test->test_file->description) &&
!strcmp (skip->test_description, test->description)) {
return true;
}
}
return false;
}
struct _failpoint_t {
char *client_id;
char *name;
uint32_t server_id;
struct _failpoint_t *next;
};
failpoint_t *
failpoint_new (char *name, char *client_id, uint32_t server_id)
{
failpoint_t *fp = (failpoint_t *) bson_malloc0 (sizeof (failpoint_t));
fp->name = bson_strdup (name);
fp->client_id = bson_strdup (client_id);
fp->server_id = server_id;
return fp;
}
void
failpoint_destroy (failpoint_t *fp)
{
if (!fp) {
return;
}
bson_free (fp->name);
bson_free (fp->client_id);
bson_free (fp);
}
/* Set server_id to 0 if the failpoint was not against a pinned mongos. */
void
register_failpoint (test_t *test,
char *name,
char *client_id,
uint32_t server_id)
{
failpoint_t *fp = NULL;
fp = failpoint_new (name, client_id, server_id);
LL_APPEND (test->failpoints, fp);
}
static bool
cleanup_failpoints (test_t *test, bson_error_t *error)
{
bool ret = false;
failpoint_t *iter = NULL;
mongoc_read_prefs_t *rp = NULL;
rp = mongoc_read_prefs_new (MONGOC_READ_PRIMARY_PREFERRED);
LL_FOREACH (test->failpoints, iter)
{
mongoc_client_t *client = NULL;
bson_t *disable_cmd = NULL;
client = entity_map_get_client (test->entity_map, iter->client_id, error);
if (!client) {
goto done;
}
disable_cmd =
tmp_bson ("{'configureFailPoint': '%s', 'mode': 'off' }", iter->name);
if (iter->server_id != 0) {
if (!mongoc_client_command_simple_with_server_id (client,
"admin",
disable_cmd,
rp,
iter->server_id,
NULL /* reply */,
error)) {
bson_destroy (disable_cmd);
goto done;
}
} else {
if (!mongoc_client_command_simple (
client, "admin", disable_cmd, rp, NULL /* reply */, error)) {
bson_destroy (disable_cmd);
goto done;
}
}
}
ret = true;
done:
mongoc_read_prefs_destroy (rp);
return ret;
}
static bool
test_has_operation (test_t *test, char *op_name)
{
bson_iter_t iter;
BSON_FOREACH (test->operations, iter)
{
bson_t op_bson;
bson_iter_bson (&iter, &op_bson);
if (0 == strcmp (bson_lookup_utf8 (&op_bson, "name"), op_name)) {
return true;
}
}
return false;
}
static const char *
get_topology_type (mongoc_client_t *client);
static bool
is_topology_type_sharded (const char *topology_type)
{
return 0 == strcmp ("sharded", topology_type) ||
0 == strcmp ("sharded-replicaset", topology_type);
}
static bool
is_topology_type_compatible (const char *test_topology_type,
const char *server_topology_type)
{
if (0 == strcmp (test_topology_type, server_topology_type)) {
return true;
}
/* If a requirement specifies a "sharded" topology and server is of type
* "sharded-replicaset", that is also compatible. */
return 0 == strcmp (test_topology_type, "sharded") &&
is_topology_type_sharded (server_topology_type);
}
/* This callback tracks the set of server IDs for all connected servers.
* The set of server IDs is used when sending a command to each individual
* server.
*/
static void
on_topology_changed (const mongoc_apm_topology_changed_t *event)
{
test_runner_t *test_runner = NULL;
const mongoc_topology_description_t *td;
mongoc_server_description_t **sds;
size_t sds_len;
size_t i;
test_runner =
(test_runner_t *) mongoc_apm_topology_changed_get_context (event);
_mongoc_array_clear (&test_runner->server_ids);
td = mongoc_apm_topology_changed_get_new_description (event);
sds = mongoc_topology_description_get_servers (td, &sds_len);
for (i = 0; i < sds_len; i++) {
uint32_t server_id = mongoc_server_description_id (sds[i]);
MONGOC_DEBUG ("topology changed, adding server id: %d", (int) server_id);
_mongoc_array_append_val (&test_runner->server_ids, server_id);
}
mongoc_server_descriptions_destroy_all (sds, sds_len);
}
/* Returns an array of all known servers IDs that the test runner
* is connected to. The server IDs can be used to target commands to
* specific servers with mongoc_client_command_simple_with_server_id().
*/
static void
test_runner_get_all_server_ids (test_runner_t *test_runner, mongoc_array_t *out)
{
bson_error_t error;
bool ret;
/* Run a 'ping' command to make sure topology has been scanned. */
ret = mongoc_client_command_simple (test_runner->internal_client,
"admin",
tmp_bson ("{'ping': 1}"),
NULL /* read prefs */,
NULL /* reply */,
NULL /* error */);
ASSERT_OR_PRINT (ret, error);
_mongoc_array_copy (out, &test_runner->server_ids);
}
/* Run killAllSessions against the primary or each mongos to terminate any
* lingering open transactions.
* See also: Spec section "Terminating Open Transactions"
*/
static bool
test_runner_terminate_open_transactions (test_runner_t *test_runner,
bson_error_t *error)
{
bson_t *kill_all_sessions_cmd = NULL;
bool ret = false;
bool cmd_ret = false;
bson_error_t cmd_error = {0};
if (0 == test_framework_skip_if_no_txns ()) {
ret = true;
goto done;
}
kill_all_sessions_cmd = tmp_bson ("{'killAllSessions': []}");
/* Run on each mongos. Target each server individually. */
if (is_topology_type_sharded (test_runner->topology_type)) {
mongoc_array_t server_ids;
size_t i;
_mongoc_array_init (&server_ids, sizeof (uint32_t));
test_runner_get_all_server_ids (test_runner, &server_ids);
for (i = 0; i < server_ids.len; i++) {
uint32_t server_id = _mongoc_array_index (&server_ids, uint32_t, i);
cmd_ret = mongoc_client_command_simple_with_server_id (
test_runner->internal_client,
"admin",
kill_all_sessions_cmd,
NULL /* read prefs. */,
server_id,
NULL,
&cmd_error);
/* Ignore error code 11601 as a workaround for SERVER-38335. */
if (!cmd_ret && cmd_error.code != 11601) {
test_set_error (
error,
"Unexpected error running killAllSessions on server (%d): %s",
(int) server_id,
cmd_error.message);
goto done;
}
}
_mongoc_array_destroy (&server_ids);
} else {
/* Run on primary. */
cmd_ret = mongoc_client_command_simple (test_runner->internal_client,
"admin",
kill_all_sessions_cmd,
NULL /* read prefs. */,
NULL,
&cmd_error);
/* Ignore error code 11601 as a workaround for SERVER-38335. */
if (!cmd_ret && cmd_error.code != 11601) {
test_set_error (
error,
"Unexpected error running killAllSessions on primary: %s",
cmd_error.message);
goto done;
}
}
ret = true;
done:
return ret;
}
static test_runner_t *
test_runner_new (void)
{
test_runner_t *test_runner = NULL;
mongoc_apm_callbacks_t *callbacks = NULL;
mongoc_uri_t *uri = NULL;
bson_error_t error;
bson_t reply;
test_runner = bson_malloc0 (sizeof (test_runner_t));
/* Create a client for internal test operations (e.g. checking server
* version) */
_mongoc_array_init (&test_runner->server_ids, sizeof (uint32_t));
callbacks = mongoc_apm_callbacks_new ();
mongoc_apm_set_topology_changed_cb (callbacks, on_topology_changed);
uri = test_framework_get_uri ();
/* In load balanced mode, the internal client must use the SINGLE_LB_MONGOS_URI. */
if (!test_framework_is_loadbalanced ()) {
/* Always use multiple mongoses if speaking to a mongos.
* Some test operations require communicating with all known mongos */
if (!test_framework_uri_apply_multi_mongos (uri, true, &error)) {
test_error ("error applying multiple mongos: %s", error.message);
}
}
test_runner->internal_client =
test_framework_client_new_from_uri (uri, NULL);
test_framework_set_ssl_opts (test_runner->internal_client);
mongoc_uri_destroy (uri);
mongoc_client_set_apm_callbacks (
test_runner->internal_client, callbacks, test_runner);
mongoc_client_set_error_api (test_runner->internal_client,
MONGOC_ERROR_API_VERSION_2);
test_runner->topology_type =
get_topology_type (test_runner->internal_client);
server_semver (test_runner->internal_client, &test_runner->server_version);
test_runner->is_serverless = test_framework_is_serverless ();
/* Terminate any possible open transactions. */
if (!test_runner_terminate_open_transactions (test_runner, &error)) {
test_error ("error terminating transactions: %s", error.message);
}
mongoc_apm_callbacks_destroy (callbacks);
/* Cache server parameters to check runOnRequirements. */
if (!mongoc_client_command_simple (test_runner->internal_client,
"admin",
tmp_bson ("{'getParameter': '*'}"),
NULL,
&reply,
&error)) {
test_error ("error getting server parameters: %s, full reply: %s",
error.message,
tmp_json (&reply));
}
test_runner->server_parameters = bson_copy (&reply);
bson_destroy (&reply);
return test_runner;
}
static void
test_runner_destroy (test_runner_t *test_runner)
{
mongoc_client_destroy (test_runner->internal_client);
_mongoc_array_destroy (&test_runner->server_ids);
bson_destroy (test_runner->server_parameters);
bson_free (test_runner);
}
static test_file_t *
test_file_new (test_runner_t *test_runner, bson_t *bson)
{
test_file_t *test_file = NULL;
bson_parser_t *parser = NULL;
char *schema_version = NULL;
test_file = bson_malloc0 (sizeof (test_file_t));
test_file->test_runner = test_runner;
parser = bson_parser_new ();
bson_parser_utf8 (parser, "description", &test_file->description);
bson_parser_utf8 (parser, "schemaVersion", &schema_version);
bson_parser_array_optional (
parser, "runOnRequirements", &test_file->run_on_requirements);
bson_parser_array_optional (
parser, "createEntities", &test_file->create_entities);
bson_parser_array_optional (parser, "initialData", &test_file->initial_data);
bson_parser_doc_optional (parser, "_yamlAnchors", &test_file->yaml_anchors);
bson_parser_array (parser, "tests", &test_file->tests);
bson_parser_parse_or_assert (parser, bson);
bson_parser_destroy (parser);
semver_parse (schema_version, &test_file->schema_version);
bson_free (schema_version);
return test_file;
}
static void
test_file_destroy (test_file_t *test_file)
{
bson_free (test_file->description);
bson_destroy (test_file->tests);
bson_destroy (test_file->initial_data);
bson_destroy (test_file->create_entities);
bson_destroy (test_file->run_on_requirements);
bson_destroy (test_file->yaml_anchors);
bson_free (test_file);
}
static test_t *
test_new (test_file_t *test_file, bson_t *bson)
{
test_t *test = NULL;
bson_parser_t *parser = NULL;
test = bson_malloc0 (sizeof (test_t));
test->test_file = test_file;
parser = bson_parser_new ();
bson_parser_utf8 (parser, "description", &test->description);
bson_parser_array_optional (
parser, "runOnRequirements", &test->run_on_requirements);
bson_parser_utf8_optional (parser, "skipReason", &test->skip_reason);
bson_parser_array (parser, "operations", &test->operations);
bson_parser_array_optional (parser, "expectEvents", &test->expect_events);
bson_parser_array_optional (parser, "outcome", &test->outcome);
bson_parser_parse_or_assert (parser, bson);
bson_parser_destroy (parser);
test->entity_map = entity_map_new ();
return test;
}
static void
test_destroy (test_t *test)
{
failpoint_t *fpiter, *fptmp;
LL_FOREACH_SAFE (test->failpoints, fpiter, fptmp)
{
failpoint_destroy (fpiter);
}
entity_map_destroy (test->entity_map);
bson_destroy (test->outcome);
bson_destroy (test->expect_events);
bson_destroy (test->operations);
bson_destroy (test->run_on_requirements);
bson_free (test->description);
bson_free (test->skip_reason);
bson_free (test);
}
static bool
is_replset (bson_t *hello_reply)
{
if (bson_has_field (hello_reply, "setName")) {
return true;
}
if (bson_has_field (hello_reply, "isreplicaset") &&
bson_lookup_bool (hello_reply, "isreplicaset") == true) {
return true;
}
return false;
}
static bool
is_sharded (bson_t *hello_reply)
{
const char *val;
if (!bson_has_field (hello_reply, "msg")) {
return false;
}
val = bson_lookup_utf8 (hello_reply, "msg");
if (0 == strcmp (val, "isdbgrid")) {
return true;
}
return false;
}
static const char *
get_topology_type (mongoc_client_t *client)
{
bool ret;
bson_t reply;
bson_error_t error;
const char *topology_type = "single";
if (test_framework_is_loadbalanced ()) {
return "load-balanced";
}
ret = mongoc_client_command_simple (
client, "admin", tmp_bson ("{'hello': 1}"), NULL, &reply, &error);
if (!ret) {
bson_destroy (&reply);
ret = mongoc_client_command_simple (
client,
"admin",
tmp_bson ("{'" HANDSHAKE_CMD_LEGACY_HELLO "': 1}"),
NULL,
&reply,
&error);
}
ASSERT_OR_PRINT (ret, error);
if (is_replset (&reply)) {
topology_type = "replicaset";
} else if (is_sharded (&reply)) {
bool is_sharded_replset;
mongoc_collection_t *config_shards = NULL;
mongoc_cursor_t *cursor = NULL;
const bson_t *shard_doc;
/* Check if this is a sharded-replicaset by querying the config.shards
* collection. */
is_sharded_replset = true;
config_shards = mongoc_client_get_collection (client, "config", "shards");
cursor = mongoc_collection_find_with_opts (config_shards,
tmp_bson ("{}"),
NULL /* opts */,
NULL /* read prefs */);
if (mongoc_cursor_error (cursor, &error)) {
test_error ("Attempting to query config.shards collection failed: %s",
error.message);
}
while (mongoc_cursor_next (cursor, &shard_doc)) {
const char *host = bson_lookup_utf8 (shard_doc, "host");
if (NULL == strstr (host, "/")) {
is_sharded_replset = false;
break;
}
}
mongoc_cursor_destroy (cursor);
mongoc_collection_destroy (config_shards);
if (is_sharded_replset) {
topology_type = "sharded-replicaset";
} else {
topology_type = "sharded";
}
}
bson_destroy (&reply);
return topology_type;
}
static void
check_schema_version (test_file_t *test_file)
{
const char *supported_version_strs[] = {"1.5"};
int i;
for (i = 0; i < sizeof (supported_version_strs) /
sizeof (supported_version_strs[0]);
i++) {
semver_t supported_version;
semver_parse (supported_version_strs[i], &supported_version);
if (supported_version.major != test_file->schema_version.major) {
continue;
}
if (!supported_version.has_minor) {
/* All minor versions for this major version are supported. */
return;
}
if (supported_version.minor >= test_file->schema_version.minor) {
return;
}
}
test_error ("Unsupported schema version: %s",
semver_to_string (&test_file->schema_version));
}
static bool
check_run_on_requirement (test_runner_t *test_runner,
bson_t *run_on_requirement,
const char *server_topology_type,
semver_t *server_version,
char **fail_reason)
{
bson_iter_t req_iter;
BSON_FOREACH (run_on_requirement, req_iter)
{
const char *key = bson_iter_key (&req_iter);
if (0 == strcmp (key, "minServerVersion")) {
semver_t min_server_version;
semver_parse (bson_iter_utf8 (&req_iter, NULL), &min_server_version);
if (semver_cmp (server_version, &min_server_version) < 0) {
*fail_reason = bson_strdup_printf (
"Server version(%s) is lower than minServerVersion(%s)",
semver_to_string (server_version),
semver_to_string (&min_server_version));
return false;
}
continue;
}
if (0 == strcmp (key, "maxServerVersion")) {
semver_t max_server_version;
semver_parse (bson_iter_utf8 (&req_iter, NULL), &max_server_version);
if (semver_cmp (server_version, &max_server_version) > 0) {
*fail_reason = bson_strdup_printf (
"Server version(%s) is higher than maxServerVersion (%s)",
semver_to_string (server_version),
semver_to_string (&max_server_version));
return false;
}
continue;
}
if (0 == strcmp (key, "topologies")) {
bool found = false;
bson_t topologies;
bson_iter_t topology_iter;
bson_iter_bson (&req_iter, &topologies);
BSON_FOREACH (&topologies, topology_iter)
{
const char *test_topology_type =
bson_iter_utf8 (&topology_iter, NULL);
if (is_topology_type_compatible (test_topology_type,
server_topology_type)) {
found = true;
continue;
}
}
if (!found) {
*fail_reason = bson_strdup_printf (
"Topology (%s) was not found among listed topologies: %s",
server_topology_type,
tmp_json (&topologies));
return false;
}
continue;
}
if (0 == strcmp (key, "serverParameters")) {
bson_t expected_params;
bson_val_t *actual_val = NULL;
bson_val_t *expected_val = NULL;
bool matched;
bson_error_t error = {0};
bson_iter_bson (&req_iter, &expected_params);
expected_val = bson_val_from_bson (&expected_params);
actual_val = bson_val_from_bson (test_runner->server_parameters);
matched = bson_match (expected_val, actual_val, false, &error);
bson_val_destroy (actual_val);
bson_val_destroy (expected_val);
if (!matched) {
*fail_reason = bson_strdup_printf ("serverParameters mismatch: %s",
error.message);
return false;
}
continue;
}
if (0 == strcmp (key, "serverless")) {
const char *serverless_mode = bson_iter_utf8 (&req_iter, NULL);
if (0 == strcmp (serverless_mode, "allow")) {
continue;
} else if (0 == strcmp (serverless_mode, "require")) {
if (!test_runner->is_serverless) {
*fail_reason =
bson_strdup_printf ("Not running in serverless mode");
return false;
}
continue;
} else if (0 == strcmp (serverless_mode, "forbid")) {
if (test_runner->is_serverless) {
*fail_reason = bson_strdup_printf ("Running in serverless mode");
return false;
}
continue;
} else {
test_error ("Unexpected serverless mode: %s", serverless_mode);
}
continue;
}
if (0 == strcmp (key, "auth")) {
bool auth_requirement = bson_iter_bool (&req_iter);
if (auth_requirement == test_framework_has_auth ()) {
continue;
}
*fail_reason = bson_strdup_printf (
"Server does not match auth requirement, test %s authentication.",
auth_requirement ? "requires" : "forbids");
return false;
}
test_error ("Unexpected runOnRequirement field: %s", key);
}
return true;
}
static bool
check_run_on_requirements (test_runner_t *test_runner,
bson_t *run_on_requirements,
const char **reason)
{
bson_string_t *fail_reasons = NULL;
bool requirements_satisfied = false;
bson_iter_t iter;
fail_reasons = bson_string_new ("");
BSON_FOREACH (run_on_requirements, iter)
{
bson_t run_on_requirement;
char *fail_reason = NULL;
bson_iter_bson (&iter, &run_on_requirement);
fail_reason = NULL;
if (check_run_on_requirement (test_runner,
&run_on_requirement,
test_runner->topology_type,
&test_runner->server_version,
&fail_reason)) {
requirements_satisfied = true;
break;
}
bson_string_append_printf (fail_reasons,
"- Requirement %s failed because: %s\n",
bson_iter_key (&iter),
fail_reason);
bson_free (fail_reason);
}
*reason = NULL;
if (!requirements_satisfied) {
(*reason) =
tmp_str ("runOnRequirements not satified:\n%s", fail_reasons->str);
}
bson_string_free (fail_reasons, true);
return requirements_satisfied;
}
static bool
test_setup_initial_data (test_t *test, bson_error_t *error)
{
test_runner_t *test_runner = NULL;
test_file_t *test_file = NULL;
bson_iter_t initial_data_iter;
test_file = test->test_file;
test_runner = test_file->test_runner;
if (!test_file->initial_data) {
return true;
}
BSON_FOREACH (test_file->initial_data, initial_data_iter)
{
bson_parser_t *parser = NULL;
bson_t collection_data;
char *collection_name = NULL;
char *database_name = NULL;
bson_t *documents = NULL;
mongoc_database_t *db = NULL;
mongoc_collection_t *coll = NULL;
mongoc_bulk_operation_t *bulk_insert = NULL;
mongoc_write_concern_t *wc = NULL;
bson_t *bulk_opts = NULL;
bson_t *drop_opts = NULL;
bson_t *create_opts = NULL;
bool ret = false;
bson_iter_bson (&initial_data_iter, &collection_data);
parser = bson_parser_new ();
bson_parser_utf8 (parser, "databaseName", &database_name);
bson_parser_utf8 (parser, "collectionName", &collection_name);
bson_parser_array (parser, "documents", &documents);
if (!bson_parser_parse (parser, &collection_data, error)) {
goto loopexit;
}
wc = mongoc_write_concern_new ();
mongoc_write_concern_set_w (wc, MONGOC_WRITE_CONCERN_W_MAJORITY);
bulk_opts = bson_new ();
mongoc_write_concern_append (wc, bulk_opts);
/* Drop the collection. */
/* Check if the server supports majority write concern on 'drop' and
* 'create'. */
if (semver_cmp_str (&test_runner->server_version, "3.4") >= 0) {
drop_opts = bson_new ();
mongoc_write_concern_append (wc, drop_opts);
create_opts = bson_new ();
mongoc_write_concern_append (wc, create_opts);
}
coll = mongoc_client_get_collection (
test_runner->internal_client, database_name, collection_name);
if (!mongoc_collection_drop_with_opts (coll, drop_opts, error)) {
if (error->code != 26 &&
(NULL == strstr (error->message, "ns not found"))) {
/* This is not a "ns not found" error. Fail the test. */
goto loopexit;
}
/* Clear an "ns not found" error. */
memset (error, 0, sizeof (bson_error_t));
}
/* Insert documents if specified. */
if (bson_count_keys (documents) > 0) {
bson_iter_t documents_iter;
bulk_insert =
mongoc_collection_create_bulk_operation_with_opts (coll, bulk_opts);
BSON_FOREACH (documents, documents_iter)
{
bson_t document;
bson_iter_bson (&documents_iter, &document);
mongoc_bulk_operation_insert (bulk_insert, &document);
}
if (!mongoc_bulk_operation_execute (bulk_insert, NULL, error)) {
goto loopexit;
}
} else {
mongoc_collection_t *new_coll = NULL;
/* Test does not need data inserted, just create the collection. */
db = mongoc_client_get_database (test_runner->internal_client,
database_name);
new_coll = mongoc_database_create_collection (
db, collection_name, create_opts, error);
if (!new_coll) {
goto loopexit;
}
mongoc_collection_destroy (new_coll);
}
ret = true;
loopexit:
mongoc_bulk_operation_destroy (bulk_insert);
bson_destroy (bulk_opts);
bson_destroy (drop_opts);
bson_destroy (create_opts);
bson_destroy (documents);
mongoc_write_concern_destroy (wc);
mongoc_collection_destroy (coll);
bson_free (database_name);
bson_free (collection_name);
bson_parser_destroy (parser);
mongoc_database_destroy (db);
if (!ret) {
return false;
}
}
return true;
}
static bool
test_create_entities (test_t *test, bson_error_t *error)
{
test_file_t *test_file = NULL;
bson_iter_t iter;
test_file = test->test_file;
if (!test_file->create_entities) {
return true;
}
/* If a test runs a 'configureFailPoint' operation, reduce heartbeat on new
* clients. */
if (test_has_operation (test, "configureFailPoint")) {
entity_map_set_reduced_heartbeat (test->entity_map, true);
}
BSON_FOREACH (test_file->create_entities, iter)
{
bson_t entity_bson;
bson_iter_bson (&iter, &entity_bson);
if (!entity_map_create (test->entity_map, &entity_bson, error)) {
return false;
}
}
return true;
}
static bool
test_run_operations (test_t *test, bson_error_t *error)
{
bool ret = false;
bson_iter_t iter;
int i = 0;
BSON_FOREACH (test->operations, iter)
{
bson_t op_bson;
bson_iter_bson (&iter, &op_bson);
if (!operation_run (test, &op_bson, error)) {
test_diagnostics_error_info ("running operation: %s",
tmp_json (&op_bson));
goto done;
}
i++;
}
ret = true;
done:
return ret;
}
static bool
test_check_event (test_t *test,
bson_t *expected,
event_t *actual,
bson_error_t *error)