|
| 1 | +#include <mongoc-cluster-private.h> |
| 2 | +#include "mongoc-cluster-private.h" |
| 3 | +#include "mongoc-client-private.h" |
| 4 | + |
| 5 | +#include "TestSuite.h" |
| 6 | +#include "test-libmongoc.h" |
| 7 | + |
| 8 | + |
| 9 | +#undef MONGOC_LOG_DOMAIN |
| 10 | +#define MONGOC_LOG_DOMAIN "cluster-test" |
| 11 | + |
| 12 | + |
| 13 | +void |
| 14 | +call_ismaster (bson_t *reply) |
| 15 | +{ |
| 16 | + bson_t ismaster = BSON_INITIALIZER; |
| 17 | + mongoc_client_t *client; |
| 18 | + bool r; |
| 19 | + |
| 20 | + BSON_APPEND_INT32 (&ismaster, "isMaster", 1); |
| 21 | + client = test_framework_client_new (NULL); |
| 22 | + r = mongoc_client_command_simple (client, "admin", &ismaster, |
| 23 | + NULL, reply, NULL); |
| 24 | + |
| 25 | + assert (r); |
| 26 | +} |
| 27 | + |
| 28 | + |
| 29 | +char * |
| 30 | +set_name (bson_t *ismaster_response) |
| 31 | +{ |
| 32 | + bson_iter_t iter; |
| 33 | + |
| 34 | + if (bson_iter_init_find (&iter, ismaster_response, "setName")) { |
| 35 | + return bson_strdup (bson_iter_utf8 (&iter, NULL)); |
| 36 | + } else { |
| 37 | + return NULL; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +int |
| 43 | +get_n_members (bson_t *ismaster_response) |
| 44 | +{ |
| 45 | + bson_iter_t iter; |
| 46 | + bson_iter_t hosts_iter; |
| 47 | + char *name; |
| 48 | + int n; |
| 49 | + |
| 50 | + if ((name = set_name (ismaster_response))) { |
| 51 | + bson_free (name); |
| 52 | + assert (bson_iter_init_find (&iter, ismaster_response, "hosts")); |
| 53 | + bson_iter_recurse (&iter, &hosts_iter); |
| 54 | + n = 0; |
| 55 | + while (bson_iter_next (&hosts_iter)) n++; |
| 56 | + return n; |
| 57 | + } else { |
| 58 | + return 1; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +#define BAD_HOST "mongodb.com:12345" |
| 64 | + |
| 65 | + |
| 66 | +/* a uri with one bogus host */ |
| 67 | +mongoc_uri_t * |
| 68 | +uri_from_ismaster_plus_one (bson_t *ismaster_response) |
| 69 | +{ |
| 70 | + /* start with one bad host and a comma */ |
| 71 | + bson_string_t *uri_str = bson_string_new ("mongodb://" BAD_HOST ","); |
| 72 | + char *name; |
| 73 | + bson_iter_t iter; |
| 74 | + bson_iter_t hosts_iter; |
| 75 | + |
| 76 | + if ((name = set_name (ismaster_response))) { |
| 77 | + bson_iter_init_find (&iter, ismaster_response, "hosts"); |
| 78 | + bson_iter_recurse (&iter, &hosts_iter); |
| 79 | + while (bson_iter_next (&hosts_iter)) { |
| 80 | + assert (BSON_ITER_HOLDS_UTF8 (&hosts_iter)); |
| 81 | + bson_string_append (uri_str, bson_iter_utf8 (&hosts_iter, NULL)); |
| 82 | + while (bson_iter_next (&hosts_iter)) { |
| 83 | + bson_string_append (uri_str, ","); |
| 84 | + bson_string_append (uri_str, bson_iter_utf8 (&hosts_iter, NULL)); |
| 85 | + } |
| 86 | + |
| 87 | + bson_string_append_printf ( |
| 88 | + uri_str, "/?replicaSet=%s&connecttimeoutms=1000", name); |
| 89 | + } |
| 90 | + |
| 91 | + bson_free (name); |
| 92 | + } else { |
| 93 | + char *host = test_framework_get_host (); |
| 94 | + |
| 95 | + bson_string_append (uri_str, host); |
| 96 | + bson_string_append (uri_str, "/?connecttimeoutms=1000"); |
| 97 | + |
| 98 | + bson_free (host); |
| 99 | + } |
| 100 | + |
| 101 | + return mongoc_uri_new (bson_string_free (uri_str, false)); |
| 102 | +} |
| 103 | + |
| 104 | + |
| 105 | +bool |
| 106 | +cluster_has_host (const mongoc_cluster_t *cluster, |
| 107 | + const char *host_and_port) |
| 108 | +{ |
| 109 | + int i; |
| 110 | + |
| 111 | + for (i = 0; i < cluster->nodes_len; i++) { |
| 112 | + if (!strcmp(cluster->nodes[i].host.host_and_port, host_and_port)) { |
| 113 | + return true; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return false; |
| 118 | +} |
| 119 | + |
| 120 | + |
| 121 | +int |
| 122 | +hosts_len (const mongoc_host_list_t *hl) |
| 123 | +{ |
| 124 | + int n = 0; |
| 125 | + |
| 126 | + if (!hl) { |
| 127 | + return 0; |
| 128 | + } |
| 129 | + |
| 130 | + do { n++; } while ((hl = hl->next)); |
| 131 | + |
| 132 | + return n; |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +void |
| 137 | +assert_hosts_equal (const mongoc_host_list_t *hl, |
| 138 | + const mongoc_cluster_t *cluster) |
| 139 | +{ |
| 140 | + ASSERT_CMPINT (hosts_len (hl), ==, cluster->nodes_len); |
| 141 | + |
| 142 | + while (hl) { |
| 143 | + if (!cluster_has_host (cluster, hl->host_and_port) && |
| 144 | + strcmp (BAD_HOST, hl->host_and_port)) { |
| 145 | + printf ("cluster has no host %s\n", hl->host_and_port); |
| 146 | + abort (); |
| 147 | + } |
| 148 | + |
| 149 | + hl = hl->next; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +/* not very exhaustive, but ensure that cluster reflects whatever server |
| 155 | + * we're connected to */ |
| 156 | +static void |
| 157 | +test_mongoc_cluster_basic (void) |
| 158 | +{ |
| 159 | + mongoc_client_t *client; |
| 160 | + bson_t reply; |
| 161 | + mongoc_uri_t *uri; |
| 162 | + const mongoc_host_list_t *hosts; |
| 163 | + bson_error_t error; |
| 164 | + int n_members; |
| 165 | + char *replica_set_name; |
| 166 | + int i; |
| 167 | + int n; |
| 168 | + |
| 169 | + call_ismaster (&reply); |
| 170 | + n_members = get_n_members (&reply); |
| 171 | + replica_set_name = set_name (&reply); |
| 172 | + uri = uri_from_ismaster_plus_one (&reply); |
| 173 | + |
| 174 | + /* get hosts list from uri */ |
| 175 | + hosts = mongoc_uri_get_hosts (uri); |
| 176 | + assert (hosts); |
| 177 | + ASSERT_CMPSTR (BAD_HOST, hosts->host_and_port); |
| 178 | + |
| 179 | + if (replica_set_name) { |
| 180 | + /* remove bad host we prepended, because the cluster will remove it |
| 181 | + * once it finds the primary */ |
| 182 | + hosts = hosts->next; |
| 183 | + assert (hosts); |
| 184 | + } |
| 185 | + |
| 186 | + client = mongoc_client_new_from_uri (uri); |
| 187 | + |
| 188 | + ASSERT_CMPINT (n_members, ==, client->cluster.nodes_len - 1); |
| 189 | + if (replica_set_name) { |
| 190 | + ASSERT_CMPINT (MONGOC_CLUSTER_REPLICA_SET, ==, client->cluster.mode); |
| 191 | + } else { |
| 192 | + /* sharded mode, since we gave 2 seeds */ |
| 193 | + ASSERT_CMPINT (MONGOC_CLUSTER_SHARDED_CLUSTER, ==, client->cluster.mode); |
| 194 | + } |
| 195 | + |
| 196 | + /* connect twice and assert cluster nodes are as expected */ |
| 197 | + for (i = 0; i < 2; i++) { |
| 198 | + /* warnings about failing to connect to mongodb.com:12345 */ |
| 199 | + suppress_one_message (); |
| 200 | + suppress_one_message (); |
| 201 | + suppress_one_message (); |
| 202 | + |
| 203 | + _mongoc_cluster_reconnect (&client->cluster, &error); |
| 204 | + |
| 205 | + assert_hosts_equal (hosts, &client->cluster); |
| 206 | + |
| 207 | + for (n = 0; n < client->cluster.nodes_len; n++) { |
| 208 | + const mongoc_cluster_node_t *node = &client->cluster.nodes[n]; |
| 209 | + bool valid_host; |
| 210 | + |
| 211 | + assert (node->valid); |
| 212 | + |
| 213 | + valid_host = (strlen (node->host.host_and_port) && |
| 214 | + strcmp (node->host.host_and_port, BAD_HOST)); |
| 215 | + |
| 216 | + if (valid_host) { |
| 217 | + assert (node->stream); |
| 218 | + } else { |
| 219 | + assert (!node->stream); |
| 220 | + } |
| 221 | + |
| 222 | + ASSERT_CMPINT (n, ==, node->index); |
| 223 | + ASSERT_CMPINT (0, ==, node->stamp); |
| 224 | + ASSERT_CMPSTR (replica_set_name, node->replSet); |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + bson_free (replica_set_name); |
| 229 | + mongoc_uri_destroy (uri); |
| 230 | + bson_destroy (&reply); |
| 231 | + mongoc_client_destroy (client); |
| 232 | +} |
| 233 | + |
| 234 | + |
| 235 | +void |
| 236 | +test_cluster_install (TestSuite *suite) |
| 237 | +{ |
| 238 | + TestSuite_Add (suite, "/Cluster/basic", test_mongoc_cluster_basic); |
| 239 | +} |
0 commit comments