File: spec_config.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 (767 lines) | stat: -rw-r--r-- 21,375 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
# frozen_string_literal: true
# rubocop:todo all

require 'singleton'
require 'pathname'

class SpecConfig
  include Singleton

  # NB: constructor should not do I/O as SpecConfig may be used by tests
  # only loading the lite spec helper. Do I/O eagerly in accessor methods.
  def initialize
    @uri_options = {}
    @ruby_options = {}
    if ENV['MONGODB_URI']
      @mongodb_uri = Mongo::URI.get(ENV['MONGODB_URI'])
      @uri_options = Mongo::Options::Mapper.transform_keys_to_symbols(@mongodb_uri.uri_options)
      if ENV['TOPOLOGY'] == 'load-balanced'
        @addresses = @mongodb_uri.servers
        @connect_options = { connect: :load_balanced }
      elsif @uri_options[:replica_set]
        @addresses = @mongodb_uri.servers
        @connect_options = { connect: :replica_set, replica_set: @uri_options[:replica_set] }
      elsif @uri_options[:connect] == :sharded || ENV['TOPOLOGY'] == 'sharded-cluster'
        @addresses = @mongodb_uri.servers
        @connect_options = { connect: :sharded }
      elsif @uri_options[:connect] == :direct
        @addresses = @mongodb_uri.servers
        @connect_options = { connect: :direct }
      end
      if @uri_options[:ssl].nil?
        @ssl = (ENV['SSL'] == 'ssl') || (ENV['SSL_ENABLED'] == 'true')
      else
        @ssl = @uri_options[:ssl]
      end
    end

    @uri_tls_options = {}
    @uri_options.each do |k, v|
      k = k.to_s.downcase
      if k.start_with?('ssl')
        @uri_tls_options[k] = v
      end
    end

    @ssl ||= false

    if (server_api = ENV['SERVER_API']) && !server_api.empty?
      @ruby_options[:server_api] = BSON::Document.new(YAML.load(server_api))
      # Since the tests pass options provided by SpecConfig directly to
      # internal driver objects (e.g. connections), transform server api
      # parameters here as they would be transformed by Client constructor.
      if (v = @ruby_options[:server_api][:version]).is_a?(Integer)
        @ruby_options[:server_api][:version] = v.to_s
      end
    end
  end

  attr_reader :uri_options, :ruby_options, :connect_options

  def addresses
    @addresses ||= begin
      if @mongodb_uri
        @mongodb_uri.servers
      else
        client = Mongo::Client.new(['localhost:27017'], server_selection_timeout: 5.02)
        begin
          client.cluster.next_primary
          @addresses = client.cluster.servers_list.map do |server|
            server.address.to_s
          end
        ensure
          client.close
        end
      end
    end
  end

  def connect_options
    @connect_options ||= begin
      # Discover deployment topology.
      # TLS options need to be merged for evergreen due to
      # https://github.com/10gen/mongo-orchestration/issues/268
      client = Mongo::Client.new(addresses, Mongo::Options::Redacted.new(
        server_selection_timeout: 5.03,
      ).merge(ssl_options).merge(ruby_options))

      begin
        case client.cluster.topology.class.name
        when /LoadBalanced/
          { connect: :load_balanced }
        when /Replica/
          { connect: :replica_set, replica_set: client.cluster.topology.replica_set_name }
        when /Sharded/
          { connect: :sharded }
        when /Single/
          { connect: :direct }
        when /Unknown/
          raise "Could not detect topology because the test client failed to connect to MongoDB deployment"
        else
          raise "Weird topology #{client.cluster.topology}"
        end
      ensure
        client.close
      end
    end
  end

  # Environment

  def ci?
    %w(1 true yes).include?(ENV['CI']&.downcase)
  end

  def mri?
    !jruby?
  end

  def jruby?
    !!(RUBY_PLATFORM =~ /\bjava\b/)
  end

  def linux?
    !!(RbConfig::CONFIG['host_os'].downcase =~ /\blinux/)
  end

  def macos?
    !!(RbConfig::CONFIG['host_os'].downcase =~ /\bdarwin/)
  end

  def windows?
    ENV['OS'] == 'Windows_NT' && !RUBY_PLATFORM.match?(/cygwin/)
  end

  def platform
    RUBY_PLATFORM
  end

  def stress?
    %w(1 true yes).include?(ENV['STRESS']&.downcase)
  end

  def fork?
    %w(1 true yes).include?(ENV['FORK']&.downcase)
  end

  # OCSP tests require python and various dependencies.
  # Assumes an OCSP responder is running on port 8100 (configured externally
  # to the test suite).
  def ocsp?
    %w(1 true yes).include?(ENV['OCSP']&.downcase)
  end

  # OCSP tests require python and various dependencies.
  # When testing OCSP verifier, there cannot be a responder running on
  # port 8100 or the tests will fail.
  def ocsp_verifier?
    %w(1 true yes).include?(ENV['OCSP_VERIFIER']&.downcase)
  end

  def ocsp_connectivity?
    ENV.key?('OCSP_CONNECTIVITY') && ENV['OCSP_CONNECTIVITY'] != ''
  end

  # Detect whether specs are running against Mongodb Atlas serverless instance.
  # This method does not do any magic, it just checks whether environment
  # variable SERVERLESS is set. This is a recommended way to inform spec runners
  # that they are running against a serverless instance
  #
  # @return [ true | false ] Whether specs are running against a serverless instance.
  def serverless?
    !!ENV['SERVERLESS']
  end

  def kill_all_server_sessions?
    !serverless? && # Serverless instances do not support killAllSessions command.
      ClusterConfig.instance.fcv_ish >= '3.6'
  end

  # Test suite configuration

  def client_debug?
    %w(1 true yes).include?(ENV['MONGO_RUBY_DRIVER_CLIENT_DEBUG']&.downcase)
  end

  def drivers_tools?
    !!ENV['DRIVERS_TOOLS']
  end

  def active_support?
    %w(1 true yes).include?(ENV['WITH_ACTIVE_SUPPORT'])
  end

  # What compressor to use, if any.
  def compressors
    uri_options[:compressors]
  end

  def retry_reads
    uri_option_or_env_var(:retry_reads, 'RETRY_READS')
  end

  def retry_writes
    uri_option_or_env_var(:retry_writes, 'RETRY_WRITES')
  end

  def uri_option_or_env_var(driver_option_symbol, env_var_key)
    case uri_options[driver_option_symbol]
    when true
      true
    when false
      false
    else
      case (ENV[env_var_key] || '').downcase
      when 'yes', 'true', 'on', '1'
        true
      when 'no', 'false', 'off', '0'
        false
      else
        nil
      end
    end
  end

  def retry_writes?
    if retry_writes == false
      false
    else
      # Current default is to retry writes
      true
    end
  end

  def ssl?
    @ssl
  end

  # Username, not user object
  def user
    @mongodb_uri && @mongodb_uri.credentials[:user]
  end

  def password
    @mongodb_uri && @mongodb_uri.credentials[:password]
  end

  def auth_source
    uri_options[:auth_source]
  end

  def connect_replica_set?
    connect_options[:connect] == :replica_set
  end

  def print_summary
    puts "Connection options: #{test_options}"
    client = ClientRegistry.instance.global_client('basic')
    client.cluster.next_primary
    puts <<-EOT
Topology: #{client.cluster.topology.class}
connect: #{connect_options[:connect]}
EOT
  end

  # Derived data

  def any_port
    addresses.first.split(':')[1] || '27017'
  end

  def spec_root
    File.join(File.dirname(__FILE__), '..')
  end

  def ssl_certs_dir
    Pathname.new("#{spec_root}/support/certificates")
  end

  def ocsp_files_dir
    Pathname.new("#{spec_root}/../.mod/drivers-evergreen-tools/.evergreen/ocsp")
  end

  # TLS certificates & keys

  def local_client_key_path
    "#{ssl_certs_dir}/client.key"
  end

  def client_key_path
    if drivers_tools? && ENV['DRIVER_TOOLS_CLIENT_KEY_PEM']
      ENV['DRIVER_TOOLS_CLIENT_KEY_PEM']
    else
      local_client_key_path
    end
  end

  def local_client_cert_path
    "#{ssl_certs_dir}/client.crt"
  end

  def client_cert_path
    if drivers_tools? && ENV['DRIVER_TOOLS_CLIENT_CERT_PEM']
      ENV['DRIVER_TOOLS_CLIENT_CERT_PEM']
    else
      local_client_cert_path
    end
  end

  def local_client_pem_path
    if (algo = ENV['OCSP_ALGORITHM'])&.empty?
      "#{ssl_certs_dir}/client.pem"
    else
      Pathname.new("#{spec_root}/support/ocsp/#{algo}/server.pem")
    end
  end

  def client_pem_path
    if drivers_tools? && ENV['DRIVER_TOOLS_CLIENT_CERT_KEY_PEM']
      ENV['DRIVER_TOOLS_CLIENT_CERT_KEY_PEM']
    else
      local_client_pem_path
    end
  end

  def client_x509_pem_path
    "#{ssl_certs_dir}/client-x509.pem"
  end

  def second_level_cert_path
    "#{ssl_certs_dir}/client-second-level.crt"
  end

  def second_level_key_path
    "#{ssl_certs_dir}/client-second-level.key"
  end

  def second_level_cert_bundle_path
    "#{ssl_certs_dir}/client-second-level-bundle.pem"
  end

  def local_client_encrypted_key_path
    "#{ssl_certs_dir}/client-encrypted.key"
  end

  def client_encrypted_key_path
    if drivers_tools? && ENV['DRIVER_TOOLS_CLIENT_KEY_ENCRYPTED_PEM']
      ENV['DRIVER_TOOLS_CLIENT_KEY_ENCRYPTED_PEM']
    else
      local_client_encrypted_key_path
    end
  end

  def client_encrypted_key_passphrase
    'passphrase'
  end

  def local_ca_cert_path
    "#{ssl_certs_dir}/ca.crt"
  end

  def ca_cert_path
    if drivers_tools? && ENV['DRIVER_TOOLS_CA_PEM']
      ENV['DRIVER_TOOLS_CA_PEM']
    else
      local_ca_cert_path
    end
  end

  def multi_ca_path
    "#{ssl_certs_dir}/multi-ca.crt"
  end

  # The default test database for all specs.
  def test_db
    'ruby-driver'.freeze
  end

  # Whether FLE tests should be enabled
  def fle?
    %w(1 true yes helper).include?(ENV['FLE']&.downcase)
  end

  # AWS IAM user access key id
  def fle_aws_key
    ENV['MONGO_RUBY_DRIVER_AWS_KEY']
  end

  # AWS IAM user secret access key
  def fle_aws_secret
    ENV['MONGO_RUBY_DRIVER_AWS_SECRET']
  end

  # Region of AWS customer master key
  def fle_aws_region
    ENV['MONGO_RUBY_DRIVER_AWS_REGION']
  end

  # Amazon resource name (ARN) of AWS customer master key
  def fle_aws_arn
    ENV['MONGO_RUBY_DRIVER_AWS_ARN']
  end

  # AWS temporary access key id (set by set-temp-creds.sh)
  def fle_aws_temp_key
    ENV['CSFLE_AWS_TEMP_ACCESS_KEY_ID']
  end

  # AWS temporary secret access key (set by set-temp-creds.sh)
  def fle_aws_temp_secret
    ENV['CSFLE_AWS_TEMP_SECRET_ACCESS_KEY']
  end

  # AWS temporary session token (set by set-temp-creds.sh)
  def fle_aws_temp_session_token
    ENV['CSFLE_AWS_TEMP_SESSION_TOKEN']
  end

  def fle_azure_tenant_id
    ENV['MONGO_RUBY_DRIVER_AZURE_TENANT_ID']
  end

  def fle_azure_client_id
    ENV['MONGO_RUBY_DRIVER_AZURE_CLIENT_ID']
  end

  def fle_azure_client_secret
    ENV['MONGO_RUBY_DRIVER_AZURE_CLIENT_SECRET']
  end

  def fle_azure_identity_platform_endpoint
    ENV['MONGO_RUBY_DRIVER_AZURE_IDENTITY_PLATFORM_ENDPOINT']
  end

  def fle_azure_key_vault_endpoint
    ENV['MONGO_RUBY_DRIVER_AZURE_KEY_VAULT_ENDPOINT']
  end

  def fle_azure_key_name
    ENV['MONGO_RUBY_DRIVER_AZURE_KEY_NAME']
  end

  def fle_gcp_email
    ENV['MONGO_RUBY_DRIVER_GCP_EMAIL']
  end

  def fle_gcp_private_key
    ENV['MONGO_RUBY_DRIVER_GCP_PRIVATE_KEY']
  end

  def fle_gcp_endpoint
    ENV['MONGO_RUBY_DRIVER_GCP_ENDPOINT']
  end

  def fle_gcp_project_id
    ENV['MONGO_RUBY_DRIVER_GCP_PROJECT_ID']
  end

  def fle_gcp_location
    ENV['MONGO_RUBY_DRIVER_GCP_LOCATION']
  end

  def fle_gcp_key_ring
    ENV['MONGO_RUBY_DRIVER_GCP_KEY_RING']
  end

  def fle_gcp_key_name
    ENV['MONGO_RUBY_DRIVER_GCP_KEY_NAME']
  end

  def fle_gcp_key_version
    ENV['MONGO_RUBY_DRIVER_GCP_KEY_VERSION']
  end

  def fle_kmip_endpoint
    "localhost:5698"
  end

  def fle_kmip_tls_ca_file
    "#{spec_root}/../.evergreen/x509gen/ca.pem"
  end

  def fle_kmip_tls_certificate_key_file
    "#{spec_root}/../.evergreen/x509gen/client.pem"
  end

  def mongocryptd_port
    if ENV['MONGO_RUBY_DRIVER_MONGOCRYPTD_PORT'] &&
      !ENV['MONGO_RUBY_DRIVER_MONGOCRYPTD_PORT'].empty?
    then
      ENV['MONGO_RUBY_DRIVER_MONGOCRYPTD_PORT'].to_i
    else
      27020
    end
  end

  def crypt_shared_lib_path
    if @without_crypt_shared_lib_path
      nil
    else
      ENV['MONGO_RUBY_DRIVER_CRYPT_SHARED_LIB_PATH']
    end
  end

  def without_crypt_shared_lib_path
    saved, @without_crypt_shared_lib_path = @without_crypt_shared_lib_path, true
    yield
  ensure
    @without_crypt_shared_lib_path = saved
  end

  attr_accessor :crypt_shared_lib_required

  def require_crypt_shared
    saved, self.crypt_shared_lib_required = crypt_shared_lib_required, true
    yield
  ensure
    self.crypt_shared_lib_required = saved
  end

  def auth?
    x509_auth? || user
  end

  # Option hashes

  def auth_options
    if x509_auth?
      {
        auth_mech: uri_options[:auth_mech],
        auth_source: '$external',
      }
    else
      {
        user: user,
        password: password,
      }.tap do |options|
        if auth_source
          options[:auth_source] = auth_source
        end
        %i(auth_mech auth_mech_properties).each do |key|
          if uri_options[key]
            options[key] = uri_options[key]
          end
        end
      end
    end
  end

  def ssl_options
    return {} unless ssl?
    {
        ssl: true,
        ssl_verify: true,
    }.tap do |options|
      # We should use bundled cetificates for ssl except for testing against
      # Atlas instances. Atlas instances have addresses in domains
      # mongodb.net or mongodb-dev.net.
      if @mongodb_uri.servers.grep(/mongodb.*\.net/).empty?
        options.merge!(
          {
            ssl_cert: client_cert_path,
            ssl_key: client_key_path,
            ssl_ca_cert: ca_cert_path,
          }
        )
      end
    end.merge(Utils.underscore_hash(@uri_tls_options))
  end

  def compressor_options
    if compressors
      {compressors: compressors}
    else
      {}
    end
  end

  def retry_writes_options
    {retry_writes: retry_writes?}
  end

  # The options needed for a successful socket connection to the server(s).
  # These exclude options needed to handshake (e.g. server api parameters).
  def connection_options
    ssl_options
  end

  # The options needed for successful monitoring of the server(s).
  # These exclude options needed to perform operations (e.g. credentials).
  def monitoring_options
    ssl_options.merge(
      server_api: ruby_options[:server_api],
    )
  end

  # Base test options.
  def base_test_options
    {
      # Automatic encryption tests require a minimum of three connections:
      # - The driver checks out a connection to build a command.
      # - It may need to encrypt the command, which could require a query to
      #   the key vault collection triggered by libmongocrypt.
      # - If the key vault client has auto encryption options, it will also
      #   attempt to encrypt this query, resulting in a third connection.
      # In the worst case using FLE may end up tripling the number of
      # connections that the driver uses at any one time.
      max_pool_size: 3,

      heartbeat_frequency: 20,

      # The test suite seems to perform a number of operations
      # requiring server selection. Hence a timeout of 1 here,
      # together with e.g. a misconfigured replica set,
      # means the test suite hangs for about 4 seconds before
      # failing.
      # Server selection timeout of 1 is insufficient for evergreen.
      server_selection_timeout: uri_options[:server_selection_timeout] || (ssl? ? 8.01 : 7.01),

      # Since connections are established under the wait queue timeout,
      # the wait queue timeout should be at least as long as the
      # connect timeout.
      wait_queue_timeout: 6.04,
      connect_timeout: 2.91,
      socket_timeout: 5.09,
      max_idle_time: 100.02,

      # Uncomment to have exceptions in background threads log complete
      # backtraces.
      #bg_error_backtrace: true,
    }.merge(ruby_options).merge(
      server_api: ruby_options[:server_api] && ::Utils.underscore_hash(ruby_options[:server_api])
    )
  end

  # Options for test suite clients.
  def test_options
    base_test_options.merge(connect_options).
      merge(ssl_options).merge(compressor_options).merge(retry_writes_options)
  end

  # TODO auth_options should probably be in test_options
  def all_test_options
    test_options.merge(auth_options)
  end

  def authorized_test_options
    test_options.merge(credentials_or_external_user(
      user: test_user.name,
      password: test_user.password,
      auth_source: auth_options[:auth_source],
    ))
  end

  # User objects

  # Gets the root system administrator user.
  def root_user
    Mongo::Auth::User.new(
      user: user || 'root-user',
      password: password || 'password',
      roles: [
        Mongo::Auth::Roles::USER_ADMIN_ANY_DATABASE,
        Mongo::Auth::Roles::DATABASE_ADMIN_ANY_DATABASE,
        Mongo::Auth::Roles::READ_WRITE_ANY_DATABASE,
        Mongo::Auth::Roles::HOST_MANAGER,
        Mongo::Auth::Roles::CLUSTER_ADMIN
      ]
    )
  end

  # Get the default test user for the suite on versions 2.6 and higher.
  def test_user
    # When testing against a serverless instance, we are not allowed to create
    # new users, we just have one user for everyhing.
    return root_user if serverless?

    Mongo::Auth::User.new(
      database: 'admin',
      user: 'ruby-test-user',
      password: 'password',
      roles: [
        { role: Mongo::Auth::Roles::READ_WRITE, db: test_db },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: test_db },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'invalid_database' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'invalid_database' },

        # For transactions examples
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'hr' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'hr' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'reporting' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'reporting' },

        # For spec tests
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'crud-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'crud-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'crud-default' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'crud-default' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'default_write_concern_db' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'default_write_concern_db' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'retryable-reads-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'retryable-reads-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'sdam-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'sdam-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'transaction-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'transaction-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'withTransaction-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'withTransaction-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'admin' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'admin' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'command-monitoring-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'command-monitoring-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'session-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'session-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'gridfs-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'gridfs-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'change-stream-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'change-stream-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'change-stream-tests-2' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'change-stream-tests-2' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'retryable-writes-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'retryable-writes-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'ts-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'ts-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'ci-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'ci-tests' },
        { role: Mongo::Auth::Roles::READ_WRITE, db: 'papi-tests' },
        { role: Mongo::Auth::Roles::DATABASE_ADMIN, db: 'papi-tests' },
      ]
    )
  end

  def x509_auth?
    uri_options[:auth_mech] == :mongodb_x509
  end

  # When we authenticate with a username & password mechanism (scram, cr)
  # we create a variety of users in the test suite for different purposes.
  # When we authenticate with passwordless mechanisms (x509, aws) we use
  # the globally specified user for all operations.
  def external_user?
    case uri_options[:auth_mech]
    when :mongodb_x509, :aws
      true
    when nil, :scram, :scram256
      false
    else
      raise "Unknown auth mechanism value: #{uri_options[:auth_mech]}"
    end
  end

  # When we use external authentication, omit all of the users we normally
  # create and authenticate with the external mechanism. This also ensures
  # our various helpers work correctly when the only users available are
  # the external ones.
  def credentials_or_external_user(creds)
    if external_user?
      auth_options
    else
      creds
    end
  end

  # Returns whether the test suite was configured with a single mongos.
  def single_mongos?
    %w(1 true yes).include?(ENV['SINGLE_MONGOS'])
  end
end