File: client_construction_spec.rb

package info (click to toggle)
ruby-mongo 2.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,764 kB
  • sloc: ruby: 108,806; makefile: 5; sh: 2
file content (381 lines) | stat: -rw-r--r-- 12,651 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# frozen_string_literal: true
# rubocop:todo all

require 'spec_helper'

# Create a client with all possible configurations (forcing/discovering each
# topology type) and ensure the resulting client is usable.
describe 'Client construction' do
  let(:base_options) do
    SpecConfig.instance.test_options.merge(
      server_selection_timeout: 5,
      database: SpecConfig.instance.test_db,
    ).merge(SpecConfig.instance.credentials_or_external_user(
      user: SpecConfig.instance.test_user.name,
      password: SpecConfig.instance.test_user.password,
      auth_source: 'admin',
    ))
  end

  context 'in single topology' do
    require_topology :single

    it 'discovers standalone' do
      options = base_options.dup
      options.delete(:connect)
      client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first],
        options)
      client['client_construction'].insert_one(test: 1)
      expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Single)
      expect(client.options[:connect]).to be nil
    end

    it 'connects directly' do
      client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first],
        base_options.merge(connect: :direct))
      client['client_construction'].insert_one(test: 1)
      expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Single)
      expect(client.options[:connect]).to eq :direct
    end

    it 'creates connection pool and keeps it populated' do
      client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first],
        base_options.merge(min_pool_size: 1, max_pool_size: 1))
      # allow connection pool to populate
      sleep 0.1

      server = client.cluster.next_primary
      expect(server.pool.size).to eq(1)
      client['client_construction'].insert_one(test: 1)
      expect(server.pool.size).to eq(1)
    end
  end

  context 'in replica set topology' do
    require_topology :replica_set

    it 'discovers replica set' do
      options = base_options.dup
      options.delete(:connect)
      options.delete(:replica_set)
      client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first],
        options)
      client['client_construction'].insert_one(test: 1)
      expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::ReplicaSetWithPrimary)
      expect(client.options[:connect]).to be nil
      expect(client.options[:replica_set]).to be nil
    end

    it 'forces replica set' do
      replica_set_name = ClusterConfig.instance.replica_set_name
      expect(replica_set_name).not_to be nil
      client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first],
        base_options.merge(connect: :replica_set,
          replica_set: replica_set_name))
      client['client_construction'].insert_one(test: 1)
      expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::ReplicaSetWithPrimary)
      expect(client.options[:connect]).to be :replica_set
      expect(client.options[:replica_set]).to eq(replica_set_name)
    end

    it 'connects directly' do
      primary_address = ClusterConfig.instance.primary_address_str
      client = ClientRegistry.instance.new_local_client([primary_address],
        base_options.merge(connect: :direct))
      client['client_construction'].insert_one(test: 1)
      expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Single)
      expect(client.options[:connect]).to eq :direct
    end

    context 'direct connection with mismached me' do
      let(:address) { ClusterConfig.instance.alternate_address.to_s }

      let(:client) do
        new_local_client([address], SpecConfig.instance.test_options)
      end

      let(:server) { client.cluster.next_primary }

      it 'sets server type to primary' do
        expect(server.description).to be_primary
      end
    end

    # This test requires a PSA deployment. The port number is fixed for our
    # Evergreen/Docker setups.
    context 'when directly connecting to arbiters' do
      let(:options) do
        SpecConfig.instance.test_options.tap do |opt|
          opt.delete(:connect)
          opt.delete(:replica_set)
          opt.update(direct_connection: true)
        end
      end

      let(:client) do
        new_local_client(['localhost:27019'], options)
      end

      let(:response) { client.command(ismaster: 1).documents.first }

      it 'connects' do
        response.fetch('arbiterOnly').should be true
      end
    end
  end

  context 'in sharded topology' do
    require_topology :sharded

    it 'connects to sharded cluster' do
      options = base_options.dup
      options.delete(:connect)
      client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first],
        base_options.merge(connect: :sharded))
      client['client_construction'].insert_one(test: 1)
      expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Sharded)
      expect(client.options[:connect]).to be :sharded
    end

    it 'connects directly' do
      primary_address = ClusterConfig.instance.primary_address_str
      client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first],
        base_options.merge(connect: :direct))
      client['client_construction'].insert_one(test: 1)
      expect(client.cluster.topology).to be_a(Mongo::Cluster::Topology::Single)
      expect(client.options[:connect]).to eq :direct
    end
  end

  context 'when time is frozen' do
    let(:now) { Time.now }
    before do
      allow(Time).to receive(:now).and_return(now)
    end

    it 'connects' do
      client = ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first],
        SpecConfig.instance.test_options)
      expect(client.cluster.topology).not_to be_a(Mongo::Cluster::Topology::Unknown)
    end
  end

  context 'with auto encryption options'do
    require_libmongocrypt
    require_enterprise
    min_server_fcv '4.2'

    # Diagnostics of leaked background threads only, these tests do not
    # actually require a clean slate. https://jira.mongodb.org/browse/RUBY-2138
    clean_slate

    include_context 'define shared FLE helpers'
    include_context 'with local kms_providers'

    let(:options) { { auto_encryption_options: auto_encryption_options } }

    let(:auto_encryption_options) do
      {
        key_vault_client: key_vault_client,
        key_vault_namespace: key_vault_namespace,
        kms_providers: kms_providers,
        # Spawn mongocryptd on non-default port for sharded cluster tests
        extra_options: extra_options,
      }
    end

    let(:client) do
      ClientRegistry.instance.new_local_client([SpecConfig.instance.addresses.first], options)
    end

    context 'with AWS kms providers with empty string credentials' do
      let(:auto_encryption_options) do
        {
          key_vault_namespace: key_vault_namespace,
          kms_providers: {
            aws: {
              access_key_id: '',
              secret_access_key: '',
            }
          },
          # Spawn mongocryptd on non-default port for sharded cluster tests
          extra_options: extra_options,
        }
      end

      it 'raises an exception' do
        expect do
          client
        end.to raise_error(ArgumentError, /The access_key_id option must be a String with at least one character; it is currently an empty string/)
      end
    end

    context 'with default key vault client' do
      let(:key_vault_client) { nil }

      shared_examples 'creates a working key vault client' do
        it 'creates a working key vault client' do
          key_vault_client = client.encrypter.key_vault_client

          result = key_vault_client[:test].insert_one(test: 1)
          expect(result).to be_ok
        end
      end

      context 'when top-level max pool size is not 0' do
        include_examples 'creates a working key vault client'

        shared_examples 'limited connection pool' do
          it 'creates a key vault client with a different cluster than the existing client' do
            key_vault_client = client.encrypter.key_vault_client
            expect(key_vault_client.cluster).not_to eq(client.cluster)
          end

          # min pool size for the key vault client can be greater than 0
          # when the key vault client is the same as the top-level client.
          # This is OK because we aren't making any more connections for FLE,
          # the minimum was requested by application for its own needs.
          it 'uses min pool size 0 for key vault client' do
            key_vault_client = client.encrypter.key_vault_client
            key_vault_client.options[:min_pool_size].should be 0
          end
        end

        context 'when top-level max pool size is not specified' do
          before do
            client.options[:max_pool_size].should be nil
          end

          include_examples 'limited connection pool'

          it 'uses unspecified max pool size for key vault client' do
            key_vault_client = client.encrypter.key_vault_client
            key_vault_client.options[:max_pool_size].should be nil
          end
        end

        context 'when top-level max pool size is specified' do
          let(:options) do
            {
              auto_encryption_options: auto_encryption_options,
              max_pool_size: 42,
            }
          end

          include_examples 'limited connection pool'

          it 'uses the same max pool size for key vault client' do
            key_vault_client = client.encrypter.key_vault_client
            key_vault_client.options[:max_pool_size].should be 42
          end
        end
      end

      context 'when top-level max pool size is 0' do
        let(:options) do
          {
            auto_encryption_options: auto_encryption_options,
            max_pool_size: 0,
          }
        end

        before do
          client.options[:max_pool_size].should be 0
        end

        include_examples 'creates a working key vault client'

        it 'creates a key vault client with the same cluster as the existing client' do
          key_vault_client = client.encrypter.key_vault_client
          expect(key_vault_client.cluster).to eq(client.cluster)
        end
      end
    end
  end

  context 'when seed addresses are repeated in host list' do
    require_topology :single

    let(:primary_address) do
      ClusterConfig.instance.primary_address_host
    end

    let(:client) do
      new_local_client([primary_address, primary_address], SpecConfig.instance.test_options)
    end

    it 'deduplicates the addresses' do
      expect(client.cluster.addresses).to eq([Mongo::Address.new(primary_address)])
    end
  end

  context 'when seed addresses are repeated in URI' do
    require_topology :single

    let(:primary_address) do
      ClusterConfig.instance.primary_address_host
    end

    let(:client) do
      new_local_client("mongodb://#{primary_address},#{primary_address}", SpecConfig.instance.test_options)
    end

    it 'deduplicates the addresses' do
      expect(client.cluster.addresses).to eq([Mongo::Address.new(primary_address)])
    end
  end

  context 'when deployment is not a sharded cluster' do
    require_topology :single, :replica_set

    let(:client) do
      ClientRegistry.instance.new_local_client(
        [SpecConfig.instance.addresses.first],
        SpecConfig.instance.test_options.merge(options),
      )
    end

    context 'when load-balanced topology is requested' do
      let(:options) do
        {connect: :load_balanced, replica_set: nil}
      end

      it 'creates the client successfully' do
        client.should be_a(Mongo::Client)
      end

      it 'fails all operations' do
        lambda do
          client.command(ping: true)
        end.should raise_error(Mongo::Error::BadLoadBalancerTarget)
      end
    end
  end

  context 'when in load-balanced mode' do
    require_topology :load_balanced

    let(:client) do
      ClientRegistry.instance.new_local_client(
        [SpecConfig.instance.addresses.first],
        SpecConfig.instance.test_options.merge(options),
      )
    end

    context 'when load-balanced topology is requested via the URI option' do
      let(:options) do
        {connect: nil, load_balanced: true}
      end

      it 'creates the client successfully' do
        client.should be_a(Mongo::Client)
      end

      it 'fails all operations' do
        lambda do
          client.command(ping: true)
        end.should raise_error(Mongo::Error::MissingServiceId)
      end
    end
  end
end