-
Notifications
You must be signed in to change notification settings - Fork 455
CDRIVER-4093 fix versioned API sample tests #848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
555d218
f6eaebe
ebe34bd
33251f6
0ddb170
9c8a5d4
afa584b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3435,6 +3435,20 @@ get_client (void) | |
return test_framework_new_default_client (); | ||
} | ||
|
||
/* Returns a test client without version API options configured. */ | ||
static mongoc_client_t * | ||
get_client_for_version_api_example (void) { | ||
mongoc_client_t *client; | ||
mongoc_uri_t *uri; | ||
|
||
uri = test_framework_get_uri (); | ||
client = mongoc_client_new_from_uri (uri); | ||
ASSERT (client); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming that since this is test code that we expect to just terminate, there's no need to release the "client" resource? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Callers of |
||
test_framework_set_ssl_opts (client); | ||
mongoc_uri_destroy (uri); | ||
return client; | ||
} | ||
|
||
static bool | ||
callback (mongoc_client_session_t *session, | ||
void *ctx, | ||
|
@@ -3580,6 +3594,7 @@ _test_sample_versioned_api_example_1 (void) | |
mongoc_server_api_t *server_api = NULL; | ||
mongoc_server_api_version_t server_api_version; | ||
bson_error_t error; | ||
bool ok; | ||
|
||
/* For a replica set, include the replica set name and a seedlist of the | ||
* members in the URI string; e.g. | ||
|
@@ -3592,12 +3607,13 @@ _test_sample_versioned_api_example_1 (void) | |
* client = mongoc_client_new (uri_sharded); | ||
*/ | ||
|
||
client = get_client (); | ||
client = get_client_for_version_api_example (); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This test sets server API options on the client. To avoid setting duplicate options, the client should not be created with default server API options. For more background: the environment variable
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth adding a comment in the code about the API options note? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, I added a note to say that |
||
|
||
mongoc_server_api_version_from_string ("1", &server_api_version); | ||
server_api = mongoc_server_api_new (server_api_version); | ||
|
||
mongoc_client_set_server_api (client, server_api, &error); | ||
ok = mongoc_client_set_server_api (client, server_api, &error); | ||
BSON_ASSERT (ok); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we care much that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. Though I looked through other examples code for reference. There aren't many assertions, but there are instances of both uses of https://github.com/mongodb/mongo-c-driver/blob/3a41062e397638399a6e491ec94bd6c861ec212c/src/libmongoc/examples/example-update.c#L52:L52 uses |
||
/* End Versioned API Example 1 */ | ||
|
||
mongoc_client_destroy (client); | ||
|
@@ -3612,6 +3628,7 @@ _test_sample_versioned_api_example_2 (void) | |
mongoc_server_api_t *server_api = NULL; | ||
mongoc_server_api_version_t server_api_version; | ||
bson_error_t error; | ||
bool ok; | ||
|
||
/* For a replica set, include the replica set name and a seedlist of the | ||
* members in the URI string; e.g. | ||
|
@@ -3624,13 +3641,14 @@ _test_sample_versioned_api_example_2 (void) | |
* client = mongoc_client_new (uri_sharded); | ||
*/ | ||
|
||
client = get_client (); | ||
client = get_client_for_version_api_example (); | ||
|
||
mongoc_server_api_version_from_string ("1", &server_api_version); | ||
server_api = mongoc_server_api_new (server_api_version); | ||
mongoc_server_api_strict (server_api, true); | ||
|
||
mongoc_client_set_server_api (client, server_api, &error); | ||
ok = mongoc_client_set_server_api (client, server_api, &error); | ||
BSON_ASSERT (ok); | ||
/* End Versioned API Example 2 */ | ||
|
||
mongoc_client_destroy (client); | ||
|
@@ -3645,6 +3663,7 @@ _test_sample_versioned_api_example_3 (void) | |
mongoc_server_api_t *server_api = NULL; | ||
mongoc_server_api_version_t server_api_version; | ||
bson_error_t error; | ||
bool ok; | ||
|
||
/* For a replica set, include the replica set name and a seedlist of the | ||
* members in the URI string; e.g. | ||
|
@@ -3657,13 +3676,14 @@ _test_sample_versioned_api_example_3 (void) | |
* client = mongoc_client_new (uri_sharded); | ||
*/ | ||
|
||
client = get_client (); | ||
client = get_client_for_version_api_example (); | ||
|
||
mongoc_server_api_version_from_string ("1", &server_api_version); | ||
server_api = mongoc_server_api_new (server_api_version); | ||
mongoc_server_api_strict (server_api, false); | ||
|
||
mongoc_client_set_server_api (client, server_api, &error); | ||
ok = mongoc_client_set_server_api (client, server_api, &error); | ||
BSON_ASSERT (ok); | ||
/* End Versioned API Example 3 */ | ||
|
||
mongoc_client_destroy (client); | ||
|
@@ -3678,6 +3698,7 @@ _test_sample_versioned_api_example_4 (void) | |
mongoc_server_api_t *server_api = NULL; | ||
mongoc_server_api_version_t server_api_version; | ||
bson_error_t error; | ||
bool ok; | ||
|
||
/* For a replica set, include the replica set name and a seedlist of the | ||
* members in the URI string; e.g. | ||
|
@@ -3690,13 +3711,14 @@ _test_sample_versioned_api_example_4 (void) | |
* client = mongoc_client_new (uri_sharded); | ||
*/ | ||
|
||
client = get_client (); | ||
client = get_client_for_version_api_example (); | ||
|
||
mongoc_server_api_version_from_string ("1", &server_api_version); | ||
server_api = mongoc_server_api_new (server_api_version); | ||
mongoc_server_api_deprecation_errors (server_api, true); | ||
|
||
mongoc_client_set_server_api (client, server_api, &error); | ||
ok = mongoc_client_set_server_api (client, server_api, &error); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I'm ok with "ok", but it might be okay-er to just check the call with BSON_ASSERT directly, if this is the only place we're using it. But, really, I'm ok either way! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, here and in the other changes! |
||
BSON_ASSERT (ok); | ||
/* End Versioned API Example 4 */ | ||
|
||
mongoc_client_destroy (client); | ||
|
@@ -3721,11 +3743,10 @@ static void _test_sample_versioned_api_example_5_6_7_8 (void) { | |
bson_t *docs[N_DOCS]; | ||
int i; | ||
bson_t reply; | ||
bson_t *cmd; | ||
int64_t count; | ||
bson_t *filter; | ||
|
||
client = get_client (); | ||
client = get_client_for_version_api_example (); | ||
mongoc_client_set_error_api (client, MONGOC_ERROR_API_VERSION_2); | ||
mongoc_server_api_version_from_string ("1", &server_api_version); | ||
server_api = mongoc_server_api_new (server_api_version); | ||
|
@@ -3789,24 +3810,28 @@ static void _test_sample_versioned_api_example_5_6_7_8 (void) { | |
ASSERT_OR_PRINT (ok, error); | ||
bson_destroy (&reply); | ||
|
||
cmd = BCON_NEW ("count", "sales"); | ||
ok = mongoc_database_command_simple ( | ||
db, cmd, NULL /* read_prefs */, &reply, &error); | ||
ASSERT_ERROR_CONTAINS (error, | ||
MONGOC_ERROR_SERVER, | ||
323, | ||
"Provided apiStrict:true, but the command count is not in API Version 1"); | ||
ASSERT (!ok); | ||
bson_destroy (&reply); | ||
BEGIN_IGNORE_DEPRECATIONS; | ||
count = mongoc_collection_count (sales, | ||
MONGOC_QUERY_NONE, | ||
NULL /* query */, | ||
0 /* skip */, | ||
0 /* limit */, | ||
NULL /* read prefs */, | ||
&error); | ||
END_IGNORE_DEPRECATIONS; | ||
ASSERT_ERROR_CONTAINS ( | ||
error, | ||
MONGOC_ERROR_SERVER, | ||
323, | ||
"Provided apiStrict:true, but the command count is not in API Version 1"); | ||
ASSERT_CMPINT64 (-1, ==, count); | ||
#if 0 | ||
/* This block not evaluated, but is inserted into documentation to represent the above reply. | ||
* Don't delete me! */ | ||
/* Start Versioned API Example 6 */ | ||
char *str = bson_as_json (&reply, NULL /* length */); | ||
printf ("%s", str); | ||
/* Prints the server reply: | ||
* { "ok" : 0, "errmsg" : "Provided apiStrict:true, but the command count is not in API Version 1", "code" : 323, "codeName" : "APIStrictError" } */ | ||
bson_free (str); | ||
printf ("%s", error.message); | ||
/* Prints the error message: | ||
* "Provided apiStrict:true, but the command count is not in API Version 1. Information on supported commands and migrations in API Version 1 can be found at https://dochub.mongodb.org/core/manual-versioned-api" */ | ||
/* End Versioned API Example 6 */ | ||
#endif | ||
|
||
|
@@ -3825,7 +3850,6 @@ static void _test_sample_versioned_api_example_5_6_7_8 (void) { | |
/* End Versioned API Example 8 */ | ||
|
||
bson_destroy (filter); | ||
bson_destroy (cmd); | ||
for (i = 0; i < N_DOCS; i++) { | ||
bson_destroy (docs[i]); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
download-mongodb.sh is copied from mongodb-labs/driver-evergreen-tools. This is required to get 5.0 server version with SERVER-58794 fixed.
On 5.0.0, the collection
create
command fails against sharded clusters when a versioned API is required. That was causing this test-asan-5.0-sharded-noauth-nosasl-nossl test failure.