Skip to content

Commit 46ff8b2

Browse files
committed
CDRIVER-939: Test with domain socket connection
1 parent 20c676d commit 46ff8b2

File tree

4 files changed

+173
-8
lines changed

4 files changed

+173
-8
lines changed

tests/test-libmongoc.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,33 @@ test_framework_getenv_int64 (const char *name,
230230
return default_value;
231231
}
232232

233+
234+
/*
235+
*--------------------------------------------------------------------------
236+
*
237+
* test_framework_get_unix_domain_socket_path --
238+
*
239+
* Get the path to Unix Domain Socket .sock of the test MongoDB server.
240+
*
241+
* Returns:
242+
* A string you must bson_free.
243+
*
244+
* Side effects:
245+
* None.
246+
*
247+
*--------------------------------------------------------------------------
248+
*/
249+
char *
250+
test_framework_get_unix_domain_socket_path (void)
251+
{
252+
char *path = test_framework_getenv ("MONGOC_TEST_UNIX_DOMAIN_SOCKET");
253+
254+
if (path) {
255+
return path;
256+
}
257+
return bson_strdup_printf ("%%2Ftmp%%2Fmongodb-%d.sock",
258+
test_framework_get_port());
259+
}
233260
/*
234261
*--------------------------------------------------------------------------
235262
*
@@ -480,6 +507,45 @@ test_framework_get_ssl (void)
480507
}
481508

482509

510+
/*
511+
*--------------------------------------------------------------------------
512+
*
513+
* test_framework_get_unix_domain_socket_uri_str --
514+
*
515+
* Get the connection string (unix domain socket style) of the test
516+
* MongoDB server based on the variables set in the environment.
517+
* Does *not* call isMaster to discover your actual topology.
518+
*
519+
* Returns:
520+
* A string you must bson_free.
521+
*
522+
* Side effects:
523+
* Same as test_framework_get_user_password.
524+
*
525+
*--------------------------------------------------------------------------
526+
*/
527+
char *
528+
test_framework_get_unix_domain_socket_uri_str ()
529+
{
530+
char *path;
531+
char *test_uri_str;
532+
char *test_uri_str_auth;
533+
534+
path = test_framework_get_unix_domain_socket_path ();
535+
test_uri_str = bson_strdup_printf (
536+
"mongodb://%s/%s",
537+
path,
538+
test_framework_get_ssl () ? "?ssl=true" : "");
539+
540+
test_uri_str_auth = test_framework_add_user_password_from_env (test_uri_str);
541+
542+
bson_free (path);
543+
bson_free (test_uri_str);
544+
545+
return test_uri_str_auth;
546+
}
547+
548+
483549
/*
484550
*--------------------------------------------------------------------------
485551
*
@@ -916,6 +982,12 @@ test_framework_is_replset (void)
916982
return is_replset;
917983
}
918984

985+
int
986+
test_framework_skip_if_single (void)
987+
{
988+
return (test_framework_is_mongos () || test_framework_is_replset());
989+
}
990+
919991
int
920992
test_framework_skip_if_mongos (void)
921993
{
@@ -928,6 +1000,16 @@ test_framework_skip_if_replset (void)
9281000
return test_framework_is_replset() ? 0 : 1;
9291001
}
9301002

1003+
int
1004+
test_framework_skip_if_windows (void)
1005+
{
1006+
#ifdef _WIN32
1007+
return false;
1008+
#else
1009+
return true;
1010+
#endif
1011+
}
1012+
9311013
bool
9321014
test_framework_max_wire_version_at_least (int version)
9331015
{

tests/test-libmongoc.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,21 @@ char *test_framework_add_user_password (const char *uri_str,
3535
const char *password);
3636
char *test_framework_get_uri_str_no_auth (const char *database_name);
3737
char *test_framework_get_uri_str (void);
38+
char *test_framework_get_unix_domain_socket_uri_str (void);
39+
char *test_framework_get_unix_domain_socket_path (void);
3840
mongoc_uri_t *test_framework_get_uri (void);
3941
void test_framework_set_ssl_opts (mongoc_client_t *client);
4042
mongoc_client_t *test_framework_client_new (void);
4143
mongoc_client_pool_t *test_framework_client_pool_new (void);
44+
bool test_framework_max_wire_version_at_least (int version);
45+
4246
bool test_framework_is_mongos (void);
4347
bool test_framework_is_replset (void);
44-
int test_framework_skip_if_mongos (void);
48+
49+
int test_framework_skip_if_mongos (void);
4550
int test_framework_skip_if_replset (void);
46-
bool test_framework_max_wire_version_at_least (int version);
51+
int test_framework_skip_if_single (void);
52+
int test_framework_skip_if_windows (void);
4753

4854
typedef struct _debug_stream_stats_t {
4955
int n_destroyed;

tests/test-mongoc-client.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,33 @@ test_mongoc_client_ipv6 (void)
852852
}
853853

854854

855+
static void
856+
test_mongoc_client_unix_domain_socket (void *context)
857+
{
858+
mongoc_client_t *client;
859+
bson_error_t error;
860+
char *uri_str;
861+
bson_iter_t iter;
862+
bson_t reply;
863+
864+
uri_str = test_framework_get_unix_domain_socket_uri_str ();
865+
client = mongoc_client_new (uri_str);
866+
867+
assert (client);
868+
869+
ASSERT_OR_PRINT (mongoc_client_get_server_status (client, NULL,
870+
&reply, &error), error);
871+
872+
assert (bson_iter_init_find (&iter, &reply, "host"));
873+
assert (bson_iter_init_find (&iter, &reply, "version"));
874+
assert (bson_iter_init_find (&iter, &reply, "ok"));
875+
876+
bson_destroy (&reply);
877+
878+
mongoc_client_destroy (client);
879+
}
880+
881+
855882
void
856883
test_client_install (TestSuite *suite)
857884
{
@@ -884,6 +911,7 @@ test_client_install (TestSuite *suite)
884911
TestSuite_Add (suite, "/Client/recovering", test_recovering);
885912
TestSuite_Add (suite, "/Client/server_status", test_server_status);
886913
TestSuite_Add (suite, "/Client/database_names", test_get_database_names);
914+
TestSuite_AddFull (suite, "/Client/connect/uds", test_mongoc_client_unix_domain_socket, NULL, NULL, test_framework_skip_if_windows);
887915

888916
#ifdef TODO_CDRIVER_689
889917
TestSuite_Add (suite, "/Client/wire_version", test_wire_version);

tests/test-mongoc-server-selection-errors.c

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
#include "test-conveniences.h"
55
#include "test-libmongoc.h"
66

7-
int skip_if_single (void)
8-
{
9-
return (test_framework_is_mongos () || test_framework_is_replset());
10-
}
11-
127
static void
138
server_selection_error_dns (const char *uri, const char *errmsg, bool assert_as)
149
{
@@ -56,6 +51,7 @@ test_server_selection_error_dns_multi_fail (void)
5651
false
5752
);
5853
}
54+
5955
static void
6056
test_server_selection_error_dns_multi_success (void *context)
6157
{
@@ -73,10 +69,63 @@ test_server_selection_error_dns_multi_success (void *context)
7369
bson_free (uri_str);
7470
}
7571

72+
static void
73+
test_server_selection_uds_auth_failure (void *context)
74+
{
75+
mongoc_client_t *client;
76+
bson_error_t error;
77+
bson_t reply;
78+
char *path;
79+
char *uri;
80+
81+
path = test_framework_get_unix_domain_socket_path ();
82+
uri = bson_strdup_printf ("mongodb://user:wrongpass@%s", path);
83+
client = mongoc_client_new (uri);
84+
85+
assert (client);
86+
87+
ASSERT_OR_PRINT (! mongoc_client_get_server_status (client, NULL,
88+
&reply, &error), error);
89+
90+
ASSERT_CMPINT (error.domain, ==, MONGOC_ERROR_CLIENT);
91+
ASSERT_CMPINT (error.code, ==, MONGOC_ERROR_CLIENT_AUTHENTICATE);
92+
93+
bson_destroy (&reply);
94+
bson_free (path);
95+
bson_free (uri);
96+
97+
mongoc_client_destroy (client);
98+
}
99+
100+
static void
101+
test_server_selection_uds_not_found (void *context)
102+
{
103+
mongoc_client_t *client;
104+
bson_error_t error;
105+
bson_t reply;
106+
107+
client = mongoc_client_new ("mongodb:///tmp/mongodb-so-close.sock");
108+
109+
assert (client);
110+
111+
ASSERT_OR_PRINT (! mongoc_client_get_server_status (client, NULL,
112+
&reply, &error), error);
113+
114+
ASSERT_CMPINT (error.domain, ==, MONGOC_ERROR_SERVER_SELECTION);
115+
ASSERT_CMPINT (error.code, ==, MONGOC_ERROR_SERVER_SELECTION_FAILURE);
116+
117+
bson_destroy (&reply);
118+
119+
mongoc_client_destroy (client);
120+
}
121+
122+
76123
void
77124
test_server_selection_errors_install (TestSuite *suite)
78125
{
79126
TestSuite_Add (suite, "/server_selection/errors/dns/single", test_server_selection_error_dns_single);
80127
TestSuite_Add (suite, "/server_selection/errors/dns/multi/fail", test_server_selection_error_dns_multi_fail);
81-
TestSuite_AddFull (suite, "/server_selection/errors/dns/multi/success", test_server_selection_error_dns_multi_success, NULL, NULL, skip_if_single);
128+
TestSuite_AddFull (suite, "/server_selection/errors/dns/multi/success", test_server_selection_error_dns_multi_success, NULL, NULL, test_framework_skip_if_single);
129+
TestSuite_AddFull (suite, "/server_selection/errors/uds/auth_failure", test_server_selection_uds_auth_failure, NULL, NULL, test_framework_skip_if_windows);
130+
TestSuite_AddFull (suite, "/server_selection/errors/uds/not_found", test_server_selection_uds_not_found, NULL, NULL, test_framework_skip_if_windows);
82131
}

0 commit comments

Comments
 (0)