Skip to content

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

Merged
merged 7 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .evergreen/download-mongodb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ get_mongodb_download_url_for ()
_DISTRO=$1
_VERSION=$2

VERSION_50="5.0.0"
VERSION_50="5.0.2"
Copy link
Collaborator Author

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.

VERSION_44="4.4.6"
VERSION_42="4.2.15"
VERSION_40="4.0.25"
Expand Down
38 changes: 29 additions & 9 deletions src/libmongoc/tests/test-mongoc-sample-commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/

/* clang-format off */
#include <assert.h>
#include <mongoc/mongoc.h>
#include <mongoc/mongoc-util-private.h>
#include <mongoc/mongoc-database-private.h>
Expand Down Expand Up @@ -3435,6 +3436,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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callers of get_client_for_version_api_example are expected to call mongoc_client_destroy to release the client.

test_framework_set_ssl_opts (client);
mongoc_uri_destroy (uri);
return client;
}

static bool
callback (mongoc_client_session_t *session,
void *ctx,
Expand Down Expand Up @@ -3592,12 +3607,13 @@ _test_sample_versioned_api_example_1 (void)
* client = mongoc_client_new (uri_sharded);
*/

client = get_client ();
/* Create a mongoc_client_t without server API options configured. */
client = get_client_for_version_api_example ();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_client calls test_framework_new_default_client, which calls test_framework_client_new.

test_framework_client_new applies server API options if required by the test environment.

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 MONGODB_API_VERSION controls whether test clients have server API options set by default..

MONGODB_API_VERSION is set on the versioned-api variant to run the test suite against servers expecting a server API options in all commands. Those tests are specified in the Versioned API test readme

If the environment variable is set, all clients created in tests MUST declare the ServerApiVersion specified.

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I added a note to say that get_client_for_version_api_example returns a client without server API options configured.


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);
assert (mongoc_client_set_server_api (client, server_api, &error));
/* End Versioned API Example 1 */

mongoc_client_destroy (client);
Expand All @@ -3624,13 +3640,14 @@ _test_sample_versioned_api_example_2 (void)
* client = mongoc_client_new (uri_sharded);
*/

client = get_client ();
/* Create a mongoc_client_t without server API options configured. */
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);
assert (mongoc_client_set_server_api (client, server_api, &error));
/* End Versioned API Example 2 */

mongoc_client_destroy (client);
Expand All @@ -3657,13 +3674,14 @@ _test_sample_versioned_api_example_3 (void)
* client = mongoc_client_new (uri_sharded);
*/

client = get_client ();
/* Create a mongoc_client_t without server API options configured. */
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);
assert (mongoc_client_set_server_api (client, server_api, &error));
/* End Versioned API Example 3 */

mongoc_client_destroy (client);
Expand All @@ -3690,13 +3708,14 @@ _test_sample_versioned_api_example_4 (void)
* client = mongoc_client_new (uri_sharded);
*/

client = get_client ();
/* Create a mongoc_client_t without server API options configured. */
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);
assert (mongoc_client_set_server_api (client, server_api, &error));
/* End Versioned API Example 4 */

mongoc_client_destroy (client);
Expand Down Expand Up @@ -3725,7 +3744,8 @@ static void _test_sample_versioned_api_example_5_6_7_8 (void) {
int64_t count;
bson_t *filter;

client = get_client ();
/* Create a mongoc_client_t without server API options configured. */
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);
Expand Down