Skip to content

Commit 464fcda

Browse files
CDRIVER-4076 skip legacy opcode / getLastError tests on pre 5.1 (#833)
- This also addresses CDRIVER-4092 by fixing tests that send OP_GET_MORE and legacy OP_QUERY Co-authored-by: vector-of-bool <[email protected]>
1 parent 723b2ff commit 464fcda

File tree

6 files changed

+122
-25
lines changed

6 files changed

+122
-25
lines changed

src/libmongoc/tests/test-libmongoc.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,6 +2484,8 @@ WIRE_VERSION_CHECKS (8)
24842484
WIRE_VERSION_CHECKS (9)
24852485
/* wire versions 10, 11, 12 were internal to the 5.0 release cycle */
24862486
WIRE_VERSION_CHECKS (13)
2487+
/* wire version 14 begins with the 5.1 prerelease. */
2488+
WIRE_VERSION_CHECKS (14)
24872489

24882490
int
24892491
test_framework_skip_if_no_dual_ip_hostname (void)
@@ -2923,3 +2925,50 @@ main (int argc, char *argv[])
29232925

29242926
return ret;
29252927
}
2928+
2929+
/*
2930+
* test_framework_skip_if_no_legacy_opcodes returns 0 if the connected server
2931+
* does not support legacy wire protocol op codes.
2932+
*
2933+
* As of SERVER-57457 and SERVER-57391, the following legacy wire protocol
2934+
* op codes have been removed in the server 5.1:
2935+
* - OP_KILL_CURSORS
2936+
* - OP_INSERT
2937+
* - OP_UPDATE
2938+
* - OP_DELETE
2939+
* - OP_GET_MORE
2940+
* - OP_QUERY (for any command other than isMaster, which drivers use to
2941+
* initially discover the min/max wire version of a server)
2942+
*/
2943+
bool
2944+
test_framework_supports_legacy_opcodes (void) {
2945+
/* Wire v14+ removed legacy opcodes */
2946+
return test_framework_skip_if_max_wire_version_less_than_14() == 0;
2947+
}
2948+
2949+
int
2950+
test_framework_skip_if_no_legacy_opcodes (void) {
2951+
if (!TestSuite_CheckLive()) {
2952+
return 0;
2953+
}
2954+
2955+
if (test_framework_supports_legacy_opcodes ()) {
2956+
return 1;
2957+
}
2958+
2959+
return 0;
2960+
}
2961+
2962+
/* SERVER-57390 removed the getLastError command on 5.1 servers. */
2963+
int
2964+
test_framework_skip_if_no_getlasterror (void) {
2965+
if (!TestSuite_CheckLive()) {
2966+
return 0;
2967+
}
2968+
2969+
if (test_framework_supports_legacy_opcodes ()) {
2970+
return 1;
2971+
}
2972+
2973+
return 0;
2974+
}

src/libmongoc/tests/test-libmongoc.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ WIRE_VERSION_CHECK_DECLS (8)
192192
WIRE_VERSION_CHECK_DECLS (9)
193193
/* wire versions 10, 11, 12 were internal to the 5.0 release cycle */
194194
WIRE_VERSION_CHECK_DECLS (13)
195+
/* wire version 14 begins with the 5.1 prerelease. */
196+
WIRE_VERSION_CHECK_DECLS (14)
195197

196198
#undef WIRE_VERSION_CHECK_DECLS
197199

@@ -242,4 +244,16 @@ test_framework_skip_if_no_aws (void);
242244
int
243245
test_framework_skip_if_no_setenv (void);
244246

247+
bool
248+
test_framework_supports_legacy_opcodes (void);
249+
250+
int
251+
test_framework_skip_if_no_legacy_opcodes (void);
252+
253+
int
254+
test_framework_skip_if_no_getlasterror (void);
255+
256+
int
257+
test_framework_skip_if_no_exhaust_cursors (void);
258+
245259
#endif

src/libmongoc/tests/test-mongoc-bulk.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4026,7 +4026,7 @@ typedef enum { HANGUP, SERVER_ERROR, ERR_TYPE_LAST } err_type_t;
40264026

40274027

40284028
static void
4029-
test_bulk_write_concern_split (void)
4029+
test_bulk_write_concern_split (void* unused)
40304030
{
40314031
mongoc_client_t *client;
40324032
mongoc_bulk_operation_t *bulk;
@@ -5012,9 +5012,12 @@ test_bulk_install (TestSuite *suite)
50125012
TestSuite_AddLive (
50135013
suite, "/BulkOperation/OP_MSG/max_msg_size", test_bulk_max_msg_size);
50145014
TestSuite_AddLive (suite, "/BulkOperation/split", test_bulk_split);
5015-
TestSuite_AddLive (suite,
5015+
TestSuite_AddFull (suite,
50165016
"/BulkOperation/write_concern/split",
5017-
test_bulk_write_concern_split);
5017+
test_bulk_write_concern_split,
5018+
NULL /* dtor */,
5019+
NULL /* ctx */,
5020+
test_framework_skip_if_no_getlasterror);
50185021
TestSuite_AddMockServerTest (suite,
50195022
"/BulkOperation/hint/single/command/secondary",
50205023
test_hint_single_command_secondary);

src/libmongoc/tests/test-mongoc-command-monitoring.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ test_client_cmd_op_ids (void)
10151015

10161016

10171017
static void
1018-
test_killcursors_deprecated (void)
1018+
test_killcursors_deprecated (void* unused)
10191019
{
10201020
cmd_test_t test;
10211021
mongoc_client_t *client;
@@ -1032,7 +1032,8 @@ test_killcursors_deprecated (void)
10321032
ASSERT_OR_PRINT (r, error);
10331033
set_cmd_test_callbacks (client, (void *) &test);
10341034

1035-
/* deprecated function without "db" or "collection", skips APM */
1035+
/* deprecated function without "db" or "collection", skips APM. This sends
1036+
* OP_KILL_CURSORS. */
10361037
mongoc_client_kill_cursor (client, 123);
10371038

10381039
ASSERT_CMPINT (0, ==, test.started_calls);
@@ -1242,9 +1243,12 @@ test_command_monitoring_install (TestSuite *suite)
12421243
suite, "/command_monitoring/client_cmd_simple", test_client_cmd_simple);
12431244
TestSuite_AddLive (
12441245
suite, "/command_monitoring/client_cmd/op_ids", test_client_cmd_op_ids);
1245-
TestSuite_AddLive (suite,
1246+
TestSuite_AddFull (suite,
12461247
"/command_monitoring/killcursors_deprecated",
1247-
test_killcursors_deprecated);
1248+
test_killcursors_deprecated,
1249+
NULL /* dtor */,
1250+
NULL /* ctx */,
1251+
test_framework_skip_if_no_legacy_opcodes);
12481252
TestSuite_AddMockServerTest (suite,
12491253
"/command_monitoring/failed_reply_mock",
12501254
test_command_failed_reply_mock);

src/libmongoc/tests/test-mongoc-cursor.c

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "mock_server/future-functions.h"
88
#include "mongoc/mongoc-cursor-private.h"
99
#include "mongoc/mongoc-collection-private.h"
10+
#include "mongoc/mongoc-error-private.h"
1011
#include "mongoc/mongoc-read-concern-private.h"
1112
#include "mongoc/mongoc-write-concern-private.h"
1213
#include "test-conveniences.h"
@@ -650,7 +651,12 @@ killcursors_succeeded (const mongoc_apm_command_succeeded_t *event)
650651
extern void
651652
_mongoc_cursor_impl_find_opquery_init (mongoc_cursor_t *cursor, bson_t *filter);
652653

653-
/* test killing a cursor with mongo_cursor_destroy and a real server */
654+
/* Tests killing a cursor with mongo_cursor_destroy and a real server.
655+
* Asserts that the cursor ID is no longer valid by attempting to get another
656+
* batch of results with the previously killed cursor ID. Uses OP_GET_MORE (on
657+
* servers older than 3.2) or a getMore command (servers 3.2+) to iterate the
658+
* cursor ID.
659+
*/
654660
static void
655661
test_kill_cursor_live (void)
656662
{
@@ -702,22 +708,40 @@ test_kill_cursor_live (void)
702708

703709
ASSERT_CMPINT (ctx.succeeded_count, ==, 1);
704710

705-
b = bson_new ();
706-
cursor = _mongoc_cursor_find_new (
707-
client, collection->ns, b, NULL, NULL, NULL, NULL);
708-
/* override the typical priming, and immediately transition to an OPQUERY
709-
* find cursor. */
710-
cursor->impl.destroy (&cursor->impl);
711-
_mongoc_cursor_impl_find_opquery_init (cursor, b);
711+
if (test_framework_supports_legacy_opcodes ()) {
712+
b = bson_new ();
713+
cursor = _mongoc_cursor_find_new (
714+
client, collection->ns, b, NULL, NULL, NULL, NULL);
715+
/* override the typical priming, and immediately transition to an OPQUERY
716+
* find cursor. */
717+
cursor->impl.destroy (&cursor->impl);
718+
_mongoc_cursor_impl_find_opquery_init (cursor, b);
719+
720+
cursor->cursor_id = ctx.cursor_id;
721+
cursor->state = END_OF_BATCH; /* meaning, "finished reading first batch" */
722+
r = mongoc_cursor_next (cursor, &doc);
723+
ASSERT (!r);
724+
ASSERT (mongoc_cursor_error (cursor, &error));
725+
ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_CURSOR, 16, "cursor is invalid");
712726

713-
cursor->cursor_id = ctx.cursor_id;
714-
cursor->state = END_OF_BATCH; /* meaning, "finished reading first batch" */
715-
r = mongoc_cursor_next (cursor, &doc);
716-
ASSERT (!r);
717-
ASSERT (mongoc_cursor_error (cursor, &error));
718-
ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_CURSOR, 16, "cursor is invalid");
727+
mongoc_cursor_destroy (cursor);
728+
} else {
729+
bson_t *cmd;
730+
731+
cmd = BCON_NEW ("getMore",
732+
BCON_INT64 (ctx.cursor_id),
733+
"collection",
734+
mongoc_collection_get_name (collection));
735+
r = mongoc_client_command_simple (
736+
client, "test", cmd, NULL /* read prefs */, NULL /* reply */, &error);
737+
ASSERT (!r);
738+
ASSERT_ERROR_CONTAINS (error,
739+
MONGOC_ERROR_QUERY,
740+
MONGOC_SERVER_ERR_CURSOR_NOT_FOUND,
741+
"not found");
742+
bson_destroy (cmd);
743+
}
719744

720-
mongoc_cursor_destroy (cursor);
721745
mongoc_bulk_operation_destroy (bulk);
722746
mongoc_collection_destroy (collection);
723747
mongoc_client_destroy (client);

src/libmongoc/tests/test-mongoc-exhaust.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -666,19 +666,22 @@ test_exhaust_install (TestSuite *suite)
666666
test_exhaust_cursor_single,
667667
NULL,
668668
NULL,
669-
skip_if_mongos);
669+
skip_if_mongos,
670+
test_framework_skip_if_no_legacy_opcodes);
670671
TestSuite_AddFull (suite,
671672
"/Client/exhaust_cursor/pool",
672673
test_exhaust_cursor_pool,
673674
NULL,
674675
NULL,
675-
skip_if_mongos);
676+
skip_if_mongos,
677+
test_framework_skip_if_no_legacy_opcodes);
676678
TestSuite_AddFull (suite,
677679
"/Client/exhaust_cursor/batches",
678680
test_exhaust_cursor_multi_batch,
679681
NULL,
680682
NULL,
681-
skip_if_mongos);
683+
skip_if_mongos,
684+
test_framework_skip_if_no_legacy_opcodes);
682685
TestSuite_AddLive (suite,
683686
"/Client/set_max_await_time_ms",
684687
test_cursor_set_max_await_time_ms);

0 commit comments

Comments
 (0)